| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* CSS/JS integrations with theme. |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @package s2Member\CSS_JS |
| 16 |
* @since 3.5 |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit ('Do not access this file directly.'); |
| 20 |
|
| 21 |
if(!class_exists('c_ws_plugin__s2member_css_js_themes')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* CSS/JS integrations with theme. |
| 25 |
* |
| 26 |
* @package s2Member\CSS_JS |
| 27 |
* @since 3.5 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_css_js_themes |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Lazy load CSS/JS files? |
| 33 |
* |
| 34 |
* @package s2Member\CSS_JS |
| 35 |
* @since 131028 |
| 36 |
* |
| 37 |
* @return boolean TRUE if we should load; else FALSE. |
| 38 |
*/ |
| 39 |
public static function lazy_load_css_js() |
| 40 |
{ |
| 41 |
static $load; // Static cache var. |
| 42 |
|
| 43 |
if(isset($load)) return $load; |
| 44 |
|
| 45 |
$null = NULL; // Needed below in earlier versions of WP. |
| 46 |
|
| 47 |
if(!$GLOBALS['WS_PLUGIN__']['s2member']['o']['lazy_load_css_js']) |
| 48 |
$load = TRUE; |
| 49 |
|
| 50 |
else if(c_ws_plugin__s2member_systematics::is_s2_systematic_use_page()) |
| 51 |
$load = TRUE; |
| 52 |
|
| 53 |
else if(!empty($_GET[apply_filters('ws_plugin__s2member_check_force_ssl_get_var_name', 's2-ssl', array())])) |
| 54 |
$load = TRUE; |
| 55 |
|
| 56 |
else if(c_ws_plugin__s2member_utils_conds::bp_is_installed() |
| 57 |
&& (bp_is_register_page() || bp_is_activation_page() || bp_is_user_profile()) |
| 58 |
) $load = TRUE; |
| 59 |
|
| 60 |
else if(is_singular() && ($post = get_post($null)) |
| 61 |
&& (stripos($post->post_content, 's2member') !== FALSE || stripos($post->post_content, '[s2') !== FALSE) |
| 62 |
) $load = TRUE; |
| 63 |
|
| 64 |
else if(preg_match('/\/wp\-signup\.php|\/wp\-login\.php|\/wp\-admin\/(?:user\/)?profile\.php|[?&]s2member/', $_SERVER['REQUEST_URI'])) |
| 65 |
$load = TRUE; |
| 66 |
|
| 67 |
if(!isset($load)) $load = FALSE; // Make sure it's set; always. |
| 68 |
|
| 69 |
return ($load = apply_filters('ws_plugin__s2member_lazy_load_css_js', $load)); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Enqueues CSS file for theme integration. |
| 74 |
* |
| 75 |
* @package s2Member\CSS_JS |
| 76 |
* @since 3.5 |
| 77 |
* |
| 78 |
* @attaches-to ``add_action('wp_print_styles');`` |
| 79 |
* |
| 80 |
* @return null After enqueuing CSS for theme integration. |
| 81 |
*/ |
| 82 |
public static function add_css() |
| 83 |
{ |
| 84 |
do_action('ws_plugin__s2member_before_add_css', get_defined_vars()); |
| 85 |
|
| 86 |
if(!is_admin() && c_ws_plugin__s2member_css_js_themes::lazy_load_css_js()) |
| 87 |
{ |
| 88 |
$s2o = $GLOBALS['WS_PLUGIN__']['s2member']['c']['s2o_url']; |
| 89 |
|
| 90 |
wp_enqueue_style('ws-plugin--s2member', $s2o.'?ws_plugin__s2member_css=1&qcABC=1', array(), c_ws_plugin__s2member_utilities::ver_checksum(), 'all'); |
| 91 |
|
| 92 |
do_action('ws_plugin__s2member_during_add_css', get_defined_vars()); |
| 93 |
} |
| 94 |
do_action('ws_plugin__s2member_after_add_css', get_defined_vars()); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Enqueues JS file for theme integration. |
| 99 |
* |
| 100 |
* Be sure s2Member's API Constants are already defined before firing this. |
| 101 |
* |
| 102 |
* @package s2Member\CSS_JS |
| 103 |
* @since 3.5 |
| 104 |
* |
| 105 |
* @attaches-to ``add_action('wp_print_scripts');`` |
| 106 |
* |
| 107 |
* @return null After enqueuing JS for theme integration. |
| 108 |
*/ |
| 109 |
public static function add_js_w_globals() |
| 110 |
{ |
| 111 |
global $pagenow; // Need this for comparisons. |
| 112 |
|
| 113 |
do_action('ws_plugin__s2member_before_add_js_w_globals', get_defined_vars()); |
| 114 |
|
| 115 |
if((!is_admin() && c_ws_plugin__s2member_css_js_themes::lazy_load_css_js()) || (is_user_admin() && $pagenow === 'profile.php' && !current_user_can('edit_users'))) |
| 116 |
{ |
| 117 |
$s2o = $GLOBALS['WS_PLUGIN__']['s2member']['c']['s2o_url']; |
| 118 |
|
| 119 |
if(is_user_logged_in()) // Separate version for logged-in Users/Members. |
| 120 |
{ |
| 121 |
$md5 = WS_PLUGIN__S2MEMBER_API_CONSTANTS_MD5; // An MD5 hash based on global key => values. |
| 122 |
// The MD5 hash allows the script to be cached in the browser until the globals happen to change. |
| 123 |
// For instance, the global variables may change when a User who is logged-in changes their Profile. |
| 124 |
wp_enqueue_script('ws-plugin--s2member', $s2o.'?ws_plugin__s2member_js_w_globals='.urlencode($md5).'&qcABC=1', array('jquery'), c_ws_plugin__s2member_utilities::ver_checksum(), TRUE); |
| 125 |
} |
| 126 |
else // Else if they are not logged in, we distinguish the JavaScript file by NOT including $md5. |
| 127 |
{ // This essentially creates 2 versions of the script. One while logged in & another when not. |
| 128 |
wp_enqueue_script('ws-plugin--s2member', $s2o.'?ws_plugin__s2member_js_w_globals=1&qcABC=1', array('jquery'), c_ws_plugin__s2member_utilities::ver_checksum(), TRUE); |
| 129 |
} |
| 130 |
do_action('ws_plugin__s2member_during_add_js_w_globals', get_defined_vars()); |
| 131 |
} |
| 132 |
do_action('ws_plugin__s2member_after_add_js_w_globals', get_defined_vars()); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Disallow async loading. |
| 137 |
* |
| 138 |
* @package s2Member\CSS_JS |
| 139 |
* @since 170221 Enhancing CloudFlare compat. |
| 140 |
* |
| 141 |
* @attaches-to ``add_filter('script_loader_tag');`` |
| 142 |
* |
| 143 |
* @param string $tag The script tag. |
| 144 |
* @param string $handle The script handle. |
| 145 |
* |
| 146 |
* @return string Possibly altered script tag. |
| 147 |
*/ |
| 148 |
public static function script_loader_tag($tag = '', $handle = '') |
| 149 |
{ |
| 150 |
if ($handle === 'ws-plugin--s2member') { |
| 151 |
$tag = str_replace(' src=', ' data-cfasync="false" src=', $tag); |
| 152 |
} |
| 153 |
return $tag; // Prevent RocketLoader from loading async. |
| 154 |
// See: <https://support.cloudflare.com/hc/en-us/articles/200169436-How-can-I-have-Rocket-Loader-ignore-my-script-s-in-Automatic-Mode-> |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|