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