PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
← All changes | classes/class-css.php +27 -31 2.1.8 → 3.4 View file →
@@ -7,8 +7,10 @@
7 7 * @author Tobias Bäthge
8 8 * @since 1.1.0
9 9 */
10 10
11 +declare(strict_types=1);
12 +
11 13 // Prohibit direct script loading.
12 14 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
13 15
14 16 /**
@@ -28,15 +30,13 @@
28 30 *
29 31 * @param string $css CSS code.
30 32 * @return string Sanitized and tidied CSS code.
31 33 */
32 - public function sanitize_css( $css ) {
34 + public function sanitize_css( string $css ): string {
33 35 $csstidy = TablePress::load_class( 'TablePress_CSSTidy', 'class.csstidy.php', 'libraries/csstidy' );
34 36
35 37 // Sanitization and not just tidying for users without enough privileges.
36 38 if ( ! current_user_can( 'unfiltered_html' ) ) {
37 - $csstidy->optimise = new TablePress_CSSTidy_optimise( $csstidy );
38 -
39 39 // Let "arrows" survive, otherwise this might be recognized as the beginning of an HTML tag and removed with other stuff behind it.
40 40 $css = str_replace( '<=', '&lt;=', $css );
41 41 // Remove all HTML tags.
42 42 $css = wp_kses( $css, 'strip' );
@@ -74,11 +74,10 @@
74 74 *
75 75 * @param string $css CSS code.
76 76 * @return string Minified CSS code.
77 77 */
78 - public function minify_css( $css ) {
78 + public function minify_css( string $css ): string {
79 79 $csstidy = TablePress::load_class( 'TablePress_CSSTidy', 'class.csstidy.php', 'libraries/csstidy' );
80 - $csstidy->optimise = new TablePress_CSSTidy_optimise( $csstidy );
81 80 $csstidy->set_cfg( 'remove_bslash', false );
82 81 $csstidy->set_cfg( 'compress_colors', true );
83 82 $csstidy->set_cfg( 'compress_font-weight', true );
84 83 $csstidy->set_cfg( 'lowercase_s', false );
@@ -95,9 +94,12 @@
95 94 $csstidy->set_cfg( 'timestamp', false );
96 95 $csstidy->set_cfg( 'template', 'highest' );
97 96
98 97 $csstidy->parse( $css );
99 - return $csstidy->print->plain();
98 + $css = $csstidy->print->plain();
99 +
100 + // Remove all CSS comments from the minified CSS code, as CSSTidy does not remove those inside a CSS selector.
101 + return preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/\n?!', '', $css );
100 102 }
101 103
102 104 /**
103 105 * Get the location (file path or URL) of the "Custom CSS" file, depending on whether it's a Multisite install or not.
@@ -107,9 +109,9 @@
107 109 * @param string $type "normal" version, "minified" version, or "combined" (with TablePress Default CSS) version.
108 110 * @param string $location "path" or "url", for file path or URL.
109 111 * @return string Full file path or full URL for the "Custom CSS" file.
110 112 */
111 - public function get_custom_css_location( $type, $location ) {
113 + public function get_custom_css_location( string $type, string $location ): string {
112 114 switch ( $type ) {
113 115 case 'combined':
114 116 $file = 'tablepress-combined.min.css';
115 117 break;
@@ -176,18 +178,18 @@
176 178 *
177 179 * @param string $type Optional. Whether to load "normal" version or "minified" version. Default "normal".
178 180 * @return string|false Custom CSS on success, false on error.
179 181 */
180 - public function load_custom_css_from_file( $type = 'normal' ) {
182 + public function load_custom_css_from_file( string $type = 'normal' ) /* : string|false */ {
181 183 $filename = $this->get_custom_css_location( $type, 'path' );
182 184 // Check if file name is valid (0 means yes).
183 185 if ( 0 !== validate_file( $filename ) ) {
184 186 return false;
185 187 }
186 - if ( ! @is_file( $filename ) ) {
188 + if ( ! @is_file( $filename ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
187 189 return false;
188 190 }
189 - if ( ! @is_readable( $filename ) ) {
191 + if ( ! @is_readable( $filename ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
190 192 return false;
191 193 }
192 194 return file_get_contents( $filename );
193 195 }
@@ -201,9 +203,9 @@
201 203 *
202 204 * @param bool $load_rtl Optional. Whether to load LTR or RTL version. Default LTR.
203 205 * @return string|false TablePress Default CSS on success, false on error.
204 206 */
205 - public function load_default_css_from_file( $load_rtl = false ) {
207 + public function load_default_css_from_file( bool $load_rtl = false ) /* : string|false */ {
206 208 $rtl = $load_rtl ? '-rtl' : '';
207 209 $filename = TABLEPRESS_ABSPATH . "css/build/default{$rtl}.css";
208 210 // Check if file name is valid (0 means yes).
209 211 if ( 0 !== validate_file( $filename ) ) {
@@ -208,12 +210,12 @@
208 210 // Check if file name is valid (0 means yes).
209 211 if ( 0 !== validate_file( $filename ) ) {
210 212 return false;
211 213 }
212 - if ( ! @is_file( $filename ) ) {
214 + if ( ! @is_file( $filename ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
213 215 return false;
214 216 }
215 - if ( ! @is_readable( $filename ) ) {
217 + if ( ! @is_readable( $filename ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
216 218 return false;
217 219 }
218 220 return file_get_contents( $filename );
219 221 }
@@ -226,9 +228,9 @@
226 228 * @param string $custom_css_normal Custom CSS code to be saved.
227 229 * @param string $custom_css_minified Minified CSS code to be saved.
228 230 * @return bool True on success, false on failure.
229 231 */
230 - public function save_custom_css_to_file( $custom_css_normal, $custom_css_minified ) {
232 + public function save_custom_css_to_file( string $custom_css_normal, string $custom_css_minified ): bool {
231 233 /**
232 234 * Filters whether the "Custom CSS" code shall be saved to a file on the server.
233 235 *
234 236 * @since 1.1.0
@@ -246,9 +248,9 @@
246 248 /*
247 249 * Do we have credentials already? (Otherwise the form will have been rendered, which is not supported here.)
248 250 * Or, if we have credentials, are they valid?
249 251 */
250 - if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
252 + if ( false === $credentials || ! WP_Filesystem( $credentials ) ) { // @phpstan-ignore argument.type
251 253 ob_end_clean();
252 254 return false;
253 255 }
254 256
@@ -266,9 +268,9 @@
266 268 * @param string $custom_css_normal Custom CSS code to be saved. If empty, files will be deleted.
267 269 * @param string $custom_css_minified Minified CSS code to be saved.
268 270 * @return bool|string True on success, false on failure, or string of HTML for the credentials form for the WP_Filesystem API, if necessary.
269 271 */
270 - public function save_custom_css_to_file_plugin_options( $custom_css_normal, $custom_css_minified ) {
272 + public function save_custom_css_to_file_plugin_options( string $custom_css_normal, string $custom_css_minified ) /* : bool|string */ {
271 273 /** This filter is documented in classes/class-css.php */
272 274 if ( ! apply_filters( 'tablepress_save_custom_css_to_file', true ) ) {
273 275 return false;
274 276 }
@@ -278,18 +280,18 @@
278 280 $credentials = request_filesystem_credentials( '', '', false, '', null, false );
279 281 // Do we have credentials already? Otherwise the form will have been rendered already.
280 282 if ( false === $credentials ) {
281 283 $form_data = ob_get_clean();
282 - $form_data = str_replace( 'name="upgrade" id="upgrade" class="button"', 'name="upgrade" id="upgrade" class="button button-primary button-large"', $form_data );
284 + $form_data = str_replace( 'name="upgrade" id="upgrade" class="button"', 'name="upgrade" id="upgrade" class="components-button is-primary"', $form_data ); // @phpstan-ignore argument.type
283 285 return $form_data;
284 286 }
285 287
286 288 // We have received credentials, but don't know if they are valid yet.
287 - if ( ! WP_Filesystem( $credentials ) ) {
289 + if ( ! WP_Filesystem( $credentials ) ) { // @phpstan-ignore argument.type
288 290 // Credentials failed, so ask again (with $error flag true).
289 291 request_filesystem_credentials( '', '', true, '', null, false );
290 292 $form_data = ob_get_clean();
291 - $form_data = str_replace( 'name="upgrade" id="upgrade" class="button"', 'name="upgrade" id="upgrade" class="button button-primary button-large"', $form_data );
293 + $form_data = str_replace( 'name="upgrade" id="upgrade" class="button"', 'name="upgrade" id="upgrade" class="components-button is-primary"', $form_data ); // @phpstan-ignore argument.type
292 294 return $form_data;
293 295 }
294 296
295 297 // We have valid access to the filesystem now -> try to save the files, or delete them if the "Custom CSS" is empty.
@@ -314,9 +316,9 @@
314 316 * @param string $custom_css_normal Custom CSS code to be saved.
315 317 * @param string $custom_css_minified Minified CSS code to be saved.
316 318 * @return bool True on success, false on failure.
317 319 */
318 - protected function _custom_css_save_helper( $custom_css_normal, $custom_css_minified ) {
320 + protected function _custom_css_save_helper( string $custom_css_normal, string $custom_css_minified ): bool {
319 321 global $wp_filesystem;
320 322
321 323 /*
322 324 * WP_CONTENT_DIR and (FTP-)Content-Dir can be different (e.g. if FTP working dir is /).
@@ -328,14 +330,8 @@
328 330
329 331 $default_css_minified = $this->load_default_css_from_file( false );
330 332 if ( false === $default_css_minified ) {
331 333 $default_css_minified = '';
332 - } else {
333 - // Change relative URLs to web font files to absolute URLs, as combining the CSS files and saving to another directory breaks the relative URLs.
334 - $absolute_path = plugins_url( 'css/build/tablepress.', TABLEPRESS__FILE__ );
335 - // Make the absolute URL protocol-relative to prevent mixed content warnings.
336 - $absolute_path = str_replace( array( 'http:', 'https:' ), '', $absolute_path );
337 - $default_css_minified = str_replace( 'url(tablepress.', 'url(' . $absolute_path, $default_css_minified );
338 334 }
339 335 $file_content = array(
340 336 'normal' => $custom_css_normal,
341 337 'minified' => $custom_css_minified,
@@ -368,9 +364,9 @@
368 364 * @since 1.0.0
369 365 *
370 366 * @return bool True on success, false on failure.
371 367 */
372 - public function delete_custom_css_files() {
368 + public function delete_custom_css_files(): bool {
373 369 // Start capturing the output, to later prevent it.
374 370 ob_start();
375 371 $credentials = request_filesystem_credentials( '', '', false, '', null, false );
376 372
@@ -377,9 +373,9 @@
377 373 /*
378 374 * Do we have credentials already? (Otherwise the form will have been rendered, which is not supported here.)
379 375 * Or, if we have credentials, are they valid?
380 376 */
381 - if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
377 + if ( false === $credentials || ! WP_Filesystem( $credentials ) ) { // @phpstan-ignore argument.type
382 378 ob_end_clean();
383 379 return false;
384 380 }
385 381
@@ -399,9 +395,9 @@
399 395 * @see save_custom_css_to_file_plugin_options()
400 396 *
401 397 * @return bool True on success, false on failure.
402 398 */
403 - protected function _custom_css_delete_helper() {
399 + protected function _custom_css_delete_helper(): bool {
404 400 global $wp_filesystem;
405 401
406 402 /*
407 403 * WP_CONTENT_DIR and (FTP-)Content-Dir can be different (e.g. if FTP working dir is /).
@@ -437,9 +433,9 @@
437 433 * Flush the CSS minification caches of common caching plugins.
438 434 *
439 435 * @since 1.4.0
440 436 */
441 - protected function _flush_caching_plugins_css_minify_caches() {
437 + protected function _flush_caching_plugins_css_minify_caches(): void {
442 438 /** This filter is documented in models/model-table.php */
443 439 if ( ! apply_filters( 'tablepress_flush_caching_plugins_caches', true ) ) {
444 440 return;
445 441 }
@@ -449,9 +445,9 @@
449 445 w3tc_minify_flush();
450 446 }
451 447 // WP Fastest Cache.
452 448 if ( isset( $GLOBALS['wp_fastest_cache'] ) && is_callable( array( $GLOBALS['wp_fastest_cache'], 'deleteCache' ) ) ) {
453 - $GLOBALS['wp_fastest_cache']->deleteCache( true );
449 + $GLOBALS['wp_fastest_cache']->deleteCache( true ); // @phpstan-ignore method.nonObject
454 450 }
455 451 }
456 452
457 453 } // class TablePress_CSS