builder
6 years ago
plugin-updates
8 years ago
settings
6 years ago
views
6 years ago
class-evf-admin-addons.php
6 years ago
class-evf-admin-assets.php
6 years ago
class-evf-admin-builder.php
7 years ago
class-evf-admin-editor.php
6 years ago
class-evf-admin-entries-table-list.php
6 years ago
class-evf-admin-entries.php
6 years ago
class-evf-admin-forms-table-list.php
6 years ago
class-evf-admin-forms.php
6 years ago
class-evf-admin-import-export.php
6 years ago
class-evf-admin-menus.php
6 years ago
class-evf-admin-notices.php
6 years ago
class-evf-admin-settings.php
6 years ago
class-evf-admin-tools.php
6 years ago
class-evf-admin-welcome.php
6 years ago
class-evf-admin.php
6 years ago
evf-admin-functions.php
6 years ago
evf-admin-functions.php
450 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EverestForms Admin Functions |
| 4 | * |
| 5 | * @package EverestForms/Admin/Functions |
| 6 | * @version 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Get all EverestForms screen ids. |
| 13 | * |
| 14 | * @return array |
| 15 | */ |
| 16 | function evf_get_screen_ids() { |
| 17 | $evf_screen_id = sanitize_title( esc_html__( 'Everest Forms', 'everest-forms' ) ); |
| 18 | $screen_ids = array( |
| 19 | 'dashboard_page_evf-welcome', |
| 20 | 'toplevel_page_' . $evf_screen_id, |
| 21 | $evf_screen_id . '_page_evf-builder', |
| 22 | $evf_screen_id . '_page_evf-entries', |
| 23 | $evf_screen_id . '_page_evf-settings', |
| 24 | $evf_screen_id . '_page_evf-tools', |
| 25 | $evf_screen_id . '_page_evf-addons', |
| 26 | ); |
| 27 | |
| 28 | return apply_filters( 'everest_forms_screen_ids', $screen_ids ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Create a page and store the ID in an option. |
| 33 | * |
| 34 | * @param mixed $slug Slug for the new page. |
| 35 | * @param string $option Option name to store the page's ID. |
| 36 | * @param string $page_title (default: '') Title for the new page. |
| 37 | * @param string $page_content (default: '') Content for the new page. |
| 38 | * @param int $post_parent (default: 0) Parent for the new page. |
| 39 | * |
| 40 | * @return int page ID |
| 41 | */ |
| 42 | function evf_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) { |
| 43 | global $wpdb; |
| 44 | |
| 45 | $option_value = get_option( $option ); |
| 46 | $page_object = get_post( $option_value ); |
| 47 | |
| 48 | if ( $option_value > 0 && $page_object ) { |
| 49 | if ( 'page' === $page_object->post_type && ! in_array( |
| 50 | $page_object->post_status, |
| 51 | array( |
| 52 | 'pending', |
| 53 | 'trash', |
| 54 | 'future', |
| 55 | 'auto-draft', |
| 56 | ), |
| 57 | true |
| 58 | ) ) { |
| 59 | // Valid page is already in place. |
| 60 | return $page_object->ID; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if ( strlen( $page_content ) > 0 ) { |
| 65 | // Search for an existing page with the specified page content (typically a shortcode). |
| 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_content LIKE %s LIMIT 1;", "%{$page_content}%" ) ); |
| 67 | } else { |
| 68 | // Search for an existing page with the specified page slug. |
| 69 | $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 ) ); |
| 70 | } |
| 71 | |
| 72 | $valid_page_found = apply_filters( 'everest_forms_create_page_id', $valid_page_found, $slug, $page_content ); |
| 73 | |
| 74 | if ( $valid_page_found ) { |
| 75 | if ( $option ) { |
| 76 | update_option( $option, $valid_page_found ); |
| 77 | } |
| 78 | |
| 79 | return $valid_page_found; |
| 80 | } |
| 81 | |
| 82 | // Search for a matching valid trashed page. |
| 83 | if ( strlen( $page_content ) > 0 ) { |
| 84 | // Search for an existing page with the specified page content (typically a shortcode). |
| 85 | $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}%" ) ); |
| 86 | } else { |
| 87 | // Search for an existing page with the specified page slug. |
| 88 | $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 ) ); |
| 89 | } |
| 90 | |
| 91 | if ( $trashed_page_found ) { |
| 92 | $page_id = $trashed_page_found; |
| 93 | $page_data = array( |
| 94 | 'ID' => $page_id, |
| 95 | 'post_status' => 'publish', |
| 96 | ); |
| 97 | wp_update_post( $page_data ); |
| 98 | } else { |
| 99 | $page_data = array( |
| 100 | 'post_status' => 'publish', |
| 101 | 'post_type' => 'page', |
| 102 | 'post_author' => 1, |
| 103 | 'post_name' => $slug, |
| 104 | 'post_title' => $page_title, |
| 105 | 'post_content' => $page_content, |
| 106 | 'post_parent' => $post_parent, |
| 107 | 'comment_status' => 'closed', |
| 108 | ); |
| 109 | $page_id = wp_insert_post( $page_data ); |
| 110 | } |
| 111 | |
| 112 | if ( $option ) { |
| 113 | update_option( $option, $page_id ); |
| 114 | } |
| 115 | |
| 116 | return $page_id; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Output admin fields. |
| 121 | * |
| 122 | * Loops though the EverestFormsoptions array and outputs each field. |
| 123 | * |
| 124 | * @param array[] $options Opens array to output. |
| 125 | */ |
| 126 | function everest_forms_admin_fields( $options ) { |
| 127 | if ( ! class_exists( 'EVF_Admin_Settings', false ) ) { |
| 128 | include dirname( __FILE__ ) . '/class-evf-admin-settings.php'; |
| 129 | } |
| 130 | |
| 131 | EVF_Admin_Settings::output_fields( $options ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Update all settings which are passed. |
| 136 | * |
| 137 | * @param array $options Options array to output. |
| 138 | * @param array $data Optional. Data to use for saving. Defaults to $_POST. |
| 139 | */ |
| 140 | function everest_forms_update_options( $options, $data = null ) { |
| 141 | if ( ! class_exists( 'EVF_Admin_Settings', false ) ) { |
| 142 | include dirname( __FILE__ ) . '/class-evf-admin-settings.php'; |
| 143 | } |
| 144 | |
| 145 | EVF_Admin_Settings::save_fields( $options, $data ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get a setting from the settings API. |
| 150 | * |
| 151 | * @param string $option_name Option name. |
| 152 | * @param mixed $default Default value. |
| 153 | * |
| 154 | * @return string |
| 155 | */ |
| 156 | function everest_forms_settings_get_option( $option_name, $default = '' ) { |
| 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 | * @param string $option Option. |
| 168 | * @param string $panel Panel. |
| 169 | * @param string $field Field. |
| 170 | * @param array $form_data Form data. |
| 171 | * @param string $label Label. |
| 172 | * @param array $args Arguments. |
| 173 | * @param boolean $echo True to echo else return. |
| 174 | * |
| 175 | * @return string |
| 176 | */ |
| 177 | function everest_forms_panel_field( $option, $panel, $field, $form_data, $label, $args = array(), $echo = true ) { |
| 178 | // Required params. |
| 179 | if ( empty( $option ) || empty( $panel ) || empty( $field ) ) { |
| 180 | return ''; |
| 181 | } |
| 182 | |
| 183 | // Setup basic vars. |
| 184 | $panel = esc_attr( $panel ); |
| 185 | $field = esc_attr( $field ); |
| 186 | $panel_id = sanitize_html_class( $panel ); |
| 187 | $parent = ! empty( $args['parent'] ) ? esc_attr( $args['parent'] ) : ''; |
| 188 | $subsection = ! empty( $args['subsection'] ) ? esc_attr( $args['subsection'] ) : ''; |
| 189 | $label = ! empty( $label ) ? $label : ''; |
| 190 | $class = ! empty( $args['class'] ) ? esc_attr( $args['class'] ) : ''; |
| 191 | $input_class = ! empty( $args['input_class'] ) ? esc_attr( $args['input_class'] ) : ''; |
| 192 | $default = isset( $args['default'] ) ? $args['default'] : ''; |
| 193 | $tinymce = isset( $args['tinymce'] ) ? $args['tinymce'] : ''; |
| 194 | $placeholder = ! empty( $args['placeholder'] ) ? esc_attr( $args['placeholder'] ) : ''; |
| 195 | $data_attr = ''; |
| 196 | $output = ''; |
| 197 | |
| 198 | // Check if we should store values in a parent array. |
| 199 | if ( ! empty( $parent ) ) { |
| 200 | if ( ! empty( $subsection ) ) { |
| 201 | $field_name = sprintf( '%s[%s][%s][%s]', $parent, $panel, $subsection, $field ); |
| 202 | $value = isset( $form_data[ $parent ][ $panel ][ $subsection ][ $field ] ) ? $form_data[ $parent ][ $panel ][ $subsection ][ $field ] : $default; |
| 203 | $panel_id = sanitize_html_class( $panel . '-' . $subsection ); |
| 204 | } else { |
| 205 | $field_name = sprintf( '%s[%s][%s]', $parent, $panel, $field ); |
| 206 | $value = isset( $form_data[ $parent ][ $panel ][ $field ] ) ? $form_data[ $parent ][ $panel ][ $field ] : $default; |
| 207 | } |
| 208 | } else { |
| 209 | $field_name = sprintf( '%s[%s]', $panel, $field ); |
| 210 | $value = isset( $form_data[ $panel ][ $field ] ) ? $form_data[ $panel ][ $field ] : $default; |
| 211 | } |
| 212 | |
| 213 | // Check for data attributes. |
| 214 | if ( ! empty( $args['data'] ) ) { |
| 215 | foreach ( $args['data'] as $key => $val ) { |
| 216 | if ( is_array( $val ) ) { |
| 217 | $val = wp_json_encode( $val ); |
| 218 | } |
| 219 | $data_attr .= ' data-' . $key . '=\'' . $val . '\''; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Determine what field type to output. |
| 224 | switch ( $option ) { |
| 225 | |
| 226 | // Text input. |
| 227 | case 'text': |
| 228 | $type = ! empty( $args['type'] ) ? esc_attr( $args['type'] ) : 'text'; |
| 229 | $output = sprintf( |
| 230 | '<input type="%s" id="everest-forms-panel-field-%s-%s" name="%s" value="%s" placeholder="%s" class="widefat %s" %s>', |
| 231 | $type, |
| 232 | sanitize_html_class( $panel_id ), |
| 233 | sanitize_html_class( $field ), |
| 234 | $field_name, |
| 235 | esc_attr( $value ), |
| 236 | $placeholder, |
| 237 | $input_class, |
| 238 | $data_attr |
| 239 | ); |
| 240 | break; |
| 241 | |
| 242 | // Textarea. |
| 243 | case 'textarea': |
| 244 | $rows = ! empty( $args['rows'] ) ? (int) $args['rows'] : '3'; |
| 245 | $output = sprintf( |
| 246 | '<textarea id="everest-forms-panel-field-%s-%s" name="%s" rows="%d" placeholder="%s" class="widefat %s" %s>%s</textarea>', |
| 247 | sanitize_html_class( $panel_id ), |
| 248 | sanitize_html_class( $field ), |
| 249 | $field_name, |
| 250 | $rows, |
| 251 | $placeholder, |
| 252 | $input_class, |
| 253 | $data_attr, |
| 254 | esc_textarea( $value ) |
| 255 | ); |
| 256 | break; |
| 257 | |
| 258 | // TinyMCE. |
| 259 | case 'tinymce': |
| 260 | $arguments = wp_parse_args( |
| 261 | $tinymce, |
| 262 | array( |
| 263 | 'media_buttons' => false, |
| 264 | 'tinymce' => false, |
| 265 | ) |
| 266 | ); |
| 267 | $arguments['textarea_name'] = $field_name; |
| 268 | $arguments['teeny'] = true; |
| 269 | $id = 'everest-forms-panel-field-' . sanitize_html_class( $panel_id ) . '-' . sanitize_html_class( $field ); |
| 270 | $id = str_replace( '-', '_', $id ); |
| 271 | ob_start(); |
| 272 | wp_editor( $value, $id, $arguments ); |
| 273 | $output = ob_get_clean(); |
| 274 | break; |
| 275 | |
| 276 | // Checkbox. |
| 277 | case 'checkbox': |
| 278 | $checked = checked( '1', $value, false ); |
| 279 | $checkbox = sprintf( |
| 280 | '<input type="hidden" name="%s" value="0" class="widefat %s" %s %s>', |
| 281 | $field_name, |
| 282 | $input_class, |
| 283 | $checked, |
| 284 | $data_attr |
| 285 | ); |
| 286 | $checkbox .= sprintf( |
| 287 | '<input type="checkbox" id="everest-forms-panel-field-%s-%s" name="%s" value="1" class="%s" %s %s>', |
| 288 | sanitize_html_class( $panel_id ), |
| 289 | sanitize_html_class( $field ), |
| 290 | $field_name, |
| 291 | $input_class, |
| 292 | $checked, |
| 293 | $data_attr |
| 294 | ); |
| 295 | $output = sprintf( |
| 296 | '<label for="everest-forms-panel-field-%s-%s" class="inline">%s', |
| 297 | sanitize_html_class( $panel_id ), |
| 298 | sanitize_html_class( $field ), |
| 299 | $checkbox . $label |
| 300 | ); |
| 301 | if ( ! empty( $args['tooltip'] ) ) { |
| 302 | $output .= sprintf( ' <i class="dashicons dashicons-editor-help everest-forms-help-tooltip" title="%s"></i>', esc_attr( $args['tooltip'] ) ); |
| 303 | } |
| 304 | $output .= '</label>'; |
| 305 | break; |
| 306 | |
| 307 | // Radio. |
| 308 | case 'radio': |
| 309 | $options = $args['options']; |
| 310 | $x = 1; |
| 311 | $output = ''; |
| 312 | foreach ( $options as $key => $item ) { |
| 313 | if ( empty( $item['label'] ) ) { |
| 314 | continue; |
| 315 | } |
| 316 | $checked = checked( $key, $value, false ); |
| 317 | $output .= sprintf( |
| 318 | '<span class="row"><input type="radio" id="everest-forms-panel-field-%s-%s-%d" name="%s" value="%s" class="widefat %s" %s %s>', |
| 319 | sanitize_html_class( $panel_id ), |
| 320 | sanitize_html_class( $field ), |
| 321 | $x, |
| 322 | $field_name, |
| 323 | $key, |
| 324 | $input_class, |
| 325 | $checked, |
| 326 | $data_attr |
| 327 | ); |
| 328 | $output .= sprintf( |
| 329 | '<label for="everest-forms-panel-field-%s-%s-%d" class="inline">%s', |
| 330 | sanitize_html_class( $panel_id ), |
| 331 | sanitize_html_class( $field ), |
| 332 | $x, |
| 333 | $item['label'] |
| 334 | ); |
| 335 | if ( ! empty( $item['tooltip'] ) ) { |
| 336 | $output .= sprintf( ' <i class="dashicons dashicons-editor-help everest-forms-help-tooltip" title="%s"></i>', esc_attr( $item['tooltip'] ) ); |
| 337 | } |
| 338 | $output .= '</label></span>'; |
| 339 | $x ++; |
| 340 | } |
| 341 | break; |
| 342 | |
| 343 | // Select. |
| 344 | case 'select': |
| 345 | if ( empty( $args['options'] ) && empty( $args['field_map'] ) ) { |
| 346 | return ''; |
| 347 | } |
| 348 | |
| 349 | if ( ! empty( $args['field_map'] ) ) { |
| 350 | $options = array(); |
| 351 | $available_fields = evf_get_form_fields( $form_data, $args['field_map'] ); |
| 352 | if ( ! empty( $available_fields ) ) { |
| 353 | foreach ( $available_fields as $id => $available_field ) { |
| 354 | $lbl = ! empty( $available_field['label'] ) ? esc_attr( $available_field['label'] ) : esc_html__( 'Field #', 'everest-forms' ) . $id; |
| 355 | $options[ $id ] = $lbl; |
| 356 | } |
| 357 | } |
| 358 | $input_class .= ' everest-forms-field-map-select'; |
| 359 | $data_attr .= ' data-field-map-allowed="' . implode( ' ', $args['field_map'] ) . '"'; |
| 360 | if ( ! empty( $placeholder ) ) { |
| 361 | $data_attr .= ' data-field-map-placeholder="' . esc_attr( $placeholder ) . '"'; |
| 362 | } |
| 363 | } else { |
| 364 | $options = $args['options']; |
| 365 | } |
| 366 | |
| 367 | $output = sprintf( |
| 368 | '<select id="everest-forms-panel-field-%s-%s" name="%s" class="widefat %s" %s>', |
| 369 | sanitize_html_class( $panel_id ), |
| 370 | sanitize_html_class( $field ), |
| 371 | $field_name, |
| 372 | $input_class, |
| 373 | $data_attr |
| 374 | ); |
| 375 | |
| 376 | if ( ! empty( $placeholder ) ) { |
| 377 | $output .= '<option value="">' . $placeholder . '</option>'; |
| 378 | } |
| 379 | |
| 380 | foreach ( $options as $key => $item ) { |
| 381 | $output .= sprintf( '<option value="%s" %s>%s</option>', esc_attr( $key ), selected( $key, $value, false ), $item ); |
| 382 | } |
| 383 | |
| 384 | $output .= '</select>'; |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | $smarttags_class = ! empty( $args['smarttags'] ) ? 'evf_smart_tag' : ''; |
| 389 | |
| 390 | // Put the pieces together.... |
| 391 | $field_open = sprintf( |
| 392 | '<div id="everest-forms-panel-field-%s-%s-wrap" class="everest-forms-panel-field %s %s %s">', |
| 393 | sanitize_html_class( $panel_id ), |
| 394 | sanitize_html_class( $field ), |
| 395 | $class, |
| 396 | $smarttags_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 | if ( ! empty( $args['smarttags'] ) ) { |
| 414 | $smart_tag = ''; |
| 415 | |
| 416 | $type = ! empty( $args['smarttags']['type'] ) ? esc_attr( $args['smarttags']['type'] ) : 'form_fields'; |
| 417 | $form_fields = ! empty( $args['smarttags']['form_fields'] ) ? esc_attr( $args['smarttags']['form_fields'] ) : ''; |
| 418 | |
| 419 | $smart_tag .= '<a href="#" class="evf-toggle-smart-tag-display" data-type="' . $type . '" data-fields="' . $form_fields . '"><span class="dashicons dashicons-editor-code"></span></a>'; |
| 420 | $smart_tag .= '<div class="evf-smart-tag-lists" style="display: none">'; |
| 421 | $smart_tag .= '<div class="smart-tag-title">'; |
| 422 | $smart_tag .= esc_html__( 'Available Fields', 'everest-forms' ); |
| 423 | $smart_tag .= '</div><ul class="evf-fields"></ul>'; |
| 424 | if ( 'all' === $type || 'other' === $type ) { |
| 425 | $smart_tag .= '<div class="smart-tag-title other-tag-title">'; |
| 426 | $smart_tag .= esc_html__( 'Others', 'everest-forms' ); |
| 427 | $smart_tag .= '</div><ul class="evf-others"></ul>'; |
| 428 | } |
| 429 | $smart_tag .= '</div>'; |
| 430 | } else { |
| 431 | $smart_tag = ''; |
| 432 | } |
| 433 | |
| 434 | $field_label .= '</label>'; |
| 435 | } else { |
| 436 | $field_label = ''; |
| 437 | $smart_tag = ''; |
| 438 | } |
| 439 | $field_close = ! empty( $args['after'] ) ? $args['after'] : ''; |
| 440 | $field_close .= '</div>'; |
| 441 | $output = $field_open . $field_label . $output . $smart_tag . $field_close; |
| 442 | |
| 443 | // Wash our hands. |
| 444 | if ( $echo ) { |
| 445 | echo $output; // phpcs:ignore WordPress.Security.EscapeOutput |
| 446 | } else { |
| 447 | return $output; |
| 448 | } |
| 449 | } |
| 450 |