| 1 |
<?php /** |
| 2 |
* @version 1.1 |
| 3 |
* @package Any |
| 4 |
* @category Load JS and CSS files |
| 5 |
* @author wpdevelop |
| 6 |
* |
| 7 |
* @web-site https://wpbookingcalendar.com/ |
| 8 |
* @email info@wpbookingcalendar.com |
| 9 |
* |
| 10 |
* @modified 2015-10-28 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
abstract class WPBC_JS_CSS { |
| 18 |
|
| 19 |
public $objects = array(); |
| 20 |
public $type; // css || js |
| 21 |
|
| 22 |
function __construct() { |
| 23 |
$this->define(); |
| 24 |
add_action( 'admin_enqueue_scripts', array( $this, 'registerScripts' ) ); |
| 25 |
add_action( 'wp_enqueue_scripts', array( $this, 'registerScripts' ) ); |
| 26 |
|
| 27 |
add_action( 'wpbc_load_js_on_admin_page', array( $this, 'load_js_on_admin_page' ) ); // Load JS. Hook fire only in admin pages of plugin. CLASS: WPBC_Admin_Menus (..\any\class\class-admin-menu.php) |
| 28 |
add_action( 'wpbc_load_css_on_admin_page', array( $this, 'load_css_on_admin_page' ) ); // Load CSS. Hook fire only in admin pages of plugin. CLASS: WPBC_Admin_Menus (..\any\class\class-admin-menu.php) |
| 29 |
} |
| 30 |
|
| 31 |
public function load_css_on_admin_page() { |
| 32 |
|
| 33 |
if ( $this->getType() == 'css' ) |
| 34 |
$this->load(); |
| 35 |
} |
| 36 |
|
| 37 |
public function load_js_on_admin_page() { |
| 38 |
|
| 39 |
if ( $this->getType() == 'js' ) |
| 40 |
$this->load(); |
| 41 |
} |
| 42 |
|
| 43 |
/** Define all Scripts or Styles here */ |
| 44 |
abstract public function define(); |
| 45 |
|
| 46 |
/** |
| 47 |
* Enqueue Scripts or Styles. |
| 48 |
* |
| 49 |
* @param type $where_to_load - can be "admin" or "client" |
| 50 |
*/ |
| 51 |
abstract public function enqueue( $where_to_load ); |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Deregister some Conflict scripts from other plugins. |
| 56 |
* |
| 57 |
* @param type $where_to_load - can be "admin" or "client" |
| 58 |
*/ |
| 59 |
abstract public function remove_conflicts( $where_to_load ); |
| 60 |
|
| 61 |
|
| 62 |
// Define CSS or JavaScript |
| 63 |
public function setType($param) { |
| 64 |
$this->type = $param; |
| 65 |
} |
| 66 |
|
| 67 |
// Get type of this script |
| 68 |
public function getType() { |
| 69 |
return $this->type; |
| 70 |
} |
| 71 |
|
| 72 |
// Add new Style or Script |
| 73 |
public function add($param) { |
| 74 |
$this->objects[] = $param; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
private function isLoad( $whereToLoadArray ) { |
| 79 |
$is_load = false; |
| 80 |
|
| 81 |
if ( ( is_admin() ) && ( in_array('admin', $whereToLoadArray ) ) ) |
| 82 |
$is_load = true; |
| 83 |
|
| 84 |
if ( ( ! is_admin() ) && ( in_array('client', $whereToLoadArray ) ) ) |
| 85 |
$is_load = true; |
| 86 |
|
| 87 |
return $is_load; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Register Scripts |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
public function registerScripts() { |
| 96 |
|
| 97 |
$this->remove_conflicts( ( is_admin() ? 'admin' : 'client' ) ); |
| 98 |
|
| 99 |
foreach ( $this->objects as $script ) { |
| 100 |
|
| 101 |
if ( $this->isLoad( $script['where_to_load'] ) ) { |
| 102 |
|
| 103 |
if ( 'css' === $this->getType() ) { |
| 104 |
wp_register_style( $script['handle'], $script['src'], $script['deps'], $script['version'] ); |
| 105 |
} else { |
| 106 |
wp_register_script( $script['handle'], $script['src'], $script['deps'], $script['version'], array( 'in_footer' => WPBC_JS_IN_FOOTER ) ); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
// Load scripts or styles here |
| 114 |
public function load(){ |
| 115 |
|
| 116 |
$is_load_scripts = true; |
| 117 |
|
| 118 |
$is_load_scripts = apply_filters( 'wpbc_is_load_script_on_this_page', $is_load_scripts ); |
| 119 |
|
| 120 |
$force = apply_filters( 'wpbc_force_client_assets', false ); |
| 121 |
|
| 122 |
if ( ! $force && ! $is_load_scripts ) return; // Exist, if on some page we do not need to load scripts |
| 123 |
|
| 124 |
|
| 125 |
foreach( $this->objects as $num => $script ) { |
| 126 |
|
| 127 |
if ( $this->isLoad( $script['where_to_load'] ) ) { |
| 128 |
|
| 129 |
if ( $this->getType() == 'css' ) { |
| 130 |
|
| 131 |
if ( $script['condition'] === false ) |
| 132 |
|
| 133 |
wp_enqueue_style( $script['handle'] ); |
| 134 |
|
| 135 |
else { |
| 136 |
|
| 137 |
if (! function_exists('wp_style_add_data') ) { // This function is available only since WordPress 3.6.0 Update |
| 138 |
wp_enqueue_style( $script['handle'] ); |
| 139 |
wp_style_add_data( $script['handle'], 'conditional', $script['condition'] ); |
| 140 |
} else { // Add additional "dynamic CSS" if the WP version older than 3.6.0 (its function suport since WP 3.3) |
| 141 |
if ( ($num-1) > -1 ) { // Its because "wp_add_inline_style" add the CSS to the already added style. So its require that some other simple style was added before |
| 142 |
wp_enqueue_style( $this->objects[($num-1)]['handle'] ); |
| 143 |
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet |
| 144 |
wp_add_inline_style( $this->objects[ ( $num - 1 ) ]['handle'], sprintf( "<!--[if " . $script['condition'] . "]>\n" . "<link rel='stylesheet' id='" . $script['handle'] . "-css' href='" . $script['src'] . "?ver=" . $script['version'] . "' type='text/css' media='all' />\n" . "<![endif]-->\n" ) ); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
} else { |
| 150 |
wp_enqueue_script( $script['handle'] ); |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
$this->enqueue( ( is_admin() ? 'admin': 'client' ) ); |
| 157 |
|
| 158 |
if ( $this->getType() == 'css' ) |
| 159 |
do_action( 'wpbc_enqueue_style', ( is_admin() ? 'admin': 'client' ) ); |
| 160 |
|
| 161 |
if ( $this->getType() == 'js' ) |
| 162 |
do_action( 'wpbc_enqueue_script', ( is_admin() ? 'admin': 'client' ) ); |
| 163 |
} |
| 164 |
|
| 165 |
} |