diff --git a/class-srcset-callback.php b/class-srcset-callback.php new file mode 100644 index 0000000..016283f --- /dev/null +++ b/class-srcset-callback.php @@ -0,0 +1,90 @@ +attachments_loop = $attachments_loop; + $upload_dir = wp_upload_dir(); + $upload_dir = $upload_dir['baseurl'] . '/'; + + while( $attachments_loop->have_posts() ) : + $attachments_loop->the_post(); + + // Get the image meta data. + if ( is_array( $img_meta = wp_get_attachment_metadata( get_the_ID() ) ) ) { + + // Get base URL. + $base_url = $upload_dir . pathinfo( $img_meta['file'], PATHINFO_DIRNAME ); + $base_url = untrailingslashit( $base_url ) . '/'; + + // Add default/full size image. + $this->attachments_array[ $upload_dir . $img_meta['file'] ] = get_the_ID(); + + // Build from sizes array. + $img_sizes = $img_meta['sizes']; + + foreach( $img_sizes as $img_size ) { + $this->attachments_array[ $base_url . $img_size['file'] ] = get_the_ID(); + } + } + endwhile; + + wp_reset_postdata(); + } + + /** + * Callback function for tevkori_filter_content_images. + * + * @since 3.0 + * + * @see tevkori_filter_content_images + * @param array $matches Array containing the regular expression matches. + */ + public function callback( $matches ) { + $atts = $matches[1]; + $sizes = $srcset = ''; + + // Check if srcset attribute is not already present. + if ( false !== strpos( 'srcset="', $atts ) ) { + + // Get the url of the original image. + preg_match( '/src="(.+?)(\-([0-9]+)x([0-9]+))?(\.[a-zA-Z]{3,4})"/i', $atts, $url_matches ); + + $url = $url_matches[1] . $url_matches[5]; + + // Get the image ID. + $id = $this->attachments_array[$url]; + + if ( $id ) { + + // Use the width and height from the image url. + if ( $url_matches[3] && $url_matches[4] ) { + $size = array( + (int) $url_matches[3], + (int) $url_matches[4] + ); + } else { + $size = 'full'; + } + + // Get the srcset string. + $srcset_string = tevkori_get_srcset_string( $id, $size ); + + if ( $srcset_string ) { + $srcset = ' ' . $srcset_string; + + // Get the sizes string. + $sizes_string = tevkori_get_sizes_string( $id, $size ); + + if ( $sizes_string && ! preg_match( '/sizes="([^"]+)"/i', $atts ) ) { + $sizes = ' ' . $sizes_string; + } + } + } + } + + return ''; + } +} \ No newline at end of file diff --git a/wp-tevko-responsive-images.php b/wp-tevko-responsive-images.php index 016c7cc..bb266c2 100644 --- a/wp-tevko-responsive-images.php +++ b/wp-tevko-responsive-images.php @@ -19,6 +19,8 @@ defined( 'ABSPATH' ) or die( "No script kiddies please!" ); // List includes +require_once( plugin_dir_path( __FILE__ ) . 'class-srcset-callback.php' ); + if ( class_exists( 'Imagick' ) ) { require_once( plugin_dir_path( __FILE__ ) . 'class-respimg.php' ); require_once( plugin_dir_path( __FILE__ ) . 'class-wp-image-editor-respimg.php' ); @@ -308,70 +310,24 @@ function tevkori_get_src_sizes( $id, $size = 'thumbnail' ) { * @param string $content The raw post content to be filtered. */ function tevkori_filter_content_images( $content ) { + preg_match_all( '/src="([^"]*)"/', $content, $matches ); + + $urls = array_pop( $matches ); + + $attachments = tevkori_attachment_urls_to_loop( $urls ); + + $content_filter = new WP_Ricg_Content_Filter( $attachments ); + return preg_replace_callback( '/]+)>/i', // Don't use an anonymous callback function because it isn't supported by PHP 5.2. - 'tevkori_filter_content_images_callback', + array( $content_filter, 'callback' ), $content ); } add_filter( 'the_content', 'tevkori_filter_content_images', 5, 1 ); -/** - * Callback function for tevkori_filter_content_images. - * - * @since 3.0 - * - * @see tevkori_filter_content_images - * @param array $matches Array containing the regular expression matches. - */ -function tevkori_filter_content_images_callback( $matches ) { - $atts = $matches[1]; - $sizes = $srcset = ''; - - // Check if srcset attribute is not already present. - if ( false !== strpos( 'srcset="', $atts ) ) { - - // Get the url of the original image. - preg_match( '/src="(.+?)(\-([0-9]+)x([0-9]+))?(\.[a-zA-Z]{3,4})"/i', $atts, $url_matches ); - - $url = $url_matches[1] . $url_matches[5]; - - // Get the image ID. - $id = attachment_url_to_postid( $url ); - - if ( $id ) { - - // Use the width and height from the image url. - if ( $url_matches[3] && $url_matches[4] ) { - $size = array( - (int) $url_matches[3], - (int) $url_matches[4] - ); - } else { - $size = 'full'; - } - - // Get the srcset string. - $srcset_string = tevkori_get_srcset_string( $id, $size ); - - if ( $srcset_string ) { - $srcset = ' ' . $srcset_string; - - // Get the sizes string. - $sizes_string = tevkori_get_sizes_string( $id, $size ); - - if ( $sizes_string && ! preg_match( '/sizes="([^"]+)"/i', $atts ) ) { - $sizes = ' ' . $sizes_string; - } - } - } - } - - return ''; -} - /** * Filter to add srcset and sizes attributes to post thumbnails and gallery images. * @@ -416,3 +372,57 @@ function tevkori_wp_image_editors( $editors ) { return $editors; } add_filter( 'wp_image_editors', 'tevkori_wp_image_editors' ); + +/** + * Convert multiple attachement URLs to thier ID. + * + * @since 3.0 + * + * @return array IDs. + **/ +function tevkori_attachment_urls_to_loop( $urls ) { + global $wpdb; + + if ( is_string( $urls ) ) { + return tevkori_attachment_urls_to_loop( array( $urls ) ); + } + + if ( ! is_array( $urls ) ) { + $urls = array(); + } + + $dir = wp_upload_dir(); + $paths = $urls; + + $site_url = parse_url( $dir['url'] ); + + foreach ( $paths as $k => $path ) { + $image_path = parse_url( $path ); + + // Force the protocols to match if needed. + if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) { + $path = str_replace( $image_path['scheme'], $site_url['scheme'], $path ); + } + + if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) { + $path = substr( $path, strlen( $dir['baseurl'] . '/' ) ); + } + + $paths[$k] = $path; + } + + $attachments = new WP_Query(array( + 'post_type' => 'attachment', + 'meta_query' => array( + array( + 'key' => '_wp_attached_file', + 'value' => $paths, + 'compare' => 'in' + ), + ), + 'posts_per_page' => '-1', + 'post_status' => 'inherit', + )); + + return $attachments; +}