PluginProbe
BeyondWords – AI audio for publishers / 4.2.4
BeyondWords – AI audio for publishers v4.2.4
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 4.2.4, at src/Component/Post/GenerateAudio/GenerateAudio.php

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