Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
1.89% covered (danger)
1.89%
1 / 53
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPGraphQL
1.89% covered (danger)
1.89%
1 / 53
50.00% covered (danger)
50.00%
1 / 2
40.00
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 graphql_register_types
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * WPGraphQL compatibility: a BeyondWords GraphQL type + field on compatible post types.
4 *
5 * @package BeyondWords\Compatibility
6 * @since   3.6.0
7 * @since   7.0.0 Refactored to BeyondWords namespace with snake_case methods.
8 */
9
10declare( strict_types = 1 );
11
12namespace BeyondWords\Compatibility;
13
14defined( 'ABSPATH' ) || exit;
15
16/**
17 * Expose BeyondWords fields in WPGraphQL.
18 *
19 * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods.
20 */
21class WPGraphQL {
22
23    /**
24     * Register WordPress hooks.
25     */
26    public static function init(): void {
27        add_action( 'graphql_register_types', [ self::class, 'graphql_register_types' ] );
28    }
29
30    /**
31     * Register the BeyondWords GraphQL type and attach it to compatible post types.
32     */
33    public static function graphql_register_types(): void {
34        register_graphql_object_type(
35            'Beyondwords',
36            [
37                'description' => __( 'BeyondWords audio details. Use this data to embed an audio player using the BeyondWords JavaScript SDK.', 'speechkit' ),
38                'fields'      => [
39                    'sourceId'  => [
40                        'description' => __( 'BeyondWords source ID', 'speechkit' ),
41                        'type'        => 'String',
42                    ],
43                    'projectId' => [
44                        'description' => __( 'BeyondWords project ID', 'speechkit' ),
45                        'type'        => 'Int',
46                    ],
47                    'contentId' => [
48                        'description' => __( 'BeyondWords content ID', 'speechkit' ),
49                        'type'        => 'String',
50                    ],
51                    'podcastId' => [
52                        'description' => __( 'BeyondWords legacy podcast ID', 'speechkit' ),
53                        'type'        => 'String',
54                    ],
55                ],
56            ]
57        );
58
59        $bw_post_types      = \BeyondWords\Settings\Utils::get_compatible_post_types();
60        $graphql_post_types = \WPGraphQL::get_allowed_post_types();
61        $post_types         = array_intersect( $bw_post_types, $graphql_post_types );
62
63        foreach ( $post_types as $post_type ) {
64            $post_type_object = get_post_type_object( $post_type );
65
66            if ( ! $post_type_object ) {
67                continue;
68            }
69
70            register_graphql_field(
71                $post_type_object->graphql_single_name,
72                'beyondwords',
73                [
74                    'type'        => 'Beyondwords',
75                    'description' => __( 'BeyondWords audio details', 'speechkit' ),
76                    'resolve'     => static function ( \WPGraphQL\Model\Post $post ) {
77                        $fields = [
78                            'sourceId' => (string) $post->ID,
79                        ];
80
81                        $project_id = \BeyondWords\Post\Meta::get_project_id( $post->ID );
82                        if ( ! empty( $project_id ) ) {
83                            $fields['projectId'] = $project_id;
84                        }
85
86                        $content_id = \BeyondWords\Post\Meta::get_content_id( $post->ID );
87                        if ( ! empty( $content_id ) ) {
88                            $fields['contentId'] = $content_id;
89                            $fields['podcastId'] = $content_id;
90                        }
91
92                        return $fields;
93                    },
94                ]
95            );
96        }
97    }
98}