Skip to content

Commit

Permalink
Implemented update feature for welcome screen
Browse files Browse the repository at this point in the history
  • Loading branch information
giucu91 committed Feb 24, 2017
1 parent ae06d68 commit bf3d3d7
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 8 deletions.
5 changes: 3 additions & 2 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ function illdy_setup() {
* plugin_slug - the plugin's slug (used for installing the plugin)
*
*/

$illdy_required_actions = array(
array(
"id" => 'illdy-req-ac-install-illdy-companion',
"title" => MT_Notify_System::create_plugin_requirement_title( __( 'Install: Illdy Companion', 'illdy' ), __( 'Activate: Illdy Companion', 'illdy' ), 'illdy-companion' ),
"title" => MT_Notify_System::create_plugin_title( __( 'Illdy Companion', 'illdy' ), 'illdy-companion' ),
"description" => __( 'It is highly recommended that you install the Illdy Companion.', 'illdy' ),
"check" => MT_Notify_System::has_import_plugin( 'illdy-companion' ),
"check" => MT_Notify_System::check_plugin_update( 'illdy-companion' ),
"type" => 'plugin',
"plugin_slug" => 'illdy-companion'
),
Expand Down
8 changes: 8 additions & 0 deletions inc/admin/welcome-screen/js/welcome.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
pagenow = 'plugin-install'
jQuery(document).on( 'wp-plugin-update-success', function( evt, response ){
location.reload();
});

jQuery(document).ready(function () {

/* If there are required actions, add an icon with the number of required actions in the About illdy page -> Actions required tab */
Expand Down Expand Up @@ -54,4 +59,7 @@ jQuery(document).ready(function () {
}
});
});



});
13 changes: 13 additions & 0 deletions inc/admin/welcome-screen/sections/actions-required.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
wp_enqueue_style( 'plugin-install' );
wp_enqueue_script( 'plugin-install' );
wp_enqueue_script( 'updates' );
wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
'totals' => wp_get_update_data(),
) );
?>

<div class="feature-section action-required demo-import-boxed" id="plugin-filter">
Expand Down Expand Up @@ -53,6 +56,11 @@
if ( !isset($active['plugin_path']) ) {
$active['plugin_path'] = '';
}

if ( $active['needs'] == 'deactivate' && !MT_Notify_System::check_plugin_update( $illdy_required_action_value['plugin_slug'] ) ) {
$active['needs'] = 'update';
}

$url = $this->create_action_link( $active['needs'], $illdy_required_action_value['plugin_slug'], $active['plugin_path'] );
$label = '';

Expand All @@ -65,6 +73,10 @@
$class = 'activate-now button button-primary';
$label = __( 'Activate', 'illdy' );
break;
case 'update':
$class = 'update-now button button-primary';
$label = __( 'Update', 'illdy' );
break;
case 'deactivate':
$class = 'deactivate-now button';
$label = __( 'Deactivate', 'illdy' );
Expand All @@ -74,6 +86,7 @@
?>
<p class="plugin-card-<?php echo esc_attr( $illdy_required_action_value['plugin_slug'] ) ?> action_button <?php echo ( $active['needs'] !== 'install' && $active['status'] ) ? 'active' : '' ?>">
<a data-slug="<?php echo esc_attr( $illdy_required_action_value['plugin_slug'] ) ?>"
data-plugin = "<?php echo esc_attr( $active['plugin_path'] ) ?>"
class="<?php echo $class; ?>"
href="<?php echo esc_url( $url ) ?>"> <?php echo $label ?> </a>
</p>
Expand Down
18 changes: 18 additions & 0 deletions inc/admin/welcome-screen/welcome-screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ public function illdy_welcome_admin_notice() {
public function illdy_welcome_style_and_scripts( $hook_suffix ) {

wp_enqueue_style( 'illdy-welcome-screen-css', get_template_directory_uri() . '/inc/admin/welcome-screen/css/welcome.css' );

$screen = get_current_screen();
if ( $screen->base != 'appearance_page_illdy-welcome' ) {
return;
}

wp_enqueue_script( 'illdy-welcome-screen-js', get_template_directory_uri() . '/inc/admin/welcome-screen/js/welcome.js', array( 'jquery' ) );

wp_localize_script( 'illdy-welcome-screen-js', 'illdyWelcomeScreenObject', array(
Expand Down Expand Up @@ -369,6 +375,18 @@ public function create_action_link( $state, $slug, $plugin_path = '' ) {
'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin_path ),
), network_admin_url( 'plugins.php' ) );
break;
case 'update':
return wp_nonce_url(
add_query_arg(
array(
'action' => 'upgrade-plugin',
'plugin' => rawurlencode( $plugin_path )
),
network_admin_url( 'update.php' )
),
'upgrade-plugin_' . $plugin_path
);
break;
}
}

Expand Down
4 changes: 4 additions & 0 deletions inc/customizer/assets/js/illdy-customizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
*
*/

jQuery(document).on( 'wp-plugin-update-success', function( evt, response ){
location.reload();
});

( function( api ) {

// Extends our custom "illdy-pro-section" section.
Expand Down
31 changes: 28 additions & 3 deletions inc/customizer/custom-recommend-action-section.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ public function create_action_link( $state, $slug, $plugin_path = '' ) {
'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin_path ),
), network_admin_url( 'plugins.php' ) );
break;
case 'update':
return wp_nonce_url(
add_query_arg(
array(
'action' => 'upgrade-plugin',
'plugin' => rawurlencode( $plugin_path )
),
network_admin_url( 'update.php' )
),
'upgrade-plugin_' . $plugin_path
);
break;
}
}

Expand Down Expand Up @@ -138,6 +150,11 @@ public function json() {
if ( !isset($active['plugin_path']) ) {
$active['plugin_path'] = '';
}

if ( $active['needs'] == 'deactivate' && !MT_Notify_System::check_plugin_update( $illdy_required_action['plugin_slug'] ) ) {
$active['needs'] = 'update';
}

$illdy_required_action['url'] = $this->create_action_link( $active['needs'], $illdy_required_action['plugin_slug'], $active['plugin_path'] );
if ( $active['needs'] !== 'install' && $active['status'] ) {
$illdy_required_action['class'] = 'active';
Expand All @@ -154,12 +171,18 @@ public function json() {
$illdy_required_action['button_class'] = 'activate-now button button-primary';
$illdy_required_action['button_label'] = __( 'Activate', 'illdy' );
break;
case 'update':
$illdy_required_action['button_class'] = 'update-now button button-primary';
$illdy_required_action['button_label'] = __( 'Update', 'illdy' );
break;
case 'deactivate':
$illdy_required_action['button_class'] = 'deactivate-now button';
$illdy_required_action['button_label'] = __( 'Deactivate', 'illdy' );
break;
}

$illdy_required_action['path'] = $active['plugin_path'];

}
$formatted_array[] = $illdy_required_action;
}
Expand Down Expand Up @@ -204,6 +227,7 @@ public function json() {
}
$info = $this->call_plugin_api( $slug );
$illdy_recommended_plugin['id'] = $slug;
$illdy_recommended_plugin['path'] = $active['plugin_path'];
$illdy_recommended_plugin['plugin_slug'] = $slug;
$illdy_recommended_plugin['description'] = $info->short_description;
$illdy_recommended_plugin['title'] = $illdy_recommended_plugin['button_label'].': '.$info->name;
Expand Down Expand Up @@ -272,9 +296,10 @@ protected function render_template() { ?>
<# if( data.required_actions[action].plugin_slug ){ #>
<div class="custom-action">
<p class="plugin-card-{{ data.required_actions[action].plugin_slug }} action_button {{ data.required_actions[action].class }}">
<a data-slug="{{ data.required_actions[action].plugin_slug }}"
class="{{ data.required_actions[action].button_class }}"
href="{{ data.required_actions[action].url }}">{{ data.required_actions[action].button_label }}</a>
<a data-slug="{{ data.required_actions[action].plugin_slug }}"
data-plugin="{{ data.required_actions[action].path }}"
class="{{ data.required_actions[action].button_class }}"
href="{{ data.required_actions[action].url }}">{{ data.required_actions[action].button_label }}</a>
</p>
</div>
<# } #>
Expand Down
5 changes: 4 additions & 1 deletion inc/customizer/customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ function illdy_customizer_js_load() {
wp_enqueue_style( 'plugin-install' );
wp_enqueue_script( 'plugin-install' );
wp_enqueue_script( 'updates' );
wp_add_inline_script( 'plugin-install', 'var pagenow = "customizer";' );
wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
'totals' => wp_get_update_data(),
) );
wp_add_inline_script( 'plugin-install', 'var pagenow = "plugin-install";' );
wp_enqueue_script( 'illdy-customizer', get_template_directory_uri() . '/inc/customizer/assets/js/illdy-customizer.js', array( 'customize-controls' ), '1.0', true );

$IlldyCustomizer = array();
Expand Down
49 changes: 47 additions & 2 deletions inc/notify-system-checks.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function has_widgets() {
/**
* @return bool
*/
public static function newmsag_has_posts() {
public static function illdy_has_posts() {
$args = array( "s" => 'Gary Johns: \'What is Aleppo\'' );
$query = get_posts( $args );

Expand All @@ -75,7 +75,7 @@ public static function newmsag_has_posts() {
public static function has_content() {
$check = array(
'widgets' => self::has_widgets(),
'posts' => self::newmsag_has_posts(),
'posts' => self::illdy_has_posts(),
);

if ( $check['widgets'] && $check['posts'] ) {
Expand Down Expand Up @@ -140,6 +140,40 @@ public static function has_import_plugin( $slug = NULL ) {
return true;
}

public static function check_plugin_need_update( $slug ) {

$update_plugin_transient = get_site_transient('update_plugins');

if ( isset($update_plugin_transient->response) ) {
$plugins = $update_plugin_transient->response;

foreach ( $plugins as $key => $plugin ) {
if ( preg_match( '|^' . $slug . '/|', $key ) ) {
return false;
}
}
}

return true;

}

public static function check_plugin_update( $slug ) {

$check = array(
'installed' => self::check_plugin_is_installed( $slug ),
'active' => self::check_plugin_is_active( $slug ),
'update' => self::check_plugin_need_update( $slug )
);

if ( ! $check['installed'] || ! $check['active'] || ! $check['update'] ) {
return false;
}

return true;

}

public static function has_import_plugins() {
$check = array(
'wordpress-importer' => array( 'installed' => false, 'active' => false ),
Expand Down Expand Up @@ -256,6 +290,17 @@ public static function create_plugin_requirement_title( $install_text, $activate

}

public static function create_plugin_title( $plugin_title, $plugin_slug ){
$installed = self::check_plugin_is_installed( $plugin_slug );
if ( ! $installed ) {
return __( 'Install : ', 'illdy' ).$plugin_title;
}elseif ( ! self::check_plugin_is_active( $plugin_slug ) && $installed ) {
return __( 'Activate : ', 'illdy' ).$plugin_title;
}else{
return __( 'Update : ', 'illdy' ).$plugin_title;
}
}

/**
* @return bool
*/
Expand Down

0 comments on commit bf3d3d7

Please sign in to comment.