53511b78 by Jeff Balicki

plugin clean up

Signed-off-by: Jeff <jeff@gotenzing.com>
1 parent c91a2ee3
Showing 817 changed files with 0 additions and 5062 deletions
<?php
if ( ! class_exists( 'acf_field_message' ) ) :
class acf_field_message extends acf_field {
public $show_in_rest = false;
/*
* __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 = 'message';
$this->label = __( 'Message', 'acf' );
$this->category = 'layout';
$this->description = __( 'Used to display a message to editors alongside other fields. Useful for providing additional context or instructions around your fields.', 'acf' );
$this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-message.png';
$this->defaults = array(
'message' => '',
'esc_html' => 0,
'new_lines' => 'wpautop',
);
}
/*
* 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 ) {
// vars
$m = $field['message'];
// wptexturize (improves "quotes")
$m = wptexturize( $m );
// esc_html
if ( $field['esc_html'] ) {
$m = esc_html( $m );
}
// new lines
if ( $field['new_lines'] == 'wpautop' ) {
$m = wpautop( $m );
} elseif ( $field['new_lines'] == 'br' ) {
$m = nl2br( $m );
}
// return
echo acf_esc_html( $m );
}
/*
* 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
*
* @param $field - an array holding all the field's data
*
* @type action
* @since 3.6
* @date 23/01/13
*/
function render_field_settings( $field ) {
acf_render_field_setting(
$field,
array(
'label' => __( 'Message', 'acf' ),
'instructions' => '',
'type' => 'textarea',
'name' => 'message',
)
);
acf_render_field_setting(
$field,
array(
'label' => __( 'New Lines', 'acf' ),
'instructions' => __( 'Controls how new lines are rendered', 'acf' ),
'type' => 'select',
'name' => 'new_lines',
'choices' => array(
'wpautop' => __( 'Automatically add paragraphs', 'acf' ),
'br' => __( 'Automatically add &lt;br&gt;', 'acf' ),
'' => __( 'No Formatting', 'acf' ),
),
)
);
acf_render_field_setting(
$field,
array(
'label' => __( 'Escape HTML', 'acf' ),
'instructions' => __( 'Allow HTML markup to display as visible text instead of rendering', 'acf' ),
'name' => 'esc_html',
'type' => 'true_false',
'ui' => 1,
)
);
}
/*
* translate_field
*
* This function will translate field settings
*
* @type function
* @date 8/03/2016
* @since 5.3.2
*
* @param $field (array)
* @return $field
*/
function translate_field( $field ) {
// translate
$field['message'] = acf_translate( $field['message'] );
// return
return $field;
}
/*
* 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 ) {
// remove name to avoid caching issue
$field['name'] = '';
// remove instructions
$field['instructions'] = '';
// remove required to avoid JS issues
$field['required'] = 0;
// set value other than 'null' to avoid ACF loading / caching issue
$field['value'] = false;
// return
return $field;
}
}
// initialize
acf_register_field_type( 'acf_field_message' );
endif; // class_exists check
<?php
if ( ! class_exists( 'acf_field_separator' ) ) :
class acf_field_separator 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 = 'separator';
$this->label = __( 'Separator', 'acf' );
$this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-separator.png';
$this->category = 'layout';
}
/*
* 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 ) {
/* do nothing */
}
/*
* 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 ) {
// remove name to avoid caching issue
$field['name'] = '';
// remove required to avoid JS issues
$field['required'] = 0;
// set value other than 'null' to avoid ACF loading / caching issue
$field['value'] = false;
// return
return $field;
}
}
// initialize
acf_register_field_type( 'acf_field_separator' );
endif; // class_exists check
<?php
if ( ! class_exists( 'acf_field_tab' ) ) :
class acf_field_tab extends acf_field {
public $show_in_rest = false;
/*
* __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 = 'tab';
$this->label = __( 'Tab', 'acf' );
$this->category = 'layout';
$this->description = __( 'Allows you to group fields into tabbed sections in the edit screen. Useful for keeping fields organized and structured.', 'acf' );
$this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-tabs.png';
$this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/tab/', 'docs', 'field-type-selection' );
$this->defaults = array(
'placement' => 'top',
'endpoint' => 0, // added in 5.2.8
);
}
/*
* 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 ) {
// vars
$atts = array(
'href' => '',
'class' => 'acf-tab-button',
'data-placement' => $field['placement'],
'data-endpoint' => $field['endpoint'],
'data-key' => $field['key'],
);
if ( isset( $field['settings-type'] ) ) {
$atts['class'] .= ' acf-settings-type-' . acf_slugify( $field['settings-type'] );
}
?>
<a <?php echo acf_esc_attrs( $atts ); ?>><?php echo acf_esc_html( $field['label'] ); ?></a>
<?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
*
* @param $field - an array holding all the field's data
*
* @type action
* @since 3.6
* @date 23/01/13
*/
function render_field_settings( $field ) {
/*
// message
$message = '';
$message .= '<p>' . __( 'Use "Tab Fields" to better organize your edit screen by grouping fields together.', 'acf') . '</p>';
$message .= '<p>' . __( 'All fields following this "tab field" (or until another "tab field" is defined) will be grouped together using this field\'s label as the tab heading.','acf') . '</p>';
// default_value
acf_render_field_setting( $field, array(
'label' => __('Instructions','acf'),
'instructions' => '',
'name' => 'notes',
'type' => 'message',
'message' => $message,
));
*/
// preview_size
acf_render_field_setting(
$field,
array(
'label' => __( 'Placement', 'acf' ),
'type' => 'select',
'name' => 'placement',
'choices' => array(
'top' => __( 'Top aligned', 'acf' ),
'left' => __( 'Left aligned', 'acf' ),
),
)
);
// endpoint
acf_render_field_setting(
$field,
array(
'label' => __( 'New Tab Group', 'acf' ),
'instructions' => __( 'Start a new group of tabs at this tab.', 'acf' ),
'name' => 'endpoint',
'type' => 'true_false',
'ui' => 1,
)
);
}
/*
* 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 ) {
// remove name to avoid caching issue
$field['name'] = '';
// remove instructions
$field['instructions'] = '';
// remove required to avoid JS issues
$field['required'] = 0;
// set value other than 'null' to avoid ACF loading / caching issue
$field['value'] = false;
// return
return $field;
}
}
// initialize
acf_register_field_type( 'acf_field_tab' );
endif; // class_exists check
?>
<?php
if ( ! class_exists( 'acf_field_text' ) ) :
class acf_field_text extends acf_field {
/*
* initialize
*
* 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 = 'text';
$this->label = __( 'Text', 'acf' );
$this->description = __( 'A basic text input, useful for storing single string values.', 'acf' );
$this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-text.png';
$this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/text/', 'docs', 'field-type-selection' );
$this->defaults = array(
'default_value' => '',
'maxlength' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
);
}
/*
* 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 ) {
$html = '';
// Prepend text.
if ( $field['prepend'] !== '' ) {
$field['class'] .= ' acf-is-prepended';
$html .= '<div class="acf-input-prepend">' . acf_esc_html( $field['prepend'] ) . '</div>';
}
// Append text.
if ( $field['append'] !== '' ) {
$field['class'] .= ' acf-is-appended';
$html .= '<div class="acf-input-append">' . acf_esc_html( $field['append'] ) . '</div>';
}
// Input.
$input_attrs = array();
foreach ( array( 'type', 'id', 'class', 'name', 'value', 'placeholder', 'maxlength', 'pattern', 'readonly', 'disabled', 'required' ) as $k ) {
if ( isset( $field[ $k ] ) ) {
$input_attrs[ $k ] = $field[ $k ];
}
}
$html .= '<div class="acf-input-wrap">' . acf_get_text_input( acf_filter_attrs( $input_attrs ) ) . '</div>';
// Display.
echo $html;
}
/*
* 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
*
* @param $field - an array holding all the field's data
*
* @type action
* @since 3.6
* @date 23/01/13
*/
function render_field_settings( $field ) {
acf_render_field_setting(
$field,
array(
'label' => __( 'Default Value', 'acf' ),
'instructions' => __( 'Appears when creating a new post', 'acf' ),
'type' => 'text',
'name' => 'default_value',
)
);
}
/**
* Renders the field settings used in the "Validation" tab.
*
* @since 6.0
*
* @param array $field The field settings array.
* @return void
*/
function render_field_validation_settings( $field ) {
acf_render_field_setting(
$field,
array(
'label' => __( 'Character Limit', 'acf' ),
'instructions' => __( 'Leave blank for no limit', 'acf' ),
'type' => 'number',
'name' => 'maxlength',
)
);
}
/**
* Renders the field settings used in the "Presentation" tab.
*
* @since 6.0
*
* @param array $field The field settings array.
* @return void
*/
function render_field_presentation_settings( $field ) {
acf_render_field_setting(
$field,
array(
'label' => __( 'Placeholder Text', 'acf' ),
'instructions' => __( 'Appears within the input', 'acf' ),
'type' => 'text',
'name' => 'placeholder',
)
);
acf_render_field_setting(
$field,
array(
'label' => __( 'Prepend', 'acf' ),
'instructions' => __( 'Appears before the input', 'acf' ),
'type' => 'text',
'name' => 'prepend',
)
);
acf_render_field_setting(
$field,
array(
'label' => __( 'Append', 'acf' ),
'instructions' => __( 'Appears after the input', 'acf' ),
'type' => 'text',
'name' => 'append',
)
);
}
/**
* validate_value
*
* Validates a field's value.
*
* @date 29/1/19
* @since 5.7.11
*
* @param (bool|string) Whether the value is vaid or not.
* @param mixed $value The field value.
* @param array $field The field array.
* @param string $input The HTML input name.
* @return (bool|string)
*/
function validate_value( $valid, $value, $field, $input ) {
// Check maxlength
if ( $field['maxlength'] && ( acf_strlen( $value ) > $field['maxlength'] ) ) {
return sprintf( __( 'Value must not exceed %d characters', 'acf' ), $field['maxlength'] );
}
// Return.
return $valid;
}
/**
* Return the schema array for the REST API.
*
* @param array $field
* @return array
*/
function get_rest_schema( array $field ) {
$schema = parent::get_rest_schema( $field );
if ( ! empty( $field['maxlength'] ) ) {
$schema['maxLength'] = (int) $field['maxlength'];
}
return $schema;
}
}
// initialize
acf_register_field_type( 'acf_field_text' );
endif; // class_exists check
<?php
if ( ! class_exists( 'acf_field_textarea' ) ) :
class acf_field_textarea extends acf_field {
/*
* initialize
*
* 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 = 'textarea';
$this->label = __( 'Text Area', 'acf' );
$this->description = __( 'A basic textarea input for storing paragraphs of text.', 'acf' );
$this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-textarea.png';
$this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/textarea/', 'docs', 'field-type-selection' );
$this->defaults = array(
'default_value' => '',
'new_lines' => '',
'maxlength' => '',
'placeholder' => '',
'rows' => '',
);
}
/*
* 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 ) {
// vars
$atts = array();
$keys = array( 'id', 'class', 'name', 'value', 'placeholder', 'rows', 'maxlength' );
$keys2 = array( 'readonly', 'disabled', 'required' );
// rows
if ( ! $field['rows'] ) {
$field['rows'] = 8;
}
// atts (value="123")
foreach ( $keys as $k ) {
if ( isset( $field[ $k ] ) ) {
$atts[ $k ] = $field[ $k ];
}
}
// atts2 (disabled="disabled")
foreach ( $keys2 as $k ) {
if ( ! empty( $field[ $k ] ) ) {
$atts[ $k ] = $k;
}
}
// remove empty atts
$atts = acf_clean_atts( $atts );
// return
acf_textarea_input( $atts );
}
/*
* 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
*
* @param $field - an array holding all the field's data
*
* @type action
* @since 3.6
* @date 23/01/13
*/
function render_field_settings( $field ) {
acf_render_field_setting(
$field,
array(
'label' => __( 'Default Value', 'acf' ),
'instructions' => __( 'Appears when creating a new post', 'acf' ),
'type' => 'textarea',
'name' => 'default_value',
)
);
}
/**
* Renders the field settings used in the "Validation" tab.
*
* @since 6.0
*
* @param array $field The field settings array.
* @return void
*/
function render_field_validation_settings( $field ) {
acf_render_field_setting(
$field,
array(
'label' => __( 'Character Limit', 'acf' ),
'instructions' => __( 'Leave blank for no limit', 'acf' ),
'type' => 'number',
'name' => 'maxlength',
)
);
}
/**
* Renders the field settings used in the "Presentation" tab.
*
* @since 6.0
*
* @param array $field The field settings array.
* @return void
*/
function render_field_presentation_settings( $field ) {
acf_render_field_setting(
$field,
array(
'label' => __( 'Rows', 'acf' ),
'instructions' => __( 'Sets the textarea height', 'acf' ),
'type' => 'number',
'name' => 'rows',
'placeholder' => 8,
)
);
acf_render_field_setting(
$field,
array(
'label' => __( 'Placeholder Text', 'acf' ),
'instructions' => __( 'Appears within the input', 'acf' ),
'type' => 'text',
'name' => 'placeholder',
)
);
acf_render_field_setting(
$field,
array(
'label' => __( 'New Lines', 'acf' ),
'instructions' => __( 'Controls how new lines are rendered', 'acf' ),
'type' => 'select',
'name' => 'new_lines',
'choices' => array(
'wpautop' => __( 'Automatically add paragraphs', 'acf' ),
'br' => __( 'Automatically add &lt;br&gt;', 'acf' ),
'' => __( 'No Formatting', 'acf' ),
),
)
);
}
/*
* format_value()
*
* This filter is applied 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 or not for template
if ( empty( $value ) || ! is_string( $value ) ) {
return $value;
}
// new lines
if ( $field['new_lines'] == 'wpautop' ) {
$value = wpautop( $value );
} elseif ( $field['new_lines'] == 'br' ) {
$value = nl2br( $value );
}
// return
return $value;
}
/**
* validate_value
*
* Validates a field's value.
*
* @date 29/1/19
* @since 5.7.11
*
* @param (bool|string) Whether the value is vaid or not.
* @param mixed $value The field value.
* @param array $field The field array.
* @param string $input The HTML input name.
* @return (bool|string)
*/
function validate_value( $valid, $value, $field, $input ) {
// Check maxlength.
if ( $field['maxlength'] && ( acf_strlen( $value ) > $field['maxlength'] ) ) {
return sprintf( __( 'Value must not exceed %d characters', 'acf' ), $field['maxlength'] );
}
// Return.
return $valid;
}
/**
* Return the schema array for the REST API.
*
* @param array $field
* @return array
*/
function get_rest_schema( array $field ) {
$schema = parent::get_rest_schema( $field );
if ( ! empty( $field['maxlength'] ) ) {
$schema['maxLength'] = (int) $field['maxlength'];
}
return $schema;
}
}
// initialize
acf_register_field_type( 'acf_field_textarea' );
endif; // class_exists check
<?php
if ( ! class_exists( 'acf_field_time_picker' ) ) :
class acf_field_time_picker 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 = 'time_picker';
$this->label = __( 'Time Picker', 'acf' );
$this->category = 'advanced';
$this->description = __( 'An interactive UI for picking a time. The time format can be customized using the field settings.', 'acf' );
$this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-time.png';
$this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/time-picker/', 'docs', 'field-type-selection' );
$this->defaults = array(
'display_format' => 'g:i a',
'return_format' => 'g:i a',
);
}
/*
* 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 ) {
// Set value.
$display_value = '';
if ( $field['value'] ) {
$display_value = acf_format_date( $field['value'], $field['display_format'] );
}
// Elements.
$div = array(
'class' => 'acf-time-picker acf-input-wrap',
'data-time_format' => acf_convert_time_to_js( $field['display_format'] ),
);
$hidden_input = array(
'id' => $field['id'],
'class' => 'input-alt',
'type' => 'hidden',
'name' => $field['name'],
'value' => $field['value'],
);
$text_input = array(
'class' => $field['class'] . ' input',
'type' => 'text',
'value' => $display_value,
);
foreach ( array( 'readonly', 'disabled' ) as $k ) {
if ( ! empty( $field[ $k ] ) ) {
$hidden_input[ $k ] = $k;
$text_input[ $k ] = $k;
}
}
// Output.
?>
<div <?php echo acf_esc_attrs( $div ); ?>>
<?php acf_hidden_input( $hidden_input ); ?>
<?php acf_text_input( $text_input ); ?>
</div>
<?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 ) {
$g_i_a = date_i18n( 'g:i a' );
$H_i_s = date_i18n( 'H:i:s' );
echo '<div class="acf-field-settings-split">';
acf_render_field_setting(
$field,
array(
'label' => __( 'Display Format', 'acf' ),
'hint' => __( 'The format displayed when editing a post', 'acf' ),
'type' => 'radio',
'name' => 'display_format',
'other_choice' => 1,
'choices' => array(
'g:i a' => '<span>' . $g_i_a . '</span><code>g:i a</code>',
'H:i:s' => '<span>' . $H_i_s . '</span><code>H:i:s</code>',
'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>',
),
)
);
acf_render_field_setting(
$field,
array(
'label' => __( 'Return Format', 'acf' ),
'hint' => __( 'The format returned via template functions', 'acf' ),
'type' => 'radio',
'name' => 'return_format',
'other_choice' => 1,
'choices' => array(
'g:i a' => '<span>' . $g_i_a . '</span><code>g:i a</code>',
'H:i:s' => '<span>' . $H_i_s . '</span><code>H:i:s</code>',
'other' => '<span>' . __( 'Custom:', 'acf' ) . '</span>',
),
)
);
echo '</div>';
}
/*
* 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 ) {
return acf_format_date( $value, $field['return_format'] );
}
/**
* This filter is applied to the $field after it is loaded from the database
* and ensures the return and display values are set.
*
* @type filter
* @since 5.11.0
* @date 28/09/21
*
* @param array $field The field array holding all the field options.
*
* @return array
*/
function load_field( $field ) {
if ( empty( $field['display_format'] ) ) {
$field['display_format'] = $this->defaults['display_format'];
}
if ( empty( $field['return_format'] ) ) {
$field['return_format'] = $this->defaults['return_format'];
}
return $field;
}
/**
* Return the schema array for the REST API.
*
* @param array $field
* @return array
*/
public function get_rest_schema( array $field ) {
return array(
'type' => array( 'string', 'null' ),
'description' => 'A `H:i:s` formatted time string.',
'required' => ! empty( $field['required'] ),
);
}
}
// initialize
acf_register_field_type( 'acf_field_time_picker' );
endif; // class_exists check
?>
<?php
if ( ! class_exists( 'acf_field_url' ) ) :
class acf_field_url extends acf_field {
/*
* initialize
*
* 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 = 'url';
$this->label = __( 'URL', 'acf' );
$this->description = __( 'A text input specifically designed for storing web addresses.', 'acf' );
$this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-url.png';
$this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/url/', 'docs', 'field-type-selection' );
$this->defaults = array(
'default_value' => '',
'placeholder' => '',
);
}
/*
* 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 ) {
// vars
$atts = array();
$keys = array( 'type', 'id', 'class', 'name', 'value', 'placeholder', 'pattern' );
$keys2 = array( 'readonly', 'disabled', 'required' );
$html = '';
// atts (value="123")
foreach ( $keys as $k ) {
if ( isset( $field[ $k ] ) ) {
$atts[ $k ] = $field[ $k ];
}
}
// atts2 (disabled="disabled")
foreach ( $keys2 as $k ) {
if ( ! empty( $field[ $k ] ) ) {
$atts[ $k ] = $k;
}
}
// remove empty atts
$atts = acf_clean_atts( $atts );
// render
$html .= '<div class="acf-input-wrap acf-url">';
$html .= '<i class="acf-icon -globe -small"></i>' . acf_get_text_input( $atts );
$html .= '</div>';
// return
echo $html;
}
/*
* 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 ) {
acf_render_field_setting(
$field,
array(
'label' => __( 'Default Value', 'acf' ),
'instructions' => __( 'Appears when creating a new post', 'acf' ),
'type' => 'text',
'name' => 'default_value',
)
);
}
/**
* Renders the field settings used in the "Presentation" tab.
*
* @since 6.0
*
* @param array $field The field settings array.
* @return void
*/
function render_field_presentation_settings( $field ) {
acf_render_field_setting(
$field,
array(
'label' => __( 'Placeholder Text', 'acf' ),
'instructions' => __( 'Appears within the input', 'acf' ),
'type' => 'text',
'name' => 'placeholder',
)
);
}
/*
* 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 empty
if ( empty( $value ) ) {
return $valid;
}
if ( strpos( $value, '://' ) !== false ) {
// url
} elseif ( strpos( $value, '//' ) === 0 ) {
// protocol relative url
} else {
$valid = __( 'Value must be a valid URL', 'acf' );
}
// return
return $valid;
}
/**
* Return the schema array for the REST API.
*
* @param array $field
* @return array
*/
public function get_rest_schema( array $field ) {
$schema = parent::get_rest_schema( $field );
$schema['format'] = 'uri';
return $schema;
}
}
// initialize
acf_register_field_type( 'acf_field_url' );
endif; // class_exists check
<?php
/*
* ACF Attachment Form Class
*
* All the logic for adding fields to attachments
*
* @class acf_form_attachment
* @package ACF
* @subpackage Forms
*/
if ( ! class_exists( 'acf_form_attachment' ) ) :
class acf_form_attachment {
/*
* __construct
*
* This function will setup the class functionality
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function __construct() {
// actions
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
// render
add_filter( 'attachment_fields_to_edit', array( $this, 'edit_attachment' ), 10, 2 );
// save
add_filter( 'attachment_fields_to_save', array( $this, 'save_attachment' ), 10, 2 );
}
/*
* admin_enqueue_scripts
*
* This action is run after post query but before any admin script / head actions.
* It is a good place to register all actions.
*
* @type action (admin_enqueue_scripts)
* @date 26/01/13
* @since 3.6.0
*
* @param N/A
* @return N/A
*/
function admin_enqueue_scripts() {
// bail early if not valid screen
if ( ! acf_is_screen( array( 'attachment', 'upload' ) ) ) {
return;
}
// load acf scripts
acf_enqueue_scripts(
array(
'uploader' => true,
)
);
// actions
if ( acf_is_screen( 'upload' ) ) {
add_action( 'admin_footer', array( $this, 'admin_footer' ), 0 );
}
}
/*
* admin_footer
*
* This function will add acf_form_data to the WP 4.0 attachment grid
*
* @type action (admin_footer)
* @date 11/09/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function admin_footer() {
// render post data
acf_form_data(
array(
'screen' => 'attachment',
'post_id' => 0,
)
);
?>
<script type="text/javascript">
// WP saves attachment on any input change, so unload is not needed
acf.unload.active = 0;
</script>
<?php
}
/*
* edit_attachment
*
* description
*
* @type function
* @date 8/10/13
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function edit_attachment( $form_fields, $post ) {
// vars
$is_page = acf_is_screen( 'attachment' );
$post_id = $post->ID;
$el = 'tr';
// get field groups
$field_groups = acf_get_field_groups(
array(
'attachment_id' => $post_id,
'attachment' => $post_id, // Leave for backwards compatibility
)
);
// render
if ( ! empty( $field_groups ) ) {
// get acf_form_data
ob_start();
acf_form_data(
array(
'screen' => 'attachment',
'post_id' => $post_id,
)
);
// open
echo '</td></tr>';
// loop
foreach ( $field_groups as $field_group ) {
// load fields
$fields = acf_get_fields( $field_group );
// override instruction placement for modal
if ( ! $is_page ) {
$field_group['instruction_placement'] = 'field';
}
// render
acf_render_fields( $fields, $post_id, $el, $field_group['instruction_placement'] );
}
// close
echo '<tr class="compat-field-acf-blank"><td>';
$html = ob_get_contents();
ob_end_clean();
$form_fields['acf-form-data'] = array(
'label' => '',
'input' => 'html',
'html' => $html,
);
}
// return
return $form_fields;
}
/*
* save_attachment
*
* description
*
* @type function
* @date 8/10/13
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function save_attachment( $post, $attachment ) {
// bail early if not valid nonce
if ( ! acf_verify_nonce( 'attachment' ) ) {
return $post;
}
// bypass validation for ajax
if ( acf_is_ajax( 'save-attachment-compat' ) ) {
acf_save_post( $post['ID'] );
// validate and save
} elseif ( acf_validate_save_post( true ) ) {
acf_save_post( $post['ID'] );
}
// return
return $post;
}
}
new acf_form_attachment();
endif;
?>
<?php
/*
* ACF Comment Form Class
*
* All the logic for adding fields to comments
*
* @class acf_form_comment
* @package ACF
* @subpackage Forms
*/
if ( ! class_exists( 'acf_form_comment' ) ) :
class acf_form_comment {
/*
* __construct
*
* This function will setup the class functionality
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function __construct() {
// actions
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
// render
add_filter( 'comment_form_field_comment', array( $this, 'comment_form_field_comment' ), 999, 1 );
// add_action( 'comment_form_logged_in_after', array( $this, 'add_comment') );
// add_action( 'comment_form', array( $this, 'add_comment') );
// save
add_action( 'edit_comment', array( $this, 'save_comment' ), 10, 1 );
add_action( 'comment_post', array( $this, 'save_comment' ), 10, 1 );
}
/*
* validate_page
*
* This function will check if the current page is for a post/page edit form
*
* @type function
* @date 23/06/12
* @since 3.1.8
*
* @param n/a
* @return (boolean)
*/
function validate_page() {
// global
global $pagenow;
// validate page
if ( $pagenow == 'comment.php' ) {
return true;
}
// return
return false;
}
/*
* admin_enqueue_scripts
*
* This action is run after post query but before any admin script / head actions.
* It is a good place to register all actions.
*
* @type action (admin_enqueue_scripts)
* @date 26/01/13
* @since 3.6.0
*
* @param n/a
* @return n/a
*/
function admin_enqueue_scripts() {
// validate page
if ( ! $this->validate_page() ) {
return;
}
// load acf scripts
acf_enqueue_scripts();
// actions
add_action( 'admin_footer', array( $this, 'admin_footer' ), 10, 1 );
add_action( 'add_meta_boxes_comment', array( $this, 'edit_comment' ), 10, 1 );
}
/*
* edit_comment
*
* This function is run on the admin comment.php page and will render the ACF fields within custom metaboxes to look native
*
* @type function
* @date 19/10/13
* @since 5.0.0
*
* @param $comment (object)
* @return n/a
*/
function edit_comment( $comment ) {
// vars
$post_id = "comment_{$comment->comment_ID}";
// get field groups
$field_groups = acf_get_field_groups(
array(
'comment' => get_post_type( $comment->comment_post_ID ),
)
);
// render
if ( ! empty( $field_groups ) ) {
// render post data
acf_form_data(
array(
'screen' => 'comment',
'post_id' => $post_id,
)
);
foreach ( $field_groups as $field_group ) {
// load fields
$fields = acf_get_fields( $field_group );
// vars
$o = array(
'id' => 'acf-' . $field_group['ID'],
'key' => $field_group['key'],
// 'style' => $field_group['style'],
'label' => $field_group['label_placement'],
'edit_url' => '',
'edit_title' => __( 'Edit field group', 'acf' ),
// 'visibility' => $visibility
);
// edit_url
if ( $field_group['ID'] && acf_current_user_can_admin() ) {
$o['edit_url'] = admin_url( 'post.php?post=' . $field_group['ID'] . '&action=edit' );
}
?>
<div id="acf-<?php echo $field_group['ID']; ?>" class="stuffbox">
<h3 class="hndle"><?php echo $field_group['title']; ?></h3>
<div class="inside">
<?php acf_render_fields( $fields, $post_id, 'div', $field_group['instruction_placement'] ); ?>
<script type="text/javascript">
if( typeof acf !== 'undefined' ) {
acf.newPostbox(<?php echo json_encode( $o ); ?>);
}
</script>
</div>
</div>
<?php
}
}
}
/*
* comment_form_field_comment
*
* description
*
* @type function
* @date 18/04/2016
* @since 5.3.8
*
* @param $post_id (int)
* @return $post_id (int)
*/
function comment_form_field_comment( $html ) {
// global
global $post;
// vars
$post_id = false;
// get field groups
$field_groups = acf_get_field_groups(
array(
'comment' => $post->post_type,
)
);
// bail early if no field groups
if ( ! $field_groups ) {
return $html;
}
// enqueue scripts
acf_enqueue_scripts();
// ob
ob_start();
// render post data
acf_form_data(
array(
'screen' => 'comment',
'post_id' => $post_id,
)
);
echo '<div class="acf-comment-fields acf-fields -clear">';
foreach ( $field_groups as $field_group ) {
$fields = acf_get_fields( $field_group );
acf_render_fields( $fields, $post_id, 'p', $field_group['instruction_placement'] );
}
echo '</div>';
// append
$html .= ob_get_contents();
ob_end_clean();
// return
return $html;
}
/*
* save_comment
*
* This function will save the comment data
*
* @type function
* @date 19/10/13
* @since 5.0.0
*
* @param comment_id (int)
* @return n/a
*/
function save_comment( $comment_id ) {
// bail early if not valid nonce
if ( ! acf_verify_nonce( 'comment' ) ) {
return $comment_id;
}
// kses
if ( isset( $_POST['acf'] ) ) {
$_POST['acf'] = wp_kses_post_deep( $_POST['acf'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized with wp_kses_post_deep().
}
// validate and save
if ( acf_validate_save_post( true ) ) {
acf_save_post( "comment_{$comment_id}" );
}
}
/*
* admin_footer
*
* description
*
* @type function
* @date 27/03/2015
* @since 5.1.5
*
* @param $post_id (int)
* @return $post_id (int)
*/
function admin_footer() {
?>
<script type="text/javascript">
(function($) {
// vars
var $spinner = $('#publishing-action .spinner');
// create spinner if not exists (may exist in future WP versions)
if( !$spinner.exists() ) {
// create spinner
$spinner = $('<span class="spinner"></span>');
// append
$('#publishing-action').prepend( $spinner );
}
})(jQuery);
</script>
<?php
}
}
new acf_form_comment();
endif;
?>
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! class_exists( 'ACF_Form_Gutenberg' ) ) :
class ACF_Form_Gutenberg {
/**
* __construct
*
* Setup for class functionality.
*
* @date 13/12/18
* @since 5.8.0
*
* @param void
* @return void
*/
function __construct() {
// Add actions.
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
// Ignore validation during meta-box-loader AJAX request.
add_action( 'acf/validate_save_post', array( $this, 'acf_validate_save_post' ), 999 );
}
/**
* enqueue_block_editor_assets
*
* Allows a safe way to customize Guten-only functionality.
*
* @date 14/12/18
* @since 5.8.0
*
* @param void
* @return void
*/
function enqueue_block_editor_assets() {
// Remove edit_form_after_title.
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 20, 0 );
// Call edit_form_after_title manually.
add_action( 'block_editor_meta_box_hidden_fields', array( $this, 'block_editor_meta_box_hidden_fields' ) );
// Customize editor metaboxes.
add_filter( 'filter_block_editor_meta_boxes', array( $this, 'filter_block_editor_meta_boxes' ) );
// Trigger ACF enqueue scripts as the site editor doesn't trigger this from form-post.php
acf_enqueue_scripts(
array(
'uploader' => true,
)
);
}
/**
* add_meta_boxes
*
* Modify screen for Gutenberg.
*
* @date 13/12/18
* @since 5.8.0
*
* @param void
* @return void
*/
function add_meta_boxes() {
// Remove 'edit_form_after_title' action.
remove_action( 'edit_form_after_title', array( acf_get_instance( 'ACF_Form_Post' ), 'edit_form_after_title' ) );
}
/**
* block_editor_meta_box_hidden_fields
*
* Modify screen for Gutenberg.
*
* @date 13/12/18
* @since 5.8.0
*
* @param void
* @return void
*/
function block_editor_meta_box_hidden_fields() {
// Manually call 'edit_form_after_title' function.
acf_get_instance( 'ACF_Form_Post' )->edit_form_after_title();
}
/**
* filter_block_editor_meta_boxes
*
* description
*
* @date 5/4/19
* @since 5.7.14
*
* @param type $var Description. Default.
* @return type Description.
*/
function filter_block_editor_meta_boxes( $wp_meta_boxes ) {
// Globals
global $current_screen;
// Move 'acf_after_title' metaboxes into 'normal' location.
if ( isset( $wp_meta_boxes[ $current_screen->id ]['acf_after_title'] ) ) {
// Extract locations.
$locations = $wp_meta_boxes[ $current_screen->id ];
// Ensure normal location exists.
if ( ! isset( $locations['normal'] ) ) {
$locations['normal'] = array();
}
if ( ! isset( $locations['normal']['high'] ) ) {
$locations['normal']['high'] = array();
}
// Append metaboxes.
foreach ( $locations['acf_after_title'] as $priority => $meta_boxes ) {
$locations['normal']['high'] = array_merge( $meta_boxes, $locations['normal']['high'] );
}
// Update original data.
$wp_meta_boxes[ $current_screen->id ] = $locations;
unset( $wp_meta_boxes[ $current_screen->id ]['acf_after_title'] );
// Avoid conflicts with saved metabox order.
add_filter( 'get_user_option_meta-box-order_' . $current_screen->id, array( $this, 'modify_user_option_meta_box_order' ) );
}
// Return
return $wp_meta_boxes;
}
/**
* modify_user_option_meta_box_order
*
* Filters the `meta-box-order_{$post_type}` value by prepending "acf_after_title" data to "normal".
* Fixes a bug where metaboxes with position "acf_after_title" do not appear in the block editor.
*
* @date 11/7/19
* @since 5.8.2
*
* @param array $stored_meta_box_order User's existing meta box order.
* @return array Modified array with meta boxes moved around.
*/
function modify_user_option_meta_box_order( $locations ) {
if ( ! empty( $locations['acf_after_title'] ) ) {
if ( ! empty( $locations['normal'] ) ) {
$locations['normal'] = $locations['acf_after_title'] . ',' . $locations['normal'];
} else {
$locations['normal'] = $locations['acf_after_title'];
}
unset( $locations['acf_after_title'] );
}
return $locations;
}
/**
* acf_validate_save_post
*
* Ignore errors during the Gutenberg "save metaboxes" AJAX request.
* Allows data to save and prevent UX issues.
*
* @date 16/12/18
* @since 5.8.0
*
* @param void
* @return void
*/
function acf_validate_save_post() {
// Check if current request came from Gutenberg.
if ( isset( $_GET['meta-box-loader'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified elsewhere.
acf_reset_validation_errors();
}
}
}
acf_new_instance( 'ACF_Form_Gutenberg' );
endif;
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'acf_form_nav_menu' ) ) :
class acf_form_nav_menu {
/*
* __construct
*
* This function will setup the class functionality
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function __construct() {
// actions
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'wp_update_nav_menu', array( $this, 'update_nav_menu' ) );
add_action( 'acf/validate_save_post', array( $this, 'acf_validate_save_post' ), 5 );
add_action( 'wp_nav_menu_item_custom_fields', array( $this, 'wp_nav_menu_item_custom_fields' ), 10, 5 );
// filters
add_filter( 'wp_get_nav_menu_items', array( $this, 'wp_get_nav_menu_items' ), 10, 3 );
add_filter( 'wp_edit_nav_menu_walker', array( $this, 'wp_edit_nav_menu_walker' ), 10, 2 );
}
/*
* admin_enqueue_scripts
*
* This action is run after post query but before any admin script / head actions.
* It is a good place to register all actions.
*
* @type action (admin_enqueue_scripts)
* @date 26/01/13
* @since 3.6.0
*
* @param N/A
* @return N/A
*/
function admin_enqueue_scripts() {
// validate screen
if ( ! acf_is_screen( 'nav-menus' ) ) {
return;
}
// load acf scripts
acf_enqueue_scripts();
// actions
add_action( 'admin_footer', array( $this, 'admin_footer' ), 1 );
}
/**
* wp_nav_menu_item_custom_fields
*
* description
*
* @date 30/7/18
* @since 5.6.9
*
* @param type $var Description. Default.
* @return type Description.
*/
function wp_nav_menu_item_custom_fields( $item_id, $item, $depth, $args, $id = '' ) {
// vars
$prefix = "menu-item-acf[$item_id]";
// get field groups
$field_groups = acf_get_field_groups(
array(
'nav_menu_item' => $item->type,
'nav_menu_item_id' => $item_id,
'nav_menu_item_depth' => $depth,
)
);
// render
if ( ! empty( $field_groups ) ) {
// open
echo '<div class="acf-menu-item-fields acf-fields -clear">';
// loop
foreach ( $field_groups as $field_group ) {
// load fields
$fields = acf_get_fields( $field_group );
// bail if not fields
if ( empty( $fields ) ) {
continue;
}
// change prefix
acf_prefix_fields( $fields, $prefix );
// render
acf_render_fields( $fields, $item_id, 'div', $field_group['instruction_placement'] );
}
// close
echo '</div>';
// Trigger append for newly created menu item (via AJAX)
if ( acf_is_ajax( 'add-menu-item' ) ) : ?>
<script type="text/javascript">
(function($) {
acf.doAction('append', $('#menu-item-settings-<?php echo $item_id; ?>') );
})(jQuery);
</script>
<?php
endif;
}
}
/*
* update_nav_menu
*
* description
*
* @type function
* @date 26/5/17
* @since 5.6.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function update_nav_menu( $menu_id ) {
// vars
$post_id = 'term_' . $menu_id;
// verify and remove nonce
if ( ! acf_verify_nonce( 'nav_menu' ) ) {
return $menu_id;
}
// validate and show errors
acf_validate_save_post( true );
// save
acf_save_post( $post_id );
// save nav menu items
$this->update_nav_menu_items( $menu_id );
}
/*
* update_nav_menu_items
*
* description
*
* @type function
* @date 26/5/17
* @since 5.6.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function update_nav_menu_items( $menu_id ) {
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
if ( empty( $_POST['menu-item-acf'] ) ) {
return;
}
$posted_values = acf_sanitize_request_args( $_POST['menu-item-acf'] );
foreach ( $posted_values as $post_id => $values ) {
acf_save_post( $post_id, $values );
}
// phpcs:enable WordPress.Security.NonceVerification.Missing
}
/**
* wp_get_nav_menu_items
*
* WordPress does not provide an easy way to find the current menu being edited.
* This function listens to when a menu's items are loaded and stores the menu.
* Needed on nav-menus.php page for new menu with no items
*
* @date 23/2/18
* @since 5.6.9
*
* @param type $var Description. Default.
* @return type Description.
*/
function wp_get_nav_menu_items( $items, $menu, $args ) {
acf_set_data( 'nav_menu_id', $menu->term_id );
return $items;
}
/**
* Called when WP renders a menu edit form.
* Used to set global data and customize the Walker class.
*
* @date 26/5/17
* @since 5.6.0
*
* @param string $class The walker class to use. Default 'Walker_Nav_Menu_Edit'.
* @param int $menu_id ID of the menu being rendered.
* @return string
*/
function wp_edit_nav_menu_walker( $class, $menu_id = 0 ) {
// update data (needed for ajax location rules to work)
acf_set_data( 'nav_menu_id', $menu_id );
// Return class.
return $class;
}
/*
* acf_validate_save_post
*
* This function will loop over $_POST data and validate
*
* @type action 'acf/validate_save_post' 5
* @date 7/09/2016
* @since 5.4.0
*
* @param n/a
* @return n/a
*/
function acf_validate_save_post() {
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
if ( empty( $_POST['menu-item-acf'] ) ) {
return;
}
$posted_values = acf_sanitize_request_args( $_POST['menu-item-acf'] );
foreach ( $posted_values as $post_id => $values ) {
// vars
$prefix = 'menu-item-acf[' . $post_id . ']';
// validate
acf_validate_values( $values, $prefix );
}
// phpcs:enable // phpcs:disable WordPress.Security.NonceVerification.Missing
}
/*
* admin_footer
*
* This function will add some custom HTML to the footer of the edit page
*
* @type function
* @date 11/06/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function admin_footer() {
// vars
$nav_menu_id = acf_get_data( 'nav_menu_id' );
$post_id = 'term_' . $nav_menu_id;
// get field groups
$field_groups = acf_get_field_groups(
array(
'nav_menu' => $nav_menu_id,
)
);
?>
<div id="tmpl-acf-menu-settings" style="display: none;">
<?php
// data (always needed to save nav menu items)
acf_form_data(
array(
'screen' => 'nav_menu',
'post_id' => $post_id,
'ajax' => 1,
)
);
// render
if ( ! empty( $field_groups ) ) {
// loop
foreach ( $field_groups as $field_group ) {
$fields = acf_get_fields( $field_group );
echo '<div class="acf-menu-settings -' . $field_group['style'] . '">';
echo '<h2>' . $field_group['title'] . '</h2>';
echo '<div class="acf-fields -left -clear">';
acf_render_fields( $fields, $post_id, 'div', $field_group['instruction_placement'] );
echo '</div>';
echo '</div>';
}
}
?>
</div>
<script type="text/javascript">
(function($) {
// append html
var html = $('#tmpl-acf-menu-settings').html();
$('#tmpl-acf-menu-settings').remove();
$('#post-body-content').append( html );
// avoid WP over-writing $_POST data
// - https://core.trac.wordpress.org/ticket/41502#ticket
$(document).on('submit', '#update-nav-menu', function() {
// vars
var $form = $(this);
var $input = $('input[name="nav-menu-data"]');
// decode json
var json = $form.serializeArray();
var json2 = [];
// loop
$.each( json, function( i, pair ) {
// avoid nesting (unlike WP)
if( pair.name === 'nav-menu-data' ) return;
// bail early if is 'acf[' input
if( pair.name.indexOf('acf[') > -1 ) return;
// append
json2.push( pair );
});
// update
$input.val( JSON.stringify(json2) );
});
})(jQuery);
</script>
<?php
}
}
acf_new_instance( 'acf_form_nav_menu' );
endif;
?>
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'ACF_Form_Post' ) ) :
class ACF_Form_Post {
/** @var string The first field groups style CSS. */
var $style = '';
/**
* __construct
*
* Sets up the class functionality.
*
* @date 5/03/2014
* @since 5.0.0
*
* @param void
* @return void
*/
function __construct() {
// initialize on post edit screens
add_action( 'load-post.php', array( $this, 'initialize' ) );
add_action( 'load-post-new.php', array( $this, 'initialize' ) );
// save
add_filter( 'wp_insert_post_empty_content', array( $this, 'wp_insert_post_empty_content' ), 10, 2 );
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
}
/**
* initialize
*
* Sets up Form functionality.
*
* @date 19/9/18
* @since 5.7.6
*
* @param void
* @return void
*/
function initialize() {
// globals
global $typenow;
$acf_post_types = acf_get_internal_post_types();
foreach ( $acf_post_types as $post_type ) {
remove_meta_box( 'submitdiv', $post_type, 'side' );
}
// restrict specific post types
$restricted = array_merge( $acf_post_types, array( 'acf-taxonomy', 'attachment' ) );
if ( in_array( $typenow, $restricted ) ) {
return;
}
// enqueue scripts
acf_enqueue_scripts(
array(
'uploader' => true,
)
);
// actions
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 2 );
}
/**
* add_meta_boxes
*
* Adds ACF metaboxes for the given $post_type and $post.
*
* @date 19/9/18
* @since 5.7.6
*
* @param string $post_type The post type.
* @param WP_Post $post The post being edited.
* @return void
*/
function add_meta_boxes( $post_type, $post ) {
// Storage for localized postboxes.
$postboxes = array();
// Get field groups for this screen.
$field_groups = acf_get_field_groups(
array(
'post_id' => $post->ID,
'post_type' => $post_type,
)
);
// Loop over field groups.
if ( $field_groups ) {
foreach ( $field_groups as $field_group ) {
// vars
$id = "acf-{$field_group['key']}"; // acf-group_123
$title = $field_group['title']; // Group 1
$context = $field_group['position']; // normal, side, acf_after_title
$priority = 'high'; // high, core, default, low
// Reduce priority for sidebar metaboxes for best position.
if ( $context == 'side' ) {
$priority = 'core';
}
/**
* Filters the metabox priority.
*
* @date 23/06/12
* @since 3.1.8
*
* @param string $priority The metabox priority (high, core, default, low).
* @param array $field_group The field group array.
*/
$priority = apply_filters( 'acf/input/meta_box_priority', $priority, $field_group );
// Localize data
$postboxes[] = array(
'id' => $id,
'key' => $field_group['key'],
'style' => $field_group['style'],
'label' => $field_group['label_placement'],
'edit' => acf_get_field_group_edit_link( $field_group['ID'] ),
);
// Add the meta box.
add_meta_box( $id, acf_esc_html( $title ), array( $this, 'render_meta_box' ), $post_type, $context, $priority, array( 'field_group' => $field_group ) );
}
// Set style from first field group.
$this->style = acf_get_field_group_style( $field_groups[0] );
// Localize postboxes.
acf_localize_data(
array(
'postboxes' => $postboxes,
)
);
}
// remove postcustom metabox (removes expensive SQL query)
if ( acf_get_setting( 'remove_wp_meta_box' ) ) {
remove_meta_box( 'postcustom', false, 'normal' );
}
// Add hidden input fields.
add_action( 'edit_form_after_title', array( $this, 'edit_form_after_title' ) );
/**
* Fires after metaboxes have been added.
*
* @date 13/12/18
* @since 5.8.0
*
* @param string $post_type The post type.
* @param WP_Post $post The post being edited.
* @param array $field_groups The field groups added.
*/
do_action( 'acf/add_meta_boxes', $post_type, $post, $field_groups );
}
/**
* edit_form_after_title
*
* Called after the title adn before the content editor.
*
* @date 19/9/18
* @since 5.7.6
*
* @param void
* @return void
*/
function edit_form_after_title() {
// globals
global $post, $wp_meta_boxes;
// render post data
acf_form_data(
array(
'screen' => 'post',
'post_id' => $post->ID,
)
);
// render 'acf_after_title' metaboxes
do_meta_boxes( get_current_screen(), 'acf_after_title', $post );
// render dynamic field group style
echo '<style type="text/css" id="acf-style">' . $this->style . '</style>';
}
/**
* render_meta_box
*
* Renders the ACF metabox HTML.
*
* @date 19/9/18
* @since 5.7.6
*
* @param WP_Post $post The post being edited.
* @param array metabox The add_meta_box() args.
* @return void
*/
function render_meta_box( $post, $metabox ) {
// vars
$id = $metabox['id'];
$field_group = $metabox['args']['field_group'];
// Render fields.
$fields = acf_get_fields( $field_group );
acf_render_fields( $fields, $post->ID, 'div', $field_group['instruction_placement'] );
}
/**
* wp_insert_post_empty_content
*
* Allows WP to insert a new post without title or post_content if ACF data exists.
*
* @date 16/07/2014
* @since 5.0.1
*
* @param bool $maybe_empty Whether the post should be considered "empty".
* @param array $postarr Array of post data.
* @return bool
*/
function wp_insert_post_empty_content( $maybe_empty, $postarr ) {
// return false and allow insert if '_acf_changed' exists
if ( $maybe_empty && acf_maybe_get_POST( '_acf_changed' ) ) {
return false;
}
// return
return $maybe_empty;
}
/*
* allow_save_post
*
* Checks if the $post is allowed to be saved.
* Used to avoid triggering "acf/save_post" on dynamically created posts during save.
*
* @type function
* @date 26/06/2016
* @since 5.3.8
*
* @param WP_Post $post The post to check.
* @return bool
*/
function allow_save_post( $post ) {
// vars
$allow = true;
// restrict post types
$restrict = array( 'auto-draft', 'revision', 'acf-field', 'acf-field-group' );
if ( in_array( $post->post_type, $restrict ) ) {
$allow = false;
}
// disallow if the $_POST ID value does not match the $post->ID
$form_post_id = (int) acf_maybe_get_POST( 'post_ID' );
if ( $form_post_id && $form_post_id !== $post->ID ) {
$allow = false;
}
// revision (preview)
if ( $post->post_type == 'revision' ) {
// allow if doing preview and this $post is a child of the $_POST ID
if ( acf_maybe_get_POST( 'wp-preview' ) == 'dopreview' && $form_post_id === $post->post_parent ) {
$allow = true;
}
}
// return
return $allow;
}
/*
* save_post
*
* Triggers during the 'save_post' action to save the $_POST data.
*
* @type function
* @date 23/06/12
* @since 1.0.0
*
* @param int $post_id The post ID
* @param WP_POST $post the post object.
* @return int
*/
function save_post( $post_id, $post ) {
// bail early if no allowed to save this post type
if ( ! $this->allow_save_post( $post ) ) {
return $post_id;
}
// verify nonce
if ( ! acf_verify_nonce( 'post' ) ) {
return $post_id;
}
// validate for published post (allow draft to save without validation)
if ( $post->post_status == 'publish' ) {
// bail early if validation fails
if ( ! acf_validate_save_post() ) {
return;
}
}
// save
acf_save_post( $post_id );
// save revision
if ( post_type_supports( $post->post_type, 'revisions' ) ) {
acf_save_post_revision( $post_id );
}
// return
return $post_id;
}
}
acf_new_instance( 'ACF_Form_Post' );
endif;
<?php
/*
* ACF Taxonomy Form Class
*
* All the logic for adding fields to taxonomy terms
*
* @class acf_form_taxonomy
* @package ACF
* @subpackage Forms
*/
if ( ! class_exists( 'acf_form_taxonomy' ) ) :
class acf_form_taxonomy {
var $view = 'add';
/*
* __construct
*
* This function will setup the class functionality
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function __construct() {
// actions
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
// save
add_action( 'create_term', array( $this, 'save_term' ), 10, 3 );
add_action( 'edit_term', array( $this, 'save_term' ), 10, 3 );
// delete
add_action( 'delete_term', array( $this, 'delete_term' ), 10, 4 );
}
/*
* validate_page
*
* This function will check if the current page is for a post/page edit form
*
* @type function
* @date 23/06/12
* @since 3.1.8
*
* @param n/a
* @return (boolean)
*/
function validate_page() {
// global
global $pagenow;
// validate page
if ( $pagenow === 'edit-tags.php' || $pagenow === 'term.php' ) {
return true;
}
// return
return false;
}
/*
* admin_enqueue_scripts
*
* This action is run after post query but before any admin script / head actions.
* It is a good place to register all actions.
*
* @type action (admin_enqueue_scripts)
* @date 26/01/13
* @since 3.6.0
*
* @param N/A
* @return N/A
*/
function admin_enqueue_scripts() {
// validate page
if ( ! $this->validate_page() ) {
return;
}
// vars
$screen = get_current_screen();
$taxonomy = $screen->taxonomy;
// load acf scripts
acf_enqueue_scripts();
// actions
add_action( 'admin_footer', array( $this, 'admin_footer' ), 10, 1 );
add_action( "{$taxonomy}_add_form_fields", array( $this, 'add_term' ), 10, 1 );
add_action( "{$taxonomy}_edit_form", array( $this, 'edit_term' ), 10, 2 );
}
/*
* add_term
*
* description
*
* @type function
* @date 8/10/13
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function add_term( $taxonomy ) {
// vars
$post_id = 'term_0';
// update vars
$this->view = 'add';
// get field groups
$field_groups = acf_get_field_groups(
array(
'taxonomy' => $taxonomy,
)
);
// render
if ( ! empty( $field_groups ) ) {
// data
acf_form_data(
array(
'screen' => 'taxonomy',
'post_id' => $post_id,
)
);
// wrap
echo '<div id="acf-term-fields" class="acf-fields -clear">';
// loop
foreach ( $field_groups as $field_group ) {
$fields = acf_get_fields( $field_group );
acf_render_fields( $fields, $post_id, 'div', 'field' );
}
// wrap
echo '</div>';
}
}
/*
* edit_term
*
* description
*
* @type function
* @date 8/10/13
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function edit_term( $term, $taxonomy ) {
// vars
$post_id = 'term_' . $term->term_id;
// update vars
$this->view = 'edit';
// get field groups
$field_groups = acf_get_field_groups(
array(
'taxonomy' => $taxonomy,
)
);
// render
if ( ! empty( $field_groups ) ) {
acf_form_data(
array(
'screen' => 'taxonomy',
'post_id' => $post_id,
)
);
foreach ( $field_groups as $field_group ) {
// title
if ( $field_group['style'] == 'default' ) {
echo '<h2>' . $field_group['title'] . '</h2>';
}
// fields
echo '<table class="form-table">';
$fields = acf_get_fields( $field_group );
acf_render_fields( $fields, $post_id, 'tr', 'field' );
echo '</table>';
}
}
}
/*
* admin_footer
*
* description
*
* @type function
* @date 27/03/2015
* @since 5.1.5
*
* @param $post_id (int)
* @return $post_id (int)
*/
function admin_footer() {
?>
<script type="text/javascript">
(function($) {
// Define vars.
var view = '<?php echo $this->view; ?>';
var $form = $('#' + view + 'tag');
var $submit = $('#' + view + 'tag input[type="submit"]:last');
// Add missing spinner.
if( !$submit.next('.spinner').length ) {
$submit.after('<span class="spinner"></span>');
}
<?php
// View: Add.
if ( $this->view == 'add' ) :
?>
// vars
var $fields = $('#acf-term-fields');
var html = '';
// Store a copy of the $fields html used later to replace after AJAX request.
// Hook into 'prepare' action to allow ACF core helpers to first modify DOM.
// Fixes issue where hidden #acf-hidden-wp-editor is initialized again.
acf.addAction('prepare', function(){
html = $fields.html();
}, 6);
// WP triggers click as primary action
$submit.on('click', function( e ){
// validate
var valid = acf.validateForm({
form: $form,
event: e,
reset: true
});
// if not valid, stop event and allow validation to continue
if( !valid ) {
e.preventDefault();
e.stopImmediatePropagation();
}
});
// listen to AJAX add-tag complete
$(document).ajaxComplete(function(event, xhr, settings) {
// bail early if is other ajax call
if( settings.data.indexOf('action=add-tag') == -1 ) {
return;
}
// bail early if response contains error
if( xhr.responseText.indexOf('wp_error') !== -1 ) {
return;
}
// action for 3rd party customization
acf.doAction('remove', $fields);
// reset HTML
$fields.html( html );
// action for 3rd party customization
acf.doAction('append', $fields);
// reset unload
acf.unload.reset();
});
<?php endif; ?>
})(jQuery);
</script>
<?php
}
/*
* save_term
*
* description
*
* @type function
* @date 8/10/13
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function save_term( $term_id, $tt_id, $taxonomy ) {
// vars
$post_id = 'term_' . $term_id;
// verify and remove nonce
if ( ! acf_verify_nonce( 'taxonomy' ) ) {
return $term_id;
}
// valied and show errors
acf_validate_save_post( true );
// save
acf_save_post( $post_id );
}
/*
* delete_term
*
* description
*
* @type function
* @date 15/10/13
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function delete_term( $term, $tt_id, $taxonomy, $deleted_term ) {
// bail early if termmeta table exists
if ( acf_isset_termmeta() ) {
return $term;
}
// globals
global $wpdb;
// vars
$search = $taxonomy . '_' . $term . '_%';
$_search = '_' . $search;
// escape '_'
// http://stackoverflow.com/questions/2300285/how-do-i-escape-in-sql-server
$search = str_replace( '_', '\_', $search );
$_search = str_replace( '_', '\_', $_search );
// delete
$result = $wpdb->query(
$wpdb->prepare(
"DELETE FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s",
$search,
$_search
)
);
}
}
new acf_form_taxonomy();
endif;
?>
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'ACF_Form_User' ) ) :
class ACF_Form_User {
/** @var string The current view (new, edit, register) */
var $view = '';
/*
* __construct
*
* This function will setup the class functionality
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function __construct() {
// enqueue
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'login_form_register', array( $this, 'login_form_register' ) );
// render
add_action( 'show_user_profile', array( $this, 'render_edit' ) );
add_action( 'edit_user_profile', array( $this, 'render_edit' ) );
add_action( 'user_new_form', array( $this, 'render_new' ) );
add_action( 'register_form', array( $this, 'render_register' ) );
// save
add_action( 'user_register', array( $this, 'save_user' ) );
add_action( 'profile_update', array( $this, 'save_user' ) );
// Perform validation before new user is registered.
add_filter( 'registration_errors', array( $this, 'filter_registration_errors' ), 10, 3 );
}
/**
* admin_enqueue_scripts
*
* Checks current screen and enqueues scripts
*
* @date 17/4/18
* @since 5.6.9
*
* @param void
* @return void
*/
function admin_enqueue_scripts() {
// bail early if not valid screen
if ( ! acf_is_screen( array( 'profile', 'user', 'user-edit', 'profile-network', 'user-network', 'user-edit-network' ) ) ) {
return;
}
// enqueue
acf_enqueue_scripts();
}
/**
* login_form_register
*
* Customizes and enqueues scripts
*
* @date 17/4/18
* @since 5.6.9
*
* @param void
* @return void
*/
function login_form_register() {
// customize action prefix so that "admin_head" = "login_head"
acf_enqueue_scripts(
array(
'context' => 'login',
)
);
}
/*
* register_user
*
* Called during the user register form
*
* @type function
* @date 8/10/13
* @since 5.0.0
*
* @param void
* @return void
*/
function render_register() {
// render
$this->render(
array(
'user_id' => 0,
'view' => 'register',
'el' => 'div',
)
);
}
/*
* render_edit
*
* Called during the user edit form
*
* @type function
* @date 8/10/13
* @since 5.0.0
*
* @param void
* @return void
*/
function render_edit( $user ) {
// add compatibility with front-end user profile edit forms such as bbPress
if ( ! is_admin() ) {
acf_enqueue_scripts();
}
// render
$this->render(
array(
'user_id' => $user->ID,
'view' => 'edit',
'el' => 'tr',
)
);
}
/*
* user_new_form
*
* description
*
* @type function
* @date 8/10/13
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function render_new() {
// Multisite uses a different 'user-new.php' form. Don't render fields here
if ( is_multisite() ) {
return;
}
// render
$this->render(
array(
'user_id' => 0,
'view' => 'add',
'el' => 'tr',
)
);
}
/*
* render
*
* This function will render ACF fields for a given $post_id parameter
*
* @type function
* @date 7/10/13
* @since 5.0.0
*
* @param $user_id (int) this can be set to 0 for a new user
* @param $user_form (string) used for location rule matching. edit | add | register
* @param $el (string)
* @return n/a
*/
function render( $args = array() ) {
// Allow $_POST data to persist across form submission attempts.
if ( isset( $_POST['acf'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
add_filter( 'acf/pre_load_value', array( $this, 'filter_pre_load_value' ), 10, 3 );
}
// defaults
$args = wp_parse_args(
$args,
array(
'user_id' => 0,
'view' => 'edit',
'el' => 'tr',
)
);
// vars
$post_id = 'user_' . $args['user_id'];
// get field groups
$field_groups = acf_get_field_groups(
array(
'user_id' => $args['user_id'] ? $args['user_id'] : 'new',
'user_form' => $args['view'],
)
);
// bail early if no field groups
if ( empty( $field_groups ) ) {
return;
}
// form data
acf_form_data(
array(
'screen' => 'user',
'post_id' => $post_id,
'validation' => ( $args['view'] == 'register' ) ? 0 : 1,
)
);
// elements
$before = '<table class="form-table"><tbody>';
$after = '</tbody></table>';
if ( $args['el'] == 'div' ) {
$before = '<div class="acf-user-' . $args['view'] . '-fields acf-fields -clear">';
$after = '</div>';
}
// loop
foreach ( $field_groups as $field_group ) {
// vars
$fields = acf_get_fields( $field_group );
// title
if ( $field_group['style'] === 'default' ) {
echo '<h2>' . $field_group['title'] . '</h2>';
}
// render
echo $before;
acf_render_fields( $fields, $post_id, $args['el'], $field_group['instruction_placement'] );
echo $after;
}
// actions
add_action( 'acf/input/admin_footer', array( $this, 'admin_footer' ), 10, 1 );
}
/*
* admin_footer
*
* description
*
* @type function
* @date 27/03/2015
* @since 5.1.5
*
* @param $post_id (int)
* @return $post_id (int)
*/
function admin_footer() {
// script
?>
<script type="text/javascript">
(function($) {
// vars
var view = '<?php echo $this->view; ?>';
// add missing spinners
var $submit = $('input.button-primary');
if( !$submit.next('.spinner').length ) {
$submit.after('<span class="spinner"></span>');
}
})(jQuery);
</script>
<?php
}
/*
* save_user
*
* description
*
* @type function
* @date 8/10/13
* @since 5.0.0
*
* @param $post_id (int)
* @return $post_id (int)
*/
function save_user( $user_id ) {
// verify nonce
if ( ! acf_verify_nonce( 'user' ) ) {
return $user_id;
}
// save
if ( acf_validate_save_post( true ) ) {
acf_save_post( "user_$user_id" );
}
}
/**
* filter_registration_errors
*
* Validates $_POST data and appends any errors to prevent new user registration.
*
* @date 12/7/19
* @since 5.8.1
*
* @param WP_Error $errors A WP_Error object containing any errors encountered during registration.
* @param string $sanitized_user_login User's username after it has been sanitized.
* @param string $user_email User's email.
* @return WP_Error
*/
function filter_registration_errors( $errors, $sanitized_user_login, $user_email ) {
if ( ! acf_validate_save_post() ) {
$acf_errors = acf_get_validation_errors();
foreach ( $acf_errors as $acf_error ) {
$errors->add(
acf_idify( $acf_error['input'] ),
acf_esc_html( acf_punctify( sprintf( __( '<strong>Error</strong>: %s', 'acf' ), $acf_error['message'] ) ) )
);
}
}
return $errors;
}
/**
* filter_pre_load_value
*
* Checks if a $_POST value exists for this field to allow persistent values.
*
* @date 12/7/19
* @since 5.8.2
*
* @param null $null A null placeholder.
* @param (int|string) $post_id The post id.
* @param array $field The field array.
* @return mixed
*/
function filter_pre_load_value( $null, $post_id, $field ) {
$field_key = $field['key'];
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified in save_user().
if ( isset( $_POST['acf'][ $field_key ] ) ) {
return $_POST['acf'][ $field_key ]; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized elsewhere.
}
// phpcs:enable WordPress.Security.NonceVerification.Missing
return $null;
}
}
// instantiate
acf_new_instance( 'ACF_Form_User' );
endif; // class_exists check
?>
<?php
/*
* ACF Widget Form Class
*
* All the logic for adding fields to widgets
*
* @class acf_form_widget
* @package ACF
* @subpackage Forms
*/
if ( ! class_exists( 'acf_form_widget' ) ) :
#[AllowDynamicProperties]
class acf_form_widget {
/*
* __construct
*
* This function will setup the class functionality
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function __construct() {
// vars
$this->preview_values = array();
$this->preview_reference = array();
$this->preview_errors = array();
// actions
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
add_action( 'in_widget_form', array( $this, 'edit_widget' ), 10, 3 );
add_action( 'acf/validate_save_post', array( $this, 'acf_validate_save_post' ), 5 );
// filters
add_filter( 'widget_update_callback', array( $this, 'save_widget' ), 10, 4 );
}
/*
* admin_enqueue_scripts
*
* This action is run after post query but before any admin script / head actions.
* It is a good place to register all actions.
*
* @type action (admin_enqueue_scripts)
* @date 26/01/13
* @since 3.6.0
*
* @param N/A
* @return N/A
*/
function admin_enqueue_scripts() {
// validate screen
if ( acf_is_screen( 'widgets' ) || acf_is_screen( 'customize' ) ) {
// valid
} else {
return;
}
// load acf scripts
acf_enqueue_scripts();
// actions
add_action( 'acf/input/admin_footer', array( $this, 'admin_footer' ), 1 );
}
/*
* acf_validate_save_post
*
* This function will loop over $_POST data and validate
*
* @type action 'acf/validate_save_post' 5
* @date 7/09/2016
* @since 5.4.0
*
* @param n/a
* @return n/a
*/
function acf_validate_save_post() {
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
// bail early if not widget
if ( ! isset( $_POST['_acf_widget_id'] ) ) {
return;
}
// vars
$id = sanitize_text_field( $_POST['_acf_widget_id'] );
$number = sanitize_text_field( $_POST['_acf_widget_number'] );
$prefix = sanitize_text_field( $_POST['_acf_widget_prefix'] );
$values = acf_sanitize_request_args( $_POST[ $id ][ $number ]['acf'] );
// validate
acf_validate_values( $values, $prefix );
// phpcs:enable WordPress.Security.NonceVerification.Missing
}
/*
* edit_widget
*
* This function will render the fields for a widget form
*
* @type function
* @date 11/06/2014
* @since 5.0.0
*
* @param $widget (object)
* @param $return (null)
* @param $instance (object)
* @return $post_id (int)
*/
function edit_widget( $widget, $return, $instance ) {
// vars
$post_id = 0;
$prefix = 'widget-' . $widget->id_base . '[' . $widget->number . '][acf]';
// get id
if ( $widget->number !== '__i__' ) {
$post_id = "widget_{$widget->id}";
}
// get field groups
$field_groups = acf_get_field_groups(
array(
'widget' => $widget->id_base,
)
);
// render
if ( ! empty( $field_groups ) ) {
// render post data
acf_form_data(
array(
'screen' => 'widget',
'post_id' => $post_id,
'widget_id' => 'widget-' . $widget->id_base,
'widget_number' => $widget->number,
'widget_prefix' => $prefix,
)
);
// wrap
echo '<div class="acf-widget-fields acf-fields -clear">';
// loop
foreach ( $field_groups as $field_group ) {
// load fields
$fields = acf_get_fields( $field_group );
// bail if not fields
if ( empty( $fields ) ) {
continue;
}
// change prefix
acf_prefix_fields( $fields, $prefix );
// render
acf_render_fields( $fields, $post_id, 'div', $field_group['instruction_placement'] );
}
// wrap
echo '</div>';
// jQuery selector looks odd, but is necessary due to WP adding an incremental number into the ID
// - not possible to find number via PHP parameters
if ( $widget->updated ) : ?>
<script type="text/javascript">
(function($) {
acf.doAction('append', $('[id^="widget"][id$="<?php echo $widget->id; ?>"]') );
})(jQuery);
</script>
<?php
endif;
}
}
/*
* save_widget
*
* This function will hook into the widget update filter and save ACF data
*
* @type function
* @date 27/05/2015
* @since 5.2.3
*
* @param $instance (array) widget settings
* @param $new_instance (array) widget settings
* @param $old_instance (array) widget settings
* @param $widget (object) widget info
* @return $instance
*/
function save_widget( $instance, $new_instance, $old_instance, $widget ) {
// validate nonce if we're not a REST API request.
// the $_POST object is not available to us to validate if we're in a REST API call.
if ( ! ( function_exists( 'wp_is_json_request' ) && wp_is_json_request() ) ) {
if ( ! acf_verify_nonce( 'widget' ) ) {
return $instance;
}
}
// bail early if not valid (!customize + acf values + nonce).
if ( isset( $_POST['wp_customize'] ) || ! isset( $new_instance['acf'] ) ) {
return $instance;
}
// save
acf_save_post( "widget_{$widget->id}", $new_instance['acf'] );
// return
return $instance;
}
/*
* admin_footer
*
* This function will add some custom HTML to the footer of the edit page
*
* @type function
* @date 11/06/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function admin_footer() {
?>
<script type="text/javascript">
(function($) {
// vars
acf.set('post_id', 'widgets');
// Only initialize visible fields.
acf.addFilter('find_fields', function( $fields ){
// not templates
$fields = $fields.not('#available-widgets .acf-field');
// not widget dragging in
$fields = $fields.not('.widget.ui-draggable-dragging .acf-field');
// return
return $fields;
});
// on publish
$('#widgets-right').on('click', '.widget-control-save', function( e ){
// vars
var $button = $(this);
var $form = $button.closest('form');
// validate
var valid = acf.validateForm({
form: $form,
event: e,
reset: true
});
// if not valid, stop event and allow validation to continue
if( !valid ) {
e.preventDefault();
e.stopImmediatePropagation();
}
});
// show
$('#widgets-right').on('click', '.widget-top', function(){
var $widget = $(this).parent();
if( $widget.hasClass('open') ) {
acf.doAction('hide', $widget);
} else {
acf.doAction('show', $widget);
}
});
$(document).on('widget-added', function( e, $widget ){
// - use delay to avoid rendering issues with customizer (ensures div is visible)
setTimeout(function(){
acf.doAction('append', $widget );
}, 100);
});
})(jQuery);
</script>
<?php
}
}
new acf_form_widget();
endif;
?>
<?php
/**
* Determine the current locale desired for the request.
*
* @since 5.0.0
*
* @global string $pagenow
*
* @return string The determined locale.
*/
if ( ! function_exists( 'determine_locale' ) ) :
function determine_locale() {
/**
* Filters the locale for the current request prior to the default determination process.
*
* Using this filter allows to override the default logic, effectively short-circuiting the function.
*
* @since 5.0.0
*
* @param string|null The locale to return and short-circuit, or null as default.
*/
$determined_locale = apply_filters( 'pre_determine_locale', null );
if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) {
return $determined_locale;
}
$determined_locale = get_locale();
if ( function_exists( 'get_user_locale' ) && is_admin() ) {
$determined_locale = get_user_locale();
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Copied from WordPress core.
if ( function_exists( 'get_user_locale' ) && isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] ) {
$determined_locale = get_user_locale();
}
if ( ! empty( $_GET['wp_lang'] ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
$determined_locale = sanitize_text_field( $_GET['wp_lang'] );
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
/**
* Filters the locale for the current request.
*
* @since 5.0.0
*
* @param string $locale The locale.
*/
return apply_filters( 'determine_locale', $determined_locale );
}
endif;
/*
* acf_get_locale
*
* Returns the current locale.
*
* @date 16/12/16
* @since 5.5.0
*
* @param void
* @return string
*/
function acf_get_locale() {
// Determine local.
$locale = determine_locale();
// Fallback to parent language for regions without translation.
// https://wpastra.com/docs/complete-list-wordpress-locale-codes/
$langs = array(
'az_TR' => 'az', // Azerbaijani (Turkey)
'zh_HK' => 'zh_TW', // Chinese (Hong Kong)
'fr_BE' => 'fr_FR', // French (Belgium)
'nn_NO' => 'nb_NO', // Norwegian (Nynorsk)
'fa_AF' => 'fa_IR', // Persian (Afghanistan)
'ru_UA' => 'ru_RU', // Russian (Ukraine)
);
if ( isset( $langs[ $locale ] ) ) {
$locale = $langs[ $locale ];
}
/**
* Filters the determined local.
*
* @date 8/1/19
* @since 5.7.10
*
* @param string $locale The local.
*/
return apply_filters( 'acf/get_locale', $locale );
}
/**
* acf_load_textdomain
*
* Loads the plugin's translated strings similar to load_plugin_textdomain().
*
* @date 8/1/19
* @since 5.7.10
*
* @param string $locale The plugin's current locale.
* @return void
*/
function acf_load_textdomain( $domain = 'acf' ) {
/**
* Filters a plugin's locale.
*
* @date 8/1/19
* @since 5.7.10
*
* @param string $locale The plugin's current locale.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
*/
$locale = apply_filters( 'plugin_locale', acf_get_locale(), $domain );
$mofile = $domain . '-' . $locale . '.mo';
// Load from plugin lang folder.
return load_textdomain( $domain, acf_get_path( 'lang/' . $mofile ) );
}
/**
* _acf_apply_language_cache_key
*
* Applies the current language to the cache key.
*
* @date 23/1/19
* @since 5.7.11
*
* @param string $key The cache key.
* @return string
*/
function _acf_apply_language_cache_key( $key ) {
// Get current language.
$current_language = acf_get_setting( 'current_language' );
if ( $current_language ) {
$key = "{$key}:{$current_language}";
}
// Return key.
return $key;
}
// Hook into filter.
add_filter( 'acf/get_cache_key', '_acf_apply_language_cache_key' );
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'ACF_Legacy_Locations' ) ) :
class ACF_Legacy_Locations {
/**
* Magic __isset method for backwards compatibility.
*
* @date 10/4/20
* @since 5.9.0
*
* @param string $key Key name.
* @return bool
*/
public function __isset( $key ) {
// _doing_it_wrong( __FUNCTION__, __( 'The ACF_Locations class should not be accessed directly.', 'acf' ), '5.9.0' );
return (
$key === 'locations'
);
}
/**
* Magic __get method for backwards compatibility.
*
* @date 10/4/20
* @since 5.9.0
*
* @param string $key Key name.
* @return mixed
*/
public function __get( $key ) {
// _doing_it_wrong( __FUNCTION__, __( 'The ACF_Locations class should not be accessed directly.', 'acf' ), '5.9.0' );
switch ( $key ) {
case 'locations':
return call_user_func( 'acf_get_location_types' );
}
return null;
}
/**
* Magic __call method for backwards compatibility.
*
* @date 10/4/20
* @since 5.9.0
*
* @param string $name The method name.
* @param array $arguments The array of arguments.
* @return mixed
*/
public function __call( $name, $arguments ) {
// _doing_it_wrong( __FUNCTION__, __( 'The ACF_Locations class should not be accessed directly.', 'acf' ), '5.9.0' );
switch ( $name ) {
case 'register_location':
return call_user_func_array( 'acf_register_location_type', $arguments );
case 'get_location':
return call_user_func_array( 'acf_get_location_type', $arguments );
case 'get_locations':
return call_user_func_array( 'acf_get_location_rule_types', $arguments );
}
}
}
endif; // class_exists check
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'ACF_Local_Meta' ) ) :
class ACF_Local_Meta {
/** @var array Storage for meta data. */
var $meta = array();
/** @var mixed Storage for the current post_id. */
var $post_id = 0;
/**
* __construct
*
* Sets up the class functionality.
*
* @date 8/10/18
* @since 5.8.0
*
* @param void
* @return void
*/
function __construct() {
// add filters
add_filter( 'acf/pre_load_post_id', array( $this, 'pre_load_post_id' ), 1, 2 );
add_filter( 'acf/pre_load_meta', array( $this, 'pre_load_meta' ), 1, 2 );
add_filter( 'acf/pre_load_metadata', array( $this, 'pre_load_metadata' ), 1, 4 );
}
/**
* add
*
* Adds postmeta to storage.
* Accepts data in either raw or request format.
*
* @date 8/10/18
* @since 5.8.0
*
* @param array $meta An array of metdata to store.
* @param mixed $post_id The post_id for this data.
* @param bool $is_main Makes this postmeta visible to get_field() without a $post_id value.
* @return array
*/
function add( $meta = array(), $post_id = 0, $is_main = false ) {
// Capture meta if supplied meta is from a REQUEST.
if ( $this->is_request( $meta ) ) {
$meta = $this->capture( $meta, $post_id );
}
// Add to storage.
$this->meta[ $post_id ] = $meta;
// Set $post_id reference when is the "main" postmeta.
if ( $is_main ) {
$this->post_id = $post_id;
}
// Return meta.
return $meta;
}
/**
* is_request
*
* Returns true if the supplied $meta is from a REQUEST (serialized <form> data).
*
* @date 11/3/19
* @since 5.7.14
*
* @param array $meta An array of metdata to check.
* @return bool
*/
function is_request( $meta = array() ) {
return acf_is_field_key( key( $meta ) );
}
/**
* capture
*
* Returns a flattened array of meta for the given postdata.
* This is achieved by simulating a save whilst capturing all meta changes.
*
* @date 26/2/19
* @since 5.7.13
*
* @param array $values An array of raw values.
* @param mixed $post_id The post_id for this data.
* @return array
*/
function capture( $values = array(), $post_id = 0 ) {
// Reset meta.
$this->meta[ $post_id ] = array();
// Listen for any added meta.
add_filter( 'acf/pre_update_metadata', array( $this, 'capture_update_metadata' ), 1, 5 );
// Simulate update.
if ( $values ) {
acf_update_values( $values, $post_id );
}
// Remove listener filter.
remove_filter( 'acf/pre_update_metadata', array( $this, 'capture_update_metadata' ), 1, 5 );
// Return meta.
return $this->meta[ $post_id ];
}
/**
* capture_update_metadata
*
* Records all meta activity and returns a non null value to bypass DB updates.
*
* @date 26/2/19
* @since 5.7.13
*
* @param null $null .
* @param (int|string) $post_id The post id.
* @param string $name The meta name.
* @param mixed $value The meta value.
* @param bool $hidden If the meta is hidden (starts with an underscore).
* @return false.
*/
function capture_update_metadata( $null, $post_id, $name, $value, $hidden ) {
$name = ( $hidden ? '_' : '' ) . $name;
$this->meta[ $post_id ][ $name ] = $value;
// Return non null value to escape update process.
return true;
}
/**
* remove
*
* Removes postmeta from storage.
*
* @date 8/10/18
* @since 5.8.0
*
* @param mixed $post_id The post_id for this data.
* @return void
*/
function remove( $post_id = 0 ) {
// unset meta
unset( $this->meta[ $post_id ] );
// reset post_id
if ( $post_id === $this->post_id ) {
$this->post_id = 0;
}
}
/**
* pre_load_meta
*
* Injects the local meta.
*
* @date 8/10/18
* @since 5.8.0
*
* @param null $null An empty parameter. Return a non null value to short-circuit the function.
* @param mixed $post_id The post_id for this data.
* @return mixed
*/
function pre_load_meta( $null, $post_id ) {
if ( isset( $this->meta[ $post_id ] ) ) {
return $this->meta[ $post_id ];
}
return $null;
}
/**
* pre_load_metadata
*
* Injects the local meta.
*
* @date 8/10/18
* @since 5.8.0
*
* @param null $null An empty parameter. Return a non null value to short-circuit the function.
* @param (int|string) $post_id The post id.
* @param string $name The meta name.
* @param bool $hidden If the meta is hidden (starts with an underscore).
* @return mixed
*/
function pre_load_metadata( $null, $post_id, $name, $hidden ) {
$name = ( $hidden ? '_' : '' ) . $name;
if ( isset( $this->meta[ $post_id ] ) ) {
if ( isset( $this->meta[ $post_id ][ $name ] ) ) {
return $this->meta[ $post_id ][ $name ];
}
return '__return_null';
}
return $null;
}
/**
* pre_load_post_id
*
* Injects the local post_id.
*
* @date 8/10/18
* @since 5.8.0
*
* @param null $null An empty parameter. Return a non null value to short-circuit the function.
* @param mixed $post_id The post_id for this data.
* @return mixed
*/
function pre_load_post_id( $null, $post_id ) {
if ( ! $post_id && $this->post_id ) {
return $this->post_id;
}
return $null;
}
}
endif; // class_exists check
/**
* acf_setup_meta
*
* Adds postmeta to storage.
*
* @date 8/10/18
* @since 5.8.0
* @see ACF_Local_Meta::add() for list of parameters.
*
* @return array
*/
function acf_setup_meta( $meta = array(), $post_id = 0, $is_main = false ) {
return acf_get_instance( 'ACF_Local_Meta' )->add( $meta, $post_id, $is_main );
}
/**
* acf_reset_meta
*
* Removes postmeta to storage.
*
* @date 8/10/18
* @since 5.8.0
* @see ACF_Local_Meta::remove() for list of parameters.
*
* @return void
*/
function acf_reset_meta( $post_id = 0 ) {
return acf_get_instance( 'ACF_Local_Meta' )->remove( $post_id );
}
<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Register store.
acf_register_store( 'location-types' );
/**
* Registers a location type.
*
* @date 8/4/20
* @since 5.9.0
*
* @param string $class_name The location class name.
* @return (ACF_Location|false)
*/
function acf_register_location_type( $class_name ) {
$store = acf_get_store( 'location-types' );
// Check class exists.
if ( ! class_exists( $class_name ) ) {
$message = sprintf( __( 'Class "%s" does not exist.', 'acf' ), $class_name );
_doing_it_wrong( __FUNCTION__, $message, '5.9.0' );
return false;
}
// Create instance.
$location_type = new $class_name();
$name = $location_type->name;
// Check location type is unique.
if ( $store->has( $name ) ) {
$message = sprintf( __( 'Location type "%s" is already registered.', 'acf' ), $name );
_doing_it_wrong( __FUNCTION__, $message, '5.9.0' );
return false;
}
// Add to store.
$store->set( $name, $location_type );
/**
* Fires after a location type is registered.
*
* @date 8/4/20
* @since 5.9.0
*
* @param string $name The location type name.
* @param ACF_Location $location_type The location type instance.
*/
do_action( 'acf/registered_location_type', $name, $location_type );
// Return location type instance.
return $location_type;
}
/**
* Returns an array of all registered location types.
*
* @date 8/4/20
* @since 5.9.0
*
* @param void
* @return array
*/
function acf_get_location_types() {
return acf_get_store( 'location-types' )->get();
}
/**
* Returns a location type for the given name.
*
* @date 18/2/19
* @since 5.7.12
*
* @param string $name The location type name.
* @return (ACF_Location|null)
*/
function acf_get_location_type( $name ) {
return acf_get_store( 'location-types' )->get( $name );
}
/**
* Returns a grouped array of all location rule types.
*
* @date 8/4/20
* @since 5.9.0
*
* @param void
* @return array
*/
function acf_get_location_rule_types() {
$types = array();
// Default categories.
$categories = array(
'post' => __( 'Post', 'acf' ),
'page' => __( 'Page', 'acf' ),
'user' => __( 'User', 'acf' ),
'forms' => __( 'Forms', 'acf' ),
);
// Loop over all location types and append to $type.
$location_types = acf_get_location_types();
foreach ( $location_types as $location_type ) {
// Ignore if not public.
if ( ! $location_type->public ) {
continue;
}
// Find category label from category name.
$category = $location_type->category;
if ( isset( $categories[ $category ] ) ) {
$category = $categories[ $category ];
}
// Append
$types[ $category ][ $location_type->name ] = esc_html( $location_type->label );
}
/**
* Filters the location rule types.
*
* @date 8/4/20
* @since 5.9.0
*
* @param array $types The location rule types.
*/
return apply_filters( 'acf/location/rule_types', $types );
}
/**
* Returns a validated location rule with all props.
*
* @date 8/4/20
* @since 5.9.0
*
* @param array $rule The location rule.
* @return array
*/
function acf_validate_location_rule( $rule = array() ) {
// Apply defaults.
$rule = wp_parse_args(
$rule,
array(
'id' => '',
'group' => '',
'param' => '',
'operator' => '==',
'value' => '',
)
);
/**
* Filters the location rule to ensure is valid.
*
* @date 8/4/20
* @since 5.9.0
*
* @param array $rule The location rule.
*/
$rule = apply_filters( "acf/location/validate_rule/type={$rule['param']}", $rule );
$rule = apply_filters( 'acf/location/validate_rule', $rule );
return $rule;
}
/**
* Returns an array of operators for a given rule.
*
* @date 30/5/17
* @since 5.6.0
*
* @param array $rule The location rule.
* @return array
*/
function acf_get_location_rule_operators( $rule ) {
$operators = ACF_Location::get_operators( $rule );
// Get operators from location type since 5.9.
$location_type = acf_get_location_type( $rule['param'] );
if ( $location_type ) {
$operators = $location_type->get_operators( $rule );
}
/**
* Filters the location rule operators.
*
* @date 30/5/17
* @since 5.6.0
*
* @param array $types The location rule operators.
*/
$operators = apply_filters( "acf/location/rule_operators/type={$rule['param']}", $operators, $rule );
$operators = apply_filters( "acf/location/rule_operators/{$rule['param']}", $operators, $rule );
$operators = apply_filters( 'acf/location/rule_operators', $operators, $rule );
return $operators;
}
/**
* Returns an array of values for a given rule.
*
* @date 30/5/17
* @since 5.6.0
*
* @param array $rule The location rule.
* @return array
*/
function acf_get_location_rule_values( $rule ) {
$values = array();
// Get values from location type since 5.9.
$location_type = acf_get_location_type( $rule['param'] );
if ( $location_type ) {
$values = $location_type->get_values( $rule );
}
/**
* Filters the location rule values.
*
* @date 30/5/17
* @since 5.6.0
*
* @param array $types The location rule values.
*/
$values = apply_filters( "acf/location/rule_values/type={$rule['param']}", $values, $rule );
$values = apply_filters( "acf/location/rule_values/{$rule['param']}", $values, $rule );
$values = apply_filters( 'acf/location/rule_values', $values, $rule );
return $values;
}
/**
* Returns true if the provided rule matches the screen args.
*
* @date 30/5/17
* @since 5.6.0
*
* @param array $rule The location rule.
* @param array $screen The screen args.
* @param array $field The field group array.
* @return bool
*/
function acf_match_location_rule( $rule, $screen, $field_group ) {
$result = false;
// Get result from location type since 5.9.
$location_type = acf_get_location_type( $rule['param'] );
if ( $location_type ) {
$result = $location_type->match( $rule, $screen, $field_group );
}
/**
* Filters the result.
*
* @date 30/5/17
* @since 5.6.0
*
* @param bool $result The match result.
* @param array $rule The location rule.
* @param array $screen The screen args.
* @param array $field_group The field group array.
*/
$result = apply_filters( "acf/location/match_rule/type={$rule['param']}", $result, $rule, $screen, $field_group );
$result = apply_filters( 'acf/location/match_rule', $result, $rule, $screen, $field_group );
$result = apply_filters( "acf/location/rule_match/{$rule['param']}", $result, $rule, $screen, $field_group );
$result = apply_filters( 'acf/location/rule_match', $result, $rule, $screen, $field_group );
return $result;
}
/**
* Returns ann array of screen args to be used against matching rules.
*
* @date 8/4/20
* @since 5.9.0
*
* @param array $screen The screen args.
* @param array $deprecated The field group array.
* @return array
*/
function acf_get_location_screen( $screen = array(), $deprecated = false ) {
// Apply defaults.
$screen = wp_parse_args(
$screen,
array(
'lang' => acf_get_setting( 'current_language' ),
'ajax' => false,
)
);
/**
* Filters the result.
*
* @date 30/5/17
* @since 5.6.0
*
* @param array $screen The screen args.
* @param array $deprecated The field group array.
*/
return apply_filters( 'acf/location/screen', $screen, $deprecated );
}
/**
* Alias of acf_register_location_type().
*
* @date 31/5/17
* @since 5.6.0
*
* @param string $class_name The location class name.
* @return (ACF_Location|false)
*/
function acf_register_location_rule( $class_name ) {
return acf_register_location_type( $class_name );
}
/**
* Alias of acf_get_location_type().
*
* @date 31/5/17
* @since 5.6.0
*
* @param string $class_name The location class name.
* @return (ACF_Location|false)
*/
function acf_get_location_rule( $name ) {
return acf_get_location_type( $name );
}
/**
* Alias of acf_validate_location_rule().
*
* @date 30/5/17
* @since 5.6.0
*
* @param array $rule The location rule.
* @return array
*/
function acf_get_valid_location_rule( $rule ) {
return acf_validate_location_rule( $rule );
}
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'ACF_Legacy_Location' ) ) :
abstract class ACF_Legacy_Location {
/**
* Constructor.
*
* @date 5/03/2014
* @since 5.0.0
*
* @param void
* @return void
*/
public function __construct() {
// Add legacy method filters.
if ( method_exists( $this, 'rule_match' ) ) {
add_filter( "acf/location/rule_match/{$this->name}", array( $this, 'rule_match' ), 5, 3 );
}
if ( method_exists( $this, 'rule_operators' ) ) {
add_filter( "acf/location/rule_operators/{$this->name}", array( $this, 'rule_operators' ), 5, 2 );
}
if ( method_exists( $this, 'rule_values' ) ) {
add_filter( "acf/location/rule_values/{$this->name}", array( $this, 'rule_values' ), 5, 2 );
}
}
/**
* Magic __call method for backwards compatibility.
*
* @date 10/4/20
* @since 5.9.0
*
* @param string $name The method name.
* @param array $arguments The array of arguments.
* @return mixed
*/
public function __call( $name, $arguments ) {
// Add backwards compatibility for legacy methods.
// - Combine 3x legacy filters cases together (remove first args).
switch ( $name ) {
case 'rule_match':
$method = isset( $method ) ? $method : 'match';
$arguments[3] = isset( $arguments[3] ) ? $arguments[3] : false; // Add $field_group param.
case 'rule_operators':
$method = isset( $method ) ? $method : 'get_operators';
case 'rule_values':
$method = isset( $method ) ? $method : 'get_values';
array_shift( $arguments );
return call_user_func_array( array( $this, $method ), $arguments );
case 'compare':
return call_user_func_array( array( $this, 'compare_to_rule' ), $arguments );
}
}
}
endif; // class_exists check
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'ACF_Location' ) ) :
abstract class ACF_Location extends ACF_Legacy_Location {
/**
* The location rule name.
*
* @since 5.9.0
* @var string
*/
public $name = '';
/**
* The location rule label.
*
* @since 5.9.0
* @var string
*/
public $label = '';
/**
* The location rule category.
*
* Accepts "post", "page", "user", "forms" or a custom label.
*
* @since 5.9.0
* @var string
*/
public $category = 'post';
/**
* Whether or not the location rule is publicly accessible.
*
* @since 5.0.0
* @var bool
*/
public $public = true;
/**
* The object type related to this location rule.
*
* Accepts an object type discoverable by `acf_get_object_type()`.
*
* @since 5.9.0
* @var string
*/
public $object_type = '';
/**
* The object subtype related to this location rule.
*
* Accepts a custom post type or custom taxonomy.
*
* @since 5.9.0
* @var string
*/
public $object_subtype = '';
/**
* Constructor.
*
* @date 8/4/20
* @since 5.9.0
*
* @param void
* @return void
*/
public function __construct() {
$this->initialize();
// Call legacy constructor.
parent::__construct();
}
/**
* Initializes props.
*
* @date 5/03/2014
* @since 5.0.0
*
* @param void
* @return void
*/
public function initialize() {
// Set props here.
}
/**
* Returns an array of operators for this location.
*
* @date 9/4/20
* @since 5.9.0
*
* @param array $rule A location rule.
* @return array
*/
public static function get_operators( $rule ) {
return array(
'==' => __( 'is equal to', 'acf' ),
'!=' => __( 'is not equal to', 'acf' ),
);
}
/**
* Returns an array of possible values for this location.
*
* @date 9/4/20
* @since 5.9.0
*
* @param array $rule A location rule.
* @return array
*/
public function get_values( $rule ) {
return array();
}
/**
* Returns the object_type connected to this location.
*
* @date 1/4/20
* @since 5.9.0
*
* @param array $rule A location rule.
* @return string
*/
public function get_object_type( $rule ) {
return $this->object_type;
}
/**
* Returns the object_subtype connected to this location.
*
* @date 1/4/20
* @since 5.9.0
*
* @param array $rule A location rule.
* @return string|array
*/
public function get_object_subtype( $rule ) {
return $this->object_subtype;
}
/**
* Matches the provided rule against the screen args returning a bool result.
*
* @date 9/4/20
* @since 5.9.0
*
* @param array $rule The location rule.
* @param array $screen The screen args.
* @param array $field_group The field group settings.
* @return bool
*/
public function match( $rule, $screen, $field_group ) {
return false;
}
/**
* Compares the given value and rule params returning true when they match.
*
* @date 17/9/19
* @since 5.8.1
*
* @param array $rule The location rule data.
* @param mixed $value The value to compare against.
* @return bool
*/
public function compare_to_rule( $value, $rule ) {
$result = ( $value == $rule['value'] );
// Allow "all" to match any value.
if ( $rule['value'] === 'all' ) {
$result = true;
}
// Reverse result for "!=" operator.
if ( $rule['operator'] === '!=' ) {
return ! $result;
}
return $result;
}
}
endif; // class_exists check