PluginProbe
BeyondWords – AI audio for publishers / 6.0.3
BeyondWords – AI audio for publishers v6.0.3
7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.7.0 All 43 releases
speechkit / src / Component / Post / GenerateAudio / GenerateAudio.php

GenerateAudio.php in BeyondWords – AI audio for publishers 6.0.3, at src/Component/Post/GenerateAudio/GenerateAudio.php

135 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @since 6.0.0 Make static.
30 */
31 public static function init()
32 {
33 add_action('wp_loaded', function (): void {
34 $postTypes = SettingsUtils::getCompatiblePostTypes();
35
36 if (is_array($postTypes)) {
37 foreach ($postTypes as $postType) {
38 add_action("save_post_{$postType}", [self::class, 'save'], 10);
39 }
40 }
41 });
42 }
43
44 /**
45 * Check whether the post type should preselect the "Generate audio" checkbox.
46 *
47 * @param \WP_Post|int $post The post object or ID.
48 *
49 * @todo move this function to somewhere reusable for the Block editor.
50 *
51 * @since 6.0.0 Make static.
52 */
53 public static function shouldPreselectGenerateAudio($post)
54 {
55 $postType = get_post_type($post);
56
57 if (! $postType) {
58 return false;
59 }
60
61 $preselect = get_option('beyondwords_preselect');
62
63 if (! is_array($preselect)) {
64 return false;
65 }
66
67 // Preselect if the post type in the settings has been checked (not the taxonomies)
68 if (array_key_exists($postType, $preselect) && $preselect[$postType] === '1') {
69 return true;
70 }
71
72 return false;
73 }
74
75 /**
76 * Render the element.
77 *
78 * @since 6.0.0 Make static and refactor generate audio check.
79 */
80 public static function element($post)
81 {
82 wp_nonce_field('beyondwords_generate_audio', 'beyondwords_generate_audio_nonce');
83
84 $generateAudio = PostMetaUtils::hasGenerateAudio($post->ID);
85 ?>
86 <!-- checkbox -->
87 <p id="beyondwords-metabox-generate-audio">
88 <input
89 type="checkbox"
90 id="beyondwords_generate_audio"
91 name="beyondwords_generate_audio"
92 value="1"
93 <?php checked($generateAudio); ?>
94 />
95 <?php esc_html_e('Generate audio', 'speechkit'); ?>
96 </p>
97 <?php
98 }
99
100 /**
101 * Save the meta when the post is saved.
102 *
103 * @since 6.0.0 Make static.
104 *
105 * @param int $postId The ID of the post being saved.
106 */
107 public static function save($postId)
108 {
109 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
110 return $postId;
111 }
112
113 // "save_post" can be triggered at other times, so verify this request came from the our component
114 if (
115 ! isset($_POST['beyondwords_generate_audio_nonce']) ||
116 ! wp_verify_nonce(
117 sanitize_key($_POST['beyondwords_generate_audio_nonce']),
118 'beyondwords_generate_audio'
119 )
120 ) {
121 return $postId;
122 }
123
124 if (isset($_POST['beyondwords_generate_audio'])) {
125 update_post_meta($postId, 'beyondwords_generate_audio', '1');
126 } else {
127 delete_post_meta($postId, 'speechkit_error_message');
128 delete_post_meta($postId, 'beyondwords_error_message');
129 update_post_meta($postId, 'beyondwords_generate_audio', '0');
130 }
131
132 return $postId;
133 }
134 }
135