| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Metabox |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Handles all things with the metabox in combination with the WordPress editor. |
| 10 |
*/ |
| 11 |
class WPSEO_Metabox_Editor { |
| 12 |
|
| 13 |
/** |
| 14 |
* Registers hooks to WordPress. |
| 15 |
* |
| 16 |
* @codeCoverageIgnore |
| 17 |
* |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public function register_hooks() { |
| 21 |
// For the Classic editor. |
| 22 |
add_filter( 'mce_css', [ $this, 'add_css_inside_editor' ] ); |
| 23 |
// For the Block/Gutenberg editor. |
| 24 |
// See https://github.com/danielbachhuber/gutenberg-migration-guide/blob/master/filter-mce-css.md. |
| 25 |
add_action( 'enqueue_block_editor_assets', [ $this, 'add_editor_styles' ] ); |
| 26 |
|
| 27 |
add_filter( 'tiny_mce_before_init', [ $this, 'add_custom_element' ] ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Adds our inside the editor CSS file to the list of CSS files to be loaded inside the editor. |
| 32 |
* |
| 33 |
* @param string $css_files The CSS files that WordPress wants to load inside the editor. |
| 34 |
* @return string The CSS files WordPress wants to load and our CSS file. |
| 35 |
*/ |
| 36 |
public function add_css_inside_editor( $css_files ) { |
| 37 |
$asset_manager = new WPSEO_Admin_Asset_Manager(); |
| 38 |
$styles = $asset_manager->special_styles(); |
| 39 |
$inside_editor = $styles['inside-editor']; |
| 40 |
|
| 41 |
$asset_location = new WPSEO_Admin_Asset_SEO_Location( WPSEO_FILE ); |
| 42 |
$url = $asset_location->get_url( $inside_editor, WPSEO_Admin_Asset::TYPE_CSS ); |
| 43 |
|
| 44 |
if ( $css_files === '' ) { |
| 45 |
$css_files = $url; |
| 46 |
} |
| 47 |
else { |
| 48 |
$css_files .= ',' . $url; |
| 49 |
} |
| 50 |
|
| 51 |
return $css_files; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Enqueues the CSS to use in the TinyMCE editor. |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function add_editor_styles() { |
| 60 |
$asset_manager = new WPSEO_Admin_Asset_Manager(); |
| 61 |
$asset_manager->enqueue_style( 'inside-editor' ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Adds a custom element to the tinyMCE editor that we need for marking the content. |
| 66 |
* |
| 67 |
* @param array $tinymce_config The tinyMCE config as configured by WordPress. |
| 68 |
* |
| 69 |
* @return array The new tinyMCE config with our added custom elements. |
| 70 |
*/ |
| 71 |
public function add_custom_element( $tinymce_config ) { |
| 72 |
if ( ! empty( $tinymce_config['custom_elements'] ) ) { |
| 73 |
$custom_elements = $tinymce_config['custom_elements']; |
| 74 |
|
| 75 |
$custom_elements .= ',~yoastmark'; |
| 76 |
} |
| 77 |
else { |
| 78 |
$custom_elements = '~yoastmark'; |
| 79 |
} |
| 80 |
|
| 81 |
$tinymce_config['custom_elements'] = $custom_elements; |
| 82 |
|
| 83 |
return $tinymce_config; |
| 84 |
} |
| 85 |
} |
| 86 |
|