diff --git a/yoast-acf-analysis.php b/yoast-acf-analysis.php
index d04e115d..e3f7c30c 100644
--- a/yoast-acf-analysis.php
+++ b/yoast-acf-analysis.php
@@ -1,6 +1,6 @@
= 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 ) ) {
+ // 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.
+ 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( plugin_basename( YOAST_ACF_ANALYSIS_FILE ) ) ) {
+ $this->deactivate();
}
}
}
- else {
- // Only enqueue when all requirements are met.
- add_filter( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
- }
}
/**
* 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' );
- printf( '
', esc_html( $message ) );
+ $message = sprintf(
+ __( 'Please %1$sinstall & activate Advanced Custom Fields%2$s to use Yoast SEO: ACF Analysis.', 'yoast-acf-analysis' ),
+ '',
+ ''
+ );
+
+ printf( '', $message );
}
/**
* 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 = sprintf(
+ __( 'Please %1$sinstall & activate Yoast SEO 3.1+%2$s to use Yoast SEO: ACF Analysis.', 'yoast-acf-analysis' ),
+ '',
+ ''
+ );
- printf( '', esc_html( $message ) );
+ printf( '', $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', __FILE__ ),
- array(
- 'jquery',
- 'wp-seo-post-scraper',
- ),
- self::VERSION
- );
- // Term page enqueue.
- wp_enqueue_script(
- 'yoast-acf-analysis-term',
- plugins_url( '/js/yoast-acf-analysis.js', __FILE__ ),
- array(
- 'jquery',
- 'wp-seo-term-scraper',
- ),
- self::VERSION
- );
+ // 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(
+ $script_prefix . '-analysis-post',
+ plugins_url( '/js/yoast-acf-analysis.js', YOAST_ACF_ANALYSIS_FILE ),
+ array(
+ 'jquery',
+ $script_prefix . '-post-scraper',
+ ),
+ self::VERSION
+ );
+ }
+
+ if ( wp_script_is( $script_prefix . '-term-scraper', 'enqueued' ) ) {
+ // Term page enqueue.
+ wp_enqueue_script(
+ $script_prefix . '-analysis-term',
+ plugins_url( '/js/yoast-acf-analysis.js', YOAST_ACF_ANALYSIS_FILE ),
+ array(
+ 'jquery',
+ $script_prefix . '-term-scraper',
+ ),
+ self::VERSION
+ );
+ }
}
/**
@@ -220,6 +229,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();