true ), 'objects' ); if ( ! empty( $post_types ) ) { foreach ( $post_types as $post_type => $data ) { if ( current_user_can( $data->cap->edit_posts ) ) $types[ $data->name ] = array( 'title' => $data->labels->name, 'url' => admin_url( 'edit.php' ), 'hidden' => array( 'post_type' => $data->name ) ); } } /* Media */ if ( current_user_can( 'upload_files' ) ) $types['media'] = array( 'title' => __( 'Media', 'ab-backend-search' ), 'url' => admin_url( 'upload.php' ) ); /* Links */ if ( current_user_can( 'manage_links' ) ) $types['links'] = array( 'title' => __( 'Links', 'ab-backend-search' ), 'url' => admin_url( 'link-manager.php' ) ); /* Comments */ if ( current_user_can( 'edit_posts' ) ) $types['comments'] = array( 'title' => __( 'Comments', 'ab-backend-search' ), 'url' => admin_url( 'edit-comments.php' ) ); /* Users */ if ( current_user_can( 'list_users' ) ) $types['users'] = array( 'title' => __( 'Users', 'ab-backend-search' ), 'url' => admin_url( 'users.php' ) ); /* Installed Plugins */ if ( current_user_can( 'activate_plugins' ) ) $types['installed_plugins'] = array( 'title' => __( 'Installed Plugins', 'ab-backend-search' ), 'url' => admin_url( 'plugins.php' ) ); /* Installed Themes */ if( current_user_can( 'switch_themes' ) && current_user_can( 'edit_theme_options' ) ) $types['installed_themes'] = array( 'title' => __( 'Installed Themes', 'ab-backend-search' ), 'url' => admin_url( 'themes.php' ) ); /* New Plugins */ if ( ! is_multisite() && current_user_can( 'install_plugins' ) ) $types['new_plugins'] = array( 'title' => __( 'New Plugins', 'ab-backend-search' ), 'url' => admin_url( 'plugin-install.php' ), 'hidden' => array( 'tab' => 'search', 'type' => 'term' ) ); /* New Themes */ if ( ! is_multisite() && current_user_can( 'install_themes' ) ) $types['new_themes'] = array( 'title' => __( 'New Themes', 'ab-backend-search' ), 'url' => admin_url( 'theme-install.php' ), 'hidden' => array( 'tab' => 'search', 'type' => 'term' ) ); } else { /* Users */ if ( current_user_can( 'manage_network_users' ) ) $types['ms_users'] = array( 'title' => __( 'Users', 'ab-backend-search' ), 'url' => network_admin_url( 'users.php' ) ); /* Sites */ if ( current_user_can( 'manage_sites' ) ) $types['ms_sites'] = array( 'title' => __( 'Sites', 'ab-backend-search' ), 'url' => network_admin_url( 'sites.php' ) ); /* Installed Plugins */ if ( current_user_can( 'activate_plugins' ) ) $types['ms_installed_plugins'] = array( 'title' => __( 'Installed Plugins', 'ab-backend-search' ), 'url' => network_admin_url( 'plugins.php' ) ); /* Installed Themes */ if ( current_user_can( 'install_themes' ) ) $types['ms_installed_themes'] = array( 'title' => __( 'Installed Themes', 'ab-backend-search' ), 'url' => network_admin_url( 'themes.php' ) ); /* New Plugins */ if ( current_user_can( 'install_plugins' ) ) $types['ms_new_plugins'] = array( 'title' => __( 'New Plugins', 'ab-backend-search' ), 'url' => network_admin_url( 'plugin-install.php' ), 'hidden' => array( 'tab' => 'search', 'type' => 'term' ) ); /* New Themes */ if ( current_user_can( 'manage_network_themes' ) ) $types['ms_new_themes'] = array( 'title' => __( 'New Themes', 'ab-backend-search' ), 'url' => network_admin_url( 'theme-install.php' ), 'hidden' => array( 'tab' => 'search', 'type' => 'term' ) ); } if ( ! empty( self::$search_types ) ) $types = array_merge( $types, (array) self::$search_types ); return apply_filters( 'ab_backend_search_types', $types ); } /** * Helper function to add hidden fields as data attributes. * * @since 0.1.0 * * @param array $fields Hidden fields * @return string HTML data attributes */ private static function _render_hidden_fields( $fields ) { $hidden = ''; if ( empty( $fields ) ) return $hidden; foreach( $fields as $name => $value ) $hidden .= sprintf( ' data-%s="%s"', esc_attr( $name ), esc_attr( $value ) ); return $hidden; } /** * Renders the search form. * * @since 0.1.1 * */ private static function _render_search_form() { $radio = $c_url = $c_hidden = ''; $first = true; // Build radio and hidden inputs foreach( self::$search_types as $type => $data ) { $hidden = empty( $data['hidden'] ) ? '' : self::_render_hidden_fields( $data['hidden'] ); $radio .= sprintf( '', esc_attr( $data['url'] ), $hidden, checked( $first, true, false ), $data['title'] ); if ( $first ) { $c_url = $data['url']; if ( ! empty( $data['hidden'] ) ) foreach ( $data['hidden'] as $name => $value ) $c_hidden .= ""; $first = false; } } // Build search form $form = '
'; return $form; } /** * Adds the search form to the Admin Bar. * * @since 0.1.0 * * @param array $wp_admin_bar Admin bar object */ public static function add_search_form( $wp_admin_bar ) { // Add form to Admin Bar $wp_admin_bar->add_menu( array( 'parent' => 'top-secondary', 'id' => 'search', 'title' => self::_render_search_form(), 'meta' => array( 'class' => 'admin-bar-search hide-if-no-js' ) ) ); } } // Please load. Thanks. add_action( 'admin_init', array( 'AB_Backend_Search', 'init' ), 20 );