Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
2.56% covered (danger)
2.56%
1 / 39
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPGraphQL
2.56% covered (danger)
2.56%
1 / 39
50.00% covered (danger)
50.00%
1 / 2
67.20
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
 graphqlRegisterTypes
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3declare(strict_types=1);
4
5namespace Beyondwords\Wordpress\Compatibility\WPGraphQL;
6
7use Beyondwords\Wordpress\Component\Post\PostMetaUtils;
8use Beyondwords\Wordpress\Component\Settings\SettingsUtils;
9use WPGraphQL as WPGraphQLPlugin;
10
11/**
12 * Expose BeyondWords fields in WPGraphQL.
13 *
14 * @since 3.6.0
15 * @since 4.7.0 Moved graphqlRegisterTypes() from Beyondwords\Wordpress\Core to here.
16 */
17class WPGraphQL
18{
19    /**
20     * Init.
21     *
22     * @since 3.6.0
23     */
24    public function init()
25    {
26        // Actions for WPGraphQL
27        add_action('graphql_register_types', array($this, 'graphqlRegisterTypes'));
28    }
29
30    /**
31     * GraphQL: Register types.
32     *
33     * @since 3.6.0
34     * @since 4.0.0 Register contentId field, and contentId/podcastId are now String, not Int
35     * @since 4.7.0 Moved graphqlRegisterTypes() from Beyondwords\Wordpress\Core to here.
36     */
37    public function graphqlRegisterTypes()
38    {
39        register_graphql_object_type('Beyondwords', [
40            'description' => __('BeyondWords audio details. Use this data to embed an audio player using the BeyondWords JavaScript SDK.', 'speechkit'), // phpcs:ignore Generic.Files.LineLength.TooLong
41            'fields' => [
42                'projectId' => [
43                    'description' => __('BeyondWords project ID', 'speechkit'),
44                    'type' => 'Int'
45                ],
46                'contentId' => [
47                    'description' => __('BeyondWords content ID', 'speechkit'),
48                    'type' => 'String'
49                ],
50                'podcastId' => [
51                    'description' => __('BeyondWords legacy podcast ID', 'speechkit'),
52                    'type' => 'String'
53                ],
54            ],
55        ]);
56
57        $beyondwordsPostTypes = SettingsUtils::getCompatiblePostTypes();
58
59        $graphqlPostTypes = WPGraphQLPlugin::get_allowed_post_types();
60
61        $postTypes = array_intersect($beyondwordsPostTypes, $graphqlPostTypes);
62
63        if (! empty($postTypes) && is_array($postTypes)) {
64            foreach ($postTypes as $postType) {
65                $postTypeObject = get_post_type_object($postType);
66
67                register_graphql_field($postTypeObject->graphql_single_name, 'beyondwords', [
68                    'type'        => 'Beyondwords',
69                    'description' => __('BeyondWords audio details', 'speechkit'),
70                    'resolve'     => function (WPGraphQLPlugin\Model\Post $post) {
71                        $beyondwords = [];
72
73                        $contentId = PostMetaUtils::getContentId($post->ID);
74
75                        if (! empty($contentId)) {
76                            $beyondwords['contentId'] = $contentId;
77                            $beyondwords['podcastId'] = $contentId; // legacy
78                        }
79
80                        $projectId = PostMetaUtils::getProjectId($post->ID);
81
82                        if (! empty($projectId)) {
83                            $beyondwords['projectId'] = $projectId;
84                        }
85
86                        return ! empty($beyondwords) ? $beyondwords : null;
87                    }
88                ]);
89            }
90        }
91    }
92}