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
Changes from 2 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
104 changes: 92 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,82 @@ 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;
}

/**
* 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.
Expand Down