-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-gf-chainedselects.php
316 lines (281 loc) · 10.2 KB
/
class-gf-chainedselects.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
defined( 'ABSPATH' ) || die();
GFForms::include_feed_addon_framework();
class GFChainedSelects extends GFAddOn {
protected $_version = GF_CHAINEDSELECTS_VERSION;
protected $_min_gravityforms_version = '2.2.2';
protected $_slug = 'gravityformschainedselects';
protected $_path = 'gravityformschainedselects/chainedselects.php';
protected $_full_path = __FILE__;
protected $_url = 'http://www.gravityforms.com';
protected $_title = 'Gravity Forms Chained Selects Add-On';
protected $_short_title = 'Chained Selects';
protected $_enable_rg_autoupgrade = true;
private static $_instance = null;
/* Permissions */
protected $_capabilities_uninstall = 'gravityforms_chainedselects_uninstall';
/* Members plugin integration */
protected $_capabilities = array( 'gravityforms_chainedselects', 'gravityforms_chainedselects_uninstall' );
/* Theme framework */
protected $_enable_theme_layer = true;
/**
* Get instance of this class.
*
* @access public
* @static
* @return object $_instance
*/
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Include Chain Select field class.
*
* @access public
* @return void
*/
public function pre_init() {
parent::pre_init();
if ( $this->is_gravityforms_supported() && class_exists( 'GF_Field' ) ) {
require_once 'includes/class-gf-field-chainedselect.php';
}
}
/**
* Enqueue scripts.
*
* @access public
* @return array $scripts
*/
public function scripts() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$scripts = array(
array(
'handle' => 'gform_chained_selects_admin',
'deps' => array( 'jquery', 'backbone', 'plupload', 'gform_form_admin' ),
'src' => $this->get_base_url() . "/js/admin{$min}.js",
'version' => $this->_version,
'enqueue' => array(
array( 'admin_page' => array( 'form_editor', 'form_settings' ) ),
),
'in_footer' => true,
'callback' => array( $this, 'localize_scripts' ),
),
array(
'handle' => 'gform_chained_selects_admin_form_editor',
'deps' => array( 'jquery', 'backbone', 'gform_form_editor', 'plupload' ),
'src' => $this->get_base_url() . "/js/admin-form-editor{$min}.js",
'version' => $this->_version,
'enqueue' => array(
array( 'admin_page' => array( 'form_editor' ) ),
),
'in_footer' => true,
'callback' => array( $this, 'localize_scripts' ),
),
array(
'handle' => 'gform_chained_selects',
'deps' => array( 'jquery', 'gform_gravityforms' ),
'src' => $this->get_base_url() . "/js/frontend{$min}.js",
'version' => $this->_version,
'enqueue' => array(
array( $this, 'should_enqueue_frontend_script' )
),
'callback' => array( $this, 'localize_scripts' ),
),
);
return array_merge( parent::scripts(), $scripts );
}
/**
* Frontend scripts should only be enqueued if we're not on a GF admin page and the form contains our field type.
*
* @param $form
*
* @return bool
*/
public function should_enqueue_frontend_script( $form ) {
return ! GFForms::get_page() && ! rgempty( GFFormsModel::get_fields_by_type( $form, array( 'chainedselect' ) ) );
}
/**
* Enqueue styles.
*
* @access public
* @return array $scripts
*/
public function styles() {
$base_url = $this->get_base_url();
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$styles = array(
array(
'handle' => 'gform_chained_selects_admin',
'src' => $base_url . "/assets/css/dist/admin{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array( 'admin_page' => array( 'form_editor', 'entry_view' ) ),
),
),
array(
'handle' => 'gform_chained_selects_theme',
'src' => $base_url . "/assets/css/dist/theme{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array( $this, 'should_enqueue_frontend_script' )
),
),
);
return array_merge( parent::styles(), $styles );
}
/**
* An array of styles to enqueue.
*
* @since 1.6
*
* @param $form
* @param $ajax
* @param $settings
* @param $block_settings
*
* @return array|\string[][]
*/
public function theme_layer_styles( $form, $ajax, $settings, $block_settings = array() ) {
$theme_slug = \GFFormDisplay::get_form_theme_slug( $form );
if ( $theme_slug !== 'orbital' ) {
return array();
}
$base_url = plugins_url( '', __FILE__ );
return array(
'framework' => array(
array( 'gravity_forms_chainedselects_theme_framework', "$base_url/assets/css/dist/theme-framework.css" ),
),
);
}
public function localize_scripts() {
wp_localize_script( 'gform_chained_selects_admin', 'gformChainedSelectData', array(
'defaultChoices' => $this->get_default_choices(),
'defaultInputs' => $this->get_default_inputs(),
'fileUploadUrl' => trailingslashit( site_url() ) . '?gf_page=' . GFCommon::get_upload_page_slug(),
'maxFileSize' => $this->get_max_file_size(),
'strings' => array(
'errorProcessingFile' => wp_strip_all_tags( __( 'There was an error processing this file.', 'gravityformschainedselects' ) ),
'errorUploadingFile' => wp_strip_all_tags( __( 'There was an error uploading this file.', 'gravityformschainedselects' ) ),
'errorFileType' => wp_strip_all_tags( __( 'Only CSV files are allowed.', 'gravityformschainedselects' ) ),
'errorFileSize' => sprintf( wp_strip_all_tags( __( 'This file is too big. Max file size is %dMB.', 'gravityformschainedselects' ) ), round( $this->get_max_file_size() / 1000000 ) ),
'importedFilterFile' => sprintf( wp_strip_all_tags( __( 'This file is imported via %sa filter%s and cannot be modified here.', 'gravityformschainedselects' ) ), '<a href="@todo">', '</a>' ),
'errorImportingFilterFile' => sprintf( wp_strip_all_tags( __( 'There was an error importing the file via %sthe filter%s.', 'gravityformschainedselects' ) ), '<a href="@todo">', '</a>' ),
)
) );
wp_localize_script( 'gform_chained_selects', 'gformChainedSelectData', array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'gform_get_next_chained_select_choices' ),
'spinner' => $this->get_spinner_url(),
'strings' => array(
'loading' => wp_strip_all_tags( __( 'Loading', 'gravityformschainedselects' ) ),
'noOptions' => wp_strip_all_tags( __( 'No options', 'gravityformschainedselects' ) ),
),
) );
}
/**
* Returns the URL of the file containing the spinner.
*
* @since 1.6
*
* @return string
*/
public function get_spinner_url() {
return GFCommon::get_base_url() . '/images/spinner' . ( $this->is_gravityforms_supported( '2.5' ) ? '.svg' : '.gif' );
}
public function get_default_choices() {
// ids are set in JS based on newly created field
return array(
array(
'text' => wp_strip_all_tags( __( 'Parent 1', 'gravityformschainedselects' ) ),
'value' => wp_strip_all_tags( __( 'Parent 1', 'gravityformschainedselects' ) ),
'isSelected' => true,
'choices' => array(
array(
'text' => wp_strip_all_tags( __( 'Child 1', 'gravityformschainedselects' ) ),
'value' => wp_strip_all_tags( __( 'Child 1', 'gravityformschainedselects' ) ),
'isSelected' => true,
),
array(
'text' => wp_strip_all_tags( __( 'Child 2', 'gravityformschainedselects' ) ),
'value' => wp_strip_all_tags( __( 'Child 2', 'gravityformschainedselects' ) ),
'isSelected' => false,
),
array(
'text' => wp_strip_all_tags( __( 'Child 3', 'gravityformschainedselects' ) ),
'value' => wp_strip_all_tags( __( 'Child 3', 'gravityformschainedselects' ) ),
'isSelected' => false,
)
)
),
array(
'text' => wp_strip_all_tags( __( 'Parent 2', 'gravityformschainedselects' ) ),
'value' => wp_strip_all_tags( __( 'Parent 2', 'gravityformschainedselects' ) ),
'isSelected' => false,
'choices' => array(
array(
'text' => wp_strip_all_tags( __( 'Child 4', 'gravityformschainedselects' ) ),
'value' => wp_strip_all_tags( __( 'Child 4', 'gravityformschainedselects' ) ),
'isSelected' => false,
),
array(
'text' => wp_strip_all_tags( __( 'Child 5', 'gravityformschainedselects' ) ),
'value' => wp_strip_all_tags( __( 'Child 5', 'gravityformschainedselects' ) ),
'isSelected' => false,
),
array(
'text' => wp_strip_all_tags( __( 'Child 6', 'gravityformschainedselects' ) ),
'value' => wp_strip_all_tags( __( 'Child 6', 'gravityformschainedselects' ) ),
'isSelected' => false,
)
)
),
array(
'text' => wp_strip_all_tags( __( 'Parent 3', 'gravityformschainedselects' ) ),
'value' => wp_strip_all_tags( __( 'Parent 3', 'gravityformschainedselects' ) ),
'isSelected' => false,
'choices' => array(
array(
'text' => wp_strip_all_tags( __( 'Child 7', 'gravityformschainedselects' ) ),
'value' => wp_strip_all_tags( __( 'Child 7', 'gravityformschainedselects' ) ),
'isSelected' => false,
),
array(
'text' => wp_strip_all_tags( __( 'Child 8', 'gravityformschainedselects' ) ),
'value' => wp_strip_all_tags( __( 'Child 8', 'gravityformschainedselects' ) ),
'isSelected' => false,
),
array(
'text' => wp_strip_all_tags( __( 'Child 9', 'gravityformschainedselects' ) ),
'value' => wp_strip_all_tags( __( 'Child 9', 'gravityformschainedselects' ) ),
'isSelected' => false,
)
)
),
);
}
public function get_default_inputs() {
return array(
array(
'label' => wp_strip_all_tags( __( 'Parents', 'gravityformschainedselects' ) ),
'id' => '',
),
array(
'label' => wp_strip_all_tags( __( 'Children', 'gravityformschainedselects' ) ),
'id' => '',
)
);
}
public function get_max_file_size() {
/**
* Filter the max file size for imported Chained Select files.
*
* @param int $size The max file size in bytes.
*
* @since 1.0
*/
return apply_filters( 'gform_chainedselects_max_file_size', 1000000 ); // 1mb
}
}