Нет описания

PopupTrigger.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Class PUM_Shortcode
  7. *
  8. * This is a base class for all popup maker & extension shortcodes.
  9. */
  10. class PUM_Shortcode_PopupTrigger extends PUM_Shortcode {
  11. /**
  12. * @var int
  13. */
  14. public $version = 2;
  15. /**
  16. * @var bool
  17. */
  18. public $has_content = true;
  19. public $ajax_rendering = true;
  20. /**
  21. * The shortcode tag.
  22. */
  23. public function tag() {
  24. return 'popup_trigger';
  25. }
  26. /**
  27. * @return string
  28. */
  29. public function label() {
  30. return __( 'Popup Trigger', 'popup-maker' );
  31. }
  32. /**
  33. * @return string
  34. */
  35. public function description() {
  36. return __( 'Inserts a click-able popup trigger.', 'popup-maker' );
  37. }
  38. /**
  39. * @return array
  40. */
  41. public function inner_content_labels() {
  42. return array(
  43. 'label' => __( 'Trigger Content', 'popup-maker' ),
  44. 'description' => __( 'Can contain other shortcodes, images, text or html content.' ),
  45. );
  46. }
  47. /**
  48. * @return array
  49. */
  50. public function post_types() {
  51. return array( 'post', 'page', 'popup' );
  52. }
  53. /**
  54. * @return array
  55. */
  56. public function fields() {
  57. $select_args = array();
  58. if ( isset( $_GET['post'] ) && is_int( (int) $_GET['post'] ) && isset( $_GET['action'] ) && $_GET['action'] == 'edit' ) {
  59. $select_args['post__not_in'] = wp_parse_id_list( array( get_the_ID(), $_GET['post'] ) );
  60. }
  61. return array(
  62. 'general' => array(
  63. 'main' => array(
  64. 'id' => array(
  65. 'label' => __( 'Targeted Popup', 'popup-maker' ),
  66. 'placeholder' => __( 'Choose a Popup', 'popup-maker' ),
  67. 'desc' => __( 'Choose which popup will be targeted by this trigger.', 'popup-maker' ),
  68. 'type' => 'select',
  69. 'post_type' => 'popup',
  70. 'priority' => 5,
  71. 'required' => true,
  72. 'options' => PUM_Helpers::popup_selectlist( $select_args ) + array(
  73. 'custom' => __( 'Custom', 'popup-maker' ),
  74. ),
  75. 'std' => 0,
  76. ),
  77. 'custom_id' => array(
  78. 'label' => __( 'Custom Popup ID', 'popup-maker' ),
  79. 'type' => 'text',
  80. 'dependencies' => array(
  81. 'id' => 'custom',
  82. ),
  83. 'std' => '',
  84. ),
  85. ),
  86. ),
  87. 'options' => array(
  88. 'main' => array(
  89. 'tag' => array(
  90. 'label' => __( 'HTML Tag', 'popup-maker' ),
  91. 'placeholder' => __( 'HTML Tags: button, span etc.', 'popup-maker' ),
  92. 'desc' => __( 'The HTML tag used to generate the trigger and wrap your text.', 'popup-maker' ),
  93. 'type' => 'text',
  94. 'std' => '',
  95. 'priority' => 10,
  96. 'required' => true,
  97. ),
  98. 'classes' => array(
  99. 'label' => __( 'CSS Class', 'popup-maker' ),
  100. 'placeholder' => __( 'CSS Class', 'popup-maker' ),
  101. 'type' => 'text',
  102. 'desc' => __( 'Add additional classes for styling.', 'popup-maker' ),
  103. 'priority' => 15,
  104. 'std' => '',
  105. ),
  106. 'class' => array(
  107. 'type' => 'hidden',
  108. ),
  109. 'do_default' => array(
  110. 'type' => 'checkbox',
  111. 'label' => __( 'Do not prevent the default click functionality.', 'popup-maker' ),
  112. 'desc' => __( 'This prevents us from disabling the browsers default action when a trigger is clicked. It can be used to allow a link to a file to both trigger a popup and still download the file.', 'popup-maker' ),
  113. 'priority' => 20,
  114. 'std' => false,
  115. ),
  116. ),
  117. ),
  118. );
  119. }
  120. /**
  121. * Shortcode handler
  122. *
  123. * @param array $atts shortcode attributes
  124. * @param string $content shortcode content
  125. *
  126. * @return string
  127. */
  128. public function handler( $atts, $content = null ) {
  129. $atts = $this->shortcode_atts( $atts );
  130. $return = '<' . $atts['tag'] . ' class="pum-trigger popmake-' . $atts['id'] . ' ' . $atts['classes'] . '" data-do-default="' . esc_attr( $atts['do_default'] ) . '">';
  131. $return .= PUM_Helpers::do_shortcode( $content );
  132. $return .= '</' . $atts['tag'] . '>';
  133. PUM_Site_Popups::preload_popup_by_id_if_enabled( $atts['id'] );
  134. return $return;
  135. }
  136. /**
  137. * Process shortcode attributes.
  138. *
  139. * Also remaps and cleans old ones.
  140. *
  141. * @param $atts
  142. *
  143. * @return array
  144. */
  145. public function shortcode_atts( $atts ) {
  146. $atts = parent::shortcode_atts( $atts );
  147. if ( empty( $atts['tag'] ) ) {
  148. $atts['tag'] = 'span';
  149. }
  150. if ( $atts['id'] == 'custom' ) {
  151. $atts['id'] = $atts['custom_id'];
  152. }
  153. if ( ! empty( $atts['class'] ) ) {
  154. $atts['classes'] .= ' ' . $atts['class'];
  155. unset( $atts['class'] );
  156. }
  157. return $atts;
  158. }
  159. /**
  160. *
  161. */
  162. public function template() { ?>
  163. <{{{attrs.tag}}} class="pum-trigger popmake-{{{attrs.id}}} {{{attrs.classes}}}">{{{attrs._inner_content}}}</{{{attrs.tag}}}><?php
  164. }
  165. }