1
|
|
|
<?php # -*- coding: utf-8 -*- |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MultisiteGlobalMedia\Rest; |
5
|
|
|
|
6
|
|
|
use MultisiteGlobalMedia\Helper; |
7
|
|
|
use MultisiteGlobalMedia\Site; |
8
|
|
|
use WP_Error; |
9
|
|
|
use WP_HTTP_Response; |
10
|
|
|
use WP_REST_Posts_Controller; |
11
|
|
|
use WP_REST_Request; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Rest |
15
|
|
|
*/ |
16
|
|
|
class Rest |
17
|
|
|
{ |
18
|
|
|
use Helper; |
19
|
|
|
const META_KEY_THUMBNAIL_ID = '_thumbnail_id'; |
20
|
|
|
const REST_FIELD_THUMBNAIL_ID = 'featured_media'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Site |
24
|
|
|
*/ |
25
|
|
|
private $site; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Rest constructor |
29
|
|
|
* |
30
|
|
|
* @param Site $site |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Site $site) |
33
|
|
|
{ |
34
|
|
|
$this->site = $site; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Filter the attachment post type registration arguments to use our own |
39
|
|
|
* REST controller. |
40
|
|
|
* |
41
|
|
|
* @param array $args |
42
|
|
|
* @param string $postType |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
* |
46
|
|
|
* @wp-hook register_post_type_args |
47
|
|
|
*/ |
48
|
|
|
public function registerPostTypeArgs(array $args, string $postType): array |
49
|
|
|
{ |
50
|
|
|
if ($postType === 'attachment') { |
51
|
|
|
$args['rest_controller_class'] = RestController::class; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $args; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Filter REST API responses for post saves which include featured_media |
59
|
|
|
* field and force the post meta update even if the id doesn't exist on the |
60
|
|
|
* current site. |
61
|
|
|
* |
62
|
|
|
* @param WP_HTTP_Response|WP_Error $response |
63
|
|
|
* @param array $handler |
64
|
|
|
* @param WP_REST_Request $request |
65
|
|
|
* |
66
|
|
|
* @return WP_HTTP_Response|WP_Error |
67
|
|
|
* |
68
|
|
|
* @wp-hook rest_request_after_callbacks |
69
|
|
|
* |
70
|
|
|
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType |
71
|
|
|
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType |
72
|
|
|
*/ |
73
|
|
|
public function restRequestAfterCallbacks($response, array $handler, WP_REST_Request $request) |
74
|
|
|
{ |
75
|
|
|
// phpcs:enable |
76
|
|
|
if (!isset($handler['callback'][0]) || !($handler['callback'][0] instanceof WP_REST_Posts_Controller)) { |
77
|
|
|
return $response; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$idPrefix = $this->site->idSitePrefix(); |
81
|
|
|
$attachmentId = (int)$request[self::REST_FIELD_THUMBNAIL_ID]; |
82
|
|
|
$postId = (int)$request['id']; |
83
|
|
|
if ($attachmentId && $this->idPrefixIncludedInAttachmentId($attachmentId, $idPrefix)) { |
84
|
|
|
update_post_meta($postId, self::META_KEY_THUMBNAIL_ID, $attachmentId); |
85
|
|
|
|
86
|
|
|
$data = $response->get_data(); |
|
|
|
|
87
|
|
|
$data[self::REST_FIELD_THUMBNAIL_ID] = $attachmentId; |
88
|
|
|
$response->set_data($data); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $response; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.