class-metabox-editor.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. public function register_hooks() {
  15. add_filter( 'mce_css', array( $this, 'add_css_inside_editor' ) );
  16. add_filter( 'tiny_mce_before_init', array( $this, 'add_custom_element' ) );
  17. }
  18. /**
  19. * Adds our inside the editor CSS file to the list of CSS files to be loaded inside the editor.
  20. *
  21. * @param string $css_files The CSS files that WordPress wants to load inside the editor.
  22. * @return string The CSS files WordPress wants to load and our CSS file.
  23. */
  24. public function add_css_inside_editor( $css_files ) {
  25. $asset_manager = new WPSEO_Admin_Asset_Manager();
  26. $styles = $asset_manager->special_styles();
  27. /** @var WPSEO_Admin_Asset $inside_editor */
  28. $inside_editor = $styles['inside-editor'];
  29. $asset_location = new WPSEO_Admin_Asset_SEO_Location( WPSEO_FILE );
  30. $url = $asset_location->get_url( $inside_editor, WPSEO_Admin_Asset::TYPE_CSS );
  31. if ( '' === $css_files ) {
  32. $css_files = $url;
  33. }
  34. else {
  35. $css_files .= ',' . $url;
  36. }
  37. return $css_files;
  38. }
  39. /**
  40. * Adds a custom element to the tinyMCE editor that we need for marking the content.
  41. *
  42. * @param array $tinymce_config The tinyMCE config as configured by WordPress.
  43. *
  44. * @return array The new tinyMCE config with our added custom elements.
  45. */
  46. public function add_custom_element( $tinymce_config ) {
  47. if ( ! empty( $tinymce_config['custom_elements'] ) ) {
  48. $custom_elements = $tinymce_config['custom_elements'];
  49. $custom_elements .= ',~yoastmark';
  50. }
  51. else {
  52. $custom_elements = '~yoastmark';
  53. }
  54. $tinymce_config['custom_elements'] = $custom_elements;
  55. return $tinymce_config;
  56. }
  57. }