assets
1 month ago
builders
1 year ago
use-form
3 weeks ago
module.php
1 year ago
preview.php
4 weeks ago
preview.php
107 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Modules\Onboarding; |
| 4 | |
| 5 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 6 | use Jet_Form_Builder\Blocks\Module; |
| 7 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 8 | use JFB_Components\Wp_Nonce\Wp_Nonce; |
| 9 | |
| 10 | class Preview { |
| 11 | |
| 12 | const NONCE_FIELD = Block_Helper::PREVIEW_NONCE_FIELD; |
| 13 | |
| 14 | private $nonce; |
| 15 | |
| 16 | public function __construct() { |
| 17 | $this->nonce = new Wp_Nonce( |
| 18 | Block_Helper::PREVIEW_NONCE_ACTION, |
| 19 | array( |
| 20 | 'name' => self::NONCE_FIELD, |
| 21 | ) |
| 22 | ); |
| 23 | } |
| 24 | |
| 25 | public function init_hooks() { |
| 26 | add_filter( |
| 27 | 'jet-form-builder/post-type/args', |
| 28 | array( $this, 'set_form_publicly_queryable' ) |
| 29 | ); |
| 30 | add_filter( |
| 31 | 'the_content', |
| 32 | array( $this, 'render_form_preview' ) |
| 33 | ); |
| 34 | add_action( |
| 35 | 'jet-form-builder/editor-assets/before', |
| 36 | array( $this, 'editor_assets_before' ), |
| 37 | 20 |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | public function set_form_publicly_queryable( array $args ): array { |
| 42 | if ( is_admin() || |
| 43 | ! $this->get_nonce()->verify() || |
| 44 | ! current_user_can( 'edit_jet_fb_form', get_the_ID() ) |
| 45 | ) { |
| 46 | return $args; |
| 47 | } |
| 48 | |
| 49 | $args['publicly_queryable'] = true; |
| 50 | |
| 51 | return $args; |
| 52 | } |
| 53 | |
| 54 | public function render_form_preview( $content ) { |
| 55 | if ( ! is_singular( 'jet-form-builder' ) ) { |
| 56 | return $content; |
| 57 | } |
| 58 | |
| 59 | if ( ! did_action( 'jet-form-builder/render-preview' ) ) { |
| 60 | do_action( 'jet-form-builder/render-preview' ); |
| 61 | } |
| 62 | |
| 63 | $form_id = get_the_ID(); |
| 64 | $latest_revision = wp_get_latest_revision_id_and_total_count(); |
| 65 | |
| 66 | if ( ! empty( $latest_revision['latest_id'] ) ) { |
| 67 | $form_id = (int) $latest_revision['latest_id']; |
| 68 | } |
| 69 | |
| 70 | /** @var Module $blocks */ |
| 71 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 72 | $blocks = jet_form_builder()->module( Module::class ); |
| 73 | |
| 74 | return $blocks->get_form_class()->render_callback_field( array( 'form_id' => $form_id ) ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @return void |
| 79 | * @throws Repository_Exception |
| 80 | */ |
| 81 | public function editor_assets_before() { |
| 82 | /** @var \JFB_Modules\Onboarding\Module $onboarding */ |
| 83 | $onboarding = jet_form_builder()->module( 'onboarding' ); |
| 84 | |
| 85 | wp_localize_script( |
| 86 | $onboarding->get_handle(), |
| 87 | 'JFBOnboardingConfig', |
| 88 | array( |
| 89 | 'previewURL' => add_query_arg( |
| 90 | array( |
| 91 | self::NONCE_FIELD => $this->get_nonce()->create(), |
| 92 | ), |
| 93 | get_permalink() |
| 94 | ), |
| 95 | ) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @return Wp_Nonce |
| 101 | */ |
| 102 | public function get_nonce(): Wp_Nonce { |
| 103 | return $this->nonce; |
| 104 | } |
| 105 | |
| 106 | } |
| 107 |