Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclusion options to meta boxes #176

Merged
merged 1 commit into from Nov 9, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
107 changes: 79 additions & 28 deletions demo/better-include.php
Expand Up @@ -27,6 +27,12 @@
'template' => array( 'fullwidth.php', 'simple.php' ),
'parent' => array( 10 )
),
'not_on' => array(
// 'id' => array( 10 ),
// 'slug' => array( 'news', 'blog' ),
// 'template' => array( 'fullwidth.php', 'simple.php' ),
'parent' => array( 0 )
)
);

/**
Expand All @@ -41,7 +47,10 @@ function rw_register_meta_boxes()
// Make sure there's no errors when the plugin is deactivated or during upgrade
if ( class_exists( 'RW_Meta_Box' ) ) {
foreach ( $meta_boxes as $meta_box ) {
if ( isset( $meta_box['only_on'] ) && ! rw_maybe_include( $meta_box['only_on'] ) ) {
if( isset( $meta_box[ 'not_on' ] ) && !rw_maybe_include( $meta_box[ 'not_on' ], 0 ) ) {
continue;
}
if( isset( $meta_box['only_on'] ) && !rw_maybe_include( $meta_box['only_on'], 1 ) ) {
continue;
}

Expand All @@ -57,7 +66,7 @@ function rw_register_meta_boxes()
*
* @return bool
*/
function rw_maybe_include( $conditions ) {
function rw_maybe_include( $conditions, $bool = -1 ) {
// Include in back-end only
if ( ! defined( 'WP_ADMIN' ) || ! WP_ADMIN ) {
return false;
Expand All @@ -80,40 +89,82 @@ function rw_maybe_include( $conditions ) {

$post_id = (int) $post_id;
$post = get_post( $post_id );

foreach ( $conditions as $cond => $v ) {
// Catch non-arrays too
if ( ! is_array( $v ) ) {
$v = array( $v );
}

switch ( $cond ) {
case 'id':
if ( in_array( $post_id, $v ) ) {
return true;
switch( $bool ) {
// if we're including (only_on)
case 1:
foreach ( $conditions as $cond => $v ) {
// Catch non-arrays too
if ( ! is_array( $v ) ) {
$v = array( $v );
}
break;
case 'parent':
$post_parent = $post->post_parent;
if ( in_array( $post_parent, $v ) ) {
return true;

switch ( $cond ) {
case 'id':
if ( in_array( $post_id, $v ) ) {
return true;
}
break;
case 'parent':
$post_parent = $post->post_parent;
if ( in_array( $post_parent, $v ) ) {
return true;
}
break;
case 'slug':
$post_slug = $post->post_name;
if ( in_array( $post_slug, $v ) ) {
return true;
}
break;
case 'template':
$template = get_post_meta( $post_id, '_wp_page_template', true );
if ( in_array( $template, $v ) ) {
return true;
}
break;
}
}
break;
case 'slug':
$post_slug = $post->post_name;
if ( in_array( $post_slug, $v ) ) {
return true;
// when we're excluding (not_on)
case 0:
foreach ( $conditions as $cond => $v ) {
// Catch non-arrays too
if ( ! is_array( $v ) ) {
$v = array( $v );
}
break;
case 'template':
$template = get_post_meta( $post_id, '_wp_page_template', true );
if ( in_array( $template, $v ) ) {
return true;

switch ( $cond ) {
case 'id':
if ( !in_array( $post_id, $v ) ) {
return true;
}
break;
case 'parent':
$post_parent = $post->post_parent;
if ( !in_array( $post_parent, $v ) ) {
return true;
}
break;
case 'slug':
$post_slug = $post->post_name;
if ( !in_array( $post_slug, $v ) ) {
return true;
}
break;
case 'template':
$template = get_post_meta( $post_id, '_wp_page_template', true );
if ( !in_array( $template, $v ) ) {
return true;
}
break;
}
}
break;
}
default:
return true;
}


// If no condition matched
return false;
}