balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-redux-cdn.php (9542B)


      1 <?php
      2 /**
      3  * Redux Framework CDN Container Class
      4  *
      5  * @author      Kevin Provance (kprovance)
      6  * @package     Redux_Framework/Classes
      7  */
      8 
      9 // Exit if accessed directly.
     10 defined( 'ABSPATH' ) || exit;
     11 
     12 if ( ! class_exists( 'Redux_CDN', false ) ) {
     13 	/**
     14 	 * Class Redux_CDN
     15 	 */
     16 	class Redux_CDN {
     17 
     18 		/**
     19 		 * Pointer to ReduxFramework object.
     20 		 *
     21 		 * @var object
     22 		 */
     23 		public static $parent;
     24 
     25 		/**
     26 		 * Flag to check for set status.
     27 		 *
     28 		 * @var bool
     29 		 */
     30 		private static $set;
     31 
     32 		/**
     33 		 * Check for enqueued status of style/script.
     34 		 *
     35 		 * @param string $handle    File handle.
     36 		 * @param string $list      Mode to check.
     37 		 * @param bool   $is_script Flag for scrip/style.
     38 		 *
     39 		 * @return bool
     40 		 */
     41 		private static function is_enqueued( string $handle, string $list, bool $is_script = true ): bool {
     42 			if ( $is_script ) {
     43 				return wp_script_is( $handle, $list );
     44 			} else {
     45 				return wp_style_is( $handle, $list );
     46 			}
     47 		}
     48 
     49 		/**
     50 		 * Register script/style.
     51 		 *
     52 		 * @param string $handle File handle.
     53 		 * @param string $src_cdn CDN source.
     54 		 * @param array  $deps File deps.
     55 		 * @param string $ver File version.
     56 		 * @param mixed  $footer_or_media True or 'all'.
     57 		 * @param bool   $is_script Script or style.
     58 		 */
     59 		private static function register( string $handle, string $src_cdn, array $deps, string $ver, $footer_or_media, bool $is_script = true ) {
     60 			if ( $is_script ) {
     61 				wp_register_script( $handle, $src_cdn, $deps, $ver, $footer_or_media );
     62 			} else {
     63 				wp_register_style( $handle, $src_cdn, $deps, $ver, $footer_or_media );
     64 			}
     65 		}
     66 
     67 		/**
     68 		 * Enqueue script or style.
     69 		 *
     70 		 * @param      string $handle File handle.
     71 		 * @param      string $src_cdn CDN source.
     72 		 * @param      array  $deps File deps.
     73 		 * @param      string $ver File version.
     74 		 * @param      mixed  $footer_or_media True or 'all'.
     75 		 * @param      bool   $is_script Script or style.
     76 		 */
     77 		private static function enqueue( string $handle, string $src_cdn, array $deps, string $ver, $footer_or_media, bool $is_script = true ) {
     78 			if ( $is_script ) {
     79 				wp_enqueue_script( $handle, $src_cdn, $deps, $ver, $footer_or_media );
     80 			} else {
     81 				wp_enqueue_style( $handle, $src_cdn, $deps, $ver, $footer_or_media );
     82 			}
     83 		}
     84 
     85 		/**
     86 		 * Enqueue/Register CDN
     87 		 *
     88 		 * @param      bool   $register Register or enqueue.
     89 		 * @param      string $handle File handle.
     90 		 * @param      string $src_cdn CDN source.
     91 		 * @param      array  $deps File deps.
     92 		 * @param      string $ver File version.
     93 		 * @param      mixed  $footer_or_media True or 'all'.
     94 		 * @param      bool   $is_script Script or style.
     95 		 */
     96 		private static function cdn( bool $register, string $handle, string $src_cdn, array $deps, string $ver, $footer_or_media, bool $is_script ) {
     97 			$tran_key = '_style_cdn_is_up';
     98 			if ( $is_script ) {
     99 				$tran_key = '_script_cdn_is_up';
    100 			}
    101 
    102 			$cdn_is_up = get_transient( $handle . $tran_key );
    103 			if ( $cdn_is_up ) {
    104 				if ( $register ) {
    105 					self::register( $handle, $src_cdn, $deps, $ver, $footer_or_media, $is_script );
    106 				} else {
    107 					self::enqueue( $handle, $src_cdn, $deps, $ver, $footer_or_media, $is_script );
    108 				}
    109 			} else {
    110 
    111 				$prefix = '/' === $src_cdn[1] ? 'http:' : '';
    112 
    113 				// phpcs:ignore WordPress.PHP.NoSilencedErrors
    114 				$cdn_response = @wp_remote_get( $prefix . $src_cdn );
    115 
    116 				if ( is_wp_error( $cdn_response ) || 200 !== wp_remote_retrieve_response_code( $cdn_response ) ) {
    117 					if ( class_exists( 'Redux_VendorURL' ) ) {
    118 						$src = Redux_VendorURL::get_url( $handle );
    119 
    120 						if ( $register ) {
    121 							self::register( $handle, $src, $deps, $ver, $footer_or_media, $is_script );
    122 						} else {
    123 							self::enqueue( $handle, $src, $deps, $ver, $footer_or_media, $is_script );
    124 						}
    125 					} else {
    126 						if ( ! self::is_enqueued( $handle, 'enqueued', $is_script ) ) {
    127 							$msg = esc_html__( 'Please wait a few minutes, then try refreshing the page. Unable to load some remotely hosted scripts.', 'redux-framework' );
    128 							if ( self::$parent->args['dev_mode'] ) {
    129 								// translators: %s: URL.
    130 								$msg = sprintf( esc_html__( 'If you are developing offline, please download and install the %s plugin/extension to bypass our CDN and avoid this warning', 'redux-framework' ), '<a href="https://github.com/reduxframework/redux-vendor-support" target="_blank">Redux Vendor Support</a>' );
    131 							}
    132 
    133 							// translators: %s: CDN handle.
    134 							$msg = '<strong>' . esc_html__( 'Redux Framework Warning', 'redux-framework' ) . '</strong><br/>' . sprintf( esc_html__( '%s CDN unavailable.  Some controls may not render properly.', 'redux-framework' ), $handle ) . '  ' . $msg;
    135 
    136 							$data = array(
    137 								'parent'  => self::$parent,
    138 								'type'    => 'error',
    139 								'msg'     => $msg,
    140 								'id'      => $handle . $tran_key,
    141 								'dismiss' => false,
    142 							);
    143 
    144 							Redux_Admin_Notices::set_notice( $data );
    145 						}
    146 					}
    147 				} else {
    148 					set_transient( $handle . $tran_key, true, MINUTE_IN_SECONDS * self::$parent->args['cdn_check_time'] );
    149 
    150 					if ( $register ) {
    151 						self::register( $handle, $src_cdn, $deps, $ver, $footer_or_media, $is_script );
    152 					} else {
    153 						self::enqueue( $handle, $src_cdn, $deps, $ver, $footer_or_media, $is_script );
    154 					}
    155 				}
    156 			}
    157 		}
    158 
    159 		/**
    160 		 * Register/enqueue file from vendor library.
    161 		 *
    162 		 * @param      bool   $register Reigster or enqueue.
    163 		 * @param      string $handle File handle.
    164 		 * @param      string $src_cdn CDN source.
    165 		 * @param      array  $deps File deps.
    166 		 * @param      string $ver File version.
    167 		 * @param      mixed  $footer_or_media True or 'all'.
    168 		 * @param      bool   $is_script Script or style.
    169 		 */
    170 		private static function vendor_plugin( bool $register, string $handle, string $src_cdn, array $deps, string $ver, $footer_or_media, bool $is_script ) {
    171 			if ( class_exists( 'Redux_VendorURL' ) ) {
    172 				$src = Redux_VendorURL::get_url( $handle );
    173 
    174 				if ( $register ) {
    175 					self::register( $handle, $src, $deps, $ver, $footer_or_media, $is_script );
    176 				} else {
    177 					self::enqueue( $handle, $src, $deps, $ver, $footer_or_media, $is_script );
    178 				}
    179 			} else {
    180 				if ( ! self::$set ) {
    181 					// translators: %s: Vendor support URL. %s: Admin pluygins page.
    182 					$msg = sprintf( esc_html__( 'The %1$s (or extension) is either not installed or not activated and thus, some controls may not render properly.  Please ensure that it is installed and %2$s', 'redux-framework' ), '<a href="https://github.com/reduxframework/redux-vendor-support">Vendor Support plugin</a>', '<a href="' . admin_url( 'plugins.php' ) . '">' . esc_html__( 'activated.', 'redux-framework' ) . '</a>' );
    183 
    184 					$data = array(
    185 						'parent'  => self::$parent,
    186 						'type'    => 'error',
    187 						'msg'     => $msg,
    188 						'id'      => $handle,
    189 						'dismiss' => false,
    190 					);
    191 
    192 					Redux_Admin_Notices::set_notice( $data );
    193 
    194 					self::$set = true;
    195 				}
    196 			}
    197 		}
    198 
    199 		/**
    200 		 * Register style CDN or local.
    201 		 *
    202 		 * @param string $handle  File handle.
    203 		 * @param string $src_cdn CDN source.
    204 		 * @param array  $deps    File deps.
    205 		 * @param string $ver     File version.
    206 		 * @param string $media   True or 'all'.
    207 		 */
    208 		public static function register_style( string $handle, string $src_cdn, array $deps = array(), string $ver = '', string $media = 'all' ) {
    209 			if ( empty( self::$parent ) || self::$parent->args['use_cdn'] ) {
    210 				self::cdn( true, $handle, $src_cdn, $deps, $ver, $media, $is_script = false );
    211 			} else {
    212 				self::vendor_plugin( true, $handle, $src_cdn, $deps, $ver, $media, $is_script = false );
    213 			}
    214 		}
    215 
    216 		/** Register script CDN or local.
    217 		 *
    218 		 * @param string $handle    File handle.
    219 		 * @param string $src_cdn   CDN source.
    220 		 * @param array  $deps      File deps.
    221 		 * @param string $ver       File version.
    222 		 * @param bool   $in_footer Script in footer.
    223 		 */
    224 		public static function register_script( string $handle, string $src_cdn, array $deps = array(), string $ver = '', bool $in_footer = false ) {
    225 			if ( empty( self::$parent ) || self::$parent->args['use_cdn'] ) {
    226 				self::cdn( true, $handle, $src_cdn, $deps, $ver, $in_footer, $is_script = true );
    227 			} else {
    228 				self::vendor_plugin( true, $handle, $src_cdn, $deps, $ver, $in_footer, $is_script = true );
    229 			}
    230 		}
    231 
    232 		/**
    233 		 * Enqueue style CDN or local.
    234 		 *
    235 		 * @param string $handle  File handle.
    236 		 * @param string $src_cdn CDN source.
    237 		 * @param array  $deps    File deps.
    238 		 * @param string $ver     File version.
    239 		 * @param string $media   Media type.
    240 		 */
    241 		public static function enqueue_style( string $handle, string $src_cdn, array $deps = array(), string $ver = '', string $media = 'all' ) {
    242 			if ( self::$parent->args['use_cdn'] ) {
    243 				self::cdn( false, $handle, $src_cdn, $deps, $ver, $media, $is_script = false );
    244 			} else {
    245 				self::vendor_plugin( false, $handle, $src_cdn, $deps, $ver, $media, $is_script = false );
    246 			}
    247 		}
    248 
    249 		/**
    250 		 * Enqueue script CDN or local.
    251 		 *
    252 		 * @param string $handle    File handle.
    253 		 * @param string $src_cdn   CDN source.
    254 		 * @param array  $deps      File seps.
    255 		 * @param string $ver       File version.
    256 		 * @param bool   $in_footer Script in footer.
    257 		 */
    258 		public static function enqueue_script( string $handle, string $src_cdn, array $deps = array(), string $ver = '', bool $in_footer = false ) {
    259 			if ( self::$parent->args['use_cdn'] ) {
    260 				self::cdn( false, $handle, $src_cdn, $deps, $ver, $in_footer, $is_script = true );
    261 			} else {
    262 				self::vendor_plugin( false, $handle, $src_cdn, $deps, $ver, $in_footer, $is_script = true );
    263 			}
    264 		}
    265 	}
    266 }