-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ticket/15 required plugin header #20
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' ) ); | ||
} | ||
|
||
/** | ||
|
@@ -515,6 +517,8 @@ public function get_required_plugins() { | |
return array(); | ||
} | ||
|
||
$required_plugins = array_merge( $required_plugins, $this->get_header_required_plugins() ); | ||
|
||
return $required_plugins; | ||
} | ||
|
||
|
@@ -618,6 +622,101 @@ 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 1.2.0 | ||
* @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; | ||
} | ||
|
||
/** | ||
* Return a list of plugins with the required header set. | ||
* | ||
* @since 1.2.0 | ||
* @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 = []; | ||
|
||
/** | ||
* Filter the value for the header that would indicate the plugin as required. | ||
* | ||
* @author Aubrey Portwood <[email protected]> | ||
* @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 ( ! in_array( $headers[ $required_header ], $values, true ) ) { | ||
continue; | ||
} | ||
|
||
$plugins[] = $file; | ||
} | ||
|
||
return $plugins; | ||
} | ||
|
||
/** | ||
* Get the key to use for the required plugin header identifier. | ||
* | ||
* @author Zach Owen | ||
* @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 1.2.0 | ||
* | ||
* @param string $header The string to use as the identifier. | ||
*/ | ||
$header = apply_filters( 'wds_required_plugin_header', $header_text ); | ||
|
||
if ( ! is_string( $header ) || empty( $header ) ) { | ||
return $header_text; | ||
} | ||
|
||
return $header; | ||
} | ||
} | ||
|
||
// Init. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reversed the logic here while I was adding my filter.