Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter wilrevehl

    (@wilrevehl)

    Looks like it ultimately uses this: wp_prepare_attachment_for_js

    Thread Starter wilrevehl

    (@wilrevehl)

    I got back into this issue today and here’s what I found:

    1. In WP_Customize_Media_Control line 748, the wp_prepare_attachment_for_js param $value is injecting the url string of the attachment and not the ID
    2. wp_prepare_attachment_for_js looks for a post ID of the attachment so this thing can be flexible for sizes.
    3. Because the wrong type of param is injected, wp_prepare_attachment_for_js fails right away and becomes useless.

    We need to figure out what causes the url and not the id to be used.

    Thread Starter wilrevehl

    (@wilrevehl)

    Update, attachment_url_to_postid fails to return a post_id when your plugin is activated, at least in the case where cloudfront/custom domain is being used.

    Thread Starter wilrevehl

    (@wilrevehl)

    Hopeful news. I was able to run a crude test with add_filter( ‘upload_dir’, ‘s3_upload_dir’, 1) where I preg_replace the get_site_url with my cloudfront one. This resolves the aforementioned. I’ll see if there’s a place I can hook into your plugin to attach it to those settings dynamically.

    Thread Starter wilrevehl

    (@wilrevehl)

    Alright, here’s a fix. It hasn’t been tested for empty or much of anything outside of our environment, so use at your discretion:

    if ( !class_exists('Go_AWS_S3_CF_Extension') ) :
    
    class Go_AWS_S3_CF_Extension {
    
    	protected static $_instance = null;
    
    	function __construct() {
    		//wait for the initiation of the AWS S3 plugin
    		add_action( 'aws_init', array( $this, 'init' ), 30 );
    	}
    
    	public static function instance() {
    		if ( is_null( self::$_instance ) ) {
    			self::$_instance = new self();
    		}
    		return self::$_instance;
    	}
    
    	/**
    	* Init this plugin
    	*
    	*/
    	public function init() {
    		//add filter for upload_dir so we can edit the url to use the S3/CF one
    		//https://ww.wp.xz.cn/support/topic/theme-customizer-image-previews-broken?replies=4#post-7112563
    		add_filter( 'upload_dir', array( $this, 'upload_dir' ), 1 );
    	}
    
    	/**
    	* Filter for WP's upload_dir
    	*
    	* hooked: upload_dir
    	* @see https://developer.ww.wp.xz.cn/reference/hooks/upload_dir/
    	* @returns array directory object
    	*/
    	public function upload_dir( $dir_obj ) {
    		//get the current site's url
    		$site_url = get_site_url();
    		//set replacement pattern
    		$url_pattern = '~'.$site_url.'~';
    		//replace first instance of string when S3 is active but the site is using the site_url
    		$dir_obj['url'] = preg_replace($url_pattern, $this->get_s3_url(), $dir_obj['url'], 1);
    		$dir_obj['baseurl'] = preg_replace($url_pattern, $this->get_s3_url(), $dir_obj['baseurl'], 1);
    
    		return $dir_obj;
    	}
    
    	/**
    	* Get AWS S3/CF URL using the plugin's global
    	*
    	* @returns string s3 url
    	* @see plugins\amazon-s3-and-cloudfront\wordpress-s3.php::48
    	*/
    	private function get_s3_url() {
    		global $as3cf;
    
    		$scheme = $as3cf->get_s3_url_scheme();
    		$bucket = $as3cf->get_setting( 'bucket' );
    		$region = $as3cf->get_setting( 'region' );
    		$domain = $as3cf->get_s3_url_domain( $bucket, $region );
    		$url = $scheme . '://' . $domain;
    		return $url;
    	}
    
    }
    endif;
    
    /**
     * Create instance of Go_AWS_S3_CF_Extension
     */
    $go_aws_s3_cf_extension = Go_AWS_S3_CF_Extension::instance();

    Cheers,

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Theme Customizer Image Previews broken’ is closed to new replies.