From 23dfb168340f6e5385d74f07c9179edbe624f77d Mon Sep 17 00:00:00 2001 From: Zach Owen Date: Fri, 14 Dec 2018 01:01:56 -0500 Subject: [PATCH 1/5] Add ability to read 'Required' plugin header. --- wds-required-plugins.php | 46 +++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/wds-required-plugins.php b/wds-required-plugins.php index c7b47f9..cb0d5e3 100644 --- a/wds-required-plugins.php +++ b/wds-required-plugins.php @@ -108,22 +108,24 @@ public static function init() { private function __construct() { // Only if we are not incompatible with something. - if ( ! $this->incompatible() ) { + if ( $this->incompatible() ) { + return; + } - // Attempt activation + load text domain in the admin. - add_filter( 'admin_init', array( $this, 'activate_if_not' ) ); - add_filter( 'admin_init', array( $this, 'required_text_markup' ) ); + // Attempt activation + load text domain in the admin. + add_filter( 'admin_init', array( $this, 'activate_if_not' ) ); + add_filter( 'admin_init', array( $this, 'required_text_markup' ) ); + add_filter( 'extra_plugin_headers', array( $this, 'add_required_plugin_header' ) ); - // Filter plugin links to remove deactivate option. - add_filter( 'plugin_action_links', array( $this, 'filter_plugin_links' ), 10, 2 ); - add_filter( 'network_admin_plugin_action_links', array( $this, 'filter_plugin_links' ), 10, 2 ); + // Filter plugin links to remove deactivate option. + add_filter( 'plugin_action_links', array( $this, 'filter_plugin_links' ), 10, 2 ); + add_filter( 'network_admin_plugin_action_links', array( $this, 'filter_plugin_links' ), 10, 2 ); - // Remove plugins from the plugins. - add_filter( 'all_plugins', array( $this, 'maybe_remove_plugins_from_list' ) ); + // Remove plugins from the plugins. + add_filter( 'all_plugins', array( $this, 'maybe_remove_plugins_from_list' ) ); - // Load text domain. - add_action( 'plugins_loaded', array( $this, 'l10n' ) ); - } + // Load text domain. + add_action( 'plugins_loaded', array( $this, 'l10n' ) ); } /** @@ -618,6 +620,26 @@ public function l10n() { load_textdomain( 'wds-required-plugins', $mofile ); self::$l10n_done = true; } + + /** + * Adds a header field for required plugins when WordPress reads plugin data. + * + * @since NEXT + * @author Zach Owen + * + * @param array $extra_headers Extra headers filtered in WP core. + * @return array + */ + public function add_required_plugin_header( $extra_headers ) { + $required_header = $this->get_required_header(); + + if ( in_array( $required_header, $extra_headers, true ) ) { + return $extra_headers; + } + + $extra_headers[] = $required_header; + return $extra_headers; + } } // Init. From 4a5f482306c201159d40c3b0c27372c04aa00f21 Mon Sep 17 00:00:00 2001 From: Zach Owen Date: Fri, 14 Dec 2018 01:02:13 -0500 Subject: [PATCH 2/5] Activate required plugins found via header --- wds-required-plugins.php | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/wds-required-plugins.php b/wds-required-plugins.php index cb0d5e3..19b87ff 100644 --- a/wds-required-plugins.php +++ b/wds-required-plugins.php @@ -517,6 +517,8 @@ public function get_required_plugins() { return array(); } + $required_plugins = array_merge( $required_plugins, $this->get_header_required_plugins() ); + return $required_plugins; } @@ -640,6 +642,62 @@ public function add_required_plugin_header( $extra_headers ) { $extra_headers[] = $required_header; return $extra_headers; } + + /** + * Return a list of plugins with the required header set. + * + * @since NEXT + * @author Zach Owen + * + * @return array + */ + public function get_header_required_plugins() { + $all_plugins = apply_filters( 'all_plugins', get_plugins() ); + + if ( empty( $all_plugins ) ) { + return; + } + + $required_header = $this->get_required_header(); + $plugins = []; + + foreach ( $all_plugins as $file => $headers ) { + if ( empty( $headers[ $required_header ] ) ) { + continue; + } + + $plugins[] = $file; + } + + return $plugins; + } + + /** + * Get the key to use for the required plugin header identifier. + * + * @author Zach Owen + * @since NEXT + * + * @return string + */ + private function get_required_header() { + /** + * Filter the text used as the identifier for the plugin being + * required. + * + * @author Zach Owen + * @since NEXT + * + * @param string $header The string to use as the identifier. + */ + $header = apply_filters( 'wds_required_plugin_header', 'Required' ); + + if ( ! is_string( $header ) || empty( $header ) ) { + return 'Required'; + } + + return $header; + } } // Init. From 13bbbafc21b0153d5d040278d29083a5ac2a2a9e Mon Sep 17 00:00:00 2001 From: Zach Owen Date: Fri, 14 Dec 2018 01:07:04 -0500 Subject: [PATCH 3/5] Fix indentation. --- wds-required-plugins.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wds-required-plugins.php b/wds-required-plugins.php index 19b87ff..7bf406c 100644 --- a/wds-required-plugins.php +++ b/wds-required-plugins.php @@ -651,7 +651,7 @@ public function add_required_plugin_header( $extra_headers ) { * * @return array */ - public function get_header_required_plugins() { + public function get_header_required_plugins() { $all_plugins = apply_filters( 'all_plugins', get_plugins() ); if ( empty( $all_plugins ) ) { From 339ebc1cc32ed1fe85d59d7a7eb39f52b98738ea Mon Sep 17 00:00:00 2001 From: Aubrey Portwood Date: Wed, 1 May 2019 11:23:00 -0700 Subject: [PATCH 4/5] Improve. Adds logic so that the value of the header needs to be truthy-ish. --- wds-required-plugins.php | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/wds-required-plugins.php b/wds-required-plugins.php index 7bf406c..af0c9bf 100644 --- a/wds-required-plugins.php +++ b/wds-required-plugins.php @@ -626,7 +626,7 @@ public function l10n() { /** * Adds a header field for required plugins when WordPress reads plugin data. * - * @since NEXT + * @since 1.2.0 * @author Zach Owen * * @param array $extra_headers Extra headers filtered in WP core. @@ -646,7 +646,7 @@ public function add_required_plugin_header( $extra_headers ) { /** * Return a list of plugins with the required header set. * - * @since NEXT + * @since 1.2.0 * @author Zach Owen * * @return array @@ -661,8 +661,25 @@ public function get_header_required_plugins() { $required_header = $this->get_required_header(); $plugins = []; + /** + * Filter the value for the header that would indicate the plugin as required. + * + * @author Aubrey Portwood + * @since 1.2.0 + * + * @var array + */ + $values = apply_filters( 'wds_required_plugins_required_header_values', [ + 'true', + 'yes', + '1', + 'on', + 'required', + 'require', + ] ); + foreach ( $all_plugins as $file => $headers ) { - if ( empty( $headers[ $required_header ] ) ) { + if ( ! in_array( $headers[ $required_header ], $values, true ) ) { continue; } @@ -676,24 +693,26 @@ public function get_header_required_plugins() { * Get the key to use for the required plugin header identifier. * * @author Zach Owen - * @since NEXT + * @since 1.2.0 * * @return string */ private function get_required_header() { + $header_text = 'Required'; + /** * Filter the text used as the identifier for the plugin being * required. * * @author Zach Owen - * @since NEXT + * @since 1.2.0 * * @param string $header The string to use as the identifier. */ - $header = apply_filters( 'wds_required_plugin_header', 'Required' ); + $header = apply_filters( 'wds_required_plugin_header', $header_text ); if ( ! is_string( $header ) || empty( $header ) ) { - return 'Required'; + return $header_text; } return $header; From c0a61fe84e368a0b3f5de31ecdbf5a4e148182d3 Mon Sep 17 00:00:00 2001 From: Aubrey Portwood Date: Wed, 1 May 2019 11:28:20 -0700 Subject: [PATCH 5/5] Add to changelog. --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index d317e52..ff8668e 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,10 @@ ____________________ # Changelog +## 1.2.0 + +- Added ability to add `Required: true` to plugin header to make plugin required vs. adding to filtered list [#20](https://github.com/WebDevStudios/WDS-Required-Plugins/pull/20/files) + ## 1.1.0 - You can have the project throw an `Exception` if there is an activation problem