-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathgppa-page-modifier.php
150 lines (106 loc) · 4.02 KB
/
gppa-page-modifier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Gravity Perks // Populate Anything // Page Modifier
* https://gravitywiz.com/documentation/gravity-forms-populate-anything/
*
* Experimental Snippet 🧪
*
* This snippet allows you to use the `:page` modifier to only process Live Merge Tags on a specific page of a
* multi-page form. This is useful when...
*
* 1. You have a large number of Live Merge Tags across multiple pages.
* 2. You're experiencing slow page loads.
* 2. Previous pages have no dependency on Live Merge Tags from subsequent pages.
*
* As an example, to only process a Live Merge Tag on page 2 of a form, you would use the following:
*
* @{My Field:1:page[2]}
*
* NOTE: This snippet is very much a proof-of-concept. It has not been tested thoroughly and may not work in all scenarios.
*
* Plugin Name: GPPA Page Modifier
* Plugin URI: https://gravitywiz.com/documentation/gravity-forms-populate-anything/
* Description: Adds a `:page` modifier, allowing you to specify on which page a given Live Merge Tag should be processed.
* Author: Gravity Wiz
* Version: 0.1
* Author URI: https://gravitywiz.com
*/
class GPPA_Page_Modifier {
private $_args = array();
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array() );
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
add_filter( 'gform_pre_replace_merge_tags', array( $this, 'handle_page_modifier' ), 10, 7 );
}
public function handle_page_modifier( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER );
if ( empty( $matches ) ) {
return $text;
}
foreach ( $matches as $match ) {
$modifiers = $this->parse_modifiers( rgar( $match, 4 ) );
if ( ! rgar( $modifiers, 'page' ) ) {
continue;
}
if ( rgpost( 'page-number' ) && rgpost( 'page-number' ) < $modifiers['page'] ) {
$text = str_replace( $match[0], '', $text );
}
}
return $text;
}
public function load_form_script( $form, $is_ajax_enabled ) {
if ( ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
add_action( 'wp_footer', array( $this, 'output_script' ) );
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
}
return $form;
}
public function output_script() {
?>
<script type="text/javascript">
( function( $ ) {
window.GPPAPageModifier = function( args ) {
var self = this;
self.init = function() {
gform.addFilter( 'gppa_batch_field_html_ajax_data', function( data ) {
data['page-number'] = gf_get_input_id_by_html_id( $( '.gform_page:visible' ).attr( 'id' ) );
return data;
} );
};
self.init();
}
} )( jQuery );
</script>
<?php
}
public function add_init_script( $form ) {
$args = array();
$script = 'new GPPAPageModifier( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array( 'gppa_page_modifier' ) );
GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
public function parse_modifiers( $modifiers_str ) {
preg_match_all( '/([a-z]+)(?:(?:\[(.+?)\])|,?)/i', $modifiers_str, $modifiers, PREG_SET_ORDER );
$parsed = array();
foreach ( $modifiers as $modifier ) {
list( $match, $modifier, $value ) = array_pad( $modifier, 3, null );
if ( $value === null ) {
$value = $modifier;
}
// Split '1,2,3' into array( 1, 2, 3 ).
if ( strpos( $value, ',' ) !== false ) {
$value = array_map( 'trim', explode( ',', $value ) );
}
$parsed[ strtolower( $modifier ) ] = $value;
}
return $parsed;
}
}
# Configuration
new GPPA_Page_Modifier();