class-settings-entity.php
789 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WPFront Scroll Top |
| 4 | * |
| 5 | * @package wpfront-scroll-top |
| 6 | * @author Syam Mohan |
| 7 | * @copyright 2013 WPFront |
| 8 | * @license GPL-2.0-or-later |
| 9 | */ |
| 10 | |
| 11 | namespace WPFront\Scroll_Top; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Entity class of WPFront Scroll Top Plugin |
| 17 | * |
| 18 | * @package wpfront-scroll-top |
| 19 | */ |
| 20 | class Settings_Entity { |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * Scroll Top Enabled. |
| 25 | * |
| 26 | * @var bool |
| 27 | */ |
| 28 | public $enabled = false; |
| 29 | |
| 30 | /** |
| 31 | * Scroll Top Javascript Async. |
| 32 | * |
| 33 | * @var bool |
| 34 | */ |
| 35 | public $javascript_async = false; |
| 36 | |
| 37 | /** |
| 38 | * Scroll Top Scroll Offset. |
| 39 | * |
| 40 | * @var int<0,max> |
| 41 | */ |
| 42 | public $scroll_offset = 100; |
| 43 | |
| 44 | /** |
| 45 | * Scroll Top Button Width. |
| 46 | * |
| 47 | * @var int<0,max> |
| 48 | */ |
| 49 | public $button_width = 0; |
| 50 | |
| 51 | /** |
| 52 | * Scroll Top Button Height. |
| 53 | * |
| 54 | * @var int<0,max> |
| 55 | */ |
| 56 | public $button_height = 0; |
| 57 | |
| 58 | /** |
| 59 | * Scroll Top Button Opacity. |
| 60 | * |
| 61 | * @var int<0,100> |
| 62 | */ |
| 63 | public $button_opacity = 80; |
| 64 | |
| 65 | /** |
| 66 | * Scroll Top Button Fade Duration. |
| 67 | * |
| 68 | * @var int<0,max> |
| 69 | */ |
| 70 | public $button_fade_duration = 0; |
| 71 | |
| 72 | /** |
| 73 | * Scroll Top Button Scroll Duration. |
| 74 | * |
| 75 | * @var int<0,max> |
| 76 | */ |
| 77 | public $scroll_duration = 400; |
| 78 | |
| 79 | /** |
| 80 | * Scroll Top Auto Hide. |
| 81 | * |
| 82 | * @var bool |
| 83 | */ |
| 84 | public $auto_hide = false; |
| 85 | |
| 86 | /** |
| 87 | * Scroll Top Auto Hide After. |
| 88 | * |
| 89 | * @var int<0,max> |
| 90 | */ |
| 91 | public $auto_hide_after = 2; |
| 92 | |
| 93 | /** |
| 94 | * Scroll Top Hide Small Window. |
| 95 | * |
| 96 | * @var bool |
| 97 | */ |
| 98 | public $hide_small_window = false; |
| 99 | |
| 100 | /** |
| 101 | * Scroll Top Window Width. |
| 102 | * |
| 103 | * @var int<0,max> |
| 104 | */ |
| 105 | public $small_window_width = 640; |
| 106 | |
| 107 | /** |
| 108 | * Scroll Top Hide WPAdmin. |
| 109 | * |
| 110 | * @var bool |
| 111 | */ |
| 112 | public $hide_wpadmin = false; |
| 113 | |
| 114 | /** |
| 115 | * Scroll Top Hide iframe. |
| 116 | * |
| 117 | * @var bool |
| 118 | */ |
| 119 | public $hide_iframe = false; |
| 120 | |
| 121 | /** |
| 122 | * Scroll Top Button Style. |
| 123 | * |
| 124 | * @var 'image'|'text'|'font-awesome' |
| 125 | */ |
| 126 | public $button_style = 'image'; |
| 127 | |
| 128 | /** |
| 129 | * Scroll Top Button Action. |
| 130 | * |
| 131 | * @var 'top'|'element'|'url' |
| 132 | */ |
| 133 | public $button_action = 'top'; |
| 134 | |
| 135 | /** |
| 136 | * Scroll Top Button Action Element Selector. |
| 137 | * |
| 138 | * @var string |
| 139 | */ |
| 140 | public $button_action_element_selector = ''; |
| 141 | |
| 142 | /** |
| 143 | * Scroll Top Button Action Container Selector. |
| 144 | * |
| 145 | * @var string |
| 146 | */ |
| 147 | public $button_action_container_selector = 'html, body'; |
| 148 | |
| 149 | /** |
| 150 | * Scroll Top Button Action Offset. |
| 151 | * |
| 152 | * @var int |
| 153 | */ |
| 154 | public $button_action_element_offset = 0; |
| 155 | |
| 156 | /** |
| 157 | * Scroll Top Button Action Page URL. |
| 158 | * |
| 159 | * @var string |
| 160 | */ |
| 161 | public $button_action_page_url = ''; |
| 162 | |
| 163 | /** |
| 164 | * Scroll Top Button Location. |
| 165 | * |
| 166 | * @var int<1,4> |
| 167 | * 1 - Bottom Right |
| 168 | * 2 - Bottom Left |
| 169 | * 3 - Top Right |
| 170 | * 4 - Top Left |
| 171 | */ |
| 172 | public $location = 1; |
| 173 | |
| 174 | /** |
| 175 | * Scroll Top Margin X. |
| 176 | * |
| 177 | * @var int |
| 178 | */ |
| 179 | public $margin_x = 20; |
| 180 | |
| 181 | /** |
| 182 | * Scroll Top Margin Y. |
| 183 | * |
| 184 | * @var int |
| 185 | */ |
| 186 | public $margin_y = 20; |
| 187 | |
| 188 | /** |
| 189 | * Scroll Top Display Pages. |
| 190 | * |
| 191 | * @var int<1,3> |
| 192 | * 1 - All pages |
| 193 | * 2 - Include in pages |
| 194 | * 3 - Exclude in pages |
| 195 | */ |
| 196 | public $display_pages = 1; |
| 197 | |
| 198 | /** |
| 199 | * Scroll Top include Pages. |
| 200 | * |
| 201 | * @var string |
| 202 | */ |
| 203 | public $include_pages = ''; |
| 204 | |
| 205 | /** |
| 206 | * Scroll Top exclude Pages. |
| 207 | * |
| 208 | * @var string |
| 209 | */ |
| 210 | public $exclude_pages = ''; |
| 211 | |
| 212 | /** |
| 213 | * Scroll Top Image. |
| 214 | * |
| 215 | * @var string |
| 216 | */ |
| 217 | public $image = '1.png'; |
| 218 | |
| 219 | /** |
| 220 | * Scroll Top Image Alt. |
| 221 | * |
| 222 | * @var string |
| 223 | */ |
| 224 | public $image_alt = ''; |
| 225 | |
| 226 | /** |
| 227 | * Scroll Top Image Title. |
| 228 | * |
| 229 | * @var string |
| 230 | */ |
| 231 | public $image_title = ''; |
| 232 | |
| 233 | /** |
| 234 | * Scroll Top Custom URL. |
| 235 | * |
| 236 | * @var string |
| 237 | */ |
| 238 | public $custom_url = ''; |
| 239 | |
| 240 | /** |
| 241 | * Scroll Top Text Button Text. |
| 242 | * |
| 243 | * @var string |
| 244 | */ |
| 245 | public $text_button_text = ''; |
| 246 | |
| 247 | /** |
| 248 | * Scroll Top Text Button Text Color. |
| 249 | * |
| 250 | * @var non-empty-string |
| 251 | */ |
| 252 | public $text_button_text_color = '#FFFFFF'; |
| 253 | |
| 254 | /** |
| 255 | * Scroll Top Text Button Background Color. |
| 256 | * |
| 257 | * @var non-empty-string |
| 258 | */ |
| 259 | public $text_button_background_color = '#000000'; |
| 260 | |
| 261 | /** |
| 262 | * Scroll Top Text Button Mouse Over Color. |
| 263 | * |
| 264 | * @var non-empty-string |
| 265 | */ |
| 266 | public $text_button_hover_color = '#000000'; |
| 267 | |
| 268 | /** |
| 269 | * Scroll Top Custom CSS for Text Button. |
| 270 | * |
| 271 | * @var string |
| 272 | */ |
| 273 | public $text_button_css = ''; |
| 274 | |
| 275 | /** |
| 276 | * Scroll Top FA Button Class. |
| 277 | * |
| 278 | * @var string |
| 279 | */ |
| 280 | public $fa_button_class = ''; |
| 281 | |
| 282 | /** |
| 283 | * Scroll Top FA Button Exclude URL. |
| 284 | * |
| 285 | * @var bool |
| 286 | */ |
| 287 | public $fa_button_exclude_url = false; |
| 288 | |
| 289 | /** |
| 290 | * Scroll Top FA Button Text Color. |
| 291 | * |
| 292 | * @var non-empty-string |
| 293 | */ |
| 294 | public $fa_button_text_color = '#000000'; |
| 295 | |
| 296 | /** |
| 297 | * Scroll Top Custom CSS. |
| 298 | * |
| 299 | * @var string |
| 300 | */ |
| 301 | public $fa_button_css = ''; |
| 302 | |
| 303 | /** |
| 304 | * Scroll Top Font Awesome URL. |
| 305 | * |
| 306 | * @var string |
| 307 | */ |
| 308 | public $fa_button_url = ''; |
| 309 | |
| 310 | /** |
| 311 | * Accessibility ARIA Label. |
| 312 | * |
| 313 | * @var string |
| 314 | */ |
| 315 | public $accessibility_aria_label = ''; |
| 316 | |
| 317 | /** |
| 318 | * Accessibility Title. |
| 319 | * |
| 320 | * @var string |
| 321 | */ |
| 322 | public $accessibility_title = ''; |
| 323 | |
| 324 | /** |
| 325 | * Accessibility Screen Reader Text. |
| 326 | * |
| 327 | * @var string |
| 328 | */ |
| 329 | public $accessibility_screen_reader_text = ''; |
| 330 | |
| 331 | /** |
| 332 | * Enqueue CSS using file. |
| 333 | * |
| 334 | * @var bool |
| 335 | */ |
| 336 | public $css_enqueue_file = false; |
| 337 | |
| 338 | /** |
| 339 | * Extra CSS for Scroll Top. |
| 340 | * |
| 341 | * @var string |
| 342 | */ |
| 343 | public $css_extra_css = ''; |
| 344 | |
| 345 | /** |
| 346 | * Last updated |
| 347 | * |
| 348 | * @var int |
| 349 | */ |
| 350 | public $last_updated = 0; |
| 351 | |
| 352 | /** |
| 353 | * WordPress wrapper instance. |
| 354 | * |
| 355 | * @var WP_Wrapper |
| 356 | */ |
| 357 | private $wp; |
| 358 | |
| 359 | /** |
| 360 | * Constructor. |
| 361 | * |
| 362 | * @param WP_Wrapper $wp WordPress wrapper instance. |
| 363 | */ |
| 364 | public function __construct( WP_Wrapper $wp ) { |
| 365 | $this->wp = $wp; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Returns Scroll Top Settings Data. |
| 370 | * |
| 371 | * @return self Returns this settings entity instance. |
| 372 | */ |
| 373 | public function get() { |
| 374 | $data = $this->wp->get_option( 'wpfront-scroll-top-options' ); |
| 375 | |
| 376 | if ( empty( $data ) ) { |
| 377 | $data = array(); |
| 378 | } |
| 379 | |
| 380 | $data = (array) $data; |
| 381 | |
| 382 | // For backward compatibility. |
| 383 | if ( isset( $data['marginX'] ) ) { |
| 384 | $data['margin_x'] = $data['marginX']; |
| 385 | } |
| 386 | |
| 387 | if ( isset( $data['marginY'] ) ) { |
| 388 | $data['margin_y'] = $data['marginY']; |
| 389 | } |
| 390 | |
| 391 | if ( isset( $data['fa_button_URL'] ) ) { |
| 392 | $data['fa_button_url'] = $data['fa_button_URL']; |
| 393 | } |
| 394 | |
| 395 | if ( isset( $data['fa_button_exclude_URL'] ) ) { |
| 396 | $data['fa_button_exclude_url'] = $data['fa_button_exclude_URL']; |
| 397 | } |
| 398 | |
| 399 | $this->set_data( $data ); |
| 400 | |
| 401 | return $this; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Save Scroll Top Option Data. |
| 406 | * |
| 407 | * @return void |
| 408 | */ |
| 409 | public function save() { |
| 410 | $data = array(); |
| 411 | |
| 412 | $data['enabled'] = $this->enabled; |
| 413 | $data['javascript_async'] = $this->javascript_async; |
| 414 | $data['scroll_offset'] = $this->scroll_offset; |
| 415 | $data['button_width'] = $this->button_width; |
| 416 | $data['button_height'] = $this->button_height; |
| 417 | $data['button_opacity'] = $this->button_opacity; |
| 418 | $data['button_fade_duration'] = $this->button_fade_duration; |
| 419 | $data['scroll_duration'] = $this->scroll_duration; |
| 420 | $data['auto_hide'] = $this->auto_hide; |
| 421 | $data['auto_hide_after'] = $this->auto_hide_after; |
| 422 | $data['hide_small_window'] = $this->hide_small_window; |
| 423 | $data['small_window_width'] = $this->small_window_width; |
| 424 | $data['hide_wpadmin'] = $this->hide_wpadmin; |
| 425 | $data['hide_iframe'] = $this->hide_iframe; |
| 426 | $data['button_style'] = $this->button_style; |
| 427 | $data['button_action'] = $this->button_action; |
| 428 | $data['button_action_element_selector'] = $this->button_action_element_selector; |
| 429 | $data['button_action_container_selector'] = $this->button_action_container_selector; |
| 430 | $data['button_action_element_offset'] = $this->button_action_element_offset; |
| 431 | $data['button_action_page_url'] = $this->button_action_page_url; |
| 432 | |
| 433 | $data['location'] = $this->location; |
| 434 | $data['margin_x'] = $this->margin_x; |
| 435 | $data['margin_y'] = $this->margin_y; |
| 436 | |
| 437 | $data['display_pages'] = $this->display_pages; |
| 438 | $data['include_pages'] = $this->include_pages; |
| 439 | $data['exclude_pages'] = $this->exclude_pages; |
| 440 | |
| 441 | $data['image'] = $this->image; |
| 442 | $data['image_alt'] = $this->image_alt; |
| 443 | $data['image_title'] = $this->image_title; |
| 444 | $data['custom_url'] = $this->custom_url; |
| 445 | |
| 446 | $data['text_button_text'] = $this->text_button_text; |
| 447 | $data['text_button_text_color'] = $this->text_button_text_color; |
| 448 | $data['text_button_background_color'] = $this->text_button_background_color; |
| 449 | $data['text_button_hover_color'] = $this->text_button_hover_color; |
| 450 | $data['text_button_css'] = $this->text_button_css; |
| 451 | |
| 452 | $data['fa_button_class'] = $this->fa_button_class; |
| 453 | $data['fa_button_url'] = $this->fa_button_url; |
| 454 | $data['fa_button_exclude_url'] = $this->fa_button_exclude_url; |
| 455 | $data['fa_button_text_color'] = $this->fa_button_text_color; |
| 456 | $data['fa_button_css'] = $this->fa_button_css; |
| 457 | |
| 458 | $data['accessibility_aria_label'] = $this->accessibility_aria_label; |
| 459 | $data['accessibility_title'] = $this->accessibility_title; |
| 460 | $data['accessibility_screen_reader_text'] = $this->accessibility_screen_reader_text; |
| 461 | |
| 462 | $data['css_enqueue_file'] = $this->css_enqueue_file; |
| 463 | $data['css_extra_css'] = $this->css_extra_css; |
| 464 | |
| 465 | $data['last_updated'] = $this->wp->time(); |
| 466 | |
| 467 | $this->wp->update_option( 'wpfront-scroll-top-options', $data ); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Set Scroll Top Option Data. |
| 472 | * |
| 473 | * @param array<mixed,mixed> $data Scroll Top option data. |
| 474 | * @return void |
| 475 | */ |
| 476 | public function set_data( $data ) { |
| 477 | |
| 478 | if ( isset( $data['enabled'] ) ) { |
| 479 | $this->enabled = $this->validate_bool( $data['enabled'] ); |
| 480 | } |
| 481 | if ( isset( $data['javascript_async'] ) ) { |
| 482 | $this->javascript_async = $this->validate_bool( $data['javascript_async'] ); |
| 483 | } |
| 484 | if ( isset( $data['scroll_offset'] ) ) { |
| 485 | $this->scroll_offset = $this->validate_absint( $data['scroll_offset'] ); |
| 486 | } |
| 487 | if ( isset( $data['button_width'] ) ) { |
| 488 | $this->button_width = $this->validate_absint( $data['button_width'] ); |
| 489 | } |
| 490 | if ( isset( $data['button_height'] ) ) { |
| 491 | $this->button_height = $this->validate_absint( $data['button_height'] ); |
| 492 | } |
| 493 | if ( isset( $data['button_opacity'] ) ) { |
| 494 | $this->button_opacity = $this->validate_range_0_100( $data['button_opacity'] ); |
| 495 | } |
| 496 | if ( isset( $data['button_fade_duration'] ) ) { |
| 497 | $this->button_fade_duration = $this->validate_absint( $data['button_fade_duration'] ); |
| 498 | } |
| 499 | if ( isset( $data['scroll_duration'] ) ) { |
| 500 | $this->scroll_duration = $this->validate_absint( $data['scroll_duration'] ); |
| 501 | } |
| 502 | if ( isset( $data['auto_hide'] ) ) { |
| 503 | $this->auto_hide = $this->validate_bool( $data['auto_hide'] ); |
| 504 | } |
| 505 | if ( isset( $data['auto_hide_after'] ) ) { |
| 506 | $this->auto_hide_after = $this->validate_absint( $data['auto_hide_after'] ); |
| 507 | } |
| 508 | if ( isset( $data['hide_small_window'] ) ) { |
| 509 | $this->hide_small_window = $this->validate_bool( $data['hide_small_window'] ); |
| 510 | } |
| 511 | if ( isset( $data['small_window_width'] ) ) { |
| 512 | $this->small_window_width = $this->validate_absint( $data['small_window_width'] ); |
| 513 | } |
| 514 | if ( isset( $data['hide_wpadmin'] ) ) { |
| 515 | $this->hide_wpadmin = $this->validate_bool( $data['hide_wpadmin'] ); |
| 516 | } |
| 517 | if ( isset( $data['hide_iframe'] ) ) { |
| 518 | $this->hide_iframe = $this->validate_bool( $data['hide_iframe'] ); |
| 519 | } |
| 520 | if ( isset( $data['button_style'] ) ) { |
| 521 | $this->button_style = $this->validate_button_style( $data['button_style'] ); |
| 522 | } |
| 523 | if ( isset( $data['button_action'] ) ) { |
| 524 | $this->button_action = $this->validate_button_action( $data['button_action'] ); |
| 525 | } |
| 526 | if ( isset( $data['button_action_element_selector'] ) ) { |
| 527 | $this->button_action_element_selector = $this->validate_string( $data['button_action_element_selector'] ); |
| 528 | } |
| 529 | if ( isset( $data['button_action_container_selector'] ) ) { |
| 530 | $this->button_action_container_selector = $this->validate_button_action_container_selector( $data['button_action_container_selector'] ); |
| 531 | } |
| 532 | if ( isset( $data['button_action_element_offset'] ) ) { |
| 533 | $this->button_action_element_offset = $this->validate_int( $data['button_action_element_offset'] ); |
| 534 | } |
| 535 | if ( isset( $data['button_action_page_url'] ) ) { |
| 536 | $this->button_action_page_url = $this->validate_url( $data['button_action_page_url'] ); |
| 537 | } |
| 538 | if ( isset( $data['location'] ) ) { |
| 539 | $this->location = $this->validate_range_1_4( $data['location'] ); |
| 540 | } |
| 541 | if ( isset( $data['margin_x'] ) ) { |
| 542 | $this->margin_x = $this->validate_int( $data['margin_x'] ); |
| 543 | } |
| 544 | if ( isset( $data['margin_y'] ) ) { |
| 545 | $this->margin_y = $this->validate_int( $data['margin_y'] ); |
| 546 | } |
| 547 | if ( isset( $data['display_pages'] ) ) { |
| 548 | $this->display_pages = $this->validate_display_pages( $data['display_pages'] ); |
| 549 | } |
| 550 | if ( isset( $data['include_pages'] ) ) { |
| 551 | $this->include_pages = $this->validate_string( $data['include_pages'] ); |
| 552 | } |
| 553 | if ( isset( $data['exclude_pages'] ) ) { |
| 554 | $this->exclude_pages = $this->validate_string( $data['exclude_pages'] ); |
| 555 | } |
| 556 | if ( isset( $data['image'] ) ) { |
| 557 | $this->image = $this->validate_string( $data['image'] ); |
| 558 | } |
| 559 | if ( isset( $data['image_alt'] ) ) { |
| 560 | $this->image_alt = $this->validate_string( $data['image_alt'] ); |
| 561 | } |
| 562 | if ( isset( $data['image_title'] ) ) { |
| 563 | $this->image_title = $this->validate_string( $data['image_title'] ); |
| 564 | } |
| 565 | if ( isset( $data['custom_url'] ) ) { |
| 566 | $this->custom_url = $this->validate_url( $data['custom_url'] ); |
| 567 | } |
| 568 | if ( isset( $data['text_button_text'] ) ) { |
| 569 | $this->text_button_text = $this->validate_string( $data['text_button_text'] ); |
| 570 | } |
| 571 | if ( isset( $data['text_button_text_color'] ) ) { |
| 572 | $this->text_button_text_color = $this->validate_color( $data['text_button_text_color'], '#FFFFFF' ); |
| 573 | } |
| 574 | if ( isset( $data['text_button_background_color'] ) ) { |
| 575 | $this->text_button_background_color = $this->validate_color( $data['text_button_background_color'], '#000000' ); |
| 576 | } |
| 577 | if ( isset( $data['text_button_hover_color'] ) ) { |
| 578 | $this->text_button_hover_color = $this->validate_color( $data['text_button_hover_color'], '#000000' ); |
| 579 | } |
| 580 | if ( isset( $data['text_button_css'] ) ) { |
| 581 | $this->text_button_css = $this->validate_string( $data['text_button_css'] ); |
| 582 | } |
| 583 | if ( isset( $data['fa_button_class'] ) ) { |
| 584 | $this->fa_button_class = $this->validate_string( $data['fa_button_class'] ); |
| 585 | } |
| 586 | if ( isset( $data['fa_button_url'] ) ) { |
| 587 | $this->fa_button_url = $this->validate_url( $data['fa_button_url'] ); |
| 588 | } |
| 589 | if ( isset( $data['fa_button_exclude_url'] ) ) { |
| 590 | $this->fa_button_exclude_url = $this->validate_bool( $data['fa_button_exclude_url'] ); |
| 591 | } |
| 592 | if ( isset( $data['fa_button_text_color'] ) ) { |
| 593 | $this->fa_button_text_color = $this->validate_color( $data['fa_button_text_color'], '#000000' ); |
| 594 | } |
| 595 | if ( isset( $data['fa_button_css'] ) ) { |
| 596 | $this->fa_button_css = $this->validate_string( $data['fa_button_css'] ); |
| 597 | } |
| 598 | if ( isset( $data['accessibility_aria_label'] ) ) { |
| 599 | $this->accessibility_aria_label = $this->validate_string( $data['accessibility_aria_label'] ); |
| 600 | } |
| 601 | if ( isset( $data['accessibility_title'] ) ) { |
| 602 | $this->accessibility_title = $this->validate_string( $data['accessibility_title'] ); |
| 603 | } |
| 604 | if ( isset( $data['accessibility_screen_reader_text'] ) ) { |
| 605 | $this->accessibility_screen_reader_text = $this->validate_string( $data['accessibility_screen_reader_text'] ); |
| 606 | } |
| 607 | if ( isset( $data['css_enqueue_file'] ) ) { |
| 608 | $this->css_enqueue_file = $this->validate_bool( $data['css_enqueue_file'] ); |
| 609 | } |
| 610 | if ( isset( $data['css_extra_css'] ) ) { |
| 611 | $this->css_extra_css = $this->validate_string( $data['css_extra_css'] ); |
| 612 | } |
| 613 | if ( isset( $data['last_updated'] ) ) { |
| 614 | $this->last_updated = $this->validate_int( $data['last_updated'] ); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Return boolean value. |
| 620 | * |
| 621 | * @param mixed $value value. |
| 622 | * @return bool $arg |
| 623 | */ |
| 624 | protected function validate_bool( $value ) { |
| 625 | $value = is_string( $value ) ? trim( $value ) : $value; |
| 626 | return ! empty( $value ); |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Return positive integer value. |
| 631 | * |
| 632 | * @param mixed $value value. |
| 633 | * @return int<0,max> $arg |
| 634 | */ |
| 635 | protected function validate_absint( $value ) { |
| 636 | $result = $this->wp->absint( is_scalar( $value ) ? $value : 0 ); |
| 637 | |
| 638 | // Ensure we always return a non-negative value. |
| 639 | return $result >= 0 ? $result : 0; |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Return integer value in range of 0 to 100. |
| 644 | * |
| 645 | * @param mixed $value value. |
| 646 | * @return int<0,100> $arg |
| 647 | */ |
| 648 | protected function validate_range_0_100( $value ) { |
| 649 | $value = $this->validate_absint( $value ); |
| 650 | if ( $value > 100 ) { |
| 651 | $value = 100; |
| 652 | } |
| 653 | |
| 654 | return $value; |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Return integer value in range of 1 to 4. |
| 659 | * |
| 660 | * @param mixed $value value. |
| 661 | * @return int<1,4> $arg |
| 662 | */ |
| 663 | protected function validate_range_1_4( $value ) { |
| 664 | $value = $this->validate_absint( $value ); |
| 665 | if ( $value < 1 ) { |
| 666 | $value = 1; |
| 667 | } |
| 668 | if ( $value > 4 ) { |
| 669 | $value = 4; |
| 670 | } |
| 671 | return $value; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Returns Button Action. |
| 676 | * |
| 677 | * @param mixed $value value. |
| 678 | * @return 'top'|'element'|'url' $arg |
| 679 | */ |
| 680 | protected function validate_button_action( $value ) { |
| 681 | $value = $this->validate_string( $value ); |
| 682 | |
| 683 | return in_array( $value, array( 'top', 'element', 'url' ), true ) ? $value : 'top'; |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Returns String. |
| 688 | * |
| 689 | * @param mixed $value value. |
| 690 | * @return string $arg |
| 691 | */ |
| 692 | protected function validate_string( $value ) { |
| 693 | $value = is_scalar( $value ) ? (string) $value : ''; |
| 694 | $value = trim( $value ); |
| 695 | |
| 696 | return $value; |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * Returns URL. |
| 701 | * |
| 702 | * @param mixed $value value. |
| 703 | * @return string $arg |
| 704 | */ |
| 705 | protected function validate_url( $value ) { |
| 706 | $value = $this->validate_string( $value ); |
| 707 | |
| 708 | return $this->wp->esc_url_raw( $value ); |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * Returns Integer. |
| 713 | * |
| 714 | * @param mixed $value value. |
| 715 | * @return int $arg |
| 716 | */ |
| 717 | protected function validate_int( $value ) { |
| 718 | $value = is_scalar( $value ) ? (int) $value : 0; |
| 719 | |
| 720 | return $value; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Returns Button Action Container Selector. |
| 725 | * |
| 726 | * @param mixed $value value. |
| 727 | * @return string $arg |
| 728 | */ |
| 729 | protected function validate_button_action_container_selector( $value ) { |
| 730 | $value = $this->validate_string( $value ); |
| 731 | |
| 732 | if ( empty( $value ) ) { |
| 733 | return 'html, body'; |
| 734 | } |
| 735 | |
| 736 | return $value; |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * Returns Color. |
| 741 | * |
| 742 | * @param mixed $value value. |
| 743 | * @param non-empty-string $d default value. |
| 744 | * @return non-empty-string $arg |
| 745 | */ |
| 746 | protected function validate_color( $value, $d ) { |
| 747 | $value = $this->validate_string( $value ); |
| 748 | $color = $this->wp->sanitize_hex_color( $value ); |
| 749 | |
| 750 | if ( empty( $color ) ) { |
| 751 | return $d; |
| 752 | } |
| 753 | |
| 754 | return $color; |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * Returns Button Style. |
| 759 | * |
| 760 | * @param mixed $value value. |
| 761 | * @return 'image'|'text'|'font-awesome' $arg |
| 762 | */ |
| 763 | protected function validate_button_style( $value ) { |
| 764 | $style = $this->validate_string( $value ); |
| 765 | |
| 766 | return in_array( $style, array( 'image', 'text', 'font-awesome' ), true ) ? $style : 'image'; |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * Returns Display Pages. |
| 771 | * |
| 772 | * @param mixed $value value. |
| 773 | * @return int<1,3> $arg |
| 774 | */ |
| 775 | protected function validate_display_pages( $value ) { |
| 776 | $value = $this->validate_absint( $value ); |
| 777 | |
| 778 | if ( $value < 1 ) { |
| 779 | return 1; |
| 780 | } |
| 781 | |
| 782 | if ( $value > 3 ) { |
| 783 | return 3; |
| 784 | } |
| 785 | |
| 786 | return $value; |
| 787 | } |
| 788 | } |
| 789 |