class-replacevar-editor.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Menu
  6. */
  7. /**
  8. * Renders a replacement variable editor.
  9. */
  10. class WPSEO_Replacevar_Editor {
  11. /**
  12. * Yoast Forms instance.
  13. *
  14. * @var Yoast_Form
  15. */
  16. private $yform;
  17. /**
  18. * The arguments required for the div to render.
  19. *
  20. * @var array {
  21. * @type string $title The title field id.
  22. * @type string $description The description field id.
  23. * @type string $page_type_recommended The page type for the context of the recommended replace vars.
  24. * @type string $page_type_specific The page type for the context of the editor specific replace vars.
  25. * @type bool $paper_style Optional. Whether the editor has paper style.
  26. * }
  27. */
  28. private $arguments;
  29. /**
  30. * Constructs the object.
  31. *
  32. * @param Yoast_Form $yform Yoast forms.
  33. * @param array $arguments {
  34. * The arguments that can be given.
  35. *
  36. * @type string $title The title field id.
  37. * @type string $description The description field id.
  38. * @type string $page_type_recommended The page type for the context of the recommended replace vars.
  39. * @type string $page_type_specific The page type for the context of the editor specific replace vars.
  40. * @type bool $paper_style Optional. Whether the editor has paper style.
  41. * }
  42. */
  43. public function __construct( Yoast_Form $yform, $arguments ) {
  44. $arguments = wp_parse_args(
  45. $arguments,
  46. [
  47. 'paper_style' => true,
  48. ]
  49. );
  50. $this->validate_arguments( $arguments );
  51. $this->yform = $yform;
  52. $this->arguments = [
  53. 'title' => (string) $arguments['title'],
  54. 'description' => (string) $arguments['description'],
  55. 'page_type_recommended' => (string) $arguments['page_type_recommended'],
  56. 'page_type_specific' => (string) $arguments['page_type_specific'],
  57. 'paper_style' => (bool) $arguments['paper_style'],
  58. ];
  59. }
  60. /**
  61. * Renders a div for the react application to mount to, and hidden inputs where
  62. * the app should store it's value so they will be properly saved when the form
  63. * is submitted.
  64. *
  65. * @return void
  66. */
  67. public function render() {
  68. $this->yform->hidden( $this->arguments['title'], $this->arguments['title'] );
  69. $this->yform->hidden( $this->arguments['description'], $this->arguments['description'] );
  70. printf(
  71. '<div
  72. data-react-replacevar-editor
  73. data-react-replacevar-title-field-id="%1$s"
  74. data-react-replacevar-metadesc-field-id="%2$s"
  75. data-react-replacevar-page-type-recommended="%3$s"
  76. data-react-replacevar-page-type-specific="%4$s"
  77. data-react-replacevar-paper-style="%5$s"></div>',
  78. esc_attr( $this->arguments['title'] ),
  79. esc_attr( $this->arguments['description'] ),
  80. esc_attr( $this->arguments['page_type_recommended'] ),
  81. esc_attr( $this->arguments['page_type_specific'] ),
  82. esc_attr( $this->arguments['paper_style'] )
  83. );
  84. }
  85. /**
  86. * Validates the replacement variable editor arguments.
  87. *
  88. * @param array $arguments The arguments to validate.
  89. *
  90. * @throws InvalidArgumentException Thrown when not all required arguments are present.
  91. */
  92. protected function validate_arguments( array $arguments ) {
  93. $required_arguments = [
  94. 'title',
  95. 'description',
  96. 'page_type_recommended',
  97. 'page_type_specific',
  98. 'paper_style',
  99. ];
  100. foreach ( $required_arguments as $field_name ) {
  101. if ( ! array_key_exists( $field_name, $arguments ) ) {
  102. throw new InvalidArgumentException(
  103. sprintf(
  104. /* translators: %1$s expands to the missing field name. */
  105. __( 'Not all required fields are given. Missing field %1$s', 'wordpress-seo' ),
  106. $field_name
  107. )
  108. );
  109. }
  110. }
  111. }
  112. }