| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Integration\Classic_Editor; |
| 4 |
|
| 5 |
use function Code_Snippets\code_snippets; |
| 6 |
use const Code_Snippets\PLUGIN_FILE; |
| 7 |
|
| 8 |
/** |
| 9 |
* This class registers a custom TinyMCE plugin and button. |
| 10 |
* |
| 11 |
* @package Code_Snippets |
| 12 |
*/ |
| 13 |
class MCE_Plugin { |
| 14 |
|
| 15 |
/** |
| 16 |
* Constructor. |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
add_action( 'init', [ $this, 'setup_mce_plugin' ] ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Perform the necessary actions to add a button to the TinyMCE editor |
| 24 |
*/ |
| 25 |
public function setup_mce_plugin() { |
| 26 |
if ( ! code_snippets()->current_user_can() ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
/* Register the TinyMCE plugin */ |
| 31 |
add_filter( |
| 32 |
'mce_external_plugins', |
| 33 |
function ( $plugins ) { |
| 34 |
$plugins['code_snippets'] = plugins_url( 'dist/mce.js', PLUGIN_FILE ); |
| 35 |
return $plugins; |
| 36 |
} |
| 37 |
); |
| 38 |
|
| 39 |
/* Add the button to the editor toolbar */ |
| 40 |
add_filter( |
| 41 |
'mce_buttons', |
| 42 |
function ( $buttons ) { |
| 43 |
$buttons[] = 'code_snippets'; |
| 44 |
return $buttons; |
| 45 |
} |
| 46 |
); |
| 47 |
|
| 48 |
/* Add the translation strings to the TinyMCE editor */ |
| 49 |
add_filter( |
| 50 |
'mce_external_languages', |
| 51 |
function ( $languages ) { |
| 52 |
$languages['code_snippets'] = __DIR__ . '/mce-strings.php'; |
| 53 |
return $languages; |
| 54 |
} |
| 55 |
); |
| 56 |
} |
| 57 |
} |
| 58 |
|