| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Handle frontend forms |
| 9 |
* |
| 10 |
* @class PH_Frontend_Scripts |
| 11 |
* @version 1.0.0 |
| 12 |
* @package PropertyHive/Classes/ |
| 13 |
* @category Class |
| 14 |
* @author PropertyHive |
| 15 |
*/ |
| 16 |
class PH_Frontend_Scripts { |
| 17 |
|
| 18 |
/** |
| 19 |
* Hook in methods |
| 20 |
*/ |
| 21 |
public static function init() { |
| 22 |
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'load_scripts' ) ); |
| 23 |
add_action( 'wp_print_scripts', array( __CLASS__, 'check_jquery' ), 25 ); |
| 24 |
add_action( 'wp_print_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 ); |
| 25 |
add_action( 'wp_print_footer_scripts', array( __CLASS__, 'localize_printed_scripts' ), 5 ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Get styles for the frontend |
| 30 |
* @return array |
| 31 |
*/ |
| 32 |
public static function get_styles() { |
| 33 |
return apply_filters( 'propertyhive_enqueue_styles', array( |
| 34 |
'propertyhive-general' => array( |
| 35 |
'src' => str_replace( array( 'http:', 'https:' ), '', PH()->plugin_url() ) . '/assets/css/propertyhive.css', |
| 36 |
'deps' => '', |
| 37 |
'version' => PH_VERSION, |
| 38 |
'media' => 'all' |
| 39 |
), |
| 40 |
) ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Register/queue frontend scripts. |
| 45 |
* |
| 46 |
* @access public |
| 47 |
* @return void |
| 48 |
*/ |
| 49 |
public static function load_scripts() { |
| 50 |
global $post; |
| 51 |
|
| 52 |
//$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 53 |
$suffix = ''; |
| 54 |
$assets_path = str_replace( array( 'http:', 'https:' ), '', PH()->plugin_url() ) . '/assets/'; |
| 55 |
$frontend_script_path = $assets_path . 'js/frontend/'; |
| 56 |
|
| 57 |
// Register any scripts for later use, or used as dependencies |
| 58 |
wp_register_script( 'jquery-cookie', $assets_path . 'js/jquery-cookie/jquery.cookie' . $suffix . '.js', array( 'jquery' ), '1.3.1', true ); |
| 59 |
|
| 60 |
wp_register_script( 'propertyhive_fancybox', $assets_path . 'js/fancybox/jquery.fancybox' . $suffix . '.js', array( 'jquery' ), '3.5.7', true ); |
| 61 |
wp_register_style( 'propertyhive_fancybox_css', $assets_path . 'css/jquery.fancybox' . $suffix . '.css', array(), '3.5.7' ); |
| 62 |
|
| 63 |
wp_register_script( 'tiny_slider', $assets_path . 'js/tiny-slider/tiny-slider.js', array( 'jquery' ), '2.9.4', true ); |
| 64 |
wp_register_script( 'propertyhive_carousel', $assets_path . 'js/frontend/carousel.js', array( 'jquery' ), PH_VERSION, true ); |
| 65 |
wp_register_style( 'tiny_slider_css', $assets_path . 'js/tiny-slider/tiny-slider.css', array(), '2.9.4' ); |
| 66 |
|
| 67 |
if ( get_option('propertyhive_lettings_fees_display_search_results', '') == 'yes' ) |
| 68 |
{ |
| 69 |
wp_enqueue_script( 'propertyhive_fancybox' ); |
| 70 |
wp_enqueue_style( 'propertyhive_fancybox_css' ); |
| 71 |
} |
| 72 |
|
| 73 |
if ( is_property() ) |
| 74 |
{ |
| 75 |
wp_enqueue_script( 'propertyhive_fancybox' ); |
| 76 |
wp_enqueue_style( 'propertyhive_fancybox_css' ); |
| 77 |
|
| 78 |
wp_enqueue_script( 'flexslider', $assets_path . 'js/flexslider/jquery.flexslider' . $suffix . '.js', array( 'jquery' ), '2.7.2', true ); |
| 79 |
wp_enqueue_script( 'flexslider-init', $assets_path . 'js/flexslider/jquery.flexslider.init' . $suffix . '.js', array( 'jquery','flexslider' ), PH_VERSION, true ); |
| 80 |
wp_enqueue_style( 'flexslider_css', $assets_path . 'css/flexslider.css', array(), '2.7.2' ); |
| 81 |
} |
| 82 |
|
| 83 |
// Global frontend scripts |
| 84 |
wp_enqueue_script( 'propertyhive_search', $frontend_script_path . 'search' . $suffix . '.js', array( 'jquery' ), PH_VERSION, true ); |
| 85 |
wp_enqueue_script( 'propertyhive_make_enquiry', $frontend_script_path . 'make-enquiry' . $suffix . '.js', array( 'jquery' ), PH_VERSION, true ); |
| 86 |
//wp_enqueue_script( 'propertyhive', $frontend_script_path . 'propertyhive' . $suffix . '.js', array( 'jquery' ), PH_VERSION, true ); |
| 87 |
|
| 88 |
$captcha_service = get_option( 'propertyhive_captcha_service', '' ); |
| 89 |
if ( $captcha_service == 'turnstile' ) |
| 90 |
{ |
| 91 |
if ( get_option( 'propertyhive_captcha_site_key', '' ) != '' && get_option( 'propertyhive_captcha_secret', '' ) != '' ) |
| 92 |
{ |
| 93 |
wp_enqueue_script( 'turnstile', 'https://challenges.cloudflare.com/turnstile/v0/api.js?onload=ph_init_turnstile', array(), PH_VERSION, array( 'strategy' => 'defer', 'in_footer' => true ) ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
wp_register_script( 'multiselect', $assets_path . 'js/multiselect/jquery.multiselect' . /*$suffix .*/ '.js', array('jquery'), '2.4.18', true ); |
| 98 |
wp_enqueue_style( 'multiselect', $assets_path . 'css/jquery.multiselect.css', array(), '2.4.18' ); |
| 99 |
|
| 100 |
wp_register_script( 'propertyhive_dynamic_population', $assets_path . 'js/frontend/dynamic-population' . /*$suffix .*/ '.js', array('jquery'), '1.0.0', true ); |
| 101 |
|
| 102 |
wp_enqueue_script( 'propertyhive_utm_tracker', $assets_path . 'js/frontend/utm-tracker' . $suffix . '.js', array( 'jquery' ), PH_VERSION, true ); |
| 103 |
|
| 104 |
// CSS Styles |
| 105 |
$enqueue_styles = self::get_styles(); |
| 106 |
|
| 107 |
if ( $enqueue_styles ) { |
| 108 |
foreach ( $enqueue_styles as $handle => $args ) { |
| 109 |
wp_enqueue_style( $handle, $args['src'], $args['deps'], $args['version'], $args['media'] ); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Localize scripts only when enqueued |
| 116 |
*/ |
| 117 |
public static function localize_printed_scripts() { |
| 118 |
global $wp; |
| 119 |
|
| 120 |
$assets_path = str_replace( array( 'http:', 'https:' ), '', PH()->plugin_url() ) . '/assets/'; |
| 121 |
|
| 122 |
if ( wp_script_is( 'propertyhive_search' ) ) |
| 123 |
{ |
| 124 |
$params = array( |
| 125 |
'custom_departments' => ph_get_custom_departments(), |
| 126 |
'do_image_resize' => false, |
| 127 |
'image_ratio' => 2 / 3, |
| 128 |
); |
| 129 |
|
| 130 |
$current_settings = get_option( 'propertyhive_template_assistant', array() ); |
| 131 |
|
| 132 |
if ( |
| 133 |
is_post_type_archive('property') |
| 134 |
|| |
| 135 |
( isset($current_settings['search_result_css_all_pages']) && $current_settings['search_result_css_all_pages'] == 'yes' ) |
| 136 |
) |
| 137 |
{ |
| 138 |
if ( |
| 139 |
isset($current_settings['search_result_layout']) && |
| 140 |
isset($current_settings['search_result_layout']) == 2 |
| 141 |
) |
| 142 |
{ |
| 143 |
$params['do_image_resize'] = true; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
$params = apply_filters( 'propertyhive_search_params', $params ); |
| 148 |
$params = apply_filters( 'propertyhive_template_assistant_script_params', $params ); |
| 149 |
|
| 150 |
wp_localize_script( 'propertyhive_search', 'propertyhive_search_params', $params ); |
| 151 |
} |
| 152 |
|
| 153 |
if ( wp_script_is( 'propertyhive_make_enquiry' ) ) |
| 154 |
{ |
| 155 |
$data = array( |
| 156 |
'ajax_url' => PH()->ajax_url(), |
| 157 |
'default_validation_error_message' => esc_html(__( 'Please ensure all required fields have been completed', 'propertyhive' )), |
| 158 |
); |
| 159 |
if ( get_option('propertyhive_captcha_service', '') != '' ) |
| 160 |
{ |
| 161 |
$data['captcha_service'] = get_option('propertyhive_captcha_service', ''); |
| 162 |
$data['recaptcha_site_key'] = get_option('propertyhive_captcha_site_key', 'recaptcha-v3'); |
| 163 |
} |
| 164 |
wp_localize_script( 'propertyhive_make_enquiry', 'propertyhive_make_property_enquiry_params', apply_filters( 'propertyhive_make_property_enquiry_params', $data ) ); |
| 165 |
} |
| 166 |
|
| 167 |
if ( wp_script_is( 'propertyhive_account' ) ) |
| 168 |
{ |
| 169 |
$data = array( |
| 170 |
'ajax_url' => PH()->ajax_url(), |
| 171 |
'my_account_url' => get_permalink( ph_get_page_id('my_account') ), |
| 172 |
'custom_departments' => ph_get_custom_departments(), |
| 173 |
'login_nonce' => wp_create_nonce( "ph_login" ), |
| 174 |
'lost_password_nonce' => wp_create_nonce( "ph_lost_password" ), |
| 175 |
'reset_password_nonce' => wp_create_nonce( "ph_reset_password" ), |
| 176 |
'register_nonce' => wp_create_nonce( "ph_register" ), |
| 177 |
'userdetails_nonce' => wp_create_nonce( "ph_userdetails" ), |
| 178 |
'requirements_nonce' => wp_create_nonce( "ph_requirements" ), |
| 179 |
'default_validation_error_message' => esc_html(__( 'Please ensure all required fields have been completed', 'propertyhive' )), |
| 180 |
); |
| 181 |
if ( get_option('propertyhive_captcha_service', '') != '' ) |
| 182 |
{ |
| 183 |
$data['captcha_service'] = get_option('propertyhive_captcha_service', ''); |
| 184 |
$data['recaptcha_site_key'] = get_option('propertyhive_captcha_site_key', 'recaptcha-v3'); |
| 185 |
} |
| 186 |
|
| 187 |
wp_localize_script( 'propertyhive_account', 'propertyhive_account_params', apply_filters( 'propertyhive_account_params', $data ) ); |
| 188 |
} |
| 189 |
|
| 190 |
if ( wp_script_is( 'multiselect' ) ) { |
| 191 |
wp_localize_script( 'multiselect', 'propertyhive_multiselect_params', apply_filters( 'propertyhive_multiselect_params', array( |
| 192 |
'search' => false, |
| 193 |
'selected_text' => __( 'selected', 'propertyhive' ), |
| 194 |
) ) ); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* PH requires jQuery 1.8 since it uses functions like .on() for events and .parseHTML. |
| 200 |
* If, by the time wp_print_scrips is called, jQuery is outdated (i.e not |
| 201 |
* using the version in core) we need to deregister it and register the |
| 202 |
* core version of the file. |
| 203 |
* |
| 204 |
* @access public |
| 205 |
* @return void |
| 206 |
*/ |
| 207 |
public static function check_jquery() { |
| 208 |
global $wp_scripts; |
| 209 |
|
| 210 |
// Enforce minimum version of jQuery |
| 211 |
if ( ! empty( $wp_scripts->registered['jquery']->ver ) && ! empty( $wp_scripts->registered['jquery']->src ) && 0 >= version_compare( $wp_scripts->registered['jquery']->ver, '1.8' ) ) { |
| 212 |
wp_deregister_script( 'jquery' ); |
| 213 |
wp_register_script( 'jquery', '/wp-includes/js/jquery/jquery.js', array(), '1.8' ); |
| 214 |
wp_enqueue_script( 'jquery' ); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
PH_Frontend_Scripts::init(); |
| 220 |
|