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