-
Notifications
You must be signed in to change notification settings - Fork 3
/
class-gfpb-form-replacer.php
247 lines (216 loc) · 8.07 KB
/
class-gfpb-form-replacer.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/**
* Adds a Replace Forms tab to the Import/Export feature.
*
* @package Gravity_Forms_Power_Boost
*/
defined( 'ABSPATH' ) || exit;
/**
* Gravity_Forms_Power_Boost_Form_Replacer
*
* This class adds a tab to the Gravity Forms Import/Export page found at Forms
* > Import/Export in the dashboard. The tab is named "Replace Forms" and works
* similarly to "Import Forms" except that it updates existing forms based on ID
* rather than always inserting duplicates.
*/
class GFPB_Form_Replacer {
const EXPORT_TAB_SLUG = 'gforms_replacer';
/**
* Adds hooks that power the form replacer feature.
*
* @return void
*/
public function add_hooks() {
// Adds an item to the Import/Export menu tabs.
add_filter( 'gform_export_menu', array( $this, 'add_replace_forms_tab' ) );
// Populate the tab with content.
add_action( 'gform_export_page_' . self::EXPORT_TAB_SLUG, array( $this, 'populate_replace_forms_tab' ) );
}
/**
* Adds a "Replace Forms" tab to the Import/Export settings page.
*
* @param array $setting_tabs An array of tabs found at Settings > Import/Export.
* @return array
*/
public function add_replace_forms_tab( $setting_tabs ) {
if ( ! class_exists( 'GFCommon' ) || ! GFCommon::current_user_can_any( 'gravityforms_edit_forms' ) ) {
return;
}
$setting_tabs[] = array(
'name' => self::EXPORT_TAB_SLUG,
'label' => __( 'Replace Forms', 'gravityforms-power-boost' ),
);
return $setting_tabs;
}
/**
* Outputs HTML that populates the Replace Forms tab of the Import/Export
* page.
*
* @return void
*/
public function populate_replace_forms_tab() {
if ( ! class_exists( 'GFCommon' ) || ! GFCommon::current_user_can_any( 'gravityforms_edit_forms' ) ) {
wp_die( 'You do not have permission to access this page' );
}
if ( isset( $_POST['import_forms'] ) ) {
check_admin_referer( 'gf_replace_forms', 'gf_replace_forms_nonce' );
if ( ! empty( $_FILES['gf_import_file']['tmp_name'][0] ) ) {
// Set initial count to 0.
$count = 0;
$forms_array = array();
// Loop through each uploaded file.
foreach ( array_map( 'sanitize_text_field', wp_unslash( $_FILES['gf_import_file']['tmp_name'] ) ) as $import_file_path ) {
// Turn the file into an array of forms.
$json = file_get_contents( $import_file_path );
if ( false === $json ) {
continue;
}
// Deserialize into an array of forms.
$forms_array = json_decode( $json, true );
// Are any of the form confirmations redirects?
foreach ( $forms_array as $form ) {
if ( empty( $form['id'] ) ) {
continue;
}
foreach ( $form['confirmations'] as $key => $confirmation ) {
if ( 'version' === $key ) {
continue;
}
if ( 'redirect' !== $confirmation['type'] ) {
continue;
}
$confirmation_config_url = admin_url(
sprintf(
'admin.php?page=gf_edit_forms&view=settings&subview=confirmation&id=%s&cid=%s',
$form['id'],
$confirmation['id']
)
);
/**
* Ask the user if $confirmation['url'] is a URL
* that needs to be updated.
*/
GFCommon::add_message(
sprintf(
'%s \'%s\', %s %s, %s %s. <a href="%s">%s</a> %s.',
__( 'A confirmation in form', 'gravityforms-power-boost' ),
$form['title'],
__( 'ID', 'gravityforms-power-boost' ),
$form['id'],
__( 'is set to redirect users to', 'gravityforms-power-boost' ),
$confirmation['url'],
$confirmation_config_url,
__( 'Click here', 'gravityforms-power-boost' ),
__( 'if this URL needs to be updated', 'gravityforms-power-boost' )
)
);
}
}
// Update the forms saved in this site.
$count += self::update_forms( $forms_array );
}
if ( 0 === $count ) {
$error_message = sprintf(
'%s <a href="admin.php?page=gf_export&view=export_form">%s</a> %s',
__( 'Forms could not be imported. Please make sure your files have the .json extension, and that they were generated by the', 'gravityforms-power-boost' ),
__( 'Gravity Forms Export form', 'gravityforms-power-boost' ),
__( 'tool.', 'gravityforms-power-boost' )
);
GFCommon::add_error_message( $error_message );
} elseif ( '-1' === $count ) {
GFCommon::add_error_message( esc_html__( 'Forms could not be imported. Your export file is not compatible with your current version of Gravity Forms.', 'gravityforms-power-boost' ) );
} else {
$form_text = $count > 1 ? esc_html__( 'forms', 'gravityforms-power-boost' ) : esc_html__( 'form', 'gravityforms-power-boost' );
$edit_link = 1 === $count ? "<a href='admin.php?page=gf_edit_forms&id={$forms_array[0]['id']}'>" . esc_html__( 'Edit Form', 'gravityforms-power-boost' ) . '</a>' : '';
GFCommon::add_message(
sprintf(
'%s %d %s %s',
esc_html__( 'Gravity Forms imported', 'gravityforms-power-boost' ),
$count,
$form_text,
__( 'successfully', 'gravityforms-power-boost' )
) . ". $edit_link"
);
}
}
}
GFExport::page_header();
?><div class="gform-settings__content">
<form method="post" enctype="multipart/form-data" class="gform_settings_form">
<?php wp_nonce_field( 'gf_replace_forms', 'gf_replace_forms_nonce' ); ?>
<div class="gform-settings-panel gform-settings-panel--full">
<header class="gform-settings-panel__header"><legend class="gform-settings-panel__title">
<?php
esc_html_e( 'Replace Forms', 'gravityforms-power-boost' );
?>
</legend></header>
<div class="gform-settings-panel__content">
<div class="gform-settings-description">
<?php
printf(
'%s <a href="admin.php?page=gf_export&view=export_form">%s</a> %s',
esc_html__( 'Select the Gravity Forms export files you would like to import. Please make sure your files have the .json extension, and that they were generated by the', 'gravityforms-power-boost' ),
esc_html__( 'Gravity Forms Export form', 'gravityforms-power-boost' ),
esc_html__( 'tool. When you click the import button below, Gravity Forms will import the forms.', 'gravityforms-power-boost' )
);
?>
<p><b>
<?php
printf(
'%s <a href="https://wordpress.org/plugins/power-boost-for-gravity-forms/">%s</a>.',
esc_html__( 'This feature updates existing forms instead of creating duplicates like the Import Forms tab. Provided by', 'gravityforms-power-boost' ),
esc_html__( 'Power Boost for Gravity Forms', 'gravityforms-power-boost' )
);
?>
</b></p></div>
<table class="form-table">
<tr valign="top">
<th scope="row">
<label for="gf_import_file"><?php esc_html_e( 'Select Files', 'gravityforms-power-boost' ); ?></label> <?php gform_tooltip( 'import_select_file' ); ?>
</th>
<td><input type="file" name="gf_import_file[]" id="gf_import_file" multiple /></td>
</tr>
</table>
<br /><br />
<input type="submit" value="<?php esc_html_e( 'Import', 'gravityforms-power-boost' ); ?>" name="import_forms" class="button large primary" />
</div>
</div>
</form>
</div>
<?php
GFExport::page_footer();
}
/**
* Overwrites existing forms when they are found to save the same ID as
* forms in the $forms_array parameter.
*
* @param array $forms_array An array of forms.
* @return int The number of forms that were updated
*/
public static function update_forms( $forms_array ) {
if ( ! class_exists( 'GFAPI' ) ) {
return 0;
}
// Update the forms.
$updated_count = 0;
foreach ( $forms_array as $form ) {
if ( empty( $form['id'] ) ) {
continue;
}
// does the form exist?
$existing = GFAPI::get_form( $form['id'] );
if ( false === $existing ) {
// no, skip this one.
continue;
}
GFAPI::update_form( $form, $form['id'] );
$updated_count++;
/**
* GFAPI::update_form() marks all forms as in active. Restore the
* previous status.
*/
GFFormsModel::update_form_active( $form['id'], $existing['is_active'] );
}
return $updated_count;
}
}