Açıklama Yok

Popup.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Class PUM_Shortcode_Popup
  7. *
  8. * Registers the popup_close shortcode.
  9. */
  10. class PUM_Shortcode_Popup extends PUM_Shortcode {
  11. public $version = 2;
  12. public $has_content = true;
  13. public $inner_content_priority = 15;
  14. /**
  15. * The shortcode tag.
  16. */
  17. public function tag() {
  18. return 'popup';
  19. }
  20. public function label() {
  21. return __( 'Popup', 'popup-maker' );
  22. }
  23. public function description() {
  24. return __( 'Insert a popup inline rather. Great for simple popups used for supporting content.', 'popup-maker' );
  25. }
  26. public function inner_content_labels() {
  27. return array(
  28. 'label' => __( 'Content', 'popup-maker' ),
  29. 'description' => __( 'Can contain other shortcodes, images, text or html content.', 'popup-maker' ),
  30. );
  31. }
  32. public function post_types() {
  33. return array();
  34. }
  35. /**
  36. * @return array
  37. */
  38. public function tabs() {
  39. return array(
  40. 'general' => __( 'General', 'popup-maker' ),
  41. 'display' => __( 'Display', 'popup-maker' ),
  42. 'position' => __( 'Position', 'popup-maker' ),
  43. 'animation' => __( 'Animation', 'popup-maker' ),
  44. 'close' => __( 'Close', 'popup-maker' ),
  45. );
  46. }
  47. /**
  48. * @return array
  49. */
  50. public function subtabs() {
  51. return apply_filters( 'pum_sub_form_shortcode_subtabs', array(
  52. 'general' => array(
  53. 'main' => __( 'General', 'popup-maker' ),
  54. ),
  55. 'display' => array(
  56. 'main' => __( 'Display', 'popup-maker' ),
  57. ),
  58. 'position' => array(
  59. 'main' => __( 'Position', 'popup-maker' ),
  60. ),
  61. 'animation' => array(
  62. 'main' => __( 'Animation', 'popup-maker' ),
  63. ),
  64. 'close' => array(
  65. 'main' => __( 'Close', 'popup-maker' ),
  66. ),
  67. ) );
  68. }
  69. public function fields() {
  70. return array(
  71. 'general' => array(
  72. 'main' => array(
  73. 'id' => array(
  74. 'label' => __( 'Unique Popup ID', 'popup-maker' ),
  75. 'placeholder' => __( '`offer`, `more-info`', 'popup-maker' ),
  76. 'desc' => __( 'Used in popup triggers to target this popup', 'popup-maker' ),
  77. 'priority' => 5,
  78. 'required' => true,
  79. ),
  80. 'title' => array(
  81. 'label' => __( 'Popup Title', 'popup-maker' ),
  82. 'placeholder' => __( 'Enter popup title text,', 'popup-maker' ),
  83. 'desc' => __( 'This will be displayed above the content. Leave it empty to disable it.', 'popup-maker' ),
  84. 'priority' => 10,
  85. ),
  86. ),
  87. ),
  88. 'display' => array(
  89. 'main' => array(
  90. 'theme_id' => array(
  91. 'type' => 'select',
  92. 'label' => __( 'Popup Theme', 'popup-maker' ),
  93. 'placeholder' => __( 'Choose a theme,', 'popup-maker' ),
  94. 'desc' => __( 'Choose which popup theme will be used.', 'popup-maker' ),
  95. 'std' => pum_get_default_theme_id(),
  96. 'select2' => true,
  97. 'options' => pum_is_settings_page() ? PUM_Helpers::popup_theme_selectlist() : null,
  98. 'required' => true,
  99. 'priority' => 5,
  100. ),
  101. 'overlay_disabled' => array(
  102. 'label' => __( 'Disable Overlay', 'popup-maker' ),
  103. 'description' => __( 'Checking this will disable and hide the overlay for this popup.', 'popup-maker' ),
  104. 'type' => 'checkbox',
  105. 'std' => false,
  106. 'priority' => 10,
  107. ),
  108. 'size' => array(
  109. 'label' => __( 'Size', 'popup-maker' ),
  110. 'description' => __( 'Select the size of the popup.', 'popup-maker' ),
  111. 'type' => 'select',
  112. 'std' => 'small',
  113. 'options' => array_flip( apply_filters( 'popmake_popup_display_size_options', array() ) ),
  114. 'priority' => 15,
  115. ),
  116. 'width' => array(
  117. 'label' => __( 'Width', 'popup-maker' ),
  118. 'priority' => 20,
  119. ),
  120. 'width_unit' => array(
  121. 'label' => __( 'Width Unit', 'popup-maker' ),
  122. 'type' => 'select',
  123. 'std' => 'px',
  124. 'options' => array_flip( apply_filters( 'popmake_size_unit_options', array() ) ),
  125. 'priority' => 25,
  126. ),
  127. 'height' => array(
  128. 'label' => __( 'Height', 'popup-maker' ),
  129. 'priority' => 30,
  130. ),
  131. 'height_unit' => array(
  132. 'label' => __( 'Height Unit', 'popup-maker' ),
  133. 'type' => 'select',
  134. 'std' => 'px',
  135. 'options' => array_flip( apply_filters( 'popmake_size_unit_options', array() ) ),
  136. 'priority' => 35,
  137. ),
  138. ),
  139. ),
  140. 'position' => array(
  141. 'main' => array(
  142. 'location' => array(
  143. 'label' => __( 'Location', 'popup-maker' ),
  144. 'description' => __( 'Choose where the popup will be displayed.', 'popup-maker' ),
  145. 'type' => 'select',
  146. 'std' => 'center top',
  147. 'priority' => 4,
  148. 'options' => array_flip( apply_filters( 'popmake_popup_display_location_options', array() ) ),
  149. ),
  150. 'position_top' => array(
  151. 'label' => __( 'Top', 'popup-maker' ),
  152. 'description' => sprintf( _x( 'Distance from the %s edge of the screen.', 'Screen Edge: top, bottom', 'popup-maker' ), strtolower( __( 'Top', 'popup-maker' ) ) ),
  153. 'type' => 'rangeslider',
  154. 'std' => 100,
  155. 'priority' => 10,
  156. 'step' => 1,
  157. 'min' => 0,
  158. 'max' => 500,
  159. 'unit' => 'px',
  160. ),
  161. 'position_bottom' => array(
  162. 'label' => __( 'Bottom', 'popup-maker' ),
  163. 'description' => sprintf( _x( 'Distance from the %s edge of the screen.', 'Screen Edge: top, bottom', 'popup-maker' ), strtolower( __( 'Bottom', 'popup-maker' ) ) ),
  164. 'type' => 'rangeslider',
  165. 'std' => 0,
  166. 'priority' => 10,
  167. 'step' => 1,
  168. 'min' => 0,
  169. 'max' => 500,
  170. 'unit' => 'px',
  171. ),
  172. 'position_left' => array(
  173. 'label' => __( 'Left', 'popup-maker' ),
  174. 'description' => sprintf( _x( 'Distance from the %s edge of the screen.', 'Screen Edge: top, bottom', 'popup-maker' ), strtolower( __( 'Left', 'popup-maker' ) ) ),
  175. 'type' => 'rangeslider',
  176. 'std' => 0,
  177. 'priority' => 10,
  178. 'step' => 1,
  179. 'min' => 0,
  180. 'max' => 500,
  181. 'unit' => 'px',
  182. ),
  183. 'position_right' => array(
  184. 'label' => __( 'Right', 'popup-maker' ),
  185. 'description' => sprintf( _x( 'Distance from the %s edge of the screen.', 'Screen Edge: top, bottom', 'popup-maker' ), strtolower( __( 'Right', 'popup-maker' ) ) ),
  186. 'type' => 'rangeslider',
  187. 'std' => 0,
  188. 'priority' => 10,
  189. 'step' => 1,
  190. 'min' => 0,
  191. 'max' => 500,
  192. 'unit' => 'px',
  193. ),
  194. ),
  195. ),
  196. 'animation' => array(
  197. 'main' => array(
  198. 'animation_type' => array(
  199. 'label' => __( 'Animation Type', 'popup-maker' ),
  200. 'description' => __( 'Select an animation type for your popup.', 'popup-maker' ),
  201. 'type' => 'select',
  202. 'std' => 'fade',
  203. 'priority' => 5,
  204. 'options' => array_flip( apply_filters( 'popmake_popup_display_animation_type_options', array() ) ),
  205. ),
  206. 'animation_speed' => array(
  207. 'label' => __( 'Animation Speed', 'popup-maker' ),
  208. 'description' => __( 'Set the animation speed for the popup.', 'popup-maker' ),
  209. 'type' => 'rangeslider',
  210. 'std' => 350,
  211. 'priority' => 10,
  212. 'step' => 10,
  213. 'min' => 50,
  214. 'max' => 1000,
  215. 'unit' => __( 'ms', 'popup-maker' ),
  216. ),
  217. 'animation_origin' => array(
  218. 'label' => __( 'Animation Origin', 'popup-maker' ),
  219. 'description' => __( 'Choose where the animation will begin.', 'popup-maker' ),
  220. 'type' => 'select',
  221. 'std' => 'center top',
  222. 'priority' => 15,
  223. 'options' => array_flip( apply_filters( 'popmake_popup_display_animation_origin_options', array() ) ),
  224. ),
  225. ),
  226. ),
  227. 'close' => array(
  228. 'main' => array(
  229. 'overlay_click' => array(
  230. 'label' => __( 'Click Overlay to Close', 'popup-maker' ),
  231. 'description' => __( 'Checking this will cause popup to close when user clicks on overlay.', 'popup-maker' ),
  232. 'type' => 'checkbox',
  233. 'std' => false,
  234. 'priority' => 5,
  235. ),
  236. ),
  237. ),
  238. );
  239. }
  240. /**
  241. * Shortcode handler
  242. *
  243. * @param array $atts shortcode attributes
  244. * @param string $content shortcode content
  245. *
  246. * @return string
  247. */
  248. public function handler( $atts, $content = null ) {
  249. global $popup;
  250. $atts = shortcode_atts( apply_filters( 'pum_popup_shortcode_default_atts', array(
  251. 'id' => "",
  252. 'title' => "",
  253. 'theme_id' => null,
  254. 'theme' => null,
  255. 'overlay_disabled' => 0,
  256. 'size' => "small",
  257. 'width' => "",
  258. 'width_unit' => "px",
  259. 'height' => "",
  260. 'height_unit' => "px",
  261. 'location' => "center top",
  262. 'position_top' => 100,
  263. 'position_left' => 0,
  264. 'position_bottom' => 0,
  265. 'position_right' => 0,
  266. 'position_fixed' => 0,
  267. 'animation_type' => "fade",
  268. 'animation_speed' => 1000,
  269. 'animation_origin' => 'top',
  270. 'overlay_click' => 0,
  271. 'esc_press' => 1,
  272. ) ), $atts, 'popup' );
  273. // We need to fake a popup using the PUM_Popup data model.
  274. $post_id = rand( - 99999, - 1 ); // negative ID, to avoid clash with a valid post
  275. $post = new stdClass();
  276. $post->ID = $post_id;
  277. $post->post_author = 1;
  278. $post->post_date = current_time( 'mysql' );
  279. $post->post_date_gmt = current_time( 'mysql', 1 );
  280. $post->post_title = $atts['title'];
  281. $post->post_content = $content;
  282. $post->post_status = 'publish';
  283. $post->comment_status = 'closed';
  284. $post->ping_status = 'closed';
  285. $post->post_name = $atts['id']; // append random number to avoid clash
  286. $post->post_type = 'popup';
  287. $post->filter = 'raw'; // important!
  288. $post->data_version = 3;
  289. $post->mock = true;
  290. // Convert to WP_Post object
  291. $wp_post = new WP_Post( $post );
  292. // Add the fake post to the cache
  293. wp_cache_add( $post_id, $wp_post, 'posts' );
  294. $popup = new PUM_Model_Popup( $wp_post );
  295. // Get Theme ID
  296. if ( ! $atts['theme_id'] ) {
  297. $atts['theme_id'] = $atts['theme'] ? $atts['theme'] : pum_get_default_theme_id();
  298. }
  299. $popup->title = $atts['title'];
  300. $popup->settings = array_merge( PUM_Admin_Popups::defaults(), array(
  301. 'disable_analytics' => true,
  302. 'theme_id' => $atts['theme_id'],
  303. 'size' => $atts['size'],
  304. 'overlay_disabled' => $atts['overlay_disabled'],
  305. 'custom_width' => $atts['width'],
  306. 'custom_width_unit' => $atts['width_unit'],
  307. 'custom_height' => $atts['height'],
  308. 'custom_height_unit' => $atts['height_unit'],
  309. 'custom_height_auto' => $atts['width'] > 0 ? 0 : 1,
  310. 'location' => $atts['location'],
  311. 'position_top' => $atts['position_top'],
  312. 'position_left' => $atts['position_left'],
  313. 'position_bottom' => $atts['position_bottom'],
  314. 'position_right' => $atts['position_right'],
  315. 'position_fixed' => $atts['position_fixed'],
  316. 'animation_type' => $atts['animation_type'],
  317. 'animation_speed' => $atts['animation_speed'],
  318. 'animation_origin' => $atts['animation_origin'],
  319. 'close_on_overlay_click' => $atts['overlay_click'],
  320. 'close_on_esc_press' => $atts['esc_press'],
  321. 'triggers' => array(
  322. array(
  323. 'type' => 'click_open',
  324. 'settings' => array(
  325. 'extra_selectors' => '#popmake-' . $atts['id'],
  326. ),
  327. ),
  328. ),
  329. ) );
  330. $current_global_popup = pum()->current_popup;
  331. pum()->current_popup = $popup;
  332. $return = pum_get_template_part( 'popup' );
  333. // Small hack to move popup to body.
  334. $return .= "<script type='text/javascript' id='pum-move-popup-" . $post_id . "'>jQuery(document).ready(function () {
  335. jQuery('#pum-" . $post_id . "').appendTo('body');
  336. window.pum_vars.popups[ 'pum-" . $popup->ID . "' ] = " . PUM_Utils_Array::safe_json_encode( $popup->get_public_settings() ) . ";
  337. window.pum_popups[ 'pum-" . $popup->ID . "' ] = " . PUM_Utils_Array::safe_json_encode( $popup->get_public_settings() ) . ";
  338. jQuery('#pum-move-popup-" . $post_id . "').remove();
  339. });</script>";
  340. pum()->current_popup = $current_global_popup;
  341. return $return;
  342. }
  343. public function template() { ?>
  344. <p class="pum-sub-form-desc">
  345. <?php _e( 'Popup', 'popup-maker' ); ?>: ID "{{attrs.id}}" </p>
  346. <?php
  347. }
  348. }