PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.3.0
JetFormBuilder — Dynamic Blocks Form Builder v3.3.0
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 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
418 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\Arguments\Default_Form_Arguments;
11 use Jet_Form_Builder\Classes\Arguments\Form_Arguments;
12 use Jet_Form_Builder\Classes\Compatibility;
13 use Jet_Form_Builder\Classes\Tools;
14 use Jet_Form_Builder\Shortcodes\Manager;
15 use JFB_Components\Module\Base_Module_After_Install_It;
16 use JFB_Components\Module\Base_Module_Dir_It;
17 use JFB_Components\Module\Base_Module_Dir_Trait;
18 use JFB_Components\Module\Base_Module_Handle_It;
19 use JFB_Components\Module\Base_Module_Handle_Trait;
20 use JFB_Components\Module\Base_Module_It;
21 use JFB_Components\Module\Base_Module_Url_It;
22 use JFB_Components\Module\Base_Module_Url_Trait;
23 use JFB_Modules\Post_Type\Rest_Api\Forms_Post_Type_Controller;
24
25 /**
26 * @since 3.2.0
27 *
28 * Class Module
29 * @package JFB_Modules\Post_Type
30 */
31 class Module implements
32 Base_Module_It,
33 Base_Module_Url_It,
34 Base_Module_Handle_It,
35 Base_Module_Dir_It,
36 Base_Module_After_Install_It {
37
38 use Base_Module_Url_Trait;
39 use Base_Module_Handle_Trait;
40 use Base_Module_Dir_Trait;
41
42 /**
43 * Used to define the editor
44 *
45 * @var boolean
46 */
47 public $is_form_editor = false;
48
49 /** @var Meta_Repository */
50 private $meta;
51
52 /** @var Actions_Repository */
53 private $post_actions;
54
55 const CAPABILITIES = array(
56 'edit_jet_fb_form',
57 'read_jet_fb_form',
58 'delete_jet_fb_form',
59 'edit_jet_fb_forms',
60 'edit_others_jet_fb_forms',
61 'delete_jet_fb_forms',
62 'publish_jet_fb_forms',
63 'read_private_jet_fb_forms',
64 );
65
66 const SLUG = 'jet-form-builder';
67
68 public function rep_item_id() {
69 return 'post-type';
70 }
71
72 public function condition(): bool {
73 return true;
74 }
75
76 public function on_install() {
77 $this->meta = new Meta_Repository();
78 $this->post_actions = new Actions_Repository();
79
80 $this->get_meta()->rep_install();
81
82 if ( is_admin() ) {
83 $this->post_actions->rep_install();
84 }
85 }
86
87 public function on_uninstall() {
88 $this->get_meta()->rep_clear();
89 $this->get_post_actions()->rep_clear();
90 }
91
92 public function init_hooks() {
93 add_action( 'init', array( $this, 'register_post_type' ) );
94 add_action( 'current_screen', array( $this, 'set_current_screen' ) );
95
96 $slug = self::SLUG;
97 add_filter( "manage_{$slug}_posts_columns", array( $this, 'filter_columns' ) );
98 add_action( "manage_{$slug}_posts_custom_column", array( $this, 'add_admin_column_content' ), 10, 2 );
99
100 /**
101 * @since 3.0.1
102 */
103 add_filter( 'gutenberg_can_edit_post_type', array( $this, 'can_edit_post_type' ), 150, 2 );
104 add_filter( 'use_block_editor_for_post_type', array( $this, 'can_edit_post_type' ), 150, 2 );
105
106 /** @since 3.1.1 */
107 // watch on this methods performance because it executed multiple times on each page load
108 add_filter( 'user_has_cap', array( $this, 'add_admin_capabilities' ) );
109
110 add_filter( 'post_row_actions', array( $this->get_post_actions(), 'base_add_action_links' ), 10, 2 );
111 add_action( 'admin_enqueue_scripts', array( $this, 'import_form_js' ) );
112 }
113
114 public function remove_hooks() {
115 remove_action( 'init', array( $this, 'register_post_type' ) );
116 remove_action( 'current_screen', array( $this, 'set_current_screen' ) );
117
118 $slug = self::SLUG;
119 remove_filter( "manage_{$slug}_posts_columns", array( $this, 'filter_columns' ) );
120 remove_action( "manage_{$slug}_posts_custom_column", array( $this, 'add_admin_column_content' ) );
121
122 /**
123 * @since 3.0.1
124 */
125 remove_filter( 'gutenberg_can_edit_post_type', array( $this, 'can_edit_post_type' ), 150 );
126 remove_filter( 'use_block_editor_for_post_type', array( $this, 'can_edit_post_type' ), 150 );
127
128 /** @since 3.1.1 */
129 // watch on this methods performance because it executed multiple times on each page load
130 remove_filter( 'user_has_cap', array( $this, 'add_admin_capabilities' ) );
131
132 remove_filter( 'post_row_actions', array( $this->get_post_actions(), 'base_add_action_links' ) );
133 remove_action( 'admin_enqueue_scripts', array( $this, 'import_form_js' ) );
134 }
135
136 public function filter_columns( $columns ) {
137 $after = array( 'date' => $columns['date'] );
138 unset( $columns['date'] );
139
140 $columns['jfb_shortcode'] = __( 'Shortcode', 'jet-form-builder' );
141
142 return array_merge( $columns, $after );
143 }
144
145 public function add_admin_column_content( $column, $form_id ) {
146 if ( 'jfb_shortcode' !== $column ) {
147 return;
148 }
149 $arguments = array_diff( $this->meta->get_args( $form_id ), Form_Arguments::arguments() );
150
151 $arguments = array_merge(
152 array( 'form_id' => $form_id ),
153 Default_Form_Arguments::arguments(),
154 $arguments
155 );
156
157 printf(
158 '<input readonly type="text" onclick="this.select()" value="%s" style="%s"/>',
159 esc_attr( Manager::get_shortcode( 'jet_fb_form', $arguments ) ),
160 'width: 100%'
161 );
162 }
163
164 public function set_current_screen() {
165 $screen = get_current_screen();
166
167 if ( ! $screen || ! $screen->is_block_editor ) {
168 return;
169 }
170
171 if ( self::SLUG === $screen->id ) {
172 $this->is_form_editor = true;
173
174 } elseif ( self::SLUG !== $screen->id ) {
175 $this->is_form_editor = false;
176 }
177 }
178
179 public static function is_form_list_page(): bool {
180 if ( ! did_action( 'current_screen' ) ) {
181 return false;
182 }
183
184 $screen = get_current_screen();
185
186 return ( 'edit-' . self::SLUG ) === $screen->id;
187 }
188
189 /**
190 * @param $can
191 * @param $post_type
192 *
193 * @return bool
194 * @since 3.0.1
195 */
196 public function can_edit_post_type( $can, $post_type ): bool {
197 return self::SLUG === $post_type ? true : $can;
198 }
199
200 /**
201 * Register templates post type
202 *
203 * @return void
204 */
205 public function register_post_type() {
206
207 $args = array(
208 'labels' => array(
209 'name' => __( 'Forms', 'jet-form-builder' ),
210 'all_items' => __( 'Forms', 'jet-form-builder' ),
211 'add_new' => __( 'Add New', 'jet-form-builder' ),
212 'add_new_item' => __( 'Add New Form', 'jet-form-builder' ),
213 'edit_item' => __( 'Edit Form', 'jet-form-builder' ),
214 'new_item' => __( 'New Form', 'jet-form-builder' ),
215 'view_item' => __( 'View Form', 'jet-form-builder' ),
216 'search_items' => __( 'Search Form', 'jet-form-builder' ),
217 'not_found' => __( 'No Forms Found', 'jet-form-builder' ),
218 'not_found_in_trash' => __( 'No Forms Found In Trash', 'jet-form-builder' ),
219 'singular_name' => __( 'JetForm', 'jet-form-builder' ),
220 'menu_name' => __( 'JetFormBuilder', 'jet-form-builder' ),
221 ),
222 'public' => true,
223 'show_ui' => true,
224 'show_in_admin_bar' => true,
225 'show_in_menu' => true,
226 'show_in_nav_menus' => false,
227 'show_in_rest' => true,
228 'rest_controller_class' => Forms_Post_Type_Controller::class,
229 'publicly_queryable' => false,
230 'exclude_from_search' => true,
231 'has_archive' => false,
232 'query_var' => false,
233 'can_export' => true,
234 'rewrite' => false,
235 'capability_type' => 'jet_fb_form',
236 'menu_icon' => $this->get_post_type_icon(),
237 'menu_position' => 120,
238 'supports' => array( 'title', 'editor', 'custom-fields' ),
239 );
240
241 register_post_type(
242 self::SLUG,
243 // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
244 apply_filters( 'jet-form-builder/post-type/args', $args )
245 );
246
247 $this->get_meta()->after_register_post_type();
248 }
249
250 public function add_admin_capabilities( $allcaps ) {
251 $capability = apply_filters( 'jet-form-builder/capability/form', 'manage_options' );
252
253 if ( empty( $allcaps[ $capability ] ) ) {
254 return $allcaps;
255 }
256
257 foreach ( self::CAPABILITIES as $capability ) {
258 $allcaps[ $capability ] = true;
259 }
260
261 return $allcaps;
262 }
263
264 public function import_form_js() {
265 if ( ! self::is_form_list_page() ) {
266 return;
267 }
268
269 $action = $this->get_post_actions()->get( 'jet_fb_import' );
270
271 $import_button = __( 'Start Import', 'jet-form-builder' );
272
273 ob_start();
274 include $this->get_dir( 'templates/import-form.php' );
275 $import_template = ob_get_clean();
276
277 $handle = $this->get_handle();
278
279 wp_enqueue_script(
280 $handle,
281 $this->get_url( 'assets/build/js/import-form.js' ),
282 array( 'jquery' ),
283 jet_form_builder()->get_version(),
284 true
285 );
286
287 wp_localize_script(
288 $handle,
289 'JetFormBuilderImportForm',
290 array(
291 'id' => $action->get_id(),
292 'template' => $import_template,
293 )
294 );
295 }
296
297 private function get_post_type_icon() {
298 $path = $this->get_dir( 'icon.php' );
299
300 return include_once $path;
301 }
302
303
304 public function maybe_get_jet_sm_ready_styles( $form_id ) {
305 return Compatibility::has_jet_sm() ? get_post_meta( $form_id, '_jet_sm_ready_style', true ) : '';
306 }
307
308 /**
309 * Returns form meta arguments:
310 * fields_layout, submit_type, captcha and required_mark
311 * in assoc array
312 *
313 * @param int|false $form_id
314 *
315 * @return array
316 */
317 public function get_args( $form_id = false ): array {
318 return $this->get_meta()->get_args( $form_id );
319 }
320
321 /**
322 * Returns form messages
323 *
324 * @param int|false $form_id
325 *
326 * @return array
327 */
328 public function get_messages( $form_id = false ) {
329 return $this->get_meta()->get_messages( $form_id );
330 }
331
332 /**
333 * Returns form actions
334 *
335 * @param int|false $form_id
336 *
337 * @return array
338 */
339 public function get_preset( $form_id = false ) {
340 return $this->get_meta()->get_preset( $form_id );
341 }
342
343 /**
344 * Returns captcha settings
345 *
346 * @param int|false $form_id
347 *
348 * @return array
349 */
350 public function get_captcha( $form_id = false ) {
351 return $this->get_meta()->get_captcha( $form_id );
352 }
353
354 /**
355 * Returns form gateways
356 *
357 * @param int|false $form_id
358 *
359 * @return array
360 */
361 public function get_gateways( $form_id = false ): array {
362 return $this->get_meta()->get_gateways( $form_id );
363 }
364
365 public function get_actions( $form_id = false ): array {
366 return $this->get_meta()->get_actions( $form_id );
367 }
368
369 /**
370 * @param string $name
371 * @param false|bool $form_id
372 *
373 * @return array|mixed
374 * @since 3.3.0
375 */
376 public function query_meta( string $name, $form_id = false ) {
377 return $this->get_meta()->get_meta( $name )->query( $form_id );
378 }
379
380 public function get_meta(): Meta_Repository {
381 return $this->meta;
382 }
383
384 /**
385 * @return Actions_Repository
386 */
387 public function get_post_actions(): Actions_Repository {
388 return $this->post_actions;
389 }
390
391 /**
392 * @deprecated 3.2.0
393 *
394 * @return string
395 */
396 public function slug(): string {
397 return self::SLUG;
398 }
399
400 /**
401 * @return Actions_Repository
402 */
403 public function get_form_meta( $meta_key, $form_id = false ) {
404 if ( false === $form_id ) {
405 $form_id = jet_fb_live()->form_id;
406 }
407
408 return Tools::decode_json(
409 get_post_meta(
410 $form_id,
411 $meta_key,
412 true
413 )
414 );
415 }
416
417 }
418