Skip to content

Commit

Permalink
update mu-loader, composer update
Browse files Browse the repository at this point in the history
  • Loading branch information
afragen committed Feb 27, 2024
1 parent 6a49a43 commit 1d846a1
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 127 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* update `freemius/wordpress-sdk`
* update `class-parser.php`
* use `is_wp_version_compatible()` and `is_php_version_compatible()` in `GU_Trait::can_update_repo()`
* update `gu-loader.php` with generic loader

#### 12.3.1 / 2023-10-19
* update `freemius/wordpress-sdk`
Expand Down
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion git-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Plugin Name: Git Updater
* Plugin URI: https://git-updater.com
* Description: A plugin to automatically update GitHub hosted plugins, themes, and language packs. Additional API plugins available for Bitbucket, GitLab, Gitea, and Gist.
* Version: 12.3.1.2
* Version: 12.3.1.4
* Author: Andy Fragen
* License: MIT
* Domain Path: /languages
Expand Down
110 changes: 0 additions & 110 deletions mu/gu-loader.php

This file was deleted.

124 changes: 124 additions & 0 deletions mu/mu-loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/**
* MU Loader
*
* @author Andy Fragen, Colin Stewart
* @license MIT
* @package mu-plugins
*/

/**
* Plugin Name: MU Loader
* Plugin URI: https://gist.github.com/afragen/9117fd930d9be16be8a5f450b809dfa8
* Description: An mu-plugin to load plugins as a must-use plugins. Disables normal plugin activation and deletion.
* Version: 0.5.0
* Author: WordPress Upgrade/Install Team
* License: MIT
* GitHub Plugin URI: https://gist.github.com/afragen/9117fd930d9be16be8a5f450b809dfa8
* Requires PHP: 8.0
* Requires WP: 5.9
*/

namespace WP_Plugin_Install_Team;

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

/**
* Class MU_Loader
*/
class MU_Loader {
/**
* Holds array of plugin files.
*
* @var $plugin_files
*/
private static $plugin_files = [ 'git-updater/git-updater.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', [ $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, [ $this, 'mu_plugin_active' ] );
add_filter( 'plugin_action_links_' . $plugin_file, [ $this, 'mu_plugin_active' ] );
add_action( 'after_plugin_row_' . $plugin_file, [ $this, 'after_plugin_row_updates' ] );
add_action( 'after_plugin_row_meta', [ $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 ) {
print "<script>jQuery('.inactive[data-plugin=\"{$plugin_file}\"]').attr('class','active');</script>";
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 mu-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();
Loading

7 comments on commit 1d846a1

@adevade
Copy link
Contributor

Choose a reason for hiding this comment

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

Changing the name of gu-loader.php broke the symbolic links for everyone that had them set up, right? It did for me at least...

@afragen
Copy link
Owner Author

Choose a reason for hiding this comment

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

Sorry about that. I was trying to move to a more generic loader. I can certainly change the name back. What do you think?

@adevade
Copy link
Contributor

Choose a reason for hiding this comment

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

No problems! I updated the symlinks on our sites 👍

@afragen
Copy link
Owner Author

Choose a reason for hiding this comment

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

FWIW the mu-loader is more versatile as you can add any installed plugin to the array for it to be mu-loaded and still be updated.

@adevade
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I don't know if maybe most people using the MU feature has copied the file over to the mu-plugins folder? In that case nothing should break for them, right?

Is there a recommended way in the docs for handling the MU feature?

@afragen
Copy link
Owner Author

Choose a reason for hiding this comment

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

The recommendation is to use the loader plugin. But you just reminded me to check and see if the info is updated. I need to update. 😉

https://git-updater.com/knowledge-base/installation/#articleTOC_5

@afragen
Copy link
Owner Author

@afragen afragen commented on 1d846a1 Mar 26, 2024

Choose a reason for hiding this comment

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

The new loader was written as a PoC for a universal mu-loader that works with plugin dependencies.

https://gist.github.com/afragen/9117fd930d9be16be8a5f450b809dfa8

Please sign in to comment.