• Resolved jannesmannes

    (@jannesmannes)


    Hi, very nice plugin you’ve created.
    I would like to suggest an improvement to make it easier to extend it: change the protected method CDN_Enabler_Rewriter::rewrite_url() to a public method. This way we can use it to filter URL’s only from our own plugin/theme code.

    Cheers!
    Jan

Viewing 3 replies - 1 through 3 (of 3 total)
  • Anonymous User 16850768

    (@anonymized-16850768)

    Thank you for the feedback, we are happy to hear that. CDN Enabler has had a major refactor. To know what would be best, what does an example string look like that you’re trying to rewrite?

    Thread Starter jannesmannes

    (@jannesmannes)

    We extend the WP API with custom fields, some of these fields contain image URL’s of image HTML tags with srcsets. For now we wrote the following code to achieve that:

    
    class Cdn {
        public static function addHooks() {
            add_filter( 'wp_get_attachment_image_src', [ Cdn::class, 'maybeRewriteUrl' ], 10, 1 );
            add_filter( 'wp_calculate_image_srcset', [ Cdn::class, 'imageSrcSet' ], 10, 5 );
            add_filter( 'acf/format_value', [ Cdn::class, 'formatAcfValue' ], 10, 3 );
        }
    
        public static function maybeRewriteUrl( $image ) {
            if ( class_exists( 'CDN_Enabler' ) ) {
                $rewrite = \CDN_Enabler::get_rewriter();
    
                $image[0] = $rewrite->rewrite_url( $image );
            }
    
            return $image;
        }
    
        public static function imageSrcSet( $sources, $size_array, $image_src, $image_meta, $attachment_id ) {
            if ( class_exists( 'CDN_Enabler' ) ) {
                $rewrite = \CDN_Enabler::get_rewriter();
                $sources = array_map( function ( $src ) use ( $rewrite ) {
                    $image = [
                        0 => $src['url'],
                    ];
    
                    $src['url'] = $rewrite->rewrite_url( $image );
    
                    return $src;
                }, $sources );
            }
    
            return $sources;
        }
    
        public static function formatAcfValue( $value, $post_id, $field ) {
            if ( $field['type'] === 'wysiwyg' && class_exists( 'CDN_Enabler' ) ) {
                $rewrite = \CDN_Enabler::get_rewriter();
    
                $value = $rewrite->rewrite( $value );
            }
    
            return $value;
        }
    }
    
    Anonymous User 16850768

    (@anonymized-16850768)

    Thanks for the additional information. On all of the WordPress API tests performed CDN Enabler is now rewriting the data retrieved (compared to only rewriting data retrieved from the the_content filter hook in version 1.0.9). Are you able to test if this is the case with version 2.0.0 without the snippet above? (Please note that you won’t want to use that snippet in version 2.0.0 due to some of the methods that have been removed.)

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘public method CDN_Enabler_Rewriter::rewrite_url()’ is closed to new replies.