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() {
// bai lealry if not widget
if( !isset($_POST['_acf_widget_id']) ) return;
// vars
$id = $_POST['_acf_widget_id'];
$number = $_POST['_acf_widget_number'];
$prefix = $_POST['_acf_widget_prefix'];
// validate
acf_validate_values( $_POST[ $id ][ $number ]['acf'], $prefix );
}
/*
* 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(
'post_id' => $post_id,
'nonce' => 'widget',
'widget_id' => 'widget-' . $widget->id_base,
'widget_number' => $widget->number,
'widget_prefix' => $prefix
));
// 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
foreach( $fields as &$field ) $field['prefix'] = $prefix;
// render
acf_render_fields( $post_id, $fields, 'div', $field_group['instruction_placement'] );
}
// 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 ): ?>
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() {
?>