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