PluginProbe
BeyondWords – AI audio for publishers / 4.4.0
BeyondWords – AI audio for publishers v4.4.0
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 / Core / Player / LegacyPlayer.php

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

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