FormPreview.php
108 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 4.16.6 add user capability check |
| 51 | * @since 2.8.0 |
| 52 | **/ |
| 53 | public function setup_form_preview() |
| 54 | { |
| 55 | if (empty($_GET['page']) || $this->slug !== $_GET['page'] || ! current_user_can('manage_give_settings')) { // WPCS: CSRF ok, input var ok. |
| 56 | return; |
| 57 | } else { |
| 58 | $this->render_page(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Renders form preview markup |
| 64 | * |
| 65 | * Uses an object buffer to display the form preview template |
| 66 | * |
| 67 | * @since 2.8.0 |
| 68 | **/ |
| 69 | public function render_page() |
| 70 | { |
| 71 | $this->register_scripts(); |
| 72 | ob_start(); |
| 73 | include_once plugin_dir_path(__FILE__) . 'templates/form-preview.php'; |
| 74 | exit; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Registers form preview scripts/styles |
| 79 | * |
| 80 | * @since 2.8.0 |
| 81 | **/ |
| 82 | protected function register_scripts() |
| 83 | { |
| 84 | wp_register_style( |
| 85 | 'give-styles', |
| 86 | (new Give_Scripts)->get_frontend_stylesheet_uri(), |
| 87 | [], |
| 88 | GIVE_VERSION, |
| 89 | 'all' |
| 90 | ); |
| 91 | |
| 92 | EnqueueScript::make('give', 'build/assets/dist/js/give.js' ) |
| 93 | ->registerTranslations() |
| 94 | ->register(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns the ID of the form used for the form preview |
| 99 | * |
| 100 | * @since 2.8.0 |
| 101 | **/ |
| 102 | protected function get_preview_form_id() |
| 103 | { |
| 104 | return $this->formRepository->getOrMake(); |
| 105 | } |
| 106 | |
| 107 | } |
| 108 |