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