FormPreview.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Onboarding\Wizard; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Give\Helpers\EnqueueScript; |
| 8 | use Give\Onboarding\FormRepository; |
| 9 | use Give_Scripts; |
| 10 | |
| 11 | /** |
| 12 | * Form Preview page class |
| 13 | * |
| 14 | * Responsible for setting up and rendering Form Preview page at wp-admin/?page=give-form-preview |
| 15 | * This URL is used as the src for an iframe which appears inside the Onboarding Wizard. |
| 16 | * |
| 17 | * @since 2.8.0 |
| 18 | */ |
| 19 | class FormPreview |
| 20 | { |
| 21 | |
| 22 | /** @var string $slug Page slug used for displaying form preview */ |
| 23 | protected $slug = 'give-form-preview'; |
| 24 | |
| 25 | /** @var FormRepository */ |
| 26 | protected $formRepository; |
| 27 | |
| 28 | public function __construct(FormRepository $formRepository) |
| 29 | { |
| 30 | $this->formRepository = $formRepository; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Adds Form Preview as dashboard page |
| 35 | * |
| 36 | * Register Form Preview as an admin page route |
| 37 | * |
| 38 | * @since 2.8.0 |
| 39 | **/ |
| 40 | public function add_page() |
| 41 | { |
| 42 | add_submenu_page('', '', '', 'manage_options', $this->slug); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Conditionally renders Form Preview markup |
| 47 | * |
| 48 | * If the current page query matches the form preview's slug, method renders the form preview. |
| 49 | * |
| 50 | * @since 2.8.0 |
| 51 | **/ |
| 52 | public function setup_form_preview() |
| 53 | { |
| 54 | if (empty($_GET['page']) || $this->slug !== $_GET['page']) { // WPCS: CSRF ok, input var ok. |
| 55 | return; |
| 56 | } else { |
| 57 | $this->render_page(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Renders form preview markup |
| 63 | * |
| 64 | * Uses an object buffer to display the form preview template |
| 65 | * |
| 66 | * @since 2.8.0 |
| 67 | **/ |
| 68 | public function render_page() |
| 69 | { |
| 70 | $this->register_scripts(); |
| 71 | ob_start(); |
| 72 | include_once plugin_dir_path(__FILE__) . 'templates/form-preview.php'; |
| 73 | exit; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Registers form preview scripts/styles |
| 78 | * |
| 79 | * @since 2.8.0 |
| 80 | **/ |
| 81 | protected function register_scripts() |
| 82 | { |
| 83 | wp_register_style( |
| 84 | 'give-styles', |
| 85 | (new Give_Scripts)->get_frontend_stylesheet_uri(), |
| 86 | [], |
| 87 | GIVE_VERSION, |
| 88 | 'all' |
| 89 | ); |
| 90 | |
| 91 | EnqueueScript::make('give', 'build/assets/dist/js/give.js' ) |
| 92 | ->registerTranslations() |
| 93 | ->register(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns the ID of the form used for the form preview |
| 98 | * |
| 99 | * @since 2.8.0 |
| 100 | **/ |
| 101 | protected function get_preview_form_id() |
| 102 | { |
| 103 | return $this->formRepository->getOrMake(); |
| 104 | } |
| 105 | |
| 106 | } |
| 107 |