comment
10 years ago
link
10 years ago
media
10 years ago
post
10 years ago
user
10 years ago
acf-placeholder.php
10 years ago
actions.php
10 years ago
custom-field.php
10 years ago
default.php
10 years ago
taxonomy.php
10 years ago
used-by-menu.php
10 years ago
wc-placeholder.php
10 years ago
used-by-menu.php
148 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Column displaying the menus the item is used in. Supported by all object types that |
| 4 | * can be referenced in menus (i.e. posts). |
| 5 | * |
| 6 | * @since 2.2.5 |
| 7 | */ |
| 8 | class CPAC_Column_Used_By_Menu extends CPAC_Column { |
| 9 | |
| 10 | /** |
| 11 | * @see CPAC_Column::init() |
| 12 | * @since 2.2.5 |
| 13 | */ |
| 14 | public function init() { |
| 15 | |
| 16 | parent::init(); |
| 17 | |
| 18 | // Properties |
| 19 | $this->properties['type'] = 'column-used_by_menu'; |
| 20 | $this->properties['label'] = __( 'Used by Menu', 'codepress-admin-columns' ); |
| 21 | |
| 22 | // Options |
| 23 | $this->options['link_to_menu'] = false; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @see CPAC_Column::apply_conditional() |
| 28 | * @since 2.2.5 |
| 29 | */ |
| 30 | function apply_conditional() { |
| 31 | if ( ! $this->get_meta_type() ) { |
| 32 | return false; |
| 33 | } |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @see CPAC_Column::get_value() |
| 39 | * @since 2.2.5 |
| 40 | */ |
| 41 | function get_value( $object_id ) { |
| 42 | |
| 43 | $menus = array(); |
| 44 | if ( $menu_ids = $this->get_raw_value( $object_id ) ) { |
| 45 | foreach ( $menu_ids as $menu_id ) { |
| 46 | $term = get_term_by( 'id', $menu_id, 'nav_menu' ); |
| 47 | |
| 48 | $title = $term->name; |
| 49 | if ( 'on' == $this->options->link_to_menu ) { |
| 50 | $title = '<a href="' . esc_url( add_query_arg( array( 'menu' => $menu_id ), admin_url('nav-menus.php') ) ) . '">' . $term->name . '</a>'; |
| 51 | } |
| 52 | |
| 53 | $menus[] = $title; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return implode( ', ', $menus ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get object metatype of the storage model |
| 62 | * |
| 63 | * @since 2.2.5 |
| 64 | */ |
| 65 | function get_meta_type() { |
| 66 | $object_type = false; |
| 67 | if ( isset( $this->storage_model->taxonomy ) ) { |
| 68 | $object_type = $this->storage_model->taxonomy; |
| 69 | } |
| 70 | elseif ( isset( $this->storage_model->post_type ) ) { |
| 71 | $object_type = $this->storage_model->post_type; |
| 72 | } |
| 73 | return $object_type; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @see CPAC_Column::get_raw_value() |
| 78 | * @since 2.2.5 |
| 79 | */ |
| 80 | function get_raw_value( $object_id ) { |
| 81 | |
| 82 | $object_type = $this->get_meta_type(); |
| 83 | |
| 84 | |
| 85 | $menu_item_ids = get_posts( array( |
| 86 | 'post_type' => 'nav_menu_item', |
| 87 | 'numberposts' => -1, |
| 88 | 'post_status' => 'publish', |
| 89 | 'fields' => 'ids', |
| 90 | 'meta_query' => array( |
| 91 | array( |
| 92 | 'key' => '_menu_item_object_id', |
| 93 | 'value' => $object_id |
| 94 | ), |
| 95 | array( |
| 96 | 'key' => '_menu_item_object', |
| 97 | 'value' => $object_type |
| 98 | ), |
| 99 | ) |
| 100 | ) ); |
| 101 | |
| 102 | if ( ! $menu_item_ids ) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | $menu_ids = wp_get_object_terms( $menu_item_ids, 'nav_menu', array( 'fields' => 'ids') ); |
| 107 | if ( ! $menu_ids || is_wp_error( $menu_ids ) ) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | return $menu_ids; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @see CPAC_Column::display_settings() |
| 116 | * @since 2.2.5 |
| 117 | */ |
| 118 | public function display_settings() { |
| 119 | |
| 120 | $this->display_field_link_to_menu(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Display the settings field for selecting whether the column value should link to the corresponding post |
| 125 | * |
| 126 | * @since 2.2.5 |
| 127 | */ |
| 128 | public function display_field_link_to_menu() { |
| 129 | |
| 130 | $field_key = 'link_to_menu'; |
| 131 | ?> |
| 132 | <tr class="column_<?php echo $field_key; ?>"> |
| 133 | <?php $this->label_view( __( 'Link to menu', 'codepress-admin-columns' ), __( 'This will make the title link to the menu.', 'codepress-admin-columns' ), $field_key ); ?> |
| 134 | <td class="input"> |
| 135 | <label for="<?php $this->attr_id( $field_key ); ?>-on"> |
| 136 | <input type="radio" value="on" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>-on"<?php checked( $this->options->link_to_menu, 'on' ); ?> /> |
| 137 | <?php _e( 'Yes'); ?> |
| 138 | </label> |
| 139 | <label for="<?php $this->attr_id( $field_key ); ?>-off"> |
| 140 | <input type="radio" value="off" name="<?php $this->attr_name( $field_key ); ?>" id="<?php $this->attr_id( $field_key ); ?>-off"<?php checked( in_array( $this->options->link_to_menu, array( '', 'off' ) ) ); ?> /> |
| 141 | <?php _e( 'No'); ?> |
| 142 | </label> |
| 143 | </td> |
| 144 | </tr> |
| 145 | <?php |
| 146 | } |
| 147 | |
| 148 | } |