class-paper-presenter.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Class WPSEO_presenter_paper.
  9. */
  10. class WPSEO_Paper_Presenter {
  11. /**
  12. * Title of the paper.
  13. *
  14. * @var string
  15. */
  16. private $title;
  17. /**
  18. * The view variables.
  19. *
  20. * @var array
  21. */
  22. private $settings;
  23. /**
  24. * The path to the view file.
  25. *
  26. * @var string
  27. */
  28. private $view_file;
  29. /**
  30. * WPSEO_presenter_paper constructor.
  31. *
  32. * @param string $title The title of the paper.
  33. * @param string $view_file Optional. The path to the view file. Use the content setting if you do not wish to use
  34. * a view file.
  35. * @param array $settings Optional. Settings for the paper.
  36. */
  37. public function __construct( $title, $view_file = null, array $settings = [] ) {
  38. $defaults = [
  39. 'paper_id' => null,
  40. 'paper_id_prefix' => 'wpseo-',
  41. 'collapsible' => false,
  42. 'collapsible_header_class' => '',
  43. 'expanded' => false,
  44. 'help_text' => '',
  45. 'title_after' => '',
  46. 'class' => '',
  47. 'content' => '',
  48. 'view_data' => [],
  49. ];
  50. $this->settings = wp_parse_args( $settings, $defaults );
  51. $this->title = $title;
  52. $this->view_file = $view_file;
  53. }
  54. /**
  55. * Renders the collapsible paper and returns it as a string.
  56. *
  57. * @return string The rendered paper.
  58. */
  59. public function get_output() {
  60. $view_variables = $this->get_view_variables();
  61. extract( $view_variables, EXTR_SKIP );
  62. $content = $this->settings['content'];
  63. if ( $this->view_file !== null ) {
  64. ob_start();
  65. require $this->view_file;
  66. $content = ob_get_clean();
  67. }
  68. ob_start();
  69. require WPSEO_PATH . 'admin/views/paper-collapsible.php';
  70. $rendered_output = ob_get_clean();
  71. return $rendered_output;
  72. }
  73. /**
  74. * Retrieves the view variables.
  75. *
  76. * @return array The view variables.
  77. */
  78. private function get_view_variables() {
  79. if ( $this->settings['help_text'] instanceof WPSEO_Admin_Help_Panel === false ) {
  80. $this->settings['help_text'] = new WPSEO_Admin_Help_Panel( '', '', '' );
  81. }
  82. $view_variables = [
  83. 'class' => $this->settings['class'],
  84. 'collapsible' => $this->settings['collapsible'],
  85. 'collapsible_config' => $this->collapsible_config(),
  86. 'collapsible_header_class' => $this->settings['collapsible_header_class'],
  87. 'title_after' => $this->settings['title_after'],
  88. 'help_text' => $this->settings['help_text'],
  89. 'view_file' => $this->view_file,
  90. 'title' => $this->title,
  91. 'paper_id' => $this->settings['paper_id'],
  92. 'paper_id_prefix' => $this->settings['paper_id_prefix'],
  93. 'yform' => Yoast_Form::get_instance(),
  94. ];
  95. return array_merge( $this->settings['view_data'], $view_variables );
  96. }
  97. /**
  98. * Retrieves the collapsible config based on the settings.
  99. *
  100. * @return array The config.
  101. */
  102. protected function collapsible_config() {
  103. if ( empty( $this->settings['collapsible'] ) ) {
  104. return [
  105. 'toggle_icon' => '',
  106. 'class' => '',
  107. 'expanded' => '',
  108. ];
  109. }
  110. if ( ! empty( $this->settings['expanded'] ) ) {
  111. return [
  112. 'toggle_icon' => 'dashicons-arrow-up-alt2',
  113. 'class' => 'toggleable-container',
  114. 'expanded' => 'true',
  115. ];
  116. }
  117. return [
  118. 'toggle_icon' => 'dashicons-arrow-down-alt2',
  119. 'class' => 'toggleable-container toggleable-container-hidden',
  120. 'expanded' => 'false',
  121. ];
  122. }
  123. }