register_importer(); } /** * Register the Importer * the regular Importer skips post meta for the menu items * * @access private * @return void */ public function register_importer() { // Register the new importer if( defined( 'WP_LOAD_IMPORTERS' ) ) { require_once( plugin_dir_path( __FILE__ ) . 'class.aMNav_Menu_Control_Import.php' ); // Register the custom importer we've created. $control_import = new aMNav_Menu_control_Import(); register_importer( 'amnav_menu_control', __( 'aMNav Menu control', 'amnav-menu-control', 'amnav-menu-control', 'aMemberMenu', 'amnav-menu-control' ), sprintf( __( 'Import %samnav menu control%s and other menu item meta skipped by the default importer', 'amnav-menu-control', 'amnav-menu-control', 'aMemberMenu', 'amnav-menu-control' ), '', '' ), array( $control_import, 'dispatch' ) ); } } /** * Make Plugin Translation-ready * CALLBACK FUNCTION FOR: add_action( 'plugins_loaded', array( $this,'load_text_domain')); * @since 1.0 */ public function load_text_domain() { load_plugin_textdomain( 'amnav-menu-control', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); } /** * Add docu link * @since 1.0 */ public function add_action_links( $plugin_meta, $plugin_file, $plugin_data, $status ) { if( $plugin_file == 'amnav-menu-control/amnav-menu-control.php' ) { $plugin_meta[] = sprintf( '%s', __( 'FAQ', 'amnav-menu-control', 'amnav-menu-control', 'aMemberMenu', 'amnav-menu-control' ) ); $plugin_meta[] = '' . __( 'Donate', 'amnav-menu-control', 'amnav-menu-control', 'aMemberMenu', 'amnav-menu-control' ) . ''; } return $plugin_meta; } /** * Override the Admin Menu Walker * @since 1.0 */ public function edit_nav_menu_walker( $walker ) { return 'Walker_Nav_Menu_Edit_Roles'; } /** * Add fields to hook added in Walker * This will allow us to play nicely with any other plugin that is adding the same hook * * @params obj $item - the menu item * @params array $args * * @since 1.0 */ public function custom_fields( $item_id, $item, $depth, $args ) { /* Get the control saved for the post. */ $products = Am_Lite::getInstance()->getProducts(); // by default nothing is checked (will match "everyone" radio) $logged_in_out = ''; /* Get the options saved for the post. */ $control = get_post_meta( $item->ID, '_amnav_menu_control', true ); // specific control are saved as an array, so "in" or an array equals "in" is checked if( is_array( $control ) || $control == 'in' ) { $logged_in_out = 'in'; } else if( $control == 'out' ) { $logged_in_out = 'out'; } // the specific control to check $checked_control = is_array( $control ) ? $control : false; // whether to display the role checkboxes $hidden = $logged_in_out == 'in' ? '' : 'display: none;'; ?>
getProducts(); // verify this came from our screen and with proper authorization. if( ! isset( $_POST['amnav-menu-control-nonce'] ) || ! wp_verify_nonce( $_POST['amnav-menu-control-nonce'], 'amnav-menu-nonce-name' ) ) { return; } $saved_data = false; if( isset( $_POST['amnav-menu-logged-in-out'][ $menu_item_db_id ] ) && $_POST['amnav-menu-logged-in-out'][ $menu_item_db_id ] == 'in' && ! empty ( $_POST['amnav-menu-control'][ $menu_item_db_id ] ) ) { $custom_control = array(); // only save allowed control foreach( $_POST['amnav-menu-control'][ $menu_item_db_id ] as $control ) { $control = intval( $control ); if( array_key_exists( $control, $products ) ) { $custom_control[] = $control; } } if( ! empty ( $custom_control ) ) { $saved_data = $custom_control; } } else if( isset( $_POST['amnav-menu-logged-in-out'][ $menu_item_db_id ] ) && in_array( $_POST['amnav-menu-logged-in-out'][ $menu_item_db_id ], array( 'in', 'out' ) ) ) { $saved_data = ( $_POST['amnav-menu-logged-in-out'][ $menu_item_db_id ] == 'in' ) ? 'in' : 'out'; } if( $saved_data ) { update_post_meta( $menu_item_db_id, '_amnav_menu_control', $saved_data ); } else { delete_post_meta( $menu_item_db_id, '_amnav_menu_control' ); } } /** * Adds value of new field to $item object * is be passed to Walker_Nav_Menu_Edit_Custom * @since 1.0 */ public function setup_nav_item( $menu_item ) { $control = get_post_meta( $menu_item->ID, '_amnav_menu_control', true ); if( ! empty( $control ) ) { $menu_item->control = $control; } return $menu_item; } /** * Exclude menu items via wp_get_nav_menu_items filter * this fixes plugin's incompatibility with theme's that use their own custom Walker * Thanks to Evan Stein @vanpop http://vanpop.com/ * @since 1.0 */ public function exclude_menu_items( $items ) { /* Get the control saved for the post. */ //$products = Am_Lite::getInstance()->getProducts(); $hide_children_of = array(); // Iterate over the items to search and destroy foreach( $items as $key => $item ) { $visible = true; // hide any item that is the child of a hidden item if( in_array( $item->menu_item_parent, $hide_children_of ) ) { $visible = false; $hide_children_of[] = $item->ID; // for nested menus } // check any item that has NMR control set if( $visible && isset( $item->control ) ) { // check all logged in, all logged out, or role switch( $item->control ) { case 'in' : $visible = Am_Lite::getInstance()->isLoggedIn() ? true : false; break; case 'out' : $visible = ! Am_Lite::getInstance()->isLoggedIn() ? true : false; break; default: $visible = false; if( Am_Lite::getInstance()->isLoggedIn() ) { $visible = true; if( is_array( $item->control ) && ! empty( $item->control ) ) { foreach( $item->control as $control ) { if( ! Am_Lite::getInstance()->haveSubscriptions( $control ) ) { $visible = false; break; } } } } break; } } // add filter to work with plugins that don't use traditional control $visible = apply_filters( 'nav_menu_control_item_visibility', $visible, $item ); // unset non-visible item if( ! $visible ) { $hide_children_of[] = $item->ID; // store ID of item unset( $items[ $key ] ); } } return $items; } /** * Maybe upgrade * * @access public * @return void */ public function maybe_upgrade() { $db_version = get_option( 'amnav_menu_control_db_version', false ); if( $db_version === false ) { update_option( 'amnav_menu_control_db_version', self::VERSION ); } } } // end class endif; // class_exists check