PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / trunk
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization vtrunk
4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 All 135 releases
← All changes | inc/media_rename/attachment_replace.php +66 -1 4.2.10 → trunk View file →
@@ -82,9 +82,9 @@
82 82 if ( ! $wp_filesystem->move( $this->file['tmp_name'], $original_file, true ) ) {
83 83 return new WP_Error( 'file_error', __( 'Could not move file.', 'optimole-wp' ) );
84 84 }
85 85
86 - $wp_filesystem->chmod( $original_file, FS_CHMOD_FILE );
86 + $permissions_normalized = $this->normalize_file_permissions( $original_file );
87 87
88 88 $this->remove_all_image_sizes();
89 89
90 90 clean_attachment_cache( $this->attachment_id );
@@ -101,9 +101,74 @@
101 101 $this->handle_scaled_images();
102 102
103 103 do_action( 'optml_attachment_replaced', $this->attachment_id );
104 104
105 + if ( ! $permissions_normalized ) {
106 + return new WP_Error( 'file_permissions_error', __( 'Error replacing file', 'optimole-wp' ) );
107 + }
108 +
105 109 return true;
110 + }
111 +
112 + /**
113 + * Normalize the permissions of the replaced file.
114 + *
115 + * @param string $file File path.
116 + *
117 + * @return bool Whether the file ended up with the expected permissions.
118 + */
119 + private function normalize_file_permissions( $file ) {
120 + global $wp_filesystem;
121 +
122 + $mode = defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : 0644;
123 +
124 + $applied = $wp_filesystem->chmod( $file, $mode );
125 +
126 + $reason = '';
127 +
128 + if ( ! $applied || ! $this->has_permissions( $file, $mode ) ) {
129 + // Fallback for transports where the filesystem abstraction can't chmod.
130 + set_error_handler(
131 + function ( $errno, $errstr ) use ( &$reason ) {
132 + $reason = $errstr;
133 +
134 + return true;
135 + }
136 + );
137 +
138 + $applied = chmod( $file, $mode );
139 +
140 + restore_error_handler();
141 + }
142 +
143 + if ( $applied && $this->has_permissions( $file, $mode ) ) {
144 + return true;
145 + }
146 +
147 + if ( OPTML_DEBUG ) {
148 + do_action(
149 + 'optml_log',
150 + sprintf( 'Could not normalize permissions to %o for replaced file %s. %s', $mode, $file, $reason )
151 + );
152 + }
153 +
154 + return false;
155 + }
156 +
157 + /**
158 + * Check the current permissions of a file against an expected mode.
159 + *
160 + * @param string $file File path.
161 + * @param int $mode Expected mode.
162 + *
163 + * @return bool
164 + */
165 + private function has_permissions( $file, $mode ) {
166 + clearstatcache( true, $file );
167 +
168 + $perms = fileperms( $file );
169 +
170 + return false !== $perms && ( $perms & 0777 ) === ( $mode & 0777 );
106 171 }
107 172
108 173 /**
109 174 * Remove all image sizes files.