Skip to content
This repository
Browse code

Exclusion conditions

  • Loading branch information...
commit 32faab1bfe9bc0b74cce2314709b40264b5d73be 1 parent 69606fa
authored November 09, 2012

Showing 1 changed file with 79 additions and 28 deletions. Show diff stats Hide diff stats

  1. 107  demo/better-include.php
107  demo/better-include.php
@@ -27,6 +27,12 @@
27 27
 		'template' => array( 'fullwidth.php', 'simple.php' ),
28 28
 		'parent'   => array( 10 )
29 29
 	),
  30
+	'not_on' => array(
  31
+		// 'id'       => array( 10 ),
  32
+		// 'slug'  => array( 'news', 'blog' ),
  33
+		// 'template' => array( 'fullwidth.php', 'simple.php' ),
  34
+		'parent'   => array( 0 )
  35
+	)
30 36
 );
31 37
 
32 38
 /**
@@ -41,7 +47,10 @@ function rw_register_meta_boxes()
41 47
 	// Make sure there's no errors when the plugin is deactivated or during upgrade
42 48
 	if ( class_exists( 'RW_Meta_Box' ) ) {
43 49
 		foreach ( $meta_boxes as $meta_box ) {
44  
-			if ( isset( $meta_box['only_on'] ) && ! rw_maybe_include( $meta_box['only_on'] ) ) {
  50
+			if( isset( $meta_box[ 'not_on' ] ) && !rw_maybe_include( $meta_box[ 'not_on' ], 0 ) ) {
  51
+				continue;
  52
+			}
  53
+			if( isset( $meta_box['only_on'] ) && !rw_maybe_include( $meta_box['only_on'], 1 ) ) {
45 54
 				continue;
46 55
 			}
47 56
 
@@ -57,7 +66,7 @@ function rw_register_meta_boxes()
57 66
  *
58 67
  * @return bool
59 68
  */
60  
-function rw_maybe_include( $conditions ) {
  69
+function rw_maybe_include( $conditions, $bool = -1 ) {
61 70
 	// Include in back-end only
62 71
 	if ( ! defined( 'WP_ADMIN' ) || ! WP_ADMIN ) {
63 72
 		return false;
@@ -80,40 +89,82 @@ function rw_maybe_include( $conditions ) {
80 89
 
81 90
 	$post_id = (int) $post_id;
82 91
 	$post    = get_post( $post_id );
83  
-
84  
-	foreach ( $conditions as $cond => $v ) {
85  
-		// Catch non-arrays too
86  
-		if ( ! is_array( $v ) ) {
87  
-			$v = array( $v );
88  
-		}
89  
-
90  
-		switch ( $cond ) {
91  
-			case 'id':
92  
-				if ( in_array( $post_id, $v ) ) {
93  
-					return true;
  92
+	switch( $bool ) {
  93
+		// if we're including (only_on)
  94
+		case 1:
  95
+			foreach ( $conditions as $cond => $v ) {
  96
+				// Catch non-arrays too
  97
+				if ( ! is_array( $v ) ) {
  98
+					$v = array( $v );
94 99
 				}
95  
-			break;
96  
-			case 'parent':
97  
-				$post_parent = $post->post_parent;
98  
-				if ( in_array( $post_parent, $v ) ) {
99  
-					return true;
  100
+
  101
+				switch ( $cond ) {
  102
+					case 'id':
  103
+						if ( in_array( $post_id, $v ) ) {
  104
+							return true;
  105
+						}
  106
+					break;
  107
+					case 'parent':
  108
+						$post_parent = $post->post_parent;
  109
+						if ( in_array( $post_parent, $v ) ) {
  110
+							return true;
  111
+						}
  112
+					break;
  113
+					case 'slug':
  114
+						$post_slug = $post->post_name;
  115
+						if ( in_array( $post_slug, $v ) ) {
  116
+							return true;
  117
+						}
  118
+					break;
  119
+					case 'template':
  120
+						$template = get_post_meta( $post_id, '_wp_page_template', true );
  121
+						if ( in_array( $template, $v ) ) {
  122
+							return true;
  123
+						}
  124
+					break;
100 125
 				}
  126
+			}
101 127
 			break;
102  
-			case 'slug':
103  
-				$post_slug = $post->post_name;
104  
-				if ( in_array( $post_slug, $v ) ) {
105  
-					return true;
  128
+		// when we're excluding (not_on)
  129
+		case 0:
  130
+			foreach ( $conditions as $cond => $v ) {
  131
+				// Catch non-arrays too
  132
+				if ( ! is_array( $v ) ) {
  133
+					$v = array( $v );
106 134
 				}
107  
-			break;
108  
-			case 'template':
109  
-				$template = get_post_meta( $post_id, '_wp_page_template', true );
110  
-				if ( in_array( $template, $v ) ) {
111  
-					return true;
  135
+
  136
+				switch ( $cond ) {
  137
+					case 'id':
  138
+						if ( !in_array( $post_id, $v ) ) {
  139
+							return true;
  140
+						}
  141
+					break;
  142
+					case 'parent':
  143
+						$post_parent = $post->post_parent;
  144
+						if ( !in_array( $post_parent, $v ) ) {
  145
+							return true;
  146
+						}
  147
+					break;
  148
+					case 'slug':
  149
+						$post_slug = $post->post_name;
  150
+						if ( !in_array( $post_slug, $v ) ) {
  151
+							return true;
  152
+						}
  153
+					break;
  154
+					case 'template':
  155
+						$template = get_post_meta( $post_id, '_wp_page_template', true );
  156
+						if ( !in_array( $template, $v ) ) {
  157
+							return true;
  158
+						}
  159
+					break;
112 160
 				}
  161
+			}
113 162
 			break;
114  
-		}
  163
+		default:
  164
+			return true;
115 165
 	}
116 166
 
  167
+
117 168
 	// If no condition matched
118 169
 	return false;
119 170
 }

0 notes on commit 32faab1

Please sign in to comment.
Something went wrong with that request. Please try again.