Skip to content
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 5 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sup>[#20](https://github.com/WebDevStudios/WDS-Required-Plugins/pull/20/files)</sup>

## 1.1.0

- You can have the project throw an `Exception` if there is an activation problem
Expand Down
123 changes: 111 additions & 12 deletions wds-required-plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Copy link
Contributor Author

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.

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' ) );
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
Expand Down