class-replacevar-field.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * @var Yoast_Form Yoast Forms instance.
  13. */
  14. private $yform;
  15. /**
  16. * @var string The id for the hidden field.
  17. */
  18. private $field_id;
  19. /**
  20. * @var string The label for the field.
  21. */
  22. private $label;
  23. /**
  24. * @var string The page type for the context of the recommended replace vars.
  25. */
  26. private $page_type_recommended;
  27. /**
  28. * @var string The page type for the context of the editor specific replace vars.
  29. */
  30. private $page_type_specific;
  31. /**
  32. * Constructs the object.
  33. *
  34. * @param Yoast_Form $yform Yoast forms.
  35. * @param string $field_id The field id.
  36. * @param string $label The field label.
  37. * @param string $page_type_recommended The page type for the context of the recommended replace vars.
  38. * @param string $page_type_specific The page type for the context of the editor specific replace vars.
  39. */
  40. public function __construct( Yoast_Form $yform, $field_id, $label, $page_type_recommended, $page_type_specific ) {
  41. $this->yform = $yform;
  42. $this->field_id = $field_id;
  43. $this->label = $label;
  44. $this->page_type_recommended = $page_type_recommended;
  45. $this->page_type_specific = $page_type_specific;
  46. }
  47. /**
  48. * Renders a div for the react application to mount to, and hidden inputs where
  49. * the app should store it's value so they will be properly saved when the form
  50. * is submitted.
  51. *
  52. * @return void
  53. */
  54. public function render() {
  55. $this->yform->hidden( $this->field_id, $this->field_id );
  56. printf( '<div
  57. data-react-replacevar-field
  58. data-react-replacevar-field-id="%1$s"
  59. data-react-replacevar-field-label="%2$s"
  60. data-react-replacevar-page-type-recommended="%3$s"
  61. data-react-replacevar-page-type-specific="%4$s"></div>',
  62. esc_attr( $this->field_id ),
  63. esc_attr( $this->label ),
  64. esc_attr( $this->page_type_recommended ),
  65. esc_attr( $this->page_type_specific )
  66. );
  67. }
  68. }