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

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

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