Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.35% |
41 / 43 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| PlayerContent | |
95.24% |
40 / 42 |
|
66.67% |
2 / 3 |
12 | |
0.00% |
0 / 1 |
| init | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| element | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
2 | |||
| save | |
85.71% |
12 / 14 |
|
0.00% |
0 / 1 |
7.14 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * BeyondWords Component: Player content |
| 7 | * |
| 8 | * @package Beyondwords\Wordpress |
| 9 | * @author Stuart McAlpine <stu@beyondwords.io> |
| 10 | * @since 5.3.0 |
| 11 | */ |
| 12 | |
| 13 | namespace Beyondwords\Wordpress\Component\Post\PlayerContent; |
| 14 | |
| 15 | use Beyondwords\Wordpress\Component\Settings\SettingsUtils; |
| 16 | |
| 17 | /** |
| 18 | * PlayerContent |
| 19 | * |
| 20 | * @since 5.3.0 |
| 21 | */ |
| 22 | defined('ABSPATH') || exit; |
| 23 | |
| 24 | class PlayerContent |
| 25 | { |
| 26 | /** |
| 27 | * Options. |
| 28 | * |
| 29 | * @since 5.3.0 Introduced. |
| 30 | * |
| 31 | * @var array Associative array of player content values and labels. |
| 32 | */ |
| 33 | public const OPTIONS = [ |
| 34 | [ |
| 35 | 'value' => '', |
| 36 | 'label' => 'Article' |
| 37 | ], |
| 38 | [ |
| 39 | 'value' => 'summary', |
| 40 | 'label' => 'Summary' |
| 41 | ], |
| 42 | ]; |
| 43 | |
| 44 | /** |
| 45 | * Constructor |
| 46 | * |
| 47 | * @since 5.3.0 Introduced. |
| 48 | * @since 6.0.0 Make static. |
| 49 | */ |
| 50 | public static function init() |
| 51 | { |
| 52 | add_action('wp_loaded', function (): void { |
| 53 | $postTypes = SettingsUtils::getCompatiblePostTypes(); |
| 54 | |
| 55 | if (is_array($postTypes)) { |
| 56 | foreach ($postTypes as $postType) { |
| 57 | add_action("save_post_{$postType}", [self::class, 'save'], 10); |
| 58 | } |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * HTML output for this component. |
| 65 | * |
| 66 | * @since 5.3.0 Introduced. |
| 67 | * @since 6.0.0 Make static. |
| 68 | * |
| 69 | * @param \WP_Post $post The post object. |
| 70 | * |
| 71 | * @return string|null |
| 72 | */ |
| 73 | public static function element($post) |
| 74 | { |
| 75 | $playerContent = get_post_meta($post->ID, 'beyondwords_player_content', true); |
| 76 | |
| 77 | wp_nonce_field('beyondwords_player_content', 'beyondwords_player_content_nonce'); |
| 78 | ?> |
| 79 | <p |
| 80 | id="beyondwords-metabox-player-content" |
| 81 | class="post-attributes-label-wrapper page-template-label-wrapper" |
| 82 | > |
| 83 | <label class="post-attributes-label" for="beyondwords_player_content"> |
| 84 | <?php esc_html_e('Player content', 'speechkit'); ?> |
| 85 | </label> |
| 86 | </p> |
| 87 | <select id="beyondwords_player_content" name="beyondwords_player_content" style="width: 100%;"> |
| 88 | <?php |
| 89 | foreach (self::OPTIONS as $option) { |
| 90 | printf( |
| 91 | '<option value="%s" %s %s>%s</option>', |
| 92 | esc_attr($option['value']), |
| 93 | selected(strval($option['value']), $playerContent), |
| 94 | disabled($option['disabled'] ?? false, true), |
| 95 | esc_html($option['label']) |
| 96 | ); |
| 97 | } |
| 98 | ?> |
| 99 | </select> |
| 100 | <?php |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Save the meta when the post is saved. |
| 105 | * |
| 106 | * @since 5.3.0 Introduced. |
| 107 | * @since 6.0.0 Make static. |
| 108 | * |
| 109 | * @param int $postId The ID of the post being saved. |
| 110 | */ |
| 111 | public static function save($postId) |
| 112 | { |
| 113 | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 114 | return $postId; |
| 115 | } |
| 116 | |
| 117 | // "save_post" can be triggered at other times, so verify this request came from the our component |
| 118 | if (! isset($_POST['beyondwords_player_content']) || ! isset($_POST['beyondwords_player_content_nonce'])) { |
| 119 | return $postId; |
| 120 | } |
| 121 | |
| 122 | // "save_post" can be triggered at other times, so verify this request came from the our component |
| 123 | if ( |
| 124 | ! wp_verify_nonce( |
| 125 | sanitize_key($_POST['beyondwords_player_content_nonce']), |
| 126 | 'beyondwords_player_content' |
| 127 | ) |
| 128 | ) { |
| 129 | return $postId; |
| 130 | } |
| 131 | |
| 132 | $playerContent = sanitize_text_field(wp_unslash($_POST['beyondwords_player_content'])); |
| 133 | |
| 134 | if (! empty($playerContent)) { |
| 135 | update_post_meta($postId, 'beyondwords_player_content', $playerContent); |
| 136 | } else { |
| 137 | delete_post_meta($postId, 'beyondwords_player_content'); |
| 138 | } |
| 139 | |
| 140 | return $postId; |
| 141 | } |
| 142 | } |