Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
IncludeTitle
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 init
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 addSetting
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Setting: IncludeTitle
7 *
8 * @package Beyondwords\Wordpress
9 * @author  Stuart McAlpine <stu@beyondwords.io>
10 * @since   5.0.0
11 */
12
13namespace Beyondwords\Wordpress\Component\Settings\Fields\IncludeTitle;
14
15use Beyondwords\Wordpress\Component\Settings\Sync;
16
17/**
18 * IncludeTitle
19 *
20 * @since 5.0.0
21 */
22defined('ABSPATH') || exit;
23
24class IncludeTitle
25{
26    /**
27     * Default value.
28     *
29     * @var string
30     */
31    public const DEFAULT_VALUE = true;
32
33    /**
34     * Option name.
35     *
36     * @var string
37     */
38    public const OPTION_NAME = 'beyondwords_project_title_enabled';
39
40    /**
41     * Init.
42     *
43     * @since 5.0.0
44     * @since 6.0.0 Make static.
45     */
46    public static function init()
47    {
48        add_action('admin_init', [self::class, 'addSetting']);
49        add_action('pre_update_option_' . self::OPTION_NAME, function ($value) {
50            Sync::syncOptionToDashboard(self::OPTION_NAME);
51            return $value;
52        });
53        add_filter('option_' . self::OPTION_NAME, 'rest_sanitize_boolean');
54    }
55
56    /**
57     * Init setting.
58     *
59     * @since 5.0.0
60     * @since 6.0.0 Make static.
61     *
62     * @return void
63     */
64    public static function addSetting()
65    {
66        register_setting(
67            'beyondwords_content_settings',
68            self::OPTION_NAME,
69            [
70                'type'              => 'boolean',
71                'sanitize_callback' => 'rest_sanitize_boolean',
72                'default'           => self::DEFAULT_VALUE,
73            ]
74        );
75
76        add_settings_field(
77            'beyondwords-include-title',
78            __('Title', 'speechkit'),
79            [self::class, 'render'],
80            'beyondwords_content',
81            'content'
82        );
83    }
84
85    /**
86     * Render setting field.
87     *
88     * @since 5.0.0
89     * @since 6.0.0 Make static.
90     *
91     * @return void
92     **/
93    public static function render()
94    {
95        $value = get_option(self::OPTION_NAME);
96        ?>
97        <div>
98            <label>
99                <input type="hidden" name="<?php echo esc_attr(self::OPTION_NAME); ?>" value="" />
100                <input
101                    type="checkbox"
102                    id="<?php echo esc_attr(self::OPTION_NAME); ?>"
103                    name="<?php echo esc_attr(self::OPTION_NAME); ?>"
104                    value="1"
105                    <?php checked($value); ?>
106                />
107                <?php esc_html_e('Include title in audio', 'speechkit'); ?>
108            </label>
109        </div>
110        <?php
111    }
112}