class-metabox-editor.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Metabox
  6. */
  7. /**
  8. * Handles all things with the metabox in combination with the WordPress editor.
  9. */
  10. class WPSEO_Metabox_Editor {
  11. /**
  12. * Registers hooks to WordPress.
  13. *
  14. * @codeCoverageIgnore
  15. */
  16. public function register_hooks() {
  17. add_filter( 'mce_css', [ $this, 'add_css_inside_editor' ] );
  18. add_filter( 'tiny_mce_before_init', [ $this, 'add_custom_element' ] );
  19. }
  20. /**
  21. * Adds our inside the editor CSS file to the list of CSS files to be loaded inside the editor.
  22. *
  23. * @param string $css_files The CSS files that WordPress wants to load inside the editor.
  24. * @return string The CSS files WordPress wants to load and our CSS file.
  25. */
  26. public function add_css_inside_editor( $css_files ) {
  27. $asset_manager = new WPSEO_Admin_Asset_Manager();
  28. $styles = $asset_manager->special_styles();
  29. $inside_editor = $styles['inside-editor'];
  30. $asset_location = new WPSEO_Admin_Asset_SEO_Location( WPSEO_FILE );
  31. $url = $asset_location->get_url( $inside_editor, WPSEO_Admin_Asset::TYPE_CSS );
  32. if ( '' === $css_files ) {
  33. $css_files = $url;
  34. }
  35. else {
  36. $css_files .= ',' . $url;
  37. }
  38. return $css_files;
  39. }
  40. /**
  41. * Adds a custom element to the tinyMCE editor that we need for marking the content.
  42. *
  43. * @param array $tinymce_config The tinyMCE config as configured by WordPress.
  44. *
  45. * @return array The new tinyMCE config with our added custom elements.
  46. */
  47. public function add_custom_element( $tinymce_config ) {
  48. if ( ! empty( $tinymce_config['custom_elements'] ) ) {
  49. $custom_elements = $tinymce_config['custom_elements'];
  50. $custom_elements .= ',~yoastmark';
  51. }
  52. else {
  53. $custom_elements = '~yoastmark';
  54. }
  55. $tinymce_config['custom_elements'] = $custom_elements;
  56. return $tinymce_config;
  57. }
  58. }