Added dependency plugins
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
<?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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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, $typenow, $wp_version;
|
||||
|
||||
|
||||
// validate page
|
||||
if( $pagenow === 'post.php' && $typenow === 'attachment' ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// validate page
|
||||
if( $pagenow === 'upload.php' && version_compare($wp_version, '4.0', '>=') ) {
|
||||
|
||||
add_action('admin_footer', array($this, 'admin_footer'), 0);
|
||||
|
||||
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() {
|
||||
|
||||
// bail early if not valid page
|
||||
if( !$this->validate_page() ) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// load acf scripts
|
||||
acf_enqueue_scripts();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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(
|
||||
'post_id' => 0,
|
||||
'nonce' => 'attachment',
|
||||
'ajax' => 1
|
||||
));
|
||||
|
||||
|
||||
?>
|
||||
<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 = $this->validate_page();
|
||||
$post_id = $post->ID;
|
||||
$el = 'tr';
|
||||
$args = array(
|
||||
'attachment' => $post_id
|
||||
);
|
||||
|
||||
|
||||
// get field groups
|
||||
$field_groups = acf_get_field_groups( $args );
|
||||
|
||||
|
||||
// render
|
||||
if( !empty($field_groups) ) {
|
||||
|
||||
// get acf_form_data
|
||||
ob_start();
|
||||
|
||||
|
||||
acf_form_data(array(
|
||||
'post_id' => $post_id,
|
||||
'nonce' => 'attachment',
|
||||
));
|
||||
|
||||
|
||||
// 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( $post_id, $fields, $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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// validate and save
|
||||
if( acf_validate_save_post(true) ) {
|
||||
|
||||
acf_save_post( $post['ID'] );
|
||||
|
||||
}
|
||||
|
||||
|
||||
// return
|
||||
return $post;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
new acf_form_attachment();
|
||||
|
||||
endif;
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,349 @@
|
||||
<?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(
|
||||
'post_id' => $post_id,
|
||||
'nonce' => 'comment'
|
||||
));
|
||||
|
||||
|
||||
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( $post_id, $fields, 'div', $field_group['instruction_placement'] ); ?>
|
||||
<script type="text/javascript">
|
||||
if( typeof acf !== 'undefined' ) {
|
||||
|
||||
acf.postbox.render(<?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;
|
||||
|
||||
|
||||
// ob
|
||||
ob_start();
|
||||
|
||||
// render post data
|
||||
acf_form_data(array(
|
||||
'post_id' => $post_id,
|
||||
'nonce' => 'comment'
|
||||
));
|
||||
|
||||
echo '<div class="acf-comment-fields acf-fields -clear">';
|
||||
|
||||
foreach( $field_groups as $field_group ) {
|
||||
|
||||
$fields = acf_get_fields( $field_group );
|
||||
|
||||
acf_render_fields( $post_id, $fields, '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'] );
|
||||
}
|
||||
|
||||
|
||||
// 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') ) :
|
||||
|
||||
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('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
|
||||
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 admin_enqueue_scripts() {
|
||||
|
||||
// validate screen
|
||||
if( !acf_is_screen('customize') ) return;
|
||||
|
||||
|
||||
// load acf scripts
|
||||
acf_enqueue_scripts();
|
||||
|
||||
|
||||
// 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 ealry 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 ealry 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 ealry 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 ealry 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 ealry if no preview_values
|
||||
if( empty($this->preview_values) ) return;
|
||||
|
||||
|
||||
// add filter
|
||||
add_filter('acf/get_field_reference', array($this, 'get_field_reference'), 10, 3);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* get_field_reference
|
||||
*
|
||||
* This function will return a field_key for a given field name + post_id
|
||||
* Normally, ACF would lookup the DB fro this connection, but a new preview widget has not yet saved anything to the DB
|
||||
*
|
||||
* @type function
|
||||
* @date 12/05/2016
|
||||
* @since 5.3.8
|
||||
*
|
||||
* @param $field_key (string)
|
||||
* @param $field_name (string)
|
||||
* @param $post_id (mixed)
|
||||
* @return $field_key
|
||||
*/
|
||||
|
||||
function get_field_reference( $field_key, $field_name, $post_id ) {
|
||||
|
||||
// look for reference
|
||||
if( isset($this->preview_fields[ $post_id ][ $field_name ]) ) {
|
||||
|
||||
// update key
|
||||
$field_key = $this->preview_fields[ $post_id ][ $field_name ];
|
||||
$field_value = $this->preview_values[ $post_id ][ $field_key ];
|
||||
|
||||
|
||||
// cache value
|
||||
acf_set_cache("get_value/post_id={$post_id}/name={$field_name}", $field_value);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 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 ealry 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 ealry 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 ealry 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 ealry 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,707 @@
|
||||
<?php
|
||||
|
||||
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
||||
|
||||
if( ! class_exists('acf_form_front') ) :
|
||||
|
||||
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
|
||||
$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 ]) ) continue;
|
||||
|
||||
|
||||
// register
|
||||
acf_add_local_field($field);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// honeypot
|
||||
if( !empty($_POST['acf']['_validate_email']) ) {
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// save post_title
|
||||
if( isset($_POST['acf']['_post_title']) ) {
|
||||
|
||||
$save['post_title'] = acf_extract_var($_POST['acf'], '_post_title');
|
||||
|
||||
}
|
||||
|
||||
|
||||
// save post_content
|
||||
if( isset($_POST['acf']['_post_content']) ) {
|
||||
|
||||
$save['post_content'] = acf_extract_var($_POST['acf'], '_post_content');
|
||||
|
||||
}
|
||||
|
||||
|
||||
// honeypot
|
||||
if( !empty($_POST['acf']['_validate_email']) ) 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;
|
||||
|
||||
|
||||
// bail ealry if form not submit
|
||||
if( empty($_POST['_acf_form']) ) return;
|
||||
|
||||
|
||||
// load form
|
||||
$form = json_decode( acf_decrypt($_POST['_acf_form']), true );
|
||||
|
||||
|
||||
// bail ealry if form is corrupt
|
||||
if( empty($form) ) return;
|
||||
|
||||
|
||||
// kses
|
||||
if( $form['kses'] && isset($_POST['acf']) ) {
|
||||
$_POST['acf'] = wp_kses_post_deep( $_POST['acf'] );
|
||||
}
|
||||
|
||||
|
||||
// validate data
|
||||
acf_validate_save_post(true);
|
||||
|
||||
|
||||
// submit
|
||||
$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() ) {
|
||||
|
||||
// array
|
||||
if( is_array($args) ) {
|
||||
|
||||
$args = $this->validate_form( $args );
|
||||
|
||||
// id
|
||||
} else {
|
||||
|
||||
$args = $this->get_form( $args );
|
||||
|
||||
}
|
||||
|
||||
|
||||
// bail early if no args
|
||||
if( !$args ) return false;
|
||||
|
||||
|
||||
// load values from this post
|
||||
$post_id = $args['post_id'];
|
||||
|
||||
|
||||
// dont load values for 'new_post'
|
||||
if( $post_id === 'new_post' ) $post_id = false;
|
||||
|
||||
|
||||
// register local fields
|
||||
foreach( $this->fields as $k => $field ) {
|
||||
|
||||
acf_add_local_field($field);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// vars
|
||||
$field_groups = array();
|
||||
$fields = array();
|
||||
|
||||
|
||||
// post_title
|
||||
if( $args['post_title'] ) {
|
||||
|
||||
// load local field
|
||||
$_post_title = acf_get_field('_post_title');
|
||||
$_post_title['value'] = $post_id ? get_post_field('post_title', $post_id) : '';
|
||||
|
||||
|
||||
// append
|
||||
$fields[] = $_post_title;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// post_content
|
||||
if( $args['post_content'] ) {
|
||||
|
||||
// load local field
|
||||
$_post_content = acf_get_field('_post_content');
|
||||
$_post_content['value'] = $post_id ? get_post_field('post_content', $post_id) : '';
|
||||
|
||||
|
||||
// append
|
||||
$fields[] = $_post_content;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// specific fields
|
||||
if( $args['fields'] ) {
|
||||
|
||||
foreach( $args['fields'] as $selector ) {
|
||||
|
||||
// append field ($strict = false to allow for better compatibility with field names)
|
||||
$fields[] = acf_maybe_get_field( $selector, $post_id, false );
|
||||
|
||||
}
|
||||
|
||||
} elseif( $args['field_groups'] ) {
|
||||
|
||||
foreach( $args['field_groups'] as $selector ) {
|
||||
|
||||
$field_groups[] = acf_get_field_group( $selector );
|
||||
|
||||
}
|
||||
|
||||
} elseif( $args['post_id'] == 'new_post' ) {
|
||||
|
||||
$field_groups = acf_get_field_groups( $args['new_post'] );
|
||||
|
||||
} else {
|
||||
|
||||
$field_groups = acf_get_field_groups(array(
|
||||
'post_id' => $args['post_id']
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
|
||||
//load fields based on field groups
|
||||
if( !empty($field_groups) ) {
|
||||
|
||||
foreach( $field_groups as $field_group ) {
|
||||
|
||||
$field_group_fields = acf_get_fields( $field_group );
|
||||
|
||||
if( !empty($field_group_fields) ) {
|
||||
|
||||
foreach( array_keys($field_group_fields) as $i ) {
|
||||
|
||||
$fields[] = acf_extract_var($field_group_fields, $i);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// honeypot
|
||||
if( $args['honeypot'] ) {
|
||||
|
||||
$fields[] = acf_get_field('_validate_email');
|
||||
|
||||
}
|
||||
|
||||
|
||||
// updated message
|
||||
if( !empty($_GET['updated']) && $args['updated_message'] ) {
|
||||
|
||||
printf( $args['html_updated_message'], $args['updated_message'] );
|
||||
|
||||
}
|
||||
|
||||
|
||||
// uploader (always set incase of multiple forms on the page)
|
||||
acf_update_setting('uploader', $args['uploader']);
|
||||
|
||||
|
||||
// display form
|
||||
if( $args['form'] ): ?>
|
||||
|
||||
<form <?php acf_esc_attr_e( $args['form_attributes']); ?>>
|
||||
|
||||
<?php endif;
|
||||
|
||||
// render post data
|
||||
acf_form_data(array(
|
||||
'post_id' => $args['post_id'],
|
||||
'nonce' => 'acf_form',
|
||||
'form' => acf_encrypt(json_encode($args))
|
||||
));
|
||||
|
||||
?>
|
||||
|
||||
<div class="acf-fields acf-form-fields -<?php echo $args['label_placement']; ?>">
|
||||
<?php
|
||||
|
||||
|
||||
// html before fields
|
||||
echo $args['html_before_fields'];
|
||||
|
||||
|
||||
// render
|
||||
acf_render_fields( $post_id, $fields, $args['field_el'], $args['instruction_placement'] );
|
||||
|
||||
|
||||
// html after fields
|
||||
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 = '' ) {
|
||||
|
||||
acf()->form_front->get_form( $id );
|
||||
|
||||
}
|
||||
|
||||
function acf_register_form( $args ) {
|
||||
|
||||
acf()->form_front->add_form( $args );
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,323 @@
|
||||
<?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);
|
||||
|
||||
|
||||
// filters
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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 = acf_get_term_post_id( 'nav_menu', $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 ) {
|
||||
|
||||
// bail ealry if not set
|
||||
if( empty($_POST['menu-item-acf']) ) return;
|
||||
|
||||
|
||||
// loop
|
||||
foreach( $_POST['menu-item-acf'] as $post_id => $values ) {
|
||||
|
||||
acf_save_post( $post_id, $values );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* wp_edit_nav_menu_walker
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @type function
|
||||
* @date 26/5/17
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function wp_edit_nav_menu_walker( $class, $menu_id = 0 ) {
|
||||
|
||||
// global
|
||||
global $acf_menu;
|
||||
|
||||
|
||||
// set var
|
||||
$acf_menu = (int) $menu_id;
|
||||
|
||||
|
||||
// include walker
|
||||
if( class_exists('Walker_Nav_Menu_Edit') ) {
|
||||
acf_include('includes/walkers/class-acf-walker-nav-menu-edit.php');
|
||||
}
|
||||
|
||||
|
||||
// return
|
||||
return 'ACF_Walker_Nav_Menu_Edit';
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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() {
|
||||
|
||||
// bail ealry if not set
|
||||
if( empty($_POST['menu-item-acf']) ) return;
|
||||
|
||||
|
||||
// loop
|
||||
foreach( $_POST['menu-item-acf'] as $post_id => $values ) {
|
||||
|
||||
// vars
|
||||
$prefix = 'menu-item-acf['.$post_id.']';
|
||||
|
||||
|
||||
// validate
|
||||
acf_validate_values( $values, $prefix );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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() {
|
||||
|
||||
// global
|
||||
global $acf_menu;
|
||||
|
||||
|
||||
// vars
|
||||
$post_id = acf_get_term_post_id( 'nav_menu', $acf_menu );
|
||||
|
||||
|
||||
// get field groups
|
||||
$field_groups = acf_get_field_groups(array(
|
||||
'nav_menu' => $acf_menu
|
||||
));
|
||||
|
||||
|
||||
?>
|
||||
<div id="tmpl-acf-menu-settings" style="display: none;">
|
||||
<?php
|
||||
|
||||
// data (always needed to save nav menu items)
|
||||
acf_form_data(array(
|
||||
'post_id' => $post_id,
|
||||
'nonce' => 'nav_menu',
|
||||
));
|
||||
|
||||
|
||||
// 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( $post_id, $fields, 'div', $field_group['instruction_placement'] );
|
||||
|
||||
echo '</div>';
|
||||
|
||||
echo '</div>';
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
(function($) {
|
||||
|
||||
// append html
|
||||
$('#post-body-content').append( $('#tmpl-acf-menu-settings').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
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new acf_form_nav_menu();
|
||||
|
||||
endif;
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,632 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ACF Post Form Class
|
||||
*
|
||||
* All the logic for adding fields to posts
|
||||
*
|
||||
* @class acf_form_post
|
||||
* @package ACF
|
||||
* @subpackage Forms
|
||||
*/
|
||||
|
||||
if( ! class_exists('acf_form_post') ) :
|
||||
|
||||
class acf_form_post {
|
||||
|
||||
var $post_id = 0,
|
||||
$typenow = '',
|
||||
$style = '';
|
||||
|
||||
|
||||
/*
|
||||
* __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_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);
|
||||
|
||||
|
||||
// ajax
|
||||
add_action('wp_ajax_acf/post/get_field_groups', array($this, 'get_field_groups'));
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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 $post, $pagenow, $typenow;
|
||||
|
||||
|
||||
// vars
|
||||
$return = false;
|
||||
|
||||
|
||||
// validate page
|
||||
if( in_array($pagenow, array('post.php', 'post-new.php')) ) {
|
||||
|
||||
$return = true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// update vars
|
||||
if( !empty($post) ) {
|
||||
|
||||
$this->post_id = $post->ID;
|
||||
$this->typenow = $typenow;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// validate post type
|
||||
if( in_array($typenow, array('acf-field-group', 'attachment')) ) {
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// validate page (Shopp)
|
||||
if( $pagenow == "admin.php" && isset( $_GET['page'] ) && $_GET['page'] == "shopp-products" && isset( $_GET['id'] ) ) {
|
||||
|
||||
$return = true;
|
||||
|
||||
$this->post_id = absint( $_GET['id'] );
|
||||
$this->typenow = 'shopp_product';
|
||||
|
||||
}
|
||||
|
||||
|
||||
// return
|
||||
return $return;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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('acf/input/admin_head', array($this,'admin_head'));
|
||||
add_action('acf/input/admin_footer', array($this,'admin_footer'));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* admin_head
|
||||
*
|
||||
* This action will find and add field groups to the current edit page
|
||||
*
|
||||
* @type action (admin_head)
|
||||
* @date 23/06/12
|
||||
* @since 3.1.8
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function admin_head() {
|
||||
|
||||
// vars
|
||||
$style_found = false;
|
||||
|
||||
|
||||
// get field groups
|
||||
$field_groups = acf_get_field_groups();
|
||||
|
||||
|
||||
// add meta boxes
|
||||
if( !empty($field_groups) ) {
|
||||
|
||||
foreach( $field_groups as $i => $field_group ) {
|
||||
|
||||
// vars
|
||||
$id = "acf-{$field_group['key']}";
|
||||
$title = $field_group['title'];
|
||||
$context = $field_group['position'];
|
||||
$priority = 'high';
|
||||
$args = array(
|
||||
'field_group' => $field_group,
|
||||
'visibility' => false
|
||||
);
|
||||
|
||||
|
||||
// tweaks to vars
|
||||
if( $context == 'side' ) {
|
||||
|
||||
$priority = 'core';
|
||||
|
||||
}
|
||||
|
||||
|
||||
// filter for 3rd party customization
|
||||
$priority = apply_filters('acf/input/meta_box_priority', $priority, $field_group);
|
||||
|
||||
|
||||
// visibility
|
||||
$args['visibility'] = acf_get_field_group_visibility( $field_group, array(
|
||||
'post_id' => $this->post_id,
|
||||
'post_type' => $this->typenow
|
||||
));
|
||||
|
||||
|
||||
// add meta box
|
||||
add_meta_box( $id, $title, array($this, 'render_meta_box'), $this->typenow, $context, $priority, $args );
|
||||
|
||||
|
||||
// update style
|
||||
if( !$style_found && $args['visibility'] ) {
|
||||
|
||||
$style_found = true;
|
||||
|
||||
$this->style = acf_get_field_group_style( $field_group );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Allow 'acf_after_title' metabox position
|
||||
add_action('edit_form_after_title', array($this, 'edit_form_after_title'));
|
||||
|
||||
|
||||
// remove postcustom metabox (removes expensive SQL query)
|
||||
if( acf_get_setting('remove_wp_meta_box') ) {
|
||||
|
||||
remove_meta_box( 'postcustom', false, 'normal' );
|
||||
|
||||
}
|
||||
|
||||
|
||||
// remove ACF values from meta postbox ()
|
||||
add_filter('is_protected_meta', array($this, 'is_protected_meta'), 10, 3);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* edit_form_after_title
|
||||
*
|
||||
* This action will allow ACF to render metaboxes after the title
|
||||
*
|
||||
* @type action
|
||||
* @date 17/08/13
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function edit_form_after_title() {
|
||||
|
||||
// globals
|
||||
global $post, $wp_meta_boxes;
|
||||
|
||||
|
||||
// render post data
|
||||
acf_form_data(array(
|
||||
'post_id' => $this->post_id,
|
||||
'nonce' => 'post',
|
||||
'ajax' => 1
|
||||
));
|
||||
|
||||
|
||||
// render
|
||||
do_meta_boxes( get_current_screen(), 'acf_after_title', $post);
|
||||
|
||||
|
||||
// clean up
|
||||
unset( $wp_meta_boxes['post']['acf_after_title'] );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* render_meta_box
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @type function
|
||||
* @date 20/10/13
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function render_meta_box( $post, $args ) {
|
||||
|
||||
// extract args
|
||||
extract( $args ); // all variables from the add_meta_box function
|
||||
extract( $args ); // all variables from the args argument
|
||||
|
||||
|
||||
// vars
|
||||
$o = array(
|
||||
'id' => $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');
|
||||
|
||||
}
|
||||
|
||||
|
||||
// load and render fields
|
||||
if( $visibility ) {
|
||||
|
||||
// load fields
|
||||
$fields = acf_get_fields( $field_group );
|
||||
|
||||
|
||||
// render
|
||||
acf_render_fields( $this->post_id, $fields, 'div', $field_group['instruction_placement'] );
|
||||
|
||||
// render replace-me div
|
||||
} else {
|
||||
|
||||
echo '<div class="acf-replace-with-fields"><div class="acf-loading"></div></div>';
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
if( typeof acf !== 'undefined' ) {
|
||||
|
||||
acf.postbox.render(<?php echo json_encode($o); ?>);
|
||||
|
||||
}
|
||||
</script>
|
||||
<?php
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* admin_footer
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @type function
|
||||
* @date 21/10/13
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function admin_footer(){
|
||||
|
||||
// get style of first field group
|
||||
echo '<style type="text/css" id="acf-style">' . $this->style . '</style>';
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* get_field_groups
|
||||
*
|
||||
* This function will return all the JSON data needed to render new metaboxes
|
||||
*
|
||||
* @type function
|
||||
* @date 21/10/13
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param n/a
|
||||
* @return n/a
|
||||
*/
|
||||
|
||||
function get_field_groups() {
|
||||
|
||||
// options
|
||||
$options = acf_parse_args($_POST, array(
|
||||
'nonce' => '',
|
||||
'post_id' => 0,
|
||||
'ajax' => 1,
|
||||
'exists' => array()
|
||||
));
|
||||
|
||||
|
||||
// vars
|
||||
$json = array();
|
||||
$exists = acf_extract_var( $options, 'exists' );
|
||||
|
||||
|
||||
// verify nonce
|
||||
if( !acf_verify_ajax() ) die();
|
||||
|
||||
|
||||
// get field groups
|
||||
$field_groups = acf_get_field_groups( $options );
|
||||
|
||||
|
||||
// bail early if no field groups
|
||||
if( empty($field_groups) ) {
|
||||
|
||||
wp_send_json_success( $json );
|
||||
|
||||
}
|
||||
|
||||
|
||||
// loop through field groups
|
||||
foreach( $field_groups as $i => $field_group ) {
|
||||
|
||||
// vars
|
||||
$item = array(
|
||||
//'ID' => $field_group['ID'], - JSON does not have ID (not used by JS anyway)
|
||||
'key' => $field_group['key'],
|
||||
'title' => $field_group['title'],
|
||||
'html' => '',
|
||||
'style' => ''
|
||||
);
|
||||
|
||||
|
||||
// style
|
||||
if( $i == 0 ) {
|
||||
|
||||
$item['style'] = acf_get_field_group_style( $field_group );
|
||||
|
||||
}
|
||||
|
||||
|
||||
// html
|
||||
if( !in_array($field_group['key'], $exists) ) {
|
||||
|
||||
// load fields
|
||||
$fields = acf_get_fields( $field_group );
|
||||
|
||||
|
||||
// get field HTML
|
||||
ob_start();
|
||||
|
||||
|
||||
// render
|
||||
acf_render_fields( $options['post_id'], $fields, 'div', $field_group['instruction_placement'] );
|
||||
|
||||
|
||||
$item['html'] = ob_get_clean();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// append
|
||||
$json[] = $item;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// return
|
||||
wp_send_json_success( $json );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* wp_insert_post_empty_content
|
||||
*
|
||||
* This function will allow WP to insert a new post without title / content if ACF data exists
|
||||
*
|
||||
* @type function
|
||||
* @date 16/07/2014
|
||||
* @since 5.0.1
|
||||
*
|
||||
* @param $maybe_empty (bool) whether the post should be considered "empty"
|
||||
* @param $postarr (array) Array of post data
|
||||
* @return $maybe_empty
|
||||
*/
|
||||
|
||||
function wp_insert_post_empty_content( $maybe_empty, $postarr ) {
|
||||
|
||||
if( $maybe_empty && acf_maybe_get_POST('_acf_changed') ) {
|
||||
|
||||
$maybe_empty = false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// return
|
||||
return $maybe_empty;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* allow_save_post
|
||||
*
|
||||
* This function will return true if the post is allowed to be saved
|
||||
*
|
||||
* @type function
|
||||
* @date 26/06/2016
|
||||
* @since 5.3.8
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function allow_save_post( $post ) {
|
||||
|
||||
// vars
|
||||
$allow = true;
|
||||
|
||||
|
||||
// disallow if is post type
|
||||
$post_types = array( 'auto-draft', 'revision', 'acf-field', 'acf-field-group' );
|
||||
if( in_array($post->post_type, $post_types) ) $allow = false;
|
||||
|
||||
|
||||
// disallow if not the form post
|
||||
$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
|
||||
if( acf_maybe_get_POST('wp-preview') == 'dopreview' ) $allow = true;
|
||||
|
||||
|
||||
// disallow if not a revision of the form post
|
||||
if( $form_post_id && $form_post_id !== $post->post_parent ) $allow = false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// return
|
||||
return $allow;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* save_post
|
||||
*
|
||||
* This function will validate and save the $_POST data
|
||||
*
|
||||
* @type function
|
||||
* @date 23/06/12
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function save_post( $post_id, $post ) {
|
||||
|
||||
// bail ealry 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' ) {
|
||||
|
||||
// show errors
|
||||
acf_validate_save_post( true );
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* is_protected_meta
|
||||
*
|
||||
* This function will remove any ACF meta from showing in the meta postbox
|
||||
*
|
||||
* @type function
|
||||
* @date 12/04/2014
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function is_protected_meta( $protected, $meta_key, $meta_type ) {
|
||||
|
||||
// if acf_get_field_reference returns a valid key, this is an acf value, so protect it!
|
||||
if( !$protected ) {
|
||||
|
||||
$reference = acf_get_field_reference( $meta_key, $this->post_id );
|
||||
|
||||
if( acf_is_field_key($reference) ) {
|
||||
|
||||
$protected = true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// return
|
||||
return $protected;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new acf_form_post();
|
||||
|
||||
endif;
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,462 @@
|
||||
<?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 $form = '#addtag';
|
||||
|
||||
|
||||
/*
|
||||
* __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 = acf_get_term_post_id( $taxonomy, 0 );
|
||||
|
||||
|
||||
// update vars
|
||||
$this->form = '#addtag';
|
||||
|
||||
|
||||
// get field groups
|
||||
$field_groups = acf_get_field_groups(array(
|
||||
'taxonomy' => $taxonomy
|
||||
));
|
||||
|
||||
|
||||
// render
|
||||
if( !empty($field_groups) ) {
|
||||
|
||||
// data
|
||||
acf_form_data(array(
|
||||
'post_id' => $post_id,
|
||||
'nonce' => 'taxonomy',
|
||||
));
|
||||
|
||||
// wrap
|
||||
echo '<div class="acf-addterm-fields acf-fields -clear">';
|
||||
|
||||
// loop
|
||||
foreach( $field_groups as $field_group ) {
|
||||
$fields = acf_get_fields( $field_group );
|
||||
acf_render_fields( $post_id, $fields, '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 = acf_get_term_post_id( $term->taxonomy, $term->term_id );
|
||||
|
||||
|
||||
// update vars
|
||||
$this->form = '#edittag';
|
||||
|
||||
|
||||
// get field groups
|
||||
$field_groups = acf_get_field_groups(array(
|
||||
'taxonomy' => $taxonomy
|
||||
));
|
||||
|
||||
|
||||
// render
|
||||
if( !empty($field_groups) ) {
|
||||
|
||||
acf_form_data(array(
|
||||
'post_id' => $post_id,
|
||||
'nonce' => 'taxonomy'
|
||||
));
|
||||
|
||||
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( $post_id, $fields, '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($) {
|
||||
|
||||
// vars
|
||||
var $spinner = $('<?php echo $this->form; ?> p.submit .spinner');
|
||||
|
||||
|
||||
// create spinner if not exists (may exist in future WP versions)
|
||||
if( !$spinner.exists() ) {
|
||||
|
||||
// create spinner
|
||||
$spinner = $('<span class="spinner"></span>');
|
||||
|
||||
|
||||
// append
|
||||
$('<?php echo $this->form; ?> p.submit').append( $spinner );
|
||||
|
||||
}
|
||||
|
||||
|
||||
// update acf validation class
|
||||
acf.validation.error_class = 'form-invalid';
|
||||
|
||||
|
||||
<?php if( $this->form == '#addtag' ): ?>
|
||||
|
||||
// store origional HTML
|
||||
var $orig = $('#addtag').children('.acf-field').clone();
|
||||
|
||||
|
||||
// events
|
||||
$('#submit').on('click', function( e ){
|
||||
|
||||
// bail early if not active
|
||||
if( !acf.validation.active ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// ignore validation (only ignore once)
|
||||
if( acf.validation.ignore ) {
|
||||
|
||||
acf.validation.ignore = 0;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// bail early if this form does not contain ACF data
|
||||
if( !$('#addtag').find('#acf-form-data').exists() ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// stop WP JS validation
|
||||
e.stopImmediatePropagation();
|
||||
|
||||
|
||||
// store submit trigger so it will be clicked if validation is passed
|
||||
acf.validation.$trigger = $(this);
|
||||
|
||||
|
||||
// run validation
|
||||
acf.validation.fetch( $('#addtag') );
|
||||
|
||||
|
||||
// stop all other click events on this input
|
||||
return false;
|
||||
|
||||
});
|
||||
|
||||
|
||||
$(document).ajaxComplete(function(event, xhr, settings) {
|
||||
|
||||
// bail early if is other ajax call
|
||||
if( settings.data.indexOf('action=add-tag') == -1 ) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// unlock form
|
||||
acf.validation.toggle( $('#addtag'), 'unlock' );
|
||||
|
||||
|
||||
// bail early if response contains error
|
||||
if( xhr.responseText.indexOf('wp_error') !== -1 ) {
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// action for 3rd party customization
|
||||
acf.do_action('remove', $('#addtag'));
|
||||
|
||||
|
||||
// remove old fields
|
||||
$('#addtag').find('.acf-field').remove();
|
||||
|
||||
|
||||
// add orig fields
|
||||
$('#acf-form-data').after( $orig.clone() );
|
||||
|
||||
|
||||
// reset unload
|
||||
acf.unload.off();
|
||||
|
||||
|
||||
// action for 3rd party customization
|
||||
acf.do_action('append', $('#addtag'));
|
||||
|
||||
});
|
||||
|
||||
<?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 = acf_get_term_post_id( $taxonomy, $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,413 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ACF User Form Class
|
||||
*
|
||||
* All the logic for adding fields to users
|
||||
*
|
||||
* @class acf_form_user
|
||||
* @package ACF
|
||||
* @subpackage Forms
|
||||
*/
|
||||
|
||||
if( ! class_exists('acf_form_user') ) :
|
||||
|
||||
class acf_form_user {
|
||||
|
||||
var $form = '#createuser';
|
||||
|
||||
|
||||
/*
|
||||
* __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('login_form_register', array($this, 'admin_enqueue_scripts'));
|
||||
|
||||
// render
|
||||
add_action('show_user_profile', array($this, 'edit_user'));
|
||||
add_action('edit_user_profile', array($this, 'edit_user'));
|
||||
add_action('user_new_form', array($this, 'user_new_form'));
|
||||
add_action('register_form', array($this, 'register_user'));
|
||||
|
||||
// save
|
||||
//add_action('edit_user_profile_update', array($this, 'save_user'));
|
||||
//add_action('personal_options_update', array($this, 'save_user'));
|
||||
add_action('user_register', array($this, 'save_user'));
|
||||
add_action('profile_update', array($this, 'save_user'));
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 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( in_array( $pagenow, array('profile.php', 'user-edit.php', 'user-new.php', 'wp-login.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('acf/input/admin_footer', array($this, 'admin_footer'), 10, 1);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* register_user
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @type function
|
||||
* @date 8/10/13
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function register_user() {
|
||||
|
||||
// update vars
|
||||
$this->form = '#registerform';
|
||||
|
||||
|
||||
// render
|
||||
$this->render( 0, 'register', 'div' );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* edit_user
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @type function
|
||||
* @date 8/10/13
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function edit_user( $user ) {
|
||||
|
||||
// update vars
|
||||
$this->form = '#your-profile';
|
||||
|
||||
|
||||
// render
|
||||
$this->render( $user->ID, 'edit', 'tr' );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* user_new_form
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @type function
|
||||
* @date 8/10/13
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function user_new_form() {
|
||||
|
||||
// update vars
|
||||
$this->form = '#createuser';
|
||||
|
||||
|
||||
// render
|
||||
$this->render( 0, 'add', '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( $user_id, $user_form, $el = 'tr' ) {
|
||||
|
||||
// vars
|
||||
$post_id = "user_{$user_id}";
|
||||
$show_title = true;
|
||||
|
||||
|
||||
// args
|
||||
$args = array(
|
||||
'user_id' => 'new',
|
||||
'user_form' => $user_form
|
||||
);
|
||||
|
||||
if( $user_id ) $args['user_id'] = $user_id;
|
||||
|
||||
|
||||
// get field groups
|
||||
$field_groups = acf_get_field_groups( $args );
|
||||
|
||||
|
||||
// bail early if no field groups
|
||||
if( empty($field_groups) ) return;
|
||||
|
||||
|
||||
// form data
|
||||
acf_form_data(array(
|
||||
'post_id' => $post_id,
|
||||
'nonce' => 'user'
|
||||
));
|
||||
|
||||
|
||||
// loop
|
||||
foreach( $field_groups as $field_group ) {
|
||||
|
||||
// vars
|
||||
$fields = acf_get_fields( $field_group );
|
||||
|
||||
|
||||
// title
|
||||
if( $show_title && $field_group['style'] === 'default' ) {
|
||||
echo '<h2>' . $field_group['title'] . '</h2>';
|
||||
}
|
||||
|
||||
|
||||
// table start
|
||||
if( $el == 'tr' ) {
|
||||
echo '<table class="form-table"><tbody>';
|
||||
} else {
|
||||
echo '<div class="acf-user-register-fields acf-fields -clear">';
|
||||
}
|
||||
|
||||
|
||||
// render fields
|
||||
acf_render_fields( $post_id, $fields, $el, $field_group['instruction_placement'] );
|
||||
|
||||
|
||||
// table end
|
||||
if( $el == 'tr' ) {
|
||||
echo '</tbody></table>';
|
||||
} else {
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* admin_footer
|
||||
*
|
||||
* description
|
||||
*
|
||||
* @type function
|
||||
* @date 27/03/2015
|
||||
* @since 5.1.5
|
||||
*
|
||||
* @param $post_id (int)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function admin_footer() {
|
||||
|
||||
?>
|
||||
<style type="text/css">
|
||||
|
||||
<?php if( is_admin() ): ?>
|
||||
|
||||
/* override for user css */
|
||||
.acf-field input[type="text"],
|
||||
.acf-field input[type="password"],
|
||||
.acf-field input[type="number"],
|
||||
.acf-field input[type="search"],
|
||||
.acf-field input[type="email"],
|
||||
.acf-field input[type="url"],
|
||||
.acf-field select {
|
||||
max-width: 25em;
|
||||
}
|
||||
|
||||
.acf-field textarea {
|
||||
max-width: 500px;
|
||||
}
|
||||
|
||||
|
||||
/* allow sub fields to display correctly */
|
||||
.acf-field .acf-field input[type="text"],
|
||||
.acf-field .acf-field input[type="password"],
|
||||
.acf-field .acf-field input[type="number"],
|
||||
.acf-field .acf-field input[type="search"],
|
||||
.acf-field .acf-field input[type="email"],
|
||||
.acf-field .acf-field input[type="url"],
|
||||
.acf-field .acf-field textarea,
|
||||
.acf-field .acf-field select {
|
||||
max-width: none;
|
||||
}
|
||||
|
||||
<?php else: ?>
|
||||
|
||||
#registerform h2 {
|
||||
margin: 1em 0;
|
||||
}
|
||||
|
||||
#registerform .acf-field .acf-label {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
#registerform .acf-field .acf-label label {
|
||||
font-weight: normal;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
#registerform p.submit {
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
<?php endif; ?>
|
||||
|
||||
</style>
|
||||
<script type="text/javascript">
|
||||
(function($) {
|
||||
|
||||
// vars
|
||||
var $spinner = $('<?php echo $this->form; ?> p.submit .spinner');
|
||||
|
||||
|
||||
// create spinner if not exists (may exist in future WP versions)
|
||||
if( !$spinner.exists() ) {
|
||||
|
||||
// create spinner (use .acf-spinner becuase .spinner CSS not included on register page)
|
||||
$spinner = $('<span class="acf-spinner"></span>');
|
||||
|
||||
|
||||
// append
|
||||
$('<?php echo $this->form; ?> p.submit').append( $spinner );
|
||||
|
||||
}
|
||||
|
||||
})(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 and remove nonce
|
||||
if( ! acf_verify_nonce('user') ) {
|
||||
|
||||
return $user_id;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// save data
|
||||
if( acf_validate_save_post(true) ) {
|
||||
|
||||
acf_save_post( "user_{$user_id}" );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new acf_form_user();
|
||||
|
||||
endif;
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,384 @@
|
||||
<?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') ) :
|
||||
|
||||
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() {
|
||||
|
||||
// bail ealry if not widget
|
||||
if( !isset($_POST['_acf_widget_id']) ) return;
|
||||
|
||||
|
||||
// vars
|
||||
$id = $_POST['_acf_widget_id'];
|
||||
$number = $_POST['_acf_widget_number'];
|
||||
$prefix = $_POST['_acf_widget_prefix'];
|
||||
|
||||
|
||||
// validate
|
||||
acf_validate_values( $_POST[ $id ][ $number ]['acf'], $prefix );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* edit_widget
|
||||
*
|
||||
* This function will render the fields for a widget form
|
||||
*
|
||||
* @type function
|
||||
* @date 11/06/2014
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param $widget (object)
|
||||
* @param $return (null)
|
||||
* @param $instance (object)
|
||||
* @return $post_id (int)
|
||||
*/
|
||||
|
||||
function edit_widget( $widget, $return, $instance ) {
|
||||
|
||||
// vars
|
||||
$post_id = 0;
|
||||
$prefix = 'widget-' . $widget->id_base . '[' . $widget->number . '][acf]';
|
||||
|
||||
|
||||
// get id
|
||||
if( $widget->number !== '__i__' ) {
|
||||
|
||||
$post_id = "widget_{$widget->id}";
|
||||
|
||||
}
|
||||
|
||||
|
||||
// get field groups
|
||||
$field_groups = acf_get_field_groups(array(
|
||||
'widget' => $widget->id_base
|
||||
));
|
||||
|
||||
|
||||
// render
|
||||
if( !empty($field_groups) ) {
|
||||
|
||||
// render post data
|
||||
acf_form_data(array(
|
||||
'post_id' => $post_id,
|
||||
'nonce' => 'widget',
|
||||
'widget_id' => 'widget-' . $widget->id_base,
|
||||
'widget_number' => $widget->number,
|
||||
'widget_prefix' => $prefix
|
||||
));
|
||||
|
||||
|
||||
// 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( $post_id, $fields, '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.do_action('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 ) {
|
||||
|
||||
// bail ealry if not valid (!customize + acf values + nonce)
|
||||
if( isset($_POST['wp_customize']) || !isset($new_instance['acf']) || !acf_verify_nonce('widget') ) 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.update('post_id', 'widgets');
|
||||
|
||||
|
||||
// restrict get fields
|
||||
acf.add_filter('get_fields', function( $fields ){
|
||||
|
||||
// widgets
|
||||
$fields = $fields.not('#available-widgets .acf-field');
|
||||
|
||||
|
||||
// customizer
|
||||
$fields = $fields.not('.widget-tpl .acf-field');
|
||||
|
||||
|
||||
// return
|
||||
return $fields;
|
||||
|
||||
});
|
||||
|
||||
|
||||
$('#widgets-right').on('click', '.widget-control-save', function( e ){
|
||||
|
||||
// vars
|
||||
var $form = $(this).closest('form');
|
||||
|
||||
|
||||
// bail early if not active
|
||||
if( !acf.validation.active ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// ignore validation (only ignore once)
|
||||
if( acf.validation.ignore ) {
|
||||
|
||||
acf.validation.ignore = 0;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// bail early if this form does not contain ACF data
|
||||
if( !$form.find('#acf-form-data').exists() ) {
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// stop WP JS validation
|
||||
e.stopImmediatePropagation();
|
||||
|
||||
|
||||
// store submit trigger so it will be clicked if validation is passed
|
||||
acf.validation.$trigger = $(this);
|
||||
|
||||
|
||||
// run validation
|
||||
acf.validation.fetch( $form );
|
||||
|
||||
|
||||
// stop all other click events on this input
|
||||
return false;
|
||||
|
||||
});
|
||||
|
||||
|
||||
$(document).on('click', '.widget-top', function(){
|
||||
|
||||
var $el = $(this).parent().children('.widget-inside');
|
||||
|
||||
setTimeout(function(){
|
||||
|
||||
acf.get_fields('', $el).each(function(){
|
||||
|
||||
acf.do_action('show_field', $(this));
|
||||
|
||||
});
|
||||
|
||||
}, 250);
|
||||
|
||||
});
|
||||
|
||||
$(document).on('widget-added', function( e, $widget ){
|
||||
|
||||
// - use delay to avoid rendering issues with customizer (ensures div is visible)
|
||||
setTimeout(function(){
|
||||
|
||||
acf.do_action('append', $widget );
|
||||
|
||||
}, 100);
|
||||
|
||||
});
|
||||
|
||||
$(document).on('widget-saved widget-updated', function( e, $widget ){
|
||||
|
||||
// unlock form
|
||||
acf.validation.toggle( $widget, 'unlock' );
|
||||
|
||||
|
||||
// submit
|
||||
acf.do_action('submit', $widget );
|
||||
|
||||
});
|
||||
|
||||
})(jQuery);
|
||||
</script>
|
||||
<?php
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
new acf_form_widget();
|
||||
|
||||
endif;
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user