diff --git a/wp-tevko-responsive-images.php b/wp-tevko-responsive-images.php index 59a3305..46d4163 100644 --- a/wp-tevko-responsive-images.php +++ b/wp-tevko-responsive-images.php @@ -87,3 +87,262 @@ function tevkori_filter_attachment_image_attributes( $attr, $attachment, $size ) return $attr; } + +/** + * Checks if there are images with 'data-sizes' and 'srcset' attributes in the post content. + * + * @since 3.2.0 + * @access private + * + * @return int Number of posts that contain images with 'data-sizes' and 'srcset' attributes. + */ +function _tevkori_find_respimg_attr() { + $matches = get_transient( 'tevkori_find_respimg_attr' ); + + // Strict comparison because the transient value can be 0, but will be false if it has not been set yet. + if ( false === $matches ) { + global $wpdb; + + $string = ']+data-sizes="[^"]+" srcset="[^"]+"'; + + $sql = " + SELECT COUNT(*) + FROM $wpdb->posts + WHERE post_type != 'revision' AND post_content RLIKE %s + "; + + $matches = $wpdb->get_var( $wpdb->prepare( $sql, $string ) ); + + set_transient( 'tevkori_find_respimg_attr', $matches, YEAR_IN_SECONDS ); + } + + return $matches; +} + +/** + * Removes the 'tevkori_find_respimg_attr' transient upon plugin deactivation. + * + * @since 3.2.0 + * @access private + * @see 'register_deactivation_hook' + */ +function _tevkori_delete_transient() { + delete_transient( 'tevkori_find_respimg_attr' ); +} +register_deactivation_hook( __FILE__, '_tevkori_delete_transient' ); + +/** + * Adds a menu page to the Tools menu if images with 'data-sizes' and 'srcset' attributes + * have been found and user has sufficient capability. + * + * @since 3.2.0 + * @access private + * @see 'add_management_page()' + */ +function _tevkori_add_menu_page() { + $matches = _tevkori_find_respimg_attr(); + + if ( $matches ) { + add_management_page( + 'RICG Responsive Images', + 'RICG Responsive Images', + 'activate_plugins', + 'ricg-responsive-images', + '_tevkori_menu_page' + ); + } +} +add_action( 'admin_menu', '_tevkori_add_menu_page' ); + +/** + * Callback function that outputs the content for the menu page. + * + * @since 3.2.0 + * @access private + * @see '_tevkori_add_menu_page()' + */ +function _tevkori_menu_page() { + global $wp_version; + + $matches = _tevkori_find_respimg_attr(); + ?> +
We have found with images that contain data-sizes
and srcset
attributes. We strongly recommend to remove those attributes from images in your posts.
To make an image responsive the img
element needs to have a srcset
and sizes
attribute. Prior to version 2.5, this plugin added a srcset
and data-sizes
attribute while inserting the image in the content and replaced data-sizes
by sizes
before a post was being displayed. Since version 2.5 those attributes are no longer added while inserting an image in the content. Instead, both srcset
and sizes
are added to images by using a filter before a post is being displayed, but not if an image already has a srcset
attribute.
The plugin still replaces data-sizes
by sizes
to make sure that images that have been inserted while version 2.4 or older was active have both the srcset
and sizes
attribute. However, after deactivating the plugin those images won't get a sizes
attribute anymore while they do have a srcset
. This is invalid markup and results in images being displayed at a wrong size!
Besides this, the functions that calculate the values for the srcset
and sizes
attributes have been improved over time and filter hooks have been added to make it possible to change the values in specific situations. As long as images already have a srcset
attribute you won't benefit from these improvements.
Therefor it's strongly recommended to remove data-sizes
and srcset
attributes from img
elements in the post content. You can do this automatically with the cleanup tool on this page or manually.
As from WordPress 4.4 the srcset
and sizes
attributes are added by WordPress itself. This is done in exactly the same way as it was done by this plugin, meaning that no attributes are added if an image already has a srcset
attribute.
No, you would no longer need to use this plugin to make images responsive if the data-sizes
and srcset
attributes have been removed from all images. However, you can keep using it to benefit from the following two features:
See the plugin page and the plugin documentation for more information.
+ + +This tool automatically removes data-sizes
and srcset
attributes from all images in the content of your posts. It updates the post content in the database. This can't be undone and if something goes wrong you might lose your content so it's very important to make a backup of your database before proceeding!
After succesfully removing these attributes from all images this page will disappear from the Tools menu and you will no longer see a warning on the dashboard and plugin menu page.
+ + + +To manually remove the data-sizes
and srcset
attributes you have to edit each post and page that contains images that were inserted while version 2.4 or older of this plugin was active. In the editor switch from the "Visual" to "Text" tab in the top right corner in order to see image markup instead of the images itself. Look for <img ... >
elements and delete data-sizes="..."
and srcset="..."
.
After you have removed these attributes from all images you can run this check. If there are no more images with these attributes this page will disappear from the Tools menu and you will no longer see a warning on the dashboard and plugin menu page.
+ + + +If you have any questions, please post a message on the support forum of this plugin.
+The RICG Responsive Images plugin needs your attention. Please visit the plugin page in the Tools menu.
Done! All data-sizes
and srcset
attributes have been succesfully removed from your images!
Check finished. Not all data-sizes
and srcset
attributes have been removed yet!
Please make a backup of your database before using the cleanup tool!
Something went wrong! We recommend to check your posts if something is broken, revert to the backup of your database if necessary, and remove the data-sizes
and srcset
attributes manually.