PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.1
JetFormBuilder — Dynamic Blocks Form Builder v3.6.1
3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 All 130 releases
jetformbuilder / modules / onboarding / preview.php
preview.php
106 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace JFB_Modules\Onboarding;
4
5 use Jet_Form_Builder\Blocks\Module;
6 use Jet_Form_Builder\Exceptions\Repository_Exception;
7 use JFB_Components\Wp_Nonce\Wp_Nonce;
8
9 class Preview {
10
11 const NONCE_FIELD = 'jfb_preview_nonce';
12
13 private $nonce;
14
15 public function __construct() {
16 $this->nonce = new Wp_Nonce(
17 'jfb-preview-form',
18 array(
19 'name' => self::NONCE_FIELD,
20 )
21 );
22 }
23
24 public function init_hooks() {
25 add_filter(
26 'jet-form-builder/post-type/args',
27 array( $this, 'set_form_publicly_queryable' )
28 );
29 add_filter(
30 'the_content',
31 array( $this, 'render_form_preview' )
32 );
33 add_action(
34 'jet-form-builder/editor-assets/before',
35 array( $this, 'editor_assets_before' ),
36 20
37 );
38 }
39
40 public function set_form_publicly_queryable( array $args ): array {
41 if ( is_admin() ||
42 ! $this->get_nonce()->verify() ||
43 ! current_user_can( 'edit_jet_fb_form', get_the_ID() )
44 ) {
45 return $args;
46 }
47
48 $args['publicly_queryable'] = true;
49
50 return $args;
51 }
52
53 public function render_form_preview( $content ) {
54 if ( ! is_singular( 'jet-form-builder' ) ) {
55 return $content;
56 }
57
58 if ( ! did_action( 'jet-form-builder/render-preview' ) ) {
59 do_action( 'jet-form-builder/render-preview' );
60 }
61
62 $form_id = get_the_ID();
63 $latest_revision = wp_get_latest_revision_id_and_total_count();
64
65 if ( ! empty( $latest_revision['latest_id'] ) ) {
66 $form_id = (int) $latest_revision['latest_id'];
67 }
68
69 /** @var Module $blocks */
70 /** @noinspection PhpUnhandledExceptionInspection */
71 $blocks = jet_form_builder()->module( Module::class );
72
73 return $blocks->get_form_class()->render_callback_field( array( 'form_id' => $form_id ) );
74 }
75
76 /**
77 * @return void
78 * @throws Repository_Exception
79 */
80 public function editor_assets_before() {
81 /** @var \JFB_Modules\Onboarding\Module $onboarding */
82 $onboarding = jet_form_builder()->module( 'onboarding' );
83
84 wp_localize_script(
85 $onboarding->get_handle(),
86 'JFBOnboardingConfig',
87 array(
88 'previewURL' => add_query_arg(
89 array(
90 self::NONCE_FIELD => $this->get_nonce()->create(),
91 ),
92 get_permalink()
93 ),
94 )
95 );
96 }
97
98 /**
99 * @return Wp_Nonce
100 */
101 public function get_nonce(): Wp_Nonce {
102 return $this->nonce;
103 }
104
105 }
106