| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPComment Frontend Scripts Class |
| 4 |
* |
| 5 |
* It will register/enqueue all wpcomment scripts to frontent. |
| 6 |
*/ |
| 7 |
|
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 10 |
|
| 11 |
|
| 12 |
class wpcomment_FRONTEND_SCRIPTS { |
| 13 |
|
| 14 |
/** |
| 15 |
* Return scripts URL. |
| 16 |
* |
| 17 |
* @var URL |
| 18 |
* |
| 19 |
*/ |
| 20 |
private static $scripts_url = ''; |
| 21 |
|
| 22 |
/** |
| 23 |
* Return current wpcomment version. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
* |
| 27 |
*/ |
| 28 |
private static $version = ''; |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Return main scripts framework class. |
| 33 |
* |
| 34 |
*/ |
| 35 |
private static $scripts_class; |
| 36 |
|
| 37 |
|
| 38 |
/** |
| 39 |
* Main Init |
| 40 |
*/ |
| 41 |
public static function init() { |
| 42 |
|
| 43 |
self::$scripts_url = WPCOMMENT_SCRIPTS::get_url(); |
| 44 |
self::$version = WPCOMMENT_SCRIPTS::get_version(); |
| 45 |
|
| 46 |
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'load_scripts' ) ); |
| 47 |
|
| 48 |
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_scripts_admin' ) ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* Register all PPOM Scripts. |
| 54 |
*/ |
| 55 |
private static function get_scripts() { |
| 56 |
|
| 57 |
$register_scripts = array( |
| 58 |
'wpcomment-sm-popup' => array( |
| 59 |
'src' => self::$scripts_url.'/js/wpcomment-simple-popup.js', |
| 60 |
'deps' => array( 'jquery' ), |
| 61 |
'version' => '1.0', |
| 62 |
), |
| 63 |
'wpcomment-plusminus-lib' => array( |
| 64 |
'src' => self::$scripts_url.'/js/wpcomment-plusminus.js', |
| 65 |
'deps' => array( 'jquery' ), |
| 66 |
'version' => '1.0', |
| 67 |
), |
| 68 |
'wpcomment-tooltip' => array( |
| 69 |
'src' => self::$scripts_url.'/js/wpcomment-tooltip.js', |
| 70 |
'deps' => array( 'jquery' ), |
| 71 |
'version' => '1.0', |
| 72 |
), |
| 73 |
'wpcomment-inputmask' => array( |
| 74 |
'src' => self::$scripts_url.'/js/inputmask/jquery.inputmask.min.js', |
| 75 |
'deps' => array( 'jquery' ), |
| 76 |
'version' => '5.0.6', |
| 77 |
), |
| 78 |
'iris' => array( |
| 79 |
'src' => admin_url( 'js/iris.min.js' ), |
| 80 |
'deps' => array( |
| 81 |
'jquery', |
| 82 |
'jquery-ui-core', |
| 83 |
'jquery-ui-draggable', |
| 84 |
'jquery-ui-slider' |
| 85 |
), |
| 86 |
'version' => '1.0.7', |
| 87 |
), |
| 88 |
'wpcomment-zoom' => array( |
| 89 |
'src' => self::$scripts_url.'/js/image-tooltip.js', |
| 90 |
'deps' => array( 'jquery' ), |
| 91 |
'version' => self::$version, |
| 92 |
), |
| 93 |
'wpcomment-bs-slider' => array( |
| 94 |
'src' => self::$scripts_url.'/js/bs-slider/bootstrap-slider.min.js', |
| 95 |
'deps' => array( 'jquery' ), |
| 96 |
'version' => '10.0.0', |
| 97 |
), |
| 98 |
'wpcomment-file-upload' => array( |
| 99 |
'src' => self::$scripts_url.'/js/file-upload.js', |
| 100 |
'deps' => array('jquery', 'plupload'), |
| 101 |
'version' => self::$version, |
| 102 |
), |
| 103 |
'wpcomment-inputs' => array( |
| 104 |
'src' => self::$scripts_url.'/js/wpcomment.inputs.js', |
| 105 |
'deps' => array( 'jquery', 'jquery-ui-datepicker' ), |
| 106 |
'version' => self::$version, |
| 107 |
), |
| 108 |
'wpcomment-tooltip-lib' => array( |
| 109 |
'src' => self::$scripts_url.'/js/tooltip/tooltip.js', |
| 110 |
'deps' => array( 'jquery' ), |
| 111 |
'version' => self::$version, |
| 112 |
), |
| 113 |
'wpcomment-validate' => array( |
| 114 |
'src' => self::$scripts_url.'/js/validate.js', |
| 115 |
'deps' => array('jquery'), |
| 116 |
'version' => self::$version, |
| 117 |
), |
| 118 |
); |
| 119 |
|
| 120 |
return apply_filters('wpcomment_frontend_scripts_before_register', $register_scripts); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
/** |
| 125 |
* Register Styles |
| 126 |
*/ |
| 127 |
private static function get_styles() { |
| 128 |
|
| 129 |
$register_styles = array( |
| 130 |
'wpcomment-main' => array( |
| 131 |
'src' => self::$scripts_url.'/css/wpcomment-style.css', |
| 132 |
'deps' => array(), |
| 133 |
'version' => self::$version, |
| 134 |
), |
| 135 |
'wpcomment-sm-popup' => array( |
| 136 |
'src' => self::$scripts_url.'/css/wpcomment-simple-popup.css', |
| 137 |
'deps' => array(), |
| 138 |
'version' => self::$version, |
| 139 |
), |
| 140 |
'wpcomment-bootstrap' => array( |
| 141 |
'src' => self::$scripts_url.'/css/bootstrap/bootstrap.min.css', |
| 142 |
'deps' => array(), |
| 143 |
'version' => '4.0.0', |
| 144 |
), |
| 145 |
'wpcomment-bootstrap-modal' => array( |
| 146 |
'src' => self::$scripts_url.'/css/bootstrap/bootstrap.modal.css', |
| 147 |
'deps' => array(), |
| 148 |
'version' => '4.0.0', |
| 149 |
), |
| 150 |
'jqueryui' => array( |
| 151 |
'src' => self::$scripts_url.'/js/ui/css/smoothness/jquery-ui-1.10.3.custom.min.css', |
| 152 |
'deps' => array(), |
| 153 |
'version' => '1.10.3', |
| 154 |
), |
| 155 |
'wpcomment-bs-slider-css' => array( |
| 156 |
'src' => self::$scripts_url.'/js/bs-slider/bootstrap-slider.min.css', |
| 157 |
'deps' => array(), |
| 158 |
'version' => '10.0.0', |
| 159 |
), |
| 160 |
'wpcomment-croppie-lib' => array( |
| 161 |
'src' => self::$scripts_url.'/js/croppie/croppie.css', |
| 162 |
'deps' => array(), |
| 163 |
'version' => '2.6.4', |
| 164 |
), |
| 165 |
'wpcomment-modal-lib' => array( |
| 166 |
'src' => self::$scripts_url.'/css/wpcomment-modal.css', |
| 167 |
'deps' => array(), |
| 168 |
'version' => '1.1.1', |
| 169 |
), |
| 170 |
'wpcomment-divider-input' => array( |
| 171 |
'src' => self::$scripts_url.'/css/divider.css', |
| 172 |
'deps' => array(), |
| 173 |
'version' => '1.0', |
| 174 |
), |
| 175 |
'wpcomment-tooltip-lib' => array( |
| 176 |
'src' => self::$scripts_url.'/js/tooltip/tooltip.css', |
| 177 |
'deps' => array(), |
| 178 |
'version' => '1.0', |
| 179 |
), |
| 180 |
|
| 181 |
); |
| 182 |
|
| 183 |
return apply_filters('wpcomment_frontend_styles_before_register', $register_styles); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Load Admin Scripts |
| 188 |
**/ |
| 189 |
public static function load_scripts_admin($hook){ |
| 190 |
|
| 191 |
// loading scripts only comments.php |
| 192 |
if( 'comment.php' !== $hook ) return; |
| 193 |
|
| 194 |
// Get all styles & scripts |
| 195 |
$all_scripts = self::get_scripts(); |
| 196 |
$all_styles = self::get_styles(); |
| 197 |
|
| 198 |
// Register all styles & scripts |
| 199 |
WPCOMMENT_SCRIPTS::register_scripts($all_scripts); |
| 200 |
WPCOMMENT_SCRIPTS::register_styles($all_styles); |
| 201 |
|
| 202 |
self::load_scripts_comments(); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
/** |
| 207 |
* Load Frontend Scripts. |
| 208 |
*/ |
| 209 |
public static function load_scripts() { |
| 210 |
|
| 211 |
$allow_posts = apply_filters('wpcomment_allowed_posts', ['post']); |
| 212 |
|
| 213 |
if ( ! in_array(get_post_type(), $allow_posts) ) return; |
| 214 |
|
| 215 |
global $post; |
| 216 |
|
| 217 |
// Get all styles & scripts |
| 218 |
$all_scripts = self::get_scripts(); |
| 219 |
$all_styles = self::get_styles(); |
| 220 |
|
| 221 |
// Register all styles & scripts |
| 222 |
WPCOMMENT_SCRIPTS::register_scripts($all_scripts); |
| 223 |
WPCOMMENT_SCRIPTS::register_styles($all_styles); |
| 224 |
|
| 225 |
if (!is_object($post)) return; |
| 226 |
|
| 227 |
self::load_scripts_comments(); |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
/** |
| 233 |
* Load Frontend Scripts by product ID. |
| 234 |
*/ |
| 235 |
public static function load_scripts_comments() { |
| 236 |
|
| 237 |
$form_obj = new WPComment_Form; |
| 238 |
|
| 239 |
if ( $form_obj::$fields ) { |
| 240 |
|
| 241 |
$wpcomment_meta_fields = $form_obj::$fields; |
| 242 |
|
| 243 |
if( ! $wpcomment_meta_fields ) return ''; |
| 244 |
|
| 245 |
$wpcomment_conditional_fields = array(); |
| 246 |
$croppie_options = array(); |
| 247 |
$global_js_vars = array(); |
| 248 |
$file_js_vars = array(); |
| 249 |
$input_js_vars = array(); |
| 250 |
$wpcomment_file_inputs = array(); |
| 251 |
$inputs_meta_updated = array(); |
| 252 |
$show_price_per_unit = false; |
| 253 |
|
| 254 |
WPCOMMENT_SCRIPTS::enqueue_style( 'wpcomment-tooltip-lib' ); |
| 255 |
WPCOMMENT_SCRIPTS::enqueue_script( 'wpcomment-tooltip-lib' ); |
| 256 |
|
| 257 |
WPCOMMENT_SCRIPTS::enqueue_style( 'wpcomment-main' ); |
| 258 |
WPCOMMENT_SCRIPTS::enqueue_style( 'wpcomment-sm-popup' ); |
| 259 |
WPCOMMENT_SCRIPTS::enqueue_script( 'wpcomment-sm-popup' ); |
| 260 |
|
| 261 |
self::add_inline_css('global'); |
| 262 |
|
| 263 |
// WPCOMMENT_SCRIPTS::inline_style('wpcomment-main', $wpcomment->inline_css); |
| 264 |
|
| 265 |
if( wpcomment_load_bootstrap_css() ) { |
| 266 |
WPCOMMENT_SCRIPTS::enqueue_style( 'wpcomment-bootstrap' ); |
| 267 |
WPCOMMENT_SCRIPTS::enqueue_style( 'wpcomment-bootstrap-modal' ); |
| 268 |
} |
| 269 |
|
| 270 |
$enable_file_rename = apply_filters('wpcomment_upload_file_rename', true, $wpcomment_meta_fields); |
| 271 |
|
| 272 |
$file_js_vars['enable_file_rename'] = $enable_file_rename; |
| 273 |
|
| 274 |
/* Global JS Inputs Vars */ |
| 275 |
$global_js_vars = array( |
| 276 |
'ajaxurl' => admin_url( 'admin-ajax.php', (is_ssl() ? 'https' : 'http') ), |
| 277 |
'plugin_url' => WPCOMMENT_URL, |
| 278 |
); |
| 279 |
|
| 280 |
|
| 281 |
if( $wpcomment_meta_fields ) { |
| 282 |
|
| 283 |
foreach($wpcomment_meta_fields as $field){ |
| 284 |
|
| 285 |
$type = isset($field['type']) ? $field['type'] : ''; |
| 286 |
$title = ( isset($field['title']) ? $field ['title'] : ''); |
| 287 |
$data_name = ( isset($field['data_name']) ? $field ['data_name'] : $title); |
| 288 |
$data_name = sanitize_key( $data_name ); |
| 289 |
$field['data_name'] = $data_name; |
| 290 |
$field['title'] = stripslashes($title); |
| 291 |
|
| 292 |
// updated single inputs meta to new variable |
| 293 |
$fields_meta = $field; |
| 294 |
|
| 295 |
if( ! wpcomment_is_field_visible($fields_meta) ) continue; |
| 296 |
|
| 297 |
// change input type in js file |
| 298 |
$fields_meta['field_type'] = apply_filters('wpcomment_js_fields', $type, $fields_meta); |
| 299 |
|
| 300 |
|
| 301 |
// Allow other types to be hooked |
| 302 |
$type = apply_filters('wpcomment_load_input_script_type', $type, $field); |
| 303 |
|
| 304 |
switch( $type ) { |
| 305 |
|
| 306 |
case 'text': |
| 307 |
if( !empty($field['input_mask']) ) { |
| 308 |
WPCOMMENT_SCRIPTS::enqueue_script( 'wpcomment-inputmask' ); |
| 309 |
} |
| 310 |
break; |
| 311 |
|
| 312 |
case 'date': |
| 313 |
if(isset($field['jquery_dp']) && $field['jquery_dp'] == 'on') { |
| 314 |
WPCOMMENT_SCRIPTS::enqueue_style( 'jqueryui' ); |
| 315 |
} |
| 316 |
break; |
| 317 |
|
| 318 |
case 'daterange': |
| 319 |
// Check if value is in GET |
| 320 |
if( !empty($_GET[$data_name]) ) { |
| 321 |
|
| 322 |
$value = sanitize_key($_GET[$data_name]); |
| 323 |
$to_dates = explode(' - ', $value); |
| 324 |
$fields_meta['start_date'] = $to_dates[0]; |
| 325 |
$fields_meta['end_date'] = $to_dates[0]; |
| 326 |
} |
| 327 |
break; |
| 328 |
|
| 329 |
case 'color': |
| 330 |
WPCOMMENT_SCRIPTS::enqueue_script( 'iris' ); |
| 331 |
|
| 332 |
if( !empty($_GET[$data_name]) ) { |
| 333 |
|
| 334 |
$fields_meta['default_color'] = sanitize_key($_GET[$data_name]); |
| 335 |
} |
| 336 |
break; |
| 337 |
|
| 338 |
case 'image': |
| 339 |
WPCOMMENT_SCRIPTS::enqueue_script( 'wpcomment-zoom' ); |
| 340 |
self::add_inline_css('image', $field); |
| 341 |
break; |
| 342 |
|
| 343 |
case 'file': |
| 344 |
|
| 345 |
$wpcomment_file_inputs[] = $field; |
| 346 |
|
| 347 |
WPCOMMENT_SCRIPTS::enqueue_script( 'wpcomment-file-upload' ); |
| 348 |
|
| 349 |
break; |
| 350 |
|
| 351 |
case 'divider': |
| 352 |
WPCOMMENT_SCRIPTS::enqueue_style( 'wpcomment-divider-input' ); |
| 353 |
break; |
| 354 |
} |
| 355 |
|
| 356 |
$inputs_meta_updated[] = $fields_meta; |
| 357 |
|
| 358 |
// Conditional fields |
| 359 |
if( isset($field['logic']) && $field['logic'] == 'on' && !empty($field['conditions']) ){ |
| 360 |
|
| 361 |
$field_conditions = $field['conditions']; |
| 362 |
|
| 363 |
//WPML Translation |
| 364 |
$condition_rules = $field_conditions['rules']; |
| 365 |
$rule_index = 0; |
| 366 |
foreach($condition_rules as $rule) { |
| 367 |
if( !isset($field_conditions['rules'][$rule_index]['element_values']) ) continue; |
| 368 |
|
| 369 |
$field_conditions['rules'][$rule_index]['element_values'] = wpcomment_wpml_translate($rule['element_values'], 'PPOM'); |
| 370 |
$rule_index++; |
| 371 |
} |
| 372 |
|
| 373 |
$wpcomment_conditional_fields[$data_name] = $field_conditions; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* creating action space to render hooks for more addons |
| 378 |
**/ |
| 379 |
do_action('wpcomment_hooks_inputs', $field, $data_name); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
WPCOMMENT_SCRIPTS::enqueue_script( 'wpcomment-inputs' ); |
| 384 |
|
| 385 |
// WPCOMMENT_SCRIPTS::inline_script('wpcomment-inputs', htmlspecialchars_decode($wpcomment->inline_js)); |
| 386 |
|
| 387 |
WPCOMMENT_SCRIPTS::inline_style( 'wpcomment-main', html_entity_decode(get_option('wpcomment_css_output')) ); |
| 388 |
|
| 389 |
$file_js_vars['file_inputs'] = $wpcomment_file_inputs; |
| 390 |
|
| 391 |
$input_js_vars['field_meta'] = $inputs_meta_updated; |
| 392 |
|
| 393 |
|
| 394 |
// $input_js_vars = apply_filters('wpcomment_input_vars', $input_js_vars, $product); |
| 395 |
|
| 396 |
// Conditional fields |
| 397 |
if( !empty($wpcomment_conditional_fields) || apply_filters('wpcomment_enqueue_conditions_js', false)) { |
| 398 |
$wpcomment_conditions_script = 'wpcomment-conditions'; |
| 399 |
$wpcomment_conditions_script = apply_filters('wpcomment_conditional_script_file', $wpcomment_conditions_script); |
| 400 |
|
| 401 |
wpcomment_SCRIPTS::enqueue_script( $wpcomment_conditions_script, self::$scripts_url."/js/{$wpcomment_conditions_script}.js", array('jquery','wpcomment-inputs') ); |
| 402 |
|
| 403 |
self::set_localize_data( $wpcomment_conditions_script, 'wpcomment_input_vars', $input_js_vars, $global_js_vars ); |
| 404 |
} |
| 405 |
|
| 406 |
if( wpcomment_is_client_validation_enabled() ){ |
| 407 |
WPCOMMENT_SCRIPTS::enqueue_script( 'wpcomment-validate' ); |
| 408 |
} |
| 409 |
|
| 410 |
self::set_localize_data( 'wpcomment-file-upload', 'wpcomment_file_vars', $file_js_vars, $global_js_vars ); |
| 411 |
self::set_localize_data( 'wpcomment-inputs', 'wpcomment_input_vars', $input_js_vars, $global_js_vars ); |
| 412 |
self::set_localize_data( 'wpcomment-sm-popup', 'wpcomment_tooltip_vars' ); |
| 413 |
} |
| 414 |
|
| 415 |
do_action('wpcomment_after_scripts_loaded', $form_obj); |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
private static function set_localize_data($handle, $var_name, $js_vars=array(), $global_js_vars=array()){ |
| 420 |
|
| 421 |
if (!wp_script_is( $handle )) { return; } |
| 422 |
|
| 423 |
$localize_data = []; |
| 424 |
switch ($handle) { |
| 425 |
|
| 426 |
case 'wpcomment-file-upload': |
| 427 |
|
| 428 |
$localize_data = array( |
| 429 |
'file_upload_path_thumb' => wpcomment_get_dir_url(true), |
| 430 |
'file_upload_path' => wpcomment_get_dir_url(), |
| 431 |
'mesage_max_files_limit' => __(' files allowed only', "wpcomment"), |
| 432 |
'delete_file_msg' => __("Are you sure?", "wpcomment"), |
| 433 |
'aviary_api_key' => '', |
| 434 |
'plupload_runtime' => (wpcomment_if_browser_is_ie()) ? 'html5,html4' : 'html5,silverlight,html4,browserplus,gear', |
| 435 |
'wpcomment_file_upload_nonce' => wp_create_nonce( 'wpcomment_uploading_file_action' ), |
| 436 |
'wpcomment_file_delete_nonce' => wp_create_nonce( 'wpcomment_deleting_file_action' ), |
| 437 |
); |
| 438 |
|
| 439 |
break; |
| 440 |
|
| 441 |
case 'wpcomment-inputs': |
| 442 |
case 'wpcomment-conditions-v2': |
| 443 |
|
| 444 |
|
| 445 |
$localize_data = array( |
| 446 |
'wpcomment_validate_nonce' => wp_create_nonce( 'wpcomment_validating_action' ), |
| 447 |
'validate_msg' => __("is a required field",'wp-comment-fields'), |
| 448 |
'image_max_msg' => __("You can only select a maximum of",'wp-comment-fields'), |
| 449 |
'image_min_msg' => __("You can only select a minimum of",'wp-comment-fields'), |
| 450 |
); |
| 451 |
|
| 452 |
break; |
| 453 |
|
| 454 |
case 'wpcomment-sm-popup': |
| 455 |
$localize_data = array( |
| 456 |
'wpcomment_tooltip_position' => wpcomment_get_option('wpcomment_input_tooltip_position', 'top'), |
| 457 |
'wpcomment_tooltip_trigger' => wpcomment_get_option('wpcomment_input_tooltip_trigger'), |
| 458 |
'wpcomment_tooltip_interactive' => wpcomment_get_option('wpcomment_input_tooltip_interactive'), |
| 459 |
'wpcomment_tooltip_animation' => wpcomment_get_option('wpcomment_input_tooltip_animation','fade'), |
| 460 |
'wpcomment_tooltip_maxwidth' => wpcomment_get_option('wpcomment_input_tooltip_maxwidth','500'), |
| 461 |
'wpcomment_tooltip_borderclr' => wpcomment_get_option('wpcomment_input_tooltip_borderclr'), |
| 462 |
'wpcomment_tooltip_bgclr' => wpcomment_get_option('wpcomment_input_tooltip_bgclr'), |
| 463 |
'wpcomment_tooltip_txtclr' => wpcomment_get_option('wpcomment_input_tooltip_txtclr'), |
| 464 |
); |
| 465 |
break; |
| 466 |
} |
| 467 |
|
| 468 |
$localize_data = array_merge($js_vars, $localize_data, $global_js_vars); |
| 469 |
|
| 470 |
$localize_data = apply_filters( $var_name, $localize_data ); |
| 471 |
|
| 472 |
WPCOMMENT_SCRIPTS::localize_script( $handle, $var_name, $localize_data ); |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
public static function add_inline_css($type, $field_meta=array()){ |
| 477 |
|
| 478 |
ob_start(); |
| 479 |
include WPCOMMENT_PATH .'/css/style.php'; |
| 480 |
$inline_styles = ob_get_clean(); |
| 481 |
|
| 482 |
WPCOMMENT_SCRIPTS::inline_style('wpcomment-main', $inline_styles); |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
wpcomment_FRONTEND_SCRIPTS::init(); |