PluginProbe
Parse.ly / 3.2.0
Parse.ly v3.2.0
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.2.0, at src/Endpoints/class-rest-metadata.php

106 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST API 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 WP_Post;
14
15 /**
16 * Injects Parse.ly Metadata to WordPress REST API
17 *
18 * @since 3.1.0
19 * @since 3.2.0 Renamed FQCN from `Parsely\Rest` to `Parsely\Endpoints\Rest_Metadata`.
20 */
21 class Rest_Metadata extends Metadata_Endpoint {
22 private const REST_VERSION = '1.0.0';
23
24 /**
25 * Register fields in WordPress REST API
26 *
27 * @since 3.1.0
28 *
29 * @return void
30 */
31 public function run(): void {
32 /**
33 * Filter whether REST API support is enabled or not.
34 *
35 * @since 3.1.0
36 *
37 * @param bool $enabled True if enabled, false if not.
38 */
39 if ( apply_filters( 'wp_parsely_enable_rest_api_support', true ) && $this->parsely->api_key_is_set() ) {
40 $this->register_meta();
41 }
42 }
43
44 /**
45 * Registers the meta field on the appropriate resource types in the REST API.
46 *
47 * @since 3.1.0
48 *
49 * @return void
50 */
51 public function register_meta(): void {
52 $options = $this->parsely->get_options();
53 $object_types = array_unique( array_merge( $options['track_post_types'], $options['track_page_types'] ) );
54
55 /**
56 * Filters the list of object types that the Parse.ly REST API is hooked into.
57 *
58 * @since 3.1.0
59 *
60 * @param string[] $object_types Array of strings containing the object types, i.e. `page`, `post`, `term`.
61 */
62 $object_types = apply_filters( 'wp_parsely_rest_object_types', $object_types );
63
64 $args = array( 'get_callback' => array( $this, 'get_callback' ) );
65 register_rest_field( $object_types, self::FIELD_NAME, $args );
66 }
67
68 /**
69 * Function to get hooked into the `get_callback` property of the `parsely` REST API field. It generates
70 * the `parsely` object in the REST API.
71 *
72 * @param array $object The WordPress object to extract to render the metadata for, usually a post or a page.
73 * @return array<string, mixed> The `parsely` object to be rendered in the REST API. Contains a version number describing the
74 * response and the `meta` object containing the actual metadata.
75 */
76 public function get_callback( array $object ): array {
77 $post_id = $object['ID'] ?? $object['id'] ?? 0;
78 $post = WP_Post::get_instance( $post_id );
79 $options = $this->parsely->get_options();
80
81 if ( false === $post ) {
82 $meta = '';
83 } else {
84 $meta = $this->parsely->construct_parsely_metadata( $options, $post );
85 }
86
87 $response = array(
88 'version' => self::REST_VERSION,
89 'meta' => $meta,
90 );
91
92 /**
93 * Filter whether REST API support in rendered string format is enabled or not.
94 *
95 * @since 3.1.0
96 *
97 * @param bool $enabled True if enabled, false if not.
98 */
99 if ( apply_filters( 'wp_parsely_enable_rest_rendered_support', true, $post ) ) {
100 $response['rendered'] = $this->get_rendered_meta( $options['meta_type'] );
101 }
102
103 return $response;
104 }
105 }
106