class-yoast-modal.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @package WPSEO\Admin
  4. */
  5. /**
  6. * Class to implement a React modal.
  7. */
  8. class Yoast_Modal {
  9. /** @var array The modal configuration. */
  10. private static $config = array();
  11. /**
  12. * Class constructor.
  13. */
  14. public function __construct() {
  15. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
  16. add_action( 'admin_footer', array( $this, 'print_localized_config' ) );
  17. }
  18. /**
  19. * Enqueues the assets needed for the modal.
  20. */
  21. public function enqueue_assets() {
  22. $asset_manager = new WPSEO_Admin_Asset_Manager();
  23. $asset_manager->enqueue_script( 'yoast-modal' );
  24. }
  25. /**
  26. * Prints the modals configuration.
  27. */
  28. public function print_localized_config() {
  29. $config = self::$config;
  30. wp_localize_script( WPSEO_Admin_Asset_Manager::PREFIX . 'yoast-modal', 'yoastModalConfig', $config );
  31. }
  32. /**
  33. * Adds a single modal configuration to the modals configuration.
  34. *
  35. * @param array $args The modal configuration arguments.
  36. */
  37. public static function add( $args ) {
  38. $defaults = self::get_defaults();
  39. $single = array_replace_recursive( $defaults, $args );
  40. self::$config[] = $single;
  41. }
  42. /**
  43. * Gets the modals configuration.
  44. */
  45. public function get_config() {
  46. return self::$config;
  47. }
  48. /**
  49. * Gets the default configuration for a modal.
  50. *
  51. * @return array {
  52. * The modal default configuration.
  53. *
  54. * @type string $mountHook Any CSS query selector to target an element that will be replaced
  55. * by the modal open button.
  56. * @type string $appElement Element the modal will hide with `aria-hidden`. For better
  57. * accessibility, set it to the most general wrapper and don't use body.
  58. * @type string $openButtonIcon Optional. Icon for the open button.
  59. * @type array $intl Locale and labels for the modal main elements. If omitted, the related
  60. * elements will not be used. Only exception is `modalAriaLabel` which is
  61. * required by the React modal component.
  62. * @type array $classes Optional. CSS classes for the modal buttons.
  63. * @type string $content The name of the React component to use as the modal content.
  64. * }
  65. */
  66. public static function get_defaults() {
  67. $config = array(
  68. 'mountHook' => '',
  69. 'appElement' => '#wpwrap',
  70. 'openButtonIcon' => '',
  71. 'intl' => array(
  72. 'locale' => WPSEO_Utils::get_user_locale(),
  73. 'open' => __( 'Open', 'wordpress-seo' ),
  74. 'modalAriaLabel' => null,
  75. 'heading' => null,
  76. 'closeIconButton' => __( 'Close', 'wordpress-seo' ),
  77. 'closeButton' => null,
  78. ),
  79. 'classes' => array(
  80. 'openButton' => '',
  81. 'closeIconButton' => '',
  82. 'closeButton' => '',
  83. ),
  84. 'content' => null,
  85. );
  86. return $config;
  87. }
  88. }