Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: rilwis/meta-box
base: 69606fa382
...
head fork: rilwis/meta-box
compare: 6e6fa3371c
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 2 contributors
Commits on Nov 09, 2012
Gabor Javorszky Exclusion conditions 32faab1
@rilwis Merge pull request #176 from javorszky/master
Exclusion options to meta boxes
6e6fa33
Showing with 79 additions and 28 deletions.
  1. +79 −28 demo/better-include.php
View
107 demo/better-include.php
@@ -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 )
+ )
);
/**
@@ -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;
}
@@ -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;
@@ -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;
}

No commit comments for this range

Something went wrong with that request. Please try again.