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

wp-graphql / wp-graphql-woocommerce / 5604143621

pending completion
5604143621

push

github

web-flow
chore: implement WPGraphQL Coding Standards (#769)

* chore: avoid magic constants [PHPCS]

* chore: change ruleset to wp-graphql-cs and lint

* devops: HPOS added back to CI with the release of WC 3.9

---------

Co-authored-by: Geoff Taylor <geoff@axistaylor.com>

562 of 562 new or added lines in 77 files covered. (100.0%)

10309 of 12440 relevant lines covered (82.87%)

53.98 hits per line

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

24.59
/includes/model/class-wc-post.php
1
<?php
2
/**
3
 * Abstract Model - WC_Post
4
 *
5
 * Defines shared functionality for WooCommerce CPT models.
6
 *
7
 * @package WPGraphQL\WooCommerce\Model
8
 * @since 0.0.1
9
 */
10

11
namespace WPGraphQL\WooCommerce\Model;
12

13
use GraphQL\Error\UserError;
14
use WPGraphQL\Model\Post;
15

16
/**
17
 * Class WC_Post
18
 *
19
 * @property \WC_Data $wc_data
20
 * @property \WP_Post $data
21
 *
22
 * @package WPGraphQL\WooCommerce\Model
23
 */
24
abstract class WC_Post extends Post {
25
        /**
26
         * Stores the WC_Data object connected to the model.
27
         *
28
         * @var \WC_Data
29
         */
30
        protected $wc_data;
31

32
        /**
33
         * WC_Post constructor
34
         *
35
         * @param \WC_Data $data  Data object to be used by the model.
36
         *
37
         * @throws \Exception  Failed to retrieve data source post object.
38
         */
39
        public function __construct( $data ) {
40
                // Store CRUD object.
41
                $this->wc_data = $data;
66✔
42

43
                // Get WP_Post object.
44
                $post = get_post( $data->get_id() );
66✔
45

46
                // Check if product is valid.
47
                if ( ! is_object( $post ) ) {
66✔
48
                        throw new \Exception( __( 'Failed to retrieve data source post object', 'wp-graphql-woocommerce' ) );
×
49
                }
50

51
                // Add $allowed_restricted_fields.
52
                if ( ! has_filter( 'graphql_allowed_fields_on_restricted_type', [ static::class, 'add_allowed_restricted_fields' ] ) ) {
66✔
53
                        add_filter( 'graphql_allowed_fields_on_restricted_type', [ static::class, 'add_allowed_restricted_fields' ], 10, 2 );
66✔
54
                }
55

56
                // Execute Post Model constructor.
57
                parent::__construct( $post );
66✔
58
        }
59

60
        /**
61
         * Injects CRUD object fields into $allowed_restricted_fields
62
         *
63
         * @param array  $allowed_restricted_fields  The fields to allow when the data is designated as restricted to the current user.
64
         * @param string $model_name                 Name of the model the filter is currently being executed in.
65
         *
66
         * @return string[]
67
         */
68
        public static function add_allowed_restricted_fields( $allowed_restricted_fields, $model_name ) {
69
                $class_name = static::class;
1✔
70
                if ( "{$class_name}Object" === $model_name ) {
1✔
71
                        return static::get_allowed_restricted_fields( $allowed_restricted_fields );
×
72
                }
73

74
                return $allowed_restricted_fields;
1✔
75
        }
76

77
        /**
78
         * Return the fields allowed to be displayed even if this entry is restricted.
79
         *
80
         * @param array $allowed_restricted_fields  The fields to allow when the data is designated as restricted to the current user.
81
         *
82
         * @return array
83
         */
84
        protected static function get_allowed_restricted_fields( $allowed_restricted_fields = [] ) {
85
                return [
×
86
                        'isRestricted',
×
87
                        'isPrivate',
×
88
                        'isPublic',
×
89
                        'id',
×
90
                        'databaseId',
×
91
                ];
×
92
        }
93

94
        /**
95
         * Forwards function calls to WC_Data sub-class instance.
96
         *
97
         * @param string $method - function name.
98
         * @param array  $args  - function call arguments.
99
         *
100
         * @return mixed
101
         *
102
         * @throws \BadMethodCallException Method not found on WC data object.
103
         */
104
        public function __call( $method, $args ) {
105
                if ( \is_callable( [ $this->wc_data, $method ] ) ) {
3✔
106
                        return $this->wc_data->$method( ...$args );
3✔
107
                }
108

109
                $class = self::class;
×
110
                throw new \BadMethodCallException( "Call to undefined method {$method} on the {$class}" );
×
111
        }
112

113
        /**
114
         * Wrapper function for deleting
115
         *
116
         * @throws \GraphQL\Error\UserError Not authorized.
117
         *
118
         * @param boolean $force_delete Should the data be deleted permanently.
119
         * @return boolean
120
         */
121
        public function delete( $force_delete = false ) {
122
                $post_type_object = $this->post_type_object;
1✔
123
                if ( empty( $post_type_object ) ) {
1✔
124
                        throw new UserError(
×
125
                                __(
×
126
                                        'Post type object not found.',
×
127
                                        'wp-graphql-woocommerce'
×
128
                                )
×
129
                        );
×
130
                }
131

132
                if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) {
1✔
133
                        throw new UserError(
×
134
                                __(
×
135
                                        'User does not have the capabilities necessary to delete this object.',
×
136
                                        'wp-graphql-woocommerce'
×
137
                                )
×
138
                        );
×
139
                }
140

141
                return $this->wc_data->delete( $force_delete );
1✔
142
        }
143

144
        /**
145
         * Method for determining if the data should be considered private or not
146
         *
147
         * @param \WP_Post $post_object The object of the post we need to verify permissions for.
148
         *
149
         * @return bool
150
         */
151
        protected function is_post_private( $post_object = null ) {
152
                /**
153
                 * Stores the incoming post type object for the post being modeled
154
                 *
155
                 * @var null|\WP_Post_Type $post_type_object
156
                 */
157
                $post_type_object = $this->post_type_object;
×
158

159
                if ( empty( $post_object ) ) {
×
160
                        $post_object = $this->data;
×
161
                        return true;
×
162
                }
163

164
                /**
165
                 * If the status is NOT publish and the user does NOT have capabilities to edit posts,
166
                 * consider the post private.
167
                 */
168
                if ( ! isset( $post_type_object->cap->edit_posts ) || ! current_user_can( $post_type_object->cap->edit_posts ) ) {
×
169
                        return true;
×
170
                }
171

172
                /**
173
                 * If the owner of the content is the current user
174
                 */
175
                if ( ( true === $this->owner_matches_current_user() ) && 'revision' !== $post_object->post_type ) {
×
176
                        return false;
×
177
                }
178

179
                if ( 'private' === $this->data->post_status && ( ! isset( $post_type_object->cap->read_private_posts ) || ! current_user_can( $post_type_object->cap->read_private_posts ) ) ) {
×
180
                        return true;
×
181
                }
182

183
                if ( 'auto-draft' === $this->data->post_status ) {
×
184
                        $parent = get_post( (int) $this->data->post_parent );
×
185

186
                        if ( empty( $parent ) ) {
×
187
                                return true;
×
188
                        }
189

190
                        $parent_post_type_obj = $post_type_object;
×
191
                        if ( 'private' === $parent->post_status ) {
×
192
                                $cap = isset( $parent_post_type_obj->cap->read_private_posts ) ? $parent_post_type_obj->cap->read_private_posts : 'read_private_posts';
×
193
                        } else {
194
                                $cap = isset( $parent_post_type_obj->cap->edit_post ) ? $parent_post_type_obj->cap->edit_post : 'edit_post';
×
195
                        }
196

197
                        if ( ! current_user_can( $cap, $parent->ID ) ) {
×
198
                                return true;
×
199
                        }
200
                }//end if
201

202
                return false;
×
203
        }
204

205
        /**
206
         * Returns the source WP_Post instance.
207
         *
208
         * @return \WP_Post
209
         */
210
        public function as_WP_Post() {
211
                return $this->data;
×
212
        }
213

214
        /**
215
         * Returns the source WC_Data instance
216
         *
217
         * @return \WC_Data
218
         */
219
        public function as_WC_Data() {
220
                return $this->wc_data;
×
221
        }
222
}
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

© 2026 Coveralls, Inc