Skip to content

Commit

Permalink
Update Plugin_Card_Helper & Notices
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekPress committed Nov 4, 2019
1 parent b43772b commit ddefb43
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 256 deletions.
1 change: 0 additions & 1 deletion Heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,4 @@ public function maybe_modify( $settings ) {

return $settings;
}

}
19 changes: 8 additions & 11 deletions Notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Heartbeat_Control;

defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' );

/**
* Simple notification flashbag
*/
Expand All @@ -28,7 +29,7 @@ class Notices {
* @var string
* @access protected
*/
protected $transient = 'heartbeat-contol_notices';
protected $transient;

/**
* Store notices.
Expand Down Expand Up @@ -63,16 +64,11 @@ public static function get_instance() {

/**
* Constructor.
*
* @param string $transient overwrite default transient value.
*/
public function __construct( $transient = null ) {
if ( ! is_null( $transient ) ) {
$this->transient = $transient;
}

$this->notices = get_transient( $this->transient );
$this->user_id = get_current_user_id();
public function __construct() {
$this->transient = basename( plugin_dir_path( __FILE__ ) ) . '_notices';
$this->notices = get_transient( $this->transient );
$this->user_id = get_current_user_id();
}

/**
Expand Down Expand Up @@ -105,9 +101,10 @@ public function append( $class, $notice ) {
public function echo_notices( $trash = true ) {
if ( $this->notices ) {
$notices = json_decode( $this->notices, true );

if ( isset( $notices[ $this->user_id ] ) ) {
foreach ( $notices[ $this->user_id ] as $n ) {
echo '<div class="notice notice-' . esc_attr( $n['class'] ) . ' is-dismissible"><p>' . esc_html( $n['notice'] ) . '</p></div>';
echo '<div class="notice notice-' . esc_attr( $n['class'] ) . ' is-dismissible"><p><strong>' . esc_html( $n['notice'] ) . '</strong></p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput
}

if ( $trash ) {
Expand Down
132 changes: 63 additions & 69 deletions Plugin_Card_Helper.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
<?php
/**
* Contains the Heartbeat_Control\Plugin_Card_Helper class.
* This check plugin info from plugins_api and help to build a functional installation plugin card
*
* @package Heartbeat_Control
*/

namespace Heartbeat_Control;

defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' );

/**
* Class Plugin_Card_Helper
* This check plugin info from plugins_api and help to build a functional installation plugin card
* This check plugin info from plugins_api and help to build a functional installation plugin card.
*
* @package Heartbeat_Control
*/
Expand Down Expand Up @@ -113,22 +106,6 @@ class Plugin_Card_Helper {
'install_url' => null,
);

/**
* Store a callback to overwrite default templating behavior.
*
* @var callable
* @access protected
*/
protected $helper_callback;

/**
* Store and pass variables to the template.
*
* @var array
* @access protected
*/
protected $template_args;

/**
* Is this card have been initialised.
*
Expand All @@ -143,10 +120,9 @@ class Plugin_Card_Helper {
* Else in some context install and activation route will not be register.
*
* @param array $args Required index plugin_slug. Use this array to pass param (force_activation active and install).
* @param mixed $template_args What ever param you want to pass for the template.
* @return void
*/
public function __construct( $args = null, $template_args = null ) {
public function __construct( $args = null ) {
$this->args = wp_parse_args(
$args,
array(
Expand All @@ -159,8 +135,11 @@ public function __construct( $args = null, $template_args = null ) {
return;
}

$this->plugin_slug = preg_replace( '@[^a-z0-9_-]@', '', strtolower( (string) $this->args['plugin_slug'] ) );
$this->template_args = $template_args;
$this->plugin_slug = preg_replace( '@[^a-z0-9_-]@', '', strtolower( (string) $this->args['plugin_slug'] ) );

if ( isset( $this->args['params'] ) ) {
$this->params = wp_parse_args( $this->args['params'], $this->params );
}

if ( ! $this->is_installed() ) {
add_action( 'admin_post_install_plugin_' . $this->plugin_slug, array( $this, 'install_callback' ) );
Expand Down Expand Up @@ -284,9 +263,9 @@ public function get_status() {
public function get_status_text( $status = null ) {
$s = ( is_string( $status ) && ! empty( $status ) ) ? $status : $this->get_status();
$st = array(
'activated' => __( 'activated', 'heartbeat-control' ),
'installed' => __( 'installed', 'heartbeat-control' ),
'not_installed' => __( 'not installed', 'heartbeat-control' ),
'activated' => __( 'activated' ),
'installed' => __( 'installed' ),
'not_installed' => __( 'not installed' ),
);
if ( isset( $this->params['status_text'][ $s ] ) ) {
return $this->params['status_text'][ $s ];
Expand All @@ -303,9 +282,9 @@ public function get_status_text( $status = null ) {
public function get_button_text( $status = null ) {
$s = ( is_string( $status ) && ! empty( $status ) ) ? $status : $this->get_status();
$bt = array(
'activated' => __( 'Already activated', 'heartbeat-control' ),
'installed' => __( 'Activate plugin', 'heartbeat-control' ),
'not_installed' => __( 'Install plugin', 'heartbeat-control' ),
'activated' => __( 'Already activated' ),
'installed' => __( 'Activate plugin' ),
'not_installed' => __( 'Install plugin' ),
);

if ( isset( $this->params['button_text'][ $s ] ) ) {
Expand Down Expand Up @@ -458,18 +437,6 @@ public function set_button_text( $array ) {
}
}

/**
* Override helper default behavior with a callable.
*
* @param callable $callback The callable.
* @return void
*/
public function set_helper_callback( $callback ) {
if ( is_callable( $callback ) ) {
$this->helper_callback = $callback;
}
}

// -- Install and activation route and logic

/**
Expand Down Expand Up @@ -502,9 +469,23 @@ public function install_callback() {
wp_safe_redirect( wp_get_referer() );
}

$notices->append( 'success', __( 'This plugin has been successfully installed and activated.', 'heartbeat-control' ) );
$notices->append(
'success',
sprintf(
// translators: %1$s: plugin title.
esc_html__( '%1$s has been successfully installed and activated.' ),
$this->get_title()
)
);
} else {
$notices->append( 'success', __( 'This plugin has been successfully installed.', 'heartbeat-control' ) );
$notices->append(
'success',
sprintf(
// translators: %1$s: plugin title.
esc_html__( '%1$s has been successfully installed.' ),
$this->get_title()
)
);
}

wp_safe_redirect( wp_get_referer() );
Expand Down Expand Up @@ -533,7 +514,14 @@ public function activate_callback() {
wp_safe_redirect( wp_get_referer() );
}

$notices->append( 'success', __( 'This plugin has been successfully activated.', 'heartbeat-control' ) );
$notices->append(
'success',
sprintf(
// translators: %1$s: plugin title.
esc_html__( '%1$s has been successfully activated.' ),
$this->get_title()
)
);

wp_safe_redirect( wp_get_referer() );
}
Expand Down Expand Up @@ -610,11 +598,7 @@ public function helper( $echo = true ) {
ob_start();
}

if ( is_callable( $this->helper_callback ) ) {
call_user_func( $this->helper_callback, $this );
} else {
$this->default_helper();
}
$this->render_helper();

if ( false === $echo ) {
$r = ob_get_contents();
Expand All @@ -628,20 +612,31 @@ public function helper( $echo = true ) {
*
* @return void
*/
protected function default_helper() {
$template_args = $this->template_args;
$helper = $this;
$file_paths = array(
HBC_PLUGIN_PATH . 'views/plugin-cards/' . $this->plugin_slug . '.php',
HBC_PLUGIN_PATH . 'views/plugin-cards/default.php',
);

foreach ( $file_paths as $fp ) {
if ( file_exists( $fp ) ) {
include $fp;
break;
}
}
protected function render_helper() { ?>
<div class="card single-link">
<div class="link-infos">
<div class="link-infos-logo"><?php echo $this->get_icon(); // phpcs:ignore WordPress.Security.EscapeOutput ?></div>
<span class="link-infos-txt">
<h3><?php echo esc_html( $this->get_title() ); ?></h3>
<p>
<?php
printf(
// translators: %1$s: status (not installed, installed or activated).
esc_html__( 'Status : %1$s' ),
esc_html( $this->get_status_text() )
);
?>
</p>
</span>
</div>
<div class="link-content"><?php echo $this->get_description(); // phpcs:ignore WordPress.Security.EscapeOutput ?></div>
<?php if ( 'activated' === $this->get_status() ) : ?>
<span class="wrapper-infos-active"><span class="dashicons dashicons-yes"></span><span class="info-active"><?php echo esc_html( $this->get_button_text() ); ?></span></span>
<?php else : ?>
<a class="link-btn button-primary referer-link <?php echo esc_attr( $this->get_status() ); ?>" href="<?php echo esc_url( $this->get_install_url() ); ?>"><?php echo esc_html( $this->get_button_text() ); ?></a>
<?php endif; ?>
</div>
<?php
}

// -- tools
Expand All @@ -664,5 +659,4 @@ public function get_current_url() {

return 'http' . ( is_ssl() ? 's' : '' ) . '://' . $_http_host . $port . $url;
}

}
47 changes: 41 additions & 6 deletions Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* Admin page handler class
*/
class Settings {

/**
* A array of plugin card.
*
Expand All @@ -33,12 +32,48 @@ public function __construct() {
$imagify_partner = new Imagify_Partner( 'heartbeat-control' );
$imagify_partner->init();
$this->plugins_block = array(
'rocket-lazy-load' => new Plugin_Card_Helper( array( 'plugin_slug' => 'rocket-lazy-load' ) ),
'wp-rocket' => new Plugin_Card_Helper( array( 'plugin_slug' => 'wp-rocket' ) ),
'imagify' => new Plugin_Card_Helper(
array( 'plugin_slug' => 'imagify' ),
'rocket-lazy-load' => new Plugin_Card_Helper(
array(
'plugin_slug' => 'rocket-lazy-load',
'params' => array(
'title' => 'LazyLoad',
),
)
),
'wp-rocket' => new Plugin_Card_Helper(
array(
'plugin_slug' => 'wp-rocket',
'params' => array(
'icon' => '<img src="' . HBC_PLUGIN_URL . 'assets/img/logo-rocket.jpg" alt="">',
'title' => 'WP Rocket',
'description' => sprintf(
// translators: %1$s %2$s: link markup.
esc_html__( 'Integrate more than 80&#x25; of web performance good practices automatically to %1$sreduce your website\'s loading time.%2$s', 'upload-max-file-size' ),
'<strong>',
'</strong>'
),
'install_url' => array(
'not_installed' => 'https://wp-rocket.me/?utm_source=wp_plugin&utm_medium=upload_max_file_size',
),
'button_text' => array(
'not_installed' => __( 'Get WP Rocket', 'upload-max-file-size' ),
),
),
)
),
'imagify' => new Plugin_Card_Helper(
array(
'imagify_partner' => $imagify_partner,
'plugin_slug' => 'imagify',
'params' => array(
'title' => 'Imagify',
'description' => sprintf(
// translators: %1$s: line break, %2$s %3$s: bold markup.
esc_html__( '%2$sReduces image file sizes%3$s without losing quality.%1$sBy compressing your images you speed up your website and boost your SEO.', 'upload-max-file-size' ),
'<br>',
'<strong>',
'</strong>'
),
),
)
),
);
Expand Down
3 changes: 2 additions & 1 deletion heartbeat-control.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
define( 'HBC_VERSION', '2.0' );
define( 'HBC_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'HBC_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
$loader = require dirname( __FILE__ ) . '/vendor/autoload.php';

require_once dirname( __FILE__ ) . '/vendor/autoload.php';

/**
* The primary Heartbeat Control class.
Expand Down
10 changes: 6 additions & 4 deletions views/admin-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' );

global $wp_version;
$heading_tag = version_compare( $wp_version, '4.3' ) >= 0 ? 'h1' : 'h2';
$notices->echo_notices();
Expand Down Expand Up @@ -62,22 +63,23 @@
<?php
printf(
// translators: %1$s: line break, %2$s %3$s: bold markup.
esc_html__( 'Looking for more optimization? %1$sThen you should use %2$sWP Rocket%3$s, and your site will be cached and optimized without you lifting a finger!', 'heartbeat-control' ),
esc_html__( 'Looking for more optimization?%1$sThen you should use %2$sWP Rocket%3$s, and your site will be cached and optimized without you lifting a finger!', 'heartbeat-control' ),
'<br>',
'<strong>',
'</strong>'
);
?>
</p>
</div>
<?php if ( 'installed' === $plugins_block['wp-rocket']->get_status() ) : ?>
<?php if ( 'installed' === $plugins_block['wp-rocket']->get_status() ) : ?>
<a class="btn referer-link <?php echo esc_attr( $plugins_block['wp-rocket']->get_status() ); ?>" href="<?php echo esc_url( $plugins_block['wp-rocket']->get_install_url() ); ?>">
<?php esc_html_e( 'Activate WP Rocket', 'heartbeat-control' ); ?>
</a>
<?php else : ?>
<?php else : ?>
<a href="https://wp-rocket.me/?utm_source=wp_plugin&utm_medium=heartbeat_control" class="btn" target="_blank" rel="noopener">
<?php esc_html_e( 'Get Wp Rocket', 'heartbeat-control' ); ?>
</a>
<?php endif; ?>
<?php endif; ?>
<div class="wrapper-img"></div>
</div>
<div class="wrapper-right">
Expand Down
Loading

0 comments on commit ddefb43

Please sign in to comment.