PluginProbe
BeyondWords – AI audio for publishers / 4.5.0
BeyondWords – AI audio for publishers v4.5.0
7.2.0 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 All 44 releases
speechkit / src / Component / Post / GenerateAudio / GenerateAudio.php

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

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