Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release/v2.5.0' into enhance/PRE…
Browse files Browse the repository at this point in the history
…SS7-15-scripts-loading
  • Loading branch information
AleTorrisi committed Jan 29, 2025
2 parents 669a2b7 + a475e2d commit e0179f1
Show file tree
Hide file tree
Showing 22 changed files with 200 additions and 164 deletions.
2 changes: 1 addition & 1 deletion components/performance/defaultText.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const defaultText = {
'wp-module-performance'
),
skip404OptionLabel: __(
'Skip 404 Handling For Static Files',
'Enable Skip 404 Handling For Static Files',
'wp-module-performance'
),
skip404NoticeTitle: __( 'Skip 404 saved', 'wp-module-performance' ),
Expand Down
10 changes: 4 additions & 6 deletions components/skip404/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import apiFetch from '@wordpress/api-fetch';

// Newfold
import { Checkbox, Container } from '@newfold/ui-component-library';
import { ToggleField, Container } from '@newfold/ui-component-library';
import { NewfoldRuntime } from '@newfold/wp-module-runtime';

const Skip404 = ( { methods, constants } ) => {
Expand Down Expand Up @@ -53,13 +53,11 @@ const Skip404 = ( { methods, constants } ) => {
title={ constants.text.skip404Title }
description={ constants.text.skip404Description }
>
<Checkbox
<ToggleField
id="skip-404"
name="skip-404"
onChange={ handleSkip404Change }
value={ skip404 }
checked={ skip404 }
label={ constants.text.skip404OptionLabel }
checked={ skip404 }
onChange={ () => handleSkip404Change( skip404, setSkip404 ) }
/>
</Container.SettingsField>
);
Expand Down
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@
"require-dev": {
"newfold-labs/wp-php-standards": "^1.2.4"
},
"scripts": {
"cs-fix": [
"phpcbf --standard=phpcs.xml ."
],
"cs-lint": [
"phpcs --standard=phpcs.xml -s ."
]
},
"scripts-descriptions": {
"fix": "Automatically fix coding standards issues where possible.",
"lint": "Check files against coding standards."
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
Expand Down
8 changes: 6 additions & 2 deletions includes/BurstSafetyMode/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
use NewfoldLabs\WP\Module\Performance\BurstSafetyMode\ResponseHeaderManager;
use WP_Forge\WP_Htaccess_Manager\htaccess;

/**
* Browser cache.
*/
class Browser {
/**
* The file marker name.
Expand All @@ -12,6 +15,9 @@ class Browser {
*/
const MARKER = 'Newfold Browser Cache';

/**
* Constructor.
*/
public function __construct() {
$responseHeaderManager = new ResponseHeaderManager();
$responseHeaderManager->addHeader( 'X-Newfold-Cache-Level', BURST_SAFETY_CACHE_LEVEL );
Expand All @@ -20,8 +26,6 @@ public function __construct() {

/**
* Add htaccess rules.
*
* @return void
*/
public static function addRules() {

Expand Down
14 changes: 11 additions & 3 deletions includes/BurstSafetyMode/ResponseHeaderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use WP_Forge\WP_Htaccess_Manager\htaccess;
use function WP_Forge\WP_Htaccess_Manager\convertContentToLines;

/**
* Response header manager.
*/
class ResponseHeaderManager {

/**
Expand All @@ -14,6 +17,11 @@ class ResponseHeaderManager {
*/
const MARKER = 'Newfold Headers';

/**
* Htaccess instance.
*
* @var htaccess
*/
public $htaccess;

/**
Expand Down Expand Up @@ -67,7 +75,7 @@ public function addHeader( string $name, string $value ) {
/**
* Add multiple headers at once.
*
* @param string[] $headers
* @param string[] $headers Headers to add.
*/
public function addHeaders( array $headers ) {
$headers = array_merge( $this->parseHeaders(), $headers );
Expand Down Expand Up @@ -95,7 +103,7 @@ public function removeAllHeaders() {
/**
* Set headers.
*
* @param array $headers
* @param array $headers Headers to set.
*/
public function setHeaders( array $headers ) {

Expand All @@ -107,7 +115,7 @@ public function setHeaders( array $headers ) {

$content = '<IfModule mod_headers.c>' . PHP_EOL;
foreach ( $headers as $key => $value ) {
$content .= "\t" . "Header set {$key} \"{$value}\"" . PHP_EOL;
$content .= "\tHeader set {$key} \"{$value}\"" . PHP_EOL;
}
$content .= '</IfModule>';

Expand Down
18 changes: 10 additions & 8 deletions includes/BurstSafetyMode/Skip404.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
use function WP_Forge\WP_Htaccess_Manager\addContent;
use function WP_Forge\WP_Htaccess_Manager\removeMarkers;

/**
* Skip 404 cache type.
*/
class Skip404 {
/**
/**
* The file marker name.
*/
const MARKER = 'Newfold Skip 404 Handling for Static Files';

/**
* Constructor.
*/
/**
* Constructor.
*/
public function __construct() {

$this->addRules();
}


/**
* Add our rules to the .htacces file.
*/
/**
* Add our rules to the .htacces file.
*/
public static function addRules() {
$content = <<<HTACCESS
<IfModule mod_rewrite.c>
Expand Down
5 changes: 3 additions & 2 deletions includes/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use NewfoldLabs\WP\Module\Performance\CacheTypes\CacheBase;
use NewfoldLabs\WP\ModuleLoader\Container;
use WP_Forge\Collection\Collection;

/**
* Cache Manager Class
* Cache manager.
*/
class CacheManager {
/**
Expand Down Expand Up @@ -73,7 +74,7 @@ public function enabledCacheTypes() {
/**
* Get an array of page cache type instances based on the enabled cache types.
*
* @return CacheBase[]
* @return CacheBase[] An array of cache type instances.
*/
public function getInstances() {
$instances = array();
Expand Down
50 changes: 31 additions & 19 deletions includes/CachePurgingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,31 @@
use NewfoldLabs\WP\Module\Performance\Concerns\Purgeable;
use wpscholar\Url;

/**
* Cache purging service.
*/
class CachePurgingService {

/**
* Cache types.
*
* @var CacheBase[]
* @var CacheBase[] $cacheTypes Cache types.
*/
public $cacheTypes = [];
public $cacheTypes = array(); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase

/**
* Constructor.
*
* @param CacheBase[] $cacheTypes
* @param CacheBase[] $cacheTypes Cache types.
*/
public function __construct( array $cacheTypes ) {

$this->cacheTypes = $cacheTypes;
$this->cacheTypes = $cacheTypes; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase

if ( $this->canPurge() ) {

// Handle manual purge requests
add_action( 'init', [ $this, 'manualPurgeRequest' ] );
add_action( 'init', array( $this, 'manualPurgeRequest' ) );

// Handle automatic purging
add_action( 'transition_post_status', array( $this, 'onSavePost' ), 10, 3 );
Expand All @@ -45,7 +48,7 @@ public function __construct( array $cacheTypes ) {
* @return bool
*/
public function canPurge() {
foreach ( $this->cacheTypes as $instance ) {
foreach ( $this->cacheTypes as $instance ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
if ( array_key_exists( Purgeable::class, class_implements( $instance ) ) ) {
return true;
}
Expand Down Expand Up @@ -86,9 +89,11 @@ public function manualPurgeRequest() {
* Purge everything.
*/
public function purgeAll() {
foreach ( $this->cacheTypes as $instance ) {
foreach ( $this->cacheTypes as $instance ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
if ( array_key_exists( Purgeable::class, class_implements( $instance ) ) ) {
/**
* Purgeable instance.
*
* @var Purgeable $instance
*/
$instance->purgeAll();
Expand All @@ -99,12 +104,14 @@ public function purgeAll() {
/**
* Purge a specific URL.
*
* @param string $url The URL to be purged.
* @param string $url The URL to be purged.
*/
public function purgeUrl( $url ) {
foreach ( $this->cacheTypes as $instance ) {
foreach ( $this->cacheTypes as $instance ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
if ( array_key_exists( Purgeable::class, class_implements( $instance ) ) ) {
/**
* Purgeable instance.
*
* @var Purgeable $instance
*/
$instance->purgeUrl( $url );
Expand All @@ -115,9 +122,9 @@ public function purgeUrl( $url ) {
/**
* Purge appropriate caches when a post is updated.
*
* @param string $oldStatus The previous post status
* @param string $newStatus The new post status
* @param \WP_Post $post The post object of the edited or created post
* @param string $oldStatus The previous post status
* @param string $newStatus The new post status
* @param \WP_Post $post The post object of the edited or created post
*/
public function onSavePost( $oldStatus, $newStatus, \WP_Post $post ) {

Expand Down Expand Up @@ -160,13 +167,12 @@ public function onSavePost( $oldStatus, $newStatus, \WP_Post $post ) {
// Purge date archive URL when post is updated.
$year_archive = get_year_link( (int) get_the_date( 'y', $post ) );
$this->purgeUrl( $year_archive );

}

/**
* Purge taxonomy term URL when a term is updated.
*
* @param int $termId Term ID
* @param int $termId Term ID
*/
public function onEditTerm( $termId ) {
$url = get_term_link( $termId );
Expand All @@ -178,7 +184,7 @@ public function onEditTerm( $termId ) {
/**
* Purge a single post when a comment is updated.
*
* @param int $commentId ID of the comment.
* @param int $commentId ID of the comment.
*/
public function onUpdateComment( $commentId ) {
$comment = get_comment( $commentId );
Expand All @@ -190,8 +196,16 @@ public function onUpdateComment( $commentId ) {
}
}

/**
* Purge all caches when an option is updated.
*
* @param string $option Option name.
* @param mixed $oldValue Old option value.
* @param mixed $newValue New option value.
*
* @return bool
*/
public function onUpdateOption( $option, $oldValue, $newValue ) {

// No need to process if nothing was updated
if ( $oldValue === $newValue ) {
return false;
Expand Down Expand Up @@ -298,13 +312,12 @@ public function onUpdateOption( $option, $oldValue, $newValue ) {
$this->purgeAll();

return true;

}

/**
* Checks if a taxonomy is public.
*
* @param string $taxonomy Taxonomy name.
* @param string $taxonomy Taxonomy name.
*
* @return boolean
*/
Expand All @@ -317,5 +330,4 @@ protected function isPublicTaxonomy( $taxonomy ) {

return $public;
}

}
4 changes: 2 additions & 2 deletions includes/CacheTypes/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use function WP_Forge\WP_Htaccess_Manager\removeMarkers;

/**
* Browser cache class
* Browser cache type.
*/
class Browser extends CacheBase {
/**
Expand All @@ -25,7 +25,7 @@ class Browser extends CacheBase {
/**
* Whether or not the code for this cache type should be loaded.
*
* @param Container $container the container.
* @param Container $container Dependency injection container.
*
* @return bool
*/
Expand Down
10 changes: 7 additions & 3 deletions includes/CacheTypes/CacheBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use NewfoldLabs\WP\ModuleLoader\Container;

/**
* Base class for cache types.
*/
abstract class CacheBase {

/**
Expand All @@ -16,9 +19,11 @@ abstract class CacheBase {
/**
* Whether or not the code for this cache type should be loaded.
*
* @return bool
* @param Container $container Dependency injection container.
*
* @return bool True if the cache type should be enabled, false otherwise.
*/
public static function shouldEnable( Container $container ) {
public static function shouldEnable( Container $container ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
return true;
}

Expand All @@ -41,5 +46,4 @@ public function setContainer( Container $container ) {
public function getContainer() {
return $this->container;
}

}
Loading

0 comments on commit e0179f1

Please sign in to comment.