Add function to add/remove theme mu-plugins

This commit is contained in:
Tetrakern 2024-06-19 12:54:53 +02:00
parent 4cb48280dd
commit d0f286ffeb
3 changed files with 139 additions and 2 deletions

View File

@ -523,7 +523,9 @@ Technically just another plugin, but one that will make your site significantly
### Recommended: Must-Use Plugins
[Must-Use Plugins](https://wordpress.org/documentation/article/must-use-plugins/) are not installed but have to be copied into the **wp-content/mu-plugins** directory (does not exist by default). They are always loaded, in alphabetical order, and before any other plugin or theme. This behavior can be exploited to boost performance. When you look into the Fictioneer theme directory, you will find an mu-plugins subdirectory with must-use plugins ready to be copied over.
[Must-Use Plugins](https://wordpress.org/documentation/article/must-use-plugins/) are not installed but have to be copied into the **wp-content/mu-plugins** directory (does not exist by default). They are always loaded, in alphabetical order, and before any other plugin or theme. This behavior can be exploited to boost performance. When you look into the Fictioneer theme directory, you will find an mu-plugins subdirectory with plugin files ready to be copied over.
**Since 5.20.2,** you can add and remove the themes mu-plugins quickly under **Fictioneer > Plugins**. It will also show you whether an update is available, but it wont install it automatically in case you customized the file. Check occasionally.
If problems arise, you can just delete the plugin files.

View File

@ -976,3 +976,79 @@ function fictioneer_append_meta_fields( $post_type, $meta_key, $meta_value ) {
}
}
// =============================================================================
// MU-PLUGINS
// =============================================================================
/**
* Initializes the mu-plugins directory in /wp-content
*
* @since 5.20.1
*/
function fictioneer_initialize_mu_plugins() {
// Make sure directory exists
if ( ! is_dir( WPMU_PLUGIN_DIR ) ) {
mkdir( WPMU_PLUGIN_DIR, 0755, true );
}
}
/**
* Returns data about theme's MU-Plugins
*
* @since 5.20.1
*
* @return array Array of plugin data.
*/
function fictioneer_get_mu_plugin_data() {
// Initialize if necessary
fictioneer_initialize_mu_plugins();
// Setup
$mu_plugins = get_mu_plugins();
$data = array(
'fictioneer_fast_requests' => array(
'key' => 'fictioneer_fast_requests',
'filename' => 'fictioneer_001_fast_requests.php',
'name' => _x( 'Fictioneer Fast Requests', 'Theme mu-plugin.', 'fictioneer' ),
'description' => _x( 'Disables plugins for selected actions to accelerate dynamic requests, such as AJAX comments. If you have many plugins installed, this can significantly reduce loading times.', 'Theme mu-plugin.', 'fictioneer' ),
'version' => '1.1.0',
'update' => false,
'active' => false
),
'fictioneer_elementor_control' => array(
'key' => 'fictioneer_elementor_control',
'filename' => 'fictioneer_002_elementor_control.php',
'name' => _x( 'Fictioneer Elementor Control', 'Theme mu-plugin.', 'fictioneer' ),
'description' => _x( 'Disables the Elementor plugin on all pages except those with a Canvas page template. Since Elementor consumes a lot of server resources, limiting it to actual use cases is sensible. However, this makes the plugin unavailable anywhere else on the frontend.', 'Theme mu-plugin.', 'fictioneer' ),
'version' => '1.0.0',
'update' => false,
'active' => false
)
);
// Active?
foreach ( $mu_plugins as $plugin_data ) {
if ( $plugin_data['Name'] === 'Fictioneer Fast Requests' ) {
$data['fictioneer_fast_requests']['active'] = true;
// Check version
if ( version_compare( $data['fictioneer_fast_requests']['version'], $plugin_data['Version'], '>' ) ) {
$data['fictioneer_fast_requests']['update'] = true;
}
}
if ( $plugin_data['Name'] === 'Fictioneer Elementor Control' ) {
$data['fictioneer_elementor_control']['active'] = true;
// Check version
if ( version_compare( $data['fictioneer_elementor_control']['version'], $plugin_data['Version'], '>' ) ) {
$data['fictioneer_elementor_control']['update'] = true;
}
}
}
// Return
return $data;
}

View File

@ -6,6 +6,12 @@
* @subpackage Fictioneer
* @since 5.7.1
*/
// Setup
$mu_plugins = get_mu_plugins();
$fictioneer_mu_plugin_data = fictioneer_get_mu_plugin_data();
?>
<div class="fictioneer-settings">
@ -14,7 +20,60 @@
<div class="fictioneer-settings__content">
<div class="fictioneer-single-column">
<?php do_action( 'fictioneer_admin_settings_plugins' ); ?>
<?php
// MU-Plugins
foreach ( $fictioneer_mu_plugin_data as $plugin_data ) {
// Start HTML ---> ?>
<div class="fictioneer-card fictioneer-card--plugin <?php echo $plugin_data['active'] ? '' : 'fictioneer-card--disabled'; ?>">
<div class="fictioneer-card__wrapper">
<div class="fictioneer-card__header fictioneer-card__header--with-actions">
<h3><?php
echo $plugin_data['name'];
if ( ! $plugin_data['active'] ) {
_ex( ' — Disabled', 'Settings plugin card.', 'fictioneer' );
} elseif ( $plugin_data['update'] ) {
_ex( ' — Update', 'Settings plugin card.', 'fictioneer' );
}
?></h3>
<div>
<?php if ( $plugin_data['active'] && ! $plugin_data['update'] ) : ?>
<a class="button button--secondary" href="<?php echo esc_url( add_query_arg( 'mu_plugin', $plugin_data['filename'], fictioneer_admin_action( 'fictioneer_disable_mu_plugin' ) ) ); ?>"><?php _e( 'Disable', 'fictioneer' ); ?></a>
<?php else : ?>
<a class="button button--secondary" href="<?php echo esc_url( add_query_arg( 'mu_plugin', $plugin_data['filename'], fictioneer_admin_action( 'fictioneer_enable_mu_plugin' ) ) ); ?>"><?php
$plugin_data['update'] ? _e( 'Update', 'fictioneer' ) : _e( 'Enable', 'fictioneer' );
?></a>
<?php endif; ?>
</div>
</div>
<div class="fictioneer-card__content">
<div class="fictioneer-card__row">
<p><?php echo $plugin_data['description']; ?></p>
</div>
<div class="fictioneer-card__row fictioneer-card__row--meta"><?php
printf( __( 'Version %s', 'fictioneer' ), $plugin_data['version'] ?? 'n/a' );
echo ' | ';
printf(
__( '<a href="%s" target="_blank" rel="noopener">MU-Plugin</a>', 'fictioneer' ),
'https://github.com/Tetrakern/fictioneer/blob/main/INSTALLATION.md#recommended-must-use-plugins'
);
echo ' | ';
printf(
__( 'By <a href="%s" target="_blank" rel="noopener">Tetrakern</a>', 'fictioneer' ),
'https://github.com/Tetrakern'
);
?></div>
</div>
</div>
</div>
<?php // <--- End HTML
}
// Hook for plugins
do_action( 'fictioneer_admin_settings_plugins' );
?>
</div>
</div>