actions
2 years ago
addons
2 years ago
admin
2 years ago
blocks
2 years ago
classes
2 years ago
db-queries
2 years ago
exceptions
2 years ago
form-actions
2 years ago
form-messages
2 years ago
form-patterns
2 years ago
form-response
2 years ago
generators
2 years ago
integrations
2 years ago
migrations
2 years ago
post-meta
2 years ago
presets
2 years ago
request
2 years ago
shortcodes
2 years ago
autoloader.php
2 years ago
file-upload.php
2 years ago
form-break.php
2 years ago
form-handler.php
2 years ago
form-manager.php
2 years ago
functions.php
2 years ago
live-form.php
2 years ago
plugin.php
2 years ago
post-type.php
2 years ago
post-type.php
393 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder; |
| 4 | |
| 5 | use Jet_Form_Builder\Classes\Arguments\Default_Form_Arguments; |
| 6 | use Jet_Form_Builder\Classes\Arguments\Form_Arguments; |
| 7 | use Jet_Form_Builder\Classes\Compatibility; |
| 8 | use Jet_Form_Builder\Classes\Get_Icon_Trait; |
| 9 | use JFB_Components\Repository\Repository_Pattern_Trait; |
| 10 | use Jet_Form_Builder\Classes\Tools; |
| 11 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 12 | use Jet_Form_Builder\Post_Meta\Actions_Meta; |
| 13 | use Jet_Form_Builder\Post_Meta\Base_Meta_Type; |
| 14 | use Jet_Form_Builder\Post_Meta\Args_Meta; |
| 15 | use Jet_Form_Builder\Post_Meta\Preset_Meta; |
| 16 | use Jet_Form_Builder\Post_Meta\Gateways_Meta; |
| 17 | use Jet_Form_Builder\Post_Meta\Messages_Meta; |
| 18 | use Jet_Form_Builder\Post_Meta\Recaptcha_Meta; |
| 19 | use Jet_Form_Builder\Post_Meta\Validation_Meta; |
| 20 | use Jet_Form_Builder\Shortcodes\Manager; |
| 21 | |
| 22 | // If this file is called directly, abort. |
| 23 | |
| 24 | if ( ! defined( 'WPINC' ) ) { |
| 25 | die; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @method Base_Meta_Type rep_get_item( $class_or_slug ) |
| 30 | * |
| 31 | * Class Post_Type |
| 32 | * |
| 33 | * @package Jet_Form_Builder |
| 34 | */ |
| 35 | class Post_Type { |
| 36 | |
| 37 | use Get_Icon_Trait; |
| 38 | use Repository_Pattern_Trait; |
| 39 | |
| 40 | /** |
| 41 | * Used to define the editor |
| 42 | * |
| 43 | * @var boolean |
| 44 | */ |
| 45 | public $is_form_editor = false; |
| 46 | |
| 47 | /** |
| 48 | * Constructor for the class |
| 49 | */ |
| 50 | public function __construct() { |
| 51 | add_action( 'init', array( $this, 'register_post_type' ) ); |
| 52 | add_action( 'current_screen', array( $this, 'set_current_screen' ) ); |
| 53 | |
| 54 | add_filter( "manage_{$this->slug()}_posts_columns", array( $this, 'filter_columns' ) ); |
| 55 | add_action( "manage_{$this->slug()}_posts_custom_column", array( $this, 'add_admin_column_content' ), 10, 2 ); |
| 56 | |
| 57 | /** |
| 58 | * @since 3.0.1 |
| 59 | */ |
| 60 | add_filter( 'gutenberg_can_edit_post_type', array( $this, 'can_edit_post_type' ), 150, 2 ); |
| 61 | add_filter( 'use_block_editor_for_post_type', array( $this, 'can_edit_post_type' ), 150, 2 ); |
| 62 | |
| 63 | /** @since 3.0.9 */ |
| 64 | add_action( 'admin_init', array( $this, 'add_admin_capabilities' ) ); |
| 65 | register_deactivation_hook( JET_FORM_BUILDER__FILE__, array( $this, 'remove_admin_capabilities' ) ); |
| 66 | } |
| 67 | |
| 68 | public function rep_instances(): array { |
| 69 | return array( |
| 70 | new Args_Meta(), |
| 71 | new Messages_Meta(), |
| 72 | new Preset_Meta(), |
| 73 | new Recaptcha_Meta(), |
| 74 | new Actions_Meta(), |
| 75 | new Gateways_Meta(), |
| 76 | new Validation_Meta(), |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | public function filter_columns( $columns ) { |
| 81 | $after = array( 'date' => $columns['date'] ); |
| 82 | unset( $columns['date'] ); |
| 83 | |
| 84 | $columns['jfb_shortcode'] = __( 'Shortcode', 'jet-form-builder' ); |
| 85 | |
| 86 | return array_merge( $columns, $after ); |
| 87 | } |
| 88 | |
| 89 | public function add_admin_column_content( $column, $form_id ) { |
| 90 | if ( 'jfb_shortcode' !== $column ) { |
| 91 | return; |
| 92 | } |
| 93 | $arguments = array_diff( $this->get_args( $form_id ), Form_Arguments::arguments() ); |
| 94 | |
| 95 | $arguments = array_merge( |
| 96 | array( 'form_id' => $form_id ), |
| 97 | Default_Form_Arguments::arguments(), |
| 98 | $arguments |
| 99 | ); |
| 100 | |
| 101 | printf( |
| 102 | '<input readonly type="text" onclick="this.select()" value="%s" style="%s"/>', |
| 103 | esc_attr( Manager::get_shortcode( 'jet_fb_form', $arguments ) ), |
| 104 | 'width: 100%' |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * Returns current post type slug |
| 111 | * |
| 112 | * @return string [type] [description] |
| 113 | */ |
| 114 | public function slug() { |
| 115 | return 'jet-form-builder'; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | public function set_current_screen() { |
| 120 | $screen = get_current_screen(); |
| 121 | |
| 122 | if ( ! $screen || ! $screen->is_block_editor ) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | if ( $this->slug() === $screen->id ) { |
| 127 | $this->is_form_editor = true; |
| 128 | |
| 129 | } elseif ( $this->slug() !== $screen->id ) { |
| 130 | $this->is_form_editor = false; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | public function is_form_list_page() { |
| 135 | if ( ! did_action( 'current_screen' ) ) { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | $screen = get_current_screen(); |
| 140 | |
| 141 | return ( "edit-{$this->slug()}" === $screen->id ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @param $can |
| 146 | * @param $post_type |
| 147 | * |
| 148 | * @return bool |
| 149 | * @since 3.0.1 |
| 150 | */ |
| 151 | public function can_edit_post_type( $can, $post_type ): bool { |
| 152 | return $this->slug() === $post_type ? true : $can; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Register templates post type |
| 157 | * |
| 158 | * @return void |
| 159 | */ |
| 160 | public function register_post_type() { |
| 161 | |
| 162 | $args = array( |
| 163 | 'labels' => array( |
| 164 | 'name' => __( 'Forms', 'jet-form-builder' ), |
| 165 | 'all_items' => __( 'Forms', 'jet-form-builder' ), |
| 166 | 'add_new' => __( 'Add New', 'jet-form-builder' ), |
| 167 | 'add_new_item' => __( 'Add New Form', 'jet-form-builder' ), |
| 168 | 'edit_item' => __( 'Edit Form', 'jet-form-builder' ), |
| 169 | 'new_item' => __( 'New Form', 'jet-form-builder' ), |
| 170 | 'view_item' => __( 'View Form', 'jet-form-builder' ), |
| 171 | 'search_items' => __( 'Search Form', 'jet-form-builder' ), |
| 172 | 'not_found' => __( 'No Forms Found', 'jet-form-builder' ), |
| 173 | 'not_found_in_trash' => __( 'No Forms Found In Trash', 'jet-form-builder' ), |
| 174 | 'singular_name' => __( 'JetForm', 'jet-form-builder' ), |
| 175 | 'menu_name' => __( 'JetFormBuilder', 'jet-form-builder' ), |
| 176 | ), |
| 177 | 'public' => true, |
| 178 | 'show_ui' => true, |
| 179 | 'show_in_admin_bar' => true, |
| 180 | 'show_in_menu' => true, |
| 181 | 'show_in_nav_menus' => false, |
| 182 | 'show_in_rest' => true, |
| 183 | 'publicly_queryable' => false, |
| 184 | 'exclude_from_search' => true, |
| 185 | 'has_archive' => false, |
| 186 | 'query_var' => false, |
| 187 | 'can_export' => true, |
| 188 | 'rewrite' => false, |
| 189 | 'capability_type' => 'jet_fb_form', |
| 190 | 'menu_icon' => $this->get_post_type_icon(), |
| 191 | 'menu_position' => 120, |
| 192 | 'supports' => array( 'title', 'editor', 'custom-fields' ), |
| 193 | ); |
| 194 | |
| 195 | $post_type = register_post_type( |
| 196 | $this->slug(), |
| 197 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 198 | apply_filters( 'jet-form-builder/post-type/args', $args ) |
| 199 | ); |
| 200 | |
| 201 | $this->rep_install(); |
| 202 | |
| 203 | /** @var Base_Meta_Type $item */ |
| 204 | foreach ( $this->rep_get_items() as $item ) { |
| 205 | register_post_meta( $this->slug(), $item->get_id(), $item->to_array() ); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | public function add_admin_capabilities() { |
| 210 | $role = get_role( 'administrator' ); |
| 211 | |
| 212 | foreach ( $this->generate_capabilities() as $capability ) { |
| 213 | $role->add_cap( $capability ); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | public function remove_admin_capabilities() { |
| 218 | $role = get_role( 'administrator' ); |
| 219 | |
| 220 | foreach ( $this->generate_capabilities() as $capability ) { |
| 221 | $role->remove_cap( $capability ); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | private function generate_capabilities(): \Generator { |
| 226 | list( $singular, $plural ) = array( 'jet_fb_form', 'jet_fb_forms' ); |
| 227 | |
| 228 | $caps = array( |
| 229 | 'edit_' . $singular, |
| 230 | 'read_' . $singular, |
| 231 | 'delete_' . $singular, |
| 232 | 'edit_' . $plural, |
| 233 | 'edit_others_' . $plural, |
| 234 | 'delete_' . $plural, |
| 235 | 'publish_' . $plural, |
| 236 | 'read_private_' . $plural, |
| 237 | ); |
| 238 | |
| 239 | foreach ( $caps as $cap ) { |
| 240 | yield $cap; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @param string $name |
| 246 | * |
| 247 | * @return false|Base_Meta_Type |
| 248 | */ |
| 249 | public function get_meta( string $name ) { |
| 250 | try { |
| 251 | return $this->rep_get_item( $name ); |
| 252 | } catch ( Repository_Exception $exception ) { |
| 253 | return false; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @param $item |
| 259 | * |
| 260 | * @throws Exceptions\Repository_Exception |
| 261 | */ |
| 262 | public function rep_before_install_item( $item ) { |
| 263 | if ( $item->is_supported() ) { |
| 264 | return; |
| 265 | } |
| 266 | $this->_rep_abort_this(); |
| 267 | } |
| 268 | |
| 269 | private function get_post_type_icon() { |
| 270 | $path = $this->get_icon_path( 'post-type.php' ); |
| 271 | |
| 272 | return include_once $path; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Returns form meta arguments: |
| 277 | * fields_layout, submit_type, captcha and required_mark |
| 278 | * in assoc array |
| 279 | * |
| 280 | * @param int|false $form_id |
| 281 | * |
| 282 | * @return array |
| 283 | */ |
| 284 | public function get_args( $form_id = false ): array { |
| 285 | return $this->get_meta( Args_Meta::class )->query( $form_id ); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Returns form messages |
| 290 | * |
| 291 | * @param int|false $form_id |
| 292 | * |
| 293 | * @return array |
| 294 | */ |
| 295 | public function get_messages( $form_id = false ) { |
| 296 | return $this->get_meta( Messages_Meta::class )->query( $form_id ); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Returns form actions |
| 301 | * |
| 302 | * @param int|false $form_id |
| 303 | * |
| 304 | * @return array |
| 305 | */ |
| 306 | public function get_actions( $form_id = false ) { |
| 307 | return $this->get_meta( Actions_Meta::class )->query( $form_id ); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Returns form actions |
| 312 | * |
| 313 | * @param int|false $form_id |
| 314 | * |
| 315 | * @return array |
| 316 | */ |
| 317 | public function get_preset( $form_id = false ) { |
| 318 | return $this->get_meta( Preset_Meta::class )->query( $form_id ); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Returns captcha settings |
| 323 | * |
| 324 | * @param int|false $form_id |
| 325 | * |
| 326 | * @return array |
| 327 | */ |
| 328 | public function get_captcha( $form_id = false ) { |
| 329 | return $this->get_meta( Recaptcha_Meta::class )->query( $form_id ); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Returns form gateways |
| 334 | * |
| 335 | * @param int|false $form_id |
| 336 | * |
| 337 | * @return array |
| 338 | */ |
| 339 | public function get_gateways( $form_id = false ): array { |
| 340 | try { |
| 341 | return $this->rep_get_item( Gateways_Meta::class )->query( $form_id ); |
| 342 | } catch ( Repository_Exception $exception ) { |
| 343 | return array(); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | public function get_validation( $form_id = false ) { |
| 348 | return $this->get_meta( Validation_Meta::class )->query( $form_id ); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * @param $meta_key |
| 353 | * @param int|false $form_id |
| 354 | * |
| 355 | * @return array|mixed |
| 356 | * @deprecated since 3.0.0 |
| 357 | */ |
| 358 | public function get_form_meta( $meta_key, $form_id = false ) { |
| 359 | if ( false === $form_id ) { |
| 360 | $form_id = jet_fb_live()->form_id; |
| 361 | } |
| 362 | |
| 363 | return Tools::decode_json( |
| 364 | get_post_meta( |
| 365 | $form_id, |
| 366 | $meta_key, |
| 367 | true |
| 368 | ) |
| 369 | ); |
| 370 | } |
| 371 | |
| 372 | public function maybe_get_jet_sm_ready_styles( $form_id ) { |
| 373 | return Compatibility::has_jet_sm() ? get_post_meta( $form_id, '_jet_sm_ready_style', true ) : ''; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Returns captcha settings |
| 378 | * |
| 379 | * @deprecated 3.1.0 Use ::get_captcha() instead |
| 380 | * |
| 381 | * @param int|false $form_id |
| 382 | * |
| 383 | * @return array |
| 384 | */ |
| 385 | public function get_recaptcha( $form_id = false ) { |
| 386 | _deprecated_function( __METHOD__, '3.1.0', __CLASS__ . '::get_captcha()' ); |
| 387 | |
| 388 | return $this->get_captcha( $form_id ); |
| 389 | } |
| 390 | |
| 391 | |
| 392 | } |
| 393 |