class-replacevar-editor.php 3.6 KB

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