Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.43% covered (success)
96.43%
81 / 84
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Notices
96.39% covered (success)
96.39%
80 / 83
40.00% covered (danger)
40.00%
2 / 5
21
0.00% covered (danger)
0.00%
0 / 1
 init
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 generatedNotice
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
5
 deletedNotice
95.45% covered (success)
95.45%
21 / 22
0.00% covered (danger)
0.00%
0 / 1
5
 failedNotice
95.45% covered (success)
95.45%
21 / 22
0.00% covered (danger)
0.00%
0 / 1
5
 errorNotice
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
5.01
1<?php
2
3declare(strict_types=1);
4
5/**
6 * BeyondWords BulkEdit Notices.
7 *
8 * Text Domain: beyondwords
9 *
10 * @package Beyondwords\Wordpress
11 * @author  Stuart McAlpine <stu@beyondwords.io>
12 * @since   3.0.0
13 */
14
15namespace Beyondwords\Wordpress\Component\Posts\BulkEdit;
16
17/**
18 * Notices
19 *
20 * @since 4.5.0
21 */
22defined('ABSPATH') || exit;
23
24class Notices
25{
26    /**
27     * Constructor
28     *
29     * @since 6.0.0 Make static.
30     */
31    public static function init()
32    {
33        add_action('admin_notices', [self::class, 'generatedNotice']);
34        add_action('admin_notices', [self::class, 'deletedNotice']);
35        add_action('admin_notices', [self::class, 'failedNotice']);
36        add_action('admin_notices', [self::class, 'errorNotice']);
37    }
38
39    /**
40     * Generated audio notice.
41     *
42     * @since 4.1.0
43     * @since 6.0.0 Make static.
44     */
45    public static function generatedNotice()
46    {
47        if (
48            ! isset($_GET['beyondwords_bulk_edit_result_nonce'])
49            || ! isset($_GET['beyondwords_bulk_generated'])
50        ) {
51            return;
52        }
53
54        if (! wp_verify_nonce(sanitize_key($_GET['beyondwords_bulk_edit_result_nonce']), 'beyondwords_bulk_edit_result')) { // phpcs:ignore Generic.Files.LineLength.TooLong
55            wp_nonce_ays('');
56        }
57
58        $count = intval(sanitize_text_field(wp_unslash($_GET['beyondwords_bulk_generated'])));
59
60        if ($count) {
61            $message = sprintf(
62                /* translators: %d is replaced with the number of posts processed */
63                _n(
64                    'Audio was requested for %d post.',
65                    'Audio was requested for %d posts.',
66                    $count,
67                    'speechkit'
68                ),
69                $count
70            );
71            ?>
72            <div id="beyondwords-bulk-edit-notice-generated" class="notice notice-info is-dismissible">
73                <p>
74                    <?php echo esc_html($message); ?>
75                </p>
76            </div>
77            <?php
78        }
79    }
80
81    /**
82     * Deleted audio notice.
83     *
84     * @since 6.0.0 Make static.
85     */
86    public static function deletedNotice()
87    {
88        if (
89            ! isset($_GET['beyondwords_bulk_edit_result_nonce'])
90            || ! isset($_GET['beyondwords_bulk_deleted'])
91        ) {
92            return;
93        }
94
95        if (! wp_verify_nonce(sanitize_key($_GET['beyondwords_bulk_edit_result_nonce']), 'beyondwords_bulk_edit_result')) { // phpcs:ignore Generic.Files.LineLength.TooLong
96            wp_nonce_ays('');
97        }
98
99        $count = intval(sanitize_text_field(wp_unslash($_GET['beyondwords_bulk_deleted'])));
100
101        if ($count) {
102            $message = sprintf(
103                /* translators: %d is replaced with the number of posts processed */
104                _n(
105                    'Audio was deleted for %d post.',
106                    'Audio was deleted for %d posts.',
107                    $count,
108                    'speechkit'
109                ),
110                $count
111            );
112            ?>
113            <div id="beyondwords-bulk-edit-notice-deleted" class="notice notice-info is-dismissible">
114                <p>
115                    <?php echo esc_html($message); ?>
116                </p>
117            </div>
118            <?php
119        }
120    }
121
122    /**
123     * Failed audio notice.
124     *
125     * @since 6.0.0 Make static.
126     */
127    public static function failedNotice()
128    {
129        if (
130            ! isset($_GET['beyondwords_bulk_edit_result_nonce'])
131            || ! isset($_GET['beyondwords_bulk_failed'])
132        ) {
133            return;
134        }
135
136        if (! wp_verify_nonce(sanitize_key($_GET['beyondwords_bulk_edit_result_nonce']), 'beyondwords_bulk_edit_result')) { // phpcs:ignore Generic.Files.LineLength.TooLong
137            wp_nonce_ays('');
138        }
139
140        $count = intval(sanitize_text_field(wp_unslash($_GET['beyondwords_bulk_failed'])));
141
142        if ($count) {
143            $message = sprintf(
144                /* translators: %d is replaced with the number of posts that were skipped */
145                _n(
146                    '%d post failed, check for errors in the BeyondWords column below.',
147                    '%d posts failed, check for errors in the BeyondWords column below.',
148                    $count,
149                    'speechkit'
150                ),
151                $count
152            );
153            ?>
154            <div id="beyondwords-bulk-edit-notice-failed" class="notice notice-error is-dismissible">
155                <p>
156                    <?php echo esc_html($message); ?>
157                </p>
158            </div>
159            <?php
160        }
161    }
162
163    /**
164     * Error notice.
165     *
166     * @since 6.0.0 Make static.
167     */
168    public static function errorNotice()
169    {
170        if (
171            ! isset($_GET['beyondwords_bulk_edit_result_nonce'])
172            || ! isset($_GET['beyondwords_bulk_error'])
173        ) {
174            return;
175        }
176
177        if (! wp_verify_nonce(sanitize_key($_GET['beyondwords_bulk_edit_result_nonce']), 'beyondwords_bulk_edit_result')) { // phpcs:ignore Generic.Files.LineLength.TooLong
178            wp_nonce_ays('');
179        }
180
181        $message = sanitize_text_field(wp_unslash($_GET['beyondwords_bulk_error']));
182
183        if ($message) {
184            ?>
185            <div id="beyondwords-bulk-edit-notice-error" class="notice notice-error is-dismissible">
186                <p>
187                    <?php echo esc_html($message); ?>
188                </p>
189            </div>
190            <?php
191        }
192    }
193}