Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
IncludeExcerpt
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 init
100.00% covered (success)
100.00%
2 / 2
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: Include excerpt
7 *
8 * @package Beyondwords\Wordpress
9 * @author  Stuart McAlpine <stu@beyondwords.io>
10 * @since   3.0.0
11 * @since   5.0.0 Rename labels from "Prepend excerpt" to "Include excerpt".
12 */
13
14namespace Beyondwords\Wordpress\Component\Settings\Fields\IncludeExcerpt;
15
16/**
17 * IncludeExcerpt
18 *
19 * @since 3.0.0
20 */
21defined('ABSPATH') || exit;
22
23class IncludeExcerpt
24{
25    /**
26     * Default value.
27     *
28     * @var string
29     */
30    public const DEFAULT_VALUE = false;
31
32    /**
33     * Option name.
34     *
35     * @var string
36     */
37    public const OPTION_NAME = 'beyondwords_prepend_excerpt';
38
39    /**
40     * Init.
41     *
42     * @since 4.0.0
43     * @since 6.0.0 Make static.
44     */
45    public static function init()
46    {
47        add_action('admin_init', [self::class, 'addSetting']);
48        add_filter('option_' . self::OPTION_NAME, 'rest_sanitize_boolean');
49    }
50
51    /**
52     * Init setting.
53     *
54     * @since 3.0.0
55     * @since 5.0.0 Rename field.
56     * @since 6.0.0 Make static.
57     *
58     * @return void
59     */
60    public static function addSetting()
61    {
62        register_setting(
63            'beyondwords_content_settings',
64            self::OPTION_NAME,
65            [
66                'type'              => 'boolean',
67                'sanitize_callback' => 'rest_sanitize_boolean',
68                'default'           => self::DEFAULT_VALUE,
69            ]
70        );
71
72        add_settings_field(
73            'beyondwords-include-excerpt',
74            __('Excerpt', 'speechkit'),
75            [self::class, 'render'],
76            'beyondwords_content',
77            'content'
78        );
79    }
80
81    /**
82     * Render setting field.
83     *
84     * @since 3.0.0
85     * @since 4.0.0 Updated label and description
86     * @since 5.0.0 Updated label and description
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 excerpts in audio', 'speechkit'); ?>
106            </label>
107        </div>
108        <?php
109    }
110}