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 / Metabox / Metabox.php

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

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