Skip to content

Commit

Permalink
Merge pull request #31 from newfold-labs/enhance/PRESS7-79-adding-per…
Browse files Browse the repository at this point in the history
…formance-link-in-wp-settings-menu

Enhance/press7-79 adding performance link in wp > settings menu and remove old settings
  • Loading branch information
arunshenoy99 authored Dec 31, 2024
2 parents 2a21b50 + f145267 commit 245fd4a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 72 deletions.
38 changes: 7 additions & 31 deletions includes/CacheTypes/Skip404.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use function WP_Forge\WP_Htaccess_Manager\addContent;
use function WP_Forge\WP_Htaccess_Manager\removeMarkers;

/**
* Skip404 Class
*/
class Skip404 extends CacheBase {

/**
Expand All @@ -20,7 +23,7 @@ class Skip404 extends CacheBase {
/**
* Whether or not the code for this cache type should be loaded.
*
* @param Container $container
* @param Container $container the container.
*
* @return bool
*/
Expand All @@ -33,35 +36,9 @@ public static function shouldEnable( Container $container ) {
*/
public function __construct() {

new OptionListener( Performance::OPTION_SKIP_404, [ __CLASS__, 'maybeAddRules' ] );

add_filter( 'newfold_update_htaccess', [ $this, 'onUpdateHtaccess' ] );
add_action( 'admin_init', [ $this, 'registerSettings' ], 11 );
}

/**
* Register our setting to the main performance settings section.
*/
public function registerSettings() {

global $wp_settings_fields;

add_settings_field(
Performance::OPTION_SKIP_404,
__( 'Skip WordPress 404 Handling For Static Files', 'newfold-performance-module' ),
'NewfoldLabs\\WP\\Module\\Performance\\getSkip404InputField',
'general',
Performance::SETTINGS_SECTION
);

register_setting( 'general', Performance::OPTION_SKIP_404 );

// Remove the setting from EPC if it exists - TODO: Remove when no longer using EPC
if ( $this->container->get( 'hasMustUsePlugin' ) ) {
unset( $wp_settings_fields['general']['epc_settings_section'] );
unregister_setting( 'general', 'epc_skip_404_handling' );
}
new OptionListener( Performance::OPTION_SKIP_404, array( __CLASS__, 'maybeAddRules' ) );

add_filter( 'newfold_update_htaccess', array( $this, 'onUpdateHtaccess' ) );
}

/**
Expand All @@ -80,7 +57,7 @@ public function onUpdateHtaccess() {
/**
* Conditionally add or remove .htaccess rules based on option value.
*
* @param bool|null $shouldSkip404Handling
* @param bool|null $shouldSkip404Handling if should skip 404 handling.
*/
public static function maybeAddRules( $shouldSkip404Handling ) {
(bool) $shouldSkip404Handling ? self::addRules() : self::removeRules();
Expand Down Expand Up @@ -124,5 +101,4 @@ public static function onActivation() {
public static function onDeactivation() {
self::removeRules();
}

}
77 changes: 36 additions & 41 deletions includes/Performance.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace NewfoldLabs\WP\Module\Performance;

use NewfoldLabs\WP\Module\Performance\RestApi\RestApi;
use NewfoldLabs\WP\ModuleLoader\Container;
use NewfoldLabs\WP\Module\Performance\Permissions;
use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller;
use NewfoldLabs\WP\Module\Performance\RestApi\RestApi;

use Automattic\Jetpack\Current_Plan;

Expand Down Expand Up @@ -75,12 +75,9 @@ public function __construct( Container $container ) {
$cachePurger = new CachePurgingService( $cacheManager->getInstances() );

add_action( 'admin_bar_menu', array( $this, 'adminBarMenu' ), 100 );
new LinkPrefetch( $container );
add_action( 'admin_menu', array( $this, 'add_sub_menu_page' ) );

// Ensure that purgeable cache types are enabled before showing the UI.
if ( $cachePurger->canPurge() ) {
add_action( 'admin_bar_menu', array( $this, 'adminBarMenu' ), 100 );
}
new LinkPrefetch( $container );

$container->set( 'cachePurger', $cachePurger );

Expand Down Expand Up @@ -120,8 +117,7 @@ function () {
* Add hooks.
*/
public function hooks() {

add_action( 'admin_init', array( $this, 'registerSettings' ), 11 );
add_action( 'admin_init', array( $this, 'remove_epc_settings' ), 99 );

new OptionListener( self::OPTION_CACHE_LEVEL, array( $this, 'onCacheLevelChange' ) );

Expand Down Expand Up @@ -153,6 +149,23 @@ function () {
add_filter( 'action_scheduler_cleanup_batch_size', array( $this, 'nfd_as_cleanup_batch_size' ) );
}

/**
* Remove EPC Settings if needed
*
* @return void
*/
public function remove_epc_settings() {
global $wp_settings_fields, $wp_settings_sections;
//phpcs:ignore
// Remove the setting from EPC if it exists - TODO: Remove when no longer using EPC
if ( $this->container->get( 'hasMustUsePlugin' ) ) {
unset( $wp_settings_fields['general']['epc_settings_section'] );
unset( $wp_settings_sections['general']['epc_settings_section'] );
unregister_setting( 'general', 'endurance_cache_level' );
unregister_setting( 'general', 'epc_skip_404_handling' );
}
}

/**
* Update the default action scheduler retention period to 5 days instead of 30.
* The actions scheduler table tends to grow to gigantic sizes and this should help.
Expand Down Expand Up @@ -217,39 +230,6 @@ public function onCacheLevelChange( $cacheLevel ) {
}
}

/**
* Register settings
*/
public function registerSettings() {

global $wp_settings_fields;

$section_name = self::SETTINGS_SECTION;

add_settings_section(
$section_name,
'<span id="' . self::SETTINGS_ID . '">' . esc_html__( 'Caching', 'newfold-performance-module' ) . '</span>',
'__return_false',
'general'
);

add_settings_field(
self::OPTION_CACHE_LEVEL,
__( 'Cache Level', 'newfold-performance-module' ),
__NAMESPACE__ . '\\getCacheLevelDropdown',
'general',
$section_name
);

register_setting( 'general', self::OPTION_CACHE_LEVEL );

// Remove the setting from EPC if it exists - TODO: Remove when no longer using EPC
if ( $this->container->get( 'hasMustUsePlugin' ) ) {
unset( $wp_settings_fields['general']['epc_settings_section'] );
unregister_setting( 'general', 'endurance_cache_level' );
}
}

/**
* Add options to the WordPress admin bar.
*
Expand Down Expand Up @@ -303,6 +283,21 @@ public function adminBarMenu( \WP_Admin_Bar $wp_admin_bar ) {
}

/**
* Add performance menu in WP/Settings
*/
public function add_sub_menu_page() {
$brand = $this->container->get( 'plugin' )['id'];
add_management_page(
__( 'Performance', 'newfold-performance-module' ),
__( 'Performance', 'newfold-performance-module' ),
'manage_options',
admin_url( "admin.php?page=$brand#/performance" ),
null,
5
);
}

/*
* Enqueue scripts and styles in admin
*/
public function enqueue_scripts() {
Expand Down

0 comments on commit 245fd4a

Please sign in to comment.