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

132 lines 3.8 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 use Parsely\Models\Smart_Link;
16 use Parsely\Models\Smart_Link_Status;
17 use Parsely\Models\Inbound_Smart_Link;
18
19 /**
20 * Injects Parse.ly Metadata to WordPress REST API.
21 *
22 * @since 3.1.0
23 * @since 3.2.0 Renamed FQCN from `Parsely\Rest` to `Parsely\Endpoints\Rest_Metadata`.
24 */
25 class Rest_Metadata extends Metadata_Endpoint {
26 private const REST_VERSION = '1.1.0';
27
28 /**
29 * Registers fields in WordPress REST API.
30 *
31 * @since 3.1.0
32 */
33 public function run(): void {
34 if ( apply_filters( 'wp_parsely_enable_rest_api_support', true ) && $this->parsely->site_id_is_set() ) {
35 $this->register_meta();
36 }
37 }
38
39 /**
40 * Registers the meta field on the appropriate resource types in the REST API.
41 *
42 * @since 3.1.0
43 */
44 public function register_meta(): void {
45 $object_types = $this->parsely->get_all_track_types();
46
47 /**
48 * Filters the list of object types that the Parse.ly REST API is hooked into.
49 *
50 * @since 3.1.0
51 *
52 * @param string[] $object_types Array of strings containing the object types, i.e. `page`,
53 * `post`, `term`.
54 */
55 $object_types = apply_filters( 'wp_parsely_rest_object_types', $object_types );
56
57 $args = array( 'get_callback' => array( $this, 'get_callback' ) );
58 register_rest_field( $object_types, self::FIELD_NAME, $args );
59 }
60
61 /**
62 * Function to get hooked into the `get_callback` property of the `parsely`
63 * REST API field. It generates the `parsely` object in the REST API.
64 *
65 * @since 3.1.0
66 * @since 3.19.0 Added the `canonical_url` field.
67 *
68 * @param array<string, mixed> $object_data The data of the object to render the metadata for,
69 * usually a post or a page.
70 * @return array<string, mixed> The `parsely` object to be rendered in the REST API. Contains a
71 * version number describing the response and the `meta` object
72 * containing the actual metadata.
73 */
74 public function get_callback( array $object_data ): array {
75 /** @var int $post_id */
76 $post_id = $object_data['ID'] ?? $object_data['id'] ?? 0;
77 $post = WP_Post::get_instance( $post_id );
78
79 $options = $this->parsely->get_options();
80
81 $response = array(
82 'version' => self::REST_VERSION,
83 'canonical_url' => \Parsely\Parsely::get_canonical_url_from_post( $post_id ),
84 'smart_links' => array(
85 'inbound' => 0,
86 'outbound' => 0,
87 ),
88 'traffic_boost_suggestions_count' => 0,
89 );
90
91 if ( false === $post ) {
92 return $response;
93 }
94
95 $metadata = ( new Metadata( $this->parsely ) )->construct_metadata( $post );
96 $response['meta'] = $metadata;
97
98 /**
99 * Filter whether REST API support in rendered string format is enabled
100 * or not.
101 *
102 * @since 3.1.0
103 *
104 * @param bool $enabled True if enabled, false if not.
105 * @param WP_Post|false $post Current post object.
106 */
107 if ( apply_filters( 'wp_parsely_enable_rest_rendered_support', true, $post ) ) {
108 $response['rendered'] = $this->get_rendered_meta( $options['meta_type'] );
109 }
110
111 /**
112 * Filter whether the REST API returns the tracker URL.
113 *
114 * @since 3.3.0
115 *
116 * @param bool $enabled True if enabled, false if not.
117 * @param WP_Post|false $post Current post object.
118 */
119 if ( apply_filters( 'wp_parsely_enable_tracker_url', true, $post ) ) {
120 $response['tracker_url'] = $this->parsely->get_tracker_url();
121 }
122
123 // Fetch Smart Link data.
124 $response['smart_links'] = Smart_Link::get_link_counts( $post_id, Smart_Link_Status::APPLIED );
125
126 // Fetch Traffic Boost data.
127 $response['traffic_boost_suggestions_count'] = Inbound_Smart_Link::get_suggestions_count( $post_id );
128
129 return $response;
130 }
131 }
132