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