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