Нет описания

Popups.php 46KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Admin_Popups
  10. *
  11. * @since 1.7.0
  12. */
  13. class PUM_Admin_Popups {
  14. /**
  15. * Hook the initialize method to the WP init action.
  16. */
  17. public static function init() {
  18. // Adds ID to top of popup editor.
  19. add_action( 'edit_form_top', array( __CLASS__, 'add_popup_id' ) );
  20. // Change title to popup name.
  21. add_filter( 'enter_title_here', array( __CLASS__, '_default_title' ) );
  22. // Add popup title field.
  23. add_action( 'edit_form_advanced', array( __CLASS__, 'title_meta_field' ) );
  24. // Add Contextual help to post_name field.
  25. add_action( 'edit_form_before_permalink', array( __CLASS__, 'popup_post_title_contextual_message' ) );
  26. // Register Metaboxes.
  27. add_action( 'add_meta_boxes', array( __CLASS__, 'meta_box' ) );
  28. // Process meta saving.
  29. add_action( 'save_post', array( __CLASS__, 'save' ), 10, 2 );
  30. // Set the slug properly on save.
  31. add_filter( 'wp_insert_post_data', array( __CLASS__, 'set_slug' ), 99, 2 );
  32. // Dashboard columns & filters.
  33. add_filter( 'manage_edit-popup_columns', array( __CLASS__, 'dashboard_columns' ) );
  34. add_action( 'manage_posts_custom_column', array( __CLASS__, 'render_columns' ), 10, 2 );
  35. add_filter( 'manage_edit-popup_sortable_columns', array( __CLASS__, 'sortable_columns' ) );
  36. add_filter( 'default_hidden_columns', array( __CLASS__, 'hide_columns' ), 10, 2 );
  37. add_action( 'load-edit.php', array( __CLASS__, 'load' ), 9999 );
  38. add_action( 'restrict_manage_posts', array( __CLASS__, 'add_popup_filters' ), 100 );
  39. add_filter( 'post_row_actions', array( __CLASS__, 'add_id_row_actions' ), 2, 100 );
  40. add_action( 'post_submitbox_misc_actions', array( __CLASS__, 'add_enabled_toggle_editor' ), 10, 1 );
  41. }
  42. /**
  43. * Adds our enabled state toggle to the "Publish" meta box.
  44. *
  45. * @since 1.12
  46. * @param WP_POST $post The current post (i.e. the popup).
  47. */
  48. public static function add_enabled_toggle_editor( $post ) {
  49. if ( 'publish' !== $post->post_status || 'popup' !== $post->post_type ) {
  50. return;
  51. }
  52. $popup = pum_get_popup( $post->ID );
  53. $enabled = $popup->is_enabled();
  54. $nonce = wp_create_nonce( "pum_save_enabled_state_{$popup->ID}" );
  55. ?>
  56. <div class="misc-pub-section" style="display:flex;">
  57. <span style="font-weight: bold; margin-right: 10px;">Popup Enabled </span>
  58. <div class="pum-toggle-button">
  59. <input id="pum-enabled-toggle-<?php echo esc_attr( $popup->ID ); ?>" type="checkbox" <?php checked( true, $enabled ); ?> class="pum-enabled-toggle-button" data-nonce="<?php echo esc_attr( $nonce ); ?>" data-popup-id="<?php echo esc_attr( $popup->ID ); ?>">
  60. <label for="pum-enabled-toggle-<?php echo esc_attr( $popup->ID ); ?>" aria-label="Switch to enable popup"></label>
  61. </div>
  62. </div>
  63. <?php
  64. }
  65. /**
  66. * Adds the Popup ID right under the "Edit Popup" heading
  67. *
  68. * @param WP_Post $post Post object.
  69. * @since 1.12.0
  70. */
  71. public static function add_popup_id( $post ) {
  72. if ( 'popup' === $post->post_type ) {
  73. ?>
  74. <p style="margin:0;font-size:12px;">ID: <span id="popup-id" data-popup-id="<?php echo esc_attr( $post->ID ); ?>"><?php echo esc_html( $post->ID ); ?></span></p>
  75. <?php
  76. }
  77. }
  78. /**
  79. * Change default "Enter title here" input
  80. *
  81. * @param string $title Default title placeholder text.
  82. * @return string $title New placeholder text
  83. */
  84. public static function _default_title( $title ) {
  85. if ( ! is_admin() ) {
  86. return $title;
  87. }
  88. $screen = get_current_screen();
  89. if ( 'popup_theme' === $screen->post_type ) {
  90. $label = 'popup' === $screen->post_type ? __( 'Popup', 'popup-maker' ) : __( 'Popup Theme', 'popup-maker' );
  91. $title = sprintf( '%s Name', $label );
  92. }
  93. if ( 'popup' === $screen->post_type ) {
  94. $title = __( 'Popup Name', 'popup-maker' );
  95. }
  96. return $title;
  97. }
  98. /**
  99. * Renders the popup title meta field.
  100. */
  101. public static function title_meta_field() {
  102. global $post, $pagenow, $typenow;
  103. if ( has_blocks( $post ) || ( function_exists( 'use_block_editor_for_post' ) && use_block_editor_for_post( $post ) ) ) {
  104. return;
  105. }
  106. if ( ! is_admin() ) {
  107. return;
  108. }
  109. if ( 'popup' == $typenow && in_array( $pagenow, array( 'post-new.php', 'post.php' ) ) ) { ?>
  110. <div id="popup-titlediv" class="pum-form">
  111. <div id="popup-titlewrap">
  112. <label class="screen-reader-text" id="popup-title-prompt-text" for="popup-title">
  113. <?php esc_html_e( 'Popup Title', 'popup-maker' ); ?>
  114. </label>
  115. <input tabindex="2" name="popup_title" size="30" value="<?php echo esc_attr( get_post_meta( $post->ID, 'popup_title', true ) ); ?>" id="popup-title" autocomplete="off" placeholder="<?php esc_html_e( 'Popup Title', 'popup-maker' ); ?>" />
  116. <p class="pum-desc"><?php echo '(' . esc_html__( 'Optional', 'popup-maker' ) . ') ' . esc_html__( 'Shown as headline inside the popup. Can be left blank.', 'popup-maker' ); ?></p>
  117. </div>
  118. <div class="inside"></div>
  119. </div>
  120. <script>jQuery('#popup-titlediv').insertAfter('#titlediv');</script>
  121. <?php
  122. }
  123. }
  124. /**
  125. * Renders contextual help for title.
  126. */
  127. public static function popup_post_title_contextual_message() {
  128. global $post, $pagenow, $typenow;
  129. if ( has_blocks( $post ) || ( function_exists( 'use_block_editor_for_post' ) && use_block_editor_for_post( $post ) ) ) {
  130. return;
  131. }
  132. if ( ! is_admin() ) {
  133. return;
  134. }
  135. if ( 'popup' == $typenow && in_array( $pagenow, array( 'post-new.php', 'post.php' ) ) ) { ?>
  136. <p class="pum-desc"><?php echo '(' . esc_html__( 'Required', 'popup-maker' ) . ') ' . esc_html__( 'Enter a name to help you remember what this popup is about. Only you will see this.', 'popup-maker' ); ?></p>
  137. <?php
  138. }
  139. }
  140. /**
  141. * Registers popup metaboxes.
  142. */
  143. public static function meta_box() {
  144. add_meta_box( 'pum_popup_settings', __( 'Popup Settings', 'popup-maker' ), array( __CLASS__, 'render_settings_meta_box' ), 'popup', 'normal', 'high' );
  145. add_meta_box( 'pum_popup_analytics', __( 'Analytics', 'popup-maker' ), array( __CLASS__, 'render_analytics_meta_box' ), 'popup', 'side', 'high' );
  146. }
  147. /**
  148. * Ensures integrity of values.
  149. *
  150. * @param array $values Array of settings.
  151. * @return array
  152. */
  153. public static function parse_values( $values = array() ) {
  154. $defaults = self::defaults();
  155. if ( empty( $values ) ) {
  156. return $defaults;
  157. }
  158. $values = self::fill_missing_defaults( $values );
  159. return $values;
  160. }
  161. /**
  162. * Render the settings meta box wrapper and JS vars.
  163. */
  164. public static function render_settings_meta_box() {
  165. global $post;
  166. $popup = pum_get_popup( $post->ID );
  167. // Get the meta directly rather than from cached object.
  168. $settings = self::parse_values( $popup->get_settings() );
  169. wp_nonce_field( basename( __FILE__ ), 'pum_popup_settings_nonce' );
  170. wp_enqueue_script( 'popup-maker-admin' );
  171. ?>
  172. <script type="text/javascript">
  173. window.pum_popup_settings_editor = <?php echo PUM_Utils_Array::safe_json_encode( apply_filters( 'pum_popup_settings_editor_var', array(
  174. 'form_args' => array(
  175. 'id' => 'pum-popup-settings',
  176. 'tabs' => self::tabs(),
  177. 'sections' => self::sections(),
  178. 'fields' => self::fields(),
  179. ),
  180. 'conditions' => PUM_Conditions::instance()->get_conditions(),
  181. 'conditions_selectlist' => PUM_Conditions::instance()->dropdown_list(),
  182. 'triggers' => PUM_Triggers::instance()->get_triggers(),
  183. 'cookies' => PUM_Cookies::instance()->get_cookies(),
  184. 'current_values' => self::render_form_values( $settings ),
  185. ) ) ); ?>;
  186. </script>
  187. <div id="pum-popup-settings-container" class="pum-popup-settings-container">
  188. <div class="pum-no-js" style="padding: 0 12px;">
  189. <p><?php printf( esc_html__( 'If you are seeing this, the page is still loading or there are Javascript errors on this page. %sView troubleshooting guide%s', 'popup-maker' ), '<a href="https://docs.wppopupmaker.com/article/373-checking-for-javascript-errors" target="_blank">', '</a>' ); ?></p>
  190. </div>
  191. </div>
  192. <?php
  193. }
  194. /**
  195. * Used to get deprecated fields for metabox saving of old extensions.
  196. *
  197. * @deprecated 1.7.0
  198. *
  199. * @return mixed
  200. */
  201. public static function deprecated_meta_fields() {
  202. $fields = array();
  203. foreach ( self::deprecated_meta_field_groups() as $group ) {
  204. foreach ( apply_filters( 'popmake_popup_meta_field_group_' . $group, array() ) as $field ) {
  205. $fields[] = 'popup_' . $group . '_' . $field;
  206. }
  207. }
  208. return apply_filters( 'popmake_popup_meta_fields', $fields );
  209. }
  210. /**
  211. * Used to get field groups from extensions.
  212. *
  213. * @deprecated 1.7.0
  214. *
  215. * @return mixed
  216. */
  217. public static function deprecated_meta_field_groups() {
  218. return apply_filters( 'popmake_popup_meta_field_groups', array( 'display', 'close' ) );
  219. }
  220. /**
  221. * @param $post_id
  222. * @param $post
  223. */
  224. public static function save( $post_id, $post ) {
  225. if ( isset( $post->post_type ) && 'popup' != $post->post_type ) {
  226. return;
  227. }
  228. if ( ! isset( $_POST['pum_popup_settings_nonce'] ) || ! wp_verify_nonce( $_POST['pum_popup_settings_nonce'], basename( __FILE__ ) ) ) {
  229. return;
  230. }
  231. if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || isset( $_REQUEST['bulk_edit'] ) ) {
  232. return;
  233. }
  234. if ( isset( $post->post_type ) && 'revision' == $post->post_type ) {
  235. return;
  236. }
  237. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  238. return;
  239. }
  240. $popup = pum_get_popup( $post_id );
  241. if ( isset( $_POST['popup_reset_counts'] ) ) {
  242. /**
  243. * Reset popup open count, per user request.
  244. */
  245. $popup->reset_counts();
  246. }
  247. $title = ! empty ( $_POST['popup_title'] ) ? trim( sanitize_text_field( $_POST['popup_title'] ) ) : '';
  248. $popup->update_meta( 'popup_title', $title );
  249. $settings = ! empty( $_POST['popup_settings'] ) ? $_POST['popup_settings'] : array();
  250. // Sanitize JSON values.
  251. $settings['conditions'] = isset( $settings['conditions'] ) ? self::sanitize_meta( $settings['conditions'] ) : array();
  252. $settings['triggers'] = isset( $settings['triggers'] ) ? self::sanitize_meta( $settings['triggers'] ) : array();
  253. $settings['cookies'] = isset( $settings['cookies'] ) ? self::sanitize_meta( $settings['cookies'] ) : array();
  254. $settings = apply_filters( 'pum_popup_setting_pre_save', $settings, $post->ID );
  255. $settings = self::sanitize_settings( $settings );
  256. $settings = self::parse_values( $settings );
  257. $popup->update_settings( $settings, false );
  258. // TODO Remove this and all other code here. This should be clean and all code more compartmentalized.
  259. foreach ( self::deprecated_meta_fields() as $field ) {
  260. if ( isset( $_POST[ $field ] ) ) {
  261. $new = apply_filters( 'popmake_metabox_save_' . $field, $_POST[ $field ] );
  262. update_post_meta( $post_id, $field, $new );
  263. } else {
  264. delete_post_meta( $post_id, $field );
  265. }
  266. }
  267. do_action( 'pum_save_popup', $post_id, $post );
  268. }
  269. /**
  270. * Parse & prepare values for form rendering.
  271. *
  272. * Add additional data for license_key fields, split the measure fields etc.
  273. *
  274. * @param $settings
  275. *
  276. * @return mixed
  277. */
  278. public static function render_form_values( $settings ) {
  279. foreach ( $settings as $key => $value ) {
  280. $field = self::get_field( $key );
  281. if ( $field ) {
  282. switch ( $field['type'] ) {
  283. case 'measure':
  284. break;
  285. }
  286. }
  287. }
  288. return $settings;
  289. }
  290. /**
  291. * List of tabs & labels for the settings panel.
  292. *
  293. * @return array
  294. */
  295. public static function tabs() {
  296. return apply_filters( 'pum_popup_settings_tabs', array(
  297. 'general' => __( 'General', 'popup-maker' ),
  298. 'display' => __( 'Display', 'popup-maker' ),
  299. 'close' => __( 'Close', 'popup-maker' ),
  300. 'triggers' => __( 'Triggers', 'popup-maker' ),
  301. 'targeting' => __( 'Targeting', 'popup-maker' ),
  302. 'advanced' => __( 'Advanced', 'popup-maker' ),
  303. ) );
  304. }
  305. /**
  306. * List of tabs & labels for the settings panel.
  307. *
  308. * @return array
  309. */
  310. public static function sections() {
  311. return apply_filters( 'pum_popup_settings_sections', array(
  312. 'general' => array(
  313. 'main' => __( 'General Settings', 'popup-maker' ),
  314. ),
  315. 'triggers' => array(
  316. 'main' => __( 'Triggers & Cookies', 'popup-maker' ),
  317. ),
  318. 'targeting' => array(
  319. 'main' => __( 'Conditions', 'popup-maker' ),
  320. ),
  321. 'display' => array(
  322. 'preset' => __( 'Display Presets', 'popup-maker' ),
  323. 'main' => __( 'Appearance', 'popup-maker' ),
  324. 'size' => __( 'Size', 'popup-maker' ),
  325. 'animation' => __( 'Animation', 'popup-maker' ),
  326. 'sound' => __( 'Sounds', 'popup-maker' ),
  327. 'position' => __( 'Position', 'popup-maker' ),
  328. 'advanced' => __( 'Advanced', 'popup-maker' ),
  329. ),
  330. 'close' => array(
  331. 'button' => __( 'Button', 'popup-maker' ),
  332. 'forms' => __( 'Form Submission', 'popup-maker' ),
  333. 'alternate_methods' => __( 'Alternate Methods', 'popup-maker' ),
  334. ),
  335. 'advanced' => array(
  336. 'main' => __( 'Advanced', 'popup-maker' ),
  337. ),
  338. ) );
  339. }
  340. /**
  341. * Returns array of popup settings fields.
  342. *
  343. * @return mixed
  344. */
  345. public static function fields() {
  346. static $tabs;
  347. if ( ! isset( $tabs ) ) {
  348. $tabs = apply_filters( 'pum_popup_settings_fields', array(
  349. 'general' => apply_filters( 'pum_popup_general_settings_fields', array(
  350. 'main' => array(),
  351. ) ),
  352. 'triggers' => apply_filters( 'pum_popup_triggers_settings_fields', array(
  353. 'main' => array(
  354. 'triggers' => array(
  355. 'type' => 'triggers',
  356. 'std' => array(),
  357. 'priority' => 10,
  358. ),
  359. 'separator1' => array(
  360. 'type' => 'separator',
  361. 'private' => true,
  362. ),
  363. 'cookies' => array(
  364. 'type' => 'cookies',
  365. 'std' => array(),
  366. 'priority' => 20,
  367. ),
  368. ),
  369. ) ),
  370. 'targeting' => apply_filters( 'pum_popup_targeting_settings_fields', array(
  371. 'main' => array(
  372. 'conditions' => array(
  373. 'type' => 'conditions',
  374. 'std' => array(),
  375. 'priority' => 10,
  376. 'private' => true,
  377. ),
  378. 'disable_on_mobile' => array(
  379. 'label' => __( 'Disable this popup on mobile devices.', 'popup-maker' ),
  380. 'type' => 'checkbox',
  381. 'priority' => 20,
  382. ),
  383. 'disable_on_tablet' => array(
  384. 'label' => __( 'Disable this popup on tablet devices.', 'popup-maker' ),
  385. 'type' => 'checkbox',
  386. 'priority' => 20,
  387. ),
  388. ),
  389. ) ),
  390. 'display' => apply_filters( 'pum_popup_display_settings_fields', array(
  391. 'preset' => array(
  392. 'explain' => array(
  393. 'type' => 'html',
  394. 'content' => '<p>Select one of the types below to get started! Once selected, you can adjust the display settings using the tabs above.</p>'
  395. ),
  396. 'type_section' => array(
  397. 'type' => 'section',
  398. 'classes' => 'popup-types',
  399. 'fields' => array(
  400. '<div class="popup-type" data-popup-type="center-popup"><img src="' . Popup_Maker::$URL . 'assets/images/admin/display-switcher/center-popup.png" alt="' . __( 'Center Popup', 'popup-maker' ) . '"/><button class="button">' . __( 'Center Popup', 'popup-maker' ) . '</button></div>',
  401. '<div class="popup-type" data-popup-type="right-bottom-slidein"><img src="' . Popup_Maker::$URL . 'assets/images/admin/display-switcher/right-bottom-slidein.png" alt="' . __( 'Right Bottom Slide-in', 'popup-maker' ) . '"/><button class="button">' . __( 'Right Bottom Slide-in', 'popup-maker' ) . '</button></div>',
  402. '<div class="popup-type" data-popup-type="top-bar"><img src="' . Popup_Maker::$URL . 'assets/images/admin/display-switcher/top-bar.png" alt="' . __( 'Top Bar', 'popup-maker' ) . '"/><button class="button">' . __( 'Top Bar', 'popup-maker' ) . '</button></div>',
  403. '<div class="popup-type" data-popup-type="left-bottom-notice"><img src="' . Popup_Maker::$URL . 'assets/images/admin/display-switcher/left-bottom-notice.png" alt="' . __( 'Left Bottom Notice', 'popup-maker' ) . '"/><button class="button">' . __( 'Left Bottom Notice', 'popup-maker' ) . '</button></div>',
  404. ),
  405. ),
  406. ),
  407. 'main' => array(
  408. 'theme_id' => array(
  409. 'label' => __( 'Popup Theme', 'popup-maker' ),
  410. 'dynamic_desc' => sprintf( '%1$s<br/><a id="edit_theme_link" href="%3$s">%2$s</a>', __( 'Choose a theme for this popup.', 'popup-maker' ), __( 'Customize This Theme', 'popup-maker' ), admin_url( "post.php?action=edit&post={{data.value}}" ) ),
  411. 'type' => 'select',
  412. 'options' => pum_is_popup_editor() ? PUM_Helpers::popup_theme_selectlist() : null,
  413. 'std' => pum_get_default_theme_id(),
  414. ),
  415. ),
  416. 'size' => array(
  417. 'size' => array(
  418. 'label' => __( 'Size', 'popup-maker' ),
  419. 'desc' => __( 'Select the size of the popup.', 'popup-maker' ),
  420. 'type' => 'select',
  421. 'std' => 'medium',
  422. 'priority' => 10,
  423. 'options' => array(
  424. __( 'Responsive Sizes', 'popup-maker' ) => array(
  425. 'nano' => __( 'Nano - 10%', 'popup-maker' ),
  426. 'micro' => __( 'Micro - 20%', 'popup-maker' ),
  427. 'tiny' => __( 'Tiny - 30%', 'popup-maker' ),
  428. 'small' => __( 'Small - 40%', 'popup-maker' ),
  429. 'medium' => __( 'Medium - 60%', 'popup-maker' ),
  430. 'normal' => __( 'Normal - 70%', 'popup-maker' ),
  431. 'large' => __( 'Large - 80%', 'popup-maker' ),
  432. 'xlarge' => __( 'X Large - 95%', 'popup-maker' ),
  433. ),
  434. __( 'Other Sizes', 'popup-maker' ) => array(
  435. 'auto' => __( 'Auto', 'popup-maker' ),
  436. 'custom' => __( 'Custom', 'popup-maker' ),
  437. ),
  438. ),
  439. ),
  440. 'responsive_min_width' => array(
  441. 'label' => __( 'Min Width', 'popup-maker' ),
  442. 'desc' => __( 'Set a minimum width for the popup.', 'popup-maker' ),
  443. 'type' => 'measure',
  444. 'std' => '0%',
  445. 'priority' => 20,
  446. 'dependencies' => array(
  447. 'size' => array( 'nano', 'micro', 'tiny', 'small', 'medium', 'normal', 'large', 'xlarge' ),
  448. ),
  449. ),
  450. 'responsive_max_width' => array(
  451. 'label' => __( 'Max Width', 'popup-maker' ),
  452. 'desc' => __( 'Set a maximum width for the popup.', 'popup-maker' ),
  453. 'type' => 'measure',
  454. 'std' => '100%',
  455. 'priority' => 30,
  456. 'dependencies' => array(
  457. 'size' => array( 'nano', 'micro', 'tiny', 'small', 'medium', 'normal', 'large', 'xlarge' ),
  458. ),
  459. ),
  460. 'custom_width' => array(
  461. 'label' => __( 'Width', 'popup-maker' ),
  462. 'desc' => __( 'Set a custom width for the popup.', 'popup-maker' ),
  463. 'type' => 'measure',
  464. 'std' => '640px',
  465. 'priority' => 40,
  466. 'dependencies' => array(
  467. 'size' => 'custom',
  468. ),
  469. ),
  470. 'custom_height_auto' => array(
  471. 'label' => __( 'Auto Adjusted Height', 'popup-maker' ),
  472. 'desc' => __( 'Checking this option will set height to fit the content.', 'popup-maker' ),
  473. 'type' => 'checkbox',
  474. 'priority' => 50,
  475. 'dependencies' => array(
  476. 'size' => 'custom',
  477. ),
  478. ),
  479. 'custom_height' => array(
  480. 'label' => __( 'Height', 'popup-maker' ),
  481. 'desc' => __( 'Set a custom height for the popup.', 'popup-maker' ),
  482. 'type' => 'measure',
  483. 'std' => '380px',
  484. 'priority' => 60,
  485. 'dependencies' => array(
  486. 'size' => 'custom',
  487. 'custom_height_auto' => false,
  488. ),
  489. ),
  490. 'scrollable_content' => array(
  491. 'label' => __( 'Scrollable Content', 'popup-maker' ),
  492. 'desc' => __( 'Checking this option will add a scroll bar to your content.', 'popup-maker' ),
  493. 'type' => 'checkbox',
  494. 'std' => false,
  495. 'priority' => 70,
  496. 'dependencies' => array(
  497. 'size' => 'custom',
  498. 'custom_height_auto' => false,
  499. ),
  500. ),
  501. ),
  502. 'animation' => array(
  503. 'animation_type' => array(
  504. 'label' => __( 'Animation Type', 'popup-maker' ),
  505. 'desc' => __( 'Select an animation type for your popup.', 'popup-maker' ),
  506. 'type' => 'select',
  507. 'std' => 'fade',
  508. 'priority' => 10,
  509. 'options' => array(
  510. 'none' => __( 'None', 'popup-maker' ),
  511. 'slide' => __( 'Slide', 'popup-maker' ),
  512. 'fade' => __( 'Fade', 'popup-maker' ),
  513. 'fadeAndSlide' => __( 'Fade and Slide', 'popup-maker' ),
  514. // 'grow' => __( 'Grow', 'popup-maker' ),
  515. // 'growAndSlide' => __( 'Grow and Slide', 'popup-maker' ),
  516. ),
  517. ),
  518. 'animation_speed' => array(
  519. 'label' => __( 'Animation Speed', 'popup-maker' ),
  520. 'desc' => __( 'Set the animation speed for the popup.', 'popup-maker' ),
  521. 'type' => 'rangeslider',
  522. 'std' => 350,
  523. 'step' => 10,
  524. 'min' => 50,
  525. 'max' => 1000,
  526. 'unit' => __( 'ms', 'popup-maker' ),
  527. 'priority' => 20,
  528. 'dependencies' => array(
  529. 'animation_type' => array( 'slide', 'fade', 'fadeAndSlide', 'grow', 'growAndSlide' ),
  530. ),
  531. ),
  532. 'animation_origin' => array(
  533. 'label' => __( 'Animation Origin', 'popup-maker' ),
  534. 'desc' => __( 'Choose where the animation will begin.', 'popup-maker' ),
  535. 'type' => 'select',
  536. 'std' => 'center top',
  537. 'options' => array(
  538. 'top' => __( 'Top', 'popup-maker' ),
  539. 'left' => __( 'Left', 'popup-maker' ),
  540. 'bottom' => __( 'Bottom', 'popup-maker' ),
  541. 'right' => __( 'Right', 'popup-maker' ),
  542. 'left top' => __( 'Top Left', 'popup-maker' ),
  543. 'center top' => __( 'Top Center', 'popup-maker' ),
  544. 'right top' => __( 'Top Right', 'popup-maker' ),
  545. 'left center' => __( 'Middle Left', 'popup-maker' ),
  546. 'center center' => __( 'Middle Center', 'popup-maker' ),
  547. 'right center' => __( 'Middle Right', 'popup-maker' ),
  548. 'left bottom' => __( 'Bottom Left', 'popup-maker' ),
  549. 'center bottom' => __( 'Bottom Center', 'popup-maker' ),
  550. 'right bottom' => __( 'Bottom Right', 'popup-maker' ),
  551. ),
  552. 'priority' => 30,
  553. 'dependencies' => array(
  554. 'animation_type' => array( 'slide', 'fadeAndSlide', 'grow', 'growAndSlide' ),
  555. ),
  556. ),
  557. ),
  558. 'sound' => array(
  559. 'open_sound' => array(
  560. 'label' => __( 'Opening Sound', 'popup-maker' ),
  561. 'desc' => __( 'Select a sound to play when the popup opens.', 'popup-maker' ),
  562. 'type' => 'select',
  563. 'std' => 'none',
  564. 'priority' => 10,
  565. 'options' => array(
  566. 'none' => __( 'None', 'popup-maker' ),
  567. 'beep.mp3' => __( 'Beep', 'popup-maker' ),
  568. 'beep-two.mp3' => __( 'Beep 2', 'popup-maker' ),
  569. 'beep-up.mp3' => __( 'Beep Up', 'popup-maker' ),
  570. 'chimes.mp3' => __( 'Chimes', 'popup-maker' ),
  571. 'correct.mp3' => __( 'Correct', 'popup-maker' ),
  572. 'custom' => __( 'Custom Sound', 'popup-maker' ),
  573. ),
  574. ),
  575. 'custom_sound' => array(
  576. 'label' => __( 'Custom Sound URL', 'popup-maker' ),
  577. 'desc' => __( 'Enter URL to sound file.', 'popup-maker' ),
  578. 'type' => 'text',
  579. 'std' => '',
  580. 'priority' => 10,
  581. 'dependencies' => array(
  582. 'open_sound' => array( 'custom' ),
  583. ),
  584. ),
  585. ),
  586. 'position' => array(
  587. 'location' => array(
  588. 'label' => __( 'Location', 'popup-maker' ),
  589. 'desc' => __( 'Choose where the popup will be displayed.', 'popup-maker' ),
  590. 'type' => 'select',
  591. 'std' => 'center top',
  592. 'priority' => 10,
  593. 'options' => array(
  594. 'left top' => __( 'Top Left', 'popup-maker' ),
  595. 'center top' => __( 'Top Center', 'popup-maker' ),
  596. 'right top' => __( 'Top Right', 'popup-maker' ),
  597. 'left center' => __( 'Middle Left', 'popup-maker' ),
  598. 'center' => __( 'Middle Center', 'popup-maker' ),
  599. 'right center' => __( 'Middle Right', 'popup-maker' ),
  600. 'left bottom' => __( 'Bottom Left', 'popup-maker' ),
  601. 'center bottom' => __( 'Bottom Center', 'popup-maker' ),
  602. 'right bottom' => __( 'Bottom Right', 'popup-maker' ),
  603. ),
  604. ),
  605. 'position_top' => array(
  606. 'label' => __( 'Top', 'popup-maker' ),
  607. 'desc' => sprintf( _x( 'Distance from the %s edge of the screen.', 'Screen Edge: top, bottom', 'popup-maker' ), strtolower( __( 'Top', 'popup-maker' ) ) ),
  608. 'type' => 'rangeslider',
  609. 'std' => 100,
  610. 'step' => 1,
  611. 'min' => 0,
  612. 'max' => 500,
  613. 'unit' => 'px',
  614. 'priority' => 20,
  615. 'dependencies' => array(
  616. 'location' => array( 'left top', 'center top', 'right top' ),
  617. ),
  618. ),
  619. 'position_bottom' => array(
  620. 'label' => __( 'Bottom', 'popup-maker' ),
  621. 'desc' => sprintf( _x( 'Distance from the %s edge of the screen.', 'Screen Edge: top, bottom', 'popup-maker' ), strtolower( __( 'Bottom', 'popup-maker' ) ) ),
  622. 'type' => 'rangeslider',
  623. 'std' => 0,
  624. 'step' => 1,
  625. 'min' => 0,
  626. 'max' => 500,
  627. 'unit' => 'px',
  628. 'priority' => 20,
  629. 'dependencies' => array(
  630. 'location' => array( 'left bottom', 'center bottom', 'right bottom' ),
  631. ),
  632. ),
  633. 'position_left' => array(
  634. 'label' => __( 'Left', 'popup-maker' ),
  635. 'desc' => sprintf( _x( 'Distance from the %s edge of the screen.', 'Screen Edge: top, bottom', 'popup-maker' ), strtolower( __( 'Left', 'popup-maker' ) ) ),
  636. 'type' => 'rangeslider',
  637. 'std' => 0,
  638. 'step' => 1,
  639. 'min' => 0,
  640. 'max' => 500,
  641. 'unit' => 'px',
  642. 'priority' => 30,
  643. 'dependencies' => array(
  644. 'location' => array( 'left top', 'left center', 'left bottom' ),
  645. ),
  646. ),
  647. 'position_right' => array(
  648. 'label' => __( 'Right', 'popup-maker' ),
  649. 'desc' => sprintf( _x( 'Distance from the %s edge of the screen.', 'Screen Edge: top, bottom', 'popup-maker' ), strtolower( __( 'Right', 'popup-maker' ) ) ),
  650. 'type' => 'rangeslider',
  651. 'std' => 0,
  652. 'step' => 1,
  653. 'min' => 0,
  654. 'max' => 500,
  655. 'unit' => 'px',
  656. 'priority' => 30,
  657. 'dependencies' => array(
  658. 'location' => array( 'right top', 'right center', 'right bottom' ),
  659. ),
  660. ),
  661. 'position_from_trigger' => array(
  662. 'label' => __( 'Position from Trigger', 'popup-maker' ),
  663. 'desc' => sprintf( __( 'This will position the popup in relation to the %sClick Trigger%s.', 'popup-maker' ), '<a target="_blank" href="https://docs.wppopupmaker.com/article/395-trigger-click-open-overview-methods?utm_campaign=contextual-help&utm_medium=inline-doclink&utm_source=plugin-popup-editor&utm_content=position-from-trigger">', '</a>' ),
  664. 'type' => 'checkbox',
  665. 'std' => false,
  666. 'priority' => 40,
  667. ),
  668. 'position_fixed' => array(
  669. 'label' => __( 'Fixed Postioning', 'popup-maker' ),
  670. 'desc' => __( 'Checking this sets the positioning of the popup to fixed.', 'popup-maker' ),
  671. 'type' => 'checkbox',
  672. 'priority' => 50,
  673. ),
  674. ),
  675. 'advanced' => array(
  676. 'overlay_disabled' => array(
  677. 'label' => __( 'Disable Overlay', 'popup-maker' ),
  678. 'desc' => __( 'Checking this will disable and hide the overlay for this popup.', 'popup-maker' ),
  679. 'type' => 'checkbox',
  680. 'priority' => 10,
  681. ),
  682. 'stackable' => array(
  683. 'label' => __( 'Stackable', 'popup-maker' ),
  684. 'desc' => __( 'This enables other popups to remain open.', 'popup-maker' ),
  685. 'type' => 'checkbox',
  686. 'priority' => 20,
  687. ),
  688. 'disable_reposition' => array(
  689. 'label' => __( 'Disable Repositioning', 'popup-maker' ),
  690. 'desc' => __( 'This will disable automatic repositioning of the popup on window resizing.', 'popup-maker' ),
  691. 'type' => 'checkbox',
  692. 'priority' => 30,
  693. ),
  694. 'zindex' => array(
  695. 'label' => __( 'Popup Z-Index', 'popup-maker' ),
  696. 'desc' => __( 'Change the z-index layer level for the popup.', 'popup-maker' ),
  697. 'type' => 'number',
  698. 'min' => 999,
  699. 'max' => 2147483647,
  700. 'std' => 1999999999,
  701. 'priority' => 40,
  702. ),
  703. ),
  704. ) ),
  705. 'close' => apply_filters( 'pum_popup_close_settings_fields', array(
  706. 'button' => array(
  707. 'close_text' => array(
  708. 'label' => __( 'Close Text', 'popup-maker' ),
  709. 'placeholder' => __( 'Close', 'popup-maker' ),
  710. 'desc' => __( 'Override the default close text. To use a Font Awesome icon instead of text, enter the CSS classes such as "fas fa-camera".', 'popup-maker' ),
  711. 'priority' => 10,
  712. 'private' => true,
  713. ),
  714. 'close_button_delay' => array(
  715. 'label' => __( 'Close Button Delay', 'popup-maker' ),
  716. 'desc' => __( 'This delays the display of the close button.', 'popup-maker' ),
  717. 'type' => 'rangeslider',
  718. 'std' => 0,
  719. 'step' => 100,
  720. 'min' => 0,
  721. 'max' => 3000,
  722. 'unit' => __( 'ms', 'popup-maker' ),
  723. 'priority' => 20,
  724. ),
  725. ),
  726. 'forms' => [
  727. 'close_on_form_submission' => [
  728. 'label' => __( 'Close on Form Submission', 'popup-maker' ),
  729. 'desc' => __( 'Close the popup automatically after integrated form plugin submissions.', 'popup-maker' ),
  730. 'type' => 'checkbox',
  731. ],
  732. 'close_on_form_submission_delay' => [
  733. 'type' => 'rangeslider',
  734. 'label' => __( 'Delay', 'popup-maker' ),
  735. 'desc' => __( 'The delay before the popup will close after submission (in milliseconds).', 'popup-maker' ),
  736. 'std' => 0,
  737. 'min' => 0,
  738. 'max' => 10000,
  739. 'step' => 500,
  740. 'unit' => 'ms',
  741. 'dependencies' => [
  742. 'close_on_form_submission' => true,
  743. ],
  744. ],
  745. ],
  746. 'alternate_methods' => array(
  747. 'close_on_overlay_click' => array(
  748. 'label' => __( 'Click Overlay to Close', 'popup-maker' ),
  749. 'desc' => __( 'Checking this will cause popup to close when user clicks on overlay.', 'popup-maker' ),
  750. 'type' => 'checkbox',
  751. 'priority' => 10,
  752. ),
  753. 'close_on_esc_press' => array(
  754. 'label' => __( 'Press ESC to Close', 'popup-maker' ),
  755. 'desc' => __( 'Checking this will cause popup to close when user presses ESC key.', 'popup-maker' ),
  756. 'type' => 'checkbox',
  757. 'priority' => 20,
  758. ),
  759. 'close_on_f4_press' => array(
  760. 'label' => __( 'Press F4 to Close', 'popup-maker' ),
  761. 'desc' => __( 'Checking this will cause popup to close when user presses F4 key.', 'popup-maker' ),
  762. 'type' => 'checkbox',
  763. 'priority' => 30,
  764. ),
  765. ),
  766. ) ),
  767. 'advanced' => apply_filters( 'pum_popup_advanced_settings_fields', array(
  768. 'main' => array(
  769. 'disable_form_reopen' => array(
  770. 'label' => __( 'Disable automatic re-triggering of popup after non-ajax form submission.', 'popup-maker' ),
  771. 'type' => 'checkbox',
  772. 'priority' => 10,
  773. ),
  774. 'disable_accessibility' => array(
  775. 'label' => __( 'Disable accessibility features.', 'popup-maker' ),
  776. 'desc' => __( 'This includes trapping the tab key & focus inside popup while open, force focus the first element when popup open, and refocus last click trigger when closed.', 'popup-maker' ),
  777. 'type' => 'checkbox',
  778. 'priority' => 10,
  779. ),
  780. ),
  781. ) ),
  782. ) );
  783. $tabs = PUM_Admin_Helpers::parse_tab_fields(
  784. $tabs,
  785. [
  786. 'has_subtabs' => true,
  787. 'name' => 'popup_settings[%s]',
  788. ]
  789. );
  790. }
  791. return $tabs;
  792. }
  793. public static function get_field( $id ) {
  794. $tabs = self::fields();
  795. foreach ( $tabs as $tab => $sections ) {
  796. if ( PUM_Admin_Helpers::is_field( $sections ) ) {
  797. $sections = array(
  798. 'main' => array(
  799. $tab => $sections,
  800. ),
  801. );
  802. }
  803. foreach ( $sections as $section => $fields ) {
  804. foreach ( $fields as $key => $args ) {
  805. if ( $key == $id ) {
  806. return $args;
  807. }
  808. }
  809. }
  810. }
  811. return false;
  812. }
  813. /**
  814. * Sanitizes fields after submission.
  815. *
  816. * Also handles pre save manipulations for some field types (measure/license).
  817. *
  818. * @param array $settings
  819. *
  820. * @return array
  821. */
  822. public static function sanitize_settings( $settings = array() ) {
  823. $fields = self::fields();
  824. $fields = PUM_Admin_Helpers::flatten_fields_array( $fields );
  825. foreach ( $fields as $field_id => $field ) {
  826. switch ( $field['type'] ) {
  827. case 'checkbox':
  828. if ( ! isset( $settings[ $field_id ] ) ) {
  829. $settings[ $field_id ] = false;
  830. }
  831. break;
  832. }
  833. }
  834. foreach ( $settings as $key => $value ) {
  835. $field = self::get_field( $key );
  836. if ( $field ) {
  837. // Sanitize every string value.
  838. if ( is_string( $value ) ) {
  839. $settings[ $key ] = sanitize_text_field( $value );
  840. }
  841. switch ( $field['type'] ) {
  842. default:
  843. $settings[ $key ] = is_string( $value ) ? trim( $value ) : $value;
  844. break;
  845. case 'measure':
  846. $settings[ $key ] .= $settings[ $key . '_unit' ];
  847. break;
  848. }
  849. } else {
  850. // Some custom field types include multiple additional fields that do not need to be saved, strip out any non-whitelisted fields.
  851. unset( $settings[ $key ] );
  852. }
  853. }
  854. return $settings;
  855. }
  856. /**
  857. * @return array
  858. */
  859. public static function defaults() {
  860. $tabs = self::fields();
  861. $defaults = array();
  862. foreach ( $tabs as $tab_id => $sections ) {
  863. foreach ( $sections as $section_id => $fields ) {
  864. foreach ( $fields as $key => $field ) {
  865. $defaults[ $key ] = isset( $field['std'] ) ? $field['std'] : ( $field['type'] == 'checkbox' ? false : null );
  866. }
  867. }
  868. }
  869. return $defaults;
  870. }
  871. /**
  872. * Fills default settings only when missing.
  873. *
  874. * Excludes checkbox type fields where a false value is represented by the field being unset.
  875. *
  876. * @param array $settings
  877. *
  878. * @return array
  879. */
  880. public static function fill_missing_defaults( $settings = [] ) {
  881. $excluded_field_types = [ 'checkbox', 'multicheck' ];
  882. $defaults = self::defaults();
  883. foreach ( $defaults as $field_id => $default_value ) {
  884. $field = self::get_field( $field_id );
  885. if ( isset( $settings[ $field_id ] ) || in_array( $field['type'], $excluded_field_types ) ) {
  886. continue;
  887. }
  888. $settings[ $field_id ] = $default_value;
  889. }
  890. return $settings;
  891. }
  892. /**
  893. * Display analytics metabox
  894. *
  895. * @return void
  896. */
  897. public static function render_analytics_meta_box() {
  898. global $post;
  899. $popup = pum_get_popup( $post->ID );
  900. ?>
  901. <div id="pum-popup-analytics" class="pum-meta-box">
  902. <?php do_action( 'pum_popup_analytics_metabox_before', $post->ID ); ?>
  903. <?php
  904. $opens = $popup->get_event_count( 'open' );
  905. $conversions = $popup->get_event_count( 'conversion' );
  906. $conversion_rate = $opens > 0 && $opens >= $conversions ? $conversions / $opens * 100 : 0;
  907. ?>
  908. <div id="pum-popup-analytics" class="pum-popup-analytics">
  909. <table class="form-table">
  910. <tbody>
  911. <tr>
  912. <td><?php esc_html_e( 'Opens', 'popup-maker' ); ?></td>
  913. <td><?php echo esc_html( $opens ); ?></td>
  914. </tr>
  915. <tr>
  916. <td><?php esc_html_e( 'Conversions', 'popup-maker' ); ?></td>
  917. <td><?php echo esc_html( $conversions ); ?></td>
  918. </tr>
  919. <tr>
  920. <td><?php esc_html_e( 'Conversion Rate', 'popup-maker' ); ?></td>
  921. <td><?php echo esc_html( round( $conversion_rate, 2 ) ); ?>%</td>
  922. </tr>
  923. <tr class="separator">
  924. <td colspan="2">
  925. <label> <input type="checkbox" name="popup_reset_counts" id="popup_reset_counts" value="1" />
  926. <?php esc_html_e( 'Reset Counts', 'popup-maker' ); ?>
  927. </label>
  928. <?php if ( ( $reset = $popup->get_last_count_reset() ) ) : ?><br />
  929. <small>
  930. <strong><?php esc_html_e( 'Last Reset', 'popup-maker' ); ?>:</strong> <?php echo esc_html( date( 'm-d-Y H:i', $reset['timestamp'] ) ); ?>
  931. <br /> <strong><?php esc_html_e( 'Previous Opens', 'popup-maker' ); ?>:</strong> <?php echo esc_html( $reset['opens'] ); ?>
  932. <?php if ( $reset['conversions'] > 0 ) : ?>
  933. <br />
  934. <strong><?php esc_html_e( 'Previous Conversions', 'popup-maker' ); ?>:</strong> <?php echo esc_html( $reset['conversions'] ); ?>
  935. <?php endif; ?>
  936. <br /> <strong><?php esc_html_e( 'Lifetime Opens', 'popup-maker' ); ?>:</strong> <?php echo esc_html( $popup->get_event_count( 'open', 'total' ) ); ?>
  937. <?php if ( $popup->get_event_count( 'conversion', 'total' ) > 0 ) : ?>
  938. <br />
  939. <strong><?php esc_html_e( 'Lifetime Conversions', 'popup-maker' ); ?>:</strong> <?php echo esc_html( $popup->get_event_count( 'conversion', 'total' ) ); ?>
  940. <?php endif; ?>
  941. </small>
  942. <?php endif; ?>
  943. </td>
  944. </tr>
  945. </tbody>
  946. </table>
  947. </div>
  948. <?php do_action( 'pum_popup_analytics_metabox_after', $post->ID ); ?>
  949. </div>
  950. <?php
  951. }
  952. /**
  953. * @param array $meta
  954. *
  955. * @return array
  956. */
  957. public static function sanitize_meta( $meta = array() ) {
  958. if ( ! empty( $meta ) ) {
  959. foreach ( $meta as $key => $value ) {
  960. if ( is_array( $value ) ) {
  961. $meta[ $key ] = self::sanitize_meta( $value );
  962. } else if ( is_string( $value ) ) {
  963. try {
  964. $value = json_decode( stripslashes( $value ) );
  965. if ( is_object( $value ) || is_array( $value ) ) {
  966. $meta[ $key ] = PUM_Admin_Helpers::object_to_array( $value );
  967. }
  968. } catch ( Exception $e ) {
  969. }
  970. }
  971. }
  972. }
  973. return $meta;
  974. }
  975. /**
  976. * Ensures that the popups have unique slugs.
  977. *
  978. * @param $data
  979. * @param $postarr
  980. *
  981. * @return mixed
  982. */
  983. public static function set_slug( $data, $postarr ) {
  984. if ( $data['post_type'] == 'popup' ) {
  985. $data['post_name'] = wp_unique_post_slug( sanitize_title( popmake_post( 'popup_name' ) ), $postarr['ID'], $data['post_status'], $data['post_type'], $data['post_parent'] );
  986. }
  987. return $data;
  988. }
  989. /**
  990. * Defines the custom columns and their order
  991. *
  992. * @param array $_columns Array of popup columns
  993. *
  994. * @return array $columns Updated array of popup columns for
  995. * Post Type List Table
  996. */
  997. public static function dashboard_columns( $_columns ) {
  998. wp_enqueue_style( 'pum-admin-general' );
  999. $columns = array(
  1000. 'cb' => '<input type="checkbox"/>',
  1001. 'title' => __( 'Name', 'popup-maker' ),
  1002. 'enabled' => __( 'Enabled', 'popup-maker' ),
  1003. 'popup_title' => __( 'Title', 'popup-maker' ),
  1004. 'class' => __( 'CSS Class', 'popup-maker' ),
  1005. 'opens' => __( 'Opens', 'popup-maker' ),
  1006. 'conversions' => __( 'Conversions', 'popup-maker' ),
  1007. 'conversion_rate' => __( 'Conversion Rate', 'popup-maker' ),
  1008. );
  1009. // Add the date column preventing our own translation.
  1010. if ( ! empty( $_columns['date'] ) ) {
  1011. $columns['date'] = $_columns['date'];
  1012. }
  1013. if ( get_taxonomy( 'popup_tag' ) ) {
  1014. $columns['popup_tag'] = __( 'Tags', 'popup-maker' );
  1015. }
  1016. if ( get_taxonomy( 'popup_category' ) ) {
  1017. $columns['popup_category'] = __( 'Categories', 'popup-maker' );
  1018. }
  1019. // Deprecated filter.
  1020. $columns = apply_filters( 'popmake_popup_columns', $columns );
  1021. return apply_filters( 'pum_popup_columns', $columns );
  1022. }
  1023. /**
  1024. * Render Columns
  1025. *
  1026. * @param string $column_name Column name
  1027. * @param int $post_id (Post) ID
  1028. */
  1029. public static function render_columns( $column_name, $post_id ) {
  1030. $post = get_post( $post_id );
  1031. if ( 'popup' === $post->post_type ) {
  1032. $popup = pum_get_popup( $post_id );
  1033. /**
  1034. * Uncomment if need to check for permissions on certain columns.
  1035. * *
  1036. * $post_type_object = get_post_type_object( $popup->post_type );
  1037. * $can_edit_post = current_user_can( $post_type_object->cap->edit_post, $popup->ID );
  1038. */
  1039. switch ( $column_name ) {
  1040. case 'popup_title':
  1041. echo esc_html( $popup->get_title() );
  1042. break;
  1043. case 'enabled':
  1044. if ( 'publish' === $post->post_status ) {
  1045. $enabled = $popup->is_enabled();
  1046. $nonce = wp_create_nonce( "pum_save_enabled_state_{$popup->ID}" );
  1047. ?>
  1048. <div class="pum-toggle-button">
  1049. <input id="pum-enabled-toggle-<?php echo esc_attr( $popup->ID ); ?>" type="checkbox" <?php checked( true, $enabled ); ?> class="pum-enabled-toggle-button" data-nonce="<?php echo esc_attr( $nonce ); ?>" data-popup-id="<?php echo esc_attr( $popup->ID ); ?>">
  1050. <label for="pum-enabled-toggle-<?php echo esc_attr( $popup->ID ); ?>" aria-label="Switch to enable popup"></label>
  1051. </div>
  1052. <?php
  1053. } else {
  1054. ?>
  1055. <p>Popup not published</p>
  1056. <?php
  1057. }
  1058. break;
  1059. case 'popup_category':
  1060. echo get_the_term_list( $post_id, 'popup_category', '', ', ', '' );
  1061. break;
  1062. case 'popup_tag':
  1063. echo get_the_term_list( $post_id, 'popup_tag', '', ', ', '' );
  1064. break;
  1065. case 'class':
  1066. echo '<pre style="display:inline-block;margin:0;"><code>popmake-' . absint( $post_id ) . '</code></pre>';
  1067. break;
  1068. case 'opens':
  1069. if ( ! pum_extension_enabled( 'popup-analytics' ) ) {
  1070. echo esc_html( $popup->get_event_count( 'open' ) );
  1071. }
  1072. break;
  1073. case 'conversions':
  1074. if ( ! pum_extension_enabled( 'popup-analytics' ) ) {
  1075. echo esc_html( $popup->get_event_count( 'conversion' ) );
  1076. }
  1077. break;
  1078. case 'conversion_rate':
  1079. if ( ! pum_extension_enabled( 'popup-analytics' ) ) {
  1080. $opens = $popup->get_event_count( 'open' );
  1081. $conversions = $popup->get_event_count( 'conversion' );
  1082. if ( $opens > 0 && $opens >= $conversions ) {
  1083. $conversion_rate = round( $conversions / $opens * 100, 2 );
  1084. } else {
  1085. $conversion_rate = 0;
  1086. }
  1087. echo esc_html( $conversion_rate . '%' );
  1088. }
  1089. break;
  1090. }
  1091. }
  1092. }
  1093. /**
  1094. * Hide some of our columns by default
  1095. *
  1096. * @param array $hidden Array of IDs of columns hidden by default.
  1097. * @param WP_Screen $screen WP_Screen object of the current screen.
  1098. * @return array Updated $hidden
  1099. */
  1100. public static function hide_columns( $hidden, $screen ) {
  1101. if ( isset( $screen->id ) && 'edit-popup' === $screen->id ) {
  1102. $hidden[] = 'popup_title';
  1103. $hidden[] = 'date';
  1104. }
  1105. return $hidden;
  1106. }
  1107. /**
  1108. * Registers the sortable columns in the list table
  1109. *
  1110. * @param array $columns Array of the columns
  1111. *
  1112. * @return array $columns Array of sortable columns
  1113. */
  1114. public static function sortable_columns( $columns ) {
  1115. $columns['popup_title'] = 'popup_title';
  1116. $columns['opens'] = 'opens';
  1117. $columns['conversions'] = 'conversions';
  1118. return $columns;
  1119. }
  1120. /**
  1121. * Sorts Columns in the List Table
  1122. *
  1123. * @param array $vars Array of all the sort variables
  1124. *
  1125. * @return array $vars Array of all the sort variables
  1126. */
  1127. public static function sort_columns( $vars ) {
  1128. // Check if we're viewing the "popup" post type
  1129. if ( isset( $vars['post_type'] ) && 'popup' == $vars['post_type'] ) {
  1130. // Check if 'orderby' is set to "name"
  1131. if ( isset( $vars['orderby'] ) ) {
  1132. switch ( $vars['orderby'] ) {
  1133. case 'popup_title':
  1134. $vars = array_merge( $vars, array(
  1135. 'meta_key' => 'popup_title',
  1136. 'orderby' => 'meta_value',
  1137. ) );
  1138. break;
  1139. case 'opens':
  1140. if ( ! pum_extension_enabled( 'popup-analytics' ) ) {
  1141. $vars = array_merge( $vars, array(
  1142. 'meta_key' => 'popup_open_count',
  1143. 'orderby' => 'meta_value_num',
  1144. ) );
  1145. }
  1146. break;
  1147. case 'conversions':
  1148. if ( ! pum_extension_enabled( 'popup-analytics' ) ) {
  1149. $vars = array_merge( $vars, array(
  1150. 'meta_key' => 'popup_conversion_count',
  1151. 'orderby' => 'meta_value_num',
  1152. ) );
  1153. }
  1154. break;
  1155. }
  1156. }
  1157. }
  1158. return $vars;
  1159. }
  1160. /**
  1161. * Initialize sorting
  1162. */
  1163. public static function load() {
  1164. add_filter( 'request', array( __CLASS__, 'sort_columns' ) );
  1165. }
  1166. /**
  1167. * Add Popup Filters
  1168. *
  1169. * Adds taxonomy drop down filters for popups.
  1170. */
  1171. public static function add_popup_filters() {
  1172. global $typenow;
  1173. // Checks if the current post type is 'popup'
  1174. if ( $typenow == 'popup' ) {
  1175. if ( get_taxonomy( 'popup_category' ) ) {
  1176. $terms = get_terms( 'popup_category' );
  1177. if ( count( $terms ) > 0 ) {
  1178. echo "<select name='popup_category' id='popup_category' class='postform'>";
  1179. echo "<option value=''>" . __( 'Show all categories', 'popup-maker' ) . "</option>";
  1180. foreach ( $terms as $term ) {
  1181. $selected = isset( $_GET['popup_category'] ) && $_GET['popup_category'] == $term->slug ? 'selected="selected"' : '';
  1182. echo '<option value="' . esc_attr( $term->slug ) . '" ' . $selected . '>' . esc_html( $term->name ) . ' (' . $term->count . ')</option>';
  1183. }
  1184. echo "</select>";
  1185. }
  1186. }
  1187. if ( get_taxonomy( 'popup_tag' ) ) {
  1188. $terms = get_terms( 'popup_tag' );
  1189. if ( count( $terms ) > 0 ) {
  1190. echo "<select name='popup_tag' id='popup_tag' class='postform'>";
  1191. echo "<option value=''>" . __( 'Show all tags', 'popup-maker' ) . "</option>";
  1192. foreach ( $terms as $term ) {
  1193. $selected = isset( $_GET['popup_tag'] ) && $_GET['popup_tag'] == $term->slug ? 'selected="selected"' : '';
  1194. echo '<option value="' . esc_attr( $term->slug ) . '" ' . $selected . '>' . esc_html( $term->name ) . ' (' . $term->count . ')</option>';
  1195. }
  1196. echo "</select>";
  1197. }
  1198. }
  1199. }
  1200. }
  1201. /**
  1202. * Prepends Popup ID to the action row on All Popups
  1203. * @param array $actions The row actions.
  1204. * @param $post The post
  1205. *
  1206. * @return array The new actions.
  1207. */
  1208. public static function add_id_row_actions( $actions, $post ) {
  1209. // Only adjust if we are dealing with our popups.
  1210. if ( 'popup' === $post->post_type ) {
  1211. return array_merge( array( 'id' => 'ID: ' . $post->ID ), $actions );
  1212. }
  1213. return $actions;
  1214. }
  1215. }