0 ) { foreach ( self::$errors as $error ) { echo '

' . esc_html( $error ) . '

'; } } elseif ( count( self::$messages ) > 0 ) { foreach ( self::$messages as $message ) { echo '

' . esc_html( $message ) . '

'; } } } /** * Settings page. * * Handles the display of the main Social Sharing settings page in admin. */ public static function output() { global $current_section, $current_tab; $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; /** * Action to output start settings */ do_action( 'user_registration_settings_start' ); wp_enqueue_script( 'user-registration-settings', UR()->plugin_url() . '/assets/js/admin/settings' . $suffix . '.js', array( 'jquery', 'jquery-ui-datepicker', 'jquery-ui-sortable', 'iris', 'tooltipster' ), UR_VERSION, true ); wp_enqueue_script( 'ur-setup' ); wp_localize_script( 'user-registration-settings', 'user_registration_settings_params', array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'user_registration_search_global_settings_nonce' => wp_create_nonce( 'user_registration_search_global_settings' ), 'user_registration_captcha_test_nonce' => wp_create_nonce( 'user_registration_captcha_test_nonce' ), 'user_registration_my_account_selection_validator_nonce' => wp_create_nonce( 'user_registration_my_account_selection_validator' ), 'user_registration_lost_password_selection_validator_nonce' => wp_create_nonce( 'user_registration_lost_password_selection_validator' ), 'user_registration_membership_pages_selection_validator_nonce' => wp_create_nonce( 'user_registration_validate_page_none' ), 'i18n_nav_warning' => esc_html__( 'The changes you made will be lost if you navigate away from this page.', 'user-registration' ), 'i18n' => array( 'captcha_success' => esc_html__( 'Captcha Test Successful !', 'user-registration' ), 'captcha_failed' => esc_html__( 'Some error occured. Please verify that the keys you entered are valid.', 'user-registration' ), 'unsaved_changes' => esc_html__( 'You have some unsaved changes. Please save and try again.', 'user-registration' ), 'pro_feature_title' => esc_html__( 'is a Pro Feature', 'user-registration' ), 'upgrade_message' => esc_html__( 'We apologize, but %title% is not available with the free version. To access this fantastic features, please consider upgrading to the %plan%.', 'user-registration' ), 'upgrade_plan' => esc_html__( 'Upgrade Plan', 'user-registration' ), 'upgrade_link' => esc_url( 'https://wpuserregistration.com/pricing/?utm_source=integration-settings&utm_medium=premium-addon-popup&utm_campaign=' . urlencode( UR()->utm_campaign ) ), ), 'is_advanced_field_active' => is_plugin_active( 'user-registration-advanced-fields/user-registration-advanced-fields.php' ), ) ); // Include settings pages. self::get_settings_pages(); // Get current tab/section. $current_tab = empty( $_GET['tab'] ) ? 'general' : sanitize_title( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification $current_section = empty( $_REQUEST['section'] ) ? '' : sanitize_title( wp_unslash( $_REQUEST['section'] ) ); // phpcs:ignore WordPress.Security.NonceVerification /** * Filter to save settings actions * * @param boolean Save Settings or Not. */ $flag = apply_filters( 'user_registration_settings_save_action', true ); if ( $flag ) { // Save settings if data has been posted. if ( ! empty( $_POST ) && ! empty( $_REQUEST['_wpnonce'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification self::save(); } } // Add any posted messages. if ( ! empty( $_GET['ur_error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification self::add_error( wp_unslash( $_GET['ur_error'] ) ); // phpcs:ignore } if ( ! empty( $_GET['ur_message'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification self::add_message( wp_unslash( $_GET['ur_error'] ) ); // phpcs:ignore } /** * Filter to get tabs for settings page * * @param array Array of settings page */ $tabs = apply_filters( 'user_registration_settings_tabs_array', array() ); if ( 'import_export' === $current_tab ) { $GLOBALS['hide_save_button'] = true; } include __DIR__ . '/views/html-admin-settings.php'; } /** * Get a setting from the settings API. * * @param mixed $option_name Option Name. * @param mixed $default Default. * * @return string */ public static function get_option( $option_name, $default = '' ) { global $current_section; if ( 'add-new-popup' === $current_section ) { return $default; } else { // Array value. if ( null !== $option_name ) { if ( strstr( $option_name, '[' ) ) { parse_str( $option_name, $option_array ); // Option name is first key. $option_name = current( array_keys( $option_array ) ); // Get value. $option_values = get_option( $option_name, '' ); $key = key( $option_array[ $option_name ] ); if ( isset( $option_values[ $key ] ) ) { $option_value = $option_values[ $key ]; } else { $option_value = null; } } else { $option_value = get_option( $option_name, null ); } } if ( is_array( $option_value ) ) { $option_value = array_map( 'stripslashes', $option_value ); } elseif ( ! is_null( $option_value ) ) { $option_value = stripslashes( $option_value ); } return null === $option_value ? $default : $option_value; } } /** * Output admin fields. * * Loops though the user registration options array and outputs each field. * * @param array $options Opens array to output. */ public static function output_fields( $options ) { $settings = ''; if ( is_array( $options ) && ! empty( $options ) ) { $back_link = isset( $options['back_link'] ) ? esc_url( $options['back_link'] ) : ''; $back_link_text = isset( $options['back_link_text'] ) ? wp_kses_post( $options['back_link_text'] ) : ''; if ( isset( $options['back_link'] ) ) { $settings .= ''; if ( isset( $options['back_link_text'] ) ) { $settings .= wp_kses_post( $back_link_text ); } $settings .= ''; } $settings .= ''; if ( ! empty( $options['desc'] ) ) { $settings .= '

' . wptexturize( wp_kses_post( $options['desc'] ) ) . '

'; } if ( isset( $options['sections'] ) ) { foreach ( $options['sections'] as $id => $section ) { if ( ! isset( $section['type'] ) ) { continue; } if ( 'card' === $section['type'] ) { $section_id = isset( $section['id'] ) ? "id='". $section['id'] . "'" : ''; $settings .= '
'; $header_css = ''; if ( isset( $section['preview_link'] ) ) { $header_css = 'display:flex; justify-content: space-between;'; } $settings .= '
'; if ( ! empty( $section['title'] ) ) { $settings .= '

' . esc_html( strtoupper( $section['title'] ) ); if ( isset( $section['back_link'] ) ) { $settings .= wp_kses_post( $section['back_link'] ); } $settings .= '

'; } if ( isset( $section['preview_link'] ) ) { $settings .= wp_kses_post( $section['preview_link'] ); } $settings .= '
'; if ( ! empty( $section['desc'] ) ) { $settings .= '

' . wptexturize( wp_kses_post( $section['desc'] ) ) . '

'; } $settings .= '
'; if ( ! empty( $id ) ) { /** * Action to output settings */ do_action( 'user_registration_settings_' . sanitize_title( $id ) ); } } if ( 'accordian' === $section['type'] ) { $available_in = isset( $section['available_in'] ) ? sanitize_text_field( wp_unslash( $section['available_in'] ) ) : ''; $is_captcha = isset( $section['settings_type'] ) ? ' ur-captcha-settings' : ''; $is_captcha_header = isset( $section['settings_type'] ) ? $is_captcha . '-header' : ''; $is_captcha_body = isset( $section['settings_type'] ) ? $is_captcha . '-body' : ''; if ( isset( $section['video_id'] ) ) { $inactive_class = 'user-registration-inactive-addon'; $extras = 'data-title="' . esc_attr( $section['title'] ) . '"'; $extras .= 'data-id="' . esc_attr( $section['id'] ) . '"'; $extras .= 'data-video="' . esc_attr( $section['video_id'] ) . '"'; $extras .= 'data-available-in="' . esc_attr( $available_in ) . '"'; $settings .= '
'; } else { $settings .= '
'; } $settings .= '
'; $settings .= '
'; $settings .= ''; $settings .= ''; $settings .= ''; if ( ! empty( $section['title'] ) ) { $settings .= '

' . esc_html( $section['title'] ); $settings .= '

'; } $settings .= '
'; $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= '
'; if ( isset( $section['video_id'] ) ) { $settings .= '
'; } else { $settings .= '
'; } if ( ! empty( $id ) ) { /** * Action to output settings */ do_action( 'user_registration_settings_' . sanitize_title( $id ) ); } } if ( isset( $section['settings'] ) && ( is_array( $section['settings'] ) || is_object( $section['settings'] ) ) ) { foreach ( $section['settings'] as $key => $value ) { if ( ! isset( $value['type'] ) ) { continue; } if ( ! isset( $value['id'] ) ) { $value['id'] = ''; } if ( ! isset( $value['row_class'] ) ) { $value['row_class'] = ''; } if ( ! isset( $value['rows'] ) ) { $value['rows'] = ''; } if ( ! isset( $value['cols'] ) ) { $value['cols'] = ''; } if ( ! isset( $value['title'] ) ) { $value['title'] = isset( $value['name'] ) ? $value['name'] : ''; } if ( ! isset( $value['class'] ) ) { $value['class'] = ''; } if ( ! isset( $value['css'] ) ) { $value['css'] = ''; } if ( ! isset( $value['default'] ) ) { $value['default'] = ''; } if ( ! isset( $value['desc'] ) ) { $value['desc'] = ''; } if ( ! isset( $value['desc_tip'] ) ) { $value['desc_tip'] = false; } if ( ! isset( $value['desc_field'] ) ) { $value['desc_field'] = false; } if ( ! isset( $value['placeholder'] ) ) { $value['placeholder'] = ''; } // Capitalize Setting Label. $value['title'] = self::capitalize_title( $value['title'] ); // Custom attribute handling. $custom_attributes = array(); if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) ) { foreach ( $value['custom_attributes'] as $attribute => $attribute_value ) { $custom_attributes[] = esc_attr( $attribute ) . '=' . esc_attr( $attribute_value ) . ''; } } // Description handling. $field_description = self::get_field_description( $value ); extract( $field_description ); // phpcs:ignore // Switch based on type. switch ( $value['type'] ) { // Standard text inputs and subtypes like 'number'. case 'text': case 'email': case 'number': case 'password': case 'date': $option_value = self::get_option( $value['id'], $value['default'] ); $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= '
'; break; case 'nonce': $settings .= '
'; $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= '
'; break; // Color picker. case 'color': $option_value = self::get_option( $value['id'], $value['default'] ); $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= '‎' . wp_kses_post( $description ); $settings .= '
'; $settings .= '
'; break; // Textarea. case 'textarea': $option_value = self::get_option( $value['id'], $value['default'] ); $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= wp_kses_post( $description ); $settings .= ''; $settings .= '
'; $settings .= '
'; break; // Select boxes. case 'select': case 'multiselect': $option_value = self::get_option( $value['id'], $value['default'] ); $settings .= '
'; $settings .= ''; $settings .= '
'; $multiple = ''; $type = ''; if ( 'multiselect' == $value['type'] ) { $type = '[]'; $multiple = 'multiple="multiple"'; } $settings .= '' . wp_kses_post( $description ); $settings .= '
'; $settings .= '
'; break; // Radio inputs. case 'radio': $option_value = self::get_option( $value['id'], $value['default'] ); $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= '
'; $settings .= wp_kses_post( $description ); $settings .= '
    '; foreach ( $value['options'] as $key => $val ) { $settings .= '
  • '; $settings .= ''; $settings .= '
  • '; } $settings .= '
'; $settings .= '
'; $settings .= '
'; $settings .= '
'; break; // Checkbox input. case 'checkbox': $option_value = self::get_option( $value['id'], $value['default'] ); $visbility_class = array(); if ( ! isset( $value['hide_if_checked'] ) ) { $value['hide_if_checked'] = false; } if ( ! isset( $value['show_if_checked'] ) ) { $value['show_if_checked'] = false; } if ( 'yes' === $value['hide_if_checked'] || 'yes' === $value['show_if_checked'] ) { $visbility_class[] = 'hidden_option'; } if ( 'option' === $value['hide_if_checked'] ) { $visbility_class[] = 'hide_options_if_checked'; } if ( 'option' === $value['show_if_checked'] ) { $visbility_class[] = 'show_options_if_checked'; } $settings .= '
'; if ( ! isset( $value['checkboxgroup'] ) || 'start' === $value['checkboxgroup'] ) { $settings .= ''; $settings .= '
'; $settings .= '
'; } else { $settings .= '
'; $settings .= '
'; } $settings .= ''; $settings .= '
'; $settings .= wp_kses_post( $description ); $settings .= wp_kses_post( $desc_field ); $settings .= '
'; break; // Single page selects. case 'single_select_page': $args = array( 'name' => $value['id'], 'id' => $value['id'], 'sort_column' => 'menu_order', 'sort_order' => 'ASC', 'show_option_none' => ' ', 'class' => $value['class'], 'echo' => false, 'selected' => absint( self::get_option( $value['id'], $value['default'] ) ), ); if ( isset( $value['args'] ) ) { $args = wp_parse_args( $value['args'], $args ); } $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= str_replace( ' id=', " data-placeholder='" . esc_attr__( 'Select a page…', 'user-registration' ) . "' style='" . esc_attr( $value['css'] ) . "' class='" . esc_attr( $value['class'] ) . "' id=", wp_dropdown_pages( $args ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $settings .= wp_kses_post( $description ); $settings .= '
'; $settings .= '
'; break; case 'tinymce': $editor_settings = array( 'name' => esc_attr( $value['id'] ), 'id' => esc_attr( $value['id'] ), 'style' => esc_attr( $value['css'] ), 'default' => esc_attr( $value['default'] ), 'class' => esc_attr( $value['class'] ), 'quicktags' => array( 'buttons' => 'em,strong,link' ), 'tinymce' => array( 'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator', 'theme_advanced_buttons2' => '', ), 'editor_css' => '', ); $option_value = self::get_option( $value['id'], $value['default'] ); $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= wp_kses_post( $description ); // Output buffer for tinymce editor. ob_start(); wp_editor( $option_value, $value['id'], $editor_settings ); $settings .= ob_get_clean(); $settings .= '
'; $settings .= '
'; break; case 'link': $settings .= '
'; $settings .= ''; $settings .= '
'; if ( isset( $value['buttons'] ) && is_array( $value['buttons'] ) ) { foreach ( $value['buttons'] as $button ) { $settings .= '' . esc_html( $button['title'] ) . ''; } } $settings .= ( isset( $value['desc'] ) && isset( $value['desc_tip'] ) && true !== $value['desc_tip'] ) ? '

' . wp_kses_post( $value['desc'] ) . '

' : ''; $settings .= '
'; $settings .= '
'; break; // Image upload. case 'image': $option_value = self::get_option( $value['id'], $value['default'] ); $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= '' . esc_attr__( 'Header Logo', 'user-registration' ) . ''; $settings .= ''; $settings .= ''; $settings .= ' '; $settings .= '
'; $settings .= '
'; wp_enqueue_media(); break; // Radio image inputs. case 'radio-image': $option_value = self::get_option( $value['id'], $value['default'] ); $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= '
    '; foreach ( $value['options'] as $key => $val ) { $settings .= '
  • '; $settings .= ''; $settings .= '
  • '; } $settings .= '
'; $settings .= '
'; $settings .= '
'; break; // Toggle input. case 'toggle': $option_value = self::get_option( $value['id'], $value['default'] ); $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= '
'; $settings .= ''; $settings .= ''; $settings .= ''; $settings .= ''; $settings .= '
'; $settings .= wp_kses_post( $description ); $settings .= wp_kses_post( $desc_field ); $settings .= '
'; $settings .= '
'; break; case 'radio-group': $option_value = self::get_option( $value['id'], $value['default'] ); $options = isset( $value['options'] ) ? $value['options'] : array(); // $args['choices'] for backward compatibility. Modified since 1.5.7. if ( ! empty( $options ) ) { $settings .= '
'; $settings .= ''; $settings .= '
'; $settings .= '
    '; foreach ( $options as $option_index => $option_text ) { $class = str_replace( ' ', '-', strtolower( $option_text ) ); $settings .= '
  • '; $checked = ''; if ( '' !== $option_value ) { $checked = checked( $option_value, trim( $option_index ), false ); } $settings .= ''; $settings .= '
  • '; } $settings .= '
'; $settings .= '
'; $settings .= '
'; } break; // Default: run an action. default: /** * Filter to retrieve default admin field for output * * @param string $settings Settings. * @param mixed $settings Field value. */ $settings = apply_filters( 'user_registration_admin_field_' . $value['type'], $settings, $value ); break; }// End switch case. } } elseif ( isset( $section['settings'] ) && is_string( $section['settings'] ) ) { $settings .= $section['settings']; } /** * Filter to retrieve extra settings for this section. * * @param string $settings Settings. * @param mixed $options Section options. */ $settings = apply_filters( 'user_registration_admin_after_global_settings', $settings, $options ); $settings .= '
'; $settings .= '
'; if ( ! empty( $section['id'] ) ) { /** * Action after output settings */ do_action( 'user_registration_settings_' . sanitize_title( $section['id'] ) . '_after' ); } }// End foreach. } } echo $settings; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * Helper function to get the formated description and tip HTML for a * given form field. Plugins can call this when implementing their own custom * settings types. * * @param array $value The form field value array. * * @return array The description and tip as a 2 element array */ public static function get_field_description( $value ) { $description = ''; $tooltip_html = ''; $desc_field = ''; if ( true === $value['desc_tip'] ) { $tooltip_html = $value['desc']; } elseif ( ! empty( $value['desc_tip'] ) ) { $description = $value['desc']; $tooltip_html = $value['desc_tip']; } elseif ( ! empty( $value['desc'] ) ) { $description = $value['desc']; } if ( ! empty( $value['desc_field'] ) ) { $desc_field = $value['desc_field']; } if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ) ) ) { $description = '

' . wp_kses_post( $description ) . '

'; } elseif ( $description && in_array( $value['type'], array( 'checkbox' ) ) ) { $description = wp_kses_post( $description ); } elseif ( $description ) { $description = ' ' . wp_kses_post( $description ) . ' '; } if ( $desc_field && in_array( $value['type'], array( 'textarea', 'radio', 'checkbox' ) ) ) { $desc_field = '

' . wp_kses_post( $desc_field ) . '

'; } elseif ( $desc_field ) { $desc_field = ' ' . wp_kses_post( $desc_field ) . ' '; } if ( $tooltip_html ) { $tooltip_html = ur_help_tip( $tooltip_html ); } return array( 'description' => $description, 'desc_field' => $desc_field, 'tooltip_html' => $tooltip_html, ); } /** * Save admin fields. * * Loops though the user registration options array and outputs each field. * * @param array $options Options array to output. * * @return bool */ public static function save_fields( $options ) { if ( empty( $_POST ) ) { // phpcs:ignore WordPress.Security.NonceVerification return false; } // Options to update will be stored here and saved later. $update_options = array(); if ( empty( $options ) ) { return false; } // Return if default wp_login is disabled and no redirect url is set. $is_wp_login_disabled_error = apply_filters( 'user_registration_settings_prevent_default_login', $_POST ); if ( $is_wp_login_disabled_error && 'redirect_login_error' === $is_wp_login_disabled_error ) { return; } // Loop options and get values to save. foreach ( $options['sections'] as $id => $section ) { if ( ! isset( $id ) || ! isset( $section['type'] ) ) { continue; } foreach ( $section['settings'] as $option ) { // Get posted value. if ( null !== $option['id'] ) { if ( strstr( $option['id'], '[' ) ) { parse_str( $option['id'], $option_name_array ); $option_name = sanitize_text_field( current( array_keys( $option_name_array ) ) ); $setting_name = key( $option_name_array[ $option_name ] ); $raw_value = isset( $_POST[ $option_name ][ $setting_name ] ) ? wp_unslash( $_POST[ $option_name ][ $setting_name ] ) : null; // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized } else { $option_name = sanitize_text_field( $option['id'] ); $setting_name = ''; $raw_value = isset( $_POST[ $option['id'] ] ) ? wp_unslash( $_POST[ $option['id'] ] ) : null; // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized } } // Format the value based on option type. switch ( $option['type'] ) { case 'checkbox': case 'toggle': $value = ur_string_to_bool( $raw_value ); break; case 'textarea': $value = wp_kses_post( trim( $raw_value ) ); break; case 'multiselect': $value = array_filter( array_map( 'ur_clean', (array) $raw_value ) ); break; case 'select': $allowed_values = empty( $option['options'] ) ? array() : array_keys( $option['options'] ); if ( empty( $option['default'] ) && empty( $allowed_values ) ) { $value = null; break; } $default = ( empty( $option['default'] ) ? $allowed_values[0] : $option['default'] ); $value = in_array( $raw_value, $allowed_values ) ? sanitize_text_field( $raw_value ) : sanitize_text_field( $default ); break; case 'tinymce': $value = wpautop( $raw_value ); break; default: $value = ur_clean( $raw_value ); break; } /** * Filter to Sanitize the value of an option. * * @param boolean $value String converted to Boolean * @param mixed $option Option to save * @param string $raw_value Option value to save */ $value = apply_filters( 'user_registration_admin_settings_sanitize_option', $value, $option, $raw_value ); /** * Filter to Sanitize the value of an option by option name. * * @param boolean $value String converted to Boolean * @param mixed $option Option value to save * @param string $raw_value Option value to save */ $value = apply_filters( "user_registration_admin_settings_sanitize_option_$option_name", $value, $option, $raw_value ); if ( is_null( $value ) || count( self::$errors ) > 0 ) { continue; } // Check if option is an array and handle that differently to single values. if ( $option_name && $setting_name ) { if ( ! isset( $update_options[ $option_name ] ) ) { $update_options[ $option_name ] = get_option( $option_name, array() ); } if ( ! is_array( $update_options[ $option_name ] ) ) { $update_options[ $option_name ] = array(); } $update_options[ $option_name ][ $setting_name ] = $value; } else { $update_options[ $option_name ] = $value; } } }// End foreach(). // Save all options in our array. foreach ( $update_options as $name => $value ) { update_option( $name, $value ); } return true; } /** * Capitalize Settings Title Phrase. * * @param string $text Setting Label. */ public static function capitalize_title( $text = null ) { $prepositions = array( 'at', 'by', 'for', 'in', 'on', 'to', 'or' ); $words = explode( ' ', $text ); $capitalized_words = array(); foreach ( $words as $word ) { $word = trim( $word ); if ( ! in_array( $word, $prepositions ) ) { // Check if the word is a shash separated terms. Eg: "Hide/Show". if ( strpos( $word, '/' ) ) { $separate_terms = explode( '/', $word ); $capitalized_terms = array(); foreach ( $separate_terms as $term ) { $capitalized_terms[] = ucfirst( $term ); } $word = implode( '/', $capitalized_terms ); } elseif ( strpos( $word, 'CAPTCHA' ) ) { $word = $word; } else { $word = ucfirst( $word ); } } $capitalized_words[] = $word; } return implode( ' ', $capitalized_words ); } /** * Search GLobal Settings. */ public static function search_settings() { $search_string = isset( $_POST['search_string'] ) ? sanitize_text_field( wp_unslash( $_POST['search_string'] ) ) : ''; //phpcs:ignore; $search_url = ''; $found = false; // Create an array of results to return as JSON. $autocomplete_results = array(); $index = 0; $settings = self::get_settings_pages(); if ( ! empty( $settings ) ) { foreach ( $settings as $key => $section ) { if ( is_bool( $section ) || ! method_exists( $section, 'get_settings' ) ) { unset( $settings[ $key ] ); continue; } $reflection = new ReflectionProperty( get_class( $section ), 'id' ); if ( ! $reflection->isPublic() ) { unset( $settings[ $key ] ); continue; } } foreach ( $settings as $section ) { $subsections = array_values( array_unique( array_merge( array( '' ), array_keys( $section->get_sections() ) ) ) ); if ( ! empty( $subsections ) ) { foreach ( $subsections as $subsection ) { if ( 'user-registration-invite-codes' === $section->id ) { if ( '' !== $subsection ) { $subsection_array = $section->get_settings( $subsection ); } } else { switch ( $subsection ) { case 'login-options': $subsection_array = get_login_options_settings(); break; case 'frontend-messages': $subsection_array = $section->get_frontend_messages_settings(); break; default: $subsection_array = $section->get_settings( $subsection ); break; } } if ( is_array( $subsection_array ) && ! empty( $subsection_array ) ) { $flattened_array = self::flatten_array( $subsection_array ); $result = self::search_string_in_array( $search_string, $flattened_array ); if ( ! empty( $result ) ) { foreach ( $result as $key => $value ) { $match = array_search( $value['title'], array_column( $autocomplete_results, 'label' ), true ); //phpcs:ignore; if ( false === $match ) { $autocomplete_results[ $index ]['label'] = $value['title']; $autocomplete_results[ $index ]['desc'] = $value['desc']; if ( ! empty( $subsection ) ) { $autocomplete_results[ $index ]['value'] = admin_url( 'admin.php?page=user-registration-settings&tab=' . $section->id . '§ion=' . $subsection . '&searched_option=' . $value['id'] ); } else { $autocomplete_results[ $index ]['value'] = admin_url( 'admin.php?page=user-registration-settings&tab=' . $section->id . '&searched_option=' . $value['id'] ); } ++$index; } } continue; } } } } } } if ( ! empty( $autocomplete_results ) ) { wp_send_json_success( array( 'results' => $autocomplete_results, ) ); } else { $autocomplete_results[ $index ]['label'] = __( 'No Search result found !', 'user-registration' ); $autocomplete_results[ $index ]['desc'] = ''; $autocomplete_results[ $index ]['value'] = 'no_result_found'; wp_send_json_success( array( 'results' => $autocomplete_results, ) ); } } /** * Search String in Array. * * @param string $string_to_search String to Search. * @param array $array Search Array. */ public static function search_string_in_array( $string_to_search, $array ) { $result = array(); if ( is_object( $array ) ) { $array = (array) $array; } $index = 0; foreach ( $array as $key => $value ) { if ( is_array( $value ) ) { foreach ( $value as $text ) { if ( ! is_array( $text ) ) { if ( stripos( $text, $string_to_search ) !== false ) { $result[ $index ]['id'] = isset( $value['id'] ) ? $value['id'] : 'true'; $result[ $index ]['title'] = isset( $value['title'] ) ? $value['title'] : 'true'; $desc_tip = isset( $value['desc_tip'] ) && true !== $value['desc_tip'] ? $value['desc_tip'] : ''; $desc = isset( $value['desc'] ) && true !== $value['desc'] ? $value['desc'] : ''; $result[ $index ]['desc'] = ! empty( $desc_tip ) ? $desc_tip : $desc; ++$index; break; } } } } } return $result; } /** * Return Non Nested Array from Nested Settings Array * * @param array $nested_array Nested Settings Array. * * @return array */ public static function flatten_array( $nested_array ) { $settings_array = array(); // create an empty array to store the list of settings. if ( isset( $nested_array['sections'] ) ) { // loop through each section in the array. foreach ( $nested_array['sections'] as $section ) { if ( isset( $section['settings'] ) ) { if ( is_string( $section['settings'] ) ) { continue; } // loop through each setting in the section and add it to the $settings_array. foreach ( $section['settings'] as $setting ) { $settings_array[] = $setting; } } else { $inner_settings = self::flatten_array( $section ); if ( ! empty( $inner_settings ) ) { $settings_array[] = $inner_settings; } } } } return $settings_array; } }