Brak opisu

class-pum-gravity-forms.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. class PUM_Gravity_Forms_Integation {
  3. public static function init() {
  4. add_filter( 'gform_form_settings_menu', array( __CLASS__, 'settings_menu' ) );
  5. add_action( 'gform_form_settings_page_popup-maker', array( __CLASS__, 'render_settings_page' ) );
  6. add_filter( 'pum_get_cookies', array( __CLASS__, 'register_cookies' ) );
  7. add_filter( 'gform_get_form_filter', array( __CLASS__, 'get_form' ), 10, 2 );
  8. add_action( 'popmake_preload_popup', array( __CLASS__, 'preload' ) );
  9. add_action( 'popmake_popup_before_inner', array( __CLASS__, 'force_ajax' ) );
  10. add_action( 'popmake_popup_after_inner', array( __CLASS__, 'force_ajax' ) );
  11. }
  12. public static function force_ajax() {
  13. if ( current_action() == 'popmake_popup_before_inner' ) {
  14. add_filter( 'shortcode_atts_gravityforms', array( __CLASS__, 'gfrorms_shortcode_atts' ) );
  15. }
  16. if ( current_action() == 'popmake_popup_after_inner' ) {
  17. remove_filter( 'shortcode_atts_gravityforms', array( __CLASS__, 'gfrorms_shortcode_atts' ) );
  18. }
  19. }
  20. public static function gfrorms_shortcode_atts( $out ) {
  21. $out['ajax'] = 'true';
  22. return $out;
  23. }
  24. public static function preload( $popup_id ) {
  25. if ( function_exists( 'gravity_form_enqueue_scripts' ) ) {
  26. $popup = pum_get_popup( $popup_id );
  27. if ( has_shortcode( $popup->post_content, 'gravityform' ) ) {
  28. $regex = "/\[gravityform.*id=[\'\"]?([0-9]*)[\'\"]?.*/";
  29. $popup = get_post( $popup_id );
  30. preg_match_all( $regex, $popup->post_content, $matches );
  31. foreach ( $matches[1] as $form_id ) {
  32. add_filter( "gform_confirmation_anchor_{$form_id}", '__return_false' );
  33. gravity_form_enqueue_scripts( $form_id, true );
  34. }
  35. }
  36. }
  37. }
  38. public static function settings_menu( $setting_tabs ) {
  39. $setting_tabs['998.002'] = array(
  40. 'name' => 'popup-maker',
  41. 'label' => __( 'Popup Maker', 'popup-maker' ),
  42. );
  43. return $setting_tabs;
  44. }
  45. public static function get_form( $form_string, $form ) {
  46. $settings = wp_json_encode( self::form_options( $form['id'] ) );
  47. $field = "<input type='hidden' class='gforms-pum' value='$settings' />";
  48. $form_string = preg_replace( '/(<form.*>)/', "$1 \r\n " . $field, $form_string );
  49. return $form_string;
  50. }
  51. /**
  52. * Get default values.
  53. *
  54. * @return array
  55. */
  56. public static function defaults() {
  57. return array(
  58. 'closepopup' => false,
  59. 'closedelay' => 0,
  60. 'openpopup' => false,
  61. 'openpopup_id' => 0,
  62. );
  63. }
  64. /**
  65. * Get a specific forms options.
  66. *
  67. * @param $id
  68. *
  69. * @return array
  70. */
  71. public static function form_options( $id ) {
  72. $settings = get_option( 'gforms_pum_' . $id, self::defaults() );
  73. return wp_parse_args( $settings, self::defaults() );
  74. }
  75. /**
  76. * Registers new cookie events.
  77. *
  78. * @param array $cookies
  79. *
  80. * @return array
  81. */
  82. public static function register_cookies( $cookies = array() ) {
  83. $cookies['gforms_form_success'] = array(
  84. 'labels' => array(
  85. 'name' => __( 'Gravity Form Success (deprecated. Use Form Submission instead.)', 'popup-maker' ),
  86. ),
  87. 'fields' => pum_get_cookie_fields(),
  88. );
  89. return $cookies;
  90. }
  91. public static function render_settings_page() {
  92. $form_id = rgget( 'id' );
  93. self::save();
  94. $settings = self::form_options( $form_id );
  95. GFFormSettings::page_header( __( 'Popup Settings', 'popup-maker' ) );
  96. ?>
  97. <div id="popup_settings-editor">
  98. <form id="popup_settings_edit_form" method="post">
  99. <table class="form-table gforms_form_settings">
  100. <tr>
  101. <th scope="row">
  102. <label for="gforms-pum-closepopup"><?php _e( 'Close Popup', 'popup-maker' ); ?></label>
  103. </th>
  104. <td>
  105. <input type="checkbox" id="gforms-pum-closepopup" name="gforms-pum[closepopup]" value="true" <?php checked( $settings['closepopup'], true ); ?> />
  106. </td>
  107. </tr>
  108. <tr id="gforms-pum-closedelay-wrapper">
  109. <th scope="row">
  110. <label for="gforms-pum-closedelay"><?php _e( 'Delay', 'popup-maker' ); ?></label>
  111. </th>
  112. <td>
  113. <?php if ( strlen( $settings['closedelay'] ) >= 3 ) {
  114. $settings['closedelay'] = $settings['closedelay'] / 1000;
  115. } ?>
  116. <input type="number" id="gforms-pum-closedelay" min="0" step="1" name="gforms-pum[closedelay]" style="width: 100px;" value="<?php echo esc_attr( $settings['closedelay'] ); ?>" /><?php _e( 'seconds', 'popup-maker' ); ?>
  117. </td>
  118. </tr>
  119. <tr>
  120. <th scope="row">
  121. <label for="gforms-pum-openpopup"><?php _e( 'Open Popup', 'popup-maker' ); ?></label>
  122. </th>
  123. <td>
  124. <input type="checkbox" id="gforms-pum-openpopup" name="gforms-pum[openpopup]" value="true" <?php checked( $settings['openpopup'], true ); ?> />
  125. </td>
  126. </tr>
  127. <tr id="gforms-pum-openpopup_id-wrapper">
  128. <th scope="row">
  129. <label for="gforms-pum-openpopup_id"><?php _e( 'Popup', 'popup-maker' ); ?></label>
  130. </th>
  131. <td>
  132. <select id="gforms-pum-openpopup_id" name="gforms-pum[openpopup_id]">
  133. <?php foreach ( self::get_popup_list() as $option ) { ?>
  134. <option value="<?php echo esc_attr( $option['value'] ); ?>" <?php selected( $settings['openpopup_id'], $option['value'] ); ?>><?php echo $option['label']; ?></option>
  135. <?php } ?>
  136. </select>
  137. </td>
  138. </tr>
  139. </table>
  140. <input type="hidden" id="form_id" name="form_id" value="<?php echo esc_attr( $form_id ); ?>" />
  141. <p class="submit">
  142. <input type="submit" name="save" value="<?php _e( 'Save', 'popup-maker' ); ?>" class="button-primary">
  143. </p>
  144. <?php wp_nonce_field( 'gform_popup_settings_edit', 'gform_popup_settings_edit' ); ?>
  145. </form>
  146. <script type="text/javascript">
  147. (function ($) {
  148. var $open = $('#gforms-pum-openpopup'),
  149. $close = $('#gforms-pum-closepopup'),
  150. $popup_id_wrapper = $('#gforms-pum-openpopup_id-wrapper'),
  151. $delay_wrapper = $('#gforms-pum-closedelay-wrapper');
  152. function check_open() {
  153. if ($open.is(':checked')) {
  154. $popup_id_wrapper.show();
  155. } else {
  156. $popup_id_wrapper.hide();
  157. }
  158. }
  159. function check_close() {
  160. if ($close.is(':checked')) {
  161. $delay_wrapper.show();
  162. } else {
  163. $delay_wrapper.hide();
  164. }
  165. }
  166. check_open();
  167. check_close();
  168. $open.on('click', check_open);
  169. $close.on('click', check_close);
  170. }(jQuery));
  171. </script>
  172. </div> <!-- / popup-editor -->
  173. <?php
  174. GFFormSettings::page_footer();
  175. }
  176. /**
  177. * Get a list of popups for a select box.
  178. *
  179. * @return array
  180. */
  181. public static function get_popup_list() {
  182. $popup_list = array(
  183. array(
  184. 'value' => '',
  185. 'label' => __( 'Select a popup', 'popup-maker' ),
  186. ),
  187. );
  188. $popups = get_posts( array(
  189. 'post_type' => 'popup',
  190. 'post_status' => array( 'publish' ),
  191. 'posts_per_page' => - 1,
  192. ) );
  193. foreach ( $popups as $popup ) {
  194. $popup_list[] = array(
  195. 'value' => $popup->ID,
  196. 'label' => $popup->post_title,
  197. );
  198. }
  199. return $popup_list;
  200. }
  201. /**
  202. * Save form popup options.
  203. */
  204. public static function save() {
  205. if ( empty( $_POST ) || ! check_admin_referer( 'gform_popup_settings_edit', 'gform_popup_settings_edit' ) ) {
  206. return;
  207. }
  208. $form_id = rgget( 'id' );
  209. if ( ! empty( $_POST['gforms-pum'] ) ) {
  210. $settings = $_POST['gforms-pum'];
  211. // Sanitize values.
  212. $settings['openpopup'] = ! empty( $settings['openpopup'] );
  213. $settings['openpopup_id'] = ! empty( $settings['openpopup_id'] ) ? absint( $settings['openpopup_id'] ) : 0;
  214. $settings['closepopup'] = ! empty( $settings['closepopup'] );
  215. $settings['closedelay'] = ! empty( $settings['closedelay'] ) ? absint( $settings['closedelay'] ) : 0;
  216. update_option( 'gforms_pum_' . $form_id, $settings );
  217. } else {
  218. delete_option( 'gforms_pum_' . $form_id );
  219. }
  220. }
  221. }
  222. /**
  223. *
  224. * add_action( 'gform_loaded', array( 'PUM_Gravity_Forms_Integration', 'load' ), 5 );
  225. *
  226. * class PUM_Gravity_Forms_Integration {
  227. *
  228. * public static function load() {
  229. * if ( ! method_exists( 'GFForms', 'include_feed_addon_framework' ) ) {
  230. * return;
  231. * }
  232. * require_once 'gravity-forms/class-pum-gf-popup-addon.php';
  233. * GFAddOn::register( 'PUM_GF_Popup_Addon' );
  234. * }
  235. * }
  236. *
  237. * function pum_gf_addon() {
  238. * return PUM_GF_Popup_Addon::get_instance();
  239. * }
  240. *
  241. */