PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.3.1
JetFormBuilder — Dynamic Blocks Form Builder v3.3.1
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 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / post-type / module.php
jetformbuilder / modules / post-type Last commit date
actions 2 years ago assets 2 years ago meta 2 years ago rest-api 2 years ago templates 2 years ago actions-repository.php 2 years ago icon.php 2 years ago meta-repository.php 2 years ago module.php 2 years ago
module.php
379 lines
1 <?php
2
3 namespace JFB_Modules\Post_Type;
4
5 // If this file is called directly, abort.
6 if ( ! defined( 'WPINC' ) ) {
7 die;
8 }
9
10 use Jet_Form_Builder\Classes\Compatibility;
11 use Jet_Form_Builder\Classes\Tools;
12 use JFB_Components\Module\Base_Module_After_Install_It;
13 use JFB_Components\Module\Base_Module_Dir_It;
14 use JFB_Components\Module\Base_Module_Dir_Trait;
15 use JFB_Components\Module\Base_Module_Handle_It;
16 use JFB_Components\Module\Base_Module_Handle_Trait;
17 use JFB_Components\Module\Base_Module_It;
18 use JFB_Components\Module\Base_Module_Url_It;
19 use JFB_Components\Module\Base_Module_Url_Trait;
20 use JFB_Modules\Post_Type\Rest_Api\Forms_Post_Type_Controller;
21
22 /**
23 * @since 3.2.0
24 *
25 * Class Module
26 * @package JFB_Modules\Post_Type
27 */
28 class Module implements
29 Base_Module_It,
30 Base_Module_Url_It,
31 Base_Module_Handle_It,
32 Base_Module_Dir_It,
33 Base_Module_After_Install_It {
34
35 use Base_Module_Url_Trait;
36 use Base_Module_Handle_Trait;
37 use Base_Module_Dir_Trait;
38
39 /**
40 * Used to define the editor
41 *
42 * @var boolean
43 */
44 public $is_form_editor = false;
45
46 /** @var Meta_Repository */
47 private $meta;
48
49 /** @var Actions_Repository */
50 private $post_actions;
51
52 const CAPABILITIES = array(
53 'edit_jet_fb_form',
54 'read_jet_fb_form',
55 'delete_jet_fb_form',
56 'edit_jet_fb_forms',
57 'edit_others_jet_fb_forms',
58 'delete_jet_fb_forms',
59 'publish_jet_fb_forms',
60 'read_private_jet_fb_forms',
61 );
62
63 const SLUG = 'jet-form-builder';
64
65 public function rep_item_id() {
66 return 'post-type';
67 }
68
69 public function condition(): bool {
70 return true;
71 }
72
73 public function on_install() {
74 $this->meta = new Meta_Repository();
75 $this->post_actions = new Actions_Repository();
76
77 $this->get_meta()->rep_install();
78
79 if ( is_admin() ) {
80 $this->post_actions->rep_install();
81 }
82 }
83
84 public function on_uninstall() {
85 $this->get_meta()->rep_clear();
86 $this->get_post_actions()->rep_clear();
87 }
88
89 public function init_hooks() {
90 add_action( 'init', array( $this, 'register_post_type' ) );
91 add_action( 'current_screen', array( $this, 'set_current_screen' ) );
92
93 /**
94 * @since 3.0.1
95 */
96 add_filter( 'gutenberg_can_edit_post_type', array( $this, 'can_edit_post_type' ), 150, 2 );
97 add_filter( 'use_block_editor_for_post_type', array( $this, 'can_edit_post_type' ), 150, 2 );
98
99 /** @since 3.1.1 */
100 // watch on this methods performance because it executed multiple times on each page load
101 add_filter( 'user_has_cap', array( $this, 'add_admin_capabilities' ) );
102
103 add_filter( 'post_row_actions', array( $this->get_post_actions(), 'base_add_action_links' ), 10, 2 );
104 add_action( 'admin_enqueue_scripts', array( $this, 'import_form_js' ) );
105 }
106
107 public function remove_hooks() {
108 remove_action( 'init', array( $this, 'register_post_type' ) );
109 remove_action( 'current_screen', array( $this, 'set_current_screen' ) );
110
111 /**
112 * @since 3.0.1
113 */
114 remove_filter( 'gutenberg_can_edit_post_type', array( $this, 'can_edit_post_type' ), 150 );
115 remove_filter( 'use_block_editor_for_post_type', array( $this, 'can_edit_post_type' ), 150 );
116
117 /** @since 3.1.1 */
118 // watch on this methods performance because it executed multiple times on each page load
119 remove_filter( 'user_has_cap', array( $this, 'add_admin_capabilities' ) );
120
121 remove_filter( 'post_row_actions', array( $this->get_post_actions(), 'base_add_action_links' ) );
122 remove_action( 'admin_enqueue_scripts', array( $this, 'import_form_js' ) );
123 }
124
125 public function set_current_screen() {
126 $screen = get_current_screen();
127
128 if ( ! $screen || ! $screen->is_block_editor ) {
129 return;
130 }
131
132 if ( self::SLUG === $screen->id ) {
133 $this->is_form_editor = true;
134
135 } elseif ( self::SLUG !== $screen->id ) {
136 $this->is_form_editor = false;
137 }
138 }
139
140 public static function is_form_list_page(): bool {
141 if ( ! did_action( 'current_screen' ) ) {
142 return false;
143 }
144
145 $screen = get_current_screen();
146
147 return ( 'edit-' . self::SLUG ) === $screen->id;
148 }
149
150 /**
151 * @param $can
152 * @param $post_type
153 *
154 * @return bool
155 * @since 3.0.1
156 */
157 public function can_edit_post_type( $can, $post_type ): bool {
158 return self::SLUG === $post_type ? true : $can;
159 }
160
161 /**
162 * Register templates post type
163 *
164 * @return void
165 */
166 public function register_post_type() {
167
168 $args = array(
169 'labels' => array(
170 'name' => __( 'Forms', 'jet-form-builder' ),
171 'all_items' => __( 'Forms', 'jet-form-builder' ),
172 'add_new' => __( 'Add New', 'jet-form-builder' ),
173 'add_new_item' => __( 'Add New Form', 'jet-form-builder' ),
174 'edit_item' => __( 'Edit Form', 'jet-form-builder' ),
175 'new_item' => __( 'New Form', 'jet-form-builder' ),
176 'view_item' => __( 'View Form', 'jet-form-builder' ),
177 'search_items' => __( 'Search Form', 'jet-form-builder' ),
178 'not_found' => __( 'No Forms Found', 'jet-form-builder' ),
179 'not_found_in_trash' => __( 'No Forms Found In Trash', 'jet-form-builder' ),
180 'singular_name' => __( 'JetForm', 'jet-form-builder' ),
181 'menu_name' => __( 'JetFormBuilder', 'jet-form-builder' ),
182 ),
183 'public' => true,
184 'show_ui' => true,
185 'show_in_admin_bar' => true,
186 'show_in_menu' => true,
187 'show_in_nav_menus' => false,
188 'show_in_rest' => true,
189 'rest_controller_class' => Forms_Post_Type_Controller::class,
190 'publicly_queryable' => false,
191 'exclude_from_search' => true,
192 'has_archive' => false,
193 'query_var' => false,
194 'can_export' => true,
195 'rewrite' => false,
196 'capability_type' => 'jet_fb_form',
197 'menu_icon' => $this->get_post_type_icon(),
198 'menu_position' => 120,
199 'supports' => array( 'title', 'editor', 'custom-fields' ),
200 );
201
202 register_post_type(
203 self::SLUG,
204 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
205 apply_filters( 'jet-form-builder/post-type/args', $args )
206 );
207
208 $this->get_meta()->after_register_post_type();
209 }
210
211 public function add_admin_capabilities( $allcaps ) {
212 $capability = apply_filters( 'jet-form-builder/capability/form', 'manage_options' );
213
214 if ( empty( $allcaps[ $capability ] ) ) {
215 return $allcaps;
216 }
217
218 foreach ( self::CAPABILITIES as $capability ) {
219 $allcaps[ $capability ] = true;
220 }
221
222 return $allcaps;
223 }
224
225 public function import_form_js() {
226 if ( ! self::is_form_list_page() ) {
227 return;
228 }
229
230 $action = $this->get_post_actions()->get( 'jet_fb_import' );
231
232 $import_button = __( 'Start Import', 'jet-form-builder' );
233
234 ob_start();
235 include $this->get_dir( 'templates/import-form.php' );
236 $import_template = ob_get_clean();
237
238 $handle = $this->get_handle();
239
240 wp_enqueue_script(
241 $handle,
242 $this->get_url( 'assets/build/js/import-form.js' ),
243 array( 'jquery' ),
244 jet_form_builder()->get_version(),
245 true
246 );
247
248 wp_localize_script(
249 $handle,
250 'JetFormBuilderImportForm',
251 array(
252 'id' => $action->get_id(),
253 'template' => $import_template,
254 )
255 );
256 }
257
258 private function get_post_type_icon() {
259 $path = $this->get_dir( 'icon.php' );
260
261 return include_once $path;
262 }
263
264
265 public function maybe_get_jet_sm_ready_styles( $form_id ) {
266 return Compatibility::has_jet_sm() ? get_post_meta( $form_id, '_jet_sm_ready_style', true ) : '';
267 }
268
269 /**
270 * Returns form meta arguments:
271 * fields_layout, submit_type, captcha and required_mark
272 * in assoc array
273 *
274 * @param int|false $form_id
275 *
276 * @return array
277 */
278 public function get_args( $form_id = false ): array {
279 return $this->get_meta()->get_args( $form_id );
280 }
281
282 /**
283 * Returns form messages
284 *
285 * @param int|false $form_id
286 *
287 * @return array
288 */
289 public function get_messages( $form_id = false ) {
290 return $this->get_meta()->get_messages( $form_id );
291 }
292
293 /**
294 * Returns form actions
295 *
296 * @param int|false $form_id
297 *
298 * @return array
299 */
300 public function get_preset( $form_id = false ) {
301 return $this->get_meta()->get_preset( $form_id );
302 }
303
304 /**
305 * Returns captcha settings
306 *
307 * @param int|false $form_id
308 *
309 * @return array
310 */
311 public function get_captcha( $form_id = false ) {
312 return $this->get_meta()->get_captcha( $form_id );
313 }
314
315 /**
316 * Returns form gateways
317 *
318 * @param int|false $form_id
319 *
320 * @return array
321 */
322 public function get_gateways( $form_id = false ): array {
323 return $this->get_meta()->get_gateways( $form_id );
324 }
325
326 public function get_actions( $form_id = false ): array {
327 return $this->get_meta()->get_actions( $form_id );
328 }
329
330 /**
331 * @param string $name
332 * @param false|bool $form_id
333 *
334 * @return array|mixed
335 * @since 3.3.0
336 */
337 public function query_meta( string $name, $form_id = false ) {
338 return $this->get_meta()->get_meta( $name )->query( $form_id );
339 }
340
341 public function get_meta(): Meta_Repository {
342 return $this->meta;
343 }
344
345 /**
346 * @return Actions_Repository
347 */
348 public function get_post_actions(): Actions_Repository {
349 return $this->post_actions;
350 }
351
352 /**
353 * @deprecated 3.2.0
354 *
355 * @return string
356 */
357 public function slug(): string {
358 return self::SLUG;
359 }
360
361 /**
362 * @return Actions_Repository
363 */
364 public function get_form_meta( $meta_key, $form_id = false ) {
365 if ( false === $form_id ) {
366 $form_id = jet_fb_live()->form_id;
367 }
368
369 return Tools::decode_json(
370 get_post_meta(
371 $form_id,
372 $meta_key,
373 true
374 )
375 );
376 }
377
378 }
379