PHP Class WC_Settings_API

Show file Open project: woocommerce/woocommerce Class Usage Examples

Public Properties

Property Type Description
$errors Validation errors.
$form_fields array Form option fields.
$id string ID of the class extending the settings API. Used in option names.
$plugin_id string The plugin ID. Used for option names.
$settings array Setting values.

Protected Properties

Property Type Description
$data array The posted settings data. When empty, $_POST data will be used.

Public Methods

Method Description
add_error ( string $error ) Add an error message for display in admin on save.
admin_options ( ) Output the admin options table.
display_errors ( ) Display admin error messages.
format_settings ( array $value ) : array Format settings if needed.
generate_checkbox_html ( mixed $key, mixed $data ) : string Generate Checkbox HTML.
generate_color_html ( mixed $key, mixed $data ) : string Generate Color Picker Input HTML.
generate_decimal_html ( mixed $key, mixed $data ) : string Generate Decimal Input HTML.
generate_multiselect_html ( mixed $key, mixed $data ) : string Generate Multiselect HTML.
generate_password_html ( mixed $key, mixed $data ) : string Generate Password Input HTML.
generate_price_html ( mixed $key, mixed $data ) : string Generate Price Input HTML.
generate_select_html ( mixed $key, mixed $data ) : string Generate Select HTML.
generate_settings_html ( array $form_fields = [], $echo = true ) : string Generate Settings HTML.
generate_text_html ( mixed $key, mixed $data ) : string Generate Text Input HTML.
generate_textarea_html ( mixed $key, mixed $data ) : string Generate Textarea HTML.
generate_title_html ( mixed $key, mixed $data ) : string Generate Title HTML.
get_custom_attribute_html ( array $data ) : string Get custom attributes.
get_description_html ( array $data ) : string Get HTML for descriptions.
get_errors ( ) Get admin error messages.
get_field_default ( array $field ) : string Get a fields default value. Defaults to "" if not set.
get_field_key ( mixed $key ) : string Prefix key for settings.
get_field_type ( array $field ) : string Get a fields type. Defaults to "text" if not set.
get_field_value ( string $key, array $field, array $post_data = [] ) : string Get a field's posted and validated value.
get_form_fields ( ) : array Get the form fields after they are initialized.
get_option ( string $key, mixed $empty_value = null ) : string get_option function.
get_option_key ( ) : string Return the name of the option in the WP DB.
get_post_data ( ) : array Returns the POSTed data, to be used to save the settings.
get_tooltip_html ( array $data ) : string Get HTML for tooltips.
init_form_fields ( ) : string Initialise settings form fields.
init_settings ( ) Initialise Settings.
process_admin_options ( ) : boolean Processes and saves options.
set_post_data ( $data = [] ) Sets the POSTed data. This method can be used to set specific data, instead of taking it from the $_POST array.
validate_checkbox_field ( string $key, string | null $value ) : string Validate Checkbox Field.
validate_decimal_field ( string $key, string | null $value ) : string Validate Decimal Field.
validate_multiselect_field ( string $key, string $value ) : string Validate Multiselect Field.
validate_password_field ( string $key, string | null $value ) : string Validate Password Field. No input sanitization is used to avoid corrupting passwords.
validate_price_field ( string $key, string | null $value ) : string Validate Price Field.
validate_select_field ( string $key, string $value ) : string Validate Select Field.
validate_settings_fields ( $form_fields = [] ) Validate the data on the "Settings" form.
validate_text_field ( string $key, string | null $value ) : string Validate Text Field.
validate_textarea_field ( string $key, string | null $value ) : string Validate Textarea Field.

Protected Methods

Method Description
set_defaults ( $field ) Set default required properties for each field.

Method Details

add_error() public method

Add an error message for display in admin on save.
public add_error ( string $error )
$error string

admin_options() public method

Output the admin options table.
public admin_options ( )

display_errors() public method

Display admin error messages.
public display_errors ( )

format_settings() public method

Format settings if needed.
Deprecation: 2.6.0 Unused
public format_settings ( array $value ) : array
$value array
return array

generate_checkbox_html() public method

Generate Checkbox HTML.
Since: 1.0.0
public generate_checkbox_html ( mixed $key, mixed $data ) : string
$key mixed
$data mixed
return string

generate_color_html() public method

Generate Color Picker Input HTML.
Since: 1.0.0
public generate_color_html ( mixed $key, mixed $data ) : string
$key mixed
$data mixed
return string

generate_decimal_html() public method

Generate Decimal Input HTML.
Since: 1.0.0
public generate_decimal_html ( mixed $key, mixed $data ) : string
$key mixed
$data mixed
return string

generate_multiselect_html() public method

Generate Multiselect HTML.
Since: 1.0.0
public generate_multiselect_html ( mixed $key, mixed $data ) : string
$key mixed
$data mixed
return string

generate_password_html() public method

Generate Password Input HTML.
Since: 1.0.0
public generate_password_html ( mixed $key, mixed $data ) : string
$key mixed
$data mixed
return string

generate_price_html() public method

Generate Price Input HTML.
Since: 1.0.0
public generate_price_html ( mixed $key, mixed $data ) : string
$key mixed
$data mixed
return string

generate_select_html() public method

Generate Select HTML.
Since: 1.0.0
public generate_select_html ( mixed $key, mixed $data ) : string
$key mixed
$data mixed
return string

generate_settings_html() public method

Generate the HTML for the fields on the "settings" screen.
Since: 1.0.0
public generate_settings_html ( array $form_fields = [], $echo = true ) : string
$form_fields array (default: array())
return string the html for the settings

generate_text_html() public method

Generate Text Input HTML.
Since: 1.0.0
public generate_text_html ( mixed $key, mixed $data ) : string
$key mixed
$data mixed
return string

generate_textarea_html() public method

Generate Textarea HTML.
Since: 1.0.0
public generate_textarea_html ( mixed $key, mixed $data ) : string
$key mixed
$data mixed
return string

generate_title_html() public method

Generate Title HTML.
Since: 1.0.0
public generate_title_html ( mixed $key, mixed $data ) : string
$key mixed
$data mixed
return string

get_custom_attribute_html() public method

Get custom attributes.
public get_custom_attribute_html ( array $data ) : string
$data array
return string

get_description_html() public method

Get HTML for descriptions.
public get_description_html ( array $data ) : string
$data array
return string

get_errors() public method

Get admin error messages.
public get_errors ( )

get_field_default() public method

Get a fields default value. Defaults to "" if not set.
public get_field_default ( array $field ) : string
$field array
return string

get_field_key() public method

Prefix key for settings.
public get_field_key ( mixed $key ) : string
$key mixed
return string

get_field_type() public method

Get a fields type. Defaults to "text" if not set.
public get_field_type ( array $field ) : string
$field array
return string

get_field_value() public method

Get a field's posted and validated value.
public get_field_value ( string $key, array $field, array $post_data = [] ) : string
$key string
$field array
$post_data array
return string

get_form_fields() public method

Get the form fields after they are initialized.
public get_form_fields ( ) : array
return array of options

get_option() public method

Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
public get_option ( string $key, mixed $empty_value = null ) : string
$key string
$empty_value mixed
return string The value specified for the option or a default value for the option.

get_option_key() public method

Return the name of the option in the WP DB.
Since: 2.6.0
public get_option_key ( ) : string
return string

get_post_data() public method

Returns the POSTed data, to be used to save the settings.
public get_post_data ( ) : array
return array

get_tooltip_html() public method

Get HTML for tooltips.
public get_tooltip_html ( array $data ) : string
$data array
return string

init_form_fields() public method

Add an array of fields to be displayed on the gateway's settings screen.
Since: 1.0.0
public init_form_fields ( ) : string
return string

init_settings() public method

Store all settings in a single database entry and make sure the $settings array is either the default or the settings stored in the database.
Since: 1.0.0
public init_settings ( )

process_admin_options() public method

If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out.
public process_admin_options ( ) : boolean
return boolean was anything saved?

set_defaults() protected method

Set default required properties for each field.
protected set_defaults ( $field )

set_post_data() public method

Sets the POSTed data. This method can be used to set specific data, instead of taking it from the $_POST array.
public set_post_data ( $data = [] )

validate_checkbox_field() public method

If not set, return "no", otherwise return "yes".
public validate_checkbox_field ( string $key, string | null $value ) : string
$key string
$value string | null Posted Value
return string

validate_decimal_field() public method

Make sure the data is escaped correctly, etc.
public validate_decimal_field ( string $key, string | null $value ) : string
$key string
$value string | null Posted Value
return string

validate_multiselect_field() public method

Validate Multiselect Field.
public validate_multiselect_field ( string $key, string $value ) : string
$key string
$value string Posted Value
return string

validate_password_field() public method

Validate Password Field. No input sanitization is used to avoid corrupting passwords.
public validate_password_field ( string $key, string | null $value ) : string
$key string
$value string | null Posted Value
return string

validate_price_field() public method

Make sure the data is escaped correctly, etc.
public validate_price_field ( string $key, string | null $value ) : string
$key string
$value string | null Posted Value
return string

validate_select_field() public method

Validate Select Field.
public validate_select_field ( string $key, string $value ) : string
$key string
$value string Posted Value
return string

validate_settings_fields() public method

Validate the data on the "Settings" form.
Deprecation: 2.6.0 No longer used
public validate_settings_fields ( $form_fields = [] )

validate_text_field() public method

Make sure the data is escaped correctly, etc.
public validate_text_field ( string $key, string | null $value ) : string
$key string Field key
$value string | null Posted Value
return string

validate_textarea_field() public method

Validate Textarea Field.
public validate_textarea_field ( string $key, string | null $value ) : string
$key string
$value string | null Posted Value
return string

Property Details

$data protected property

The posted settings data. When empty, $_POST data will be used.
protected array $data
return array

$errors public property

Validation errors.
public $errors

$form_fields public property

Form option fields.
public array $form_fields
return array

$id public property

ID of the class extending the settings API. Used in option names.
public string $id
return string

$plugin_id public property

The plugin ID. Used for option names.
public string $plugin_id
return string

$settings public property

Setting values.
public array $settings
return array