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