Nenhuma Descrição

FormPreview.php 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace MailPoet\Router\Endpoints;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Config\AccessControl;
  5. use MailPoet\Form\PreviewPage;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. class FormPreview {
  8. const ENDPOINT = 'form_preview';
  9. const ACTION_VIEW = 'view';
  10. /** @var WPFunctions */
  11. private $wp;
  12. /** @var array|null */
  13. private $data;
  14. /** @var PreviewPage */
  15. private $formPreviewPage;
  16. public $allowedActions = [self::ACTION_VIEW];
  17. public $permissions = [
  18. 'global' => AccessControl::NO_ACCESS_RESTRICTION,
  19. ];
  20. public function __construct(
  21. WPFunctions $wp,
  22. PreviewPage $formPreviewPage
  23. ) {
  24. $this->wp = $wp;
  25. $this->formPreviewPage = $formPreviewPage;
  26. }
  27. public function view(array $data) {
  28. $this->data = $data;
  29. $this->wp->addFilter('the_content', [$this,'renderContent'], 10);
  30. $this->wp->addFilter('the_title', [$this->formPreviewPage,'renderTitle'], 10, 2);
  31. $this->wp->addFilter('show_admin_bar', function () {
  32. return false;
  33. });
  34. }
  35. public function renderContent(): string {
  36. if (!array_key_exists('id', $this->data ?? []) || !isset($this->data['form_type'])) {
  37. return '';
  38. }
  39. return $this->formPreviewPage->renderPage(
  40. (int)$this->data['id'],
  41. (string)$this->data['form_type'],
  42. (string)$this->data['editor_url']
  43. );
  44. }
  45. }