Page Optimize - Version 0.4.3

Version Description

Download this release

Release Info

Developer aidvu
Plugin Icon wp plugin Page Optimize
Version 0.4.3
Comparing to
See all releases

Code changes from version 0.4.2 to 0.4.3

Files changed (3) hide show
  1. page-optimize.php +5 -2
  2. readme.txt +7 -1
  3. service.php +13 -16
page-optimize.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Page Optimize
4
  Plugin URI: https://wordpress.org/plugins/page-optimize/
5
  Description: Optimizes JS and CSS for faster page load and render in the browser.
6
  Author: Automattic
7
- Version: 0.4.2
8
  Author URI: http://automattic.com/
9
  */
10
 
@@ -17,6 +17,10 @@ if ( ! defined( 'PAGE_OPTIMIZE_ABSPATH' ) ) {
17
  define( 'PAGE_OPTIMIZE_ABSPATH', ABSPATH );
18
  }
19
 
 
 
 
 
20
  define( 'PAGE_OPTIMIZE_CRON_CACHE_CLEANUP_JOB', 'page_optimize_cron_cache_cleanup' );
21
 
22
  // TODO: Copy tests from nginx-http-concat and/or write them
@@ -24,7 +28,6 @@ define( 'PAGE_OPTIMIZE_CRON_CACHE_CLEANUP_JOB', 'page_optimize_cron_cache_cleanu
24
  // TODO: Make concat URL dir configurable
25
  if ( isset( $_SERVER['REQUEST_URI'] ) && '/_static/' === substr( $_SERVER['REQUEST_URI'], 0, 9 ) ) {
26
  require_once __DIR__ . '/service.php';
27
- page_optimize_service_request();
28
  exit;
29
  }
30
 
4
  Plugin URI: https://wordpress.org/plugins/page-optimize/
5
  Description: Optimizes JS and CSS for faster page load and render in the browser.
6
  Author: Automattic
7
+ Version: 0.4.3
8
  Author URI: http://automattic.com/
9
  */
10
 
17
  define( 'PAGE_OPTIMIZE_ABSPATH', ABSPATH );
18
  }
19
 
20
+ if ( ! defined( 'PAGE_OPTIMIZE_CSS_MINIFY' ) ) {
21
+ define( 'PAGE_OPTIMIZE_CSS_MINIFY', false );
22
+ }
23
+
24
  define( 'PAGE_OPTIMIZE_CRON_CACHE_CLEANUP_JOB', 'page_optimize_cron_cache_cleanup' );
25
 
26
  // TODO: Copy tests from nginx-http-concat and/or write them
28
  // TODO: Make concat URL dir configurable
29
  if ( isset( $_SERVER['REQUEST_URI'] ) && '/_static/' === substr( $_SERVER['REQUEST_URI'], 0, 9 ) ) {
30
  require_once __DIR__ . '/service.php';
 
31
  exit;
32
  }
33
 
readme.txt CHANGED
@@ -4,7 +4,7 @@ Tags: performance
4
  Requires at least: 5.3
5
  Tested up to: 5.3
6
  Requires PHP: 7.2
7
- Stable tag: 0.4.2
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -33,6 +33,12 @@ To change the cache location, set this constant to the absolute filesystem path
33
 
34
  To disable caching, set this constant to `false`. Please note that disabling Page Optimize caching may negatively impact performance unless you are caching elsewhere.
35
 
 
 
 
 
 
 
36
  == Testing ==
37
 
38
  To test features without enabling them for the entire site, you may append query params to a WordPress post or page URL. For example, to test enabling JavaScript concatenation for `https://example.com/blog/`, you can use the URL `https://example.com/blog/?concat-js=1`.
4
  Requires at least: 5.3
5
  Tested up to: 5.3
6
  Requires PHP: 7.2
7
+ Stable tag: 0.4.3
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
33
 
34
  To disable caching, set this constant to `false`. Please note that disabling Page Optimize caching may negatively impact performance unless you are caching elsewhere.
35
 
36
+ = PAGE_OPTIMIZE_CSS_MINIFY =
37
+
38
+ Page Optimize has CSS Minification capabilities which are off by default.
39
+
40
+ If you're using caching, and not minifying CSS elsewhere, it is recommended to enable it by setting it to `true`.
41
+
42
  == Testing ==
43
 
44
  To test features without enabling them for the entire site, you may append query params to a WordPress post or page URL. For example, to test enabling JavaScript concatenation for `https://example.com/blog/`, you can use the URL `https://example.com/blog/?concat-js=1`.
service.php CHANGED
@@ -5,9 +5,6 @@ $page_optimize_types = array(
5
  'js' => 'application/javascript'
6
  );
7
 
8
- // TODO: Provide a settings button to clear the cache
9
- // TODO: Should we provide a default at all? Is this a reasonable default?
10
-
11
  function page_optimize_service_request() {
12
  $use_cache = defined( 'PAGE_OPTIMIZE_CACHE_DIR' ) && ! empty( PAGE_OPTIMIZE_CACHE_DIR );
13
  if ( $use_cache && ! is_dir( PAGE_OPTIMIZE_CACHE_DIR ) && ! mkdir( PAGE_OPTIMIZE_CACHE_DIR, 0775, true ) ) {
@@ -51,14 +48,11 @@ function page_optimize_service_request() {
51
 
52
  $etag = '"' . md5( file_get_contents( $cache_file ) ) . '"';
53
 
54
- ob_start( 'ob_gzhandler' );
55
  header( 'X-Page-Optimize: cached' );
56
  header( 'Cache-Control: max-age=' . 31536000 );
57
  header( 'ETag: ' . $etag );
58
 
59
  echo file_get_contents( $cache_file ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- We need to trust this unfortunately.
60
- $output = ob_get_clean();
61
- echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- We need to trust this unfortunately.
62
  die();
63
  }
64
  }
@@ -86,7 +80,6 @@ function page_optimize_service_request() {
86
 
87
  function page_optimize_build_output() {
88
  global $page_optimize_types;
89
- ob_start( 'ob_gzhandler' );
90
 
91
  require_once __DIR__ . '/cssmin/cssmin.php';
92
 
@@ -152,7 +145,11 @@ function page_optimize_build_output() {
152
  $pre_output = '';
153
  $output = '';
154
 
155
- $css_minify = new tubalmartin\CssMin\Minifier;
 
 
 
 
156
 
157
  foreach ( $args as $uri ) {
158
  $fullpath = page_optimize_get_path( $uri );
@@ -243,7 +240,9 @@ function page_optimize_build_output() {
243
  );
244
  }
245
 
246
- $buf = $css_minify->run( $buf );
 
 
247
  }
248
 
249
  if ( $page_optimize_types['js'] === $mime_type ) {
@@ -259,11 +258,9 @@ function page_optimize_build_output() {
259
  "Content-Type: $mime_type",
260
  );
261
 
262
- echo $pre_output . $output;
263
-
264
  return array(
265
  'headers' => $headers,
266
- 'content' => ob_get_clean(),
267
  );
268
  }
269
 
@@ -308,11 +305,9 @@ function page_optimize_get_path( $uri ) {
308
  }
309
 
310
  if ( defined( 'PAGE_OPTIMIZE_CONCAT_BASE_DIR' ) ) {
311
- if ( file_exists( PAGE_OPTIMIZE_CONCAT_BASE_DIR . "/$uri" ) ) {
312
- $path = realpath( PAGE_OPTIMIZE_CONCAT_BASE_DIR . "/$uri" );
313
- }
314
 
315
- if ( empty( $path ) && file_exists( PAGE_OPTIMIZE_ABSPATH . "/$uri" ) ) {
316
  $path = realpath( PAGE_OPTIMIZE_ABSPATH . "/$uri" );
317
  }
318
  } else {
@@ -329,3 +324,5 @@ function page_optimize_get_path( $uri ) {
329
 
330
  return $path;
331
  }
 
 
5
  'js' => 'application/javascript'
6
  );
7
 
 
 
 
8
  function page_optimize_service_request() {
9
  $use_cache = defined( 'PAGE_OPTIMIZE_CACHE_DIR' ) && ! empty( PAGE_OPTIMIZE_CACHE_DIR );
10
  if ( $use_cache && ! is_dir( PAGE_OPTIMIZE_CACHE_DIR ) && ! mkdir( PAGE_OPTIMIZE_CACHE_DIR, 0775, true ) ) {
48
 
49
  $etag = '"' . md5( file_get_contents( $cache_file ) ) . '"';
50
 
 
51
  header( 'X-Page-Optimize: cached' );
52
  header( 'Cache-Control: max-age=' . 31536000 );
53
  header( 'ETag: ' . $etag );
54
 
55
  echo file_get_contents( $cache_file ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- We need to trust this unfortunately.
 
 
56
  die();
57
  }
58
  }
80
 
81
  function page_optimize_build_output() {
82
  global $page_optimize_types;
 
83
 
84
  require_once __DIR__ . '/cssmin/cssmin.php';
85
 
145
  $pre_output = '';
146
  $output = '';
147
 
148
+ $should_minify_css = defined( 'PAGE_OPTIMIZE_CSS_MINIFY' ) && ! empty( PAGE_OPTIMIZE_CSS_MINIFY );
149
+
150
+ if ( $should_minify_css ) {
151
+ $css_minify = new tubalmartin\CssMin\Minifier;
152
+ }
153
 
154
  foreach ( $args as $uri ) {
155
  $fullpath = page_optimize_get_path( $uri );
240
  );
241
  }
242
 
243
+ if ( $should_minify_css ) {
244
+ $buf = $css_minify->run( $buf );
245
+ }
246
  }
247
 
248
  if ( $page_optimize_types['js'] === $mime_type ) {
258
  "Content-Type: $mime_type",
259
  );
260
 
 
 
261
  return array(
262
  'headers' => $headers,
263
+ 'content' => $pre_output . $output,
264
  );
265
  }
266
 
305
  }
306
 
307
  if ( defined( 'PAGE_OPTIMIZE_CONCAT_BASE_DIR' ) ) {
308
+ $path = realpath( PAGE_OPTIMIZE_CONCAT_BASE_DIR . "/$uri" );
 
 
309
 
310
+ if ( false === $path ) {
311
  $path = realpath( PAGE_OPTIMIZE_ABSPATH . "/$uri" );
312
  }
313
  } else {
324
 
325
  return $path;
326
  }
327
+
328
+ page_optimize_service_request();