| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base class for CDN integrations |
| 4 |
* |
| 5 |
* @package Caldera_Forms Modified by QuantumCloud |
| 6 |
* @author Josh Pollock <Josh@CalderaWP.com> |
| 7 |
* @license GPL-2.0+ |
| 8 |
* @link |
| 9 |
* @copyright 2017 CalderaWP LLC |
| 10 |
*/ |
| 11 |
|
| 12 |
abstract class Qcformbuilder_Forms_CDN implements Qcformbuilder_Forms_CDN_Contract{ |
| 13 |
|
| 14 |
/** |
| 15 |
* Slugs of all registered styles |
| 16 |
* |
| 17 |
* @since 1.5.3 |
| 18 |
* |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
protected $style_slugs; |
| 22 |
|
| 23 |
/** |
| 24 |
* Slugs of all registered scripts |
| 25 |
* |
| 26 |
* @since 1.5.3 |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
protected $script_slugs; |
| 31 |
|
| 32 |
/** |
| 33 |
* Current version |
| 34 |
* |
| 35 |
* @since 1.5.3 |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected $version; |
| 40 |
|
| 41 |
/** |
| 42 |
* Version to provide to CDN |
| 43 |
* |
| 44 |
* @since 1.5.3 |
| 45 |
* |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
protected $cdn_version; |
| 49 |
|
| 50 |
/** |
| 51 |
* Url for Qcformbuilder Forms dir to be replaces |
| 52 |
* |
| 53 |
* @since 1.5.3 |
| 54 |
* |
| 55 |
* @var string |
| 56 |
*/ |
| 57 |
protected $base_url; |
| 58 |
|
| 59 |
/** |
| 60 |
* Transfer protocol |
| 61 |
* |
| 62 |
* @since 1.5.3 |
| 63 |
* |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
protected $protocol; |
| 67 |
|
| 68 |
/** |
| 69 |
* Qcformbuilder_Forms_CDN constructor. |
| 70 |
* |
| 71 |
* @since 1.5.3 |
| 72 |
* |
| 73 |
* @param $base_url |
| 74 |
* @param $version |
| 75 |
*/ |
| 76 |
public function __construct( $base_url, $version){ |
| 77 |
$this->base_url = $base_url; |
| 78 |
$this->version = $version; |
| 79 |
$this->set_protocol(); |
| 80 |
$this->set_cdn_version(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Add hooks |
| 85 |
* |
| 86 |
* @since 1.5.3 |
| 87 |
*/ |
| 88 |
public function add_hooks(){ |
| 89 |
add_filter( 'qcformbuilder_forms_script_urls', array( $this, 'filter_script_urls' ) ); |
| 90 |
add_filter( 'qcformbuilder_forms_style_urls', array( $this, 'filter_style_urls' ) ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Remove hooks |
| 95 |
* |
| 96 |
* @since 1.5.3 |
| 97 |
*/ |
| 98 |
public function remove_hooks(){ |
| 99 |
remove_filter( 'qcformbuilder_forms_script_urls', array( $this, 'filter_script_urls' ) ); |
| 100 |
remove_filter( 'qcformbuilder_forms_style_urls', array( $this, 'filter_style_urls' ) ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Replace urls for scripts |
| 105 |
* |
| 106 |
* @uses "qcformbuilder_forms_script_urls" filter |
| 107 |
* |
| 108 |
* @since 1.5.3 |
| 109 |
* |
| 110 |
* @param array $urls Array of slug => url |
| 111 |
* |
| 112 |
* @return array |
| 113 |
*/ |
| 114 |
public function filter_script_urls( $urls ){ |
| 115 |
$this->script_slugs = array_keys( $urls ); |
| 116 |
return $this->replace_urls( $urls ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Replace urls for styles |
| 121 |
* |
| 122 |
* @uses "qcformbuilder_forms_style_urls" filter |
| 123 |
* |
| 124 |
* @since 1.5.3 |
| 125 |
* |
| 126 |
* @param array $urls Array of slug => url |
| 127 |
* |
| 128 |
* @return array |
| 129 |
*/ |
| 130 |
public function filter_style_urls( $urls ){ |
| 131 |
$this->style_slugs = array_keys( $urls ); |
| 132 |
|
| 133 |
return $this->replace_urls( $urls ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Replace all urls in an array with CDN url |
| 138 |
* |
| 139 |
* @since 1.5.3 |
| 140 |
* |
| 141 |
* @param array $urls Array of slug => url |
| 142 |
* |
| 143 |
* @return array |
| 144 |
*/ |
| 145 |
protected function replace_urls( $urls ){ |
| 146 |
foreach ( $urls as $slug => $url ) { |
| 147 |
$urls[ $slug ] = $this->replace_url( $url ); |
| 148 |
} |
| 149 |
|
| 150 |
return $urls; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Replace one URL with CDN url |
| 157 |
* |
| 158 |
* @since 1.5.3 |
| 159 |
* |
| 160 |
* @param string $url The URL |
| 161 |
* |
| 162 |
* @return string |
| 163 |
*/ |
| 164 |
protected function replace_url( $url ){ |
| 165 |
return str_replace( $this->base_url, $this->protocol . $this->cdn_url(), $url ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Set the protocol property based on is_ssl() |
| 170 |
* |
| 171 |
* @since 1.5.3 |
| 172 |
*/ |
| 173 |
protected function set_protocol(){ |
| 174 |
if( is_ssl() ){ |
| 175 |
$this->protocol = 'https:'; |
| 176 |
}else{ |
| 177 |
$this->protocol = 'http:'; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Change protocol for CDN links |
| 182 |
* |
| 183 |
* Should not be needed, but WordPress' is_ssl() is sometimes wrong. |
| 184 |
* |
| 185 |
* @since 1.5.3 |
| 186 |
* |
| 187 |
* NOTE: return with : IE http: or https" |
| 188 |
*/ |
| 189 |
$this->protocol = apply_filters( 'qcformbuilder_forms_cdn_protocol', $this->protocol ); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Set version to use with CDN |
| 194 |
* |
| 195 |
* @since 1.5.3 |
| 196 |
*/ |
| 197 |
protected function set_cdn_version(){ |
| 198 |
/** |
| 199 |
* Use to set version of scripts and styles to request via CDN |
| 200 |
* |
| 201 |
* If you have checked out Qcformbuilder Forms from Github and are on master branch current version might not exist in CDN yet, setting to "latest" will pull latest form Github |
| 202 |
* |
| 203 |
* @since 1.5.3 |
| 204 |
*/ |
| 205 |
$this->cdn_version = apply_filters( 'qcformbuilder_forms_cdn_version', $this->version ); |
| 206 |
} |
| 207 |
|
| 208 |
} |