enrich
2 years ago
events
2 years ago
formEvents
2 years ago
logger
2 years ago
views
2 years ago
class-custom-event-factory.php
7 years ago
class-custom-event.php
2 years ago
class-event-id-generator.php
5 years ago
class-events-manager-ajax_hook.php
2 years ago
class-events-manager.php
2 years ago
class-fixed-notices.php
2 years ago
class-pixel.php
7 years ago
class-plugin-updater.php
2 years ago
class-plugin.php
7 years ago
class-pys.php
2 years ago
class-settings.php
2 years ago
functions-admin.php
2 years ago
functions-common.php
2 years ago
functions-custom-event.php
3 years ago
functions-edd.php
2 years ago
functions-gdpr.php
2 years ago
functions-license.php
2 years ago
functions-migrate.php
3 years ago
functions-optin.php
2 years ago
functions-promo-notices.php
3 years ago
functions-system-report.php
7 years ago
functions-update-plugin.php
6 years ago
functions-woo.php
2 years ago
options_defaults.json
2 years ago
options_fields.json
2 years ago
class-settings.php
977 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PixelYourSite; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; // Exit if accessed directly. |
| 7 | } |
| 8 | |
| 9 | abstract class Settings { |
| 10 | |
| 11 | /** |
| 12 | * Options section slug |
| 13 | * |
| 14 | * @var string |
| 15 | */ |
| 16 | private $slug; |
| 17 | |
| 18 | /** |
| 19 | * Options values |
| 20 | * |
| 21 | * @var array |
| 22 | */ |
| 23 | private $values = array(); |
| 24 | |
| 25 | /** |
| 26 | * Database option key |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | private $option_key = ''; |
| 31 | |
| 32 | /** |
| 33 | * Default options values |
| 34 | * |
| 35 | * @var array |
| 36 | */ |
| 37 | private $defaults = array(); |
| 38 | |
| 39 | /** |
| 40 | * List of all options |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | private $options = array(); |
| 45 | |
| 46 | private $defaults_json_path; |
| 47 | |
| 48 | /** |
| 49 | * Constructor |
| 50 | * |
| 51 | * @param string $slug |
| 52 | */ |
| 53 | public function __construct( $slug ) { |
| 54 | $this->slug = $slug; |
| 55 | $this->option_key = 'pys_' . $slug; |
| 56 | } |
| 57 | |
| 58 | public function getSlug() { |
| 59 | return $this->slug; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Load options fields and options defaults from specified files |
| 64 | * |
| 65 | * @param string $fields Path to options fields file |
| 66 | * @param string $defaults Path to options defaults file |
| 67 | */ |
| 68 | public function locateOptions( $fields, $defaults ) { |
| 69 | |
| 70 | $this->loadJSON( $fields, false ); |
| 71 | $this->loadJSON( $defaults, true ); |
| 72 | |
| 73 | $this->defaults_json_path = $defaults; |
| 74 | |
| 75 | } |
| 76 | |
| 77 | public function resetToDefaults() { |
| 78 | |
| 79 | if ( ! file_exists( $this->defaults_json_path ) ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | $content = file_get_contents( $this->defaults_json_path ); |
| 84 | $values = json_decode( $content, true ); |
| 85 | |
| 86 | $this->updateOptions( $values ); |
| 87 | |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Load options fields or options defaults from specified file |
| 92 | * |
| 93 | * @param string $file |
| 94 | * @param bool $is_defaults |
| 95 | */ |
| 96 | private function loadJSON( $file, $is_defaults ) { |
| 97 | |
| 98 | if ( ! file_exists( $file ) ) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | $content = file_get_contents( $file ); |
| 103 | $values = json_decode( $content, true ); |
| 104 | |
| 105 | if ( null === $values ) { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | if ( $is_defaults ) { |
| 110 | $this->defaults = $values; |
| 111 | } else { |
| 112 | $this->options = $values; |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Add new option field |
| 119 | * |
| 120 | * @param string $key |
| 121 | * @param string $field_type |
| 122 | * @param mixed $default |
| 123 | */ |
| 124 | public function addOption( $key, $field_type, $default ) { |
| 125 | $this->options[ $key ] = $field_type; |
| 126 | $this->defaults[ $key ] = $default; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Gets an option value or its default value |
| 131 | * |
| 132 | * @param string $key Option key |
| 133 | * @param mixed $fallback Option fallback value if no default is set |
| 134 | * |
| 135 | * @return mixed The value specified for the option or a default value for the option. |
| 136 | */ |
| 137 | public function getOption( $key, $fallback = null ) { |
| 138 | |
| 139 | $this->maybeLoad(); |
| 140 | |
| 141 | // get option default if unset |
| 142 | if ( ! isset( $this->values[ $key ] ) ) { |
| 143 | $this->values[ $key ] = isset( $this->defaults[ $key ] ) |
| 144 | ? $this->defaults[ $key ] : null; |
| 145 | } |
| 146 | |
| 147 | // use fall back value if default is not set |
| 148 | if ( null === $this->values[ $key ] && ! is_null( $fallback ) ) { |
| 149 | $this->values[ $key ] = $fallback; |
| 150 | } |
| 151 | |
| 152 | return $this->values[ $key ]; |
| 153 | |
| 154 | } |
| 155 | public function setOption($key, $value){ |
| 156 | $this->maybeLoad(); |
| 157 | if (isset($value) ) { |
| 158 | $this->values[ $key ] = $value; |
| 159 | } |
| 160 | } |
| 161 | /** |
| 162 | * Load values from database |
| 163 | * |
| 164 | * @param bool $force Force options load |
| 165 | */ |
| 166 | private function maybeLoad( $force = false ) { |
| 167 | |
| 168 | if ( $force || empty( $this->values ) ) { |
| 169 | $this->values = get_option( $this->option_key, null ); |
| 170 | } |
| 171 | |
| 172 | // if there are no settings defined, use default values |
| 173 | if ( ! is_array( $this->values ) ) { |
| 174 | $this->values = $this->defaults; |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | |
| 179 | public function reloadOptions() { |
| 180 | $this->maybeLoad( true ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Sanitize and save options |
| 185 | * |
| 186 | * @param null|array $values Optional. If set, options values will be received from param instead of $_POST. |
| 187 | */ |
| 188 | public function updateOptions( $values = null ) { |
| 189 | |
| 190 | $this->maybeLoad(); |
| 191 | |
| 192 | if ( is_array( $values ) ) { |
| 193 | $form_data = $values; |
| 194 | } else { |
| 195 | $form_data = isset( $_POST['pys'][ $this->slug ] ) ? $_POST['pys'][ $this->slug ] : array(); |
| 196 | } |
| 197 | |
| 198 | // save posted fields |
| 199 | foreach ( $form_data as $key => $value ) { |
| 200 | |
| 201 | if ( isset( $this->options[ $key ] ) ) { |
| 202 | $this->values[ $key ] = $this->sanitize_form_field( $key, $value ); |
| 203 | } |
| 204 | |
| 205 | } |
| 206 | |
| 207 | update_option( $this->option_key, $this->values ); |
| 208 | |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Sanitize form field |
| 213 | * |
| 214 | * @param string $key Field key |
| 215 | * @param array $value Field value |
| 216 | * |
| 217 | * @return mixed Sanitized field value |
| 218 | */ |
| 219 | private function sanitize_form_field( $key, $value ) { |
| 220 | |
| 221 | $type = $this->options[ $key ]; |
| 222 | |
| 223 | // look for very specific sanitization filter |
| 224 | $filter_name = "{$this->option_key}_settings_sanitize_{$key}_field"; |
| 225 | if ( has_filter( $filter_name ) ) { |
| 226 | return apply_filters( $filter_name, $value ); |
| 227 | } |
| 228 | |
| 229 | // look for a sanitize_FIELDTYPE_field method |
| 230 | if ( is_callable( array( $this, 'sanitize_' . $type . '_field' ) ) ) { |
| 231 | return $this->{'sanitize_' . $type . '_field'}( $value ); |
| 232 | } |
| 233 | |
| 234 | // fallback to text |
| 235 | return $this->sanitize_text_field( $value ); |
| 236 | |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Output text input |
| 241 | * |
| 242 | * @param $key |
| 243 | * @param string $placeholder |
| 244 | * @param bool $disabled |
| 245 | * @param bool $hidden |
| 246 | * @param bool $empty |
| 247 | */ |
| 248 | public function render_text_input( $key, $placeholder = '', $disabled = false, $hidden = false, $empty = false) { |
| 249 | |
| 250 | $attr_name = "pys[$this->slug][$key]"; |
| 251 | $attr_id = 'pys_' . $this->slug . '_' . $key; |
| 252 | $attr_value = $empty == false ? $this->getOption( $key ) : ""; |
| 253 | |
| 254 | $classes = array( 'form-control' ); |
| 255 | |
| 256 | if( $hidden ) { |
| 257 | $classes[] = 'form-control-hidden'; |
| 258 | } |
| 259 | |
| 260 | $classes = implode( ' ', $classes ); |
| 261 | |
| 262 | ?> |
| 263 | |
| 264 | <input <?php disabled( $disabled ); ?> type="text" name="<?php esc_attr_e( $attr_name ); ?>" |
| 265 | id="<?php esc_attr_e( $attr_id ); ?>" |
| 266 | value="<?php esc_attr_e( $attr_value ); ?>" |
| 267 | placeholder="<?php esc_attr_e( $placeholder ); ?>" |
| 268 | class="<?php esc_attr_e( $classes ); ?>"> |
| 269 | |
| 270 | <?php |
| 271 | |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Output pixel ID input (text) |
| 276 | * |
| 277 | * @param $key |
| 278 | * @param string $placeholder |
| 279 | * @param int $index |
| 280 | */ |
| 281 | public function render_pixel_id( $key, $placeholder = '', $index = 0 ) { |
| 282 | |
| 283 | $attr_name = "pys[$this->slug][$key][]"; |
| 284 | $attr_id = 'pys_' . $this->slug . '_' . $key . '_' . $index; |
| 285 | |
| 286 | $values = (array) $this->getOption( $key ); |
| 287 | $attr_value = isset( $values[ $index ] ) ? $values[ $index ] : null; |
| 288 | |
| 289 | ?> |
| 290 | |
| 291 | <input type="text" name="<?php esc_attr_e( $attr_name ); ?>" |
| 292 | id="<?php esc_attr_e( $attr_id ); ?>" |
| 293 | value="<?php esc_attr_e( $attr_value ); ?>" |
| 294 | placeholder="<?php esc_attr_e( $placeholder ); ?>" |
| 295 | class="form-control"> |
| 296 | |
| 297 | <?php |
| 298 | |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Output text area input array item |
| 303 | * |
| 304 | * @param $key |
| 305 | * @param string $placeholder |
| 306 | * @param int $index |
| 307 | */ |
| 308 | public function render_text_area_array_item( $key, $placeholder = '', $index = 0 ) { |
| 309 | |
| 310 | $attr_name = "pys[$this->slug][$key][]"; |
| 311 | $attr_id = 'pys_' . $this->slug . '_' . $key . '_' . $index; |
| 312 | |
| 313 | $values = (array) $this->getOption( $key ); |
| 314 | $attr_value = isset( $values[ $index ] ) ? $values[ $index ] : null; |
| 315 | |
| 316 | ?> |
| 317 | |
| 318 | <textarea type="text" name="<?php esc_attr_e( $attr_name ); ?>" |
| 319 | id="<?php esc_attr_e( $attr_id ); ?>" |
| 320 | placeholder="<?php esc_attr_e( $placeholder ); ?>" |
| 321 | class="form-control"><?php esc_attr_e( $attr_value ); ?></textarea> |
| 322 | |
| 323 | <?php |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Output text input array item |
| 328 | * |
| 329 | * @param $key |
| 330 | * @param string $placeholder |
| 331 | * @param int $index |
| 332 | */ |
| 333 | public function render_text_input_array_item( $key, $placeholder = '', $index = 0,$hidden = false ) { |
| 334 | |
| 335 | $attr_name = "pys[$this->slug][$key][]"; |
| 336 | $attr_id = 'pys_' . $this->slug . '_' . $key . '_' . $index; |
| 337 | |
| 338 | $values = (array) $this->getOption( $key ); |
| 339 | $attr_value = isset( $values[ $index ] ) ? $values[ $index ] : null; |
| 340 | |
| 341 | ?> |
| 342 | |
| 343 | <input type=<?=$hidden? "hidden": "text"?> name="<?php esc_attr_e( $attr_name ); ?>" |
| 344 | id="<?php esc_attr_e( $attr_id ); ?>" |
| 345 | value="<?php esc_attr_e( $attr_value ); ?>" |
| 346 | placeholder="<?php esc_attr_e( $placeholder ); ?>" |
| 347 | class="form-control"> |
| 348 | <?php |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Output text area input |
| 353 | * |
| 354 | * @param $key |
| 355 | * @param string $placeholder |
| 356 | * @param bool $disabled |
| 357 | * @param bool $hidden |
| 358 | */ |
| 359 | public function render_text_area_input( $key, $placeholder = '', $disabled = false, $hidden = false ) { |
| 360 | |
| 361 | $attr_name = "pys[$this->slug][$key]"; |
| 362 | $attr_id = 'pys_' . $this->slug . '_' . $key; |
| 363 | $attr_value = $this->getOption( $key ); |
| 364 | |
| 365 | $classes = array( 'form-control' ); |
| 366 | |
| 367 | if( $hidden ) { |
| 368 | $classes[] = 'form-control-hidden'; |
| 369 | } |
| 370 | |
| 371 | $classes = implode( ' ', $classes ); |
| 372 | |
| 373 | ?> |
| 374 | |
| 375 | <textarea <?php disabled( $disabled ); ?> name="<?php esc_attr_e( $attr_name ); ?>" |
| 376 | id="<?php esc_attr_e( $attr_id ); ?>" rows="5" |
| 377 | placeholder="<?php esc_attr_e( $placeholder ); ?>" |
| 378 | class="<?php esc_attr_e( $classes ); ?>"><?php esc_html_e( $attr_value ); ?></textarea> |
| 379 | |
| 380 | <?php |
| 381 | |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Output checkbox input stylized as switcher |
| 386 | * |
| 387 | * @param $key |
| 388 | * @param bool $collapse |
| 389 | * @param bool $disabled |
| 390 | */ |
| 391 | public function render_switcher_input( $key, $collapse = false, $disabled = false ) { |
| 392 | |
| 393 | $attr_name = "pys[$this->slug][$key]"; |
| 394 | $attr_id = 'pys_' . $this->slug . '_' . $key; |
| 395 | $attr_value = $this->getOption( $key ); |
| 396 | |
| 397 | $classes = array( 'custom-switch' ); |
| 398 | |
| 399 | if ( $collapse ) { |
| 400 | $classes[] = 'collapse-control'; |
| 401 | } |
| 402 | |
| 403 | if ( $disabled ) { |
| 404 | $classes[] = 'disabled'; |
| 405 | $attr_name = ""; |
| 406 | $attr_value = false; |
| 407 | } |
| 408 | |
| 409 | $classes = implode( ' ', $classes ); |
| 410 | |
| 411 | ?> |
| 412 | |
| 413 | <div class="<?php esc_attr_e( $classes ); ?>"> |
| 414 | |
| 415 | <?php if ( ! $disabled ) : ?> |
| 416 | <input type="hidden" name="<?php esc_attr_e( $attr_name ); ?>" value="0"> |
| 417 | <?php endif; ?> |
| 418 | |
| 419 | <?php if ( $collapse ) : ?> |
| 420 | <input type="checkbox" name="<?php esc_attr_e( $attr_name ); ?>" value="1" <?php disabled( $disabled, |
| 421 | true ); ?> <?php checked( $attr_value, true ); ?> |
| 422 | id="<?php esc_attr_e( $attr_id ); ?>" |
| 423 | class="custom-switch-input" |
| 424 | data-target="pys_<?php esc_attr_e( $this->slug ); ?>_<?php esc_attr_e( $key ); ?>_panel"> |
| 425 | <?php else : ?> |
| 426 | <input type="checkbox" name="<?php esc_attr_e( $attr_name ); ?>" value="1" <?php disabled( $disabled, |
| 427 | true ); ?> <?php checked( $attr_value, true ); ?> id="<?php esc_attr_e( $attr_id ); ?>" |
| 428 | class="custom-switch-input"> |
| 429 | <?php endif; ?> |
| 430 | |
| 431 | <label class="custom-switch-btn" for="<?php esc_attr_e( $attr_id ); ?>"></label> |
| 432 | </div> |
| 433 | |
| 434 | <?php |
| 435 | |
| 436 | } |
| 437 | |
| 438 | public function render_switcher_input_array( $key, $index = 0) { |
| 439 | |
| 440 | $attr_name = "pys[$this->slug][$key][]"; |
| 441 | $attr_id = 'pys_' . $this->slug . '_' . $key."_".$index; |
| 442 | $attr_values = (array)$this->getOption( $key ); |
| 443 | $value = "index_".$index; |
| 444 | $valueIndex = array_search($value,$attr_values); |
| 445 | |
| 446 | $classes = array( 'custom-switch' ); |
| 447 | |
| 448 | $classes = implode( ' ', $classes ); |
| 449 | |
| 450 | ?> |
| 451 | |
| 452 | <div class="<?php esc_attr_e( $classes ); ?>"> |
| 453 | <input type="checkbox" |
| 454 | name="<?php esc_attr_e( $attr_name ); ?>" |
| 455 | value="<?=$value?>" |
| 456 | <?=$valueIndex !== false ? "checked" : "" ?> |
| 457 | id="<?php esc_attr_e( $attr_id ); ?>" |
| 458 | class="custom-switch-input"> |
| 459 | |
| 460 | <label class="custom-switch-btn" for="<?php esc_attr_e( $attr_id ); ?>"></label> |
| 461 | </div> |
| 462 | |
| 463 | <?php |
| 464 | |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Output checkbox input |
| 469 | * |
| 470 | * @param $key |
| 471 | * @param $label |
| 472 | * @param bool $disabled |
| 473 | */ |
| 474 | public function render_checkbox_input( $key, $label, $disabled = false ) { |
| 475 | |
| 476 | $attr_name = "pys[$this->slug][$key]"; |
| 477 | $attr_value = $this->getOption( $key ); |
| 478 | |
| 479 | ?> |
| 480 | |
| 481 | <label class="custom-control custom-checkbox"> |
| 482 | <input type="hidden" name="<?php esc_attr_e( $attr_name ); ?>" value="0"> |
| 483 | <input type="checkbox" name="<?php esc_attr_e( $attr_name ); ?>" value="1" |
| 484 | class="custom-control-input" <?php disabled( $disabled, true ); ?> <?php checked( $attr_value, |
| 485 | true ); ?>> |
| 486 | <span class="custom-control-indicator"></span> |
| 487 | <span class="custom-control-description"><?php echo wp_kses_post( $label ); ?></span> |
| 488 | </label> |
| 489 | |
| 490 | <?php |
| 491 | |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Output radio input |
| 496 | * |
| 497 | * @param $key |
| 498 | * @param $value |
| 499 | * @param $label |
| 500 | * @param bool $disabled |
| 501 | */ |
| 502 | public function render_radio_input( $key, $value, $label, $disabled = false, $with_pro_badge = false ) { |
| 503 | |
| 504 | $attr_name = "pys[$this->slug][$key]"; |
| 505 | |
| 506 | ?> |
| 507 | |
| 508 | <label class="custom-control custom-radio"> |
| 509 | <input type="radio" name="<?php esc_attr_e( $attr_name ); ?>" <?php disabled( $disabled, true ); ?> |
| 510 | class="custom-control-input" <?php checked( $this->getOption( $key ), $value ); ?> |
| 511 | value="<?php esc_attr_e( $value ); ?>"> |
| 512 | <span class="custom-control-indicator"></span> |
| 513 | <span class="custom-control-description"><?php echo wp_kses_post( $label ); ?></span> |
| 514 | <?php if ( $with_pro_badge ) { |
| 515 | renderCogBadge(); |
| 516 | } ?> |
| 517 | </label> |
| 518 | |
| 519 | <?php |
| 520 | |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Output number input |
| 525 | * |
| 526 | * @param string $key |
| 527 | * @param string $placeholder |
| 528 | * @param bool $disabled |
| 529 | */ |
| 530 | public function render_number_input( $key, $placeholder = '', $disabled = false,$max = null,$min = 0 ) { |
| 531 | |
| 532 | $attr_name = "pys[$this->slug][$key]"; |
| 533 | $attr_id = 'pys_' . $this->slug . '_' . $key; |
| 534 | $attr_value = $this->getOption( $key ); |
| 535 | |
| 536 | ?> |
| 537 | |
| 538 | <input <?php disabled( $disabled ); ?> type="number" name="<?php esc_attr_e( $attr_name ); ?>" |
| 539 | id="<?php esc_attr_e( $attr_id ); ?>" |
| 540 | value="<?php esc_attr_e( $attr_value ); ?>" |
| 541 | placeholder="<?php esc_attr_e( $placeholder ); ?>" |
| 542 | min="<?=$min?>" class="form-control" |
| 543 | <?php if($max != null) : ?> max="<?=$max?>" <?php endif; ?> |
| 544 | > |
| 545 | |
| 546 | <?php |
| 547 | |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Output select input |
| 552 | * |
| 553 | * @param $key |
| 554 | * @param $options |
| 555 | * @param bool $disabled |
| 556 | * @param null $visibility_target |
| 557 | * @param null $visibility_value |
| 558 | */ |
| 559 | public function render_select_input( $key, $options, $disabled = false, $visibility_target = null, |
| 560 | $visibility_value = null ) { |
| 561 | |
| 562 | $attr_name = "pys[$this->slug][$key]"; |
| 563 | $attr_id = 'pys_' . $this->slug . '_' . $key; |
| 564 | |
| 565 | $classes = array( 'form-control-sm' ); |
| 566 | |
| 567 | if ( $visibility_target ) { |
| 568 | $classes[] = 'controls-visibility'; |
| 569 | } |
| 570 | |
| 571 | $classes = implode( ' ', $classes ); |
| 572 | |
| 573 | ?> |
| 574 | |
| 575 | <select class="<?php esc_attr_e( $classes ); ?>" id="<?php esc_attr_e( $attr_id ); ?>" |
| 576 | name="<?php esc_attr_e( $attr_name ); ?>" <?php disabled( $disabled ); ?> |
| 577 | data-target="<?php esc_attr_e( $visibility_target ); ?>" |
| 578 | data-value="<?php esc_attr_e( $visibility_value ); ?>" autocomplete="off"> |
| 579 | |
| 580 | <option value="" disabled selected>Please, select...</option> |
| 581 | |
| 582 | <?php foreach ( $options as $option_key => $option_value ) : ?> |
| 583 | <option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, |
| 584 | esc_attr( $this->getOption( $key ) ) ); ?> <?php disabled( $option_key, |
| 585 | 'disabled' ); ?>><?php echo esc_attr( $option_value ); ?></option> |
| 586 | <?php endforeach; ?> |
| 587 | </select> |
| 588 | |
| 589 | <?php |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Output multi select input |
| 594 | * |
| 595 | * @param $key |
| 596 | * @param $values |
| 597 | * @param bool $disabled |
| 598 | */ |
| 599 | public function render_multi_select_input( $key, $values, $disabled = false ) { |
| 600 | |
| 601 | $attr_name = "pys[$this->slug][$key][]"; |
| 602 | $attr_id = 'pys_' . $this->slug . '_' . $key; |
| 603 | |
| 604 | $selected = $this->getOption( $key ); |
| 605 | |
| 606 | ?> |
| 607 | |
| 608 | <input type="hidden" name="<?php esc_attr_e( $attr_name ); ?>" value=""> |
| 609 | <select class="form-control pys-pysselect2" name="<?php esc_attr_e( $attr_name ); ?>" |
| 610 | id="<?php esc_attr_e( $attr_id ); ?>" <?php disabled( $disabled ); ?> style="width: 100%;" |
| 611 | multiple> |
| 612 | |
| 613 | <?php foreach ( $values as $option_key => $option_value ) : ?> |
| 614 | <option value="<?php echo esc_attr( $option_key ); ?>" |
| 615 | <?php selected( in_array( $option_key, $selected ) ); ?> |
| 616 | <?php disabled( $option_key, 'disabled' ); ?> |
| 617 | > |
| 618 | <?php echo esc_attr( $option_value ); ?> |
| 619 | </option> |
| 620 | <?php endforeach; ?> |
| 621 | |
| 622 | </select> |
| 623 | |
| 624 | <?php |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Output tags select input |
| 629 | * |
| 630 | * @param $key |
| 631 | * @param bool $disabled |
| 632 | */ |
| 633 | public function render_tags_select_input( $key, $disabled = false ) { |
| 634 | |
| 635 | $attr_name = "pys[$this->slug][$key][]"; |
| 636 | $attr_id = 'pys_' . $this->slug . '_' . $key; |
| 637 | |
| 638 | $tags = $this->getOption( $key ); |
| 639 | $tags = is_array( $tags ) ? array_filter( $tags ) : array(); |
| 640 | |
| 641 | ?> |
| 642 | |
| 643 | <input type="hidden" name="<?php esc_attr_e( $attr_name ); ?>" value=""> |
| 644 | <select class="form-control pys-tags-pysselect2" name="<?php esc_attr_e( $attr_name ); ?>" |
| 645 | id="<?php esc_attr_e( $attr_id ); ?>" <?php disabled( $disabled ); ?> style="width: 100%;" |
| 646 | multiple> |
| 647 | |
| 648 | <?php foreach ( $tags as $tag ) : ?> |
| 649 | <option value="<?php echo esc_attr( $tag ); ?>" selected> |
| 650 | <?php echo esc_attr( $tag ); ?> |
| 651 | </option> |
| 652 | <?php endforeach; ?> |
| 653 | |
| 654 | </select> |
| 655 | |
| 656 | <?php |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Sanitize text field value |
| 661 | * |
| 662 | * @param $value |
| 663 | * |
| 664 | * @return string |
| 665 | */ |
| 666 | public function sanitize_text_field( $value ) { |
| 667 | |
| 668 | $value = is_null( $value ) ? '' : $value; |
| 669 | |
| 670 | return wp_kses_post( trim( stripslashes( $value ) ) ); |
| 671 | |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Sanitize textarea field value |
| 676 | * |
| 677 | * @param $value |
| 678 | * |
| 679 | * @return string |
| 680 | */ |
| 681 | public function sanitize_textarea_field( $value ){ |
| 682 | |
| 683 | $value = is_null( $value ) ? '' : $value; |
| 684 | |
| 685 | return trim( stripslashes( $value ) ); |
| 686 | |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Sanitize number field value |
| 691 | * |
| 692 | * @param $value |
| 693 | * |
| 694 | * @return int |
| 695 | */ |
| 696 | public function sanitize_number_field( $value ) { |
| 697 | return (int) $value; |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * Sanitize checkbox field value |
| 702 | * |
| 703 | * @param $value |
| 704 | * |
| 705 | * @return bool |
| 706 | */ |
| 707 | public function sanitize_checkbox_field( $value ) { |
| 708 | |
| 709 | if ( is_bool( $value ) || is_numeric( $value ) ) { |
| 710 | return (bool) $value; |
| 711 | } else { |
| 712 | return false; |
| 713 | } |
| 714 | |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Sanitize radio field value |
| 719 | * |
| 720 | * @param $value |
| 721 | * |
| 722 | * @return null|string |
| 723 | */ |
| 724 | public function sanitize_radio_field( $value ) { |
| 725 | return ! is_null( $value ) ? trim( stripslashes( $value ) ) : null; |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * Sanitize select field value |
| 730 | * |
| 731 | * @see deepSanitizeTextField() |
| 732 | * |
| 733 | * @param $value |
| 734 | * |
| 735 | * @return array|string |
| 736 | */ |
| 737 | public function sanitize_select_field( $value ) { |
| 738 | |
| 739 | $value = is_null( $value ) ? '' : $value; |
| 740 | |
| 741 | return deepSanitizeTextField( stripslashes( $value ) ); |
| 742 | |
| 743 | } |
| 744 | |
| 745 | /** |
| 746 | * Sanitize tags select value |
| 747 | * |
| 748 | * @see deepSanitizeTextField() |
| 749 | * |
| 750 | * @param $value |
| 751 | * |
| 752 | * @return array |
| 753 | */ |
| 754 | public function sanitize_multi_select_field( $value ) { |
| 755 | return is_array( $value ) ? array_map( 'PixelYourSite\deepSanitizeTextField', $value ) : array(); |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * @param $value |
| 760 | * |
| 761 | * @return array |
| 762 | */ |
| 763 | public function sanitize_tag_select_field( $value ) { |
| 764 | return is_array( $value ) ? array_map( 'PixelYourSite\deepSanitizeTextField', $value ) : array(); |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Sanitize array field value |
| 769 | * |
| 770 | * @param $values |
| 771 | * |
| 772 | * @return array |
| 773 | */ |
| 774 | public function sanitize_array_field( $values ) { |
| 775 | |
| 776 | $values = is_array( $values ) ? $values : array(); |
| 777 | $sanitized = array(); |
| 778 | |
| 779 | foreach ( $values as $key => $value ) { |
| 780 | |
| 781 | $new_value = $this->sanitize_text_field( $value ); |
| 782 | |
| 783 | if ( ! empty( $new_value ) && ! in_array( $new_value, $sanitized ) ) { |
| 784 | $sanitized[ $key ] = $new_value; |
| 785 | } |
| 786 | |
| 787 | } |
| 788 | |
| 789 | return $sanitized; |
| 790 | |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * Sanitize array field value |
| 795 | * |
| 796 | * @param $values |
| 797 | * |
| 798 | * @return array |
| 799 | */ |
| 800 | public function sanitize_array_textarea_field( $values ) { |
| 801 | |
| 802 | $values = is_array( $values ) ? $values : array(); |
| 803 | $sanitized = array(); |
| 804 | |
| 805 | foreach ( $values as $key => $value ) { |
| 806 | |
| 807 | $new_value = $this->sanitize_textarea_field( $value ); |
| 808 | |
| 809 | if ( ! empty( $new_value ) && ! in_array( $new_value, $sanitized ) ) { |
| 810 | $sanitized[ $key ] = $new_value; |
| 811 | } |
| 812 | |
| 813 | } |
| 814 | |
| 815 | return $sanitized; |
| 816 | } |
| 817 | /** |
| 818 | * Sanitize array field value with duplicates value |
| 819 | * |
| 820 | * @param $values |
| 821 | * |
| 822 | * @return array |
| 823 | */ |
| 824 | public function sanitize_array_v_field( $values ) { |
| 825 | |
| 826 | $values = is_array( $values ) ? $values : array(); |
| 827 | $sanitized = array(); |
| 828 | |
| 829 | foreach ( $values as $key => $value ) { |
| 830 | |
| 831 | $new_value = $this->sanitize_text_field( $value ); |
| 832 | |
| 833 | if ( ! empty( $new_value ) ) { |
| 834 | $sanitized[ $key ] = $new_value; |
| 835 | } |
| 836 | |
| 837 | } |
| 838 | |
| 839 | return $sanitized; |
| 840 | |
| 841 | } |
| 842 | public function render_checkbox_input_array( $key, $label, $index = 0, $disabled = false ) { |
| 843 | |
| 844 | $attr_name = "pys[$this->slug][$key][]"; |
| 845 | $attr_values = (array)$this->getOption( $key ); |
| 846 | $value = "index_".$index; |
| 847 | $valueIndex = array_search($value,$attr_values); |
| 848 | |
| 849 | ?> |
| 850 | |
| 851 | <label class="custom-control custom-checkbox"> |
| 852 | <input type="checkbox" name="<?php esc_attr_e( $attr_name ); ?>" value="<?=$value?>" |
| 853 | class="custom-control-input" <?php disabled( $disabled, true ); ?> |
| 854 | <?=$valueIndex !== false ? "checked" : "" ?>> |
| 855 | <span class="custom-control-indicator"></span> |
| 856 | <span class="custom-control-description"><?php echo wp_kses_post( $label ); ?></span> |
| 857 | </label> |
| 858 | |
| 859 | <?php |
| 860 | |
| 861 | } |
| 862 | function renderDummyTextInput( $placeholder = '' ) { |
| 863 | ?> |
| 864 | |
| 865 | <input type="text" disabled="disabled" placeholder="<?php esc_html_e( $placeholder ); ?>" class="form-control"> |
| 866 | |
| 867 | <?php |
| 868 | } |
| 869 | function renderDummyNumberInput($default = 0) { |
| 870 | ?> |
| 871 | |
| 872 | <input type="number" disabled="disabled" min="0" max="100" class="form-control" value="<?=$default?>"> |
| 873 | |
| 874 | <?php |
| 875 | } |
| 876 | |
| 877 | function renderDummySwitcher($isEnable = false) { |
| 878 | $attr = $isEnable ? " checked='checked'" : ""; |
| 879 | ?> |
| 880 | |
| 881 | <div class="custom-switch disabled"> |
| 882 | <input type="checkbox" value="1" <?=$attr?> disabled="disabled" class="custom-switch-input"> |
| 883 | <label class="custom-switch-btn"></label> |
| 884 | </div> |
| 885 | |
| 886 | <?php |
| 887 | } |
| 888 | |
| 889 | function renderDummyCheckbox( $label, $with_pro_badge = false ) { |
| 890 | ?> |
| 891 | |
| 892 | <label class="custom-control custom-checkbox <?php echo $with_pro_badge ? 'custom-checkbox-badge' : ''; ?>"> |
| 893 | <input type="checkbox" value="1" |
| 894 | class="custom-control-input" disabled="disabled"> |
| 895 | <span class="custom-control-indicator"></span> |
| 896 | <span class="custom-control-description"> |
| 897 | <?php echo wp_kses_post( $label ); ?> |
| 898 | <?php if ( $with_pro_badge ) { |
| 899 | renderProBadge(); |
| 900 | } ?> |
| 901 | </span> |
| 902 | </label> |
| 903 | |
| 904 | <?php |
| 905 | } |
| 906 | |
| 907 | function renderDummyRadioInput( $label, $checked = false ) { |
| 908 | ?> |
| 909 | |
| 910 | <label class="custom-control custom-radio"> |
| 911 | <input type="radio" disabled="disabled" |
| 912 | class="custom-control-input" <?php checked( $checked ); ?>> |
| 913 | <span class="custom-control-indicator"></span> |
| 914 | <span class="custom-control-description"><?php echo wp_kses_post( $label ); ?></span> |
| 915 | </label> |
| 916 | |
| 917 | <?php |
| 918 | } |
| 919 | |
| 920 | function renderDummyTagsFields( $tags = array() ) { |
| 921 | ?> |
| 922 | |
| 923 | <select class="form-control pys-tags-pysselect2" disabled="disabled" style="width: 100%;" multiple> |
| 924 | |
| 925 | <?php if(!empty($tags)){ |
| 926 | foreach ( $tags as $tag ) : ?> |
| 927 | <option value="<?php echo esc_attr( $tag ); ?>" selected> |
| 928 | <?php echo esc_attr( $tag ); ?> |
| 929 | </option> |
| 930 | <?php endforeach; |
| 931 | } |
| 932 | ?> |
| 933 | |
| 934 | </select> |
| 935 | |
| 936 | <?php |
| 937 | } |
| 938 | |
| 939 | function renderDummySelectInput( $value, $full_width = false ) { |
| 940 | |
| 941 | $attr_width = $full_width ? 'width: 100%;' : ''; |
| 942 | |
| 943 | ?> |
| 944 | |
| 945 | <select class="form-control form-control-sm" disabled="disabled" autocomplete="off" style="<?php esc_attr_e( $attr_width ); ?>"> |
| 946 | <option value="" disabled selected><?php esc_html_e( $value ); ?></option> |
| 947 | </select> |
| 948 | |
| 949 | <?php |
| 950 | } |
| 951 | |
| 952 | function renderProBadge( $url = null,$label = "Pro Feature" ) { |
| 953 | |
| 954 | if ( ! $url ) { |
| 955 | $url = 'https://www.pixelyoursite.com/'; |
| 956 | } |
| 957 | |
| 958 | $url = untrailingslashit( $url ) . '/?utm_source=pys-free-plugin&utm_medium=pro-badge&utm_campaign=pro-feature'; |
| 959 | |
| 960 | echo ' <a href="' . esc_url( $url ) . '" target="_blank" class="badge badge-pill badge-pro">'.$label.' <i class="fa fa-external-link" aria-hidden="true"></i></a>'; |
| 961 | } |
| 962 | public function convertTimeToSeconds($timeValue = 24, $type = 'hours') |
| 963 | { |
| 964 | switch ($type){ |
| 965 | case 'hours': |
| 966 | $time = $timeValue * 60 * 60; |
| 967 | break; |
| 968 | case 'minute': |
| 969 | $time = $timeValue * 60; |
| 970 | break; |
| 971 | case 'seconds': |
| 972 | $time = $timeValue; |
| 973 | break; |
| 974 | } |
| 975 | return $time; |
| 976 | } |
| 977 | } |