Açıklama Yok

Triggers.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /*********************************************
  3. * Copyright (c) 2020, Code Atlantic LLC
  4. ********************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. // Exit if accessed directly.
  7. exit;
  8. }
  9. /**
  10. * Class PUM_Triggers
  11. */
  12. class PUM_Triggers {
  13. /**
  14. * @var PUM_Triggers
  15. */
  16. public static $instance;
  17. /**
  18. * @var bool
  19. */
  20. public $preload_posts = false;
  21. /**
  22. * @var array
  23. */
  24. public $triggers;
  25. /**
  26. * Initializes the triggers.
  27. */
  28. public static function init() {
  29. self::instance();
  30. }
  31. /**
  32. * Creates the triggers instance.
  33. *
  34. * @return PUM_Triggers
  35. */
  36. public static function instance() {
  37. if ( ! isset( self::$instance ) ) {
  38. self::$instance = new self();
  39. self::$instance->preload_posts = pum_is_popup_editor();
  40. }
  41. return self::$instance;
  42. }
  43. /**
  44. * Adds an array of triggers to the initialized triggers.
  45. *
  46. * @param array $triggers The array of triggers to add.
  47. * @uses PUM_Triggers::add_trigger()
  48. */
  49. public function add_triggers( $triggers = array() ) {
  50. foreach ( $triggers as $key => $trigger ) {
  51. if ( empty( $trigger['id'] ) && ! is_numeric( $key ) ) {
  52. $trigger['id'] = $key;
  53. }
  54. $this->add_trigger( $trigger );
  55. }
  56. }
  57. /**
  58. * Initializes a single trigger
  59. *
  60. * @param array $trigger The trigger array.
  61. */
  62. public function add_trigger( $trigger = array() ) {
  63. if ( ! empty( $trigger['id'] ) && ! isset( $this->triggers[ $trigger['id'] ] ) ) {
  64. $trigger = wp_parse_args(
  65. $trigger,
  66. array(
  67. 'id' => '',
  68. 'name' => '',
  69. 'modal_title' => '',
  70. 'settings_column' => '',
  71. 'priority' => 10,
  72. 'tabs' => $this->get_tabs(),
  73. 'fields' => array(),
  74. )
  75. );
  76. if ( empty( $trigger['modal_title'] ) && ! empty( $trigger['name'] ) ) {
  77. $trigger['modal_title'] = sprintf( _x( '%s Trigger Settings', 'trigger settings modal title', 'popup-maker' ), $trigger['name'] );
  78. }
  79. // Here for backward compatibility to merge in labels properly.
  80. $labels = $this->get_labels();
  81. $trigger_labels = isset( $labels[ $trigger['id'] ] ) ? $labels[ $trigger['id'] ] : array();
  82. if ( ! empty( $trigger_labels ) ) {
  83. foreach ( $trigger_labels as $key => $value ) {
  84. if ( empty( $trigger[ $key ] ) ) {
  85. $trigger[ $key ] = $value;
  86. }
  87. }
  88. }
  89. // Remove cookie fields.
  90. if ( ! empty( $trigger['fields']['cookie'] ) ) {
  91. unset( $trigger['fields']['cookie'] );
  92. }
  93. // Add cookie fields for all triggers automatically.
  94. if ( empty( $trigger['fields']['general']['cookie_name'] ) ) {
  95. $trigger['fields']['general'] = array_merge( $trigger['fields']['general'], $this->cookie_fields() );
  96. }
  97. $this->triggers[ $trigger['id'] ] = apply_filters( 'pum_trigger', $trigger );
  98. }
  99. }
  100. /**
  101. * Retrieves all initialized triggers.
  102. *
  103. * @return array The triggers
  104. */
  105. public function get_triggers() {
  106. if ( ! isset( $this->triggers ) ) {
  107. $this->register_triggers();
  108. }
  109. return $this->triggers;
  110. }
  111. /**
  112. * Retrieves a single trigger by the trigger key
  113. *
  114. * @param string|null $trigger The key for the trigger.
  115. * @return mixed|null The trigger array or null if none found
  116. */
  117. public function get_trigger( $trigger = null ) {
  118. $triggers = $this->get_triggers();
  119. return isset( $triggers[ $trigger ] ) ? $triggers[ $trigger ] : null;
  120. }
  121. /**
  122. * @param null $trigger
  123. * @param array $settings
  124. *
  125. * @return array
  126. * @deprecated
  127. *
  128. */
  129. public function validate_trigger( $trigger = null, $settings = array() ) {
  130. return $settings;
  131. }
  132. /**
  133. * Registers all known triggers when called.
  134. *
  135. * @uses PUM_Triggers::add_triggers()
  136. */
  137. public function register_triggers() {
  138. $triggers = apply_filters(
  139. 'pum_registered_triggers',
  140. array(
  141. 'click_open' => array(
  142. 'name' => __( 'Click Open', 'popup-maker' ),
  143. 'modal_title' => __( 'Click Trigger Settings', 'popup-maker' ),
  144. 'settings_column' => sprintf( '<strong>%1$s</strong>: %2$s', __( 'Extra Selectors', 'popup-maker' ), '{{data.extra_selectors}}' ),
  145. 'fields' => array(
  146. 'general' => array(
  147. 'click_info' => array(
  148. 'type' => 'html',
  149. 'content' => '<p>' . __( 'Adding the class "popmake-<span id="pum-default-click-trigger-class">{popup-ID}</span>" to an element will trigger it to be opened once clicked. Additionally you can add additional CSS selectors below.', 'popup-maker' ) . '</p>',
  150. ),
  151. 'extra_selectors' => array(
  152. 'label' => __( 'Extra CSS Selectors', 'popup-maker' ),
  153. 'desc' => __( 'For more than one selector, separate by comma (,)', 'popup-maker' ) . '<br /><strong>eg: </strong>' . __( ' .class-here, .class-2-here, #button_id', 'popup-maker' ),
  154. 'placeholder' => __( '.class-here', 'popup-maker' ),
  155. 'doclink' => 'https://docs.wppopupmaker.com/article/147-getting-css-selectors?utm_campaign=contextual-help&utm_medium=inline-doclink&utm_source=plugin-popup-editor&utm_content=extra-selectors',
  156. ),
  157. ),
  158. 'advanced' => array(
  159. 'do_default' => array(
  160. 'type' => 'checkbox',
  161. 'label' => __( 'Do not prevent the default click functionality.', 'popup-maker' ),
  162. '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' ),
  163. ),
  164. ),
  165. ),
  166. ),
  167. 'auto_open' => array(
  168. 'name' => __( 'Time Delay / Auto Open', 'popup-maker' ),
  169. 'modal_title' => __( 'Time Delay Settings', 'popup-maker' ),
  170. 'settings_column' => sprintf( '<strong>%1$s</strong>: %2$s', __( 'Delay', 'popup-maker' ), '{{data.delay}}' ),
  171. 'fields' => array(
  172. 'general' => array(
  173. 'delay' => array(
  174. 'type' => 'rangeslider',
  175. 'label' => __( 'Delay', 'popup-maker' ),
  176. 'desc' => __( 'The delay before the popup will open in milliseconds.', 'popup-maker' ),
  177. 'std' => 500,
  178. 'min' => 0,
  179. 'max' => 10000,
  180. 'step' => 500,
  181. 'unit' => 'ms',
  182. ),
  183. ),
  184. ),
  185. ),
  186. 'form_submission' => array(
  187. 'name' => __( 'Form Submission', 'popup-maker' ),
  188. //'settings_column' => sprintf( '<strong>%1$s</strong>: %2$s', __( 'Form', 'popup-maker' ), '' ),
  189. 'fields' => array(
  190. 'general' => array(
  191. 'form' => array(
  192. 'type' => 'select',
  193. 'label' => __( 'Form', 'popup-maker' ),
  194. 'options' => $this->preload_posts ? array_merge(
  195. array(
  196. 'any' => __( 'Any Supported Form*', 'popup-maker' ),
  197. __( 'Popup Maker', 'popup-maker' ) => array(
  198. 'pumsubform' => __( 'Subscription Form', 'popup-maker' ),
  199. ),
  200. ),
  201. PUM_Integrations::get_integrated_forms_selectlist()
  202. ) : array(),
  203. 'std' => 'any',
  204. ),
  205. 'delay' => array(
  206. 'type' => 'rangeslider',
  207. 'label' => __( 'Delay', 'popup-maker' ),
  208. 'desc' => __( 'The delay before the popup will open in milliseconds.', 'popup-maker' ),
  209. 'std' => 0,
  210. 'min' => 0,
  211. 'max' => 10000,
  212. 'step' => 500,
  213. 'unit' => 'ms',
  214. ),
  215. ),
  216. ),
  217. ),
  218. )
  219. );
  220. foreach ( $triggers as $key => $trigger ) {
  221. $triggers[ $key ]['fields'] = PUM_Admin_Helpers::parse_tab_fields(
  222. $triggers[ $key ]['fields'],
  223. array(
  224. 'has_subtabs' => false,
  225. 'name' => '%s',
  226. )
  227. );
  228. }
  229. // @deprecated filter.
  230. $old_triggers = apply_filters( 'pum_get_triggers', array() );
  231. foreach ( $old_triggers as $type => $trigger ) {
  232. if ( isset( $triggers[ $type ] ) ) {
  233. continue;
  234. }
  235. if ( ! empty( $trigger['fields'] ) ) {
  236. foreach ( $trigger['fields'] as $tab_id => $tab_fields ) {
  237. foreach ( $tab_fields as $field_id => $field ) {
  238. if ( ! empty( $field['options'] ) ) {
  239. $trigger['fields'][ $tab_id ][ $field_id ]['options'] = array_flip( $trigger['fields'][ $tab_id ][ $field_id ]['options'] );
  240. }
  241. }
  242. }
  243. }
  244. $triggers[ $type ] = $trigger;
  245. }
  246. $this->add_triggers( $triggers );
  247. }
  248. /**
  249. * Prepares an array to create a dropdown list of triggers
  250. *
  251. * @return array An array of triggers with ID as key and name as value
  252. */
  253. public function dropdown_list() {
  254. $_triggers = $this->get_triggers();
  255. $triggers = array();
  256. foreach ( $_triggers as $id => $trigger ) {
  257. $triggers[ $id ] = $trigger['name'];
  258. }
  259. return $triggers;
  260. }
  261. /**
  262. * Returns the cookie fields used for trigger options.
  263. *
  264. * @return array
  265. * @uses filter pum_trigger_cookie_fields
  266. */
  267. public function cookie_fields() {
  268. /**
  269. * Filter the array of default trigger cookie fields.
  270. *
  271. * @param array $fields The list of trigger cookie fields.
  272. */
  273. return apply_filters(
  274. 'pum_trigger_cookie_fields',
  275. array(
  276. 'cookie_name' => $this->cookie_field(),
  277. )
  278. );
  279. }
  280. /**
  281. * Returns the cookie field used for trigger options.
  282. *
  283. * @return array
  284. * @uses filter pum_trigger_cookie_field
  285. */
  286. public function cookie_field() {
  287. /**
  288. * Filter the array of default trigger cookie field.
  289. *
  290. * @param array $fields The list of trigger cookie field.
  291. */
  292. return apply_filters(
  293. 'pum_trigger_cookie_field',
  294. array(
  295. 'label' => __( 'Cookie Name', 'popup-maker' ),
  296. 'desc' => __( 'Choose which cookies will disable this trigger?', 'popup-maker' ),
  297. 'type' => 'select',
  298. 'multiple' => true,
  299. 'as_array' => true,
  300. 'select2' => true,
  301. 'priority' => 99,
  302. 'options' => array(
  303. 'add_new' => __( 'Add New Cookie', 'popup-maker' ),
  304. ),
  305. )
  306. );
  307. }
  308. /**
  309. * Returns an array of section labels for all triggers.
  310. *
  311. * Use the filter pum_get_trigger_section_labels to add or modify labels.
  312. *
  313. * @return array
  314. */
  315. public function get_tabs() {
  316. /**
  317. * Filter the array of trigger section labels.
  318. *
  319. * @param array $to_do The list of trigger section labels.
  320. */
  321. return apply_filters(
  322. 'pum_get_trigger_tabs',
  323. array(
  324. 'general' => __( 'General', 'popup-maker' ),
  325. 'cookie' => __( 'Cookie', 'popup-maker' ),
  326. 'advanced' => __( 'Advanced', 'popup-maker' ),
  327. )
  328. );
  329. }
  330. /**
  331. * Returns an array of trigger labels.
  332. *
  333. * Use the filter pum_get_trigger_labels to add or modify labels.
  334. *
  335. * @return array
  336. */
  337. public function get_labels() {
  338. static $labels;
  339. if ( ! isset( $labels ) ) {
  340. /**
  341. * Filter the array of trigger labels.
  342. *
  343. * @param array $to_do The list of trigger labels.
  344. */
  345. $labels = apply_filters( 'pum_get_trigger_labels', array() );
  346. }
  347. return $labels;
  348. }
  349. }