@@ -107,11 +107,11 @@ |
||
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
| 110 | - * Registers a Meta Data object as defined REST field. |
|
| 111 | - * |
|
| 112 | - * @param \PinkCrab\Registerables\Meta_Data $meta |
|
| 113 | - * @return void |
|
| 114 | - */ |
|
| 110 | + * Registers a Meta Data object as defined REST field. |
|
| 111 | + * |
|
| 112 | + * @param \PinkCrab\Registerables\Meta_Data $meta |
|
| 113 | + * @return void |
|
| 114 | + */ |
|
| 115 | 115 | public function register_meta_rest_field( Meta_Data $meta ) { |
| 116 | 116 | // Skip if not sub type defined for post or term. |
| 117 | 117 | if ( null === $meta->get_subtype() ) { |
@@ -37,8 +37,8 @@ discard block |
||
| 37 | 37 | * @return bool |
| 38 | 38 | * @throws \Exception if fails to register meta data. |
| 39 | 39 | */ |
| 40 | - public function register_for_post_type( Meta_Data $meta, string $post_type ):bool { |
|
| 41 | - return $this->register_meta( $meta, 'post', $post_type ); |
|
| 40 | + public function register_for_post_type(Meta_Data $meta, string $post_type):bool { |
|
| 41 | + return $this->register_meta($meta, 'post', $post_type); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | /** |
@@ -49,8 +49,8 @@ discard block |
||
| 49 | 49 | * @return bool |
| 50 | 50 | * @throws \Exception if fails to register meta data. |
| 51 | 51 | */ |
| 52 | - public function register_for_term( Meta_Data $meta, string $taxonomy ):bool { |
|
| 53 | - return $this->register_meta( $meta, 'term', $taxonomy ); |
|
| 52 | + public function register_for_term(Meta_Data $meta, string $taxonomy):bool { |
|
| 53 | + return $this->register_meta($meta, 'term', $taxonomy); |
|
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | /** |
@@ -64,25 +64,25 @@ discard block |
||
| 64 | 64 | * @return bool |
| 65 | 65 | * @throws \Exception if fails to register meta data. |
| 66 | 66 | */ |
| 67 | - protected function register_meta( Meta_Data $meta, string $meta_type, string $sub_type ): bool { |
|
| 67 | + protected function register_meta(Meta_Data $meta, string $meta_type, string $sub_type): bool { |
|
| 68 | 68 | // Clone and set the post type, while enforcing it as a post meta. |
| 69 | 69 | $meta = clone $meta; |
| 70 | - $meta->object_subtype( $sub_type ); |
|
| 71 | - $meta->meta_type( $meta_type ); |
|
| 70 | + $meta->object_subtype($sub_type); |
|
| 71 | + $meta->meta_type($meta_type); |
|
| 72 | 72 | |
| 73 | 73 | // Normalise rest schema model to array. |
| 74 | - $meta = $this->normalise_rest_schema( $meta ); |
|
| 74 | + $meta = $this->normalise_rest_schema($meta); |
|
| 75 | 75 | |
| 76 | - $result = register_meta( $meta->get_meta_type(), $meta->get_meta_key(), $meta->parse_args() ); |
|
| 77 | - if ( ! $result ) { |
|
| 76 | + $result = register_meta($meta->get_meta_type(), $meta->get_meta_key(), $meta->parse_args()); |
|
| 77 | + if ( ! $result) { |
|
| 78 | 78 | throw new \Exception( |
| 79 | 79 | "Failed to register {$meta->get_meta_key()} (meta) for {$sub_type} of {$meta_type} type" |
| 80 | 80 | ); |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | // Maybe register rest fields. |
| 84 | - if ( false !== $meta->get_rest_schema() ) { |
|
| 85 | - $this->register_meta_rest_field( $meta ); |
|
| 84 | + if (false !== $meta->get_rest_schema()) { |
|
| 85 | + $this->register_meta_rest_field($meta); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | return $result; |
@@ -97,11 +97,11 @@ discard block |
||
| 97 | 97 | * @param \PinkCrab\Registerables\Meta_Data $meta |
| 98 | 98 | * @return \PinkCrab\Registerables\Meta_Data |
| 99 | 99 | */ |
| 100 | - protected function normalise_rest_schema( Meta_Data $meta ): Meta_Data { |
|
| 101 | - if ( \class_exists( 'PinkCrab\WP_Rest_Schema\Argument\Argument' ) |
|
| 100 | + protected function normalise_rest_schema(Meta_Data $meta): Meta_Data { |
|
| 101 | + if (\class_exists('PinkCrab\WP_Rest_Schema\Argument\Argument') |
|
| 102 | 102 | && $meta->get_rest_schema() instanceof \PinkCrab\WP_Rest_Schema\Argument\Argument |
| 103 | 103 | ) { |
| 104 | - $meta->rest_schema( \PinkCrab\WP_Rest_Schema\Parser\Argument_Parser::for_meta_data( $meta->get_rest_schema() ) ); |
|
| 104 | + $meta->rest_schema(\PinkCrab\WP_Rest_Schema\Parser\Argument_Parser::for_meta_data($meta->get_rest_schema())); |
|
| 105 | 105 | } |
| 106 | 106 | return $meta; |
| 107 | 107 | } |
@@ -112,22 +112,22 @@ discard block |
||
| 112 | 112 | * @param \PinkCrab\Registerables\Meta_Data $meta |
| 113 | 113 | * @return void |
| 114 | 114 | */ |
| 115 | - public function register_meta_rest_field( Meta_Data $meta ) { |
|
| 115 | + public function register_meta_rest_field(Meta_Data $meta) { |
|
| 116 | 116 | // Skip if not sub type defined for post or term. |
| 117 | - if ( null === $meta->get_subtype() ) { |
|
| 117 | + if (null === $meta->get_subtype()) { |
|
| 118 | 118 | return; |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | add_action( |
| 122 | 122 | 'rest_api_init', |
| 123 | - function () use ( $meta ) { |
|
| 123 | + function() use ($meta) { |
|
| 124 | 124 | register_rest_field( |
| 125 | 125 | $meta->get_subtype(), |
| 126 | 126 | $meta->get_meta_key(), |
| 127 | 127 | array( // @phpstan-ignore-line WP Docblock doesn't give enough details of callable param types, so throws false positive |
| 128 | - 'get_callback' => $meta->get_rest_view() ?? $this->create_rest_get_method( $meta ), |
|
| 128 | + 'get_callback' => $meta->get_rest_view() ?? $this->create_rest_get_method($meta), |
|
| 129 | 129 | 'schema' => $meta->get_rest_schema(), |
| 130 | - 'update_callback' => $meta->get_rest_update() ?? $this->create_rest_update_method( $meta ), |
|
| 130 | + 'update_callback' => $meta->get_rest_update() ?? $this->create_rest_update_method($meta), |
|
| 131 | 131 | ) |
| 132 | 132 | ); |
| 133 | 133 | } |
@@ -140,23 +140,23 @@ discard block |
||
| 140 | 140 | * @param \PinkCrab\Registerables\Meta_Data $meta |
| 141 | 141 | * @return callable(array<mixed>):void |
| 142 | 142 | */ |
| 143 | - protected function create_rest_get_method( Meta_Data $meta ): callable { |
|
| 144 | - return function( $model ) use ( $meta ) { |
|
| 145 | - switch ( $meta->get_meta_type() ) { |
|
| 143 | + protected function create_rest_get_method(Meta_Data $meta): callable { |
|
| 144 | + return function($model) use ($meta) { |
|
| 145 | + switch ($meta->get_meta_type()) { |
|
| 146 | 146 | case 'post': |
| 147 | - $value = get_post_meta( $model['id'], $meta->get_meta_key(), true ); |
|
| 147 | + $value = get_post_meta($model['id'], $meta->get_meta_key(), true); |
|
| 148 | 148 | break; |
| 149 | 149 | |
| 150 | 150 | case 'term': |
| 151 | - $value = get_term_meta( $model['id'], $meta->get_meta_key(), true ); |
|
| 151 | + $value = get_term_meta($model['id'], $meta->get_meta_key(), true); |
|
| 152 | 152 | break; |
| 153 | 153 | |
| 154 | 154 | case 'user': |
| 155 | - $value = get_user_meta( $model['id'], $meta->get_meta_key(), true ); |
|
| 155 | + $value = get_user_meta($model['id'], $meta->get_meta_key(), true); |
|
| 156 | 156 | break; |
| 157 | 157 | |
| 158 | 158 | case 'comment': |
| 159 | - $value = get_comment_meta( $model['id'], $meta->get_meta_key(), true ); |
|
| 159 | + $value = get_comment_meta($model['id'], $meta->get_meta_key(), true); |
|
| 160 | 160 | break; |
| 161 | 161 | |
| 162 | 162 | default: |
@@ -174,31 +174,31 @@ discard block |
||
| 174 | 174 | * @param Meta_Data $meta |
| 175 | 175 | * @return callable(mixed $value, \WP_Post|\WP_Term|\WP_User|\WP_Comment $object) |
| 176 | 176 | */ |
| 177 | - protected function create_rest_update_method( Meta_Data $meta ): callable { |
|
| 177 | + protected function create_rest_update_method(Meta_Data $meta): callable { |
|
| 178 | 178 | /** |
| 179 | 179 | * @param mixed $value |
| 180 | 180 | * @param \WP_Post|\WP_Term|\WP_User|\WP_Comment $object |
| 181 | 181 | */ |
| 182 | - return function( $value, $object ) use ( $meta ) { |
|
| 183 | - switch ( $meta->get_meta_type() ) { |
|
| 182 | + return function($value, $object) use ($meta) { |
|
| 183 | + switch ($meta->get_meta_type()) { |
|
| 184 | 184 | case 'post': |
| 185 | 185 | /** @var \WP_Post $object */ |
| 186 | - update_post_meta( $object->ID, $meta->get_meta_key(), $value ); |
|
| 186 | + update_post_meta($object->ID, $meta->get_meta_key(), $value); |
|
| 187 | 187 | break; |
| 188 | 188 | |
| 189 | 189 | case 'term': |
| 190 | 190 | /** @var \WP_Term $object */ |
| 191 | - update_term_meta( $object->term_id, $meta->get_meta_key(), $value ); |
|
| 191 | + update_term_meta($object->term_id, $meta->get_meta_key(), $value); |
|
| 192 | 192 | break; |
| 193 | 193 | |
| 194 | 194 | case 'user': |
| 195 | 195 | /** @var \WP_User $object */ |
| 196 | - update_user_meta( $object->ID, $meta->get_meta_key(), $value ); |
|
| 196 | + update_user_meta($object->ID, $meta->get_meta_key(), $value); |
|
| 197 | 197 | break; |
| 198 | 198 | |
| 199 | 199 | case 'comment': |
| 200 | 200 | /** @var \WP_Comment $object */ |
| 201 | - update_comment_meta( (int) $object->comment_ID, $meta->get_meta_key(), $value ); |
|
| 201 | + update_comment_meta((int) $object->comment_ID, $meta->get_meta_key(), $value); |
|
| 202 | 202 | break; |
| 203 | 203 | |
| 204 | 204 | default: |
@@ -72,15 +72,15 @@ discard block |
||
| 72 | 72 | protected $default = ''; |
| 73 | 73 | |
| 74 | 74 | /** |
| 75 | - * The meta fields callbacks |
|
| 76 | - * |
|
| 77 | - * @var array{ |
|
| 78 | - * sanitize: null|callable, |
|
| 79 | - * permissions: null|callable, |
|
| 80 | - * rest_view: null|callable(mixed[]): void, |
|
| 81 | - * rest_update: null|callable(mixed,\WP_Post|\WP_Term|\WP_User|\WP_Comment): void |
|
| 82 | - * } |
|
| 83 | - */ |
|
| 75 | + * The meta fields callbacks |
|
| 76 | + * |
|
| 77 | + * @var array{ |
|
| 78 | + * sanitize: null|callable, |
|
| 79 | + * permissions: null|callable, |
|
| 80 | + * rest_view: null|callable(mixed[]): void, |
|
| 81 | + * rest_update: null|callable(mixed,\WP_Post|\WP_Term|\WP_User|\WP_Comment): void |
|
| 82 | + * } |
|
| 83 | + */ |
|
| 84 | 84 | protected $callbacks = array( |
| 85 | 85 | 'sanitize' => null, |
| 86 | 86 | 'permissions' => null, |
@@ -233,11 +233,11 @@ discard block |
||
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | /** |
| 236 | - * Sets the GET callback for REST requests. |
|
| 237 | - * |
|
| 238 | - * @param callable|null $callback |
|
| 239 | - * @return self |
|
| 240 | - */ |
|
| 236 | + * Sets the GET callback for REST requests. |
|
| 237 | + * |
|
| 238 | + * @param callable|null $callback |
|
| 239 | + * @return self |
|
| 240 | + */ |
|
| 241 | 241 | public function rest_view( ?callable $callback ): self { |
| 242 | 242 | $this->callbacks['rest_view'] = $callback; |
| 243 | 243 | return $this; |
@@ -314,10 +314,10 @@ discard block |
||
| 314 | 314 | } |
| 315 | 315 | |
| 316 | 316 | /** |
| 317 | - * Gets the GET callback for REST requests. |
|
| 318 | - * |
|
| 319 | - * @return null|callable(mixed[]): void |
|
| 320 | - */ |
|
| 317 | + * Gets the GET callback for REST requests. |
|
| 318 | + * |
|
| 319 | + * @return null|callable(mixed[]): void |
|
| 320 | + */ |
|
| 321 | 321 | public function get_rest_view(): ?callable { |
| 322 | 322 | return $this->callbacks['rest_view']; |
| 323 | 323 | } |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | */ |
| 103 | 103 | protected $meta_key; |
| 104 | 104 | |
| 105 | - public function __construct( string $meta_key ) { |
|
| 105 | + public function __construct(string $meta_key) { |
|
| 106 | 106 | $this->meta_key = $meta_key; |
| 107 | 107 | } |
| 108 | 108 | |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | * @param string $meta_type Object type meta applies to |
| 114 | 114 | * @return self |
| 115 | 115 | */ |
| 116 | - public function meta_type( string $meta_type ): self { |
|
| 116 | + public function meta_type(string $meta_type): self { |
|
| 117 | 117 | $this->meta_type = $meta_type; |
| 118 | 118 | return $this; |
| 119 | 119 | } |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | * @param string|null $object_subtype Holds a secondary object type, used for post type and taxonomy. |
| 125 | 125 | * @return self |
| 126 | 126 | */ |
| 127 | - public function object_subtype( ?string $object_subtype ): self { |
|
| 127 | + public function object_subtype(?string $object_subtype): self { |
|
| 128 | 128 | $this->object_subtype = $object_subtype ?? ''; |
| 129 | 129 | return $this; |
| 130 | 130 | } |
@@ -136,7 +136,7 @@ discard block |
||
| 136 | 136 | * @param string $type |
| 137 | 137 | * @return self |
| 138 | 138 | */ |
| 139 | - public function type( string $type ): self { |
|
| 139 | + public function type(string $type): self { |
|
| 140 | 140 | $this->type = $type; |
| 141 | 141 | return $this; |
| 142 | 142 | } |
@@ -147,9 +147,9 @@ discard block |
||
| 147 | 147 | * @param string $post_type |
| 148 | 148 | * @return self |
| 149 | 149 | */ |
| 150 | - public function post_type( string $post_type ): self { |
|
| 151 | - $this->meta_type( 'post' ); |
|
| 152 | - $this->object_subtype( $post_type ); |
|
| 150 | + public function post_type(string $post_type): self { |
|
| 151 | + $this->meta_type('post'); |
|
| 152 | + $this->object_subtype($post_type); |
|
| 153 | 153 | return $this; |
| 154 | 154 | } |
| 155 | 155 | |
@@ -159,9 +159,9 @@ discard block |
||
| 159 | 159 | * @param string $taxonomy |
| 160 | 160 | * @return self |
| 161 | 161 | */ |
| 162 | - public function taxonomy( string $taxonomy ): self { |
|
| 163 | - $this->meta_type( 'term' ); |
|
| 164 | - $this->object_subtype( $taxonomy ); |
|
| 162 | + public function taxonomy(string $taxonomy): self { |
|
| 163 | + $this->meta_type('term'); |
|
| 164 | + $this->object_subtype($taxonomy); |
|
| 165 | 165 | return $this; |
| 166 | 166 | } |
| 167 | 167 | |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * |
| 173 | 173 | * @return self |
| 174 | 174 | */ |
| 175 | - public function description( string $description ): self { |
|
| 175 | + public function description(string $description): self { |
|
| 176 | 176 | $this->description = $description; |
| 177 | 177 | return $this; |
| 178 | 178 | } |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | * @param bool $single Meta value is single value or array |
| 184 | 184 | * @return self |
| 185 | 185 | */ |
| 186 | - public function single( bool $single = true ): self { |
|
| 186 | + public function single(bool $single = true): self { |
|
| 187 | 187 | $this->single = $single; |
| 188 | 188 | return $this; |
| 189 | 189 | } |
@@ -194,7 +194,7 @@ discard block |
||
| 194 | 194 | * @param mixed $default |
| 195 | 195 | * @return self |
| 196 | 196 | */ |
| 197 | - public function default( $default ): self { |
|
| 197 | + public function default($default): self { |
|
| 198 | 198 | $this->default = $default; |
| 199 | 199 | return $this; |
| 200 | 200 | } |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | * @param callable(mixed):mixed $callback |
| 206 | 206 | * @return self |
| 207 | 207 | */ |
| 208 | - public function sanitize( callable $callback ): self { |
|
| 208 | + public function sanitize(callable $callback): self { |
|
| 209 | 209 | $this->callbacks['sanitize'] = $callback; |
| 210 | 210 | return $this; |
| 211 | 211 | } |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | * @param callable $callback |
| 217 | 217 | * @return self |
| 218 | 218 | */ |
| 219 | - public function permissions( callable $callback ): self { |
|
| 219 | + public function permissions(callable $callback): self { |
|
| 220 | 220 | $this->callbacks['permissions'] = $callback; |
| 221 | 221 | return $this; |
| 222 | 222 | } |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | * @param bool|array<mixed> $rest_schema|PinkCrab\WP_Rest_Schema\Argument\Argument Rest schema definitions |
| 228 | 228 | * @return self |
| 229 | 229 | */ |
| 230 | - public function rest_schema( $rest_schema ): self { |
|
| 230 | + public function rest_schema($rest_schema): self { |
|
| 231 | 231 | $this->rest_schema = $rest_schema; |
| 232 | 232 | return $this; |
| 233 | 233 | } |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | * @param callable|null $callback |
| 239 | 239 | * @return self |
| 240 | 240 | */ |
| 241 | - public function rest_view( ?callable $callback ): self { |
|
| 241 | + public function rest_view(?callable $callback): self { |
|
| 242 | 242 | $this->callbacks['rest_view'] = $callback; |
| 243 | 243 | return $this; |
| 244 | 244 | } |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | * @param null|callable(mixed,\WP_Post|\WP_Term|\WP_User|\WP_Comment):void $callback |
| 250 | 250 | * @return self |
| 251 | 251 | */ |
| 252 | - public function rest_update( ?callable $callback ): self { |
|
| 252 | + public function rest_update(?callable $callback): self { |
|
| 253 | 253 | $this->callbacks['rest_update'] = $callback; |
| 254 | 254 | return $this; |
| 255 | 255 | } |
@@ -271,7 +271,7 @@ discard block |
||
| 271 | 271 | ); |
| 272 | 272 | |
| 273 | 273 | // Set subtype. |
| 274 | - if ( $this->object_subtype !== null ) { |
|
| 274 | + if ($this->object_subtype !== null) { |
|
| 275 | 275 | $args['object_subtype'] = $this->object_subtype; |
| 276 | 276 | } |
| 277 | 277 | return $args; |