暂无描述

Popup.php 29KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Model_Popup
  10. *
  11. * @since 1.4
  12. */
  13. class PUM_Model_Popup extends PUM_Abstract_Model_Post {
  14. /** @var string */
  15. protected $required_post_type = 'popup';
  16. /** @var array */
  17. public $conditions_filtered = array();
  18. /**
  19. * @var string
  20. *
  21. * @deprecated 1.8.0 Was used in PUM ALM extension, needs time to get those changes published.
  22. * @toberemoved v1.9.0
  23. */
  24. public $content;
  25. /** @var bool */
  26. public $doing_passive_migration = false;
  27. /**
  28. * The current model version.
  29. *
  30. * 1 - v1.0.0
  31. * 2 - v1.4.0
  32. * 3 - v1.7.0
  33. *
  34. * @var int
  35. */
  36. public $model_version = 3;
  37. /**
  38. * The version of the data currently stored for the current item.
  39. *
  40. * 1 - v1.0.0
  41. * 2 - v1.4.0
  42. * 3 - v1.7.0
  43. *
  44. * @var int
  45. */
  46. public $data_version;
  47. # TODO Remove these once no longer needed.
  48. /**
  49. * @var array
  50. * @deprecated 1.7.0
  51. */
  52. public $display;
  53. /**
  54. * @var array
  55. * @deprecated 1.7.0
  56. */
  57. public $close;
  58. /**
  59. * Used to hackishly insert settings for generated popups not stored in DB. (Shortcodes).
  60. *
  61. * @var array
  62. * @since 1.8.0
  63. */
  64. public $settings = null;
  65. /**
  66. * Used to hackishly insert title for generated popups not stored in DB. (Shortcodes).
  67. *
  68. * @var string
  69. * @since 1.8.0
  70. */
  71. public $title = null;
  72. /**
  73. * Used to hackishly change the model to prevent queries. (Shortcodes).
  74. *
  75. * @var string
  76. * @since 1.8.0
  77. */
  78. public $mock = false;
  79. /**
  80. * @param $key
  81. * @param bool $single
  82. *
  83. * @return mixed|false
  84. */
  85. public function get_meta( $key, $single = true ) {
  86. if ( $this->mock ) {
  87. return false;
  88. }
  89. return parent::get_meta( $key, $single );
  90. }
  91. /**
  92. * Returns the title of a popup.
  93. *
  94. * @uses filter `pum_popup_get_title`
  95. *
  96. * @return string
  97. */
  98. public function get_title() {
  99. $title = isset( $this->title ) ? $this->title : $this->get_meta( 'popup_title' );
  100. return (string) apply_filters( 'pum_popup_get_title', (string) $title, $this->ID );
  101. }
  102. /**
  103. * Returns the content of a popup.
  104. *
  105. * @uses filter `pum_popup_content`
  106. *
  107. * @return string
  108. */
  109. public function get_content() {
  110. /** @deprecated 1.8.0 */
  111. $this->content = $this->post_content;
  112. return apply_filters( 'pum_popup_content', $this->post_content, $this->ID );
  113. }
  114. /**
  115. * Returns array of all popup settings.
  116. *
  117. * @return array
  118. */
  119. public function get_settings() {
  120. if ( ! isset( $this->settings ) ) {
  121. // This hack is here to allow creating popups on the fly without saved meta.
  122. $settings = isset( $this->settings ) ? $this->settings : $this->get_meta( 'popup_settings' );
  123. if ( ! is_array( $settings ) ) {
  124. $settings = array();
  125. }
  126. // Review: the above should be removed and replaced with a hooked filter here to supply defaults when $settings === false.
  127. $this->settings = apply_filters( 'pum_popup_settings', $settings, $this->ID );
  128. }
  129. return $this->settings;
  130. }
  131. /**
  132. * Returns a specific popup setting with optional default value when not found.
  133. *
  134. * @param $key
  135. * @param bool $default
  136. *
  137. * @return bool|mixed
  138. */
  139. public function get_setting( $key, $default = false ) {
  140. $settings = $this->get_settings();
  141. return isset( $settings[ $key ] ) ? $settings[ $key ] : $default;
  142. }
  143. /**
  144. * @param string $key
  145. * @param mixed $value
  146. *
  147. * @return bool|int
  148. */
  149. public function update_setting( $key, $value ) {
  150. // TODO Once fields have been merged into the model itself, add automatic validation here.
  151. $new_settings = array( $key => $value );
  152. return $this->update_settings( $new_settings, true );
  153. }
  154. /**
  155. * @param array $new_settings
  156. * @param bool $merge
  157. *
  158. * @return bool|int
  159. */
  160. public function update_settings( $new_settings = array(), $merge = true ) {
  161. $settings = $this->get_settings();
  162. // TODO Once fields have been merged into the model itself, add automatic validation here.
  163. if ( $merge ) {
  164. foreach ( $new_settings as $key => $value ) {
  165. $settings[ $key ] = $value;
  166. }
  167. } else {
  168. $settings = $new_settings;
  169. }
  170. if ( empty( $settings['theme_id'] ) ) {
  171. $settings['theme_id'] = pum_get_default_theme_id();
  172. }
  173. if ( empty( $settings['theme_slug'] ) ) {
  174. $settings['theme_slug'] = get_post_field( 'post_name', $settings['theme_id'] );
  175. }
  176. return $this->update_meta( 'popup_settings', $settings );
  177. }
  178. /**
  179. * Returns cleansed public settings for a popup.
  180. *
  181. * @return array
  182. */
  183. public function get_public_settings() {
  184. $settings = wp_parse_args( $this->get_settings(), PUM_Admin_Popups::defaults() );
  185. foreach ( $settings as $key => $value ) {
  186. $field = PUM_Admin_Popups::get_field( $key );
  187. if ( false === $field && isset( $value ) ) {
  188. // This is a value set programatically, not by a defined field. ex theme_slug.
  189. $settings[ $key ] = $value;
  190. continue;
  191. }
  192. if ( $field['private'] ) {
  193. unset( $settings[ $key ] );
  194. } elseif ( 'checkbox' === $field['type'] ) {
  195. $settings[ $key ] = (bool) $value;
  196. }
  197. }
  198. $settings['id'] = $this->ID;
  199. $settings['slug'] = $this->post_name;
  200. $filters = array( 'js_only' => true );
  201. if ( $this->has_conditions( $filters ) ) {
  202. $settings['conditions'] = $this->get_conditions( $filters );
  203. }
  204. return apply_filters( 'pum_popup_get_public_settings', $settings, $this );
  205. }
  206. /**
  207. * @return array
  208. */
  209. public function get_cookies() {
  210. return apply_filters( 'pum_popup_get_cookies', $this->get_setting( 'cookies', array() ), $this->ID );
  211. }
  212. /**
  213. * @param $event
  214. *
  215. * @return bool
  216. */
  217. public function has_cookie( $event ) {
  218. foreach ( (array) $this->get_cookies() as $cookie ) {
  219. if ( $cookie['event'] == $event ) {
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. /**
  226. * @return array
  227. */
  228. public function get_triggers() {
  229. $triggers = $this->get_setting( 'triggers', array() );
  230. // Automatically add click trigger when on the front end.
  231. if ( ! is_admin() && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
  232. $has_click_trigger = false;
  233. foreach ( $triggers as $trigger ) {
  234. if ( $trigger['type'] == 'click_open' ) {
  235. $has_click_trigger = true;
  236. }
  237. }
  238. if ( ! $has_click_trigger && apply_filters( 'pum_add_default_click_trigger', true, $this->ID ) ) {
  239. $triggers[] = array(
  240. 'type' => 'click_open',
  241. 'settings' => array(
  242. 'extra_selectors' => '',
  243. 'cookie_name' => null,
  244. ),
  245. );
  246. }
  247. }
  248. return apply_filters( 'pum_popup_get_triggers', $triggers, $this->ID );
  249. }
  250. /**
  251. * @param $type
  252. *
  253. * @return bool
  254. */
  255. public function has_trigger( $type ) {
  256. $triggers = $this->get_triggers();
  257. foreach ( $triggers as $trigger ) {
  258. if ( $trigger['type'] == $type ) {
  259. return true;
  260. }
  261. }
  262. return false;
  263. }
  264. /**
  265. * Returns this popups theme id or the default id.
  266. *
  267. * @uses filter `pum_popup_get_theme_id`
  268. *
  269. * @return int $theme_id
  270. */
  271. public function get_theme_id() {
  272. // TODO replace usage of popmake_get_default_popup_theme.
  273. $theme_id = $this->get_setting( 'theme_id', pum_get_default_theme_id() );
  274. return (int) apply_filters( 'pum_popup_get_theme_id', $theme_id, $this->ID );
  275. }
  276. /**
  277. * Retrieve settings in the form of deprecated grouped arrays.
  278. *
  279. * @param $group
  280. * @param null $key
  281. *
  282. * @return mixed
  283. */
  284. protected function _dep_get_settings_group( $group, $key = null ) {
  285. if ( $this->mock ) {
  286. return array();
  287. }
  288. if ( ! $this->$group ) {
  289. /**
  290. * Remap old meta settings to new settings location for v1.7. This acts as a passive migration when needed.
  291. */
  292. $remapped_keys = $this->remapped_meta_settings_keys( $group );
  293. // This will only return data from extensions as core data has been migrated already.
  294. $group_values = $this->get_meta( "popup_$group" );
  295. if ( ! $group_values || ! is_array( $group_values ) ) {
  296. $group_values = array();
  297. }
  298. // Data manipulation begins here. We don't want any of this saved, only returned for backward compatibility.
  299. foreach ( $remapped_keys as $old_key => $new_key ) {
  300. $group_values[ $old_key ] = $this->get_setting( $new_key );
  301. }
  302. $deprecated_values = popmake_get_popup_meta_group( $group, $this->ID );
  303. if ( ! empty( $deprecated_values ) ) {
  304. foreach ( $deprecated_values as $old_key => $value ) {
  305. if ( ! isset( $group_values[ $old_key ] ) ) {
  306. $group_values[ $old_key ] = $value;
  307. }
  308. }
  309. }
  310. $this->$group = $group_values;
  311. }
  312. $values = apply_filters( "pum_popup_get_$group", $this->$group, $this->ID );
  313. if ( ! $key ) {
  314. return $values;
  315. }
  316. $value = isset ( $values[ $key ] ) ? $values[ $key ] : null;
  317. if ( ! isset( $value ) ) {
  318. $value = $this->get_meta( "popup_{$group}_{$key}" );
  319. }
  320. return apply_filters( "pum_popup_get_{$group}_" . $key, $value, $this->ID );
  321. }
  322. /**
  323. * @param $group
  324. *
  325. * @return array|mixed
  326. */
  327. public function remapped_meta_settings_keys( $group ) {
  328. $remapped_meta_settings_keys = array(
  329. 'display' => array(
  330. 'stackable' => 'stackable',
  331. 'overlay_disabled' => 'overlay_disabled',
  332. 'scrollable_content' => 'scrollable_content',
  333. 'disable_reposition' => 'disable_reposition',
  334. 'size' => 'size',
  335. 'responsive_min_width' => 'responsive_min_width',
  336. 'responsive_min_width_unit' => 'responsive_min_width_unit',
  337. 'responsive_max_width' => 'responsive_max_width',
  338. 'responsive_max_width_unit' => 'responsive_max_width_unit',
  339. 'custom_width' => 'custom_width',
  340. 'custom_width_unit' => 'custom_width_unit',
  341. 'custom_height' => 'custom_height',
  342. 'custom_height_unit' => 'custom_height_unit',
  343. 'custom_height_auto' => 'custom_height_auto',
  344. 'location' => 'location',
  345. 'position_from_trigger' => 'position_from_trigger',
  346. 'position_top' => 'position_top',
  347. 'position_left' => 'position_left',
  348. 'position_bottom' => 'position_bottom',
  349. 'position_right' => 'position_right',
  350. 'position_fixed' => 'position_fixed',
  351. 'animation_type' => 'animation_type',
  352. 'animation_speed' => 'animation_speed',
  353. 'animation_origin' => 'animation_origin',
  354. 'overlay_zindex' => 'overlay_zindex',
  355. 'zindex' => 'zindex',
  356. ),
  357. 'close' => array(
  358. 'text' => 'close_text',
  359. 'button_delay' => 'close_button_delay',
  360. 'overlay_click' => 'close_on_overlay_click',
  361. 'esc_press' => 'close_on_esc_press',
  362. 'f4_press' => 'close_on_f4_press',
  363. ),
  364. );
  365. return isset( $remapped_meta_settings_keys[ $group ] ) ? $remapped_meta_settings_keys[ $group ] : array();
  366. }
  367. /**
  368. * Returns all or single display settings.
  369. *
  370. * @deprecated 1.7.0 Use get_setting instead.
  371. *
  372. * @param string|null $key
  373. *
  374. * @return mixed
  375. */
  376. public function get_display( $key = null ) {
  377. $display = $this->_dep_get_settings_group( 'display', $key );
  378. foreach (
  379. array(
  380. 'responsive_min_width',
  381. 'responsive_max_width',
  382. 'custom_width',
  383. 'custom_height',
  384. ) as $key => $value
  385. ) {
  386. $temp = isset( $display[ $key ] ) ? $display[ $key ] : false;
  387. if ( $temp && is_string( $temp ) ) {
  388. $display[ $key ] = preg_replace( '/\D/', '', $temp );
  389. $display[ $key . '_unit' ] = str_replace( $display[ $key ], '', $temp );
  390. }
  391. }
  392. return $display;
  393. }
  394. /**
  395. * Returns all or single close settings.
  396. *
  397. * @deprecated 1.7.0 Use get_setting instead.
  398. *
  399. * @param string|null $key
  400. *
  401. * @return mixed
  402. */
  403. public function get_close( $key = null ) {
  404. return $this->_dep_get_settings_group( 'close', $key );
  405. }
  406. /**
  407. * Returns the slug for a theme. Used for CSS classes.
  408. *
  409. * @return string
  410. */
  411. private function get_theme_slug() {
  412. $theme_slug = $this->get_setting( 'theme_slug' );
  413. if ( false === $theme_slug ) {
  414. $theme_slug = get_post_field( 'post_name', $this->get_theme_id() );
  415. $this->update_setting( 'theme_slug', $theme_slug );
  416. }
  417. return $theme_slug;
  418. }
  419. /**
  420. * Returns array of classes for this popup.
  421. *
  422. * @param string $element The key or html element identifier.
  423. *
  424. * @return array $classes
  425. */
  426. public function get_classes( $element = 'overlay' ) {
  427. $classes = array(
  428. 'overlay' => array(
  429. 'pum',
  430. 'pum-overlay',
  431. 'pum-theme-' . $this->get_theme_id(),
  432. 'pum-theme-' . $this->get_theme_slug(),
  433. 'popmake-overlay', // Backward Compatibility
  434. ),
  435. 'container' => array(
  436. 'pum-container',
  437. 'popmake', // Backward Compatibility
  438. 'theme-' . $this->get_theme_id(), // Backward Compatibility
  439. ),
  440. 'title' => array(
  441. 'pum-title',
  442. 'popmake-title', // Backward Compatibility
  443. ),
  444. 'content' => array(
  445. 'pum-content',
  446. 'popmake-content', // Backward Compatibility
  447. ),
  448. 'close' => array(
  449. 'pum-close',
  450. 'popmake-close' // Backward Compatibility
  451. ),
  452. );
  453. $size = $this->get_setting( 'size', 'medium' );
  454. if ( in_array( $size, array( 'nano', 'micro', 'tiny', 'small', 'medium', 'normal', 'large', 'xlarge' ) ) ) {
  455. $classes['container'] = array_merge( $classes['container'], array(
  456. 'pum-responsive',
  457. 'pum-responsive-' . $size,
  458. 'responsive', // Backward Compatibility
  459. 'size-' . $size, // Backward Compatibility
  460. ) );
  461. } elseif ( $size == 'custom' ) {
  462. $classes['container'][] = 'size-custom'; // Backward Compatibility
  463. }
  464. if ( ! $this->get_setting( 'custom_height_auto' ) && $this->get_setting( 'scrollable_content' ) ) {
  465. $classes['container'] = array_merge( $classes['container'], array(
  466. 'pum-scrollable',
  467. 'scrollable', // Backward Compatibility
  468. ) );
  469. }
  470. if ( $this->get_setting( 'position_fixed' ) ) {
  471. $classes['container'][] = 'pum-position-fixed';
  472. }
  473. if ( $this->get_setting( 'overlay_disabled' ) ) {
  474. $classes['overlay'][] = 'pum-overlay-disabled';
  475. }
  476. if ( $this->get_setting( 'disable_accessibility' ) ) {
  477. $classes['overlay'][] = 'pum-accessibility-disabled';
  478. }
  479. if ( $this->get_setting( 'close_on_overlay_click' ) ) {
  480. $classes['overlay'][] = 'pum-click-to-close';
  481. }
  482. // Add a class for each trigger type.
  483. foreach ( $this->get_triggers() as $trigger ) {
  484. if ( ! in_array( $trigger['type'], $classes['overlay'] ) ) {
  485. $classes['overlay'][] = $trigger['type'];
  486. }
  487. }
  488. if ( is_singular( 'popup' ) ) {
  489. $classes['overlay'][] = 'pum-preview';
  490. }
  491. $classes = apply_filters( 'pum_popup_classes', $classes, $this->ID );
  492. if ( ! isset( $classes[ $element ] ) ) {
  493. $classes[ $element ] = array();
  494. }
  495. return apply_filters( "pum_popup_{$element}_classes", $classes[ $element ], $this->ID );
  496. }
  497. /**
  498. * Returns array for data attribute of this popup.
  499. *
  500. * @deprecated 1.8.0
  501. *
  502. * @return array|bool
  503. */
  504. public function get_data_attr() {
  505. if ( $this->mock ) {
  506. return false;
  507. }
  508. $data_attr = array(
  509. 'id' => $this->ID,
  510. 'slug' => $this->post_name,
  511. 'theme_id' => $this->get_theme_id(),
  512. 'cookies' => $this->get_cookies(),
  513. 'triggers' => $this->get_triggers(),
  514. 'mobile_disabled' => $this->mobile_disabled() ? true : null,
  515. 'tablet_disabled' => $this->tablet_disabled() ? true : null,
  516. 'meta' => array(
  517. 'display' => $this->get_display(),
  518. 'close' => $this->get_close(),
  519. // Added here for backward compatibility in extensions.
  520. 'click_open' => popmake_get_popup_meta( 'click_open', $this->ID ),
  521. ),
  522. );
  523. $filters = array( 'js_only' => true );
  524. if ( $this->has_conditions( $filters ) ) {
  525. $data_attr['conditions'] = $this->get_conditions( $filters );
  526. }
  527. return apply_filters( 'pum_popup_data_attr', $data_attr, $this->ID );
  528. }
  529. /**
  530. * Returns the close button text.
  531. *
  532. * @return string
  533. */
  534. public function close_text() {
  535. $text = $this->get_setting( 'close_text', '&#215;' );
  536. $theme_text = pum_get_theme_close_text( $this->get_theme_id() );
  537. if ( empty( $text ) && ! empty( $theme_text ) ) {
  538. $text = $theme_text;
  539. }
  540. return apply_filters( 'pum_popup_close_text', $text, $this->ID );
  541. }
  542. /**
  543. * Returns true if the close button should be rendered.
  544. *
  545. * @uses apply_filters `pum_popup_show_close_button`
  546. *
  547. * @return bool
  548. */
  549. public function show_close_button() {
  550. return (bool) apply_filters( 'pum_popup_show_close_button', true, $this->ID );
  551. }
  552. /**
  553. * Get the popups conditions.
  554. *
  555. * @param array $filters
  556. *
  557. * @return array
  558. */
  559. public function get_conditions( $filters = array() ) {
  560. $filters = wp_parse_args( $filters, array(
  561. 'php_only' => null,
  562. 'js_only' => null,
  563. ) );
  564. $cache_key = hash( 'md5', json_encode( $filters ) );
  565. // Check if these exclusion filters have already been applied and prevent extra processing.
  566. $conditions = isset( $this->conditions_filtered[ $cache_key ] ) ? $this->conditions_filtered[ $cache_key ] : false;
  567. if ( ! $conditions ) {
  568. $conditions = $this->get_setting( 'conditions', array() );
  569. // Sanity Check on the values not operand value.
  570. foreach ( $conditions as $group_key => $group ) {
  571. foreach ( $group as $key => $condition ) {
  572. if ( $this->exclude_condition( $condition, $filters ) ) {
  573. unset( $conditions[ $group_key ][ $key ] );
  574. if ( empty( $conditions[ $group_key ] ) ) {
  575. unset( $conditions[ $group_key ] );
  576. break;
  577. }
  578. continue;
  579. }
  580. $conditions[ $group_key ][ $key ] = $this->parse_condition( $condition );
  581. }
  582. if ( ! empty( $conditions[ $group_key ] ) ) {
  583. // Renumber each subarray.
  584. $conditions[ $group_key ] = array_values( $conditions[ $group_key ] );
  585. }
  586. }
  587. // Renumber top arrays.
  588. $conditions = array_values( $conditions );
  589. $this->conditions_filtered[ $cache_key ] = $conditions;
  590. }
  591. return apply_filters( 'pum_popup_get_conditions', $conditions, $this->ID, $filters );
  592. }
  593. /**
  594. * Ensures condition data integrity.
  595. *
  596. * @param $condition
  597. *
  598. * @return array
  599. */
  600. public function parse_condition( $condition ) {
  601. $condition = wp_parse_args( $condition, array(
  602. 'target' => '',
  603. 'not_operand' => false,
  604. 'settings' => array(),
  605. ) );
  606. $condition['not_operand'] = (bool) $condition['not_operand'];
  607. /** Backward compatibility layer */
  608. foreach ( $condition['settings'] as $key => $value ) {
  609. $condition[ $key ] = $value;
  610. }
  611. // The not operand value is missing, set it to false.
  612. return $condition;
  613. }
  614. /**
  615. * @param $condition
  616. * @param array $filters
  617. *
  618. * @return bool
  619. */
  620. public function exclude_condition( $condition, $filters = array() ) {
  621. $exclude = false;
  622. // The condition target doesn't exist. Lets ignore this condition.
  623. if ( empty( $condition['target'] ) ) {
  624. return true;
  625. }
  626. $condition_args = PUM_Conditions::instance()->get_condition( $condition['target'] );
  627. // The condition target doesn't exist. Lets ignore this condition.
  628. if ( ! $condition_args ) {
  629. return true;
  630. }
  631. if ( $filters['js_only'] && $condition_args['advanced'] != true ) {
  632. return true;
  633. } elseif ( $filters['php_only'] && $condition_args['advanced'] != false ) {
  634. return true;
  635. }
  636. return $exclude;
  637. }
  638. /**
  639. * Checks if this popup has any conditions.
  640. *
  641. * @param array $filters
  642. *
  643. * @return bool
  644. */
  645. public function has_conditions( $filters = array() ) {
  646. return (bool) count( $this->get_conditions( $filters ) );
  647. }
  648. /**
  649. * Checks if the popup has a specific condition.
  650. *
  651. * Generally used for conditional asset loading.
  652. *
  653. * @param array|string $conditions
  654. *
  655. * @return bool
  656. */
  657. public function has_condition( $conditions ) {
  658. if ( ! $this->has_conditions() ) {
  659. return false;
  660. }
  661. $found = false;
  662. if ( ! is_array( $conditions ) ) {
  663. $conditions = array( $conditions );
  664. }
  665. foreach ( $this->get_conditions() as $group => $conds ) {
  666. foreach ( $conds as $condition ) {
  667. if ( in_array( $condition['target'], $conditions ) ) {
  668. $found = true;
  669. }
  670. }
  671. }
  672. return (bool) $found;
  673. }
  674. /**
  675. * Retrieves the 'enabled' meta key and returns true if popup is enabled
  676. *
  677. * @since 1.12
  678. * @return bool True if enabled
  679. */
  680. public function is_enabled() {
  681. $enabled = $this->get_meta( 'enabled' );
  682. // Post ID not valid.
  683. if ( false === $enabled ) {
  684. return false;
  685. }
  686. // If the key is missing...
  687. if ( '' === $enabled ) {
  688. // Set it to enabled.
  689. $enabled = 1;
  690. $this->update_meta( 'enabled', $enabled );
  691. } else {
  692. // Else, load it in.
  693. $enabled = intval( $enabled );
  694. if ( ! in_array( $enabled, array( 0, 1 ), true ) ) {
  695. $enabled = 1;
  696. }
  697. }
  698. if ( 1 === $enabled ) {
  699. return true;
  700. } else {
  701. return false;
  702. }
  703. }
  704. /**
  705. * Returns whether or not the popup is visible in the loop.
  706. *
  707. * @return bool
  708. */
  709. public function is_loadable() {
  710. // Loadable defaults to true if no conditions. Making the popup available everywhere.
  711. $loadable = true;
  712. if ( ! $this->ID ) {
  713. return false;
  714. // Published/private
  715. }
  716. // If popup is not enabled, this popup is not loadable.
  717. if ( ! $this->is_enabled() ) {
  718. return false;
  719. }
  720. $filters = array( 'php_only' => true );
  721. if ( $this->has_conditions( $filters ) ) {
  722. // All Groups Must Return True. Break if any is false and set $loadable to false.
  723. foreach ( $this->get_conditions( $filters ) as $group => $conditions ) {
  724. // Groups are false until a condition proves true.
  725. $group_check = false;
  726. // At least one group condition must be true. Break this loop if any condition is true.
  727. foreach ( $conditions as $condition ) {
  728. // If any condition passes, set $group_check true and break.
  729. if ( ! $condition['not_operand'] && $this->check_condition( $condition ) ) {
  730. $group_check = true;
  731. break;
  732. } elseif ( $condition['not_operand'] && ! $this->check_condition( $condition ) ) {
  733. $group_check = true;
  734. break;
  735. }
  736. }
  737. // If any group of conditions doesn't pass, popup is not loadable.
  738. if ( ! $group_check ) {
  739. $loadable = false;
  740. }
  741. }
  742. }
  743. return apply_filters( 'pum_popup_is_loadable', $loadable, $this->ID );
  744. }
  745. /**
  746. * Check an individual condition with settings.
  747. *
  748. * @param array $condition
  749. *
  750. * @return bool
  751. */
  752. public function check_condition( $condition = array() ) {
  753. $condition_args = PUM_Conditions::instance()->get_condition( $condition['target'] );
  754. if ( ! $condition_args ) {
  755. return false;
  756. }
  757. $condition['settings'] = isset( $condition['settings'] ) && is_array( $condition['settings'] ) ? $condition['settings'] : array();
  758. return (bool) call_user_func( $condition_args['callback'], $condition, $this );
  759. }
  760. /**
  761. * Check if mobile was disabled
  762. *
  763. * @return bool
  764. */
  765. public function mobile_disabled() {
  766. return (bool) apply_filters( 'pum_popup_mobile_disabled', $this->get_setting( 'disable_on_mobile' ), $this->ID );
  767. }
  768. /**
  769. * Check if tablet was disabled
  770. *
  771. * @return bool
  772. */
  773. public function tablet_disabled() {
  774. return (bool) apply_filters( 'pum_popup_tablet_disabled', (bool) $this->get_setting( 'disable_on_tablet' ), $this->ID );
  775. }
  776. /**
  777. * Get a popups event count.
  778. *
  779. * @param string $event
  780. * @param string $which
  781. *
  782. * @return int
  783. */
  784. public function get_event_count( $event = 'open', $which = 'current' ) {
  785. switch ( $which ) {
  786. case 'current':
  787. $current = $this->get_meta( "popup_{$event}_count" );
  788. // Save future queries by inserting a valid count.
  789. if ( $current === false || ! is_numeric( $current ) ) {
  790. $current = 0;
  791. $this->update_meta( "popup_{$event}_count", $current );
  792. }
  793. return absint( $current );
  794. case 'total':
  795. $total = $this->get_meta( "popup_{$event}_count_total" );
  796. // Save future queries by inserting a valid count.
  797. if ( $total === false || ! is_numeric( $total ) ) {
  798. $total = 0;
  799. $this->update_meta( "popup_{$event}_count_total", $total );
  800. }
  801. return absint( $total );
  802. }
  803. return 0;
  804. }
  805. /**
  806. * Increase popup event counts.
  807. *
  808. * @param string $event
  809. */
  810. public function increase_event_count( $event = 'open' ) {
  811. /**
  812. * This section simply ensures that all keys exist before the below query runs. This should only ever cause extra queries once per popup, usually in the admin.
  813. */
  814. //$this->set_event_defaults( $event );
  815. $keys = PUM_Analytics::event_keys( $event );
  816. // Set the current count
  817. $current = $this->get_event_count( $event );
  818. if ( ! $current ) {
  819. $current = 0;
  820. }
  821. $current = $current + 1;
  822. // Set the total count since creation.
  823. $total = $this->get_event_count( $event, 'total' );
  824. if ( ! $total ) {
  825. $total = 0;
  826. }
  827. $total = $total + 1;
  828. $this->update_meta( 'popup_' . $keys[0] . '_count', absint( $current ) );
  829. $this->update_meta( 'popup_' . $keys[0] . '_count_total', absint( $total ) );
  830. $this->update_meta( 'popup_last_' . $keys[1], current_time( 'timestamp', 0 ) );
  831. $site_total = get_option( 'pum_total_' . $keys[0] . '_count', 0 );
  832. $site_total ++;
  833. update_option( 'pum_total_' . $keys[0] . '_count', $site_total );
  834. // If is multisite add this blogs total to the site totals.
  835. if ( function_exists( 'is_multisite' ) && is_multisite() ) {
  836. $network_total = get_site_option( 'pum_site_total_' . $keys[0] . '_count', false );
  837. $network_total = ! $network_total ? $site_total : $network_total + 1;
  838. update_site_option( 'pum_site_total_' . $keys[0] . '_count', $network_total );
  839. }
  840. }
  841. /**
  842. * @param $event
  843. */
  844. public function set_event_defaults( $event ) {
  845. $this->get_event_count( $event );
  846. $this->get_event_count( $event, 'total' );
  847. $keys = PUM_Analytics::event_keys( $event );
  848. $last = $this->get_meta( 'popup_last_' . $keys[1] );
  849. if ( empty( $last ) || ! is_numeric( $last ) ) {
  850. $this->update_meta( 'popup_last_' . $keys[1], 0 );
  851. }
  852. }
  853. /**
  854. * Log and reset popup open count to 0.
  855. */
  856. public function reset_counts() {
  857. // Log the reset time and count.
  858. add_post_meta( $this->ID, 'popup_count_reset', array(
  859. 'timestamp' => current_time( 'timestamp', 0 ),
  860. 'opens' => absint( $this->get_event_count( 'open', 'current' ) ),
  861. 'conversions' => absint( $this->get_event_count( 'conversion', 'current' ) ),
  862. ) );
  863. foreach ( array( 'open', 'conversion' ) as $event ) {
  864. $keys = PUM_Analytics::event_keys( $event );
  865. $this->update_meta( 'popup_' . $keys[0] . '_count', 0 );
  866. $this->update_meta( 'popup_last_' . $keys[1], 0 );
  867. }
  868. }
  869. /**
  870. * Returns the last reset information.
  871. *
  872. * @return mixed
  873. */
  874. public function get_last_count_reset() {
  875. $resets = $this->get_meta( 'popup_count_reset', false );
  876. if ( empty ( $resets ) ) {
  877. // No results found.
  878. return false;
  879. }
  880. if ( ! empty( $resets['timestamp'] ) ) {
  881. // Looks like the result is already the last one, return it.
  882. return $resets;
  883. }
  884. if ( count( $resets ) == 1 ) {
  885. // Looks like we only got one result, return it.
  886. return $resets[0];
  887. }
  888. usort( $resets, array( $this, "compare_resets" ) );
  889. return $resets[0];
  890. }
  891. /**
  892. * @param $a
  893. * @param $b
  894. *
  895. * @return bool
  896. */
  897. public function compare_resets( $a, $b ) {
  898. return ( float ) $a['timestamp'] < ( float ) $b['timestamp'];
  899. }
  900. /**
  901. * @param $post WP_Post
  902. */
  903. public function setup( $post ) {
  904. parent::setup( $post );
  905. if ( ! $this->is_valid() ) {
  906. return;
  907. }
  908. // REVIEW Does this need to be here or somewhere else like get_meta/get_setting?
  909. if ( ! isset( $this->data_version ) ) {
  910. $this->data_version = (int) $this->get_meta( 'data_version' );
  911. if ( ! $this->data_version ) {
  912. $theme = $this->get_meta( 'popup_theme' );
  913. $display_settings = $this->get_meta( 'popup_display' );
  914. // If there are existing settings set the data version to 2 so they can be updated.
  915. // Otherwise set to the current version as this is a new popup.
  916. $is_v2 = ( ! empty( $display_settings ) && is_array( $display_settings ) ) || $theme > 0;
  917. $this->data_version = $is_v2 ? 2 : $this->model_version;
  918. $this->update_meta( 'data_version', $this->data_version );
  919. }
  920. }
  921. if ( $this->data_version < $this->model_version && pum_passive_popup_upgrades_enabled() ) {
  922. /**
  923. * Process passive settings migration as each popup is loaded. The will only run each migration routine once for each popup.
  924. */
  925. $this->passive_migration();
  926. }
  927. }
  928. /**
  929. * Allows for passive migration routines based on the current data version.
  930. */
  931. public function passive_migration() {
  932. $this->doing_passive_migration = true;
  933. for ( $i = $this->data_version; $this->data_version < $this->model_version; $i ++ ) {
  934. do_action_ref_array( 'pum_popup_passive_migration_' . $this->data_version, array( &$this ) );
  935. $this->data_version ++;
  936. /**
  937. * Update the popups data version.
  938. */
  939. $this->update_meta( 'data_version', $this->data_version );
  940. }
  941. do_action_ref_array( 'pum_popup_passive_migration', array( &$this, $this->data_version ) );
  942. $this->doing_passive_migration = false;
  943. }
  944. /**
  945. * @deprecated 1.7.0 Still used in several extension migration routines, so needs to stay for now.
  946. */
  947. public function save() {
  948. try {
  949. pum()->popups->update_item( $this->ID, $this->to_array() );
  950. } catch ( Exception $e ) {
  951. return;
  952. }
  953. }
  954. /**
  955. * @deprecated 1.8.0 Only here to prevent possible errors.
  956. *
  957. * @param $id
  958. * @param bool $force
  959. *
  960. * @return PUM_Model_Popup
  961. */
  962. public static function instance( $id, $force = false ) {
  963. return pum_get_popup( $id );
  964. }
  965. }