From 739132eea9841cbc0498fe76782ee58a2c65d3ee Mon Sep 17 00:00:00 2001 From: Jip Moors Date: Thu, 16 Jun 2016 13:32:35 +0200 Subject: [PATCH 1/5] Refactored to make it more readable Moved __FILE__ usage to define, this allows for class to be moved to own file in the future --- yoast-acf-analysis.php | 97 ++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 37 deletions(-) diff --git a/yoast-acf-analysis.php b/yoast-acf-analysis.php index d04e115d..d286ef0e 100644 --- a/yoast-acf-analysis.php +++ b/yoast-acf-analysis.php @@ -13,6 +13,10 @@ exit; } +if ( ! defined( 'YOAST_ACF_ANALYSIS_FILE ' ) ) { + define( 'YOAST_ACF_ANALYSIS_FILE', __FILE__ ); +} + /** * Class Yoast_ACF_Analysis * @@ -45,50 +49,37 @@ public function admin_init() { $notice_functions = array(); - // ACF - if ( ! class_exists( 'acf' ) && ! is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) { - $notice_functions[] = 'acf_not_active_notification'; - } - - // Yoast SEO for WordPress - if ( ! defined( 'WPSEO_VERSION' ) ) { - $notice_functions[] = 'wordpress_seo_requirements_not_met'; + // Check for: Yoast SEO for WordPress. + if ( defined( 'WPSEO_VERSION' ) ) { + // Make sure that version is >= 3.1 + if ( version_compare( substr( WPSEO_VERSION, 0, 3 ), '3.1', '<' ) ) { + $notice_functions[] = 'wordpress_seo_requirements_not_met'; + } } - // Make sure that version is >= 3.1 - else if ( version_compare( substr( WPSEO_VERSION, 0, 3 ), '3.1', '<' ) ) { + else { $notice_functions[] = 'wordpress_seo_requirements_not_met'; } - // Stop here if we cannot do the job we are hired to do. - if ( ! empty( $notice_functions ) ) { - // Deactivate if installed as a plugin. - if ( current_user_can( 'activate_plugins' ) && is_plugin_active( plugin_basename( __FILE__ ) ) ) { - foreach ( $notice_functions as $function ) { - add_action( 'admin_notices', array( $this, $function ) ); - } - unset( $function ); - - $file = plugin_basename( __FILE__ ); - - deactivate_plugins( $file, false, is_network_admin() ); + // Check for: ACF. + if ( ! class_exists( 'acf' ) && ! is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) { + $notice_functions[] = 'acf_not_active_notification'; + } - // Add to recently active plugins list. - if ( ! is_network_admin() ) { - update_option( 'recently_activated', array( $file => time() ) + (array) get_option( 'recently_activated' ) ); - } else { - update_site_option( 'recently_activated', array( $file => time() ) + (array) get_site_option( 'recently_activated' ) ); - } + // Enqueue when no problems were found. + if ( empty( $notice_functions ) ) { + add_filter( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); + } + else { + // Show notices to users who can act on them. + if ( current_user_can( 'activate_plugins' ) ) { + $this->show_notices( $notice_functions ); - // Prevent trying again on page reload. - if ( isset( $_GET['activate'] ) ) { - unset( $_GET['activate'] ); + // Deactivate this plugin if we are not a mu-plugin. + if ( is_plugin_active( YOAST_ACF_ANALYSIS_FILE ) ) { + $this->deactivate(); } } } - else { - // Only enqueue when all requirements are met. - add_filter( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); - } } /** @@ -116,7 +107,7 @@ public function enqueue_scripts() { // Post page enqueue. wp_enqueue_script( 'yoast-acf-analysis-post', - plugins_url( '/js/yoast-acf-analysis.js', __FILE__ ), + plugins_url( '/js/yoast-acf-analysis.js', YOAST_ACF_ANALYSIS_FILE ), array( 'jquery', 'wp-seo-post-scraper', @@ -127,7 +118,7 @@ public function enqueue_scripts() { // Term page enqueue. wp_enqueue_script( 'yoast-acf-analysis-term', - plugins_url( '/js/yoast-acf-analysis.js', __FILE__ ), + plugins_url( '/js/yoast-acf-analysis.js', YOAST_ACF_ANALYSIS_FILE ), array( 'jquery', 'wp-seo-term-scraper', @@ -220,6 +211,38 @@ private function get_field_data( $fields ) { return trim( $output ); } + + /** + * Show the notices that are queued + * + * @param array $notice_functions Array of functions to call. + */ + private function show_notices( $notice_functions ) { + foreach ( $notice_functions as $function ) { + add_action( 'admin_notices', array( $this, $function ) ); + } + } + + /** + * Deactivate this plugin + */ + private function deactivate() { + $file = plugin_basename( YOAST_ACF_ANALYSIS_FILE ); + deactivate_plugins( $file, false, is_network_admin() ); + + // Add to recently active plugins list. + if ( ! is_network_admin() ) { + update_option( 'recently_activated', array( $file => $_SERVER['REQUEST_TIME'] ) + (array) get_option( 'recently_activated' ) ); + } + else { + update_site_option( 'recently_activated', array( $file => $_SERVER['REQUEST_TIME'] ) + (array) get_site_option( 'recently_activated' ) ); + } + + // Prevent trying again on page reload. + if ( isset( $_GET['activate'] ) ) { + unset( $_GET['activate'] ); + } + } } new Yoast_ACF_Analysis(); From 9ae133f4c4bf1db785a99cb177435ca9f925e12d Mon Sep 17 00:00:00 2001 From: Jip Moors Date: Thu, 16 Jun 2016 13:59:34 +0200 Subject: [PATCH 2/5] Unified plugin name to new format. Fixed ''plugin_basename" for plugin active check --- yoast-acf-analysis.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yoast-acf-analysis.php b/yoast-acf-analysis.php index d286ef0e..c17061b9 100644 --- a/yoast-acf-analysis.php +++ b/yoast-acf-analysis.php @@ -1,6 +1,6 @@ show_notices( $notice_functions ); // Deactivate this plugin if we are not a mu-plugin. - if ( is_plugin_active( YOAST_ACF_ANALYSIS_FILE ) ) { + if ( is_plugin_active( plugin_basename( YOAST_ACF_ANALYSIS_FILE ) ) ) { $this->deactivate(); } } From 47dce76519c705a492911d8137419c5aa1d79be1 Mon Sep 17 00:00:00 2001 From: Jip Moors Date: Thu, 16 Jun 2016 14:17:25 +0200 Subject: [PATCH 3/5] String change: "ACF Yoast Analysis" to "Yoast SEO: ACF Analysis" --- yoast-acf-analysis.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yoast-acf-analysis.php b/yoast-acf-analysis.php index c17061b9..01fbab97 100644 --- a/yoast-acf-analysis.php +++ b/yoast-acf-analysis.php @@ -86,7 +86,7 @@ public function admin_init() { * Notify that we need ACF to be installed and active. */ public function acf_not_active_notification() { - $message = __( 'ACF Yoast Analysis requires Advanced Custom Fields (free or pro) to be installed and activated.', 'yoast-acf-analysis' ); + $message = __( 'Yoast SEO: ACF Analysis requires Advanced Custom Fields (free or pro) to be installed and activated.', 'yoast-acf-analysis' ); printf( '

%s

', esc_html( $message ) ); } @@ -95,7 +95,7 @@ public function acf_not_active_notification() { * Notify that we need Yoast SEO for WordPress to be installed and active. */ public function wordpress_seo_requirements_not_met() { - $message = __( 'ACF Yoast Analysis requires Yoast SEO for WordPress 3.1+ to be installed and activated.', 'yoast-acf-analysis' ); + $message = __( 'Yoast SEO: ACF Analysis requires Yoast SEO for WordPress 3.1+ to be installed and activated.', 'yoast-acf-analysis' ); printf( '

%s

', esc_html( $message ) ); } From 51724bd8e2921f53bf45d05dee15d78503044746 Mon Sep 17 00:00:00 2001 From: Jip Moors Date: Thu, 16 Jun 2016 14:36:41 +0200 Subject: [PATCH 4/5] Fixed script dependencies Added check if script is loaded instead of forcing dependencies to be loaded on all pages --- yoast-acf-analysis.php | 64 +++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/yoast-acf-analysis.php b/yoast-acf-analysis.php index 01fbab97..678dc985 100644 --- a/yoast-acf-analysis.php +++ b/yoast-acf-analysis.php @@ -67,7 +67,8 @@ public function admin_init() { // Enqueue when no problems were found. if ( empty( $notice_functions ) ) { - add_filter( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); + // Make sure we load very late to be able to check enqueue of scripts we depend on. + add_filter( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 999 ); } else { // Show notices to users who can act on them. @@ -86,45 +87,58 @@ public function admin_init() { * Notify that we need ACF to be installed and active. */ public function acf_not_active_notification() { - $message = __( 'Yoast SEO: ACF Analysis requires Advanced Custom Fields (free or pro) to be installed and activated.', 'yoast-acf-analysis' ); - printf( '

%s

', esc_html( $message ) ); + $message = sprintf( + __( 'Please %1$sinstall & activate Advanced Custom Fields%2$s to use Yoast SEO: ACF Analysis.', 'yoast-acf-analysis' ), + '', + '' + ); + + printf( '

%s

', $message ); } /** * Notify that we need Yoast SEO for WordPress to be installed and active. */ public function wordpress_seo_requirements_not_met() { - $message = __( 'Yoast SEO: ACF Analysis requires Yoast SEO for WordPress 3.1+ to be installed and activated.', 'yoast-acf-analysis' ); + $message = sprintf( + __( 'Please %1$sinstall & activate Yoast SEO 3.1+%2$s to use Yoast SEO: ACF Analysis.', 'yoast-acf-analysis' ), + '', + '' + ); - printf( '

%s

', esc_html( $message ) ); + printf( '

%s

', $message ); } /** * Enqueue JavaScript file to feed data to Yoast Content Analyses. */ public function enqueue_scripts() { - // Post page enqueue. - wp_enqueue_script( - 'yoast-acf-analysis-post', - plugins_url( '/js/yoast-acf-analysis.js', YOAST_ACF_ANALYSIS_FILE ), - array( - 'jquery', - 'wp-seo-post-scraper', - ), - self::VERSION - ); + if ( wp_script_is( 'yoast-seo-post-scraper', 'enqueued' ) ) { + // Post page enqueue. + wp_enqueue_script( + 'yoast-acf-analysis-post', + plugins_url( '/js/yoast-acf-analysis.js', YOAST_ACF_ANALYSIS_FILE ), + array( + 'jquery', + 'yoast-seo-post-scraper', + ), + self::VERSION + ); + } - // Term page enqueue. - wp_enqueue_script( - 'yoast-acf-analysis-term', - plugins_url( '/js/yoast-acf-analysis.js', YOAST_ACF_ANALYSIS_FILE ), - array( - 'jquery', - 'wp-seo-term-scraper', - ), - self::VERSION - ); + if ( wp_script_is( 'yoast-seo-term-scraper', 'enqueued' ) ) { + // Term page enqueue. + wp_enqueue_script( + 'yoast-acf-analysis-term', + plugins_url( '/js/yoast-acf-analysis.js', YOAST_ACF_ANALYSIS_FILE ), + array( + 'jquery', + 'yoast-seo-term-scraper', + ), + self::VERSION + ); + } } /** From 1e6863923fbeac9a8a9d3f6bcff3dc43f30b0420 Mon Sep 17 00:00:00 2001 From: Jip Moors Date: Thu, 16 Jun 2016 14:42:24 +0200 Subject: [PATCH 5/5] Depend prefix on Asset Manager existence --- yoast-acf-analysis.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/yoast-acf-analysis.php b/yoast-acf-analysis.php index 678dc985..e3f7c30c 100644 --- a/yoast-acf-analysis.php +++ b/yoast-acf-analysis.php @@ -114,27 +114,31 @@ public function wordpress_seo_requirements_not_met() { * Enqueue JavaScript file to feed data to Yoast Content Analyses. */ public function enqueue_scripts() { - if ( wp_script_is( 'yoast-seo-post-scraper', 'enqueued' ) ) { + + // If the Asset Manager exists then we need to use a different prefix. + $script_prefix = ( class_exists( 'WPSEO_Admin_Asset_Manager' ) ? 'yoast-seo' : 'wp-seo' ); + + if ( wp_script_is( $script_prefix . '-post-scraper', 'enqueued' ) ) { // Post page enqueue. wp_enqueue_script( - 'yoast-acf-analysis-post', + $script_prefix . '-analysis-post', plugins_url( '/js/yoast-acf-analysis.js', YOAST_ACF_ANALYSIS_FILE ), array( 'jquery', - 'yoast-seo-post-scraper', + $script_prefix . '-post-scraper', ), self::VERSION ); } - if ( wp_script_is( 'yoast-seo-term-scraper', 'enqueued' ) ) { + if ( wp_script_is( $script_prefix . '-term-scraper', 'enqueued' ) ) { // Term page enqueue. wp_enqueue_script( - 'yoast-acf-analysis-term', + $script_prefix . '-analysis-term', plugins_url( '/js/yoast-acf-analysis.js', YOAST_ACF_ANALYSIS_FILE ), array( 'jquery', - 'yoast-seo-term-scraper', + $script_prefix . '-term-scraper', ), self::VERSION );