CDN Enabler's rewriting is quite simplistic, however – it simply checks for and rewrites any applicable URLs within the entire HTML of the page. In some circumstances, this is undesirable. For example, my Divi code snippet module is for displaying source code "as is", and URLs included in that code should not be modified by CDN Enabler. Since CDN Enabler itself doesn't provide a way of preventing the rewriting short of manually excluding every URL used in the source code, I needed a way to programmatically disable CDN Enabler's rewriting as appropriate. Here's a couple of solutions I came up with:
Disabling Rewriting on an Entire Page
1 add_filter('template_redirect', 'disable_cdn_enabler_rewrites', 9); 2 3 function disable_cdn_enabler_rewrites($content) { 4 remove_action('template_redirect', 'CDN_Enabler::handle_rewrite_hook', 10); 5 }
Disabling Rewriting for Some Content / URLs Only
To address this, I came up with this improved solution. It's more complicated, but basically it grabs the content of the raw content of the post, uses a modified version of CDN Enabler's own code to get any URLs within that content, and then adds them as exclusions to CDN Enabler's option (via a filter). The end result is that URLs in the content assigned to the $excluded_content variable won't be converted to CDN URLs. In this example I've assigned the entire page content to $excluded_content, but really you'd want to extract some specific part of the page to exclude from processing (for example, when I use it, I extract the code snippet code from the module's shortcode, should it appear within the page).
The only thing you should need to modify for your own needs is the body of the pre_get_options function to get the content / URLs you wish to exclude.
Here's the code:
1 add_action('template_redirect', array(new cdn_enabler_fix(), 'pre_get_options'), 9); 2 3 class cdn_enabler_fix { 4 5 private $excluded_urls = array(); 6 7 public function __construct() {} 8 9 public function pre_get_options() { 10 11 // Get the post content 12 global $post; 13 if (!isset($post) || !isset($post->ID)) { return; } 14 $content = get_post_field('post_content', $post->ID); 15 16 // Get and store the content to exclude from CDN Enabler URL replacement 17 $excluded_content = $content 18 19 // Get URLs to exclude 20 $this->excluded_urls = $this->get_urls($excluded_content); 21 22 // Filter the CDN Enabler option to add the exclusions 23 add_filter('option_cdn_enabler', array($this, 'add_exclusions')); 24 } 25 26 function add_exclusions($options) { 27 28 if (!is_array($options)) { return $options; } 29 30 31 // Add the excluded URLs to the CDN Enabler option 32 $urls = implode(',', $this->excluded_urls); 33 $options['excludes'] = empty($options['excludes'])?$urls:$options['excludes'].','.$urls; 34 35 return $options; 36 } 37 38 // Extract a list of URLs from the excluded content that CDN enabler could rewrite 39 // - Based on CDN Enabler's own regex code (v1.0.7) 40 public function get_urls($content) { 41 42 $options = wp_parse_args( 43 get_option('cdn_enabler', array()), 44 array( 45 'url' => get_option('home'), 46 'dirs' => 'wp-content,wp-includes', 47 'excludes' => '.php', 48 'relative' => 1, 49 'https' => 0, 50 'keycdn_api_key' => '', 51 'keycdn_zone_id' => '', 52 ) 53 ); 54 55 // check if HTTPS and use CDN over HTTPS enabled 56 if (!$options['https'] && isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on') { 57 return array(); 58 } 59 60 // get dir scope in regex format 61 $input = explode(',', $options['dirs']); 62 if ($options['dirs'] == '' || count($input) < 1) { 63 return 'wp\-content|wp\-includes'; 64 } 65 $dirs = implode('|', array_map('quotemeta', array_map('trim', $input))); 66 67 $url = quotemeta(get_option('home')); 68 $relative_url = substr($url, strpos($url, '//')); 69 70 $blog_url = $options['https'] 71 ? '(https?:|)'.$relative_url 72 : '(http:|)'.$relative_url; 73 74 // regex rule start 75 $regex_rule = '#(?<=[(\"\'])'; 76 77 // check if relative paths 78 if ($options['relative']) { 79 $regex_rule .= '(?:'.$blog_url.')?'; 80 } else { 81 $regex_rule .= $blog_url; 82 } 83 84 // regex rule end 85 $regex_rule .= '/(?:((?:'.$dirs.')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])#'; 86 87 $matches = array(); 88 preg_match_all($regex_rule, $content, $matches); 89 90 return $matches[0]; 91 } 92 } 93 94
Want get more out of Divi?

Hundreds of new features for Divi
in one easy-to-use plugin
0 Comments