FormPreview.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Onboarding\Wizard; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit; |
| 6 | |
| 7 | use Give_Scripts; |
| 8 | use Give\Onboarding\Helpers\FormatList; |
| 9 | use Give\Onboarding\FormRepository; |
| 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 | $this->formRepository = $formRepository; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Adds Form Preview as dashboard page |
| 34 | * |
| 35 | * Register Form Preview as an admin page route |
| 36 | * |
| 37 | * @since 2.8.0 |
| 38 | **/ |
| 39 | public function add_page() { |
| 40 | add_submenu_page( '', '', '', 'manage_options', $this->slug ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Conditionally renders Form Preview markup |
| 45 | * |
| 46 | * If the current page query matches the form preview's slug, method renders the form preview. |
| 47 | * |
| 48 | * @since 2.8.0 |
| 49 | **/ |
| 50 | public function setup_form_preview() { |
| 51 | if ( empty( $_GET['page'] ) || $this->slug !== $_GET['page'] ) { // WPCS: CSRF ok, input var ok. |
| 52 | return; |
| 53 | } else { |
| 54 | $this->render_page(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Renders form preview markup |
| 60 | * |
| 61 | * Uses an object buffer to display the form preview template |
| 62 | * |
| 63 | * @since 2.8.0 |
| 64 | **/ |
| 65 | public function render_page() { |
| 66 | |
| 67 | $this->register_scripts(); |
| 68 | ob_start(); |
| 69 | include_once plugin_dir_path( __FILE__ ) . 'templates/form-preview.php'; |
| 70 | exit; |
| 71 | |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Registers form preview scripts/styles |
| 76 | * |
| 77 | * @since 2.8.0 |
| 78 | **/ |
| 79 | protected function register_scripts() { |
| 80 | |
| 81 | wp_register_style( |
| 82 | 'give-styles', |
| 83 | ( new Give_Scripts )->get_frontend_stylesheet_uri(), |
| 84 | [], |
| 85 | GIVE_VERSION, |
| 86 | 'all' |
| 87 | ); |
| 88 | |
| 89 | wp_register_script( |
| 90 | 'give', |
| 91 | GIVE_PLUGIN_URL . 'assets/dist/js/give.js', |
| 92 | [ 'jquery' ], |
| 93 | GIVE_VERSION |
| 94 | ); |
| 95 | |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Returns the ID of the form used for the form preview |
| 100 | * |
| 101 | * @since 2.8.0 |
| 102 | **/ |
| 103 | protected function get_preview_form_id() { |
| 104 | return $this->formRepository->getOrMake(); |
| 105 | } |
| 106 | |
| 107 | } |
| 108 |