class-paper-presenter.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. * @var string Title of the paper
  13. */
  14. private $title;
  15. /**
  16. * @var array The view variables.
  17. */
  18. private $settings;
  19. /**
  20. * @var string The path to the view file.
  21. */
  22. private $view_file;
  23. /**
  24. * WPSEO_presenter_paper constructor.
  25. *
  26. * @param string $title The title of the paper.
  27. * @param string $view_file The path to the view file.
  28. * @param array $settings Optional. Settings for the paper.
  29. */
  30. public function __construct( $title, $view_file, array $settings = array() ) {
  31. $defaults = array(
  32. 'paper_id' => null,
  33. 'collapsible' => false,
  34. 'expanded' => false,
  35. 'help_text' => '',
  36. 'title_after' => '',
  37. 'view_data' => array(),
  38. );
  39. $this->settings = wp_parse_args( $settings, $defaults );
  40. $this->title = $title;
  41. $this->view_file = $view_file;
  42. }
  43. /**
  44. * Renders the collapsible paper and returns it as a string.
  45. *
  46. * @return string The rendered paper.
  47. */
  48. public function get_output() {
  49. extract( $this->get_view_variables(), EXTR_SKIP );
  50. ob_start();
  51. require WPSEO_PATH . 'admin/views/paper-collapsible.php' ;
  52. $rendered_output = ob_get_clean();
  53. return $rendered_output;
  54. }
  55. /**
  56. * Retrieves the view variables.
  57. *
  58. * @return array The view variables.
  59. */
  60. private function get_view_variables() {
  61. if ( $this->settings['help_text'] instanceof WPSEO_Admin_Help_Panel === false ) {
  62. $this->settings['help_text'] = new WPSEO_Admin_Help_Panel( '', '', '' );
  63. }
  64. $view_variables = array(
  65. 'collapsible' => $this->settings['collapsible'],
  66. 'collapsible_config' => $this->collapsible_config(),
  67. 'title_after' => $this->settings['title_after'],
  68. 'help_text' => $this->settings['help_text'],
  69. 'view_file' => $this->view_file,
  70. 'title' => $this->title,
  71. 'paper_id' => $this->settings['paper_id'],
  72. 'yform' => Yoast_Form::get_instance(),
  73. );
  74. return array_merge( $this->settings['view_data'], $view_variables );
  75. }
  76. /**
  77. * Retrieves the collapsible config based on the settings.
  78. *
  79. * @return array The config.
  80. */
  81. protected function collapsible_config() {
  82. if ( empty( $this->settings['collapsible'] ) ) {
  83. return array(
  84. 'toggle_icon' => '',
  85. 'class' => '',
  86. 'expanded' => '',
  87. );
  88. }
  89. if ( ! empty( $this->settings['expanded'] ) ) {
  90. return array(
  91. 'toggle_icon' => 'dashicons-arrow-up-alt2',
  92. 'class' => 'toggleable-container',
  93. 'expanded' => 'true',
  94. );
  95. }
  96. return array(
  97. 'toggle_icon' => 'dashicons-arrow-down-alt2',
  98. 'class' => 'toggleable-container toggleable-container-hidden',
  99. 'expanded' => 'false',
  100. );
  101. }
  102. }