Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
57.45% |
27 / 47 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
GenerateAudio | |
57.45% |
27 / 47 |
|
25.00% |
1 / 4 |
42.97 | |
0.00% |
0 / 1 |
init | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
shouldPreselectGenerateAudio | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
5.03 | |||
element | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
save | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
6.01 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | /** |
6 | * BeyondWords Component: Generate audio |
7 | * |
8 | * @package Beyondwords\Wordpress |
9 | * @author Stuart McAlpine <stu@beyondwords.io> |
10 | * @since 3.0.0 |
11 | */ |
12 | |
13 | namespace Beyondwords\Wordpress\Component\Post\GenerateAudio; |
14 | |
15 | use Beyondwords\Wordpress\Component\Post\PostMetaUtils; |
16 | use Beyondwords\Wordpress\Component\Settings\SettingsUtils; |
17 | |
18 | /** |
19 | * GenerateAudio |
20 | * |
21 | * @since 3.0.0 |
22 | */ |
23 | class GenerateAudio |
24 | { |
25 | /** |
26 | * Init. |
27 | * |
28 | * @since 4.0.0 |
29 | */ |
30 | public function init() |
31 | { |
32 | add_action('wp_loaded', function () { |
33 | $postTypes = SettingsUtils::getCompatiblePostTypes(); |
34 | |
35 | if (is_array($postTypes)) { |
36 | foreach ($postTypes as $postType) { |
37 | add_action("save_post_{$postType}", array($this, 'save'), 10); |
38 | } |
39 | } |
40 | }); |
41 | } |
42 | |
43 | /** |
44 | * todo move this function to somewhere reusable for the Block editor. |
45 | */ |
46 | public function shouldPreselectGenerateAudio($post) |
47 | { |
48 | $postType = get_post_type($post); |
49 | |
50 | if (! $postType) { |
51 | return false; |
52 | } |
53 | |
54 | $preselect = get_option('beyondwords_preselect'); |
55 | |
56 | if (! is_array($preselect)) { |
57 | return false; |
58 | } |
59 | |
60 | // Preselect if the post type in the settings has been checked (not the taxonomies) |
61 | if (array_key_exists($postType, $preselect) && $preselect[$postType] === '1') { |
62 | return true; |
63 | } |
64 | |
65 | return false; |
66 | } |
67 | |
68 | public function element($post) |
69 | { |
70 | wp_nonce_field('beyondwords_generate_audio', 'beyondwords_generate_audio_nonce'); |
71 | |
72 | $generateAudio = PostMetaUtils::hasGenerateAudio($post->ID); |
73 | |
74 | if (! $generateAudio) { |
75 | // Check whether "0" has explicitly been saved |
76 | $generateAudioMeta = PostMetaUtils::getRenamedPostMeta($post->ID, 'generate_audio', true); |
77 | |
78 | if ($generateAudioMeta !== '0' && $this->shouldPreselectGenerateAudio($post)) { |
79 | $generateAudio = true; |
80 | } |
81 | } |
82 | ?> |
83 | <!-- checkbox --> |
84 | <p id="beyondwords-metabox-generate-audio"> |
85 | <input |
86 | type="checkbox" |
87 | id="beyondwords_generate_audio" |
88 | name="beyondwords_generate_audio" |
89 | value="1" |
90 | <?php checked($generateAudio); ?> |
91 | /> |
92 | <?php esc_html_e('Generate audio', 'speechkit'); ?> |
93 | </p> |
94 | <?php |
95 | } |
96 | |
97 | /** |
98 | * Save the meta when the post is saved. |
99 | * |
100 | * @param int $postId The ID of the post being saved. |
101 | */ |
102 | public function save($postId) |
103 | { |
104 | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
105 | return $postId; |
106 | } |
107 | |
108 | // "save_post" can be triggered at other times, so verify this request came from the our component |
109 | if ( |
110 | ! isset($_POST['beyondwords_generate_audio_nonce']) || |
111 | ! wp_verify_nonce( |
112 | sanitize_key($_POST['beyondwords_generate_audio_nonce']), |
113 | 'beyondwords_generate_audio' |
114 | ) |
115 | ) { |
116 | return $postId; |
117 | } |
118 | |
119 | if (isset($_POST['beyondwords_generate_audio'])) { |
120 | update_post_meta($postId, 'beyondwords_generate_audio', '1'); |
121 | } else { |
122 | delete_post_meta($postId, 'speechkit_error_message'); |
123 | delete_post_meta($postId, 'beyondwords_error_message'); |
124 | update_post_meta($postId, 'beyondwords_generate_audio', '0'); |
125 | } |
126 | |
127 | return $postId; |
128 | } |
129 | } |