'wpbc_' * , 'db_saving_type' => 'togather' ['togather'|'separate'|'separate_prefix'] * ) * @param array $fields_values - optional array of values. If skipped, then case system try to get values from DB. */ public function __construct( $id, $options = array(), $fields_values = array() ) { $this->id = $id; // Define name of this settings page . $default_options = array( 'db_prefix_option' => 'wpbc_', // Prefix for adding to option name before saving to DB. 'db_saving_type' => 'separate' // Type of DB saving: 'togather' | 'separate' | 'separate_prefix'. /* 'togather' - saving all parameters from settings page to one DB option 'separate' - saving each settings parameters as separate DB options 'separate_prefix' - saving each settings parameters as separate DB options with prefix $id */ ); $this->options = wp_parse_args( $options, $default_options ); // Define what fields we are having. $this->init_settings_fields(); // Init all Fields Rows for settings page. // Get Values for the fields if ( empty( $fields_values ) ) { $this->define_fields_values_from_db(); // Define Fields Values from DB. } else { $this->define_fields_values( $fields_values ); // Define Fields by transmited values. } // Set Values for the fields. $this->set_values_to_fields(); // Assign $this->fields_values['some_field'] to $this->fields[ 'some_field' ]['value']. add_action( 'wpbc_after_settings_content', array( $this, 'enqueue_validate_js' ), 10, 3 ); // Add JavaScript if you need. } /** * Validate Form Fields before $_POST request * * @param string $page_tag */ public function enqueue_validate_js( $page_tag, $active_page_tab, $active_page_subtab ) { if ( empty( $this->validated_form_id ) ) { return; } // Get Fields to validate $fields_to_validate = array(); foreach ( $this->fields as $field_name => $field_values ) { if ( ( isset( $field_values['validate_as'] ) ) && ( ! empty( $field_values['validate_as'] ) ) ) { $field_html_id = $this->id . '_' . $field_name; $fields_to_validate[ $field_html_id ] = $field_values['validate_as']; } } // JavaScript . $js_script = ''; $js_script .= " jQuery('#" . $this->validated_form_id . "').on( 'submit', function(){ "; // Catch Submit event. foreach ( $fields_to_validate as $field_html_id => $field_validate_array ) { // Validate Required. $js_script .= " if ( jQuery('#" . $field_html_id . "').val() == '' ) { " . "\n"; $js_script .= " wpbc_field_highlight( '#" . $field_html_id . "' );" . "\n"; //$js_script .= " wpbc_scroll_to( '#". $field_html_id ."' );" . "\n" ; //$js_script .= " showWarningUnderElement( '#". $field_html_id ."' );" . "\n" ; $js_script .= " return false; " . "\n"; // - cancel event - Submit. $js_script .= " }" . "\n"; } $js_script .= " } ); " . "\n"; // Eneque JS to the footer of the page. wpbc_enqueue_js( $js_script ); } // ----------------------------------------------------------------------------------------------------------------- // Abstract methods // ----------------------------------------------------------------------------------------------------------------- /** * Init all fields rows for settings page */ abstract public function init_settings_fields(); // ----------------------------------------------------------------------------------------------------------------- // Functions // ----------------------------------------------------------------------------------------------------------------- /** * Get array of Default fields Names and Values. * Example: * $default_values = array( * 'my_date_format' => get_option('date_format') * , 'my_time_format' => get_option('time_format') * ); * * @return array */ public function get_default_values() { $default_values = array(); foreach ( $this->fields as $field_name => $field_data ) { if ( isset( $field_data['default'] ) ) { $default_values[ $field_name ] = $field_data['default']; } else { // Action here if no default value in field. } } return $default_values; } /** * Define "Default Values" for all exist settings Fields. * * @param array $fields_values - Optional. Field values */ public function define_fields_values( $fields_values = array(), $is_check_exist_values_from_fields = true ) { // Default . if ( $is_check_exist_values_from_fields ) { $default_values = $this->get_default_values(); } else { $default_values = array(); } // Parse. $this->fields_values = wp_parse_args( $fields_values, $default_values ); if ( $is_check_exist_values_from_fields ) { // We no need to check it after saving to DB. $defined_values_in_fields = array(); foreach ( $this->fields as $field_name => $field_data ) { if ( isset( $field_data['value'] ) ) { $defined_values_in_fields[ $field_name ] = $field_data['value']; } } $this->fields_values = wp_parse_args( $defined_values_in_fields, $this->fields_values ); } } /** * Set Values to Fiels * Assign $this->fields_values to $fields[ 'some_field' ]['value'] */ public function set_values_to_fields() { foreach ( $this->fields_values as $field_name => $field_value ) { if ( isset( $this->fields[ $field_name ] ) ) { $this->fields[ $field_name ]['value'] = $field_value; } } } /** * Get ID of this settings page * * @return string */ public function get_id() { return $this->id; } /** * Get Value of specfic Field * * @param string $field_name * * @return mixed or false, if field does not exist * */ public function get_field_value( $field_name ) { if ( empty( $this->fields_values ) ) { $this->define_fields_values(); } // If empty, then define by Default values return isset( $this->fields_values[ $field_name ] ) ? $this->fields_values[ $field_name ] : false; } /** * Set Value to specfic Field * * @param string $field_name * * @return mixed or false, if field does not exist * */ public function set_field_value( $field_name, $field_value ) { if ( empty( $this->fields_values ) ) { $this->define_fields_values(); } // If empty, then define by Default values $this->fields_values[ $field_name ] = $field_value; } /** * Get all exist form fields * and Init fields, if fields are empty. * * @return array */ public function get_form_fields() { if ( empty( $this->fields ) ) { $this->init_settings_fields(); } return $this->fields; } /** * Generate Settings Table */ public function show( $group = false ) { ?> generate_settings_rows( $group ); ?>
get_form_fields(); } $html = ''; foreach ( $form_fields as $k => $v ) { $k = $this->id . '_' . $k; if ( ! isset( $v['type'] ) || ( $v['type'] == '' ) ) { $v['type'] = 'text'; } if ( ( $group === false ) || ( ( ! isset( $v['group'] ) ) && ( $group == 'general' ) ) || ( ( isset( $v['group'] ) ) && ( $group == $v['group'] ) ) ) { if ( method_exists( $this, 'field_' . $v['type'] . '_row' ) ) { $html .= $this->{'field_' . $v['type'] . '_row'}( $k, $v ); } else { $html .= $this->{'field_text_row'}( $k, $v ); } } } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $html; } // // ----------------------------------------------------------------------------------------------------------------- // Input Fields // ----------------------------------------------------------------------------------------------------------------- /** * Text Input Row * * @param string $field_name - name of field * @param array $field - parameters * @param boolean $echo - show or return reaults {default true} * * @return string - html || nothing */ public function field_text_row( $field_name, $field, $echo = true ) { if ( ! $echo ) { return self::field_text_row_static( $field_name, $field, $echo ); } else { self::field_text_row_static( $field_name, $field, $echo ); } } /** * Text Color Row * * @param string $field_name - name of field * @param array $field - parameters * @param boolean $echo - show or return reaults {default true} * * @return string - html || nothing */ public function field_color_row( $field_name, $field, $echo = true ) { if ( ! $echo ) { return self::field_color_row_static( $field_name, $field, $echo ); } else { self::field_color_row_static( $field_name, $field, $echo ); } } /** * Textarea Row * * @param string $field_name - name of field * @param array $field - parameters * @param boolean $echo - show or return reaults {default true} * * @return string - html || nothing */ public function field_textarea_row( $field_name, $field, $echo = true ) { if ( ! $echo ) { return self::field_textarea_row_static( $field_name, $field, $echo ); } else { self::field_textarea_row_static( $field_name, $field, $echo ); } } /** * WP Rich Content Edit Textarea Row * * @param string $field_name - name of field * @param array $field - parameters * @param boolean $echo - show or return reaults {default true} * * @return string - html || nothing */ public function field_wp_textarea_row( $field_name, $field, $echo = true ) { if ( ! $echo ) { return self::field_wp_textarea_row_static( $field_name, $field, $echo ); } else { self::field_wp_textarea_row_static( $field_name, $field, $echo ); } } /** * Radio buttons field row * * @param string $field_name - name of field * @param array $field - parameters * @param boolean $echo - show or return reaults {default true} * * @return string - html || nothing */ public function field_radio_row( $field_name, $field, $echo = true ) { if ( ! $echo ) { return self::field_radio_row_static( $field_name, $field, $echo ); } else { self::field_radio_row_static( $field_name, $field, $echo ); } } /** * Selectbox field row * * @param string $field_name - name of field * @param array $field - parameters * @param boolean $echo - show or return reaults {default true} * * @return string - html || nothing */ public function field_select_row( $field_name, $field, $echo = true ) { if ( ! $echo ) { return self::field_select_row_static( $field_name, $field, $echo ); } else { self::field_select_row_static( $field_name, $field, $echo ); } } /** * Checkbox field row * * @param string $field_name - name of field * @param array $field - parameters * @param boolean $echo - show or return reaults {default true} * * @return string - html || nothing */ public function field_checkbox_row( $field_name, $field, $echo = true ) { if ( ! $echo ) { return self::field_checkbox_row_static( $field_name, $field, $echo ); } else { self::field_checkbox_row_static( $field_name, $field, $echo ); } } // ----------------------------------------------------------------------------------------------------------------- // Static Methods // ----------------------------------------------------------------------------------------------------------------- /** * Static Text Input Row * * @param string $field_name - name of field * @param array $field - parameters * @param boolean $echo - show or return reaults {default true} * * @return string - html || nothing */ public static function field_text_row_static( $field_name, $field, $echo = true ) { $defaults = array( 'title' => '', 'disabled' => false, 'class' => '', 'css' => '', 'placeholder' => '', 'type' => 'text', 'description' => '', 'attr' => array(), 'group' => 'general', 'tr_class' => '', 'only_field' => false, 'description_tag' => 'p', 'validate_as' => array() ); $field = wp_parse_args( $field, $defaults ); if ( ! $echo ) { ob_start(); } if ( ! $field['only_field'] ) { ?>
autocomplete="off" />
'', 'disabled' => false, 'class' => '', 'css' => '', 'placeholder' => '', 'type' => 'text', 'description' => '', 'attr' => array(), 'group' => 'general', 'tr_class' => '', 'only_field' => false, 'description_tag' => 'p' ); $field = wp_parse_args( $field, $defaults ); if ( ! $echo ) { ob_start(); } if ( ! $field['only_field'] ) { ?>
autocomplete="off" />
'', 'disabled' => false, 'class' => '', 'css' => '', 'placeholder' => '', 'type' => 'text', 'description' => '', 'attr' => array(), 'rows' => 3, 'cols' => 20, 'show_in_2_cols' => false, 'group' => 'general', 'tr_class' => '', 'only_field' => false, 'description_tag' => 'p', 'validate_as' => array() ); $field = wp_parse_args( $field, $defaults ); if ( ! $echo ) { ob_start(); } if ( ! $field['only_field'] ) { ?>
'; // FixIn: 9.6.3.3. echo '
'; } ?>
'', 'disabled' => false, 'class' => '', 'css' => '', 'placeholder' => '', 'type' => 'text', 'description' => '', 'attr' => array(), 'rows' => 3, 'editor_height' => '', 'cols' => 20, 'teeny' => true, 'show_visual_tabs' => true, 'default_editor' => 'tinymce', // 'tinymce' | 'html' // 'html' is used for the "Text" editor tab. 'drag_drop_upload' => false, 'show_in_2_cols' => false, 'group' => 'general', 'tr_class' => '', 'only_field' => false, 'description_tag' => 'p' ); $field = wp_parse_args( $field, $defaults ); if ( ! $echo ) { ob_start(); } if ( ! $field['only_field'] ) { ?>
'; // FixIn: 9.6.3.3. echo '
'; } ?> false, 'media_buttons' => false, 'textarea_name' => esc_attr( $field_name ), 'textarea_rows' => $field['rows'], 'editor_class' => 'wpbc-textarea-tinymce ' . esc_attr( $field['class'] ) // Any extra CSS Classes to append to the Editor textarea , 'teeny' => esc_attr( $field['teeny'] ) // Whether to output the minimal editor configuration used in PressThis , 'drag_drop_upload' => esc_attr( $field['drag_drop_upload'] ) // Enable Drag & Drop Upload Support (since WordPress 3.9) , 'tinymce' => $field['show_visual_tabs'] // Remove Visual Mode from the Editor , 'default_editor' => $field['default_editor'] // 'tinymce' | 'html' // 'html' is used for the "Text" editor tab. , 'editor_height' => $field['editor_height'] // '' | '500' ) ); echo wp_kses_post( self::description_static( $field, $field['description_tag'] ) ); ?>
'', 'label' => '', 'disabled' => false, 'disabled_options' => array(), 'class' => '', 'css' => '', 'type' => 'radio', 'description' => '', 'attr' => array(), 'options' => array(), 'group' => 'general', 'tr_class' => '', 'only_field' => false, 'is_new_line' => false, 'description_tag' => 'span' ); $field = wp_parse_args( $field, $defaults ); if ( ! $echo ) { ob_start(); } if ( ! $field['only_field'] ) { ?>
$option_title ) { $option_parameters = array(); if ( is_array( $option_title ) ) { $option_parameters = $option_title; $option_title = $option_title['title']; } ?>' : '     '; if ( isset( $option_parameters['html'] ) ) { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $option_parameters['html'] ; } } ?>
'', 'label' => '', 'disabled' => false, 'disabled_options' => array(), 'class' => '', 'css' => '', 'type' => 'select', 'description' => '', 'multiple' => false, 'attr' => array(), 'options' => array(), 'group' => 'general', 'tr_class' => '', 'only_field' => false, 'description_tag' => 'span' ); $field = wp_parse_args( $field, $defaults ); if ( ! $echo ) { ob_start(); } if ( ! $field['only_field'] ) { ?>
'', 'label' => '', 'disabled' => false, 'class' => '', 'css' => '', 'type' => 'checkbox', 'description' => '', 'attr' => array(), 'group' => 'general', 'tr_class' => '', 'only_field' => false, 'is_new_line' => true, 'description_tag' => 'span' ); $field = wp_parse_args( $field, $defaults ); if ( ! $echo ) { ob_start(); } if ( ! $field['only_field'] ) { ?>
', '/i>', $field['label'] ); $params_checkbox = array( 'id' => $field_name // HTML ID of element , 'name' => $field_name, 'label' => array( 'title' => wp_kses_post( $field['label'] ), 'position' => 'right' ) // FixIn: 9.6.1.5. , 'toggle_style' => $field['css'] // CSS of select element , 'class' => $field['class'] // CSS Class of select element , 'disabled' => $field['disabled'], 'attr' => $field['attr'] // Any additional attributes, if this radio | checkbox element , 'legend' => wp_kses_post( $field['title'] ) // aria-label parameter , 'value' => $field['value'] // Some Value from optins array that selected by default , 'selected' => ( ( 'On' == $field['value'] ) ? true : false ) // Selected or not //, 'onfocus' => "console.log( 'ON FOCUS:', jQuery( this ).is(':checked') , 'in element:' , jQuery( this ) );" // JavaScript code //, 'onchange' => "wpbc_ajx_booking_send_search_request_with_params( {'ui_usr__send_emails': (jQuery( this ).is(':checked') ? 'send' : 'not_send') } );" // JavaScript code //, 'hint' => array( 'title' => __('Send email notification to customer about this operation' ,'booking') , 'position' => 'top' ) ); $params_checkbox = wp_parse_args( $params_checkbox, $field ); wpbc_flex_toggle( $params_checkbox ); ?> ' : '     '; echo wp_kses_post( self::description_static( $field, $field['description_tag'] ) ); } else { ?> ' : '     '; ?>
array(), 'class' => '', 'css' => '', 'description' => '', 'cols' => 1, 'group' => 'general', 'tr_class' => '', 'description_tag' => 'span', 'only_field' => false ); $field = wp_parse_args( $field, $defaults ); if ( ! $echo ) { ob_start(); } if ( ! $field['only_field'] ) { ?>

'', 'css' => '', 'group' => 'general', 'tr_class' => '' ); $field = wp_parse_args( $field, $defaults ); if ( ! $echo ) { ob_start(); } ?>
'', 'cols' => 1, 'group' => 'general', 'tr_class' => '' ); $field = wp_parse_args( $field, $defaults ); if ( ! $echo ) { ob_start(); } ?> '', 'group' => 'general' ); $field = wp_parse_args( $field, $defaults ); if ( ! $echo ) { ob_start(); } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $field['html'] ; if ( ! $echo ) { return ob_get_clean(); } } /** * JavaScript Row - insert JavaScript * * @param string $field_name - name of field * @param array $field - parameters * @param boolean $echo - show or return reaults {default true} * * @return string - html || nothing */ public function field_js_row( $field_name, $field, $echo = true ) { if ( ! $echo ) { return self::field_js_row_static( $field_name, $field, $echo ); } else { self::field_js_row_static( $field_name, $field, $echo ); } } /** * Static JavaScript Row - insert JavaScript * * @param string $field_name - name of field * @param array $field - parameters * @param boolean $echo - show or return reaults {default true} * * @return string - html || nothing */ public static function field_js_row_static( $field_name, $field, $echo = true ) { $defaults = array( 'js' => '', 'group' => 'general' ); $field = wp_parse_args( $field, $defaults ); if ( ! $echo ) { ob_start(); } ?> $attr_v ) { $attributes[] = esc_attr( $attr ) . '="' . esc_attr( $attr_v ) . '"'; } } return implode( ' ', $attributes ); } /** * Get Description * * @param array $field * @param string $html_tag - HTML element, which will be used as separator. Default - 'p' * * @return string */ public static function description_static( $field, $html_tag = 'p' ) { if ( empty( $html_tag ) ) { $html_tag = 'p'; } return $field['description'] ? '<' . $html_tag . ' class="description">' . wp_kses_post( $field['description'] ) . '' . "\n" : ''; } public static function label_static( $field_name, $field ) { if ( empty( $field['title'] ) ) { return ''; } $defaults = array( 'title' => '', 'label_class' => '', 'label_css' => '', 'type' => '' ); $field = wp_parse_args( $field, $defaults ); if ( ! empty( $field['type'] ) ) { $field['label_class'] .= ' wpbc-form-' . $field['type']; } if ( ! empty( $field['label_css'] ) ) { $field['label_css'] = ' style="' . $field['label_css'] . '"'; } return ''; } // // ----------------------------------------------------------------------------------------------------------------- // Validate POSTs // ----------------------------------------------------------------------------------------------------------------- /** * Validate Settings Field Data. * * Validate the data on the "Settings" form. * * @since 1.0.0 * @uses method_exists() * @param array $form_fields (default: array()) */ public function validate_post( $form_fields = array() ) { // Check server restrictions in php.ini file relative to length of $_POST variabales if ( function_exists ('wpbc_check_post_key_max_number')) { wpbc_check_post_key_max_number(); } if ( ! $form_fields ) { $form_fields = $this->get_form_fields(); } $this->validated_fields = array(); foreach ( $form_fields as $k => $v ) { if ( ! empty( $v['is_demo_safe'] ) ) { // Skip saving values for fields with this parameter ( 'is_demo_safe' => true ) continue; } if ( empty( $v['type'] ) ) { $v['type'] = 'text'; } // Look for a validate_FIELDID_post method for special handling if ( method_exists( $this, 'validate_' . $k . '_post' ) ) { $field = $this->{'validate_' . $k . '_post'}( $this->id . '_' . $k ); $this->validated_fields[ $k ] = $field; // Look for a validate_FIELDTYPE_post method } elseif ( method_exists( $this, 'validate_' . $v['type'] . '_post' ) ) { $field = $this->{'validate_' . $v['type'] . '_post'}( $this->id . '_' . $k ); $this->validated_fields[ $k ] = $field; // Default to text } else { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing if ( isset( $_POST[ $this->id . '_' . $k ] ) ) { //Check this for non Fields elements, like simple HTML or TEXT or JS $field = $this->{'validate_text_post'}( $this->id . '_' . $k ); $this->validated_fields[ $k ] = $field; } } } return $this->validated_fields; } // /** * Validate Text in POST request - escape data correctly. * * @param string $post_key - key for POST * @return string | false, if no such POST */ public function validate_text_post( $post_key ) { return self::validate_text_post_static( $post_key ); } // FixIn: 10.14.2.2 /** * Validate Color: '#ff00ff' in POST request - escape data correctly. * * @param string $post_key - key for POST * @return string | false, if no such POST */ public function validate_color_post( $post_key ) { return self::validate_color_static( $post_key ); } // FixIn: 10.14.2.2 /** * Static Validate Color field in POST request - escape data correctly. * * @param string $post_key - key for POST * @return string | false, if no such POST */ public static function validate_color_static( $post_key ) { $value = ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing if ( isset( $_POST[ $post_key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $value = sanitize_hex_color( trim( stripslashes( $_POST[ $post_key ] ) ) ); if ( null === $value ) { $value = ''; } } return $value; } /** * Static Validate Text in POST request - escape data correctly. * * @param string $post_key - key for POST * @return string | false, if no such POST */ public static function validate_text_post_static( $post_key, $post_index = false ) { $value = false; // FixIn: 9.2.4.2. // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing if ( isset( $_POST[ $post_key ] ) ) { if ( $post_index !== false ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.InputNotValidated $post_value = $_POST[ $post_key ][ $post_index ]; } else { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.InputNotValidated $post_value = $_POST[ $post_key ]; } } if ( isset( $post_value ) ) { $value = wp_kses_post( trim( stripslashes( $post_value ) ) ); } return $value; } /** * Validate Email field in POST request - escape data correctly. * * @param string $post_key - key for POST * @return string | false, if no such POST */ public function validate_email_post( $post_key ) { return self::validate_email_post_static( $post_key ); } /** * Static Validate Email field in POST request - escape data correctly. * * @param string $post_key - key for POST * @return string | false, if no such POST */ public static function validate_email_post_static( $post_key ) { $value = false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing if ( isset( $_POST[ $post_key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $value = sanitize_email( trim( stripslashes( $_POST[ $post_key ] ) ) ); } return $value; } /** * Validate Textarea in POST request - escape data correctly. * * @param string $post_key - key for POST * @return string | false, if no such POST */ public function validate_textarea_post( $post_key ) { return self::validate_textarea_post_static( $post_key ); } /** * Static Validate Textarea in POST request - escape data correctly. * * @param string $post_key - key for POST * @return string | false, if no such POST */ public static function validate_textarea_post_static( $post_key ) { $value = false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing if ( isset( $_POST[ $post_key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $value = wp_kses( trim( stripslashes( $_POST[ $post_key ] ) ), array_merge( array( 'iframe' => array( 'src' => true, 'style' => true, 'id' => true, 'class' => true ) ), wp_kses_allowed_html( 'post' ) ) ); } return $value; } /** * Validate WP Textarea in POST request - escape data correctly. * * Same as Textarea * * @param string $post_key - key for POST * @return string | false, if no such POST */ public function validate_wp_textarea_post( $post_key ){ return self::validate_wp_textarea_post_static( $post_key ); } /** * Static Validate WP Textarea in POST request - escape data correctly. * * Same as Textarea * * @param string $post_key - key for POST * @return string | false, if no such POST */ public static function validate_wp_textarea_post_static( $post_key ){ return self::validate_textarea_post_static( $post_key ); } /** * Validate Checkbox in POST request - escape data correctly. * * @param string $post_key - key for POST * @return string: 'On' | 'Off' */ public function validate_checkbox_post( $post_key ) { return self::validate_checkbox_post_static( $post_key ); } /** * Static Validate Checkbox in POST request - escape data correctly. * * @param string $post_key - key for POST * @return string: 'On' | 'Off' */ public static function validate_checkbox_post_static( $post_key ) { $status = 'Off'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing if ( isset( $_POST[ $post_key ] ) && ( in_array( $_POST[ $post_key ], array( 'On', 'Off' ) ) ) ) { $status = 'On'; } return $status; } /** * Validate Select in POST request - escape data correctly. * * @param string $post_key - key for POST * @return string | array | false, if no such POST */ public function validate_select_post( $post_key ) { return self::validate_select_post_static( $post_key ); } /** * Static Validate Select in POST request - escape data correctly. * * @param string $post_key - key for POST * @return string | array | false, if no such POST */ public static function validate_select_post_static( $post_key ) { $value = false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing if ( isset( $_POST[ $post_key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing if ( is_array( $_POST[ $post_key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $value = array_map( 'sanitize_text_field', array_map( 'stripslashes', (array) $_POST[ $post_key ] ) ); } else { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $value = sanitize_text_field( stripslashes( $_POST[ $post_key ] ) ); } } return $value; } /** * Validate Radio in POST request - escape data correctly. * * Its the same as Select field * * @param string $post_key - key for POST * @return string | array | false, if no such POST */ public function validate_radio_post( $post_key ) { return self::validate_radio_post_static( $post_key ); } /** * Static Validate Radio in POST request - escape data correctly. * * Its the same as Select field * * @param string $post_key - key for POST * @return string | array | false, if no such POST */ public static function validate_radio_post_static( $post_key ) { return self::validate_select_post_static( $post_key ); } // // ----------------------------------------------------------------------------------------------------------------- // Save | Get from DB // ----------------------------------------------------------------------------------------------------------------- /** * Save Setting Options to DB, then Reinit Settings Fields with these new Values... * * @param string $settings_id - ID of the settings * @param array $validated_fields - List of validated fields in format array( field_name => field_value, ... ) * @param string $how_to_save - 'togather' - default - save as one field | 'separately_with_prefix' - save separately but with adding settings_id | 'separately' - separately each field only with adding db prefix */ public function save_to_db( $validated_fields ){ $settings_id = $this->get_id(); $how_to_save = $this->options['db_saving_type']; if ( $how_to_save == 'togather' ) { /* wpbc_ settings_general = Array ( [date_format] => F j, Y [time_format] => H:i [hr_time_format] => ) */ update_bk_option( $this->options['db_prefix_option'] . $settings_id , $validated_fields ); } elseif ( $how_to_save == 'separate_prefix' ) { // wpbc_ settings_general_ date_format => F j, Y foreach ( (array) $validated_fields as $field_name => $field_value ) { update_bk_option( $this->options['db_prefix_option'] . $settings_id . '_' . $field_name , $field_value ); } } else { // $how_to_save == 'separate' // wpbc_ date_format => F j, Y foreach ( (array) $validated_fields as $field_name => $field_value ) { update_bk_option( $this->options['db_prefix_option'] . $field_name , $field_value ); } } // Redefine fields values for New Saved values $this->define_fields_values( $validated_fields , false ); // Set Values for the fields $this->set_values_to_fields(); // Assign $this->fields_values['some_field'] to $this->fields[ 'some_field' ]['value'] $this->fields = apply_filters( 'wpbc_fields_after_saving_to_db', $this->fields, $this->id ); //debuge('After Saving to DB', $this->fields_values, $this->fields); } public function define_fields_values_from_db() { $how_to_save = $this->options['db_saving_type']; switch ( $how_to_save ) { case 'togather': $fields_values_from_db = get_bk_option( $this->options['db_prefix_option'] . $this->get_id() ); $this->define_fields_values( $fields_values_from_db ); // Define Fields by values from DB break; case 'separate_prefix': $this->define_fields_values(); // Reinit Fields - need to know how many fields and names of fields $fields_values_from_db = array(); foreach ( $this->fields_values as $field_name => $field_value ) { $got_value = get_bk_option( $this->options['db_prefix_option'] . $this->get_id() . '_' . $field_name ); // If we do not have this value in DB ( === false ), then do not assing this value - will have Default Value if ( $got_value !== false ) $fields_values_from_db[ $field_name ] = $got_value; } $this->define_fields_values( $fields_values_from_db ); // Define Fields by values from DB break; default: // 'separate' $this->define_fields_values(); // Reinit Fields - need to know how many fields and names of fields $fields_values_from_db = array(); //debuge('Before Loading from DB', $this->fields_values, $this->fields); foreach ( $this->fields_values as $field_name => $field_value ) { $got_value = get_bk_option( $this->options['db_prefix_option'] . $field_name ); // If we do not have this value in DB ( === false ), then do not assing this value - will have Default Value if ( $got_value !== false ) $fields_values_from_db[ $field_name ] = $got_value; } $this->define_fields_values( $fields_values_from_db ); // Define Fields by values from DB //debuge('After loaded from DB', $this->fields_values, $this->fields); break; } } // ----------------------------------------------------------------------------------------------------------------- // Install | Uninstall // ----------------------------------------------------------------------------------------------------------------- /** Actiovation of Plugin. Save to DB initial values of Settings Fields. */ public function activate() { $settings_id = $this->get_id(); $how_to_save = $this->options['db_saving_type']; $default_values = $this->get_default_values(); // Get "Default" values from $this->fields array. if ( $how_to_save == 'togather' ) { /* wpbc_ settings_general = Array ( [date_format] => F j, Y [time_format] => H:i [hr_time_format] => ) */ add_bk_option( $this->options['db_prefix_option'] . $settings_id , $default_values ); } elseif ( $how_to_save == 'separate_prefix' ) { // wpbc_ settings_general_ date_format => F j, Y foreach ( (array) $default_values as $field_name => $field_value ) { add_bk_option( $this->options['db_prefix_option'] . $settings_id . '_' . $field_name , $field_value ); } } else { // $how_to_save == 'separate' // wpbc_ date_format => F j, Y foreach ( (array) $default_values as $field_name => $field_value ) { add_bk_option( $this->options['db_prefix_option'] . $field_name , $field_value ); } } } /** Uninstall. Deactivation of Plugin. Delete Settings Fields from DB. */ public function deactivate() { $settings_id = $this->get_id(); $how_to_save = $this->options['db_saving_type']; $default_values = $this->get_default_values(); // Get "Default" values from $this->fields array. if ( $how_to_save == 'togather' ) { /* wpbc_ settings_general = Array ( [date_format] => F j, Y [time_format] => H:i [hr_time_format] => ) */ delete_bk_option( $this->options['db_prefix_option'] . $settings_id ); } elseif ( $how_to_save == 'separate_prefix' ) { // wpbc_ settings_general_ date_format => F j, Y foreach ( (array) $default_values as $field_name => $field_value ) { delete_bk_option( $this->options['db_prefix_option'] . $settings_id . '_' . $field_name ); } } else { // $how_to_save == 'separate' // wpbc_ date_format => F j, Y foreach ( (array) $default_values as $field_name => $field_value ) { delete_bk_option( $this->options['db_prefix_option'] . $field_name ); } } } }