From 4476fbec53a694b1dad96b6e9b92285d0ce90951 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 7 May 2016 05:59:28 +0200 Subject: [PATCH] Improve requirements check. This PR solves a number of issues: 1. The js was only enqueued if the current user had the `activate_plugins` ability (=(super)-admin) rendering the plugin largely useless for multi-author websites where most authors are non-admins. 2. The requirements were checked using `is_plugin_active()` which is unreliable. Changed this to a check based on code rather than on the `is_plugin_active()` function. This is more reliable as: a) it will also take into account plugins loaded through the must-use plugins directory b) it will still work even if the user has renamed the folder for the plugin within `wp-content/plugins/` (which happens more often than you'd expect). 3. Allow for *this* plugin to be installed as a must-use plugin, i.e. the admin notice and the deactivation should only happen if the plugin is installed as a normal plugin. So re-arranged the code to deal with that. 4. Get rid of the use of `get_plugin_data()` which is an expensive function if all you really want to do is get the version number of the plugin. Also the original function call was wrong causing a number of errors and failing to retrieve the version number as `dirname( __FILE__ )` was passed, while the function expects `__FILE__`. Replaced with a class constant. Fixes #6 (point 2) This PR additionally fixes the following PHP errors which were caused by the bug outlined in point 3: `PHP Warning: fopen(/path/to/plugins/yoast-seo-acf-analysis): failed to open stream: Permission denied` `PHP Warning: fread() expects parameter 1 to be resource, boolean given in /path/to/wp-includes/functions.php on line 4466` `PHP Warning: fclose() expects parameter 1 to be resource, boolean given in /path/to/wp-includes/functions.php on line 4469` --- yoast-acf-analysis.php | 72 ++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/yoast-acf-analysis.php b/yoast-acf-analysis.php index ed182bf5..d04e115d 100644 --- a/yoast-acf-analysis.php +++ b/yoast-acf-analysis.php @@ -21,8 +21,7 @@ */ class Yoast_ACF_Analysis { - /** @var array Plugin information. */ - private $plugin_data = null; + const VERSION = '1.1.0'; /** * Yoast_ACF_Analysis constructor. @@ -44,46 +43,51 @@ function __construct() { */ public function admin_init() { - // Require ACF and Yoast - if ( current_user_can( 'activate_plugins' ) ) { - $deactivate = false; + $notice_functions = array(); - // ACF - if ( ! is_plugin_active( 'advanced-custom-fields/acf.php' ) && ! is_plugin_active( 'advanced-custom-fields-pro/acf.php' ) ) { - add_action( 'admin_notices', array( $this, 'acf_not_active_notification' ) ); - $deactivate = true; - } + // 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 ( ! is_plugin_active( 'wordpress-seo/wp-seo.php' ) && ! is_plugin_active( 'wordpress-seo-premium/wp-seo-premium.php' ) ) { - add_action( 'admin_notices', array( $this, 'wordpress_seo_requirements_not_met' ) ); - $deactivate = true; - } - else { - // Compare if version is >= 3.0 - if ( defined( 'WPSEO_VERSION' ) ) { - if ( version_compare( substr( WPSEO_VERSION, 0, 3 ), '3.1', '<' ) ) { - add_action( 'admin_notices', array( $this, 'wordpress_seo_requirements_not_met' ) ); - $deactivate = true; - } + // Yoast SEO for WordPress + if ( ! defined( 'WPSEO_VERSION' ) ) { + $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', '<' ) ) { + $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() ); - // Deactivate when we cannot do the job we are hired to do. - if ( $deactivate ) { - deactivate_plugins( plugin_basename( __FILE__ ) ); + // 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' ) ); + } + // Prevent trying again on page reload. if ( isset( $_GET['activate'] ) ) { unset( $_GET['activate'] ); } - - return; } - - // Only enqueue when we are active. + } + else { + // Only enqueue when all requirements are met. add_filter( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); - - $this->plugin_data = get_plugin_data( dirname( __FILE__ ) ); } } @@ -117,7 +121,7 @@ public function enqueue_scripts() { 'jquery', 'wp-seo-post-scraper', ), - $this->plugin_data['Version'] + self::VERSION ); // Term page enqueue. @@ -128,7 +132,7 @@ public function enqueue_scripts() { 'jquery', 'wp-seo-term-scraper', ), - $this->plugin_data['Version'] + self::VERSION ); }