Passed
Push — master ( 323542...b3d3d7 )
by Frank
06:59 queued 02:19
created

src/Rest/Rest.php (5 issues)

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;
0 ignored issues
show
The type WP_Error was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use WP_HTTP_Response;
0 ignored issues
show
The type WP_HTTP_Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use WP_REST_Posts_Controller;
0 ignored issues
show
The type WP_REST_Posts_Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use WP_REST_Request;
0 ignored issues
show
The type WP_REST_Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
The function update_post_meta was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
            /** @scrutinizer ignore-call */ 
85
            update_post_meta($postId, self::META_KEY_THUMBNAIL_ID, $attachmentId);
Loading history...
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