initial commit
This commit is contained in:
@ -0,0 +1,236 @@
|
||||
<?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;
|
||||
|
||||
?>
|
@ -0,0 +1,342 @@
|
||||
<?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;
|
||||
|
||||
?>
|
@ -0,0 +1,471 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'acf_form_customizer' ) ) :
|
||||
|
||||
#[AllowDynamicProperties]
|
||||
class acf_form_customizer {
|
||||
|
||||
|
||||
/*
|
||||
* __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_fields = array();
|
||||
$this->preview_errors = array();
|
||||
|
||||
// actions
|
||||
add_action( 'customize_controls_init', array( $this, 'customize_controls_init' ) );
|
||||
add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ), 1, 1 );
|
||||
add_action( 'customize_save', array( $this, 'customize_save' ), 1, 1 );
|
||||
|
||||
// save
|
||||
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 customize_controls_init() {
|
||||
|
||||
// load acf scripts
|
||||
acf_enqueue_scripts(
|
||||
array(
|
||||
'context' => 'customize_controls',
|
||||
)
|
||||
);
|
||||
|
||||
// actions
|
||||
add_action( 'acf/input/admin_footer', array( $this, 'admin_footer' ), 1 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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 ) {
|
||||
|
||||
// bail early if not valid (customize + acf values + nonce)
|
||||
if ( ! isset( $_POST['wp_customize'] ) || ! isset( $new_instance['acf'] ) || ! acf_verify_nonce( 'widget' ) ) {
|
||||
return $instance;
|
||||
}
|
||||
|
||||
// vars
|
||||
$data = array(
|
||||
'post_id' => "widget_{$widget->id}",
|
||||
'values' => array(),
|
||||
'fields' => array(),
|
||||
);
|
||||
|
||||
// append values
|
||||
$data['values'] = $new_instance['acf'];
|
||||
|
||||
// append fields (name => key relationship) - used later in 'acf/get_field_reference' for customizer previews
|
||||
foreach ( $data['values'] as $k => $v ) {
|
||||
|
||||
// get field
|
||||
$field = acf_get_field( $k );
|
||||
|
||||
// continue if no field
|
||||
if ( ! $field ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// update
|
||||
$data['fields'][ $field['name'] ] = $field['key'];
|
||||
|
||||
}
|
||||
|
||||
// append data to instance
|
||||
$instance['acf'] = $data;
|
||||
|
||||
// return
|
||||
return $instance;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* settings
|
||||
*
|
||||
* This function will return an array of cutomizer settings that include ACF data
|
||||
* similar to `$customizer->settings();`
|
||||
*
|
||||
* @type function
|
||||
* @date 22/03/2016
|
||||
* @since 5.3.2
|
||||
*
|
||||
* @param $customizer (object)
|
||||
* @return $value (mixed)
|
||||
*/
|
||||
|
||||
function settings( $customizer ) {
|
||||
|
||||
// vars
|
||||
$data = array();
|
||||
$settings = $customizer->settings();
|
||||
|
||||
// bail early if no settings
|
||||
if ( empty( $settings ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// loop over settings
|
||||
foreach ( $settings as $setting ) {
|
||||
|
||||
// vars
|
||||
$id = $setting->id;
|
||||
|
||||
// verify settings type
|
||||
if ( substr( $id, 0, 6 ) == 'widget' || substr( $id, 0, 7 ) == 'nav_menu' ) {
|
||||
// allow
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
// get value
|
||||
$value = $setting->post_value();
|
||||
|
||||
// bail early if no acf
|
||||
if ( ! is_array( $value ) || ! isset( $value['acf'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// set data
|
||||
$setting->acf = $value['acf'];
|
||||
|
||||
// append
|
||||
$data[] = $setting;
|
||||
|
||||
}
|
||||
|
||||
// bail early if no settings
|
||||
if ( empty( $data ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// return
|
||||
return $data;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* customize_preview_init
|
||||
*
|
||||
* This function is called when customizer preview is initialized
|
||||
*
|
||||
* @type function
|
||||
* @date 22/03/2016
|
||||
* @since 5.3.2
|
||||
*
|
||||
* @param $customizer (object)
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function customize_preview_init( $customizer ) {
|
||||
|
||||
// get customizer settings (widgets)
|
||||
$settings = $this->settings( $customizer );
|
||||
|
||||
// bail early if no settings
|
||||
if ( empty( $settings ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// append values
|
||||
foreach ( $settings as $setting ) {
|
||||
|
||||
// get acf data
|
||||
$data = $setting->acf;
|
||||
|
||||
// append acf_value to preview_values
|
||||
$this->preview_values[ $data['post_id'] ] = $data['values'];
|
||||
$this->preview_fields[ $data['post_id'] ] = $data['fields'];
|
||||
|
||||
}
|
||||
|
||||
// bail early if no preview_values
|
||||
if ( empty( $this->preview_values ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// add filters
|
||||
add_filter( 'acf/pre_load_value', array( $this, 'pre_load_value' ), 10, 3 );
|
||||
add_filter( 'acf/pre_load_reference', array( $this, 'pre_load_reference' ), 10, 3 );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* pre_load_value
|
||||
*
|
||||
* Used to inject preview value
|
||||
*
|
||||
* @date 2/2/18
|
||||
* @since 5.6.5
|
||||
*
|
||||
* @param type $var Description. Default.
|
||||
* @return type Description.
|
||||
*/
|
||||
|
||||
function pre_load_value( $value, $post_id, $field ) {
|
||||
|
||||
// check
|
||||
if ( isset( $this->preview_values[ $post_id ][ $field['key'] ] ) ) {
|
||||
return $this->preview_values[ $post_id ][ $field['key'] ];
|
||||
}
|
||||
|
||||
// return
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* pre_load_reference
|
||||
*
|
||||
* Used to inject preview value
|
||||
*
|
||||
* @date 2/2/18
|
||||
* @since 5.6.5
|
||||
*
|
||||
* @param type $var Description. Default.
|
||||
* @return type Description.
|
||||
*/
|
||||
|
||||
function pre_load_reference( $field_key, $field_name, $post_id ) {
|
||||
|
||||
// check
|
||||
if ( isset( $this->preview_fields[ $post_id ][ $field_name ] ) ) {
|
||||
return $this->preview_fields[ $post_id ][ $field_name ];
|
||||
}
|
||||
|
||||
// return
|
||||
return $field_key;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* customize_save
|
||||
*
|
||||
* This function is called when customizer saves a widget.
|
||||
* Normally, the widget_update_callback filter would be used, but the customizer disables this and runs a custom action
|
||||
* class-customizer-settings.php will save the widget data via the function set_root_value which uses update_option
|
||||
*
|
||||
* @type function
|
||||
* @date 22/03/2016
|
||||
* @since 5.3.2
|
||||
*
|
||||
* @param $customizer (object)
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function customize_save( $customizer ) {
|
||||
|
||||
// get customizer settings (widgets)
|
||||
$settings = $this->settings( $customizer );
|
||||
|
||||
// bail early if no settings
|
||||
if ( empty( $settings ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// append values
|
||||
foreach ( $settings as $setting ) {
|
||||
|
||||
// get acf data
|
||||
$data = $setting->acf;
|
||||
|
||||
// save acf data
|
||||
acf_save_post( $data['post_id'], $data['values'] );
|
||||
|
||||
// remove [acf] data from saved widget array
|
||||
$id_data = $setting->id_data();
|
||||
add_filter( 'pre_update_option_' . $id_data['base'], array( $this, 'pre_update_option' ), 10, 3 );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* pre_update_option
|
||||
*
|
||||
* this function will remove the [acf] data from widget insance
|
||||
*
|
||||
* @type function
|
||||
* @date 22/03/2016
|
||||
* @since 5.3.2
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function pre_update_option( $value, $option, $old_value ) {
|
||||
|
||||
// bail early if no value
|
||||
if ( empty( $value ) ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
// loop over widgets
|
||||
// WP saves all widgets (of the same type) as an array of widgets
|
||||
foreach ( $value as $i => $widget ) {
|
||||
|
||||
// bail early if no acf
|
||||
if ( ! isset( $widget['acf'] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// remove widget
|
||||
unset( $value[ $i ]['acf'] );
|
||||
|
||||
}
|
||||
|
||||
// return
|
||||
return $value;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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($) {
|
||||
|
||||
// customizer saves widget on any input change, so unload is not needed
|
||||
acf.unload.active = 0;
|
||||
|
||||
|
||||
// hack customizer function to remove bug caused by WYSIWYG field using aunique ID
|
||||
// customizer compares returned AJAX HTML with the HTML of the widget form.
|
||||
// the _getInputsSignature() function is used to generate a string based of input name + id.
|
||||
// because ACF generates a unique ID on the WYSIWYG field, this string will not match causing the preview function to bail.
|
||||
// an attempt was made to remove the WYSIWYG unique ID, but this caused multiple issues in the wp-admin and altimately doesn't make sense with the tinymce rule that all editors must have a unique ID.
|
||||
// source: wp-admin/js/customize-widgets.js
|
||||
|
||||
// vars
|
||||
var WidgetControl = wp.customize.Widgets.WidgetControl.prototype;
|
||||
|
||||
|
||||
// backup functions
|
||||
WidgetControl.__getInputsSignature = WidgetControl._getInputsSignature;
|
||||
WidgetControl.__setInputState = WidgetControl._setInputState;
|
||||
|
||||
|
||||
// modify __getInputsSignature
|
||||
WidgetControl._getInputsSignature = function( inputs ) {
|
||||
|
||||
// vars
|
||||
var signature = this.__getInputsSignature( inputs );
|
||||
safe = [];
|
||||
|
||||
|
||||
// split
|
||||
signature = signature.split(';');
|
||||
|
||||
|
||||
// loop
|
||||
for( var i in signature ) {
|
||||
|
||||
// vars
|
||||
var bit = signature[i];
|
||||
|
||||
|
||||
// bail early if acf is found
|
||||
if( bit.indexOf('acf') !== -1 ) continue;
|
||||
|
||||
|
||||
// append
|
||||
safe.push( bit );
|
||||
|
||||
}
|
||||
|
||||
|
||||
// update
|
||||
signature = safe.join(';');
|
||||
|
||||
|
||||
// return
|
||||
return signature;
|
||||
|
||||
};
|
||||
|
||||
|
||||
// modify _setInputState
|
||||
// this function deosn't seem to run on widget title/content, only custom fields
|
||||
// either way, this function is not needed and will break ACF fields
|
||||
WidgetControl._setInputState = function( input, state ) {
|
||||
|
||||
return true;
|
||||
|
||||
};
|
||||
|
||||
})(jQuery);
|
||||
</script>
|
||||
<?php
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new acf_form_customizer();
|
||||
|
||||
endif;
|
||||
|
||||
?>
|
@ -0,0 +1,643 @@
|
||||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'acf_form_front' ) ) :
|
||||
#[AllowDynamicProperties]
|
||||
class acf_form_front {
|
||||
|
||||
/** @var array An array of registered form settings */
|
||||
private $forms = array();
|
||||
|
||||
/** @var array An array of default fields */
|
||||
public $fields = array();
|
||||
|
||||
|
||||
/*
|
||||
* __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->fields = array(
|
||||
|
||||
'_post_title' => array(
|
||||
'prefix' => 'acf',
|
||||
'name' => '_post_title',
|
||||
'key' => '_post_title',
|
||||
'label' => __( 'Title', 'acf' ),
|
||||
'type' => 'text',
|
||||
'required' => true,
|
||||
),
|
||||
|
||||
'_post_content' => array(
|
||||
'prefix' => 'acf',
|
||||
'name' => '_post_content',
|
||||
'key' => '_post_content',
|
||||
'label' => __( 'Content', 'acf' ),
|
||||
'type' => 'wysiwyg',
|
||||
),
|
||||
|
||||
'_validate_email' => array(
|
||||
'prefix' => 'acf',
|
||||
'name' => '_validate_email',
|
||||
'key' => '_validate_email',
|
||||
'label' => __( 'Validate Email', 'acf' ),
|
||||
'type' => 'text',
|
||||
'value' => '',
|
||||
'wrapper' => array( 'style' => 'display:none !important;' ),
|
||||
),
|
||||
|
||||
);
|
||||
|
||||
// actions
|
||||
add_action( 'acf/validate_save_post', array( $this, 'validate_save_post' ), 1 );
|
||||
|
||||
// filters
|
||||
add_filter( 'acf/pre_save_post', array( $this, 'pre_save_post' ), 5, 2 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* validate_form
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @type function
|
||||
* @date 28/2/17
|
||||
* @since 5.5.8
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function validate_form( $args ) {
|
||||
|
||||
// defaults
|
||||
// Todo: Allow message and button text to be generated by CPT settings.
|
||||
$args = wp_parse_args(
|
||||
$args,
|
||||
array(
|
||||
'id' => 'acf-form',
|
||||
'post_id' => false,
|
||||
'new_post' => false,
|
||||
'field_groups' => false,
|
||||
'fields' => false,
|
||||
'post_title' => false,
|
||||
'post_content' => false,
|
||||
'form' => true,
|
||||
'form_attributes' => array(),
|
||||
'return' => add_query_arg( 'updated', 'true', acf_get_current_url() ),
|
||||
'html_before_fields' => '',
|
||||
'html_after_fields' => '',
|
||||
'submit_value' => __( 'Update', 'acf' ),
|
||||
'updated_message' => __( 'Post updated', 'acf' ),
|
||||
'label_placement' => 'top',
|
||||
'instruction_placement' => 'label',
|
||||
'field_el' => 'div',
|
||||
'uploader' => 'wp',
|
||||
'honeypot' => true,
|
||||
'html_updated_message' => '<div id="message" class="updated"><p>%s</p></div>', // 5.5.10
|
||||
'html_submit_button' => '<input type="submit" class="acf-button button button-primary button-large" value="%s" />', // 5.5.10
|
||||
'html_submit_spinner' => '<span class="acf-spinner"></span>', // 5.5.10
|
||||
'kses' => true, // 5.6.5
|
||||
)
|
||||
);
|
||||
|
||||
$args['form_attributes'] = wp_parse_args(
|
||||
$args['form_attributes'],
|
||||
array(
|
||||
'id' => $args['id'],
|
||||
'class' => 'acf-form',
|
||||
'action' => '',
|
||||
'method' => 'post',
|
||||
)
|
||||
);
|
||||
|
||||
// filter post_id
|
||||
$args['post_id'] = acf_get_valid_post_id( $args['post_id'] );
|
||||
|
||||
// new post?
|
||||
if ( $args['post_id'] === 'new_post' ) {
|
||||
|
||||
$args['new_post'] = wp_parse_args(
|
||||
$args['new_post'],
|
||||
array(
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'draft',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
// filter
|
||||
$args = apply_filters( 'acf/validate_form', $args );
|
||||
|
||||
// return
|
||||
return $args;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* add_form
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @type function
|
||||
* @date 28/2/17
|
||||
* @since 5.5.8
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function add_form( $args = array() ) {
|
||||
|
||||
// validate
|
||||
$args = $this->validate_form( $args );
|
||||
|
||||
// append
|
||||
$this->forms[ $args['id'] ] = $args;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* get_form
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @type function
|
||||
* @date 28/2/17
|
||||
* @since 5.5.8
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function get_form( $id = '' ) {
|
||||
|
||||
// bail early if not set
|
||||
if ( ! isset( $this->forms[ $id ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// return
|
||||
return $this->forms[ $id ];
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* validate_save_post
|
||||
*
|
||||
* This function will validate fields from the above array
|
||||
*
|
||||
* @type function
|
||||
* @date 7/09/2016
|
||||
* @since 5.4.0
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function validate_save_post() {
|
||||
|
||||
// register field if isset in $_POST
|
||||
foreach ( $this->fields as $k => $field ) {
|
||||
|
||||
// bail early if no in $_POST
|
||||
if ( ! isset( $_POST['acf'][ $k ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
|
||||
continue;
|
||||
}
|
||||
|
||||
// register
|
||||
acf_add_local_field( $field );
|
||||
|
||||
}
|
||||
|
||||
// honeypot
|
||||
if ( ! empty( $_POST['acf']['_validate_email'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Data not used; presence indicates spam.
|
||||
|
||||
acf_add_validation_error( '', __( 'Spam Detected', 'acf' ) );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* pre_save_post
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @type function
|
||||
* @date 7/09/2016
|
||||
* @since 5.4.0
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function pre_save_post( $post_id, $form ) {
|
||||
|
||||
// vars
|
||||
$save = array(
|
||||
'ID' => 0,
|
||||
);
|
||||
|
||||
// determine save data
|
||||
if ( is_numeric( $post_id ) ) {
|
||||
|
||||
// update post
|
||||
$save['ID'] = $post_id;
|
||||
|
||||
} elseif ( $post_id == 'new_post' ) {
|
||||
|
||||
// merge in new post data
|
||||
$save = array_merge( $save, $form['new_post'] );
|
||||
|
||||
} else {
|
||||
|
||||
// not post
|
||||
return $post_id;
|
||||
|
||||
}
|
||||
|
||||
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified in check_submit_form().
|
||||
// save post_title
|
||||
if ( isset( $_POST['acf']['_post_title'] ) ) {
|
||||
|
||||
$save['post_title'] = acf_extract_var( $_POST['acf'], '_post_title' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized by WP when saved.
|
||||
|
||||
}
|
||||
|
||||
// save post_content
|
||||
if ( isset( $_POST['acf']['_post_content'] ) ) {
|
||||
|
||||
$save['post_content'] = acf_extract_var( $_POST['acf'], '_post_content' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized by WP when saved.
|
||||
|
||||
}
|
||||
// phpcs:enable WordPress.Security.NonceVerification.Missing
|
||||
|
||||
// honeypot
|
||||
if ( ! empty( $_POST['acf']['_validate_email'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Data not used; presence indicates spam.
|
||||
return false;
|
||||
}
|
||||
|
||||
// validate
|
||||
if ( count( $save ) == 1 ) {
|
||||
|
||||
return $post_id;
|
||||
|
||||
}
|
||||
|
||||
// save
|
||||
if ( $save['ID'] ) {
|
||||
|
||||
wp_update_post( $save );
|
||||
|
||||
} else {
|
||||
|
||||
$post_id = wp_insert_post( $save );
|
||||
|
||||
}
|
||||
|
||||
// return
|
||||
return $post_id;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* enqueue
|
||||
*
|
||||
* This function will enqueue a form
|
||||
*
|
||||
* @type function
|
||||
* @date 7/09/2016
|
||||
* @since 5.4.0
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function enqueue_form() {
|
||||
|
||||
// check
|
||||
$this->check_submit_form();
|
||||
|
||||
// load acf scripts
|
||||
acf_enqueue_scripts();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* check_submit_form
|
||||
*
|
||||
* This function will maybe submit form data
|
||||
*
|
||||
* @type function
|
||||
* @date 3/3/17
|
||||
* @since 5.5.10
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function check_submit_form() {
|
||||
|
||||
// Verify nonce.
|
||||
if ( ! acf_verify_nonce( 'acf_form' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Confirm form was submit.
|
||||
if ( ! isset( $_POST['_acf_form'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load registered form using id.
|
||||
$form = $this->get_form( acf_sanitize_request_args( $_POST['_acf_form'] ) );
|
||||
|
||||
// Fallback to encrypted JSON.
|
||||
if ( ! $form ) {
|
||||
$form = json_decode( acf_decrypt( sanitize_text_field( $_POST['_acf_form'] ) ), true );
|
||||
if ( ! $form ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Run kses on all $_POST data.
|
||||
if ( $form['kses'] && isset( $_POST['acf'] ) ) {
|
||||
$_POST['acf'] = wp_kses_post_deep( $_POST['acf'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- False positive.
|
||||
}
|
||||
|
||||
// Validate data and show errors.
|
||||
// Todo: Return WP_Error and show above form, keeping input values.
|
||||
acf_validate_save_post( true );
|
||||
|
||||
// Submit form.
|
||||
$this->submit_form( $form );
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* submit_form
|
||||
*
|
||||
* This function will submit form data
|
||||
*
|
||||
* @type function
|
||||
* @date 3/3/17
|
||||
* @since 5.5.10
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function submit_form( $form ) {
|
||||
|
||||
// filter
|
||||
$form = apply_filters( 'acf/pre_submit_form', $form );
|
||||
|
||||
// vars
|
||||
$post_id = acf_maybe_get( $form, 'post_id', 0 );
|
||||
|
||||
// add global for backwards compatibility
|
||||
$GLOBALS['acf_form'] = $form;
|
||||
|
||||
// allow for custom save
|
||||
$post_id = apply_filters( 'acf/pre_save_post', $post_id, $form );
|
||||
|
||||
// save
|
||||
acf_save_post( $post_id );
|
||||
|
||||
// restore form (potentially modified)
|
||||
$form = $GLOBALS['acf_form'];
|
||||
|
||||
// action
|
||||
do_action( 'acf/submit_form', $form, $post_id );
|
||||
|
||||
// vars
|
||||
$return = acf_maybe_get( $form, 'return', '' );
|
||||
|
||||
// redirect
|
||||
if ( $return ) {
|
||||
|
||||
// update %placeholders%
|
||||
$return = str_replace( '%post_id%', $post_id, $return );
|
||||
$return = str_replace( '%post_url%', get_permalink( $post_id ), $return );
|
||||
|
||||
// redirect
|
||||
wp_redirect( $return );
|
||||
exit;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* render
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @type function
|
||||
* @date 7/09/2016
|
||||
* @since 5.4.0
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function render_form( $args = array() ) {
|
||||
|
||||
// Vars.
|
||||
$is_registered = false;
|
||||
$field_groups = array();
|
||||
$fields = array();
|
||||
|
||||
// Allow form settings to be directly provided.
|
||||
if ( is_array( $args ) ) {
|
||||
$args = $this->validate_form( $args );
|
||||
|
||||
// Otherwise, lookup registered form.
|
||||
} else {
|
||||
$is_registered = true;
|
||||
$args = $this->get_form( $args );
|
||||
if ( ! $args ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Extract vars.
|
||||
$post_id = $args['post_id'];
|
||||
|
||||
// Prevent ACF from loading values for "new_post".
|
||||
if ( $post_id === 'new_post' ) {
|
||||
$post_id = false;
|
||||
}
|
||||
|
||||
// Set uploader type.
|
||||
acf_update_setting( 'uploader', $args['uploader'] );
|
||||
|
||||
// Register local fields.
|
||||
foreach ( $this->fields as $k => $field ) {
|
||||
acf_add_local_field( $field );
|
||||
}
|
||||
|
||||
// Append post_title field.
|
||||
if ( $args['post_title'] ) {
|
||||
$_post_title = acf_get_field( '_post_title' );
|
||||
$_post_title['value'] = $post_id ? get_post_field( 'post_title', $post_id ) : '';
|
||||
$fields[] = $_post_title;
|
||||
}
|
||||
|
||||
// Append post_content field.
|
||||
if ( $args['post_content'] ) {
|
||||
$_post_content = acf_get_field( '_post_content' );
|
||||
$_post_content['value'] = $post_id ? get_post_field( 'post_content', $post_id ) : '';
|
||||
$fields[] = $_post_content;
|
||||
}
|
||||
|
||||
// Load specific fields.
|
||||
if ( $args['fields'] ) {
|
||||
|
||||
// Lookup fields using $strict = false for better compatibility with field names.
|
||||
foreach ( $args['fields'] as $selector ) {
|
||||
$fields[] = acf_maybe_get_field( $selector, $post_id, false );
|
||||
}
|
||||
|
||||
// Load specific field groups.
|
||||
} elseif ( $args['field_groups'] ) {
|
||||
foreach ( $args['field_groups'] as $selector ) {
|
||||
$field_groups[] = acf_get_field_group( $selector );
|
||||
}
|
||||
|
||||
// Load fields for the given "new_post" args.
|
||||
} elseif ( $args['post_id'] == 'new_post' ) {
|
||||
$field_groups = acf_get_field_groups( $args['new_post'] );
|
||||
|
||||
// Load fields for the given "post_id" arg.
|
||||
} else {
|
||||
$field_groups = acf_get_field_groups(
|
||||
array(
|
||||
'post_id' => $args['post_id'],
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// load fields from the found field groups.
|
||||
if ( $field_groups ) {
|
||||
foreach ( $field_groups as $field_group ) {
|
||||
$_fields = acf_get_fields( $field_group );
|
||||
if ( $_fields ) {
|
||||
foreach ( $_fields as $_field ) {
|
||||
$fields[] = $_field;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add honeypot field.
|
||||
if ( $args['honeypot'] ) {
|
||||
$fields[] = acf_get_field( '_validate_email' );
|
||||
}
|
||||
|
||||
// Display updated_message
|
||||
if ( ! empty( $_GET['updated'] ) && $args['updated_message'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Used as a flag; data not used.
|
||||
printf( $args['html_updated_message'], $args['updated_message'] );
|
||||
}
|
||||
|
||||
// display form
|
||||
if ( $args['form'] ) : ?>
|
||||
<form <?php echo acf_esc_attrs( $args['form_attributes'] ); ?>>
|
||||
<?php
|
||||
endif;
|
||||
|
||||
// Render hidde form data.
|
||||
acf_form_data(
|
||||
array(
|
||||
'screen' => 'acf_form',
|
||||
'post_id' => $args['post_id'],
|
||||
'form' => $is_registered ? $args['id'] : acf_encrypt( json_encode( $args ) ),
|
||||
)
|
||||
);
|
||||
|
||||
?>
|
||||
<div class="acf-fields acf-form-fields -<?php echo esc_attr( $args['label_placement'] ); ?>">
|
||||
<?php echo $args['html_before_fields']; ?>
|
||||
<?php acf_render_fields( $fields, $post_id, $args['field_el'], $args['instruction_placement'] ); ?>
|
||||
<?php echo $args['html_after_fields']; ?>
|
||||
</div>
|
||||
<?php if ( $args['form'] ) : ?>
|
||||
<div class="acf-form-submit">
|
||||
<?php printf( $args['html_submit_button'], $args['submit_value'] ); ?>
|
||||
<?php echo $args['html_submit_spinner']; ?>
|
||||
</div>
|
||||
</form>
|
||||
<?php endif;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// initialize
|
||||
acf()->form_front = new acf_form_front();
|
||||
|
||||
endif; // class_exists check
|
||||
|
||||
|
||||
/*
|
||||
* Functions
|
||||
*
|
||||
* alias of acf()->form->functions
|
||||
*
|
||||
* @type function
|
||||
* @date 11/06/2014
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
|
||||
function acf_form_head() {
|
||||
|
||||
acf()->form_front->enqueue_form();
|
||||
|
||||
}
|
||||
|
||||
function acf_form( $args = array() ) {
|
||||
|
||||
acf()->form_front->render_form( $args );
|
||||
|
||||
}
|
||||
|
||||
function acf_get_form( $id = '' ) {
|
||||
|
||||
return acf()->form_front->get_form( $id );
|
||||
|
||||
}
|
||||
|
||||
function acf_register_form( $args ) {
|
||||
|
||||
acf()->form_front->add_form( $args );
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,190 @@
|
||||
<?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;
|
@ -0,0 +1,397 @@
|
||||
<?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;
|
||||
?>
|
@ -0,0 +1,345 @@
|
||||
<?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;
|
||||
|
||||
|
@ -0,0 +1,409 @@
|
||||
<?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;
|
||||
|
||||
|
||||
?>
|
@ -0,0 +1,389 @@
|
||||
<?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
|
||||
|
||||
?>
|
@ -0,0 +1,335 @@
|
||||
<?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;
|
||||
|
||||
?>
|
Reference in New Issue
Block a user