• Hello,

    I’m trying to finally fix the caching with Domain Mapping on, here you can see how far I’ve gotten. Unfortunately, it only works when you’re on the mapped domain. Not on the original subdomain.

    I’ve added notes on why I think it doesn’t work. I hope you can elaborate on this issue and maybe together we can find a fix.

    Thanks!

    add_action( 'save_post', 'wap_mapped_clear', 20);
    
    /**
     * Forces an extra flush on mapped domain.
     *
     * @requires plugin Domain Mapping by WPMUdev
     *
     * @since 1.0.2
     */
    function wap_mapped_clear() {
    	//* Check for domain-mapping plugin
    	if ( is_plugin_active( 'domain-mapping/domain-mapping.php' ) ) {
    		global $wpdb,$blog_id;
    
    		$ismapped = wp_cache_get('wap_mapped_clear_' . $blog_id, 'domain_mapping' );
    		if ( false === $ismapped ) {
    			$ismapped = $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM {$wpdb->base_prefix}domain_mapping WHERE blog_id = %d", $blog_id ) ); //string
    			wp_cache_set('wap_mapped_clear_' . $blog_id, $ismapped, 'domain_mapping', 3600 ); // 1 hour
    		}
    
    		//* string $ismapped, if mapped != ''
    		// We shouldn't flush the object cache here, otherwise the wp_cache_set is useless above this.
    		if ( !empty($ismapped) ) {
    			add_action( 'save_post', 'wap_w3tc_flush_all', 21 ); // We just flush it entirely. But only if the domain is mapped! :D
    		//	add_action( 'save_post', 'wap_w3tc_flush_page_mapped', 21 ); // Unfortunately, not working. I'll try to discuss this with Frederick.
    		}
    	}
    }
    
    /**
     * Flushes entire page cache & the specific page on both mapped / non mapped url.
     *
     * Fixes Domain Mapping mixed cache
     *
     * @param array $post_ID 	the updated post_ID
     *
     * @since 1.1.0
     */
    function wap_w3tc_flush_page_mapped( $post_ID ) {
    	global $wpdb, $blog_id, $current_blog;
    
    	$post = get_post( $post_ID );
    
    	//* Check if subdomain install, else just flush all
    	if ((defined('SUBDOMAIN_INSTALL') && SUBDOMAIN_INSTALL) || (defined('VHOST') && VHOST == 'yes')) {
    
    		$originaldomain = $current_blog->domain;
    
    		//* Get mapped domain
    		$mappeddomain = wp_cache_get('wap_mapped_clear_' . $blog_id, 'domain_mapping' );
    		if ( false === $mappeddomain ) {
    			$mappeddomain = $wpdb->get_var( $wpdb->prepare( "SELECT domain FROM {$wpdb->base_prefix}domain_mapping WHERE blog_id = %d", $blog_id ) ); //string
    			wp_cache_set('wap_mapped_clear_' . $blog_id, $mappeddomain, 'domain_mapping', 3600 ); // 1 hour
    		}
    
    		//* Get scheme setting of orginal domain
    		$mappedscheme = wp_cache_get('wap_mapped_scheme_' . $blog_id, 'domain_mapping' );
    		if ( false === $scheme ) {
    			$mappedscheme = $wpdb->get_var( $wpdb->prepare( "SELECT scheme FROM {$wpdb->base_prefix}domain_mapping WHERE blog_id = %d", $blog_id ) ); //string
    			wp_cache_set('wap_mapped_scheme_' . $blog_id, $mappedscheme, 'domain_mapping', 3600 ); // 1 hour
    		}
    
    		//* Get scheme of mapped domain
    		if ($mappedscheme === '1') {
    			$scheme_mapped = 'http://'; //https
    		} else if ($mappedscheme === '0') {
    			$scheme_mapped = 'http://';
    		}
    
    		//* Get scheme of orginal domain
    		if ( method_exists( 'Domainmap_Plugin', 'instance' ) ) {
    			$domainmap_instance = Domainmap_Plugin::instance();
    			$schemeoriginal = $domainmap_instance->get_option("map_force_frontend_ssl") ? 'http://' : 'http://'; //https : http
    		} else {
    			$schemeoriginal = is_ssl() ? 'http://' : 'http://'; //Fallback, not too reliable. //https : http
    		}
    
    		$relative_url_slash_it = wp_make_link_relative( trailingslashit( get_permalink( $post_ID ) ) );
    		$relative_url = wp_make_link_relative( get_permalink( $post_ID ) );
    
    		if ( $post->ID == get_option( 'page_on_front' ) ) {
    			$geturls = array(
    				$mappeddomain, // example: mappedomain.com
    				$scheme_mapped . $mappeddomain, // example: http://mappedomain.com
    				$originaldomain, // example: subdomain.maindomain.com
    				$schemeoriginal . $originaldomain, // example: https://subdomain.maindomain.com
    			);
    		} else {
    			$geturls = array (
    				$mappeddomain . $relative_url, // example: mappedomain.com/some-post(/)
    				$mappeddomain . $relative_url_slash_it, // example: mappedomain.com/some-post/
    
    				$scheme_mapped .  $mappeddomain . $relative_url, // example: http://mappedomain.com/some-post(/)
    				$scheme_mapped .  $mappeddomain . $relative_url_slash_it, // example: http://mappedomain.com/some-post/
    
    				$originaldomain . $relative_url, // example: subdomain.maindomain.com/some-post(/)
    				$originaldomain . $relative_url_slash_it, // example: subdomain.maindomain.com/some-post/
    
    				$schemeoriginal . $originaldomain . $relative_url, // example: https://subdomain.maindomain.com/some-post(/)
    				$schemeoriginal . $originaldomain . $relative_url_slash_it, // example: https://subdomain.maindomain.com/some-post/
    			);
    		}
    
    		//* Flush both mapped and original
    		foreach ( $geturls as $key => $url ) {
    			if ( function_exists( 'w3tc_pgcache_flush_url' ) )
    				w3tc_pgcache_flush_url( $url );
    
    			if ( function_exists( 'w3tc_pgcache_flush_post' ) ) {
    				w3tc_pgcache_flush_post( $post_ID );
    				w3tc_pgcache_flush_post( $post_id = null );
    			}
    
    			if ( function_exists( 'w3_instance' ) ) {
    				$w3_pgcache = w3_instance('W3_CacheFlush');
    				return $w3_pgcache->prime_post($post_ID);
    			}
    		}
    
    		/**
    		 * There are only two hard things in Computer Science: cache invalidation and naming things.
    		 * -- Phil Karlton
    		 *
    		 * Why this doesn't work as expected:
    		 *  + Cache IS invalidated. This is good.
    		 *	- Old cache file/chunk still exists.
    		 *	- Cache ISN'T rebuilt. So the invalid cache is loaded upon request until the ORIGINAL url fetches it.
    		 *	- ORIGINAL url can't be requested (on visit), this is because the domain is mapped.
    		 *	- Old cache stays in there until explicit expiration date sent in the cron as set in the w3 total cache settings.
    		 *
    		 * The solution:
    		 *	+ Add a cache rebuild mechanism that we can fire directly
    		 *	+ Make urls' be more explicit. This is a hard issue and shouldn't be addressed by unique ID's, but rather a find and replace (for URLs).
    		 *
    		 * This is a serious bug that should've been addressed a few years ago. This url clearing thing simply doesn't work.
    		 */
    
    		/**
    		 * The original functions
    		 * @param url
    		 *
    		 * @example
    		 */
    		/*
    		function flush_url($url) {
    			static $cache, $mobile_groups, $referrer_groups, $encryptions, $compressions;
    			if (!isset($cache)) $cache = $this->_get_cache();
    			if (!isset($mobile_groups)) $mobile_groups  = $this->_get_mobile_groups();
    			if (!isset($referrer_groups)) $referrer_groups = $this->_get_referrer_groups();
    			if (!isset($encryptions)) $encryptions = $this->_get_encryptions();
    			if (!isset($compressions)) $compressions = $this->_get_compressions();
    
    			$this->_flush_url($url, $cache, $mobile_groups, $referrer_groups, $encryptions, $compressions);
    		}
    
    		function _flush_url($url, $cache, $mobile_groups, $referrer_groups, $encryptions, $compressions) {
    			foreach ($mobile_groups as $mobile_group) {
    				foreach ($referrer_groups as $referrer_group) {
    					foreach ($encryptions as $encryption) {
    						foreach ($compressions as $compression) {
    							$page_key = $this->_get_page_key($mobile_group, $referrer_group, $encryption, $compression, false, $url);
    							$cache->delete($page_key);
    						}
    					}
    				}
    			}
    		}
    		*/
    
    	} else {
    
    		// Purge the entire page cache (current domain)
    		// This should be more elaborated, but I don't have the resources or time to apply this.
    		if ( function_exists( 'w3tc_pgcache_flush' ) )
    			w3tc_pgcache_flush();
    
    	}
    }

    https://wordpress.org/plugins/w3-total-cache/

  • The topic ‘Trying to clear cache through w3tc_pgcache_flush_url, not working’ is closed to new replies.