123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- <?php
- if ( ! class_exists( 'acf_field' ) ) :
- class acf_field {
- // vars
- var $name = '',
- $label = '',
- $category = 'basic',
- $defaults = array(),
- $l10n = array(),
- $public = true;
- public $show_in_rest = true;
- /*
- * __construct
- *
- * This function will initialize the field type
- *
- * @type function
- * @date 5/03/2014
- * @since 5.0.0
- *
- * @param n/a
- * @return n/a
- */
- function __construct() {
- // initialize
- $this->initialize();
- // register info
- acf_register_field_type_info(
- array(
- 'label' => $this->label,
- 'name' => $this->name,
- 'category' => $this->category,
- 'public' => $this->public,
- )
- );
- // value
- $this->add_field_filter( 'acf/load_value', array( $this, 'load_value' ), 10, 3 );
- $this->add_field_filter( 'acf/update_value', array( $this, 'update_value' ), 10, 3 );
- $this->add_field_filter( 'acf/format_value', array( $this, 'format_value' ), 10, 3 );
- $this->add_field_filter( 'acf/validate_value', array( $this, 'validate_value' ), 10, 4 );
- $this->add_field_action( 'acf/delete_value', array( $this, 'delete_value' ), 10, 3 );
- // field
- $this->add_field_filter( 'acf/validate_rest_value', array( $this, 'validate_rest_value' ), 10, 3 );
- $this->add_field_filter( 'acf/validate_field', array( $this, 'validate_field' ), 10, 1 );
- $this->add_field_filter( 'acf/load_field', array( $this, 'load_field' ), 10, 1 );
- $this->add_field_filter( 'acf/update_field', array( $this, 'update_field' ), 10, 1 );
- $this->add_field_filter( 'acf/duplicate_field', array( $this, 'duplicate_field' ), 10, 1 );
- $this->add_field_action( 'acf/delete_field', array( $this, 'delete_field' ), 10, 1 );
- $this->add_field_action( 'acf/render_field', array( $this, 'render_field' ), 9, 1 );
- $this->add_field_action( 'acf/render_field_settings', array( $this, 'render_field_settings' ), 9, 1 );
- $this->add_field_action( 'acf/render_field_general_settings', array( $this, 'render_field_general_settings' ), 9, 1 );
- $this->add_field_action( 'acf/render_field_validation_settings', array( $this, 'render_field_validation_settings' ), 9, 1 );
- $this->add_field_action( 'acf/render_field_presentation_settings', array( $this, 'render_field_presentation_settings' ), 9, 1 );
- $this->add_field_action( 'acf/render_field_conditional_logic_settings', array( $this, 'render_field_conditional_logic_settings' ), 9, 1 );
- $this->add_field_filter( 'acf/prepare_field', array( $this, 'prepare_field' ), 10, 1 );
- $this->add_field_filter( 'acf/translate_field', array( $this, 'translate_field' ), 10, 1 );
- // input actions
- $this->add_action( 'acf/input/admin_enqueue_scripts', array( $this, 'input_admin_enqueue_scripts' ), 10, 0 );
- $this->add_action( 'acf/input/admin_head', array( $this, 'input_admin_head' ), 10, 0 );
- $this->add_action( 'acf/input/form_data', array( $this, 'input_form_data' ), 10, 1 );
- $this->add_filter( 'acf/input/admin_l10n', array( $this, 'input_admin_l10n' ), 10, 1 );
- $this->add_action( 'acf/input/admin_footer', array( $this, 'input_admin_footer' ), 10, 1 );
- // field group actions
- $this->add_action( 'acf/field_group/admin_enqueue_scripts', array( $this, 'field_group_admin_enqueue_scripts' ), 10, 0 );
- $this->add_action( 'acf/field_group/admin_head', array( $this, 'field_group_admin_head' ), 10, 0 );
- $this->add_action( 'acf/field_group/admin_footer', array( $this, 'field_group_admin_footer' ), 10, 0 );
- }
- /*
- * initialize
- *
- * This function will initialize the field type
- *
- * @type function
- * @date 27/6/17
- * @since 5.6.0
- *
- * @param n/a
- * @return n/a
- */
- function initialize() {
- /* do nothing */
- }
- /*
- * add_filter
- *
- * This function checks if the function is_callable before adding the filter
- *
- * @type function
- * @date 5/03/2014
- * @since 5.0.0
- *
- * @param $tag (string)
- * @param $function_to_add (string)
- * @param $priority (int)
- * @param $accepted_args (int)
- * @return n/a
- */
- function add_filter( $tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1 ) {
- // bail early if no callable
- if ( ! is_callable( $function_to_add ) ) {
- return;
- }
- // add
- add_filter( $tag, $function_to_add, $priority, $accepted_args );
- }
- /*
- * add_field_filter
- *
- * This function will add a field type specific filter
- *
- * @type function
- * @date 29/09/2016
- * @since 5.4.0
- *
- * @param $tag (string)
- * @param $function_to_add (string)
- * @param $priority (int)
- * @param $accepted_args (int)
- * @return n/a
- */
- function add_field_filter( $tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1 ) {
- // append
- $tag .= '/type=' . $this->name;
- // add
- $this->add_filter( $tag, $function_to_add, $priority, $accepted_args );
- }
- /*
- * add_action
- *
- * This function checks if the function is_callable before adding the action
- *
- * @type function
- * @date 5/03/2014
- * @since 5.0.0
- *
- * @param $tag (string)
- * @param $function_to_add (string)
- * @param $priority (int)
- * @param $accepted_args (int)
- * @return n/a
- */
- function add_action( $tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1 ) {
- // bail early if no callable
- if ( ! is_callable( $function_to_add ) ) {
- return;
- }
- // add
- add_action( $tag, $function_to_add, $priority, $accepted_args );
- }
- /*
- * add_field_action
- *
- * This function will add a field type specific filter
- *
- * @type function
- * @date 29/09/2016
- * @since 5.4.0
- *
- * @param $tag (string)
- * @param $function_to_add (string)
- * @param $priority (int)
- * @param $accepted_args (int)
- * @return n/a
- */
- function add_field_action( $tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1 ) {
- // append
- $tag .= '/type=' . $this->name;
- // add
- $this->add_action( $tag, $function_to_add, $priority, $accepted_args );
- }
- /*
- * validate_field
- *
- * This function will append default settings to a field
- *
- * @type filter ("acf/validate_field/type={$this->name}")
- * @since 3.6
- * @date 23/01/13
- *
- * @param $field (array)
- * @return $field (array)
- */
- function validate_field( $field ) {
- // bail early if no defaults
- if ( ! is_array( $this->defaults ) ) {
- return $field;
- }
- // merge in defaults but keep order of $field keys
- foreach ( $this->defaults as $k => $v ) {
- if ( ! isset( $field[ $k ] ) ) {
- $field[ $k ] = $v;
- }
- }
- // return
- return $field;
- }
- /*
- * admin_l10n
- *
- * This function will append l10n text translations to an array which is later passed to JS
- *
- * @type filter ("acf/input/admin_l10n")
- * @since 3.6
- * @date 23/01/13
- *
- * @param $l10n (array)
- * @return $l10n (array)
- */
- function input_admin_l10n( $l10n ) {
- // bail early if no defaults
- if ( empty( $this->l10n ) ) {
- return $l10n;
- }
- // append
- $l10n[ $this->name ] = $this->l10n;
- // return
- return $l10n;
- }
- /**
- * Add additional validation for fields being updated via the REST API.
- *
- * @param bool $valid
- * @param mixed $value
- * @param array $field
- *
- * @return bool|WP_Error
- */
- public function validate_rest_value( $valid, $value, $field ) {
- return $valid;
- }
- /**
- * Return the schema array for the REST API.
- *
- * @param array $field
- * @return array
- */
- public function get_rest_schema( array $field ) {
- $schema = array(
- 'type' => array( 'string', 'null' ),
- 'required' => ! empty( $field['required'] ),
- );
- if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
- $schema['default'] = $field['default_value'];
- }
- return $schema;
- }
- /**
- * Return an array of links for addition to the REST API response. Each link is an array and must have both `rel` and
- * `href` keys. The `href` key must be a REST API resource URL. If a link is marked as `embeddable`, the `_embed` URL
- * parameter will trigger WordPress to dispatch an internal sub request and load the object within the same request
- * under the `_embedded` response property.
- *
- * e.g;
- * [
- * [
- * 'rel' => 'acf:post',
- * 'href' => 'https://example.com/wp-json/wp/v2/posts/497',
- * 'embeddable' => true,
- * ],
- * [
- * 'rel' => 'acf:user',
- * 'href' => 'https://example.com/wp-json/wp/v2/users/2',
- * 'embeddable' => true,
- * ],
- * ]
- *
- * @param mixed $value The raw (unformatted) field value.
- * @param string|int $post_id
- * @param array $field
- * @return array
- */
- public function get_rest_links( $value, $post_id, array $field ) {
- return array();
- }
- /**
- * Apply basic formatting to prepare the value for default REST output.
- *
- * @param mixed $value
- * @param string|int $post_id
- * @param array $field
- * @return mixed
- */
- public function format_value_for_rest( $value, $post_id, array $field ) {
- return $value;
- }
- }
- endif; // class_exists check
|