Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #7071 Clean CSS & fonts for host fonts locally #7110

Draft
wants to merge 6 commits into
base: feature/host-google-fonts
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions inc/Engine/Media/Fonts/Clean/Clean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\Fonts\Clean;

class Clean {
/**
* Filesystem instance
*
* @var Filesystem
*/
private $filesystem;

Check failure on line 12 in inc/Engine/Media/Fonts/Clean/Clean.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Property WP_Rocket\Engine\Media\Fonts\Clean\Clean::$filesystem has unknown class WP_Rocket\Engine\Media\Fonts\Clean\Filesystem as its type.

/**
* Base path for fonts
*
* @var string
*/
private $base_path;

/**
* Constructor
*
* @param Filesystem $filesystem Filesystem instance.
*/
public function __construct( $filesystem ) {

Check failure on line 26 in inc/Engine/Media/Fonts/Clean/Clean.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Parameter $filesystem of method WP_Rocket\Engine\Media\Fonts\Clean\Clean::__construct() has invalid type WP_Rocket\Engine\Media\Fonts\Clean\Filesystem.
$this->filesystem = $filesystem;
$this->base_path = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_PATH', '' ) . 'fonts/' . get_current_blog_id() . '/';
}

/**
* Clean CSS & fonts files stored locally
*
* @return void
*/
public function clean_css_fonts() {
$this->filesystem->delete_all_files_from_directory( $this->base_path );

Check failure on line 37 in inc/Engine/Media/Fonts/Clean/Clean.php

View workflow job for this annotation

GitHub Actions / WPRocket lint with PHP Stan. PHP 8.2 on ubuntu-latest.

Call to method delete_all_files_from_directory() on an unknown class WP_Rocket\Engine\Media\Fonts\Clean\Filesystem.
}

/**
* Clean CSS & fonts files stored locally on option change
*
* @param mixed $old_value Old option value.
* @param mixed $value New option value.
*
* @return void
*/
public function clean_on_option_change( $old_value, $value ) {
if ( ! isset( $old_value['host_fonts_locally'], $value['host_fonts_locally'] ) ) {
return;
}

if ( $old_value['host_fonts_locally'] === $value['host_fonts_locally'] ) {
return;
}

$this->clean_css_fonts();

do_action( 'rocket_fonts_locally_hosted_changed' );
}
}
57 changes: 57 additions & 0 deletions inc/Engine/Media/Fonts/Clean/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\Fonts\Clean;

use WP_Rocket\Event_Management\Subscriber_Interface;

class Subscriber implements Subscriber_Interface {
/**
* Clean instance
*
* @var Clean
*/
private $clean;

/**
* Constructor
*
* @param Clean $clean Clean instance.
*/
public function __construct( Clean $clean ) {
$this->clean = $clean;
}

/**
* Returns an array of events that this subscriber wants to listen to.
*
* @return array
*/
public static function get_subscribed_events(): array {
return [
'rocket_clean_domain' => 'clean_css_fonts',
'update_option_wp_rocket_settings' => [ 'clean_on_option_change', 10, 2 ],
];
}

/**
* Clean CSS & fonts files stored locally
*
* @return void
*/
public function clean_css_fonts() {
$this->clean->clean_css_fonts();
}

/**
* Clean CSS & fonts files stored locally on option change
*
* @param mixed $old_value Old option value.
* @param mixed $value New option value.
*
* @return void
*/
public function clean_on_option_change( $old_value, $value ) {
$this->clean->clean_on_option_change( $old_value, $value );
}
}
1 change: 1 addition & 0 deletions inc/Engine/Optimization/RUCSS/Admin/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static function get_subscribed_events(): array {
'switch_theme' => 'truncate_used_css',
'permalink_structure_changed' => 'truncate_used_css',
'rocket_domain_options_changed' => 'truncate_used_css',
'rocket_host_fonts_locally_changed' => 'truncate_used_css',
'wp_trash_post' => 'delete_used_css_on_update_or_delete',
'delete_post' => 'delete_used_css_on_update_or_delete',
'clean_post_cache' => 'delete_used_css_on_update_or_delete',
Expand Down
Loading