Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dieppon/78dd30de409cd590d85e5d5daeb5c0e9 to your computer and use it in GitHub Desktop.
Save dieppon/78dd30de409cd590d85e5d5daeb5c0e9 to your computer and use it in GitHub Desktop.
Fix autoptimize making new files with 600 permissions
From 78f71273f3152048786edac8690ffae22cbd1001 Mon Sep 17 00:00:00 2001
From: Victor Dieppa Garriga <[email protected]>
Date: Fri, 24 Apr 2020 12:46:23 +0100
Subject: [PATCH] Fix autoptimize making new files with 600 permissions
---
.../autoptimize/classes/autoptimizeCache.php | 33 +++++++++++++------
.../plugins/autoptimize/config/default.php | 4 +--
2 files changed, 25 insertions(+), 12 deletions(-)
diff --git docroot/wp-content/plugins/autoptimize/classes/autoptimizeCache.php docroot/wp-content/plugins/autoptimize/classes/autoptimizeCache.php
index 28a46c35..3fccd241 100644
--- docroot/wp-content/plugins/autoptimize/classes/autoptimizeCache.php
+++ docroot/wp-content/plugins/autoptimize/classes/autoptimizeCache.php
@@ -90,6 +90,8 @@ class autoptimizeCache
*/
public function cache( $data, $mime )
{
+ global $wp_filesystem;
+
// off by default; check if cachedirs exist every time before caching
//
// to be activated for users that experience these ugly errors;
@@ -104,27 +106,27 @@ class autoptimizeCache
$phpcode = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $file );
$phpcode = str_replace( array( '%%CONTENT%%', 'exit;' ), array( $mime, '' ), $phpcode );
- file_put_contents( $this->cachedir . $this->filename, $phpcode );
- file_put_contents( $this->cachedir . $this->filename . '.none', $data );
+ $wp_filesystem->put_contents( $this->cachedir . $this->filename, $phpcode );
+ $wp_filesystem->put_contents( $this->cachedir . $this->filename . '.none', $data );
} else {
// Write code to cache without doing anything else.
- file_put_contents( $this->cachedir . $this->filename, $data );
+ $wp_filesystem->put_contents( $this->cachedir . $this->filename, $data );
// save fallback .js or .css file if filter true (to be false by default) but not if snippet or single.
if ( self::do_fallback() && strpos( $this->filename, '_snippet_' ) === false && strpos( $this->filename, '_single_' ) === false ) {
$_extension = pathinfo( $this->filename, PATHINFO_EXTENSION );
$_fallback_file = AUTOPTIMIZE_CACHEFILE_PREFIX . 'fallback.' . $_extension;
if ( ! file_exists( $this->cachedir . $_extension . '/' . $_fallback_file ) ) {
- file_put_contents( $this->cachedir . $_extension . '/' . $_fallback_file, $data );
+ $wp_filesystem->put_contents( $this->cachedir . $_extension . '/' . $_fallback_file, $data );
}
}
if ( apply_filters( 'autoptimize_filter_cache_create_static_gzip', false ) ) {
// Create an additional cached gzip file.
- file_put_contents( $this->cachedir . $this->filename . '.gz', gzencode( $data, 9, FORCE_GZIP ) );
+ $wp_filesystem->put_contents( $this->cachedir . $this->filename . '.gz', gzencode( $data, 9, FORCE_GZIP ) );
// If PHP Brotli extension is installed, create an additional cached Brotli file.
if ( function_exists( 'brotli_compress' ) ) {
- file_put_contents( $this->cachedir . $this->filename . '.br', brotli_compress( $data, 11, BROTLI_GENERIC ) );
+ $wp_filesystem->put_contents( $this->cachedir . $this->filename . '.br', brotli_compress( $data, 11, BROTLI_GENERIC ) );
}
}
}
@@ -520,10 +522,16 @@ class autoptimizeCache
*/
public static function cacheavail()
{
+ global $wp_filesystem;
+
if ( false === autoptimizeCache::check_and_create_dirs() ) {
return false;
}
+ if ( null === $wp_filesystem ) {
+ WP_Filesystem();
+ }
+
// Using .htaccess inside our cache folder to overrule wp-super-cache.
$htaccess = AUTOPTIMIZE_CACHE_DIR . '/.htaccess';
if ( ! is_file( $htaccess ) ) {
@@ -592,7 +600,7 @@ class autoptimizeCache
if ( self::do_fallback() ) {
$content .= "\nErrorDocument 404 " . trailingslashit( parse_url( content_url(), PHP_URL_PATH ) ) . 'autoptimize_404_handler.php';
}
- @file_put_contents( $htaccess, $content ); // @codingStandardsIgnoreLine
+ $wp_filesystem->put_contents( $htaccess, $content );
}
if ( self::do_fallback() ) {
@@ -609,6 +617,8 @@ class autoptimizeCache
* Return bool
*/
public static function check_fallback_php() {
+ global $wp_filesystem;
+
$_fallback_filename = 'autoptimize_404_handler.php';
$_fallback_php = trailingslashit( WP_CONTENT_DIR ) . $_fallback_filename;
$_fallback_status = true;
@@ -620,7 +630,7 @@ class autoptimizeCache
if ( apply_filters( 'autoptimize_filter_cache_fallback_log_errors', false ) ) {
$_fallback_php_contents = str_replace( '// error_log', 'error_log', $_fallback_php_contents );
}
- $_fallback_status = file_put_contents( $_fallback_php, $_fallback_php_contents );
+ $_fallback_status = $wp_filesystem->put_contents( $_fallback_php, $_fallback_php_contents );
}
return $_fallback_status;
@@ -666,9 +676,12 @@ class autoptimizeCache
*/
protected static function check_cache_dir( $dir )
{
+ global $wp_filesystem;
+
// Try creating the dir if it doesn't exist.
if ( ! file_exists( $dir ) ) {
- @mkdir( $dir, 0775, true ); // @codingStandardsIgnoreLine
+ $wp_filesystem->mkdir( $dir );
+ $wp_filesystem->chmod( 0775, false, true );
if ( ! file_exists( $dir ) ) {
return false;
}
@@ -682,7 +695,7 @@ class autoptimizeCache
// Create an index.html in there to avoid prying eyes!
$idx_file = rtrim( $dir, '/\\' ) . '/index.html';
if ( ! is_file( $idx_file ) ) {
- @file_put_contents( $idx_file, '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>' ); // @codingStandardsIgnoreLine
+ $wp_filesystem->put_contents( $idx_file, '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>' );
}
return true;
diff --git docroot/wp-content/plugins/autoptimize/config/default.php docroot/wp-content/plugins/autoptimize/config/default.php
index 7080855f..2f4a0c85 100644
--- docroot/wp-content/plugins/autoptimize/config/default.php
+++ docroot/wp-content/plugins/autoptimize/config/default.php
@@ -76,12 +76,12 @@ if (($modTimeMatch)||($eTagMatch)) {
if($encoding != 'none' && $iscompressed == false)
{
//Write the content we sent
- file_put_contents(__FILE__.'.'.$encoding,$contents);
+ $wp_filesystem->put_contents(__FILE__.'.'.$encoding,$contents);
//And write the new content
$flag = ($encoding == 'gzip' ? FORCE_DEFLATE : FORCE_GZIP);
$ext = ($encoding == 'gzip' ? 'deflate' : 'gzip');
$contents = gzencode($code,9,$flag);
- file_put_contents(__FILE__.'.'.$ext,$contents);
+ $wp_filesystem->put_contents(__FILE__.'.'.$ext,$contents);
}
}
--
2.17.2 (Apple Git-113)
@futtta
Copy link

futtta commented Apr 24, 2020

Thanks for that @dieppon :-)

Some questions:

  • did you have this issue from the start, or did this suddenly occur?
  • are you (or your hoster) also on cloudlinux Imunify 360?
  • re. the code: where exactly are you setting the 600 permissions?
  • re. the code: do you consider this a drop-in replacement?

@dieppon
Copy link
Author

dieppon commented Apr 24, 2020

did you have this issue from the start, or did this suddenly occur?

it start happening when we updated the plugin from 2.3.2 to 2.6.2

are you (or your hoster) also on cloudlinux Imunify 360?

I'm not sure about that.

re. the code: where exactly are you setting the 600 permissions?

Im not, without the patch that is permission the css and js ends up with.

re. the code: do you consider this a drop-in replacement?

I'm just sharing the change we did the plugin that works for us. Check if it works for you!

@futtta
Copy link

futtta commented Apr 24, 2020 via email

@dieppon
Copy link
Author

dieppon commented Apr 24, 2020

Without the patch the permission is 600 with the patch is 644.

@futtta
Copy link

futtta commented Apr 24, 2020

Well, as I wrote; OS level stuff (umask), there have been no changes in how AO writes to cache since I took over (back in 2013) :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment