Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.29% |
33 / 35 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| DisplayPlayer | |
94.12% |
32 / 34 |
|
66.67% |
2 / 3 |
11.02 | |
0.00% |
0 / 1 |
| init | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| save | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
6.17 | |||
| element | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * BeyondWords Display Player element. |
| 7 | * |
| 8 | * @package Beyondwords\Wordpress |
| 9 | * @author Stuart McAlpine <stu@beyondwords.io> |
| 10 | * @since 3.0.0 |
| 11 | */ |
| 12 | |
| 13 | namespace Beyondwords\Wordpress\Component\Post\DisplayPlayer; |
| 14 | |
| 15 | use Beyondwords\Wordpress\Component\Post\PostMetaUtils; |
| 16 | use Beyondwords\Wordpress\Component\Settings\SettingsUtils; |
| 17 | |
| 18 | /** |
| 19 | * PostMetabox |
| 20 | * |
| 21 | * @since 3.0.0 |
| 22 | */ |
| 23 | defined('ABSPATH') || exit; |
| 24 | |
| 25 | class DisplayPlayer |
| 26 | { |
| 27 | /** |
| 28 | * Init. |
| 29 | * |
| 30 | * @since 4.0.0 |
| 31 | * @since 6.0.0 Make static. |
| 32 | */ |
| 33 | public static function init() |
| 34 | { |
| 35 | add_action('wp_loaded', function (): void { |
| 36 | $postTypes = SettingsUtils::getCompatiblePostTypes(); |
| 37 | |
| 38 | if (is_array($postTypes)) { |
| 39 | foreach ($postTypes as $postType) { |
| 40 | add_action("save_post_{$postType}", [self::class, 'save'], 20); |
| 41 | } |
| 42 | } |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Save the meta when the post is saved. |
| 48 | * |
| 49 | * @since 6.0.0 Make static. |
| 50 | * |
| 51 | * @param int $postId The ID of the post being saved. |
| 52 | */ |
| 53 | public static function save($postId) |
| 54 | { |
| 55 | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 56 | return $postId; |
| 57 | } |
| 58 | |
| 59 | // "save_post" can be triggered at other times, so verify this request came from the our component |
| 60 | if ( |
| 61 | ! isset($_POST['beyondwords_display_player_nonce']) || |
| 62 | ! wp_verify_nonce( |
| 63 | sanitize_key($_POST['beyondwords_display_player_nonce']), |
| 64 | 'beyondwords_display_player' |
| 65 | ) |
| 66 | ) { |
| 67 | return $postId; |
| 68 | } |
| 69 | |
| 70 | if (isset($_POST['beyondwords_display_player'])) { |
| 71 | update_post_meta($postId, 'beyondwords_disabled', ''); |
| 72 | } else { |
| 73 | update_post_meta($postId, 'beyondwords_disabled', '1'); |
| 74 | } |
| 75 | |
| 76 | return $postId; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Render the element. |
| 81 | * |
| 82 | * @since 6.0.0 Make static, fix checkbox checked bug. |
| 83 | * |
| 84 | * @param \WP_Post $post The post object. |
| 85 | */ |
| 86 | public static function element($post) |
| 87 | { |
| 88 | if (!($post instanceof \WP_Post)) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | wp_nonce_field('beyondwords_display_player', 'beyondwords_display_player_nonce'); |
| 93 | |
| 94 | $displayPlayer = ! PostMetaUtils::getDisabled($post->ID); |
| 95 | ?> |
| 96 | <!-- checkbox --> |
| 97 | <p id="beyondwords-metabox-display-player"> |
| 98 | <input |
| 99 | type="checkbox" |
| 100 | id="beyondwords_display_player" |
| 101 | name="beyondwords_display_player" |
| 102 | value="1" |
| 103 | <?php checked($displayPlayer); ?> |
| 104 | /> |
| 105 | <?php esc_html_e('Display player', 'speechkit'); ?> |
| 106 | </p> |
| 107 | <?php |
| 108 | } |
| 109 | } |