| @@ -1,11 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * GraphQL support class. | |
| 4 | - * Note: this class will only work if the WPGraphQL plugin is installed. | |
| 3 | + * Endpoints: GraphQL metadata endpoint class | |
| 5 | 4 | * |
| 5 | + * Adds support for GraphQL. | |
| 6 | + * Note: This class will only work if the WPGraphQL plugin is installed. | |
| 7 | + * | |
| 6 | 8 | * @package Parsely |
| 7 | - * @since 3.2.0 | |
| 9 | + * @since 3.2.0 | |
| 8 | 10 | */ |
| 9 | 11 | |
| 10 | 12 | declare(strict_types=1); |
| 11 | 13 | |
| @@ -13,9 +15,9 @@ | ||
| 13 | 15 | |
| 14 | 16 | use WP_Post; |
| 15 | 17 | |
| 16 | 18 | /** |
| 17 | - * Add Parse.ly metadata fields to the WP GraphQL server. | |
| 19 | + * Adds Parse.ly metadata fields to the WP GraphQL server. | |
| 18 | 20 | * |
| 19 | 21 | * @since 3.2.0 |
| 20 | 22 | */ |
| 21 | 23 | class GraphQL_Metadata extends Metadata_Endpoint { |
| @@ -22,33 +24,30 @@ | ||
| 22 | 24 | private const GRAPHQL_VERSION = '1.0.0'; |
| 23 | 25 | private const GRAPHQL_CONTAINER_TYPE = 'ParselyMetaContainer'; |
| 24 | 26 | |
| 25 | 27 | /** |
| 26 | - * Register fields in WPGraphQL plugin. | |
| 28 | + * Registers fields in WPGraphQL plugin. | |
| 27 | 29 | * |
| 28 | 30 | * @since 3.2.0 |
| 29 | - * | |
| 30 | - * @return void | |
| 31 | 31 | */ |
| 32 | 32 | public function run(): void { |
| 33 | 33 | /** |
| 34 | - * Filter whether GraphQL support is enabled or not. | |
| 34 | + * Filters whether GraphQL support is enabled or not. | |
| 35 | 35 | * |
| 36 | 36 | * @since 3.2.0 |
| 37 | 37 | * |
| 38 | 38 | * @param bool $enabled True if enabled, false if not. |
| 39 | 39 | */ |
| 40 | - if ( apply_filters( 'wp_parsely_enable_graphql_support', true ) && $this->parsely->api_key_is_set() ) { | |
| 40 | + if ( apply_filters( 'wp_parsely_enable_graphql_support', true ) && $this->parsely->site_id_is_set() ) { | |
| 41 | 41 | add_action( 'graphql_register_types', array( $this, 'register_meta' ) ); |
| 42 | 42 | } |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | - * Register the meta field on the appropriate resource types in the REST API. | |
| 46 | + * Registers the meta field on the appropriate resource types in the REST | |
| 47 | + * API. | |
| 47 | 48 | * |
| 48 | 49 | * @since 3.2.0 |
| 49 | - * | |
| 50 | - * @return void | |
| 51 | 50 | */ |
| 52 | 51 | public function register_meta(): void { |
| 53 | 52 | $this->register_object_types(); |
| 54 | 53 | $this->register_fields(); |
| @@ -54,13 +53,12 @@ | ||
| 54 | 53 | $this->register_fields(); |
| 55 | 54 | } |
| 56 | 55 | |
| 57 | 56 | /** |
| 58 | - * Register the new custom types for Parse.ly Metadata into the GraphQL instance. | |
| 57 | + * Registers the new custom types for Parse.ly Metadata into the GraphQL | |
| 58 | + * instance. | |
| 59 | 59 | * |
| 60 | 60 | * @since 3.2.0 |
| 61 | - * | |
| 62 | - * @return void | |
| 63 | 61 | */ |
| 64 | 62 | private function register_object_types(): void { |
| 65 | 63 | $container_type = array( |
| 66 | 64 | 'description' => __( 'Parse.ly Metadata root type.', 'wp-parsely' ), |
| @@ -78,9 +76,9 @@ | ||
| 78 | 76 | 'description' => __( |
| 79 | 77 | 'HTML string containing the metadata in JSON-LD. Intended to be rendered in the front-end as is.', |
| 80 | 78 | 'wp-parsely' |
| 81 | 79 | ), |
| 82 | - 'resolve' => function() { | |
| 80 | + 'resolve' => function () { | |
| 83 | 81 | return self::get_rendered_meta( 'repeated_metas' ); |
| 84 | 82 | }, |
| 85 | 83 | ), |
| 86 | 84 | 'jsonLd' => array( |
| @@ -94,9 +92,9 @@ | ||
| 94 | 92 | 'description' => __( |
| 95 | 93 | 'HTML string containing the metadata in JSON-LD. Intended to be rendered in the front-end as is.', |
| 96 | 94 | 'wp-parsely' |
| 97 | 95 | ), |
| 98 | - 'resolve' => function( array $parsely_meta, array $args ) { | |
| 96 | + 'resolve' => function ( array $parsely_meta, array $args ) { | |
| 99 | 97 | $json_ld = self::get_rendered_meta( 'json_ld' ); |
| 100 | 98 | |
| 101 | 99 | if ( isset( $args['removeWrappingTag'] ) && true === $args['removeWrappingTag'] ) { |
| 102 | 100 | // phpcs:ignore WordPressVIPMinimum.Functions.StripTags.StripTagsOneParameter |
| @@ -119,25 +117,27 @@ | ||
| 119 | 117 | register_graphql_object_type( self::GRAPHQL_CONTAINER_TYPE, $container_type ); |
| 120 | 118 | } |
| 121 | 119 | |
| 122 | 120 | /** |
| 123 | - * Register the custom metadata fields, so they can be queried in GraphQL. | |
| 121 | + * Registers the custom metadata fields, so they can be queried in GraphQL. | |
| 124 | 122 | * |
| 125 | 123 | * @since 3.2.0 |
| 126 | - * | |
| 127 | - * @return void | |
| 128 | 124 | */ |
| 129 | 125 | private function register_fields(): void { |
| 130 | 126 | $resolve = function ( \WPGraphQL\Model\Post $graphql_post ) { |
| 131 | 127 | $post_id = $graphql_post->ID; |
| 132 | - $post = WP_Post::get_instance( $post_id ); | |
| 133 | 128 | |
| 129 | + if ( ! is_int( $post_id ) ) { | |
| 130 | + return null; | |
| 131 | + } | |
| 132 | + | |
| 133 | + $post = WP_Post::get_instance( $post_id ); | |
| 134 | + | |
| 134 | 135 | if ( false === $post ) { |
| 135 | 136 | return null; |
| 136 | 137 | } |
| 137 | 138 | |
| 138 | - $options = $this->parsely->get_options(); | |
| 139 | - $object_types = array_unique( array_merge( $options['track_post_types'], $options['track_page_types'] ) ); | |
| 139 | + $object_types = $this->parsely->get_all_track_types(); | |
| 140 | 140 | $current_object_type = get_post_type( $post ); |
| 141 | 141 | |
| 142 | 142 | return array( |
| 143 | 143 | 'version' => self::GRAPHQL_VERSION, |
| @@ -148,9 +148,9 @@ | ||
| 148 | 148 | |
| 149 | 149 | $config = array( |
| 150 | 150 | 'type' => self::GRAPHQL_CONTAINER_TYPE, |
| 151 | 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.', | |
| 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 | 153 | 'wp-parsely' |
| 154 | 154 | ), |
| 155 | 155 | 'resolve' => $resolve, |
| 156 | 156 | ); |