* @copyright Copyright (c) 2017, Rahul Aryan * @license http://opensource.org/licenses/gpl-3.0.php GNU Public License */ namespace AnsPress\Form; // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Base field class. * * @since 4.1.0 */ class Field { /** * The current field name. * * @var string */ public $field_name = ''; /** * The original field name. * * @var string */ public $original_name = ''; /** * The form name. * * @var string */ public $form_name; /** * Unique name without square brackets. * * @var string */ public $field_id = ''; /** * The field arguments. * * @var array */ public $args = []; /** * The field HTML markup. * * @var string */ protected $html = ''; /** * The HTML output order. * * @var array */ protected $output_order = []; /** * The errors. * * @var array */ public $errors = []; /** * Field type. * * @var string */ public $type = 'input'; /** * Child fields. * * @var object */ public $child; /** * Is editing. * * @var boolean */ public $editing = false; /** * Editing post ID. * * @var boolean|integer */ public $editing_id = false; /** * Is validated? * * @var boolean */ protected $validated = false; /** * The validation callbacks. * * @var array */ protected $validate_cb = []; /** * The sanitization callback. * * @var array */ protected $sanitize_cb = []; /** * Is sanitized? * * @var boolean */ protected $sanitized = false; /** * The sanitized values. * * @var array */ public $sanitized_value; /** * The field value (unsafe). * * @var mixed */ public $value = null; /** * Initialize the class. * * @param string $form_name Name of parent form. * @param string $name Name of field. * @param array $args Field arguments. */ public function __construct( $form_name, $name, $args ) { $this->original_name = $name; $this->field_name = $form_name . '[' . $name . ']'; $this->form_name = $form_name; $this->args = $args; $this->field_id = $this->id(); $this->prepare(); } /** * Prepare field. * * @return void */ protected function prepare() { $this->sanitize_cb(); $this->validate_cb(); } /** * Get parent form. * * @return object */ public function form() { $form_name = explode( '.', ap_to_dot_notation( $this->form_name ) ); return anspress()->get_form( $form_name[0] ); } /** * Parse sanitization callbacks. * * @return void */ protected function sanitize_cb() { if ( ! empty( $this->args['sanitize'] ) ) { if ( is_array( $this->args['sanitize'] ) ) { $this->sanitize_cb = array_unique( $this->args['sanitize'] ); } else { $this->sanitize_cb = array_unique( explode( ',', $this->args['sanitize'] ) ); } } } /** * Parse validation callbacks. * * @return void */ protected function validate_cb() { if ( ! empty( $this->args['validate'] ) ) { if ( is_array( $this->args['validate'] ) ) { $this->validate_cb = array_unique( $this->args['validate'] ); } else { $this->validate_cb = array_unique( explode( ',', $this->args['validate'] ) ); } } } /** * Get a value from a path or default value if the path doesn't exist * * @param string $key Path. * @param mixed $default Default value. * @param array $array Array to search. * @return mixed */ public function get( $key, $default = null, $array = null ) { $keys = explode( '.', (string) $key ); if ( null === $array ) { $array = &$this->args; } foreach ( $keys as $key ) { if ( ! is_array( $array ) || ! array_key_exists( $key, $array ) ) { return $default; } $array = &$array[ $key ]; } return $array; } /** * Add HTML markup to property. * * @param string $html Html as string. * @return void */ public function add_html( $html ) { $this->html = $this->html . $html; } /** * Order of HTML markup. * * @return void * @since 4.1.8 Allow overriding order from arguments. */ protected function html_order() { if ( empty( $this->args['output_order'] ) ) { $this->output_order = [ 'wrapper_start', 'label', 'field_wrap_start', 'errors', 'field_markup', 'desc', 'field_wrap_end', 'wrapper_end' ]; } else { $this->output_order = $this->args['output_order']; } } /** * Create field markup. * * @return null|string */ public function output() { $this->html_order(); foreach ( (array) $this->output_order as $method ) { if ( method_exists( $this, $method ) ) { $this->add_html( $this->$method() ); } } if ( ! empty( $this->html ) ) { return $this->html; // xss okay. } } /** * Get POST (unsafe) value of a field. * * @return null|mixed */ public function unsafe_value() { $request_value = $this->get( ap_to_dot_notation( $this->field_name ), null, $_REQUEST ); if ( isset( $request_value ) ) { return wp_unslash( $request_value ); } } /** * Check if request value is set. * * @return boolean */ public function isset_value() { $request_value = $this->get( ap_to_dot_notation( $this->field_name ), null, $_REQUEST ); if ( is_null( $request_value ) ) { return false; } return true; } /** * Get value of a field. * * @param mixed $custom_val Set custom value for field. * @return mixed * * @since 4.1.8 Pass a value to set it as value of field. */ public function value( $custom_val = null ) { if ( null !== $custom_val ) { $this->value = $custom_val; return true; } if ( ! is_null( $this->value ) ) { return $this->value; } $sanitized = $this->sanitize(); if ( null !== $sanitized ) { return $sanitized; } return $this->get( 'value' ); } /** * Field wrapper start. * * @return void */ protected function field_wrap_start() { $this->add_html( '