Skip to content

Commit d5a7150

Browse files
authored
Merge pull request #25 from webmandesign/fix/prevent-infinite-loop
Preventing infinite loop
2 parents 7dd2385 + a9652ca commit d5a7150

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

wptt-webfont-loader.php

+28-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Download webfonts locally.
44
*
55
* @package wptt/font-loader
6+
* @link https://github.com/WPTT/webfont-loader
67
* @license https://opensource.org/licenses/MIT
78
*/
89

@@ -101,9 +102,9 @@ class WPTT_WebFont_Loader {
101102
*
102103
* @access protected
103104
* @since 1.1.0
104-
* @var string
105+
* @var null|string
105106
*/
106-
protected $css;
107+
protected $css = null;
107108

108109
/**
109110
* Cleanup routine frequency.
@@ -138,6 +139,11 @@ public function __construct( $url = '' ) {
138139
*/
139140
public function get_url() {
140141

142+
// If remote URL is empty just return itself.
143+
if ( empty( $this->remote_url ) ) {
144+
return $this->remote_url;
145+
}
146+
141147
// Check if the local stylesheet exists.
142148
if ( $this->local_file_exists() ) {
143149

@@ -180,10 +186,20 @@ public function get_local_stylesheet_url() {
180186
*/
181187
public function get_styles() {
182188

189+
// If remote URL is empty, set empty string.
190+
if ( empty( $this->remote_url ) ) {
191+
$this->css = '';
192+
}
193+
194+
// If the CSS is set already, return it.
195+
if ( is_string( $this->css ) ) {
196+
return $this->css;
197+
}
198+
183199
// If we already have the local file, return its contents.
184-
$local_stylesheet_contents = $this->get_local_stylesheet_contents();
185-
if ( $local_stylesheet_contents ) {
186-
return $local_stylesheet_contents;
200+
$this->css = $this->get_local_stylesheet_contents();
201+
if ( ! empty( $this->css ) ) {
202+
return $this->css;
187203
}
188204

189205
// Get the remote URL contents.
@@ -220,7 +236,6 @@ public function get_styles() {
220236
* @return string|false Returns the remote URL contents.
221237
*/
222238
public function get_local_stylesheet_contents() {
223-
$local_path = $this->get_local_stylesheet_path();
224239

225240
// Check if the local stylesheet exists.
226241
if ( $this->local_file_exists() ) {
@@ -232,7 +247,7 @@ public function get_local_stylesheet_contents() {
232247
}
233248

234249
ob_start();
235-
include $local_path;
250+
include $this->get_local_stylesheet_path();
236251
return ob_get_clean();
237252
}
238253

@@ -464,7 +479,7 @@ protected function write_stylesheet() {
464479

465480
// If the folder doesn't exist, create it.
466481
if ( ! file_exists( $this->get_fonts_folder() ) ) {
467-
$this->get_filesystem()->mkdir( $this->get_fonts_folder(), FS_CHMOD_DIR );
482+
$filesystem->mkdir( $this->get_fonts_folder(), FS_CHMOD_DIR );
468483
}
469484

470485
// If the file doesn't exist, create it. Return false if it can not be created.
@@ -474,7 +489,7 @@ protected function write_stylesheet() {
474489

475490
// If we got this far, we need to write the file.
476491
// Get the CSS.
477-
if ( ! $this->css ) {
492+
if ( null === $this->css ) {
478493
$this->get_styles();
479494
}
480495

@@ -529,6 +544,10 @@ public function set_font_format( $format = 'woff2' ) {
529544
/**
530545
* Check if the local stylesheet exists.
531546
*
547+
* The name of this method is wrong. Should be "no_local_file_exists()"
548+
* as it returns true if the file does NOT exist.
549+
* Keeping the original name not to break 3rd party scripts.
550+
*
532551
* @access public
533552
* @since 1.1.0
534553
* @return bool

0 commit comments

Comments
 (0)