From bf3d3d77a35e50c17aa2de214f98fbc117ecd454 Mon Sep 17 00:00:00 2001 From: Giucu Date: Fri, 24 Feb 2017 15:10:49 +0200 Subject: [PATCH] Implemented update feature for welcome screen --- functions.php | 5 +- inc/admin/welcome-screen/js/welcome.js | 8 +++ .../sections/actions-required.php | 13 +++++ inc/admin/welcome-screen/welcome-screen.php | 18 +++++++ inc/customizer/assets/js/illdy-customizer.js | 4 ++ .../custom-recommend-action-section.php | 31 ++++++++++-- inc/customizer/customizer.php | 5 +- inc/notify-system-checks.php | 49 ++++++++++++++++++- 8 files changed, 125 insertions(+), 8 deletions(-) diff --git a/functions.php b/functions.php index 21ee6e5..2795405 100755 --- a/functions.php +++ b/functions.php @@ -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' ), diff --git a/inc/admin/welcome-screen/js/welcome.js b/inc/admin/welcome-screen/js/welcome.js index bf37964..8395afe 100755 --- a/inc/admin/welcome-screen/js/welcome.js +++ b/inc/admin/welcome-screen/js/welcome.js @@ -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 */ @@ -54,4 +59,7 @@ jQuery(document).ready(function () { } }); }); + + + }); diff --git a/inc/admin/welcome-screen/sections/actions-required.php b/inc/admin/welcome-screen/sections/actions-required.php index d9fd63e..b798282 100755 --- a/inc/admin/welcome-screen/sections/actions-required.php +++ b/inc/admin/welcome-screen/sections/actions-required.php @@ -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(), + ) ); ?>
@@ -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 = ''; @@ -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' ); @@ -74,6 +86,7 @@ ?>

diff --git a/inc/admin/welcome-screen/welcome-screen.php b/inc/admin/welcome-screen/welcome-screen.php index 44aeaaa..8c81e04 100755 --- a/inc/admin/welcome-screen/welcome-screen.php +++ b/inc/admin/welcome-screen/welcome-screen.php @@ -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( @@ -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; } } diff --git a/inc/customizer/assets/js/illdy-customizer.js b/inc/customizer/assets/js/illdy-customizer.js index 1f4262d..8736ce5 100644 --- a/inc/customizer/assets/js/illdy-customizer.js +++ b/inc/customizer/assets/js/illdy-customizer.js @@ -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. diff --git a/inc/customizer/custom-recommend-action-section.php b/inc/customizer/custom-recommend-action-section.php index 3e41355..abf32d0 100644 --- a/inc/customizer/custom-recommend-action-section.php +++ b/inc/customizer/custom-recommend-action-section.php @@ -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; } } @@ -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'; @@ -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; } @@ -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; @@ -272,9 +296,10 @@ protected function render_template() { ?> <# if( data.required_actions[action].plugin_slug ){ #> <# } #> diff --git a/inc/customizer/customizer.php b/inc/customizer/customizer.php index 988a342..4561967 100755 --- a/inc/customizer/customizer.php +++ b/inc/customizer/customizer.php @@ -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(); diff --git a/inc/notify-system-checks.php b/inc/notify-system-checks.php index 4b22daa..18020c9 100755 --- a/inc/notify-system-checks.php +++ b/inc/notify-system-checks.php @@ -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 ); @@ -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'] ) { @@ -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 ), @@ -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 */