Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AutoPublish
100.00% covered (success)
100.00%
38 / 38
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%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Setting: Auto-publish
7 *
8 * @package Beyondwords\Wordpress
9 * @author  Stuart McAlpine <stu@beyondwords.io>
10 * @since   5.1.0
11 */
12
13namespace Beyondwords\Wordpress\Component\Settings\Fields\AutoPublish;
14
15use Beyondwords\Wordpress\Component\Settings\Sync;
16
17/**
18 * AutoPublish
19 *
20 * @since 5.1.0
21 */
22defined('ABSPATH') || exit;
23
24class AutoPublish
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_auto_publish_enabled';
39
40    /**
41     * Init.
42     *
43     * @since 5.1.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.1.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-auto-publish',
78            __('Auto-publish', 'speechkit'),
79            [self::class, 'render'],
80            'beyondwords_content',
81            'content'
82        );
83    }
84
85    /**
86     * Render setting field.
87     *
88     * @since 5.1.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
108                esc_html_e(
109                    'When auto-publish is disabled all audio content created in WordPress will need to be manually published in the BeyondWords dashboard',  // phpcs:ignore Generic.Files.LineLength.TooLong
110                    'speechkit'
111                );
112                ?>
113            </label>
114        </div>
115        <?php
116    }
117}