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

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

130 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 setup
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 & Elementor.
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, true); ?>
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_text_field($_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 }
130