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

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

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