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 / Core / Player / LegacyPlayer.php

LegacyPlayer.php in BeyondWords – AI audio for publishers 4.2.4, at src/Core/Player/LegacyPlayer.php

234 lines 6.8 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 namespace Beyondwords\Wordpress\Core\Player;
6
7 use Beyondwords\Wordpress\Component\Post\PostMetaUtils;
8 use Beyondwords\Wordpress\Component\Settings\PlayerUI\PlayerUI;
9 use Beyondwords\Wordpress\Core\Environment;
10 use Beyondwords\Wordpress\Core\CoreUtils;
11 use Beyondwords\Wordpress\Core\Player\Player;
12 use Symfony\Component\DomCrawler\Crawler;
13
14 /**
15 * The "Legacy" BeyondWords Player.
16 *
17 * @deprecated Scheduled for removal in v5.0
18 **/
19 class LegacyPlayer extends Player
20 {
21 /**
22 * Constructor
23 */
24 public function __construct()
25 {
26 // Actions
27 add_action('init', array($this, 'registerShortcodes'));
28 add_action('wp_enqueue_scripts', array($this, 'enqueueScripts'));
29
30 // Filters
31 add_filter('the_content', array($this, 'autoPrependPlayer'), 1000000);
32 add_filter('newsstand_the_content', array($this, 'autoPrependPlayer'));
33 }
34
35 /**
36 * Should we show the BeyondWords audio player?
37 *
38 * We DO NOT want to show the player if:
39 * 1. BeyondWords has been disabled in our plugin settings.
40 * 2. The current post type has not been selected in our plugin settings.
41 * 3. The current post has specifically been disabled from processing.
42 *
43 * The return value of this can be overriden with the WordPress
44 * "beyondwords_post_player_enabled" filter.
45 *
46 * @param int|WP_Post (Optional) Post ID or WP_Post object. Default is global $post.
47 *
48 * @since 3.0.0
49 * @since 3.3.4 Accept int|WP_Post as method parameter.
50 * @since 4.0.0 Check beyondwords_player_ui custom field.
51 *
52 * @return bool
53 **/
54 public function isPlayerEnabled($post = null)
55 {
56 $post = get_post($post);
57
58 if (! ($post instanceof \WP_Post)) {
59 return false;
60 }
61
62 // Assume we can show the player
63 $enabled = true;
64
65 // Has 'Display Player' been unchecked?
66 if (PostMetaUtils::getDisabled($post->ID)) {
67 $enabled = false;
68 }
69
70 /**
71 * Filters the enabled/disabled (shown/hidden) status of the player for each post.
72 *
73 * @since 3.3.3
74 *
75 * @param boolean $enabled Is the player enabled (shown) for this post?
76 * @param int $post_id WordPress post ID.
77 */
78 $enabled = apply_filters('beyondwords_post_player_enabled', $enabled, $post->ID);
79
80 return $enabled;
81 }
82
83 /**
84 * Register the JavaScript for the public-facing side of the site.
85 *
86 * @since 3.0.0
87 *
88 * @return void
89 */
90 public function enqueueScripts()
91 {
92 if (! is_singular() && ! is_admin()) {
93 return;
94 }
95
96 // JS SDK Player script, filtered by $this->scriptLoaderTag()
97 add_filter('script_loader_tag', array($this, 'scriptLoaderTag'), 10, 3);
98
99 wp_enqueue_script(
100 'beyondwords-sdk',
101 Environment::getJsSdkUrlLegacy(),
102 array(),
103 null,
104 true
105 );
106 }
107
108 /**
109 * Filters the HTML script tag of an enqueued script.
110 *
111 * @param string $tag The <script> tag for the enqueued script.
112 * @param string $handle The script's registered handle.
113 * @param string $src The script's source URL.
114 *
115 * @since 3.0.0
116 *
117 * @see https://developer.wordpress.org/reference/hooks/script_loader_tag/
118 * @see https://stackoverflow.com/a/59594789
119 *
120 * @return string
121 */
122 public function scriptLoaderTag($tag, $handle, $src)
123 {
124 if ($handle === 'beyondwords-sdk') :
125 if (! $this->usePlayerJsSdk()) {
126 return '';
127 }
128
129 $post = get_post();
130
131 $params = $this->jsPlayerParams($post);
132
133 ob_start();
134 ?>
135 <script id="beyondwords-sdk" type="module">
136 //<![CDATA[
137 import BeyondWords from '<?php echo esc_url($src); ?>';
138 import { v4 as uuidv4 } from 'https://jspm.dev/uuid';
139
140 const PLAYER_SELECTOR = 'div[data-beyondwords-player]:not([data-beyondwords-init])';
141
142 const players = Array.from(
143 document.querySelectorAll(PLAYER_SELECTOR)
144 );
145
146 await Promise.all(players.map((item, index) => {
147 const playerId = `beyondwords-player-${uuidv4()}`;
148
149 item.setAttribute('id', playerId);
150
151 return BeyondWords.player({
152 ...<?php echo wp_json_encode($params, JSON_FORCE_OBJECT); ?>,
153 "renderNode": playerId,
154 }).then((player) => {
155 item.setAttribute('data-beyondwords-init', 'true');
156 console.log(`🔊 #${playerId} is initialized`);
157 }).catch(err => {
158 console.error(err);
159 });
160 }));
161 //]]>
162 </script>
163 <?php
164 return ob_get_clean();
165 endif;
166
167 return $tag;
168 }
169
170 /**
171 * JavaScript SDK parameters.
172 *
173 * Note that the default return value for this method is an associative array, but
174 * the HTML output will be forced to an object due to `wp_json_encode($params, JSON_FORCE_OBJECT)`
175 * in `Player::scriptLoaderTag()`.
176 *
177 * @since 3.1.0
178 *
179 * @see https://docs.beyondwords.io/docs/javascript-sdk-automatic-player
180 *
181 * @param WP_Post $post WordPress Post.
182 *
183 * @return array
184 */
185 public function jsPlayerParams($post)
186 {
187 if (!($post instanceof \WP_Post)) {
188 return [];
189 }
190
191 $projectId = PostMetaUtils::getProjectId($post->ID);
192 $podcastId = PostMetaUtils::getContentId($post->ID);
193
194 $skBackend = Environment::getBackendUrl();
195 $skBackendApi = Environment::getApiUrl();
196
197 $params = [
198 'projectId' => (int)$projectId,
199 'podcastId' => $podcastId,
200 ];
201
202 if (get_option('beyondwords_player_size') === 'medium') {
203 $params['playerType'] = 'manual';
204 }
205
206 if (strlen($skBackend)) {
207 $params['skBackend'] = esc_url($skBackend);
208 }
209
210 if (is_admin()) {
211 $params['processingStatus'] = true;
212 $params['apiWriteKey'] = get_option('beyondwords_api_key', '');
213
214 if (strlen($skBackendApi)) {
215 $params['skBackendApi'] = esc_url($skBackendApi);
216 }
217 }
218
219 if (defined('BEYONDWORDS_DEBUG') && BEYONDWORDS_DEBUG) {
220 $params['debug'] = true;
221 }
222
223 /**
224 * Filters the BeyondWords JavaScript SDK parameters.
225 *
226 * @since 3.3.3
227 *
228 * @param array The default JS SDK params.
229 */
230 $params = apply_filters('beyondwords_js_player_params', $params);
231
232 return $params;
233 }
234 }