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-graphql-metadata.php

class-graphql-metadata.php in Parse.ly 3.20.2, at src/Endpoints/class-graphql-metadata.php

160 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Endpoints: GraphQL metadata endpoint class
4 *
5 * Adds support for GraphQL.
6 * Note: This class will only work if the WPGraphQL plugin is installed.
7 *
8 * @package Parsely
9 * @since 3.2.0
10 */
11
12 declare(strict_types=1);
13
14 namespace Parsely\Endpoints;
15
16 use WP_Post;
17
18 /**
19 * Adds Parse.ly metadata fields to the WP GraphQL server.
20 *
21 * @since 3.2.0
22 */
23 class GraphQL_Metadata extends Metadata_Endpoint {
24 private const GRAPHQL_VERSION = '1.0.0';
25 private const GRAPHQL_CONTAINER_TYPE = 'ParselyMetaContainer';
26
27 /**
28 * Registers fields in WPGraphQL plugin.
29 *
30 * @since 3.2.0
31 */
32 public function run(): void {
33 /**
34 * Filters whether GraphQL support is enabled or not.
35 *
36 * @since 3.2.0
37 *
38 * @param bool $enabled True if enabled, false if not.
39 */
40 if ( apply_filters( 'wp_parsely_enable_graphql_support', true ) && $this->parsely->site_id_is_set() ) {
41 add_action( 'graphql_register_types', array( $this, 'register_meta' ) );
42 }
43 }
44
45 /**
46 * Registers the meta field on the appropriate resource types in the REST
47 * API.
48 *
49 * @since 3.2.0
50 */
51 public function register_meta(): void {
52 $this->register_object_types();
53 $this->register_fields();
54 }
55
56 /**
57 * Registers the new custom types for Parse.ly Metadata into the GraphQL
58 * instance.
59 *
60 * @since 3.2.0
61 */
62 private function register_object_types(): void {
63 $container_type = array(
64 'description' => __( 'Parse.ly Metadata root type.', 'wp-parsely' ),
65 'fields' => array(
66 'version' => array(
67 'type' => 'String',
68 'description' => __( 'Revision of the metadata format.', 'wp-parsely' ),
69 ),
70 'scriptUrl' => array(
71 'type' => 'String',
72 'description' => __( 'URL of the Parse.ly tracking script, specific to the site.', 'wp-parsely' ),
73 ),
74 'repeatedMetas' => array(
75 'type' => 'String',
76 'description' => __(
77 'HTML string containing the metadata in JSON-LD. Intended to be rendered in the front-end as is.',
78 'wp-parsely'
79 ),
80 'resolve' => function () {
81 return self::get_rendered_meta( 'repeated_metas' );
82 },
83 ),
84 'jsonLd' => array(
85 'type' => 'String',
86 'args' => array(
87 'removeWrappingTag' => array(
88 'type' => 'Boolean',
89 'description' => __( 'Return rendered tags without the `script` wrapping tags.', 'wp-parsely' ),
90 ),
91 ),
92 'description' => __(
93 'HTML string containing the metadata in JSON-LD. Intended to be rendered in the front-end as is.',
94 'wp-parsely'
95 ),
96 'resolve' => function ( array $parsely_meta, array $args ) {
97 $json_ld = self::get_rendered_meta( 'json_ld' );
98
99 if ( isset( $args['removeWrappingTag'] ) && true === $args['removeWrappingTag'] ) {
100 // phpcs:ignore WordPressVIPMinimum.Functions.StripTags.StripTagsOneParameter
101 $json_ld = strip_tags( $json_ld );
102 $json_ld = trim( $json_ld );
103 }
104
105 return $json_ld;
106 },
107 ),
108 'isTracked' => array(
109 'type' => 'Boolean',
110 'description' => __(
111 'Boolean indicating whether the current object\'s page type should be tracked according to user\'s settings.',
112 'wp-parsely'
113 ),
114 ),
115 ),
116 );
117 register_graphql_object_type( self::GRAPHQL_CONTAINER_TYPE, $container_type );
118 }
119
120 /**
121 * Registers the custom metadata fields, so they can be queried in GraphQL.
122 *
123 * @since 3.2.0
124 */
125 private function register_fields(): void {
126 $resolve = function ( \WPGraphQL\Model\Post $graphql_post ) {
127 $post_id = $graphql_post->ID;
128
129 if ( ! is_int( $post_id ) ) {
130 return null;
131 }
132
133 $post = WP_Post::get_instance( $post_id );
134
135 if ( false === $post ) {
136 return null;
137 }
138
139 $object_types = $this->parsely->get_all_track_types();
140 $current_object_type = get_post_type( $post );
141
142 return array(
143 'version' => self::GRAPHQL_VERSION,
144 'scriptUrl' => $this->parsely->get_tracker_url(),
145 'isTracked' => in_array( $current_object_type, $object_types, true ),
146 );
147 };
148
149 $config = array(
150 'type' => self::GRAPHQL_CONTAINER_TYPE,
151 'description' => __(
152 'Parse.ly metadata fields, to be rendered in the front-end so they can be parsed by the crawler. See https://docs.parse.ly/crawler/',
153 'wp-parsely'
154 ),
155 'resolve' => $resolve,
156 );
157 register_graphql_field( 'ContentNode', self::FIELD_NAME, $config );
158 }
159 }
160