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