<?php
class acf_controller_post
{
function __construct()
{
add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'));
add_action('save_post', array($this, 'save_post'), 10, 1);
add_action('wp_ajax_acf/post/render_fields', array($this, 'ajax_render_fields'));
add_action('wp_ajax_acf/post/get_style', array($this, 'ajax_get_style'));
}
function validate_page()
{
global $pagenow, $typenow;
$return = false;
if( in_array( $pagenow, array('post.php', 'post-new.php') ) )
{
global $typenow;
if( $typenow != "acf" )
{
$return = true;
}
}
(Shopp)
if( $pagenow == "admin.php" && isset( $_GET['page'] ) && $_GET['page'] == "shopp-products" && isset( $_GET['id'] ) )
{
$return = true;
}
return $return;
}
function admin_enqueue_scripts()
{
if( ! $this->validate_page() )
{
return;
}
do_action('acf/input/admin_enqueue_scripts');
add_action('admin_head', array($this, 'admin_head'));
}
function admin_head()
{
s
global $post, $pagenow, $typenow;
if( $pagenow == "admin.php" && isset( $_GET['page'] ) && $_GET['page'] == "shopp-products" && isset( $_GET['id'] ) )
{
$typenow = "shopp_product";
}
$post_id = $post ? $post->ID : 0;
$filter = array(
'post_id' => $post_id,
'post_type' => $typenow
);
$metabox_ids = array();
$metabox_ids = apply_filters( 'acf/location/match_field_groups', $metabox_ids, $filter );
$style = '';
if( isset($metabox_ids[0]) )
{
$style = $this->get_style( $metabox_ids[0] );
}
echo '<style type="text/css" id="acf_style" >' . $style . '</style>';
do_action('acf/input/admin_head');
$acfs = apply_filters('acf/get_field_groups', array());
if( $acfs )
{
foreach( $acfs as $acf )
{
$acf['options'] = apply_filters('acf/field_group/get_options', array(), $acf['id']);
$show = in_array( $acf['id'], $metabox_ids ) ? 1 : 0;
$priority = 'high';
if( $acf['options']['position'] == 'side' )
{
$priority = 'core';
}
$priority = apply_filters('acf/input/meta_box_priority', $priority, $acf);
add_meta_box(
'acf_' . $acf['id'],
$acf['title'],
array($this, 'meta_box_input'),
$typenow,
$acf['options']['position'],
$priority,
array( 'field_group' => $acf, 'show' => $show, 'post_id' => $post_id )
);
}
}
add_action('edit_form_after_title', array($this, 'edit_form_after_title'));
add_filter( 'is_protected_meta', array($this, 'is_protected_meta'), 10, 3 );
}
function edit_form_after_title()
{
s
global $post, $wp_meta_boxes;
do_meta_boxes( get_current_screen(), 'acf_after_title', $post);
unset( $wp_meta_boxes['post']['acf_after_title'] );
?>
<div style="display:none">
<input type="hidden" name="acf_has_changed" id="acf-has-changed" value="0" />
</div>
<?php
}
function meta_box_input( $post, $args )
{
extract( $args );
$class = 'acf_postbox ' . $args['field_group']['options']['layout'];
$toggle_class = 'acf_postbox-toggle';
if( ! $args['show'] )
{
$class .= ' acf-hidden';
$toggle_class .= ' acf-hidden';
}
if( $args['show'] )
{
$fields = apply_filters('acf/field_group/get_fields', array(), $args['field_group']['id']);
do_action('acf/create_fields', $fields, $args['post_id']);
}
else
{
echo '<div class="acf-replace-with-fields"><div class="acf-loading"></div></div>';
}
echo '<div style="display:none">';
echo '<input type="hidden" name="acf_nonce" value="' . wp_create_nonce( 'input' ) . '" />';
?>
<script type="text/javascript">
(function($) {
$('#<?php echo $id; ?>').addClass('<?php echo $class; ?>').removeClass('hide-if-js');
$('#adv-settings label[for="<?php echo $id; ?>-hide"]').addClass('<?php echo $toggle_class; ?>');
})(jQuery);
</script>
<?php
echo '</div>';
}
function get_style( $acf_id )
{
$options = apply_filters('acf/field_group/get_options', array(), $acf_id);
$html = '';
if( in_array('permalink', $options['hide_on_screen']) )
{
$html .= '#edit-slug-box {display: none;} ';
}
if( in_array('the_content', $options['hide_on_screen']) )
{
$html .= '#postdivrich {display: none;} ';
}
if( in_array('excerpt', $options['hide_on_screen']) )
{
$html .= '#postexcerpt, #screen-meta label[for=postexcerpt-hide] {display: none;} ';
}
if( in_array('custom_fields', $options['hide_on_screen']) )
{
$html .= '#postcustom, #screen-meta label[for=postcustom-hide] { display: none; } ';
}
if( in_array('discussion', $options['hide_on_screen']) )
{
$html .= '#commentstatusdiv, #screen-meta label[for=commentstatusdiv-hide] {display: none;} ';
}
if( in_array('comments', $options['hide_on_screen']) )
{
$html .= '#commentsdiv, #screen-meta label[for=commentsdiv-hide] {display: none;} ';
}
if( in_array('slug', $options['hide_on_screen']) )
{
$html .= '#slugdiv, #screen-meta label[for=slugdiv-hide] {display: none;} ';
}
if( in_array('author', $options['hide_on_screen']) )
{
$html .= '#authordiv, #screen-meta label[for=authordiv-hide] {display: none;} ';
}
if( in_array('format', $options['hide_on_screen']) )
{
$html .= '#formatdiv, #screen-meta label[for=formatdiv-hide] {display: none;} ';
}
if( in_array('featured_image', $options['hide_on_screen']) )
{
$html .= '#postimagediv, #screen-meta label[for=postimagediv-hide] {display: none;} ';
}
if( in_array('revisions', $options['hide_on_screen']) )
{
$html .= '#revisionsdiv, #screen-meta label[for=revisionsdiv-hide] {display: none;} ';
}
if( in_array('categories', $options['hide_on_screen']) )
{
$html .= '#categorydiv, #screen-meta label[for=categorydiv-hide] {display: none;} ';
}
if( in_array('tags', $options['hide_on_screen']) )
{
$html .= '#tagsdiv-post_tag, #screen-meta label[for=tagsdiv-post_tag-hide] {display: none;} ';
}
if( in_array('send-trackbacks', $options['hide_on_screen']) )
{
$html .= '#trackbacksdiv, #screen-meta label[for=trackbacksdiv-hide] {display: none;} ';
}
return $html;
}
function ajax_get_style()
{
$options = array(
'acf_id' => 0,
'nonce' => ''
);
$options = array_merge($options, $_POST);
if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') )
{
die(0);
}
style
echo $this->get_style( $options['acf_id'] );
die;
}
function ajax_render_fields()
{
$options = array(
'acf_id' => 0,
'post_id' => 0,
'nonce' => ''
);
$options = array_merge($options, $_POST);
if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') )
{
die(0);
}
$acfs = apply_filters('acf/get_field_groups', array());
if( $acfs )
{
foreach( $acfs as $acf )
{
if( $acf['id'] == $options['acf_id'] )
{
$fields = apply_filters('acf/field_group/get_fields', array(), $acf['id']);
do_action('acf/create_fields', $fields, $options['post_id']);
break;
}
}
}
die();
}
function save_post( $post_id )
{
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
{
return $post_id;
}
if( !isset($_POST['acf_nonce'], $_POST['fields']) || !wp_verify_nonce($_POST['acf_nonce'], 'input') )
{
return $post_id;
}
if( isset($GLOBALS['acf_save_lock']) && $GLOBALS['acf_save_lock'] )
{
return $post_id;
}
do_action('acf/save_post', $post_id);
}
function is_protected_meta( $protected, $meta_key, $meta_type ) {
s
global $post;
if( !$protected ) {
$reference = get_field_reference( $meta_key, $post->ID );
if( substr($reference, 0, 6) === 'field_' ) {
$protected = true;
}
}
return $protected;
}
}
new acf_controller_post();
?>