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