Page Permalinks
Add permalinks for each page of your multi-page forms.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gpmpn-page-permalinks.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/**
* Gravity Perks // Multi-page Navigation // Page Permalinks
* https://gravitywiz.com/documentation/gravity-forms-multi-page-navigation/
*
* Add permalinks for each page of your multi-page forms.
*
* Plugin Name: GP Multi-page Navigation — Page Permalinks
* Plugin URI: https://gravitywiz.com/documentation/gravity-forms-multi-page-navigation/
* Description: Add permalinks for each page of your multi-page forms.
* Author: Gravity Wiz
* Version: 0.2
* Author URI: https://gravitywiz.com
*/
class GPMPN_Page_Permalinks {
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(
'form_id' => false,
'permalinks' => 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() {
// @todo For some reason after this runs for newly added page permalinks, all post permalinks are broken
add_filter( 'option_rewrite_rules', array( $this, 'hotload_rewrite_rules' ) );
add_action( 'gpmpn_default_page', array( $this, 'set_default_form_page' ) );
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 );
$this->add_query_var();
$this->add_rewrite_rules();
}
public function add_query_var() {
global $wp;
$wp->add_query_var( 'gpmpn_page' );
}
public function add_rewrite_rules() {
foreach ( $this->_args['permalinks'] as $page => $permalink ) {
add_rewrite_rule( "^{$this->_args['pagename']}/{$permalink}?", "index.php?pagename={$this->_args['pagename']}&gpmpn_page={$page}", 'top' );
}
}
public function hotload_rewrite_rules( $rules ) {
static $did_rewrite_rules;
if ( ! is_array( $rules ) ) {
$rules = array();
}
$needs_flush = false;
foreach ( $this->_args['permalinks'] as $page => $permalink ) {
if ( ! array_key_exists( "^{$this->_args['pagename']}/{$permalink}?", $rules ) ) {
$rules = array( "^{$this->_args['pagename']}/{$permalink}?" => "index.php?pagename={$this->_args['pagename']}&gpmpn_page={$page}" ) + $rules;
$needs_flush = true;
}
}
// if ( $needs_flush ) {
// if ( ! $did_rewrite_rules ) {
// $did_rewrite_rules = true;
// $this->flush_rewrite_rules();
// }
// }
return $rules;
}
public function flush_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
public function set_default_form_page( $page ) {
return get_query_var( 'gpmpn_page' );
}
public function load_form_script( $form, $is_ajax_enabled ) {
if ( $this->is_applicable_form( $form ) && ! 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.GPMPNPagePermalinks = function( args ) {
var self = this;
self.history = [];
// copy all args to current object: (list expected props)
for( var prop in args ) {
if( args.hasOwnProperty( prop ) ) {
self[ prop ] = args[ prop ];
}
}
self.init = function() {
if ( typeof window[ 'GPMPNPagePermalinks_{0}'.gformFormat( self.formId ) ] !== 'undefined' ) {
return;
}
window[ 'GPMPNPagePermalinks_{0}'.gformFormat( self.formId ) ] = self;
self.history.push( { gpmpnPage: self.defaultPage } );
console.log( 'init', self.history );
$( document ).on( 'gform_page_loaded', function( event, formId, currentPage ) {
if ( formId == self.formId ) {
self.setPageState( currentPage );
}
} );
$( document ).on( 'gform_confirmation_loaded', function( event, formId ) {
if ( formId == self.formId ) {
self.setPageState( 0 );
}
} );
window.addEventListener( 'popstate', function ( event ) {
// @todo Update to navigate the form to correct page based on the path.
// var currentPage = self.history.slice( -1 )[0].gpmpnPage;
// var previousPage = self.history.slice( -2 )[0].gpmpnPage;
// if ( previousPage < currentPage ) {
// $( '.gform_previous_button:visible' ).click();
// }
// Temporary solution: reload first page when user uses browser's back/next buttons.
window.location = window.location.origin + '/{0}/{1}/'.gformFormat( self.pagename, self.permalinks[1] );
} );
self.isInit = true;
};
self.setPageState = function( currentPage ) {
self.currentPage = parseInt( currentPage );
var state = { gpmpnPage: self.currentPage };
self.history.push( state );
window.history.pushState( state, null, '/{0}/{1}/'.gformFormat( self.pagename, self.permalinks[ currentPage ] ) );
}
self.init();
}
} )( jQuery );
</script>
<?php
}
public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return;
}
$args = array(
'formId' => $this->_args['form_id'],
'pagename' => $this->_args['pagename'],
'permalinks' => $this->_args['permalinks'],
'defaultPage' => get_query_var( 'gpmpn_page' ),
);
$script = 'if ( typeof window.GPMPNPagePermalinks !== \'undefined\' ) { new GPMPNPagePermalinks( ' . json_encode( $args ) . ' ); }';
$slug = implode( '_', array( 'gpmpn_page_permalinks', $this->_args['form_id'] ) );
GFFormDisplay::add_init_script( $this->_args['form_id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || (int) $form_id == (int) $this->_args['form_id'];
}
}
# Configuration
new GPMPN_Page_Permalinks( array(
'form_id' => 889,
'pagename' => 'permalinks-for-form-pages',
'permalinks' => array(
1 => 'page-one',
2 => 'page-two',
3 => 'page-three',
0 => 'confirmation',
),
) );