Keine Beschreibung

class-pum-cf7.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_CF7_Integration
  10. */
  11. class PUM_CF7_Integration {
  12. /**
  13. * Initialize if CF7 is active.
  14. */
  15. public static function init() {
  16. add_filter( 'pum_get_cookies', array( __CLASS__, 'register_cookies' ) );
  17. add_filter( 'wpcf7_editor_panels', array( __CLASS__, 'editor_panels' ) );
  18. add_action( 'wpcf7_after_save', array( __CLASS__, 'save' ) );
  19. add_filter( 'wpcf7_form_elements', array( __CLASS__, 'form_elements' ) );
  20. add_action( 'popmake_preload_popup', array( __CLASS__, 'preload' ) );
  21. }
  22. /**
  23. * Check if the popups use CF7 Forms and force enqueue their assets.
  24. *
  25. * @param $popup_id
  26. */
  27. public static function preload( $popup_id ) {
  28. $popup = pum_get_popup( $popup_id );
  29. if ( has_shortcode( $popup->post_content, 'contact-form-7' ) ) {
  30. if ( defined( 'WPCF7_LOAD_JS' ) && ! WPCF7_LOAD_JS ) {
  31. return;
  32. }
  33. if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
  34. wpcf7_enqueue_scripts();
  35. }
  36. if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
  37. wpcf7_enqueue_styles();
  38. }
  39. }
  40. }
  41. /**
  42. * Append a hidden meta html element with the forms popup settings.
  43. *
  44. * @param $elements
  45. *
  46. * @return string
  47. */
  48. public static function form_elements( $elements ) {
  49. $form = wpcf7_get_current_contact_form();
  50. $settings = wp_json_encode( self::form_options( $form->id() ) );
  51. return $elements . "<input type='hidden' class='wpcf7-pum' value='$settings' />";
  52. }
  53. /**
  54. * Get a specific forms options.
  55. *
  56. * @param $id
  57. *
  58. * @return array
  59. */
  60. public static function form_options( $id ) {
  61. $settings = get_option( 'cf7_pum_' . $id, self::defaults() );
  62. return wp_parse_args( $settings, self::defaults() );
  63. }
  64. /**
  65. * Get default values.
  66. *
  67. * @return array
  68. */
  69. public static function defaults() {
  70. return array(
  71. 'closepopup' => false,
  72. 'closedelay' => 0,
  73. 'openpopup' => false,
  74. 'openpopup_id' => 0,
  75. );
  76. }
  77. /**
  78. * Registers new cookie events.
  79. *
  80. * @param array $cookies
  81. *
  82. * @return array
  83. */
  84. public static function register_cookies( $cookies = array() ) {
  85. $cookies['cf7_form_success'] = array(
  86. 'labels' => array(
  87. 'name' => __( 'Contact Form 7 Success (deprecated. Use Form Submission instead.)', 'popup-maker' ),
  88. ),
  89. 'fields' => pum_get_cookie_fields(),
  90. );
  91. return $cookies;
  92. }
  93. /**
  94. * Register new CF7 form editor tab.
  95. *
  96. * @param array $panels
  97. *
  98. * @return array
  99. */
  100. public static function editor_panels( $panels = array() ) {
  101. return array_merge( $panels, array(
  102. 'popups' => array(
  103. 'title' => __( 'Popup Settings', 'popup-maker' ),
  104. 'callback' => array( __CLASS__, 'editor_panel' ),
  105. ),
  106. ) );
  107. }
  108. /**
  109. * Render the popup tab.
  110. *
  111. * @param object $args
  112. */
  113. public static function editor_panel( $args ) {
  114. $settings = self::form_options( $args->id() ); ?>
  115. <h2><?php _e( 'Popup Settings', 'popup-maker' ); ?></h2>
  116. <p class="description"><?php _e( 'These settings control popups after successful form submissions.', 'popup-maker' ); ?></p>
  117. <table class="form-table">
  118. <tbody>
  119. <tr>
  120. <th scope="row">
  121. <label for="wpcf7-pum-closepopup"><?php _e( 'Close Popup', 'popup-maker' ); ?></label>
  122. </th>
  123. <td>
  124. <input type="checkbox" id="wpcf7-pum-closepopup" name="wpcf7-pum[closepopup]" value="true" <?php checked( $settings['closepopup'], true ); ?> />
  125. </td>
  126. </tr>
  127. <tr id="wpcf7-pum-closedelay-wrapper">
  128. <th scope="row">
  129. <label for="wpcf7-pum-closedelay"><?php _e( 'Delay', 'popup-maker' ); ?></label>
  130. </th>
  131. <td>
  132. <?php if ( strlen( $settings['closedelay'] ) >= 3 ) {
  133. $settings['closedelay'] = $settings['closedelay'] / 1000;
  134. } ?>
  135. <input type="number" id="wpcf7-pum-closedelay" min="0" step="1" name="wpcf7-pum[closedelay]" style="width: 100px;" value="<?php echo esc_attr( $settings['closedelay'] ); ?>" /><?php _e( 'seconds', 'popup-maker' ); ?>
  136. </td>
  137. </tr>
  138. <tr>
  139. <th scope="row">
  140. <label for="wpcf7-pum-openpopup"><?php _e( 'Open Popup', 'popup-maker' ); ?></label>
  141. </th>
  142. <td>
  143. <input type="checkbox" id="wpcf7-pum-openpopup" name="wpcf7-pum[openpopup]" value="true" <?php checked( $settings['openpopup'], true ); ?> />
  144. </td>
  145. </tr>
  146. <tr id="wpcf7-pum-openpopup_id-wrapper">
  147. <th scope="row">
  148. <label for="wpcf7-pum-openpopup_id"><?php _e( 'Popup', 'popup-maker' ); ?></label>
  149. </th>
  150. <td>
  151. <select id="wpcf7-pum-openpopup_id" name="wpcf7-pum[openpopup_id]">
  152. <?php foreach ( self::get_popup_list() as $option ) { ?>
  153. <option value="<?php echo esc_attr( $option['value'] ); ?>" <?php selected( $settings['openpopup_id'], $option['value'] ); ?>><?php echo $option['label']; ?></option>
  154. <?php } ?>
  155. </select>
  156. </td>
  157. </tr>
  158. </tbody>
  159. </table>
  160. <script>
  161. (function ($) {
  162. var $open = $('#wpcf7-pum-openpopup'),
  163. $close = $('#wpcf7-pum-closepopup'),
  164. $popup_id_wrapper = $('#wpcf7-pum-openpopup_id-wrapper'),
  165. $delay_wrapper = $('#wpcf7-pum-closedelay-wrapper');
  166. function check_open() {
  167. if ($open.is(':checked')) {
  168. $popup_id_wrapper.show();
  169. } else {
  170. $popup_id_wrapper.hide();
  171. }
  172. }
  173. function check_close() {
  174. if ($close.is(':checked')) {
  175. $delay_wrapper.show();
  176. } else {
  177. $delay_wrapper.hide();
  178. }
  179. }
  180. check_open();
  181. check_close();
  182. $open.on('click', check_open);
  183. $close.on('click', check_close);
  184. }(jQuery));
  185. </script>
  186. <?php
  187. }
  188. /**
  189. * Get a list of popups for a select box.
  190. *
  191. * @return array
  192. */
  193. public static function get_popup_list() {
  194. $popup_list = array(
  195. array(
  196. 'value' => 0,
  197. 'label' => __( 'Select a popup', 'popup-maker' ),
  198. ),
  199. );
  200. $popups = get_posts( array(
  201. 'post_type' => 'popup',
  202. 'post_status' => array( 'publish' ),
  203. 'posts_per_page' => - 1,
  204. ) );
  205. foreach ( $popups as $popup ) {
  206. $popup_list[] = array(
  207. 'value' => $popup->ID,
  208. 'label' => $popup->post_title,
  209. );
  210. }
  211. return $popup_list;
  212. }
  213. /**
  214. * Save form popup options.
  215. *
  216. * @param $args
  217. */
  218. public static function save( $args ) {
  219. if ( ! empty( $_POST['wpcf7-pum'] ) ) {
  220. $settings = $_POST['wpcf7-pum'];
  221. // Sanitize values.
  222. $settings['openpopup'] = ! empty( $settings['openpopup'] );
  223. $settings['openpopup_id'] = ! empty( $settings['openpopup_id'] ) ? absint( $settings['openpopup_id'] ) : 0;
  224. $settings['closepopup'] = ! empty( $settings['closepopup'] );
  225. $settings['closedelay'] = ! empty( $settings['closedelay'] ) ? absint( $settings['closedelay'] ) : 0;
  226. update_option( 'cf7_pum_' . $args->id(), $settings );
  227. } else {
  228. delete_option( 'cf7_pum_' . $args->id() );
  229. }
  230. }
  231. }