123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721 |
- <?php
- if ( ! class_exists( 'acf_field__group' ) ) :
- class acf_field__group extends acf_field {
- /*
- * __construct
- *
- * This function will setup the field type data
- *
- * @type function
- * @date 5/03/2014
- * @since 5.0.0
- *
- * @param n/a
- * @return n/a
- */
- function initialize() {
- // vars
- $this->name = 'group';
- $this->label = __( 'Group', 'acf' );
- $this->category = 'layout';
- $this->defaults = array(
- 'sub_fields' => array(),
- 'layout' => 'block',
- );
- $this->have_rows = 'single';
- // field filters
- $this->add_field_filter( 'acf/prepare_field_for_export', array( $this, 'prepare_field_for_export' ) );
- $this->add_field_filter( 'acf/prepare_field_for_import', array( $this, 'prepare_field_for_import' ) );
- }
- /*
- * load_field()
- *
- * This filter is appied to the $field after it is loaded from the database
- *
- * @type filter
- * @since 3.6
- * @date 23/01/13
- *
- * @param $field - the field array holding all the field options
- *
- * @return $field - the field array holding all the field options
- */
- function load_field( $field ) {
- // vars
- $sub_fields = acf_get_fields( $field );
- // append
- if ( $sub_fields ) {
- $field['sub_fields'] = $sub_fields;
- }
- // return
- return $field;
- }
- /*
- * load_value()
- *
- * This filter is applied to the $value after it is loaded from the db
- *
- * @type filter
- * @since 3.6
- * @date 23/01/13
- *
- * @param $value (mixed) the value found in the database
- * @param $post_id (mixed) the $post_id from which the value was loaded
- * @param $field (array) the field array holding all the field options
- * @return $value
- */
- function load_value( $value, $post_id, $field ) {
- // bail early if no sub fields
- if ( empty( $field['sub_fields'] ) ) {
- return $value;
- }
- // modify names
- $field = $this->prepare_field_for_db( $field );
- // load sub fields
- $value = array();
- // loop
- foreach ( $field['sub_fields'] as $sub_field ) {
- // load
- $value[ $sub_field['key'] ] = acf_get_value( $post_id, $sub_field );
- }
- // return
- return $value;
- }
- /*
- * format_value()
- *
- * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
- *
- * @type filter
- * @since 3.6
- * @date 23/01/13
- *
- * @param $value (mixed) the value which was loaded from the database
- * @param $post_id (mixed) the $post_id from which the value was loaded
- * @param $field (array) the field array holding all the field options
- *
- * @return $value (mixed) the modified value
- */
- function format_value( $value, $post_id, $field ) {
- // bail early if no value
- if ( empty( $value ) ) {
- return false;
- }
- // modify names
- $field = $this->prepare_field_for_db( $field );
- // loop
- foreach ( $field['sub_fields'] as $sub_field ) {
- // extract value
- $sub_value = acf_extract_var( $value, $sub_field['key'] );
- // format value
- $sub_value = acf_format_value( $sub_value, $post_id, $sub_field );
- // append to $row
- $value[ $sub_field['_name'] ] = $sub_value;
- }
- // return
- return $value;
- }
- /*
- * update_value()
- *
- * This filter is appied to the $value before it is updated in the db
- *
- * @type filter
- * @since 3.6
- * @date 23/01/13
- *
- * @param $value - the value which will be saved in the database
- * @param $field - the field array holding all the field options
- * @param $post_id - the $post_id of which the value will be saved
- *
- * @return $value - the modified value
- */
- function update_value( $value, $post_id, $field ) {
- // bail early if no value
- if ( ! acf_is_array( $value ) ) {
- return null;
- }
- // bail early if no sub fields
- if ( empty( $field['sub_fields'] ) ) {
- return null;
- }
- // modify names
- $field = $this->prepare_field_for_db( $field );
- // loop
- foreach ( $field['sub_fields'] as $sub_field ) {
- // vars
- $v = false;
- // key (backend)
- if ( isset( $value[ $sub_field['key'] ] ) ) {
- $v = $value[ $sub_field['key'] ];
- // name (frontend)
- } elseif ( isset( $value[ $sub_field['_name'] ] ) ) {
- $v = $value[ $sub_field['_name'] ];
- // empty
- } else {
- // input is not set (hidden by conditioanl logic)
- continue;
- }
- // update value
- acf_update_value( $v, $post_id, $sub_field );
- }
- // return
- return '';
- }
- /*
- * prepare_field_for_db
- *
- * This function will modify sub fields ready for update / load
- *
- * @type function
- * @date 4/11/16
- * @since 5.5.0
- *
- * @param $field (array)
- * @return $field
- */
- function prepare_field_for_db( $field ) {
- // bail early if no sub fields
- if ( empty( $field['sub_fields'] ) ) {
- return $field;
- }
- // loop
- foreach ( $field['sub_fields'] as &$sub_field ) {
- // prefix name
- $sub_field['name'] = $field['name'] . '_' . $sub_field['_name'];
- }
- // return
- return $field;
- }
- /*
- * render_field()
- *
- * Create the HTML interface for your field
- *
- * @param $field - an array holding all the field's data
- *
- * @type action
- * @since 3.6
- * @date 23/01/13
- */
- function render_field( $field ) {
- // bail early if no sub fields
- if ( empty( $field['sub_fields'] ) ) {
- return;
- }
- // load values
- foreach ( $field['sub_fields'] as &$sub_field ) {
- // add value
- if ( isset( $field['value'][ $sub_field['key'] ] ) ) {
- // this is a normal value
- $sub_field['value'] = $field['value'][ $sub_field['key'] ];
- } elseif ( isset( $sub_field['default_value'] ) ) {
- // no value, but this sub field has a default value
- $sub_field['value'] = $sub_field['default_value'];
- }
- // update prefix to allow for nested values
- $sub_field['prefix'] = $field['name'];
- // restore required
- if ( $field['required'] ) {
- $sub_field['required'] = 0;
- }
- }
- // render
- if ( $field['layout'] == 'table' ) {
- $this->render_field_table( $field );
- } else {
- $this->render_field_block( $field );
- }
- }
- /*
- * render_field_block
- *
- * description
- *
- * @type function
- * @date 12/07/2016
- * @since 5.4.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function render_field_block( $field ) {
- // vars
- $label_placement = ( $field['layout'] == 'block' ) ? 'top' : 'left';
- // html
- echo '<div class="acf-fields -' . $label_placement . ' -border">';
- foreach ( $field['sub_fields'] as $sub_field ) {
- acf_render_field_wrap( $sub_field );
- }
- echo '</div>';
- }
- /*
- * render_field_table
- *
- * description
- *
- * @type function
- * @date 12/07/2016
- * @since 5.4.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function render_field_table( $field ) {
- ?>
- <table class="acf-table">
- <thead>
- <tr>
- <?php
- foreach ( $field['sub_fields'] as $sub_field ) :
- // prepare field (allow sub fields to be removed)
- $sub_field = acf_prepare_field( $sub_field );
- // bail early if no field
- if ( ! $sub_field ) {
- continue;
- }
- // vars
- $atts = array();
- $atts['class'] = 'acf-th';
- $atts['data-name'] = $sub_field['_name'];
- $atts['data-type'] = $sub_field['type'];
- $atts['data-key'] = $sub_field['key'];
- // Add custom width
- if ( $sub_field['wrapper']['width'] ) {
- $atts['data-width'] = $sub_field['wrapper']['width'];
- $atts['style'] = 'width: ' . $sub_field['wrapper']['width'] . '%;';
- }
- ?>
- <th <?php echo acf_esc_attrs( $atts ); ?>>
- <?php acf_render_field_label( $sub_field ); ?>
- <?php acf_render_field_instructions( $sub_field ); ?>
- </th>
- <?php endforeach; ?>
- </tr>
- </thead>
- <tbody>
- <tr class="acf-row">
- <?php
- foreach ( $field['sub_fields'] as $sub_field ) {
- acf_render_field_wrap( $sub_field, 'td' );
- }
- ?>
- </tr>
- </tbody>
- </table>
- <?php
- }
- /*
- * render_field_settings()
- *
- * Create extra options for your field. This is rendered when editing a field.
- * The value of $field['name'] can be used (like bellow) to save extra data to the $field
- *
- * @type action
- * @since 3.6
- * @date 23/01/13
- *
- * @param $field - an array holding all the field's data
- */
- function render_field_settings( $field ) {
- // vars
- $args = array(
- 'fields' => $field['sub_fields'],
- 'parent' => $field['ID'],
- 'is_subfield' => true,
- );
- ?>
- <div class="acf-field acf-field-setting-sub_fields" data-setting="group" data-name="sub_fields">
- <div class="acf-label">
- <label><?php _e( 'Sub Fields', 'acf' ); ?></label>
- </div>
- <div class="acf-input acf-input-sub">
- <?php
- acf_get_view( 'field-group-fields', $args );
- ?>
- </div>
- </div>
- <?php
- // layout
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Layout', 'acf' ),
- 'instructions' => __( 'Specify the style used to render the selected fields', 'acf' ),
- 'type' => 'radio',
- 'name' => 'layout',
- 'layout' => 'horizontal',
- 'choices' => array(
- 'block' => __( 'Block', 'acf' ),
- 'table' => __( 'Table', 'acf' ),
- 'row' => __( 'Row', 'acf' ),
- ),
- )
- );
- }
- /*
- * validate_value
- *
- * description
- *
- * @type function
- * @date 11/02/2014
- * @since 5.0.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function validate_value( $valid, $value, $field, $input ) {
- // bail early if no $value
- if ( empty( $value ) ) {
- return $valid;
- }
- // bail early if no sub fields
- if ( empty( $field['sub_fields'] ) ) {
- return $valid;
- }
- // loop
- foreach ( $field['sub_fields'] as $sub_field ) {
- // get sub field
- $k = $sub_field['key'];
- // bail early if value not set (conditional logic?)
- if ( ! isset( $value[ $k ] ) ) {
- continue;
- }
- // required
- if ( $field['required'] ) {
- $sub_field['required'] = 1;
- }
- // validate
- acf_validate_value( $value[ $k ], $sub_field, "{$input}[{$k}]" );
- }
- // return
- return $valid;
- }
- /*
- * duplicate_field()
- *
- * This filter is appied to the $field before it is duplicated and saved to the database
- *
- * @type filter
- * @since 3.6
- * @date 23/01/13
- *
- * @param $field - the field array holding all the field options
- *
- * @return $field - the modified field
- */
- function duplicate_field( $field ) {
- // get sub fields
- $sub_fields = acf_extract_var( $field, 'sub_fields' );
- // save field to get ID
- $field = acf_update_field( $field );
- // duplicate sub fields
- acf_duplicate_fields( $sub_fields, $field['ID'] );
- // return
- return $field;
- }
- /**
- * prepare_field_for_export
- *
- * Prepares the field for export.
- *
- * @date 11/03/2014
- * @since 5.0.0
- *
- * @param array $field The field settings.
- * @return array
- */
- function prepare_field_for_export( $field ) {
- // Check for sub fields.
- if ( ! empty( $field['sub_fields'] ) ) {
- $field['sub_fields'] = acf_prepare_fields_for_export( $field['sub_fields'] );
- }
- return $field;
- }
- /**
- * prepare_field_for_import
- *
- * Returns a flat array of fields containing all sub fields ready for import.
- *
- * @date 11/03/2014
- * @since 5.0.0
- *
- * @param array $field The field settings.
- * @return array
- */
- function prepare_field_for_import( $field ) {
- // Check for sub fields.
- if ( ! empty( $field['sub_fields'] ) ) {
- $sub_fields = acf_extract_var( $field, 'sub_fields' );
- // Modify sub fields.
- foreach ( $sub_fields as $i => $sub_field ) {
- $sub_fields[ $i ]['parent'] = $field['key'];
- $sub_fields[ $i ]['menu_order'] = $i;
- }
- // Return array of [field, sub_1, sub_2, ...].
- return array_merge( array( $field ), $sub_fields );
- }
- return $field;
- }
- /*
- * delete_value
- *
- * Called when deleting this field's value.
- *
- * @date 1/07/2015
- * @since 5.2.3
- *
- * @param mixed $post_id The post ID being saved
- * @param string $meta_key The field name as seen by the DB
- * @param array $field The field settings
- * @return void
- */
- function delete_value( $post_id, $meta_key, $field ) {
- // bail early if no sub fields
- if ( empty( $field['sub_fields'] ) ) {
- return null;
- }
- // modify names
- $field = $this->prepare_field_for_db( $field );
- // loop
- foreach ( $field['sub_fields'] as $sub_field ) {
- acf_delete_value( $post_id, $sub_field );
- }
- }
- /**
- * delete_field
- *
- * Called when deleting a field of this type.
- *
- * @date 8/11/18
- * @since 5.8.0
- *
- * @param arra $field The field settings.
- * @return void
- */
- function delete_field( $field ) {
- // loop over sub fields and delete them
- if ( $field['sub_fields'] ) {
- foreach ( $field['sub_fields'] as $sub_field ) {
- acf_delete_field( $sub_field['ID'] );
- }
- }
- }
- /**
- * Return the schema array for the REST API.
- *
- * @param array $field
- * @return array
- */
- public function get_rest_schema( array $field ) {
- $schema = array(
- 'type' => array( 'object', 'null' ),
- 'properties' => array(),
- 'required' => ! empty( $field['required'] ),
- );
- foreach ( $field['sub_fields'] as $sub_field ) {
- if ( $sub_field_schema = acf_get_field_rest_schema( $sub_field ) ) {
- $schema['properties'][ $sub_field['name'] ] = $sub_field_schema;
- }
- }
- return $schema;
- }
- /**
- * Apply basic formatting to prepare the value for default REST output.
- *
- * @param mixed $value
- * @param int|string $post_id
- * @param array $field
- * @return array|mixed
- */
- public function format_value_for_rest( $value, $post_id, array $field ) {
- if ( empty( $value ) || ! is_array( $value ) || empty( $field['sub_fields'] ) ) {
- return $value;
- }
- // Loop through each row and within that, each sub field to process sub fields individually.
- foreach ( $field['sub_fields'] as $sub_field ) {
- // Extract the sub field 'field_key'=>'value' pair from the $value and format it.
- $sub_value = acf_extract_var( $value, $sub_field['key'] );
- $sub_value = acf_format_value_for_rest( $sub_value, $post_id, $sub_field );
- // Add the sub field value back to the $value but mapped to the field name instead
- // of the key reference.
- $value[ $sub_field['name'] ] = $sub_value;
- }
- return $value;
- }
- }
- // initialize
- acf_register_field_type( 'acf_field__group' );
- endif; // class_exists check
- ?>
|