<?php
class acf_everything_fields
{
var $settings,
$data;
function __construct()
{
$this->data = array(
'page_id' => '',
'metabox_ids' => array(),
'page_type' => '',
'page_action' => '',
'option_name' => '',
);
add_action('admin_menu', array($this, 'admin_menu'));
add_action('wp_ajax_acf/everything_fields', array($this, 'acf_everything_fields'));
add_filter('attachment_fields_to_edit', array($this, 'attachment_fields_to_edit'), 10, 2);
add_filter('attachment_fields_to_save', array($this, 'save_attachment'), 10, 2);
add_action('create_term', array($this, 'save_taxonomy'));
add_action('edited_term', array($this, 'save_taxonomy'));
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('shopp_category_saved', array($this, 'shopp_category_saved'));
add_action('delete_term', array($this, 'delete_term'), 10, 4);
}
function attachment_fields_to_edit( $form_fields, $post )
{
$screen = get_current_screen();
$post_id = $post->ID;
if( $screen && $screen->base == 'post' ) {
return $form_fields;
}
$filter = array( 'post_type' => 'attachment' );
$metabox_ids = array();
$metabox_ids = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter );
if( empty($metabox_ids) )
{
return $form_fields;
}
$acfs = apply_filters('acf/get_field_groups', array());
if( is_array($acfs) ) { foreach( $acfs as $acf ) {
if( !in_array( $acf['id'], $metabox_ids ) )
{
continue;
}
$fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']);
if( is_array($fields) ) { foreach( $fields as $i => $field ) {
if( !$field || !$field['type'] || $field['type'] == 'null' )
{
continue;
}
if( !isset($field['value']) )
{
$field['value'] = apply_filters('acf/load_value', false, $post_id, $field);
$field['value'] = apply_filters('acf/format_value', $field['value'], $post_id, $field);
}
$field['name'] = 'fields[' . $field['key'] . ']';
ob_start();
do_action('acf/create_field', $field);
$html = ob_get_contents();
ob_end_clean();
$form_fields[ $field['name'] ] = array(
'label' => $field['label'],
'input' => 'html',
'html' => $html
);
}};
}}
return $form_fields;
}
function save_attachment( $post, $attachment )
{
$post_id = $post['ID'];
do_action('acf/save_post', $post_id);
return $post;
}
function validate_page()
{
global $pagenow, $wp_version;
$return = false;
page
if( in_array( $pagenow, array( 'edit-tags.php', 'term.php', 'profile.php', 'user-new.php', 'user-edit.php', 'media.php' ) ) )
{
$return = true;
}
page (Shopp)
if( $pagenow == "admin.php" && isset( $_GET['page'], $_GET['id'] ) && $_GET['page'] == "shopp-categories" )
{
$return = true;
}
if( $pagenow === 'upload.php' && version_compare($wp_version, '4.0', '>=') ) {
$return = true;
}
return $return;
}
function admin_menu()
{
global $pagenow;
page
if( ! $this->validate_page() ) return;
$filter = array();
if( $pagenow == "admin.php" && isset( $_GET['page'], $_GET['id'] ) && $_GET['page'] == "shopp-categories" )
{
$_GET['id'] = filter_var($_GET['id'], FILTER_SANITIZE_STRING);
$this->data['page_type'] = "shopp_category";
$filter['ef_taxonomy'] = "shopp_category";
$this->data['page_action'] = "add";
$this->data['option_name'] = "";
if( $_GET['id'] != "new" )
{
$this->data['page_action'] = "edit";
$this->data['option_name'] = "shopp_category_" . $_GET['id'];
}
} elseif( $pagenow == "edit-tags.php" || $pagenow == "term.php" ) {
$taxonomy = 'post_tag';
$term_id = 0;
if( !empty($_GET['taxonomy']) ) {
$taxonomy = filter_var($_GET['taxonomy'], FILTER_SANITIZE_STRING);
}
if( !empty($_GET['tag_ID']) ) {
$term_id = filter_var($_GET['tag_ID'], FILTER_SANITIZE_NUMBER_INT);
}
$filter['ef_taxonomy'] = $taxonomy;
$this->data['page_type'] = "taxonomy";
$this->data['page_action'] = "add";
$this->data['option_name'] = "";
if( $term_id ) {
$this->data['page_action'] = "edit";
$this->data['option_name'] = $taxonomy . "_" . $term_id;
}
}
elseif( $pagenow == "profile.php" )
{
$this->data['page_type'] = "user";
$filter['ef_user'] = get_current_user_id();
$this->data['page_action'] = "edit";
$this->data['option_name'] = "user_" . get_current_user_id();
}
elseif( $pagenow == "user-edit.php" && isset($_GET['user_id']) )
{
$_GET['user_id'] = filter_var($_GET['user_id'], FILTER_SANITIZE_NUMBER_INT);
$this->data['page_type'] = "user";
$filter['ef_user'] = $_GET['user_id'];
$this->data['page_action'] = "edit";
$this->data['option_name'] = "user_" . $_GET['user_id'];
}
elseif( $pagenow == "user-new.php" )
{
$this->data['page_type'] = "user";
$filter['ef_user'] ='all';
$this->data['page_action'] = "add";
$this->data['option_name'] = "";
}
elseif( $pagenow == "media.php" || $pagenow == 'upload.php' )
{
$this->data['page_type'] = "media";
$filter['post_type'] = 'attachment';
$this->data['page_action'] = "add";
$this->data['option_name'] = "";
if(isset($_GET['attachment_id']))
{
$_GET['attachment_id'] = filter_var($_GET['attachment_id'], FILTER_SANITIZE_NUMBER_INT);
$this->data['page_action'] = "edit";
$this->data['option_name'] = $_GET['attachment_id'];
}
}
$metabox_ids = array();
$this->data['metabox_ids'] = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter );
if( empty( $this->data['metabox_ids'] ) )
{
return false;
}
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
add_action('admin_head', array($this, 'admin_head'));
}
function admin_enqueue_scripts()
{
do_action('acf/input/admin_enqueue_scripts');
}
function admin_head()
{
global $pagenow;
user js + css
do_action('acf/input/admin_head');
?>
<script type="text/javascript">
(function($) {
acf.data = {
action : 'acf/everything_fields',
metabox_ids : '<?php echo implode( ', ', $this->data['metabox_ids'] ); ?>',
page_type : '<?php echo $this->data['page_type']; ?>',
page_action : '<?php echo $this->data['page_action']; ?>',
option_name : '<?php echo $this->data['option_name']; ?>'
};
$(document).ready(function() {
$.ajax({
url: ajaxurl,
data: acf.data,
type: 'post',
dataType: 'html',
success: function(html) {
<?php
if($this->data['page_type'] == "user")
{
if($this->data['page_action'] == "add")
{
echo "$('#createuser > table.form-table:last > tbody').append( html );";
}
else
{
echo "$('#your-profile .form-table:last').after( html );";
}
}
elseif($this->data['page_type'] == "shopp_category")
{
echo "$('#post-body-content').append( html );";
}
elseif($this->data['page_type'] == "taxonomy")
{
if($this->data['page_action'] == "add")
{
echo "$('#addtag > p.submit').before( html );";
}
else
{
echo "$('#edittag > table.form-table:first > tbody').append( html );";
}
}
elseif($this->data['page_type'] == "media")
{
if($this->data['page_action'] == "add")
{
echo "$('#addtag > p.submit').before( html );";
}
else
{
echo "$('#media-single-form table tbody tr.submit').before( html );";
}
}
?>
setTimeout( function() {
$(document).trigger('acf/setup_fields', $('#wpbody') );
}, 200);
}
});
$(document).ajaxComplete(function(event, xhr, settings) {
data = acf.helpers.url_to_object(settings.data);
if( data.action != 'add-tag' )
{
return;
}
var $el = $('#addtag');
$el.find('.acf_wysiwyg textarea').each(function() {
var textarea = $(this),
id = textarea.attr('id'),
editor = tinyMCE.get( id );
if( editor )
{
editor.setContent('');
editor.save();
}
});
$el.find('.field .active').removeClass('active');
$el.find('input[type="checkbox"]').removeAttr('checked');
});
});
})(jQuery);
</script>
<?php
}
function save_taxonomy( $term_id )
{
if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
{
return $term_id;
}
if( !isset($_POST['taxonomy']) )
{
return;
}
$post_id = $_POST['taxonomy'] . '_' . $term_id;
do_action('acf/save_post', $post_id);
}
function save_user( $user_id )
{
if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
{
return $user_id;
}
$post_id = 'user_' . $user_id;
do_action('acf/save_post', $post_id);
}
function shopp_category_saved( $category )
{
if( !isset($_POST['acf_nonce']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
{
return $category;
}
$post_id = 'shopp_category_' . $category->id;
do_action('acf/save_post', $post_id);
}
function acf_everything_fields()
{
$defaults = array(
'metabox_ids' => '',
'page_type' => '',
'page_action' => '',
'option_name' => '',
);
$options = array_merge($defaults, $_POST);
$options['metabox_ids'] = explode( ', ', $options['metabox_ids'] );
$acfs = apply_filters('acf/get_field_groups', false);
$layout = 'tr';
if( $options['page_type'] == "taxonomy" && $options['page_action'] == "add")
{
$layout = 'div';
}
if( $options['page_type'] == "shopp_category")
{
$layout = 'metabox';
}
if( $acfs )
{
foreach( $acfs as $acf )
{
$acf['options'] = apply_filters('acf/field_group/get_options', array(), $acf['id']);
if( !in_array( $acf['id'], $options['metabox_ids'] ) )
{
continue;
}
dictates heading
$title = true;
if( $acf['options']['layout'] == 'no_box' )
{
$title = false;
}
if( $options['page_action'] == "edit" && $options['page_type'] == 'user' )
{
if( $title )
{
echo '<h3>' .$acf['title'] . '</h3>';
}
echo '<table class="form-table"><tbody>';
}
if( $layout == 'tr' )
{
echo '<tr style="display:none;"><td colspan="2"><input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" /></td></tr>';
}
else
{
echo '<input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" />';
}
if( $layout == 'metabox' )
{
echo '<div class="postbox acf_postbox" id="acf_'. $acf['id'] .'">';
echo '<div title="Click to toggle" class="handlediv"><br></div><h3 class="hndle"><span>' . $acf['title'] . '</span></h3>';
echo '<div class="inside">';
}
$fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']);
if( is_array($fields) ) { foreach( $fields as $field ) {
if( !$field['type'] || $field['type'] == 'null' ) continue;
if( !isset($field['value']) )
{
$field['value'] = apply_filters('acf/load_value', false, $options['option_name'], $field);
$field['value'] = apply_filters('acf/format_value', $field['value'], $options['option_name'], $field);
}
$required_class = "";
$required_label = "";
if( $field['required'] )
{
$required_class = ' required';
$required_label = ' <span class="required">*</span>';
}
if( $layout == 'metabox' )
{
echo '<div id="acf-' . $field['name'] . '" class="field field_type-' . $field['type'] . ' field_key-' . $field['key'] . $required_class . '" data-field_name="' . $field['name'] . '" data-field_key="' . $field['key'] . '" data-field_type="' . $field['type'] . '">';
echo '<p class="label">';
echo '<label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>';
echo $field['instructions'];
echo '</p>';
$field['name'] = 'fields[' . $field['key'] . ']';
do_action('acf/create_field', $field);
echo '</div>';
}
elseif( $layout == 'div' )
{
echo '<div id="acf-' . $field['name'] . '" class="form-field field field_type-' . $field['type'] . ' field_key-' . $field['key'] . $required_class . '" data-field_name="' . $field['name'] . '" data-field_key="' . $field['key'] . '" data-field_type="' . $field['type'] . '">';
echo '<label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label>';
$field['name'] = 'fields[' . $field['key'] . ']';
do_action('acf/create_field', $field );
if($field['instructions']) echo '<p class="description">' . $field['instructions'] . '</p>';
echo '</div>';
}
else
{
echo '<tr id="acf-' . $field['name'] . '" class="form-field field field_type-' . $field['type'] . ' field_key-'.$field['key'] . $required_class . '" data-field_name="' . $field['name'] . '" data-field_key="' . $field['key'] . '" data-field_type="' . $field['type'] . '">';
echo '<th valign="top" scope="row"><label for="fields[' . $field['key'] . ']">' . $field['label'] . $required_label . '</label></th>';
echo '<td>';
$field['name'] = 'fields[' . $field['key'] . ']';
do_action('acf/create_field', $field );
if($field['instructions']) echo '<p class="description">' . $field['instructions'] . '</p>';
echo '</td>';
echo '</tr>';
}
}}
if( $layout == 'metabox' )
{
echo '</div></div>';
}
if( $options['page_action'] == "edit" && $options['page_type'] == 'user' )
{
echo '</tbody></table>';
}
}
}
die();
}
function delete_term( $term, $tt_id, $taxonomy, $deleted_term )
{
global $wpdb;
$values = $wpdb->query($wpdb->prepare(
"DELETE FROM $wpdb->options WHERE option_name LIKE %s",
'%' . $taxonomy . '_' . $term . '%'
));
}
}
new acf_everything_fields();
?>