PluginProbe
BeyondWords – AI audio for publishers / 4.5.0
BeyondWords – AI audio for publishers v4.5.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 / Component / Post / AddPlayer / AddPlayer.php

AddPlayer.php in BeyondWords – AI audio for publishers 4.5.0, at src/Component/Post/AddPlayer/AddPlayer.php

168 lines 4.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 "Add Player" component.
7 *
8 * @package Beyondwords\Wordpress
9 * @author Stuart McAlpine <[email protected]>
10 * @since 3.2.0
11 */
12
13 namespace Beyondwords\Wordpress\Component\Post\AddPlayer;
14
15 use Beyondwords\Wordpress\Core\CoreUtils;
16
17 /**
18 * AddPlayer setup
19 *
20 * @since 3.2.0
21 */
22 class AddPlayer
23 {
24 // The CSS declaration block for the player preview in both Classic Editor and Block Editor.
25 public const PLAYER_PREVIEW_STYLE_FORMAT = "iframe [data-beyondwords-player]:empty:after, .edit-post-visual-editor [data-beyondwords-player]:empty:after { content: '%s'; }"; // phpcs:ignore Generic.Files.LineLength.TooLong
26
27 // The player preview placeholder text.
28 public const PLAYER_PREVIEW_TEXT = 'Player placeholder: The position of the audio player.';
29
30 /**
31 * Init.
32 *
33 * @since 4.0.0
34 */
35 public function init()
36 {
37 add_action('init', array($this, 'registerBlock'));
38 add_action('enqueue_block_editor_assets', array($this, 'addBlockEditorStylesheet'));
39
40 add_action('admin_head', array($this, 'addEditorStyles'));
41 add_filter('tiny_mce_before_init', array($this, 'filterTinyMceSettings'));
42
43 add_filter('mce_external_plugins', array($this, 'addPlugin'));
44 add_filter('mce_buttons', array($this, 'addButton'));
45 add_filter('mce_css', array($this, 'addStylesheet'));
46 }
47
48 /**
49 * Register Block.
50 */
51 public function registerBlock()
52 {
53 \register_block_type(__DIR__);
54 }
55
56 /**
57 * Add TinyMCE buttons.
58 *
59 * @param array TinyMCE plugin array
60 */
61 public function addPlugin($plugin_array)
62 {
63 $plugin_array['beyondwords_player'] = BEYONDWORDS__PLUGIN_URI . 'src/Component/Post/AddPlayer/tinymce.js';
64 return $plugin_array;
65 }
66
67 /**
68 * Register TinyMCE buttons.
69 *
70 * @param array TinyMCE buttons array
71 */
72 public function addButton($buttons)
73 {
74 $advIndex = array_search('wp_adv', $buttons);
75
76 if ($advIndex === false) {
77 $advIndex = count($buttons);
78 }
79
80 array_splice($buttons, $advIndex, 0, ['beyondwords_player']);
81
82 return $buttons;
83 }
84
85 /**
86 * Filters the comma-delimited list of stylesheets to load in TinyMCE.
87 */
88 public function addStylesheet($stylesheets)
89 {
90 return $stylesheets . ',' . BEYONDWORDS__PLUGIN_URI . 'src/Component/Post/AddPlayer/AddPlayer.css';
91 }
92
93 /**
94 * "Player Preview" i18n styles.
95 *
96 * Player preview uses the CSS :after to set the content so we pass the CSS through WordPress i18n functions here.
97 *
98 * @since 3.3.0
99 *
100 * @return string CSS Block for player preview i18n delcerations.
101 */
102 public function playerPreviewI18nStyles()
103 {
104 return sprintf(
105 self::PLAYER_PREVIEW_STYLE_FORMAT,
106 esc_attr__(self::PLAYER_PREVIEW_TEXT, 'speechkit')
107 );
108 }
109
110 /**
111 * Tiny MCE before init.
112 *
113 * Adds i18n-compatible TinyMCE Classic Editor CSS for the player placeholder.
114 *
115 * @since 3.3.0
116 *
117 * @param mixed[] $setings An array with TinyMCE config.
118 *
119 * @return mixed[] An array with TinyMCE config.
120 */
121 public function filterTinyMceSettings($settings)
122 {
123 if (isset($settings['content_style'])) {
124 $settings['content_style'] .= ' ' . $this->playerPreviewI18nStyles() . ' ';
125 } else {
126 $settings['content_style'] = $this->playerPreviewI18nStyles() . ' ';
127 }
128
129 return $settings;
130 }
131
132 /**
133 * Add editor styles.
134 *
135 * Adds i18n-compatible Block Editor CSS for the player placeholder.
136 *
137 * @since 3.3.0
138 */
139 public function addEditorStyles()
140 {
141 $allowed_html = array(
142 'style' => array(),
143 );
144
145 echo wp_kses(
146 sprintf('<style>%s</style>', $this->playerPreviewI18nStyles()),
147 $allowed_html
148 );
149 }
150
151 /**
152 * Add Block Editor Stylesheet.
153 */
154 public function addBlockEditorStylesheet($hook)
155 {
156 // Only enqueue for Gutenberg/Post screens
157 if (CoreUtils::isGutenbergPage() || $hook === 'post.php' || $hook === 'post-new.php') {
158 // Register the Classic/Block Editor "Add Player" CSS
159 wp_enqueue_style(
160 'beyondwords-AddPlayer',
161 BEYONDWORDS__PLUGIN_URI . 'src/Component/Post/AddPlayer/AddPlayer.css',
162 array(),
163 BEYONDWORDS__PLUGIN_VERSION
164 );
165 }
166 }
167 }
168