Skip to content

Commit

Permalink
increase API server request timeout and improve cleanup plugin (#182)
Browse files Browse the repository at this point in the history
* increase API server request timeout

* differentiate multi-site from non-multisite cleanup
  • Loading branch information
mlwilkerson committed Sep 1, 2022
1 parent a6bcdaa commit b35642d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
2 changes: 1 addition & 1 deletion bin/make-cleanup-dist-zip
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if ($zip->open($zip_filename, ZipArchive::CREATE)!==TRUE) {
$options = array( 'remove_path' => 'integrations/plugins');

$zip->addGlob(
'integrations/plugins/font-awesome-cleanup/index.php',
'integrations/plugins/font-awesome-cleanup/*.php',
0,
$options
);
Expand Down
1 change: 1 addition & 0 deletions includes/class-fontawesome-metadata-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public function metadata_query( $query_string, $ignore_auth = false ) {
'Content-Type' => 'application/json',
),
'body' => '{"query": ' . wp_json_encode( $query_string ) . '}',
'timeout' => 10, // seconds.
);

if ( ! $ignore_auth ) {
Expand Down
16 changes: 12 additions & 4 deletions integrations/plugins/font-awesome-cleanup/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
* Plugin Name: Font Awesome Cleanup
* Plugin URI: https://fontawesome.com/
* Description: Cleans out Font Awesome plugin settings from the WordPress database, including any child sites in multisite.
* Version: 0.0.2
* Version: 0.0.3
* Author: Font Awesome
* Author URI: https://fontawesome.com/
* License: GPLv2 (or later)
*/

defined( 'WPINC' ) || die;

function plugin_version() { return '0.0.2'; }
require_once trailingslashit( __DIR__ ) . 'utils.php';

function plugin_version() { return '0.0.3'; }

function plugin_name() { return 'font-awesome-cleanup'; }

Expand Down Expand Up @@ -154,8 +156,8 @@ function cleanup_site() {

function font_awesome_plugin_is_active() {
$active_plugins = array_merge(
wp_get_active_and_valid_plugins(),
wp_get_active_network_plugins()
fa_get_active_and_valid_plugins(),
fa_get_active_network_plugins()
);

return count(array_filter($active_plugins, function($plugin_name) {
Expand Down Expand Up @@ -189,6 +191,12 @@ function filter_action_links( $links ) {
);

function display_cleanup_scope() {
if ( is_multisite() ) {
display_cleanup_scope_multisite();
}
}

function display_cleanup_scope_multisite() {
$is_cleanup_network_active = is_plugin_active_for_network( plugin_file() );

$sites = [];
Expand Down
77 changes: 77 additions & 0 deletions integrations/plugins/font-awesome-cleanup/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
namespace FontAwesomeCleanup;

/**
* Returns array of network plugin files to be included in global scope.
*
* Adapted from the private function in WP Core: wp_get_active_network_plugins()
*
* The default directory is wp-content/plugins. To change the default directory
* manually, define `WP_PLUGIN_DIR` and `WP_PLUGIN_URL` in `wp-config.php`.
*
* @return string[] Array of absolute paths to files to include.
*/
function fa_get_active_network_plugins() {
$active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
if ( empty( $active_plugins ) ) {
return array();
}

$plugins = array();
$active_plugins = array_keys( $active_plugins );
sort( $active_plugins );

foreach ( $active_plugins as $plugin ) {
if ( ! validate_file( $plugin ) // $plugin must validate as file.
&& '.php' === substr( $plugin, -4 ) // $plugin must end with '.php'.
&& file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist.
) {
$plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
}
}

return $plugins;
}

/**
* Retrieve an array of active and valid plugin files.
*
* Adapted from the private function in WP Core: wp_get_active_and_valid_plugins()
*
* The default directory is `wp-content/plugins`. To change the default
* directory manually, define `WP_PLUGIN_DIR` and `WP_PLUGIN_URL`
* in `wp-config.php`.
*
* @return string[] Array of paths to plugin files relative to the plugins directory.
*/
function fa_get_active_and_valid_plugins() {
$plugins = array();
$active_plugins = (array) get_option( 'active_plugins', array() );

if ( empty( $active_plugins ) || wp_installing() ) {
return $plugins;
}

$network_plugins = is_multisite() ? fa_get_active_network_plugins() : false;

foreach ( $active_plugins as $plugin ) {
if ( ! validate_file( $plugin ) // $plugin must validate as file.
&& '.php' === substr( $plugin, -4 ) // $plugin must end with '.php'.
&& file_exists( WP_PLUGIN_DIR . '/' . $plugin ) // $plugin must exist.
// Not already included as a network plugin.
&& ( ! $network_plugins || ! in_array( WP_PLUGIN_DIR . '/' . $plugin, $network_plugins, true ) )
) {
$plugins[] = WP_PLUGIN_DIR . '/' . $plugin;
}
}

/*
* Remove plugins from the list of active plugins when we're on an endpoint
* that should be protected against WSODs and the plugin is paused.
*/
if ( wp_is_recovery_mode() ) {
$plugins = wp_skip_paused_plugins( $plugins );
}

return $plugins;
}

0 comments on commit b35642d

Please sign in to comment.