actions
4 years ago
addons
4 years ago
admin
4 years ago
blocks
4 years ago
classes
4 years ago
compatibility
4 years ago
db-queries
4 years ago
dev-mode
4 years ago
exceptions
4 years ago
form-actions
4 years ago
form-messages
4 years ago
form-patterns
4 years ago
form-response
4 years ago
gateways
4 years ago
generators
4 years ago
integrations
4 years ago
presets
4 years ago
request
4 years ago
rest-api
4 years ago
shortcodes
4 years ago
widgets
4 years ago
autoloader.php
4 years ago
file-upload.php
4 years ago
form-break.php
4 years ago
form-handler.php
4 years ago
form-manager.php
4 years ago
live-form.php
4 years ago
plugin.php
4 years ago
post-type.php
4 years ago
post-type.php
377 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder; |
| 4 | |
| 5 | use Jet_Form_Builder\Classes\Compatibility; |
| 6 | use Jet_Form_Builder\Classes\Get_Icon_Trait; |
| 7 | use Jet_Form_Builder\Classes\Messages_Helper_Trait; |
| 8 | use Jet_Form_Builder\Classes\Tools; |
| 9 | use Jet_Form_Builder\Shortcodes\Manager; |
| 10 | |
| 11 | // If this file is called directly, abort. |
| 12 | |
| 13 | if ( ! defined( 'WPINC' ) ) { |
| 14 | die; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Post_Type class |
| 19 | */ |
| 20 | class Post_Type { |
| 21 | |
| 22 | use Messages_Helper_Trait; |
| 23 | use Get_Icon_Trait; |
| 24 | |
| 25 | public $allow_gateways; |
| 26 | |
| 27 | /** |
| 28 | * Used to define the editor |
| 29 | * |
| 30 | * @var boolean |
| 31 | */ |
| 32 | public $is_form_editor; |
| 33 | |
| 34 | public $screen; |
| 35 | |
| 36 | /** |
| 37 | * Constructor for the class |
| 38 | */ |
| 39 | public function __construct() { |
| 40 | add_action( 'init', array( $this, 'register_post_type' ) ); |
| 41 | add_action( 'current_screen', array( $this, 'set_current_screen' ) ); |
| 42 | |
| 43 | add_filter( "manage_{$this->slug()}_posts_columns", array( $this, 'filter_columns' ) ); |
| 44 | add_action( "manage_{$this->slug()}_posts_custom_column", array( $this, 'add_admin_column_content' ), 10, 2 ); |
| 45 | } |
| 46 | |
| 47 | public function filter_columns( $columns ) { |
| 48 | $after = array( 'date' => $columns['date'] ); |
| 49 | unset( $columns['date'] ); |
| 50 | |
| 51 | $columns['jfb_shortcode'] = __( 'Shortcode', 'jet-form-builder' ); |
| 52 | |
| 53 | return array_merge( $columns, $after ); |
| 54 | } |
| 55 | |
| 56 | public function add_admin_column_content( $column, $form_id ) { |
| 57 | if ( 'jfb_shortcode' !== $column ) { |
| 58 | return; |
| 59 | } |
| 60 | $arguments = json_decode( get_post_meta( $form_id, '_jf_args', true ), true ); |
| 61 | $arguments = array_diff( $arguments, $this->get_default_args() ); |
| 62 | $arguments = array_merge( array( 'form_id' => $form_id ), $this->get_default_args_on_render(), $arguments ); |
| 63 | |
| 64 | printf( |
| 65 | '<input readonly type="text" onclick="this.select()" value="%s" style="%s"/>', |
| 66 | esc_attr( Manager::get_shortcode( 'jet_fb_form', $arguments ) ), |
| 67 | 'width: 100%' |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Returns current post type slug |
| 74 | * |
| 75 | * @return string [type] [description] |
| 76 | */ |
| 77 | public function slug() { |
| 78 | return 'jet-form-builder'; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | public function set_current_screen() { |
| 83 | $this->screen = get_current_screen(); |
| 84 | |
| 85 | if ( ! $this->screen->action ) { |
| 86 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 87 | $this->screen->action = ! empty( $_GET['action'] ) ? sanitize_key( $_GET['action'] ) : ''; |
| 88 | } |
| 89 | |
| 90 | $is_editor_page = in_array( $this->screen->action, array( 'add', 'edit' ), true ); |
| 91 | |
| 92 | if ( $this->slug() === $this->screen->id && $is_editor_page ) { |
| 93 | $this->is_form_editor = true; |
| 94 | |
| 95 | } elseif ( $this->slug() !== $this->screen->id && $is_editor_page ) { |
| 96 | $this->is_form_editor = false; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public function is_form_list_page() { |
| 101 | return ( "edit-{$this->slug()}" === $this->screen->id ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Register templates post type |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | public function register_post_type() { |
| 110 | |
| 111 | $args = array( |
| 112 | 'labels' => array( |
| 113 | 'name' => __( 'Forms', 'jet-form-builder' ), |
| 114 | 'all_items' => __( 'Forms', 'jet-form-builder' ), |
| 115 | 'add_new' => __( 'Add New', 'jet-form-builder' ), |
| 116 | 'add_new_item' => __( 'Add New Form', 'jet-form-builder' ), |
| 117 | 'edit_item' => __( 'Edit Form', 'jet-form-builder' ), |
| 118 | 'new_item' => __( 'New Form', 'jet-form-builder' ), |
| 119 | 'view_item' => __( 'View Form', 'jet-form-builder' ), |
| 120 | 'search_items' => __( 'Search Form', 'jet-form-builder' ), |
| 121 | 'not_found' => __( 'No Forms Found', 'jet-form-builder' ), |
| 122 | 'not_found_in_trash' => __( 'No Forms Found In Trash', 'jet-form-builder' ), |
| 123 | 'singular_name' => __( 'JetForm', 'jet-form-builder' ), |
| 124 | 'menu_name' => __( 'JetFormBuilder', 'jet-form-builder' ), |
| 125 | ), |
| 126 | 'public' => true, |
| 127 | 'show_ui' => true, |
| 128 | 'show_in_admin_bar' => true, |
| 129 | 'show_in_menu' => true, |
| 130 | 'show_in_nav_menus' => false, |
| 131 | 'show_in_rest' => true, |
| 132 | 'publicly_queryable' => false, |
| 133 | 'exclude_from_search' => true, |
| 134 | 'has_archive' => false, |
| 135 | 'query_var' => false, |
| 136 | 'can_export' => true, |
| 137 | 'rewrite' => false, |
| 138 | 'capability_type' => 'post', |
| 139 | 'menu_icon' => $this->get_post_type_icon(), |
| 140 | 'menu_position' => 120, |
| 141 | 'supports' => array( 'title', 'editor', 'custom-fields' ), |
| 142 | ); |
| 143 | |
| 144 | $post_type = register_post_type( |
| 145 | $this->slug(), |
| 146 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 147 | apply_filters( 'jet-form-builder/post-type/args', $args ) |
| 148 | ); |
| 149 | |
| 150 | $this->set_default_messages(); |
| 151 | |
| 152 | $meta = array( |
| 153 | '_jf_args' => array( |
| 154 | 'type' => 'string', |
| 155 | 'default' => wp_json_encode( $this->get_default_args() ), |
| 156 | ), |
| 157 | '_jf_recaptcha' => array( |
| 158 | 'type' => 'string', |
| 159 | 'default' => '{}', |
| 160 | ), |
| 161 | |
| 162 | '_jf_actions' => array( |
| 163 | 'type' => 'string', |
| 164 | 'default' => '[]', |
| 165 | ), |
| 166 | '_jf_messages' => array( |
| 167 | 'type' => 'string', |
| 168 | 'default' => $this->get_default_messages_values_json(), |
| 169 | ), |
| 170 | '_jf_preset' => array( |
| 171 | 'type' => 'string', |
| 172 | 'default' => '{}', |
| 173 | ), |
| 174 | ); |
| 175 | |
| 176 | if ( Plugin::instance()->allow_gateways ) { |
| 177 | $meta['_jf_gateways'] = array( |
| 178 | 'type' => 'string', |
| 179 | 'default' => '{}', |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | foreach ( $meta as $key => $args ) { |
| 184 | |
| 185 | $args = array_merge( |
| 186 | $args, |
| 187 | array( |
| 188 | 'show_in_rest' => true, |
| 189 | 'single' => true, |
| 190 | 'auth_callback' => function ( $res, $key, $post_id, $user_id, $cap ) { |
| 191 | return user_can( $user_id, 'edit_post', $post_id ); |
| 192 | }, |
| 193 | ) |
| 194 | ); |
| 195 | |
| 196 | register_post_meta( $this->slug(), $key, $args ); |
| 197 | |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | |
| 202 | private function get_post_type_icon() { |
| 203 | $path = $this->get_icon_path( 'post-type.php' ); |
| 204 | |
| 205 | return include_once $path; |
| 206 | } |
| 207 | |
| 208 | public function get_form_meta( $meta_key, $form_id ) { |
| 209 | return Tools::decode_json( |
| 210 | get_post_meta( |
| 211 | $form_id, |
| 212 | $meta_key, |
| 213 | true |
| 214 | ) |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Returns form meta arguments: |
| 220 | * fields_layout, submit_type, captcha and required_mark |
| 221 | * in assoc array |
| 222 | * |
| 223 | * @param $form_id |
| 224 | * |
| 225 | * @return array |
| 226 | */ |
| 227 | public function get_args( $form_id ): array { |
| 228 | $args = $this->get_form_meta( '_jf_args', $form_id ); |
| 229 | |
| 230 | if ( ! is_array( $args ) ) { |
| 231 | return array(); |
| 232 | } |
| 233 | |
| 234 | return $args; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Returns form messages |
| 239 | * |
| 240 | * @param $form_id |
| 241 | * |
| 242 | * @return array |
| 243 | */ |
| 244 | public function get_messages( $form_id ) { |
| 245 | $messages = $this->get_form_meta( '_jf_messages', $form_id ); |
| 246 | |
| 247 | if ( empty( $messages ) ) { |
| 248 | return $this->messages; |
| 249 | } |
| 250 | |
| 251 | return $messages; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Returns form actions |
| 256 | * |
| 257 | * @param $form_id |
| 258 | * |
| 259 | * @return array |
| 260 | */ |
| 261 | public function get_actions( $form_id ) { |
| 262 | return $this->get_form_meta( '_jf_actions', $form_id ); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Returns form actions |
| 267 | * |
| 268 | * @param $form_id |
| 269 | * |
| 270 | * @return array |
| 271 | */ |
| 272 | public function get_preset( $form_id ) { |
| 273 | return $this->get_form_meta( '_jf_preset', $form_id ); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Returns captcha settings |
| 278 | * |
| 279 | * @param $form_id |
| 280 | * |
| 281 | * @return array |
| 282 | */ |
| 283 | public function get_recaptcha( $form_id ) { |
| 284 | return $this->get_form_meta( '_jf_recaptcha', $form_id ); |
| 285 | } |
| 286 | |
| 287 | public function maybe_get_jet_sm_ready_styles( $form_id ) { |
| 288 | return Compatibility::has_jet_sm() ? get_post_meta( $form_id, '_jet_sm_ready_style', true ) : ''; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | /** |
| 293 | * Returns form gateways |
| 294 | * |
| 295 | * @param $form_id |
| 296 | * |
| 297 | * @return array |
| 298 | */ |
| 299 | public function get_gateways( $form_id ) { |
| 300 | return $this->get_form_meta( '_jf_gateways', $form_id ); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | public function get_default_args() { |
| 305 | return array( |
| 306 | 'form_id' => '', |
| 307 | 'submit_type' => '', |
| 308 | 'required_mark' => '', |
| 309 | 'fields_layout' => '', |
| 310 | 'enable_progress' => null, |
| 311 | 'fields_label_tag' => '', |
| 312 | 'load_nonce' => '', |
| 313 | ); |
| 314 | } |
| 315 | |
| 316 | public function get_default_args_on_render() { |
| 317 | return array( |
| 318 | 'submit_type' => 'reload', |
| 319 | 'required_mark' => '*', |
| 320 | 'fields_layout' => 'column', |
| 321 | 'enable_progress' => false, |
| 322 | 'fields_label_tag' => 'div', |
| 323 | 'load_nonce' => 'render', |
| 324 | ); |
| 325 | } |
| 326 | |
| 327 | public function set_default_messages() { |
| 328 | $this->messages = apply_filters( |
| 329 | // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 330 | 'jet-form-builder/message-types', |
| 331 | array( |
| 332 | 'success' => array( |
| 333 | 'label' => __( 'Form successfully submitted.', 'jet-form-builder' ), |
| 334 | 'value' => 'Form successfully submitted.', |
| 335 | ), |
| 336 | 'failed' => array( |
| 337 | 'label' => __( 'Submit failed.', 'jet-form-builder' ), |
| 338 | 'value' => 'There was an error trying to submit form. Please try again later.', |
| 339 | ), |
| 340 | 'validation_failed' => array( |
| 341 | 'label' => __( 'Validation error', 'jet-form-builder' ), |
| 342 | 'value' => 'One or more fields have an error. Please check and try again.', |
| 343 | ), |
| 344 | 'captcha_failed' => array( |
| 345 | 'label' => __( 'Captcha validation failed', 'jet-form-builder' ), |
| 346 | 'value' => __( 'Captcha validation failed', 'jet-form-builder' ), |
| 347 | ), |
| 348 | 'invalid_email' => array( |
| 349 | 'label' => __( 'Entered an invalid email', 'jet-form-builder' ), |
| 350 | 'value' => 'The e-mail address entered is invalid.', |
| 351 | ), |
| 352 | 'empty_field' => array( |
| 353 | 'label' => __( 'Required field is empty', 'jet-form-builder' ), |
| 354 | 'value' => 'The field is required.', |
| 355 | ), |
| 356 | 'internal_error' => array( |
| 357 | 'label' => __( 'Internal server error', 'jet-form-builder' ), |
| 358 | 'value' => 'Internal server error. Please try again later.', |
| 359 | ), |
| 360 | 'upload_max_files' => array( |
| 361 | 'label' => __( 'Media Specific: Max files limit', 'jet-form-builder' ), |
| 362 | 'value' => 'Maximum upload files limit is reached.', |
| 363 | ), |
| 364 | 'upload_max_size' => array( |
| 365 | 'label' => __( 'Media Specific: Max size reached', 'jet-form-builder' ), |
| 366 | 'value' => 'Upload max size exceeded.', |
| 367 | ), |
| 368 | 'upload_mime_types' => array( |
| 369 | 'label' => __( 'Media Specific: File type error', 'jet-form-builder' ), |
| 370 | 'value' => 'File type is not allowed.', |
| 371 | ), |
| 372 | ) |
| 373 | ); |
| 374 | } |
| 375 | |
| 376 | } |
| 377 |