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

wp-graphql / wp-graphql / 15710056976

17 Jun 2025 02:27PM UTC coverage: 84.17% (-0.1%) from 84.287%
15710056976

push

github

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

15925 of 18920 relevant lines covered (84.17%)

258.66 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
 * @extends \WPGraphQL\Model\Model<\WP_Term>
20
 */
21
class Menu extends Model {
22
        /**
23
         * Menu constructor.
24
         *
25
         * @param \WP_Term $term The incoming WP_Term object that needs modeling
26
         *
27
         * @return void
28
         */
29
        public function __construct( \WP_Term $term ) {
13✔
30
                $this->data = $term;
13✔
31
                parent::__construct();
13✔
32
        }
33

34
        /**
35
         * {@inheritDoc}
36
         *
37
         * If a Menu is not connected to a menu that's assigned to a location
38
         * it's not considered a public node.
39
         */
40
        public function is_private() {
13✔
41

42
                // If the current user can edit theme options, consider the menu public
43
                if ( current_user_can( 'edit_theme_options' ) ) {
13✔
44
                        return false;
6✔
45
                }
46

47
                $locations = get_nav_menu_locations();
10✔
48
                if ( empty( $locations ) ) {
10✔
49
                        return true;
1✔
50
                }
51
                $location_ids = array_values( $locations );
9✔
52
                if ( empty( $location_ids ) || ! in_array( $this->data->term_id, $location_ids, true ) ) {
9✔
53
                        return true;
2✔
54
                }
55

56
                return false;
7✔
57
        }
58

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

77
                                        if ( empty( $menu_locations ) || ! is_array( $menu_locations ) ) {
7✔
78
                                                return null;
1✔
79
                                        }
80

81
                                        $locations = null;
6✔
82
                                        foreach ( $menu_locations as $location => $id ) {
6✔
83
                                                if ( absint( $id ) === ( $this->data->term_id ) ) {
6✔
84
                                                        $locations[] = $location;
6✔
85
                                                }
86
                                        }
87

88
                                        return $locations;
6✔
89
                                },
13✔
90
                                'name'       => function () {
13✔
91
                                        return ! empty( $this->data->name ) ? $this->data->name : null;
9✔
92
                                },
13✔
93
                                'slug'       => function () {
13✔
94
                                        return ! empty( $this->data->slug ) ? urldecode( $this->data->slug ) : null;
7✔
95
                                },
13✔
96

97
                                // Deprecated.
98
                                'menuId'     => function () {
13✔
99
                                        return $this->databaseId;
×
100
                                },
13✔
101
                        ];
13✔
102
                }
103
        }
104
}
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