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

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

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