PluginProbe
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI / 1.1.5
Everest Forms – Contact Form, Payment Form, Quiz, Survey & Custom Form Builder with AI v1.1.5
3.6.1 3.6.0 3.5.3 3.5.2 3.5.1 3.5.0 3.4.8 3.4.7 3.4.6 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5.1 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 All 165 releases
everest-forms / includes / admin / evf-admin-functions.php
evf-admin-functions.php
429 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * EverestForms Admin Functions
4 *
5 * @package EverestForms/Admin/Functions
6 * @version 1.0.0
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * Get all EverestForms screen ids.
15 *
16 * @return array
17 */
18 function evf_get_screen_ids() {
19
20 $evf_screen_id = sanitize_title( __( 'Everest Forms', 'everest-forms' ) );
21 $screen_ids = array(
22 'toplevel_page_' . $evf_screen_id,
23 $evf_screen_id . '_page_edit-evf-form',
24 $evf_screen_id . '_page_evf-settings',
25 $evf_screen_id . '_page_evf-status',
26 $evf_screen_id . '_page_evf-addons',
27 );
28
29 return apply_filters( 'everest_forms_screen_ids', $screen_ids );
30 }
31
32 /**
33 * Create a page and store the ID in an option.
34 *
35 * @param mixed $slug Slug for the new page
36 * @param string $option Option name to store the page's ID
37 * @param string $page_title (default: '') Title for the new page
38 * @param string $page_content (default: '') Content for the new page
39 * @param int $post_parent (default: 0) Parent for the new page
40 *
41 * @return int page ID
42 */
43 function evf_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) {
44 global $wpdb;
45
46 $option_value = get_option( $option );
47
48 if ( $option_value > 0 && ( $page_object = get_post( $option_value ) ) ) {
49 if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array(
50 'pending',
51 'trash',
52 'future',
53 'auto-draft'
54 ) )
55 ) {
56 // Valid page is already in place
57 return $page_object->ID;
58 }
59 }
60
61 if ( strlen( $page_content ) > 0 ) {
62 // Search for an existing page with the specified page content (typically a shortcode)
63 $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
64 } else {
65 // Search for an existing page with the specified page slug
66 $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_name = %s LIMIT 1;", $slug ) );
67 }
68
69 $valid_page_found = apply_filters( 'everest_forms_create_page_id', $valid_page_found, $slug, $page_content );
70
71 if ( $valid_page_found ) {
72 if ( $option ) {
73 update_option( $option, $valid_page_found );
74 }
75
76 return $valid_page_found;
77 }
78
79 // Search for a matching valid trashed page
80 if ( strlen( $page_content ) > 0 ) {
81 // Search for an existing page with the specified page content (typically a shortcode)
82 $trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
83 } else {
84 // Search for an existing page with the specified page slug
85 $trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) );
86 }
87
88 if ( $trashed_page_found ) {
89 $page_id = $trashed_page_found;
90 $page_data = array(
91 'ID' => $page_id,
92 'post_status' => 'publish',
93 );
94 wp_update_post( $page_data );
95 } else {
96 $page_data = array(
97 'post_status' => 'publish',
98 'post_type' => 'page',
99 'post_author' => 1,
100 'post_name' => $slug,
101 'post_title' => $page_title,
102 'post_content' => $page_content,
103 'post_parent' => $post_parent,
104 'comment_status' => 'closed',
105 );
106 $page_id = wp_insert_post( $page_data );
107 }
108
109 if ( $option ) {
110 update_option( $option, $page_id );
111 }
112
113 return $page_id;
114 }
115
116 /**
117 * Output admin fields.
118 *
119 * Loops though the EverestFormsoptions array and outputs each field.
120 *
121 * @param array $options Opens array to output
122 */
123 function everest_forms_admin_fields( $options ) {
124
125 if ( ! class_exists( 'EVF_Admin_Settings', false ) ) {
126 include( dirname( __FILE__ ) . '/class-evf-admin-settings.php' );
127 }
128
129 EVF_Admin_Settings::output_fields( $options );
130 }
131
132 /**
133 * Update all settings which are passed.
134 *
135 * @param array $options
136 * @param array $data
137 */
138 function everest_forms_update_options( $options, $data = null ) {
139
140 if ( ! class_exists( 'EVF_Admin_Settings', false ) ) {
141 include( dirname( __FILE__ ) . '/class-evf-admin-settings.php' );
142 }
143
144 EVF_Admin_Settings::save_fields( $options, $data );
145 }
146
147 /**
148 * Get a setting from the settings API.
149 *
150 * @param mixed $option_name
151 * @param mixed $default
152 *
153 * @return string
154 */
155 function everest_forms_settings_get_option( $option_name, $default = '' ) {
156
157 if ( ! class_exists( 'EVF_Admin_Settings', false ) ) {
158 include( dirname( __FILE__ ) . '/class-evf-admin-settings.php' );
159 }
160
161 return EVF_Admin_Settings::get_option( $option_name, $default );
162 }
163
164 /**
165 * Outputs fields to be used on panels (settings etc).
166 *
167 * @since 1.0.0
168 *
169 * @param string $option
170 * @param string $panel
171 * @param string $field
172 * @param array $form_data
173 * @param string $label
174 * @param array $args
175 * @param boolean $echo
176 *
177 * @return string
178 */
179 function everest_forms_panel_field( $option, $panel, $field, $form_data, $label, $args = array(), $echo = true ) {
180
181 // Required params
182 if ( empty( $option ) || empty( $panel ) || empty( $field ) ) {
183 return '';
184 }
185
186 // Setup basic vars
187 $panel = esc_attr( $panel );
188 $field = esc_attr( $field );
189 $panel_id = sanitize_html_class( $panel );
190 $parent = ! empty( $args['parent'] ) ? esc_attr( $args['parent'] ) : '';
191 $subsection = ! empty( $args['subsection'] ) ? esc_attr( $args['subsection'] ) : '';
192 $label = ! empty( $label ) ? $label : '';
193 $class = ! empty( $args['class'] ) ? esc_attr( $args['class'] ) : '';
194 $input_class = ! empty( $args['input_class'] ) ? esc_attr( $args['input_class'] ) : '';
195 $default = isset( $args['default'] ) ? $args['default'] : '';
196 $tinymce = isset( $args['tinymce'] ) ? $args['tinymce'] : '';
197 $placeholder = ! empty( $args['placeholder'] ) ? esc_attr( $args['placeholder'] ) : '';
198 $data_attr = '';
199 $output = '';
200
201 // Check if we should store values in a parent array
202 if ( ! empty( $parent ) ) {
203 if ( ! empty( $subsection ) ) {
204 $field_name = sprintf( '%s[%s][%s][%s]', $parent, $panel, $subsection, $field );
205 $value = isset( $form_data[ $parent ][ $panel ][ $subsection ][ $field ] ) ? $form_data[ $parent ][ $panel ][ $subsection ][ $field ] : $default;
206 $panel_id = sanitize_html_class( $panel . '-' . $subsection );
207 } else {
208 $field_name = sprintf( '%s[%s][%s]', $parent, $panel, $field );
209 $value = isset( $form_data[ $parent ][ $panel ][ $field ] ) ? $form_data[ $parent ][ $panel ][ $field ] : $default;
210 }
211 } else {
212 $field_name = sprintf( '%s[%s]', $panel, $field );
213 $value = isset( $form_data[ $panel ][ $field ] ) ? $form_data[ $panel ][ $field ] : $default;
214 }
215
216 // Check for data attributes
217 if ( ! empty( $args['data'] ) ) {
218 foreach ( $args['data'] as $key => $val ) {
219 if ( is_array( $val ) ) {
220 $val = wp_json_encode( $val );
221 }
222 $data_attr .= ' data-' . $key . '=\'' . $val . '\'';
223 }
224 }
225
226 // Determine what field type to output
227 switch ( $option ) {
228
229 // Text input
230 case 'text':
231
232 $type = ! empty( $args['type'] ) ? esc_attr( $args['type'] ) : 'text';
233 $output = sprintf(
234 '<input type="%s" id="everest-forms-panel-field-%s-%s" name="%s" value="%s" placeholder="%s" class="%s" %s>',
235 $type,
236 sanitize_html_class( $panel_id ),
237 sanitize_html_class( $field ),
238 $field_name,
239 esc_attr( $value ),
240 $placeholder,
241 $input_class,
242 $data_attr
243 );
244 break;
245
246 // Textarea
247 case 'textarea':
248 $rows = ! empty( $args['rows'] ) ? (int) $args['rows'] : '3';
249 $output = sprintf(
250 '<textarea id="everest-forms-panel-field-%s-%s" name="%s" rows="%d" placeholder="%s" class="%s" %s>%s</textarea>',
251 sanitize_html_class( $panel_id ),
252 sanitize_html_class( $field ),
253 $field_name,
254 $rows,
255 $placeholder,
256 $input_class,
257 $data_attr,
258 esc_textarea( $value )
259 );
260 break;
261
262 // TinyMCE
263 case 'tinymce':
264 $args = wp_parse_args( $tinymce, array(
265 'media_buttons' => false,
266 'tinymce' => false,
267 ) );
268 $args['textarea_name'] = $field_name;
269 $args['teeny'] = true;
270 $id = 'everest-forms-panel-field-' . sanitize_html_class( $panel_id ) . '-' . sanitize_html_class( $field );
271 $id = str_replace( '-', '_', $id );
272 ob_start();
273 wp_editor( $value, $id, $args );
274 $output = ob_get_clean();
275 break;
276
277 // Checkbox
278 case 'checkbox':
279 $checked = checked( '1', $value, false );
280 $checkbox = sprintf(
281 '<input type="hidden" id="everest-forms-panel-field-%s-%s" name="%s" value="0" class="%s" %s %s>',
282 sanitize_html_class( $panel_id ),
283 sanitize_html_class( $field ),
284 $field_name,
285 $input_class,
286 $checked,
287 $data_attr
288 );
289 $checkbox .= sprintf(
290 '<input type="checkbox" id="everest-forms-panel-field-%s-%s" name="%s" value="1" class="%s" %s %s>',
291 sanitize_html_class( $panel_id ),
292 sanitize_html_class( $field ),
293 $field_name,
294 $input_class,
295 $checked,
296 $data_attr
297 );
298 $output = sprintf(
299 '<label for="everest-forms-panel-field-%s-%s" class="inline">%s',
300 sanitize_html_class( $panel_id ),
301 sanitize_html_class( $field ),
302 $checkbox . $label
303 );
304 if ( ! empty( $args['tooltip'] ) ) {
305 $output .= sprintf( ' <i class="dashicons dashicons-editor-help everest-forms-help-tooltip" title="%s"></i>', esc_attr( $args['tooltip'] ) );
306 }
307 $output .= '</label>';
308 break;
309
310 // Radio
311 case 'radio':
312 $options = $args['options'];
313 $x = 1;
314 $output = '';
315 foreach ( $options as $key => $item ) {
316 if ( empty( $item['label'] ) ) {
317 continue;
318 }
319 $checked = checked( $key, $value, false );
320 $output .= sprintf(
321 '<span class="row"><input type="radio" id="everest-forms-panel-field-%s-%s-%d" name="%s" value="%s" class="%s" %s %s>',
322 sanitize_html_class( $panel_id ),
323 sanitize_html_class( $field ),
324 $x,
325 $field_name,
326 $key,
327 $input_class,
328 $checked,
329 $data_attr
330 );
331 $output .= sprintf(
332 '<label for="everest-forms-panel-field-%s-%s-%d" class="inline">%s',
333 sanitize_html_class( $panel_id ),
334 sanitize_html_class( $field ),
335 $x,
336 $item['label']
337 );
338 if ( ! empty( $item['tooltip'] ) ) {
339 $output .= sprintf( ' <i class="dashicons dashicons-editor-help everest-forms-help-tooltip" title="%s"></i>', esc_attr( $item['tooltip'] ) );
340 }
341 $output .= '</label></span>';
342 $x ++;
343 }
344 break;
345
346 // Select
347 case 'select':
348 if ( empty( $args['options'] ) && empty( $args['field_map'] ) ) {
349 return '';
350 }
351
352 if ( ! empty( $args['field_map'] ) ) {
353 $options = array();
354 $available_fields = evf_get_form_fields( $form_data, $args['field_map'] );
355 if ( ! empty( $available_fields ) ) {
356 foreach ( $available_fields as $id => $available_field ) {
357 $lbl = ! empty( $available_field['label'] ) ? esc_attr( $available_field['label'] ) : __( 'Field #', 'everest-forms' ) . $id;
358 $options[ $id ] = $lbl;
359 }
360 }
361 $input_class .= ' everest-forms-field-map-select';
362 $data_attr .= ' data-field-map-allowed="' . implode( ' ', $args['field_map'] ) . '"';
363 if ( ! empty( $placeholder ) ) {
364 $data_attr .= ' data-field-map-placeholder="' . esc_attr( $placeholder ) . '"';
365 }
366 } else {
367 $options = $args['options'];
368 }
369
370 $output = sprintf(
371 '<select id="everest-forms-panel-field-%s-%s" name="%s" class="%s" %s>',
372 sanitize_html_class( $panel_id ),
373 sanitize_html_class( $field ),
374 $field_name,
375 $input_class,
376 $data_attr
377 );
378
379 if ( ! empty( $placeholder ) ) {
380 $output .= '<option value="">' . $placeholder . '</option>';
381 }
382
383 foreach ( $options as $key => $item ) {
384 $output .= sprintf( '<option value="%s" %s>%s</option>', esc_attr( $key ), selected( $key, $value, false ), $item );
385 }
386
387 $output .= '</select>';
388 break;
389 }
390
391 // Put the pieces together....
392 $field_open = sprintf(
393 '<div id="everest-forms-panel-field-%s-%s-wrap" class="everest-forms-panel-field %s %s">',
394 sanitize_html_class( $panel_id ),
395 sanitize_html_class( $field ),
396 $class,
397 'everest-forms-panel-field-' . sanitize_html_class( $option )
398 );
399 $field_open .= ! empty( $args['before'] ) ? $args['before'] : '';
400 if ( ! in_array( $option, array( 'checkbox' ), true ) && ! empty( $label ) ) {
401 $field_label = sprintf(
402 '<label for="everest-forms-panel-field-%s-%s">%s',
403 sanitize_html_class( $panel_id ),
404 sanitize_html_class( $field ),
405 $label
406 );
407 if ( ! empty( $args['tooltip'] ) ) {
408 $field_label .= sprintf( ' <i class="dashicons dashicons-editor-help everest-forms-help-tooltip" title="%s"></i>', esc_attr( $args['tooltip'] ) );
409 }
410 if ( ! empty( $args['after_tooltip'] ) ) {
411 $field_label .= $args['after_tooltip'];
412 }
413
414 $field_label .= '</label>';
415 } else {
416 $field_label = '';
417 }
418 $field_close = ! empty( $args['after'] ) ? $args['after'] : '';
419 $field_close .= '</div>';
420 $output = $field_open . $field_label . $output . $field_close;
421
422 // Wash our hands.
423 if ( $echo ) {
424 echo $output;
425 } else {
426 return $output;
427 }
428 }
429