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