Comment
8 years ago
Link
8 years ago
Media
8 years ago
Post
8 years ago
User
8 years ago
Actions.php
8 years ago
AjaxValue.php
8 years ago
CustomField.php
8 years ago
Menu.php
8 years ago
Meta.php
8 years ago
Placeholder.php
8 years ago
RelationInterface.php
8 years ago
Taxonomy.php
8 years ago
WooCommercePlaceholder.php
8 years ago
Menu.php
118 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Column displaying the menus the item is used in. Supported by all object types that |
| 9 | * can be referenced in menus (i.e. posts). |
| 10 | * |
| 11 | * @since 2.2.5 |
| 12 | */ |
| 13 | class AC_Column_Menu extends AC_Column { |
| 14 | |
| 15 | public function __construct() { |
| 16 | $this->set_type( 'column-used_by_menu' ); |
| 17 | $this->set_label( __( 'Menu', 'codepress-admin-columns' ) ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * @see AC_Column::get_raw_value() |
| 22 | * @since 2.2.5 |
| 23 | */ |
| 24 | public function get_raw_value( $object_id ) { |
| 25 | return $this->get_menus( $object_id, array( 'fields' => 'ids' ) ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @return string Object type: 'post', 'page' or 'user' |
| 30 | */ |
| 31 | public function get_object_type() { |
| 32 | $object_type = $this->get_post_type(); |
| 33 | |
| 34 | if ( ! $object_type ) { |
| 35 | $object_type = $this->get_taxonomy(); |
| 36 | } |
| 37 | |
| 38 | if ( ! $object_type ) { |
| 39 | $object_type = $this->get_list_screen()->get_meta_type(); |
| 40 | } |
| 41 | |
| 42 | return $object_type; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return string |
| 47 | */ |
| 48 | public function get_item_type() { |
| 49 | $item_type = $this->get_list_screen()->get_meta_type(); |
| 50 | |
| 51 | switch ( $item_type ) { |
| 52 | case 'post' : |
| 53 | $item_type = 'post_type'; |
| 54 | break; |
| 55 | } |
| 56 | |
| 57 | return $item_type; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param int $object_id |
| 62 | * |
| 63 | * @return array |
| 64 | */ |
| 65 | public function get_menu_item_ids( $object_id ) { |
| 66 | $menu_item_ids = get_posts( array( |
| 67 | 'post_type' => 'nav_menu_item', |
| 68 | 'posts_per_page' => -1, |
| 69 | 'post_status' => 'publish', |
| 70 | 'fields' => 'ids', |
| 71 | 'meta_query' => array( |
| 72 | array( |
| 73 | 'key' => '_menu_item_object_id', |
| 74 | 'value' => $object_id, |
| 75 | ), |
| 76 | array( |
| 77 | 'key' => '_menu_item_object', |
| 78 | 'value' => $this->get_object_type(), |
| 79 | ), |
| 80 | ), |
| 81 | ) ); |
| 82 | |
| 83 | return $menu_item_ids; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param int $object_id |
| 88 | * @param array $args |
| 89 | * |
| 90 | * @return array |
| 91 | */ |
| 92 | public function get_menus( $object_id, $args = array() ) { |
| 93 | |
| 94 | $menu_item_ids = $this->get_menu_item_ids( $object_id ); |
| 95 | |
| 96 | if ( ! $menu_item_ids ) { |
| 97 | return array(); |
| 98 | } |
| 99 | |
| 100 | $menu_ids = wp_get_object_terms( $menu_item_ids, 'nav_menu', $args ); |
| 101 | |
| 102 | if ( ! $menu_ids || is_wp_error( $menu_ids ) ) { |
| 103 | return array(); |
| 104 | } |
| 105 | |
| 106 | return $menu_ids; |
| 107 | } |
| 108 | |
| 109 | public function register_settings() { |
| 110 | $this->add_setting( new AC_Settings_Column_LinkToMenu( $this ) ); |
| 111 | } |
| 112 | |
| 113 | public function is_valid() { |
| 114 | return in_array( $this->get_list_screen()->get_meta_type(), array( 'post', 'user', 'term', 'comment' ) ); |
| 115 | } |
| 116 | |
| 117 | } |
| 118 |