Brak opisu

PopupCookie.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Class PUM_Shortcode_PopupCookie
  7. *
  8. * Registers the popup_cookie shortcode.
  9. */
  10. class PUM_Shortcode_PopupCookie extends PUM_Shortcode {
  11. public $version = 2;
  12. public $has_content = false;
  13. /**
  14. * The shortcode tag.
  15. */
  16. public function tag() {
  17. return 'popup_cookie';
  18. }
  19. public function label() {
  20. return __( 'Popup Cookie', 'popup-maker' );
  21. }
  22. public function description() {
  23. return __( 'Insert this to manually set cookies when users view the content containing the code.', 'popup-maker' );
  24. }
  25. public function post_types() {
  26. return array( '*' );
  27. }
  28. public function fields() {
  29. return [
  30. 'general' => [
  31. 'main' => [
  32. 'name' => [
  33. 'label' => __( 'Cookie Name', 'popup-maker' ),
  34. 'placeholder' => __( 'Cookie Name ex. popmaker-123', 'popup-maker' ),
  35. 'desc' => __( 'The name that will be used when checking for or saving this cookie.', 'popup-maker' ),
  36. 'std' => '',
  37. ],
  38. 'expires' => [
  39. 'label' => __( 'Cookie Time', 'popup-maker' ),
  40. 'placeholder' => __( '364 days 23 hours 59 minutes 59 seconds', 'popup-maker' ),
  41. 'desc' => __( 'Enter a plain english time before cookie expires.', 'popup-maker' ),
  42. 'std' => '1 month',
  43. ],
  44. 'sitewide' => [
  45. 'label' => __( 'Sitewide Cookie', 'popup-maker' ),
  46. 'desc' => __( 'This will prevent the popup from triggering on all pages until the cookie expires.', 'popup-maker' ),
  47. 'type' => 'checkbox',
  48. 'std' => true,
  49. ],
  50. 'only_onscreen' => [
  51. 'label' => __( 'Only when visible on-screen', 'popup-maker' ),
  52. 'desc' => __( 'This will prevent the cookie from getting set until the user scrolls it into viewport.', 'popup-maker' ),
  53. 'type' => 'checkbox',
  54. 'std' => false,
  55. ],
  56. ],
  57. ],
  58. ];
  59. }
  60. /**
  61. * Shortcode handler
  62. *
  63. * @param array $atts shortcode attributes
  64. * @param string $content shortcode content
  65. *
  66. * @return string
  67. */
  68. public function handler( $atts, $content = null ) {
  69. $atts = $this->shortcode_atts( $atts );
  70. $args = [
  71. 'name' => $atts['name'],
  72. 'time' => $atts['expires'],
  73. 'path' => $atts['sitewide'],
  74. ];
  75. // This shortcode requires our scripts, but can be used on pages where no popups exist.
  76. wp_enqueue_script( 'popup-maker-site' );
  77. $onscreen = 'data-only-onscreen="' . ( $atts['only_onscreen'] ? 1 : 0 ) . '"';
  78. return "<div class='pum-cookie' data-cookie-args='" . json_encode( $args ) . "' $onscreen></div>";
  79. }
  80. public function template() { ?>
  81. <div class="pum-cookie"><?php echo __( 'Popup Cookie', 'popup-maker' ); ?></div><?php
  82. }
  83. }