Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ConfigBuilder | |
100.00% |
25 / 25 |
|
100.00% |
2 / 2 |
10 | |
100.00% |
1 / 1 |
| build | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| merge_post_settings | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
8 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Builds the parameters object passed to the BeyondWords JS SDK. |
| 4 | * |
| 5 | * @package BeyondWords\Player |
| 6 | * @since 6.0.0 |
| 7 | * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. |
| 8 | */ |
| 9 | |
| 10 | declare( strict_types = 1 ); |
| 11 | |
| 12 | namespace BeyondWords\Player; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * Constructs the SDK parameters object for one post. |
| 18 | * |
| 19 | * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods. |
| 20 | */ |
| 21 | class ConfigBuilder { |
| 22 | |
| 23 | /** |
| 24 | * Build the SDK parameters object for a post. |
| 25 | * |
| 26 | * @param \WP_Post $post WordPress post object. |
| 27 | * |
| 28 | * @return object Parameters for the JS SDK. |
| 29 | */ |
| 30 | public static function build( \WP_Post $post ): object { |
| 31 | $project_id = \BeyondWords\Post\Meta::get_project_id( $post->ID ); |
| 32 | |
| 33 | $params = [ |
| 34 | 'projectId' => is_numeric( $project_id ) ? (int) $project_id : $project_id, |
| 35 | ]; |
| 36 | |
| 37 | $params = self::merge_post_settings( $post, $params ); |
| 38 | |
| 39 | return (object) apply_filters( 'beyondwords_player_sdk_params', $params, $post->ID ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Merge per-post overrides into the SDK parameters. |
| 44 | * |
| 45 | * For Magic Embed (client-side) integration without a content ID we flip |
| 46 | * to source-ID mode so the SDK fetches by source post ID instead. |
| 47 | * |
| 48 | * @param \WP_Post $post Post object. |
| 49 | * @param array<string,mixed> $params Existing params. |
| 50 | * |
| 51 | * @return array<string,mixed> |
| 52 | */ |
| 53 | public static function merge_post_settings( \WP_Post $post, array $params ): array { |
| 54 | $content_id = \BeyondWords\Post\Meta::get_content_id( $post->ID ); |
| 55 | |
| 56 | if ( ! empty( $content_id ) ) { |
| 57 | $params['contentId'] = (string) $content_id; |
| 58 | } |
| 59 | |
| 60 | if ( \BeyondWords\Settings\Fields::PLAYER_UI_HEADLESS === get_option( \BeyondWords\Settings\Fields::OPTION_PLAYER_UI ) ) { |
| 61 | $params['showUserInterface'] = false; |
| 62 | } |
| 63 | |
| 64 | // "Script" is the AI-summarization output, so script → summary:true. |
| 65 | // None/audio_post add nothing — the SDK defaults to audio + body. |
| 66 | $embed = \BeyondWords\Editor\Components\SettingsFields::get_effective_embed( $post->ID ); |
| 67 | |
| 68 | switch ( $embed ) { |
| 69 | case \BeyondWords\Editor\Components\SettingsFields::EMBED_AUDIO_SCRIPT: |
| 70 | $params['summary'] = true; |
| 71 | break; |
| 72 | case \BeyondWords\Editor\Components\SettingsFields::EMBED_VIDEO_POST: |
| 73 | $params['video'] = true; |
| 74 | break; |
| 75 | case \BeyondWords\Editor\Components\SettingsFields::EMBED_VIDEO_SCRIPT: |
| 76 | $params['video'] = true; |
| 77 | $params['summary'] = true; |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | $method = \BeyondWords\Settings\Fields::get_integration_method( $post ); |
| 82 | |
| 83 | if ( \BeyondWords\Settings\Fields::INTEGRATION_CLIENT_SIDE === $method && empty( $params['contentId'] ) ) { |
| 84 | $params['clientSideEnabled'] = true; |
| 85 | $params['sourceId'] = (string) $post->ID; |
| 86 | unset( $params['contentId'] ); |
| 87 | } |
| 88 | |
| 89 | return $params; |
| 90 | } |
| 91 | } |