From 6218b64a62d27bc4cfa0c246e57cdef90b198c70 Mon Sep 17 00:00:00 2001 From: Oliver Juhas Date: Wed, 17 Apr 2024 20:36:06 +0200 Subject: [PATCH 1/2] Preventing infinite loop If the remote URL is generated automatically in the theme, it may be even empty. In such case there is an infinite PHP loop produced, which causes memory error. These changes prevent infinite loop from happening. --- wptt-webfont-loader.php | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/wptt-webfont-loader.php b/wptt-webfont-loader.php index 3457217..25a2eec 100644 --- a/wptt-webfont-loader.php +++ b/wptt-webfont-loader.php @@ -3,6 +3,7 @@ * Download webfonts locally. * * @package wptt/font-loader + * @link https://github.com/WPTT/webfont-loader * @license https://opensource.org/licenses/MIT */ @@ -101,9 +102,9 @@ class WPTT_WebFont_Loader { * * @access protected * @since 1.1.0 - * @var string + * @var null|string */ - protected $css; + protected $css = null; /** * Cleanup routine frequency. @@ -138,6 +139,11 @@ public function __construct( $url = '' ) { */ public function get_url() { + // If remote URL is empty just return itself. + if ( empty( $this->remote_url ) ) { + return $this->remote_url; + } + // Check if the local stylesheet exists. if ( $this->local_file_exists() ) { @@ -180,10 +186,20 @@ public function get_local_stylesheet_url() { */ public function get_styles() { + // If remote URL is empty, set empty string. + if ( empty( $this->remote_url ) ) { + $this->css = ''; + } + + // If the CSS is set already, return it. + if ( is_string( $this->css ) ) { + return $this->css; + } + // If we already have the local file, return its contents. - $local_stylesheet_contents = $this->get_local_stylesheet_contents(); - if ( $local_stylesheet_contents ) { - return $local_stylesheet_contents; + $this->css = $this->get_local_stylesheet_contents(); + if ( ! empty( $this->css ) ) { + return $this->css; } // Get the remote URL contents. @@ -220,7 +236,6 @@ public function get_styles() { * @return string|false Returns the remote URL contents. */ public function get_local_stylesheet_contents() { - $local_path = $this->get_local_stylesheet_path(); // Check if the local stylesheet exists. if ( $this->local_file_exists() ) { @@ -232,7 +247,7 @@ public function get_local_stylesheet_contents() { } ob_start(); - include $local_path; + include $this->get_local_stylesheet_path(); return ob_get_clean(); } @@ -464,7 +479,7 @@ protected function write_stylesheet() { // If the folder doesn't exist, create it. if ( ! file_exists( $this->get_fonts_folder() ) ) { - $this->get_filesystem()->mkdir( $this->get_fonts_folder(), FS_CHMOD_DIR ); + $filesystem->mkdir( $this->get_fonts_folder(), FS_CHMOD_DIR ); } // If the file doesn't exist, create it. Return false if it can not be created. @@ -474,7 +489,7 @@ protected function write_stylesheet() { // If we got this far, we need to write the file. // Get the CSS. - if ( ! $this->css ) { + if ( is_null( $this->css ) ) { $this->get_styles(); } @@ -529,6 +544,10 @@ public function set_font_format( $format = 'woff2' ) { /** * Check if the local stylesheet exists. * + * The name of this method is wrong. Should be "no_local_file_exists()" + * as it returns true if the file does NOT exist. + * Keeping the original name not to break 3rd party scripts. + * * @access public * @since 1.1.0 * @return bool From a9652cad7a1deb7c4df48cc5c271dc67c4889190 Mon Sep 17 00:00:00 2001 From: Oliver Juhas Date: Tue, 30 Apr 2024 13:52:02 +0200 Subject: [PATCH 2/2] Removing `is_null()` --- wptt-webfont-loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wptt-webfont-loader.php b/wptt-webfont-loader.php index 25a2eec..920c147 100644 --- a/wptt-webfont-loader.php +++ b/wptt-webfont-loader.php @@ -489,7 +489,7 @@ protected function write_stylesheet() { // If we got this far, we need to write the file. // Get the CSS. - if ( is_null( $this->css ) ) { + if ( null === $this->css ) { $this->get_styles(); }