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