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

Add a Must-Use loader file. #136

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
129 changes: 129 additions & 0 deletions mu/aspireupdate-mu-loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/**
* MU Loader
*
* @author Andy Fragen, Colin Stewart
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to Aspirepress

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an externally-maintained resource used by multiple projects and being proposed for use by AspireUpdate, rather than AspireUpdate's own resource. If myself or @afragen release an update to the file, we can open an issue and PR to implement the updates for AspireUpdate.

Pinging @afragen as co-maintainer.

* @license MIT
* @package mu-plugins
*/

/**
* Plugin Name: AspireUpdate MU Loader
* Plugin URI: https://gist.github.com/afragen/9117fd930d9be16be8a5f450b809dfa8
* Description: Loads AspireUpdate as a Must-Use plugin. Disables normal plugin activation and deletion.
* Version: 0.6.0
* Author: WordPress Upgrade/Install Team
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to Aspirepress

* License: MIT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change License

* Text Domain: mu-loader
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update Text Domain

* GitHub Plugin URI: https://gist.github.com/afragen/9117fd930d9be16be8a5f450b809dfa8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the URL

* Requires PHP: 7.4
* Requires WP: 5.9
*/

namespace AspireUpdate;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}

/**
* Class MU_Loader
*/
class MU_Loader {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update class name to match AU Classes

/**
* Holds array of plugin files.
*
* Add filepath to array, 'my-plugin/my-plugin.php'.
*
* @var array
*/
private static $plugin_files = array( 'AspireUpdate/aspire-update.php' );

/**
* Let's get going.
* Load the plugin and hooks.
*
* @return void
*/
public function run() {
foreach ( static::$plugin_files as $plugin_file ) {
$plugin_filepath = trailingslashit( WP_PLUGIN_DIR ) . $plugin_file;
if ( file_exists( $plugin_filepath ) ) {
require $plugin_filepath;
$this->load_hooks( $plugin_file );
}
}
add_filter( 'option_active_plugins', array( $this, 'set_as_active' ) );
}

/**
* Load action and filter hooks.
*
* Remove links and disable checkbox from Plugins page so user can't delete main plugin.
* Ensure plugin shows as active.
*
* @param string $plugin_file Plugin file.
* @return void
*/
public function load_hooks( $plugin_file ) {
add_filter( 'network_admin_plugin_action_links_' . $plugin_file, array( $this, 'mu_plugin_active' ) );
add_filter( 'plugin_action_links_' . $plugin_file, array( $this, 'mu_plugin_active' ) );
add_action( 'after_plugin_row_' . $plugin_file, array( $this, 'after_plugin_row_updates' ) );
add_action( 'after_plugin_row_meta', array( $this, 'display_as_mu_plugin' ), 10, 1 );
}

/**
* Make plugin row active and disable checkbox.
*
* @param string $plugin_file Plugin file.
* @return void
*/
public function after_plugin_row_updates( $plugin_file ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
print "<script>jQuery('.inactive[data-plugin=\"{$plugin_file}\"]').attr('class','active');</script>";
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
print "<script>jQuery('.active[data-plugin=\"{$plugin_file}\"] .check-column input').attr( 'disabled','disabled' );</script>";
}

/**
* Add 'Activated as mu-plugin' to plugin row meta.
*
* @param string $plugin_file Plugin file.
* @return void
*/
public function display_as_mu_plugin( $plugin_file ) {
if ( in_array( $plugin_file, (array) static::$plugin_files, true ) ) {
printf(
'<br><span style="color:#a7aaad;">%s</span>',
esc_html__( 'Activated as Must-Use plugin', 'mu-loader' )
);
}
}

/**
* Unset action links.
*
* @param array $actions Link actions.
* @return array
*/
public function mu_plugin_active( $actions ) {
unset( $actions['activate'], $actions['delete'], $actions['deactivate'] );

return $actions;
}

/**
* Set mu-plugins as active.
*
* @param array $active_plugins Array of active plugins.
* @return array
*/
public function set_as_active( $active_plugins ) {
$active_plugins = array_merge( $active_plugins, static::$plugin_files );

return array_unique( $active_plugins );
}
}

( new MU_Loader() )->run();
3 changes: 3 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<!-- Translation files. -->
<exclude-pattern>/languages/*</exclude-pattern>

<!-- Must-Use Loader -->
<exclude-pattern>/mu/*</exclude-pattern>

<!--
#############################################################################
SET UP THE RULESET
Expand Down