If Menu – Visibility control for Menu Items - Version 0.1

Version Description

Plugin release. Included basic menu conditional statements

Download this release

Release Info

Developer andrei.igna
Plugin Icon 128x128 If Menu – Visibility control for Menu Items
Version 0.1
Comparing to
See all releases

Version 0.1

Files changed (4) hide show
  1. conditions.php +83 -0
  2. if-menu.js +7 -0
  3. if-menu.php +179 -0
  4. readme.txt +58 -0
conditions.php ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ add_filter( 'if_menu_conditions', 'if_menu_basic_conditions' );
4
+
5
+ function if_menu_basic_conditions( $conditions ) {
6
+
7
+ $conditions[] = array(
8
+ 'name' => __( 'User is logged in', 'if-menu' ),
9
+ 'condition' => 'is_user_logged_in'
10
+ );
11
+
12
+ $conditions[] = array(
13
+ 'name' => __( 'User is Admin', 'if-menu' ),
14
+ 'condition' => 'if_menu_basic_condition_admin'
15
+ );
16
+
17
+ $conditions[] = array(
18
+ 'name' => __( 'User is Editor', 'if-menu' ),
19
+ 'condition' => 'if_menu_basic_condition_editor'
20
+ );
21
+
22
+ $conditions[] = array(
23
+ 'name' => __( 'User is Subscriber', 'if-menu' ),
24
+ 'condition' => 'if_menu_basic_condition_subscriber'
25
+ );
26
+
27
+ $conditions[] = array(
28
+ 'name' => __( 'User is Author', 'if-menu' ),
29
+ 'condition' => 'if_menu_basic_condition_author'
30
+ );
31
+
32
+ $conditions[] = array(
33
+ 'name' => __( 'User is Contributor', 'if-menu' ),
34
+ 'condition' => 'if_menu_basic_condition_contributor'
35
+ );
36
+
37
+ $conditions[] = array(
38
+ 'name' => __( 'Front Page', 'if-menu' ),
39
+ 'condition' => 'is_front_page'
40
+ );
41
+
42
+ $conditions[] = array(
43
+ 'name' => __( 'Single Post', 'if-menu' ),
44
+ 'condition' => 'is_single'
45
+ );
46
+
47
+ $conditions[] = array(
48
+ 'name' => __( 'Page', 'if-menu' ),
49
+ 'condition' => 'is_page'
50
+ );
51
+
52
+ return $conditions;
53
+ }
54
+
55
+ function if_menu_basic_condition_admin() {
56
+ global $current_user;
57
+ if( is_user_logged_in() ) return in_array( 'administrator', $current_user->roles );
58
+ return false;
59
+ }
60
+
61
+ function if_menu_basic_condition_editor() {
62
+ global $current_user;
63
+ if( is_user_logged_in() ) foreach( array( 'administrator', 'editor' ) as $role ) if( in_array( $role, $current_user->roles ) ) return true;
64
+ return false;
65
+ }
66
+
67
+ function if_menu_basic_condition_author() {
68
+ global $current_user;
69
+ if( is_user_logged_in() ) foreach( array( 'administrator', 'editor', 'author' ) as $role ) if( in_array( $role, $current_user->roles ) ) return true;
70
+ return false;
71
+ }
72
+
73
+ function if_menu_basic_condition_contributor() {
74
+ global $current_user;
75
+ if( is_user_logged_in() ) foreach( array( 'administrator', 'editor', 'author', 'contributor' ) as $role ) if( in_array( $role, $current_user->roles ) ) return true;
76
+ return false;
77
+ }
78
+
79
+ function if_menu_basic_condition_subscriber() {
80
+ global $current_user;
81
+ if( is_user_logged_in() ) foreach( array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ) as $role ) if( in_array( $role, $current_user->roles ) ) return true;
82
+ return false;
83
+ }
if-menu.js ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ jQuery( function( $ ) {
2
+
3
+ $( '.menu-item-if-menu-enable' ).change( function() {
4
+ $( this ).closest( '.if-menu-enable' ).next().toggle( $( this ).prop( 'checked' ) );
5
+ } ).trigger( 'change' );
6
+
7
+ } );
if-menu.php ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: If Menu
4
+ Plugin URI: http://morewp.net/plugin/if-menu
5
+ Description: Show/hide menu items with conditional statements
6
+ Version: 0.1
7
+ Author: More WordPress
8
+ Author URI: http://morewp.net
9
+ License: GPL2
10
+ */
11
+
12
+ /* Copyright 2012 More WordPress (email: hello@morewp.net)
13
+
14
+ This program is free software; you can redistribute it and/or modify
15
+ it under the terms of the GNU General Public License, version 2, as
16
+ published by the Free Software Foundation.
17
+
18
+ This program is distributed in the hope that it will be useful,
19
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ GNU General Public License for more details.
22
+
23
+ You should have received a copy of the GNU General Public License
24
+ along with this program; if not, write to the Free Software
25
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
+ */
27
+
28
+
29
+ class If_Menu {
30
+
31
+ protected static $has_custom_walker = null;
32
+
33
+ public static function init() {
34
+ self::$has_custom_walker = 'Walker_Nav_Menu_Edit' !== apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit' );
35
+
36
+ if( is_admin() && ! self::$has_custom_walker ) {
37
+ add_action( 'admin_init', 'If_Menu::admin_init' );
38
+ add_action( 'wp_update_nav_menu_item', 'If_Menu::wp_update_nav_menu_item', 10, 2 );
39
+ add_filter( 'wp_edit_nav_menu_walker', create_function( '', 'return "If_Menu_Walker_Nav_Menu_Edit";' ) );
40
+ } elseif( is_admin() && self::$has_custom_walker ) {
41
+ add_action( 'admin_notices', 'If_Menu::admin_notices' );
42
+ } elseif( ! is_admin() && ! self::$has_custom_walker ) {
43
+ add_filter( 'wp_get_nav_menu_items', 'If_Menu::wp_get_nav_menu_items' );
44
+ }
45
+ }
46
+
47
+ public static function admin_notices() {
48
+ global $pagenow;
49
+ if( current_user_can( 'edit_theme_options' ) && ( $pagenow == 'plugins.php' || $pagenow == 'nav-menus.php' ) ) {
50
+ echo '<div class="updated"><p>' . __( '<b>If Menu</b> plugin detected another plugin(s) that modify the menus, and to don\'t break them <b>If Menu</b> plugin is disabled.', 'if-menu' ) . '</p></div>';
51
+ }
52
+ }
53
+
54
+ public static function get_conditions( $for_testing = false ) {
55
+ $conditions = apply_filters( 'if_menu_conditions', array() );
56
+
57
+ if( $for_testing ) {
58
+ $c2 = array();
59
+ foreach( $conditions as $condition ) $c2[$condition['name']] = $condition;
60
+ $conditions = $c2;
61
+ }
62
+
63
+ return $conditions;
64
+ }
65
+
66
+ public static function wp_get_nav_menu_items( $items ) {
67
+ $conditions = If_Menu::get_conditions( $for_testing = true );
68
+ $hidden_items = array();
69
+
70
+ foreach( $items as $key => $item ) {
71
+ if( in_array( $item->menu_item_parent, $hidden_items ) ) {
72
+ unset( $items[$key] );
73
+ $hidden_items[] = $item->ID;
74
+ } elseif( get_post_meta( $item->ID, 'if_menu_enable', true ) ) {
75
+ $condition_type = get_post_meta( $item->ID, 'if_menu_condition_type', true );
76
+ $condition = get_post_meta( $item->ID, 'if_menu_condition', true );
77
+
78
+ $condition_result = call_user_func( $conditions[$condition]['condition'] );
79
+ if( $condition_type == 'show' ) $condition_result = ! $condition_result;
80
+
81
+ if( $condition_result ) {
82
+ unset( $items[$key] );
83
+ $hidden_items[] = $item->ID;
84
+ }
85
+ }
86
+ }
87
+
88
+ return $items;
89
+ }
90
+
91
+ public static function admin_init() {
92
+ global $pagenow;
93
+ if( $pagenow == 'nav-menus.php' ) {
94
+ wp_enqueue_script( 'if-menu-js', plugins_url( 'if-menu.js' , __FILE__ ), array( 'jquery' ) );
95
+ }
96
+ }
97
+
98
+ public static function edit_menu_item_settings( $item ) {
99
+ $conditions = If_Menu::get_conditions();
100
+ $if_menu_enable = get_post_meta( $item->ID, 'if_menu_enable', true );
101
+ $if_menu_condition_type = get_post_meta( $item->ID, 'if_menu_condition_type', true );
102
+ $if_menu_condition = get_post_meta( $item->ID, 'if_menu_condition', true );
103
+ ob_start();
104
+ ?>
105
+
106
+ <p class="if-menu-enable description description-wide">
107
+ <label>
108
+ <input <?php checked( $if_menu_enable, 1 ) ?> type="checkbox" value="1" class="menu-item-if-menu-enable" name="menu-item-if-menu-enable[<?php echo $item->ID; ?>]" />
109
+ <?php _e( 'Enable Conditional Logic', 'if-menu' ) ?>
110
+ </label>
111
+ </p>
112
+
113
+ <p class="if-menu-condition description description-wide">
114
+ <select id="edit-menu-item-if-menu-condition-type-<?php echo $item->ID; ?>" name="menu-item-if-menu-condition-type[<?php echo $item->ID; ?>]">
115
+ <option <?php selected( 'show', $if_menu_condition_type ) ?> value="show"><?php _e( 'Show', 'if-menu' ) ?></option>
116
+ <option <?php selected( 'hide', $if_menu_condition_type ) ?> value="hide"><?php _e( 'Hide', 'if-menu' ) ?></option>
117
+ </select>
118
+ <?php _e('if', 'if-menu'); ?>
119
+ <select id="edit-menu-item-if-menu-condition-<?php echo $item->ID; ?>" name="menu-item-if-menu-condition[<?php echo $item->ID; ?>]">
120
+ <?php foreach( $conditions as $condition ): ?>
121
+ <option <?php selected( $condition['name'], $if_menu_condition ) ?>><?php echo $condition['name']; ?></option>
122
+ <?php endforeach ?>
123
+ </select>
124
+ </p>
125
+
126
+ <?php
127
+ $html = ob_get_clean();
128
+ return $html;
129
+ }
130
+
131
+ public static function wp_update_nav_menu_item( $menu_id, $menu_item_db_id ) {
132
+ $if_menu_enable = isset( $_POST['menu-item-if-menu-enable'][$menu_item_db_id] ) && $_POST['menu-item-if-menu-enable'][$menu_item_db_id] == 1;
133
+ update_post_meta( $menu_item_db_id, 'if_menu_enable', $if_menu_enable ? 1 : 0 );
134
+
135
+ if( $if_menu_enable ) {
136
+ update_post_meta( $menu_item_db_id, 'if_menu_condition_type', $_POST['menu-item-if-menu-condition-type'][$menu_item_db_id] );
137
+ update_post_meta( $menu_item_db_id, 'if_menu_condition', $_POST['menu-item-if-menu-condition'][$menu_item_db_id] );
138
+ }
139
+ }
140
+
141
+ }
142
+
143
+
144
+
145
+ /* ------------------------------------------------
146
+ Custom Walker for nav items
147
+ ------------------------------------------------ */
148
+
149
+ require_once( ABSPATH . 'wp-admin/includes/nav-menu.php' );
150
+
151
+ class If_Menu_Walker_Nav_Menu_Edit extends Walker_Nav_Menu_Edit {
152
+
153
+ function start_el(&$output, $item, $depth, $args) {
154
+ $desc_snipp = '<div class="menu-item-actions description-wide submitbox">';
155
+ parent::start_el($output, $item, $depth, $args);
156
+
157
+ $pos = strrpos( $output, $desc_snipp );
158
+ if( $pos !== false ) {
159
+ $output = substr_replace($output, If_Menu::edit_menu_item_settings( $item ) . $desc_snipp, $pos, strlen( $desc_snipp ) );
160
+ }
161
+ }
162
+
163
+ }
164
+
165
+
166
+
167
+ /* ------------------------------------------------
168
+ Include default conditions for menu items
169
+ ------------------------------------------------ */
170
+
171
+ include 'conditions.php';
172
+
173
+
174
+
175
+ /* ------------------------------------------------
176
+ Run the plugin
177
+ ------------------------------------------------ */
178
+
179
+ add_action( 'init', 'If_Menu::init' );
readme.txt ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Plugin Name ===
2
+ Contributors: andrei.igna
3
+ Tags: menu, if, conditional, statements, hide, show, dispaly
4
+ Requires at least: 3.0.0
5
+ Tested up to: 3.4
6
+ Stable tag: trunk
7
+ License: GPL2
8
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
+
10
+ Show/hide menu items with conditional statements
11
+
12
+ == Description ==
13
+
14
+ Simple plugin that adds extra functionality to Menu Items. The plugin will allow to show or hide menu items based on condition statements (Is single page, User is Logged In, etc).
15
+
16
+ The management is very easy, each menu item will have a "Enable Conditional Logic" check, that will allow to select a conditional statement (Example in Screenshots)
17
+
18
+ Basic conditional statements are included in the plugin, other will be included in future releases or can be added nu another plugin or theme.
19
+
20
+ Example of adding a new conditional statement is described in the FAQ section
21
+
22
+ == Installation ==
23
+
24
+ To install the plugin, follow the steps below
25
+
26
+ 1. Upload `if-menu` to the `/wp-content/plugins/` directory
27
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
28
+ 3. Enable conditional statements for your Menu Items in Appearance -> Menus page
29
+
30
+ == Frequently Asked Questions ==
31
+
32
+ = How can I add a conditinal statement =
33
+
34
+ New conditional statements can be added by any other plugin or theme.
35
+
36
+ Example of adding a new conditional statement
37
+
38
+ ``
39
+ add_filter( 'if_menu_conditions', 'my_new_menu_conditions' );
40
+
41
+ function my_new_menu_conditions( $conditions ) {
42
+ $conditions[] = array(
43
+ 'name' => 'If single custom-post-type', // name of the condition
44
+ 'condition' => function() { // callback - must return TRUE or FALSE
45
+ return is_singular( 'my-custom-post-type' );
46
+ }
47
+ );
48
+ }
49
+ ``
50
+
51
+ == Screenshots ==
52
+
53
+ 1. Enable conditional statements for Menu Items
54
+
55
+ == Changelog ==
56
+
57
+ = 0.1 =
58
+ Plugin release. Included basic menu conditional statements