class-replacevar-field.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Menu
  6. */
  7. /**
  8. * Renders a single replacement variable field.
  9. */
  10. class WPSEO_Replacevar_Field {
  11. /**
  12. * Forms instance.
  13. *
  14. * @var Yoast_Form Yoast
  15. */
  16. private $yform;
  17. /**
  18. * The id for the hidden field.
  19. *
  20. * @var string
  21. */
  22. private $field_id;
  23. /**
  24. * The label for the field.
  25. *
  26. * @var string
  27. */
  28. private $label;
  29. /**
  30. * The page type for the context of the recommended replace vars.
  31. *
  32. * @var string
  33. */
  34. private $page_type_recommended;
  35. /**
  36. * The page type for the context of the editor specific replace vars.
  37. *
  38. * @var string
  39. */
  40. private $page_type_specific;
  41. /**
  42. * Constructs the object.
  43. *
  44. * @param Yoast_Form $yform Yoast forms.
  45. * @param string $field_id The field id.
  46. * @param string $label The field label.
  47. * @param string $page_type_recommended The page type for the context of the recommended replace vars.
  48. * @param string $page_type_specific The page type for the context of the editor specific replace vars.
  49. */
  50. public function __construct( Yoast_Form $yform, $field_id, $label, $page_type_recommended, $page_type_specific ) {
  51. $this->yform = $yform;
  52. $this->field_id = $field_id;
  53. $this->label = $label;
  54. $this->page_type_recommended = $page_type_recommended;
  55. $this->page_type_specific = $page_type_specific;
  56. }
  57. /**
  58. * Renders a div for the react application to mount to, and hidden inputs where
  59. * the app should store it's value so they will be properly saved when the form
  60. * is submitted.
  61. *
  62. * @return void
  63. */
  64. public function render() {
  65. $this->yform->hidden( $this->field_id, $this->field_id );
  66. printf(
  67. '<div
  68. data-react-replacevar-field
  69. data-react-replacevar-field-id="%1$s"
  70. data-react-replacevar-field-label="%2$s"
  71. data-react-replacevar-page-type-recommended="%3$s"
  72. data-react-replacevar-page-type-specific="%4$s"></div>',
  73. esc_attr( $this->field_id ),
  74. esc_attr( $this->label ),
  75. esc_attr( $this->page_type_recommended ),
  76. esc_attr( $this->page_type_specific )
  77. );
  78. }
  79. }