PluginProbe
Parse.ly / 3.16.2
Parse.ly v3.16.2
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / Endpoints / class-rest-metadata.php

class-rest-metadata.php in Parse.ly 3.16.2, at src/Endpoints/class-rest-metadata.php

124 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Endpoints: REST Metadata endpoint class
4 *
5 * @package Parsely
6 * @since 3.1.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Endpoints;
12
13 use Parsely\Metadata;
14 use WP_Post;
15
16 /**
17 * Injects Parse.ly Metadata to WordPress REST API.
18 *
19 * @since 3.1.0
20 * @since 3.2.0 Renamed FQCN from `Parsely\Rest` to `Parsely\Endpoints\Rest_Metadata`.
21 */
22 class Rest_Metadata extends Metadata_Endpoint {
23 private const REST_VERSION = '1.1.0';
24
25 /**
26 * Registers fields in WordPress REST API.
27 *
28 * @since 3.1.0
29 */
30 public function run(): void {
31 /**
32 * Filter whether REST API support is enabled or not.
33 *
34 * @since 3.1.0
35 *
36 * @param bool $enabled True if enabled, false if not.
37 */
38 if ( apply_filters( 'wp_parsely_enable_rest_api_support', true ) && $this->parsely->site_id_is_set() ) {
39 $this->register_meta();
40 }
41 }
42
43 /**
44 * Registers the meta field on the appropriate resource types in the REST API.
45 *
46 * @since 3.1.0
47 */
48 public function register_meta(): void {
49 $object_types = $this->parsely->get_all_track_types();
50
51 /**
52 * Filters the list of object types that the Parse.ly REST API is hooked into.
53 *
54 * @since 3.1.0
55 *
56 * @param string[] $object_types Array of strings containing the object types, i.e. `page`,
57 * `post`, `term`.
58 */
59 $object_types = apply_filters( 'wp_parsely_rest_object_types', $object_types );
60
61 $args = array( 'get_callback' => array( $this, 'get_callback' ) );
62 register_rest_field( $object_types, self::FIELD_NAME, $args );
63 }
64
65 /**
66 * Function to get hooked into the `get_callback` property of the `parsely`
67 * REST API field. It generates the `parsely` object in the REST API.
68 *
69 * @param array<string, mixed> $object_data The data of the object to render the metadata for,
70 * usually a post or a page.
71 * @return array<string, mixed> The `parsely` object to be rendered in the REST API. Contains a
72 * version number describing the response and the `meta` object
73 * containing the actual metadata.
74 */
75 public function get_callback( array $object_data ): array {
76 /**
77 * Variable.
78 *
79 * @var int
80 */
81 $post_id = $object_data['ID'] ?? $object_data['id'] ?? 0;
82 $post = WP_Post::get_instance( $post_id );
83 $options = $this->parsely->get_options();
84
85 if ( false === $post ) {
86 $metadata = '';
87 } else {
88 $metadata = ( new Metadata( $this->parsely ) )->construct_metadata( $post );
89 }
90
91 $response = array(
92 'version' => self::REST_VERSION,
93 'meta' => $metadata,
94 );
95
96 /**
97 * Filter whether REST API support in rendered string format is enabled
98 * or not.
99 *
100 * @since 3.1.0
101 *
102 * @param bool $enabled True if enabled, false if not.
103 * @param WP_Post|false $post Current post object.
104 */
105 if ( apply_filters( 'wp_parsely_enable_rest_rendered_support', true, $post ) ) {
106 $response['rendered'] = $this->get_rendered_meta( $options['meta_type'] );
107 }
108
109 /**
110 * Filter whether the REST API returns the tracker URL.
111 *
112 * @since 3.3.0
113 *
114 * @param bool $enabled True if enabled, false if not.
115 * @param WP_Post|false $post Current post object.
116 */
117 if ( apply_filters( 'wp_parsely_enable_tracker_url', true, $post ) ) {
118 $response['tracker_url'] = $this->parsely->get_tracker_url();
119 }
120
121 return $response;
122 }
123 }
124