• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 14716683875

28 Apr 2025 07:58PM UTC coverage: 84.287% (+1.6%) from 82.648%
14716683875

push

github

actions-user
release: merge develop into master for v2.3.0

15905 of 18870 relevant lines covered (84.29%)

257.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.78
/src/Model/Menu.php
1
<?php
2

3
namespace WPGraphQL\Model;
4

5
use GraphQLRelay\Relay;
6

7
/**
8
 * Class Menu - Models data for Menus
9
 *
10
 * @property ?int          $count
11
 * @property ?int          $databaseId
12
 * @property ?string       $id
13
 * @property string[]|null $locations
14
 * @property ?string       $name
15
 * @property ?string       $slug
16
 *
17
 * @package WPGraphQL\Model
18
 */
19
class Menu extends Model {
20

21
        /**
22
         * Stores the incoming WP_Term object
23
         *
24
         * @var \WP_Term $data
25
         */
26
        protected $data;
27

28
        /**
29
         * Menu constructor.
30
         *
31
         * @param \WP_Term $term The incoming WP_Term object that needs modeling
32
         *
33
         * @return void
34
         */
35
        public function __construct( \WP_Term $term ) {
13✔
36
                $this->data = $term;
13✔
37
                parent::__construct();
13✔
38
        }
39

40
        /**
41
         * {@inheritDoc}
42
         *
43
         * If a Menu is not connected to a menu that's assigned to a location
44
         * it's not considered a public node.
45
         */
46
        public function is_private() {
13✔
47

48
                // If the current user can edit theme options, consider the menu public
49
                if ( current_user_can( 'edit_theme_options' ) ) {
13✔
50
                        return false;
6✔
51
                }
52

53
                $locations = get_nav_menu_locations();
10✔
54
                if ( empty( $locations ) ) {
10✔
55
                        return true;
1✔
56
                }
57
                $location_ids = array_values( $locations );
9✔
58
                if ( empty( $location_ids ) || ! in_array( $this->data->term_id, $location_ids, true ) ) {
9✔
59
                        return true;
2✔
60
                }
61

62
                return false;
7✔
63
        }
64

65
        /**
66
         * {@inheritDoc}
67
         */
68
        protected function init() {
13✔
69
                if ( empty( $this->fields ) ) {
13✔
70
                        $this->fields = [
13✔
71
                                'count'      => function () {
13✔
72
                                        return ! empty( $this->data->count ) ? absint( $this->data->count ) : null;
3✔
73
                                },
13✔
74
                                'databaseId' => function () {
13✔
75
                                        return ! empty( $this->data->term_id ) ? absint( $this->data->term_id ) : null;
13✔
76
                                },
13✔
77
                                'id'         => function () {
13✔
78
                                        return ! empty( $this->databaseId ) ? Relay::toGlobalId( 'term', (string) $this->databaseId ) : null;
13✔
79
                                },
13✔
80
                                'locations'  => function () {
13✔
81
                                        $menu_locations = get_theme_mod( 'nav_menu_locations' );
7✔
82

83
                                        if ( empty( $menu_locations ) || ! is_array( $menu_locations ) ) {
7✔
84
                                                return null;
1✔
85
                                        }
86

87
                                        $locations = null;
6✔
88
                                        foreach ( $menu_locations as $location => $id ) {
6✔
89
                                                if ( absint( $id ) === ( $this->data->term_id ) ) {
6✔
90
                                                        $locations[] = $location;
6✔
91
                                                }
92
                                        }
93

94
                                        return $locations;
6✔
95
                                },
13✔
96
                                'name'       => function () {
13✔
97
                                        return ! empty( $this->data->name ) ? $this->data->name : null;
9✔
98
                                },
13✔
99
                                'slug'       => function () {
13✔
100
                                        return ! empty( $this->data->slug ) ? urldecode( $this->data->slug ) : null;
7✔
101
                                },
13✔
102

103
                                // Deprecated.
104
                                'menuId'     => function () {
13✔
105
                                        return $this->databaseId;
×
106
                                },
13✔
107
                        ];
13✔
108
                }
109
        }
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc