PluginProbe
BeyondWords – AI audio for publishers / 4.0.1
BeyondWords – AI audio for publishers v4.0.1
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 / Metabox / Metabox.php

Metabox.php in BeyondWords – AI audio for publishers 4.0.1, at src/Component/Post/Metabox/Metabox.php

249 lines 6.7 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 Post Metabox.
7 *
8 * @package Beyondwords\Wordpress
9 * @author Stuart McAlpine <[email protected]>
10 * @since 3.0.0
11 */
12
13 namespace Beyondwords\Wordpress\Component\Post\Metabox;
14
15 use Beyondwords\Wordpress\Component\GenerateAudio\GenerateAudio;
16 use Beyondwords\Wordpress\Component\DisplayPlayer\DisplayPlayer;
17 use Beyondwords\Wordpress\Component\Post\PostMetaUtils;
18 use Beyondwords\Wordpress\Component\Settings\SettingsUtils;
19 use Beyondwords\Wordpress\Core\Environment;
20
21 /**
22 * PostMetabox setup
23 *
24 * @since 3.0.0
25 */
26 class Metabox
27 {
28 public $generateAudio;
29
30 public $displayPlayer;
31
32 public $selectVoice;
33
34 /**
35 * Constructor
36 */
37 public function __construct($generateAudio, $displayPlayer, $selectVoice)
38 {
39 $this->generateAudio = $generateAudio;
40 $this->displayPlayer = $displayPlayer;
41 $this->selectVoice = $selectVoice;
42
43 add_action('admin_enqueue_scripts', array($this, 'adminEnqueueScripts'));
44 add_action("add_meta_boxes", array($this, 'addMetaBox'));
45 }
46
47 /**
48 * Enque JS for Bulk Edit feature.
49 */
50 public function adminEnqueueScripts($hook)
51 {
52 // Only enqueue for Post screens
53 if ($hook === 'post.php' || $hook === 'post-new.php') {
54 // Register the Classic Editor "Metabox" CSS
55 wp_enqueue_style(
56 'beyondwords-Metabox',
57 BEYONDWORDS__PLUGIN_URI . 'src/Component/Post/Metabox/Metabox.css',
58 false,
59 BEYONDWORDS__PLUGIN_VERSION
60 );
61 }
62 }
63
64 /**
65 * Adds the meta box container.
66 *
67 * @param $postType
68 */
69 public function addMetaBox($postType)
70 {
71 $postTypes = SettingsUtils::getSupportedPostTypes();
72
73 if (is_array($postTypes) && ! in_array($postType, $postTypes)) {
74 return;
75 }
76
77 add_meta_box(
78 'beyondwords',
79 __('BeyondWords', 'speechkit'),
80 array($this, 'renderMetaBoxContent'),
81 $postType,
82 'side',
83 'default',
84 [
85 '__back_compat_meta_box' => true,
86 ]
87 );
88 }
89
90 /**
91 * Render Meta Box content.
92 *
93 * @param WP_Post $post The post object.
94 *
95 * @since 3.0.0
96 * @since 3.7.0 Show "Pending review" notice for posts with status of "pending".
97 * @since 4.0.0 Content ID is no longer an int
98 */
99 public function renderMetaBoxContent($post)
100 {
101 if (!($post instanceof \WP_Post)) {
102 return;
103 }
104
105 $contentId = PostMetaUtils::getContentId($post->ID);
106
107 if ($contentId) {
108 if (get_post_status($post) === 'pending') {
109 $this->pendingReviewNotice($post);
110 } else {
111 $this->playerEmbed($post);
112 }
113 echo '<hr />';
114 $this->displayPlayer->element($post);
115 } else {
116 $this->errors($post);
117 $this->generateAudio->element($post);
118 $this->selectVoice->element($post);
119 }
120
121 echo '<hr />';
122 $this->help();
123 }
124
125
126 /**
127 * The "Pending review" message, shown instead of the audio player
128 * if the post status in WordPress is "pending".
129 *
130 * This message is displayed instead of the player because the player
131 * cannot be rendered for audio which has been created
132 * with { published: false }.
133 *
134 * @since 3.7.0
135 *
136 * @var \WP_Post $post Post.
137 */
138 public function pendingReviewNotice($post)
139 {
140 $projectUrl = sprintf(
141 '%s/dashboard/project/%d/content',
142 Environment::getDashboardUrl(),
143 PostMetaUtils::getProjectId($post)
144 );
145
146 ?>
147 <div id="beyondwords-pending-review-message">
148 <?php
149 printf(
150 /* translators: %s is replaced with the link to the BeyondWords dashboard */
151 esc_html__('Listen to content saved as “Pending” in the %s.', 'speechkit'),
152 sprintf(
153 '<a href="%s" target="_blank" rel="nofollow">%s</a>',
154 esc_url($projectUrl),
155 esc_html__('BeyondWords dashboard', 'speechkit')
156 )
157 );
158 ?>
159 </div>
160 <?php
161 }
162
163 /**
164 * Embed a player for a WordPress post.
165 *
166 * @param int|WP_Post (Optional) Post ID or WP_Post object. Default is global $post.
167 *
168 * @since 3.x Introduced
169 * @since 4.0.1 Admin player init is now all in this one function.
170 */
171 public function playerEmbed($post = null)
172 {
173 $post = get_post($post);
174
175 if (!($post instanceof \WP_Post)) {
176 return;
177 }
178
179 $projectId = PostMetaUtils::getProjectId($post->ID);
180 $contentId = PostMetaUtils::getContentId($post->ID);
181
182 if (! $projectId || ! $contentId) {
183 return;
184 }
185
186 $writeToken = get_option('beyondwords_api_key', '');
187 ?>
188 <script async defer
189 src='<?php echo esc_url(Environment::getJsSdkUrl()); ?>'
190 onload='const player = new BeyondWords.Player({
191 target: this,
192 projectId: <?php echo esc_attr($projectId); ?>,
193 contentId: "<?php echo esc_attr($contentId); ?>",
194 adverts: [],
195 introsOutros: [],
196 playerStyle: "small",
197 writeToken: "<?php echo esc_attr($writeToken); ?>",
198 });'
199 >
200 </script>
201 <?php
202 }
203
204 public function errors($post)
205 {
206 $error = PostMetaUtils::getErrorMessage($post->ID);
207
208 if ($error) :
209 ?>
210 <div id="beyondwords-metabox-errors">
211 <div class="beyondwords-error">
212 <p>
213 <?php echo esc_html($error); ?>
214 </p>
215 </div>
216 <?php $this->regenerateInstructions(); ?>
217 </div>
218 <?php
219 endif;
220 }
221
222 public function help()
223 {
224 ?>
225 <p id="beyondwords-metabox-help">
226 <?php
227 printf(
228 /* translators: %s is replaced with the link to the support email address */
229 esc_html__('Need help? Email our support team on %s', 'speechkit'),
230 sprintf('<a href="%s">%s</a>', 'mailto:[email protected]', '[email protected]')
231 );
232 ?>
233 </p>
234 <?php
235 }
236
237 public function regenerateInstructions()
238 {
239 ?>
240 <!-- Update/regenerate -->
241 <p>
242 <?php
243 _e('To create audio, resolve the error above then select ‘Update’ with ‘Generate audio’ checked.', 'speechkit'); // phpcs:ignore
244 ?>
245 </p>
246 <?php
247 }
248 }
249