From b74aec2257f04b41f380263e7bf2ed21e15df4dc Mon Sep 17 00:00:00 2001 From: Jasper de Groot Date: Sat, 13 Feb 2016 22:07:35 +0100 Subject: [PATCH] Add a tool to remove data-sizes and srcset from image elements Fixes #178 --- wp-tevko-responsive-images.php | 259 +++++++++++++++++++++++++++++++++ 1 file changed, 259 insertions(+) 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(); + ?> +
+

RICG Responsive Images Plugin

+ +
+

We have found with images that contain data-sizes and srcset attributes. We strongly recommend to remove those attributes from images in your posts.

+
+ +

Explanation

+ +

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.

+ + = 4.4 ) : ?> +

WordPress 4.4+

+ +

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.

+ +

Do I Still Need This Plugin After Removing The Attributes?

+ +

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:

+ +
    +
  1. The plugin loads the Picturefill polyfill, a small JavaScript file that makes responsive images work on older browsers that don't offer native support
  2. +
  3. Advanced image compression (via opt-in)
  4. +
+ +

See the plugin page and the plugin documentation for more information.

+ + +

Cleanup Tool

+ +

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.

+ +
+ + + + +
+ +

Manually Remove

+ +

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.

+ +
+ + + +
+ +

Support

+ +

If you have any questions, please post a message on the support forum of this plugin.

+
+ ]+data-sizes="[^"]+" srcset="[^"]+"'; + + $sql = " + SELECT ID, post_content + FROM $wpdb->posts + WHERE post_type != 'revision' AND post_content RLIKE %s + "; + + $results = $wpdb->get_results( $wpdb->prepare( $sql, $string ), ARRAY_N ); + + if ( ! empty( $results ) ) { + foreach ( $results as $result ) { + $post_content = preg_replace( '/]+?) data-sizes="([^"]+?)" srcset="([^"]+?)"/', ' $result[0], + 'post_content' => $post_content, + ); + + wp_update_post( $post ); + } + } + + delete_transient( 'tevkori_find_respimg_attr' ); + + $matches = _tevkori_find_respimg_attr(); + + if ( $matches ) { + wp_redirect( admin_url( 'tools.php?page=ricg-responsive-images&ricg-cleanup=error' ) ); + } else { + wp_redirect( admin_url( 'plugins.php?ricg-cleanup=success' ) ); + } + } else { + wp_redirect( admin_url( 'tools.php?page=ricg-responsive-images&ricg-cleanup=backup' ) ); + } + + exit(); +} +add_action( 'admin_post_tevkori_cleanup_post_content', '_tevkori_cleanup_post_content' ); + +/** + * Triggers a new check to see if there are images with 'data-sizes' and 'srcset' attributes in the post content. + * + * @since 3.2.0 + * @access private + * @see 'admin_post' + */ +function _tevkori_check_post_content() { + check_admin_referer( 'tevkori_check_post_content', 'tevkori_check_post_content_nonce' ); + + delete_transient( 'tevkori_find_respimg_attr' ); + + $matches = _tevkori_find_respimg_attr(); + + if ( $matches ) { + wp_redirect( admin_url( 'tools.php?page=ricg-responsive-images&ricg-cleanup=notdone' ) ); + } else { + wp_redirect( admin_url( 'plugins.php?ricg-cleanup=success' ) ); + } + + exit(); +} +add_action( 'admin_post_tevkori_check_post_content', '_tevkori_check_post_content' ); + +/** + * Displays admin notices. + * + * @since 3.2.0 + * @access private + * @see 'admin_notices' + */ +function _tevkori_admin_notices() { + global $pagenow; + + $matches = _tevkori_find_respimg_attr(); + + if ( $matches && ( $pagenow == 'plugins.php' || $pagenow == 'index.php' ) ) { + echo '

The RICG Responsive Images plugin needs your attention. Please visit the plugin page in the Tools menu.

'; + } + + if ( $pagenow == 'plugins.php' && isset( $_GET['ricg-cleanup'] ) ) { + if ( 'success' == $_GET['ricg-cleanup'] ) { + echo '

Done! All data-sizes and srcset attributes have been succesfully removed from your images!

'; + } + } + if ( $pagenow == 'tools.php' && isset( $_GET['ricg-cleanup'] ) ) { + $notice = $_GET['ricg-cleanup']; + + if ( 'notdone' == $notice ) { + echo '

Check finished. Not all data-sizes and srcset attributes have been removed yet!

'; + } elseif ( 'backup' == $notice ) { + echo '

Please make a backup of your database before using the cleanup tool!

'; + } elseif ( 'error' == $notice ) { + echo '

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.

'; + } + } +} +add_action( 'admin_notices', '_tevkori_admin_notices' );