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