class-activation.php
2 years ago
class-admin-interface.php
2 years ago
class-common-methods.php
2 years ago
class-content-management.php
2 years ago
class-custom-code.php
2 years ago
class-deactivation.php
2 years ago
class-disable-components.php
2 years ago
class-login-logout.php
2 years ago
class-optimizations.php
2 years ago
class-security.php
2 years ago
class-settings-fields-render.php
2 years ago
class-settings-sanitization.php
2 years ago
class-settings-sections-fields.php
2 years ago
class-utilities.php
2 years ago
class-optimizations.php
277 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class related to Optimizations features |
| 7 | * |
| 8 | * @since 3.8.0 |
| 9 | */ |
| 10 | class Optimizations |
| 11 | { |
| 12 | private $current_url_path ; |
| 13 | /** |
| 14 | * Handler for image uploads. Convert and resize images. |
| 15 | * |
| 16 | * @since 4.3.0 |
| 17 | */ |
| 18 | public function image_upload_handler( $upload ) |
| 19 | { |
| 20 | $applicable_mime_types = array( |
| 21 | 'image/bmp', |
| 22 | 'image/x-ms-bmp', |
| 23 | 'image/png', |
| 24 | 'image/jpeg', |
| 25 | 'image/jpg', |
| 26 | 'image/webp' |
| 27 | ); |
| 28 | |
| 29 | if ( in_array( $upload['type'], $applicable_mime_types ) ) { |
| 30 | global $png_has_transparency ; |
| 31 | // Exlude from conversion and resizing images with filenames ending with '-nr', e.g. birds-nr.png |
| 32 | if ( false !== strpos( $upload['file'], '-nr.' ) ) { |
| 33 | return $upload; |
| 34 | } |
| 35 | // Convert BMP |
| 36 | if ( 'image/bmp' === $upload['type'] || 'image/x-ms-bmp' === $upload['type'] ) { |
| 37 | $upload = $this->maybe_convert_image( 'bmp', $upload ); |
| 38 | } |
| 39 | // Convert PNG without transparency |
| 40 | if ( 'image/png' === $upload['type'] ) { |
| 41 | $upload = $this->maybe_convert_image( 'png', $upload ); |
| 42 | } |
| 43 | // At this point, BMPs and non-transparent PNGs are already converted to JPGs, unless excluded with '-nr' suffix. |
| 44 | // Let's perform resize operation as needed, i.e. if image dimension is larger than specified |
| 45 | $mime_types_to_resize = array( 'image/jpeg', 'image/jpg', 'image/png' ); |
| 46 | |
| 47 | if ( !is_wp_error( $upload ) && in_array( $upload['type'], $mime_types_to_resize ) && filesize( $upload['file'] ) > 0 ) { |
| 48 | // https://developer.wordpress.org/reference/classes/wp_image_editor/ |
| 49 | $wp_image_editor = wp_get_image_editor( $upload['file'] ); |
| 50 | |
| 51 | if ( !is_wp_error( $wp_image_editor ) ) { |
| 52 | $image_size = $wp_image_editor->get_size(); |
| 53 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 54 | $max_width = $options['image_max_width']; |
| 55 | $max_height = $options['image_max_height']; |
| 56 | // Check upload image's dimension and only resize if larger than the defined max dimension |
| 57 | |
| 58 | if ( isset( $image_size['width'] ) && $image_size['width'] > $max_width || isset( $image_size['height'] ) && $image_size['height'] > $max_height ) { |
| 59 | $wp_image_editor->resize( $max_width, $max_height, false ); |
| 60 | // false is for no cropping |
| 61 | $wp_image_editor->set_quality( 90 ); |
| 62 | // default is 82 |
| 63 | $wp_image_editor->save( $upload['file'] ); |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | } |
| 71 | |
| 72 | return $upload; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Convert BMP or PNG without transparency into JPG |
| 77 | * |
| 78 | * @since 4.3.0 |
| 79 | */ |
| 80 | public function maybe_convert_image( $file_extension, $upload ) |
| 81 | { |
| 82 | global $png_has_transparency ; |
| 83 | $image_object = null; |
| 84 | // Get image object from uploaded BMP/PNG |
| 85 | if ( 'bmp' === $file_extension ) { |
| 86 | // Generate image object from BMP for conversion to JPG later |
| 87 | |
| 88 | if ( function_exists( 'imagecreatefrombmp' ) ) { |
| 89 | // PHP >= v7.2 |
| 90 | $image_object = imagecreatefrombmp( $upload['file'] ); |
| 91 | } else { |
| 92 | // PHP < v7.2 |
| 93 | require_once ASENHA_PATH . 'includes/bmp-to-image-object.php'; |
| 94 | $image_object = bmp_to_image_object( $upload['file'] ); |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | |
| 99 | if ( 'png' === $file_extension ) { |
| 100 | // Detect alpha/transparency in PNG |
| 101 | $png_has_transparency = false; |
| 102 | // We assume GD library is present, so 'imagecreatefrompng' function is available |
| 103 | // Generate image object from PNG for potential conversion to JPG later. |
| 104 | $image_object = imagecreatefrompng( $upload['file'] ); |
| 105 | // Get image dimension |
| 106 | list( $width, $height ) = getimagesize( $upload['file'] ); |
| 107 | // Run through pixels until transparent pixel is found |
| 108 | for ( $x = 0 ; $x < $width ; $x++ ) { |
| 109 | for ( $y = 0 ; $y < $height ; $y++ ) { |
| 110 | $pixel_color_index = imagecolorat( $image_object, $x, $y ); |
| 111 | $pixel_rgba = imagecolorsforindex( $image_object, $pixel_color_index ); |
| 112 | // array of red, green, blue and alpha values |
| 113 | |
| 114 | if ( $pixel_rgba['alpha'] > 0 ) { |
| 115 | // a pixel with alpha/transparency has been found |
| 116 | // alpha value range from 0 (completely opaque) to 127 (fully transparent). |
| 117 | // Ref: https://www.php.net/manual/en/function.imagecolorallocatealpha.php |
| 118 | $png_has_transparency = true; |
| 119 | break 2; |
| 120 | // Break both 'for' loops |
| 121 | } |
| 122 | |
| 123 | } |
| 124 | } |
| 125 | // Do not convert PNG with alpha/transparency |
| 126 | if ( $png_has_transparency ) { |
| 127 | return $upload; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | $wp_uploads = wp_upload_dir(); |
| 132 | $old_filename = wp_basename( $upload['file'] ); |
| 133 | // Assign new, unique file name for the converted image |
| 134 | // $new_filename = wp_basename( str_ireplace( '.' . $file_extension, '.jpg', $old_filename ) ); |
| 135 | $new_filename = str_ireplace( '.' . $file_extension, '.jpg', $old_filename ); |
| 136 | $new_filename = wp_unique_filename( dirname( $upload['file'] ), $new_filename ); |
| 137 | // When conversion from BMP/PNG to JPG is successful. Last parameter is JPG quality (0-100). |
| 138 | |
| 139 | if ( imagejpeg( $image_object, $wp_uploads['path'] . '/' . $new_filename, 90 ) ) { |
| 140 | unlink( $upload['file'] ); |
| 141 | // delete original BMP/PNG |
| 142 | // Add converted JPG info into $upload |
| 143 | $upload['file'] = $wp_uploads['path'] . '/' . $new_filename; |
| 144 | $upload['url'] = $wp_uploads['url'] . '/' . $new_filename; |
| 145 | $upload['type'] = 'image/jpeg'; |
| 146 | } |
| 147 | |
| 148 | return $upload; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Limit the number of revisions for post types |
| 153 | * |
| 154 | * @since 3.7.0 |
| 155 | */ |
| 156 | public function limit_revisions_to_max_number( $num, $post ) |
| 157 | { |
| 158 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 159 | $revisions_max_number = $options['revisions_max_number']; |
| 160 | $for_post_types = $options['enable_revisions_control_for']; |
| 161 | // Assemble single-dimensional array of post type slugs for which revisinos is being limited |
| 162 | $limited_post_types = array(); |
| 163 | foreach ( $for_post_types as $post_type_slug => $post_type_is_limited ) { |
| 164 | if ( $post_type_is_limited ) { |
| 165 | $limited_post_types[] = $post_type_slug; |
| 166 | } |
| 167 | } |
| 168 | // Change revisions number to keep if set for the post type as such |
| 169 | $post_type = $post->post_type; |
| 170 | if ( in_array( $post_type, $limited_post_types ) ) { |
| 171 | $num = $revisions_max_number; |
| 172 | } |
| 173 | return $num; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Maybe modify heartbeat tick frequency based on settings for each location |
| 178 | * |
| 179 | * @since 3.8.0 |
| 180 | */ |
| 181 | public function maybe_modify_heartbeat_frequency( $settings ) |
| 182 | { |
| 183 | if ( wp_doing_cron() ) { |
| 184 | return $settings; |
| 185 | } |
| 186 | $this->get_url_path(); |
| 187 | // defines $current_url_path |
| 188 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 189 | // Disable heartbeat autostart |
| 190 | $settings['autostart'] = false; |
| 191 | |
| 192 | if ( is_admin() ) { |
| 193 | |
| 194 | if ( '/wp-admin/post.php' == $this->current_url_path || '/wp-admin/post-new.php' == $this->current_url_path ) { |
| 195 | // Maybe modify interval on post edit screens |
| 196 | if ( 'modify' == $options['heartbeat_control_for_post_edit'] ) { |
| 197 | $settings['minimalInterval'] = absint( $options['heartbeat_interval_for_post_edit'] ); |
| 198 | } |
| 199 | } else { |
| 200 | // Maybe modify interval on admin pages |
| 201 | if ( 'modify' == $options['heartbeat_control_for_admin_pages'] ) { |
| 202 | $settings['minimalInterval'] = absint( $options['heartbeat_interval_for_admin_pages'] ); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | } else { |
| 207 | // Maybe modify interval on the frontend |
| 208 | if ( 'modify' == $options['heartbeat_control_for_frontend'] ) { |
| 209 | $settings['minimalInterval'] = absint( $options['heartbeat_interval_for_frontend'] ); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return $settings; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Maybe disable heartbeat ticks based on settings for each location |
| 218 | * |
| 219 | * @since 3.8.0 |
| 220 | */ |
| 221 | public function maybe_disable_heartbeat() |
| 222 | { |
| 223 | global $pagenow ; |
| 224 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 225 | |
| 226 | if ( is_admin() ) { |
| 227 | |
| 228 | if ( 'post.php' == $pagenow || 'post-new.php' == $pagenow ) { |
| 229 | // Maybe disable on post creation / edit screens |
| 230 | |
| 231 | if ( 'disable' == $options['heartbeat_control_for_post_edit'] ) { |
| 232 | wp_deregister_script( 'heartbeat' ); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | } else { |
| 237 | // Maybe disable on the rest of admin pages |
| 238 | |
| 239 | if ( 'disable' == $options['heartbeat_control_for_admin_pages'] ) { |
| 240 | wp_deregister_script( 'heartbeat' ); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | } |
| 245 | |
| 246 | } else { |
| 247 | // Maybe disable on the frontend |
| 248 | |
| 249 | if ( 'disable' == $options['heartbeat_control_for_frontend'] ) { |
| 250 | wp_deregister_script( 'heartbeat' ); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | } |
| 255 | |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Set current location |
| 260 | * Supported locations [editor,dashboard,frontend] |
| 261 | */ |
| 262 | public function get_url_path() |
| 263 | { |
| 264 | global $pagenow ; |
| 265 | |
| 266 | if ( isset( $_SERVER['HTTP_HOST'] ) ) { |
| 267 | $url = (( isset( $_SERVER['HTTPS'] ) ? 'https' : 'http' )) . '://' . $_SERVER["HTTP_HOST"] . '' . $_SERVER["REQUEST_URI"]; |
| 268 | } else { |
| 269 | $url = get_admin_url() . $pagenow; |
| 270 | } |
| 271 | |
| 272 | $request_path = parse_url( $url, PHP_URL_PATH ); |
| 273 | // e.g. '/wp-admin/post.php' |
| 274 | $this->current_url_path = $request_path; |
| 275 | } |
| 276 | |
| 277 | } |