| @@ -1,980 +1,980 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Importer Abstract Class | |
| 5 | - */ | |
| 6 | -abstract class WeForms_Importer_Abstract { | |
| 7 | - | |
| 8 | - /** | |
| 9 | - * The importer ID | |
| 10 | - * | |
| 11 | - * @var string | |
| 12 | - */ | |
| 13 | - public $id = ''; | |
| 14 | - | |
| 15 | - /** | |
| 16 | - * The importer title | |
| 17 | - * | |
| 18 | - * @var string | |
| 19 | - */ | |
| 20 | - public $title = ''; | |
| 21 | - | |
| 22 | - /** | |
| 23 | - * The shortcode (without bracket) | |
| 24 | - * | |
| 25 | - * @var string | |
| 26 | - */ | |
| 27 | - public $shortcode = ''; | |
| 28 | - | |
| 29 | - /** | |
| 30 | - * The ajax action string | |
| 31 | - * | |
| 32 | - * @var string | |
| 33 | - */ | |
| 34 | - protected $action = ''; | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * Conditional stub | |
| 38 | - * | |
| 39 | - * @var array | |
| 40 | - */ | |
| 41 | - public $conditionals = [ | |
| 42 | - 'condition_status' => 'no', | |
| 43 | - 'cond_field' => [], | |
| 44 | - 'cond_operator' => [ '=' ], | |
| 45 | - 'cond_option' => [ '- select -' ], | |
| 46 | - 'cond_logic' => 'all', | |
| 47 | - ]; | |
| 48 | - | |
| 49 | - public function __construct() { | |
| 50 | - add_action( 'admin_notices', [ $this, 'maybe_show_notice' ] ); | |
| 51 | - | |
| 52 | - add_action( 'wp_ajax_weforms_import_xdismiss_' . $this->id, [ $this, 'dismiss_notice' ] ); | |
| 53 | - add_action( 'wp_ajax_weforms_import_xforms_' . $this->id, [ $this, 'import_forms' ] ); | |
| 54 | - add_action( 'wp_ajax_weforms_import_xreplace_' . $this->id, [ $this, 'replace_action' ] ); | |
| 55 | - } | |
| 56 | - | |
| 57 | - /** | |
| 58 | - * Get the importer name | |
| 59 | - * | |
| 60 | - * @return string | |
| 61 | - */ | |
| 62 | - public function get_importer_name() { | |
| 63 | - return $this->title; | |
| 64 | - } | |
| 65 | - | |
| 66 | - /** | |
| 67 | - * Get the shortcode ready for regular expression | |
| 68 | - * | |
| 69 | - * @return string | |
| 70 | - */ | |
| 71 | - public function get_shortcode( $regex = false ) { | |
| 72 | - if ( $regex ) { | |
| 73 | - return preg_quote( $this->shortcode ); | |
| 74 | - } | |
| 75 | - | |
| 76 | - return $this->shortcode; | |
| 77 | - } | |
| 78 | - | |
| 79 | - /** | |
| 80 | - * Get all the forms | |
| 81 | - * | |
| 82 | - * @return array | |
| 83 | - */ | |
| 84 | - abstract protected function get_forms(); | |
| 85 | - | |
| 86 | - /** | |
| 87 | - * Get the form name | |
| 88 | - * | |
| 89 | - * @param mixed $form | |
| 90 | - * | |
| 91 | - * @return string | |
| 92 | - */ | |
| 93 | - abstract protected function get_form_name( $form ); | |
| 94 | - | |
| 95 | - /** | |
| 96 | - * Get the form id | |
| 97 | - * | |
| 98 | - * @param mixed $form | |
| 99 | - * | |
| 100 | - * @return int | |
| 101 | - */ | |
| 102 | - abstract protected function get_form_id( $form ); | |
| 103 | - | |
| 104 | - /** | |
| 105 | - * Get all form fields of a form | |
| 106 | - * | |
| 107 | - * @param mixed $form | |
| 108 | - * | |
| 109 | - * @return array | |
| 110 | - */ | |
| 111 | - abstract protected function get_form_fields( $form ); | |
| 112 | - | |
| 113 | - /** | |
| 114 | - * Get form settings of a form | |
| 115 | - * | |
| 116 | - * @param mixed $form | |
| 117 | - * | |
| 118 | - * @return array | |
| 119 | - */ | |
| 120 | - abstract protected function get_form_settings( $form ); | |
| 121 | - | |
| 122 | - /** | |
| 123 | - * Get form notifications of a form | |
| 124 | - * | |
| 125 | - * @param mixed $form | |
| 126 | - * | |
| 127 | - * @return array | |
| 128 | - */ | |
| 129 | - abstract protected function get_form_notifications( $form ); | |
| 130 | - | |
| 131 | - /** | |
| 132 | - * Check if the plugin exists | |
| 133 | - * | |
| 134 | - * @return bool | |
| 135 | - */ | |
| 136 | - abstract protected function plugin_exists(); | |
| 137 | - | |
| 138 | - /** | |
| 139 | - * Show notice if the plugin found | |
| 140 | - * | |
| 141 | - * @return void | |
| 142 | - */ | |
| 143 | - public function maybe_show_notice() { | |
| 144 | - if ( !$this->plugin_exists() ) { | |
| 145 | - return; | |
| 146 | - } | |
| 147 | - | |
| 148 | - if ( $this->is_dimissed() || !current_user_can( 'manage_options' ) ) { | |
| 149 | - return; | |
| 150 | - } ?> | |
| 151 | - <div class="notice notice-info"> | |
| 152 | - <p><strong><?php printf( esc_html_e( '%s Detected', 'weforms' ), wp_kses_post( $this->get_importer_name() ) ); ?></strong></p> | |
| 153 | - <p><?php printf( wp_kses_post( 'Hey, looks like you have <strong>%s</strong> installed. Would you like to <strong>import</strong> the forms into weForms?', 'weforms' ), wp_kses_post ( $this->get_importer_name() ) ); ?></p> | |
| 154 | - | |
| 155 | - <p> | |
| 156 | - <a href="#" class="button button-primary weforms-import-<?php echo esc_attr( $this->id );?>" id="weforms-import-<?php echo esc_attr( $this->id );?>"><?php esc_html_e( 'Import Forms', 'weforms' ); ?></a> | |
| 157 | - <a href="#" class="button weforms-import-<?php echo esc_attr( $this->id ) ;?>" id="weforms-dimiss-<?php echo esc_attr( $this->id );?>"><?php esc_html_e( 'No Thanks', 'weforms' ); ?></a> | |
| 158 | - </p> | |
| 159 | - </div> | |
| 160 | - | |
| 161 | - <script type="text/javascript"> | |
| 162 | - jQuery(function($) { | |
| 163 | - $('.notice').on('click', 'a#weforms-import-<?php echo esc_attr( $this->id );?>', function(e) { | |
| 164 | - e.preventDefault(); | |
| 165 | - | |
| 166 | - var self = $(this); | |
| 167 | - self.addClass('updating-message'); | |
| 168 | - wp.ajax.send( 'weforms_import_xforms_<?php echo esc_attr( $this->id );?>', { | |
| 169 | - data: { | |
| 170 | - type: self.data('type'), | |
| 171 | - _wpnonce: '<?php echo esc_attr( wp_create_nonce( 'weforms' ) ); ?>' | |
| 172 | - }, | |
| 173 | - | |
| 174 | - success: function(response) { | |
| 175 | - var html = '<p><strong>' + response.title + '</strong></p>' + | |
| 176 | - '<p>' + response.message + '</p>' + | |
| 177 | - '<p>' + response.action + '</p>'; | |
| 178 | - | |
| 179 | - html += '<ul>'; | |
| 180 | - _.each(response.refs, function(el, index) { | |
| 181 | - html += '<li><a target="_blank" href="admin.php?page=weforms#/form/' + el.weforms_id + '/edit">' + el.title + '</a> - <a href="admin.php?page=weforms#/form/' + el.weforms_id + '/edit" target="_blank" class="button button-small"><span class="dashicons dashicons-external"></span> Edit</a></li>'; | |
| 182 | - }); | |
| 183 | - | |
| 184 | - html += '</ul>'; | |
| 185 | - | |
| 186 | - html += '<p>' + '<a href="#" class="button button-primary weforms-<?php echo esc_attr( $this->id );?>-replace-action" data-type="replace"><?php esc_html_e( 'Replace Shortcodes', 'weforms' ); ?></a> ' + | |
| 187 | - '<a href="#" class="button weforms-<?php echo esc_attr( $this->id );?>-replace-action" data-type="skip"><?php esc_html_e( 'No Thanks', 'weforms' ); ?></a></p>'; | |
| 188 | - | |
| 189 | - self.closest('.notice').removeClass('notice-info').addClass('notice-success').html( html ); | |
| 190 | - }, | |
| 191 | - | |
| 192 | - error: function(error) { | |
| 193 | - var html = '<p><strong>' + error.title + '</strong></p>' + | |
| 194 | - '<p>' + error.message + '</p>'; | |
| 195 | - | |
| 196 | - self.closest('.notice').removeClass('notice-info').addClass('notice-error').html( html ); | |
| 197 | - }, | |
| 198 | - | |
| 199 | - complete: function() { | |
| 200 | - self.removeClass('updating-message'); | |
| 201 | - } | |
| 202 | - }); | |
| 203 | - }); | |
| 204 | - | |
| 205 | - $('.notice').on('click', '#weforms-dimiss-<?php echo esc_attr( $this->id ) ;?>', function(e) { | |
| 206 | - e.preventDefault(); | |
| 207 | - | |
| 208 | - $(this).closest('.notice').remove(); | |
| 209 | - wp.ajax.send('weforms_import_xdismiss_<?php echo esc_attr( $this->id );?>'); | |
| 210 | - }); | |
| 211 | - | |
| 212 | - $('.notice').on('click', 'a.weforms-<?php echo esc_attr( $this->id );?>-replace-action', function(e) { | |
| 213 | - e.preventDefault(); | |
| 214 | - | |
| 215 | - var self = $(this); | |
| 216 | - var notice = self.closest('.notice'); | |
| 217 | - | |
| 218 | - self.addClass('updating-message'); | |
| 219 | - | |
| 220 | - wp.ajax.send( 'weforms_import_xreplace_<?php echo esc_attr( $this->id ) ;?>', { | |
| 221 | - data: { | |
| 222 | - type: self.data('type'), | |
| 223 | - _wpnonce: '<?php echo esc_attr ( wp_create_nonce( 'weforms' ) ); ?>' | |
| 224 | - }, | |
| 225 | - | |
| 226 | - success: function(response) { | |
| 227 | - notice.remove(); | |
| 228 | - | |
| 229 | - if ( 'replace' === self.data('type') ) { | |
| 230 | - alert( response ); | |
| 231 | - } | |
| 232 | - }, | |
| 233 | - | |
| 234 | - error: function(error) { | |
| 235 | - notice.remove(); | |
| 236 | - alert( error ); | |
| 237 | - }, | |
| 238 | - | |
| 239 | - complete: function() { | |
| 240 | - self.removeClass('updating-message'); | |
| 241 | - } | |
| 242 | - }); | |
| 243 | - | |
| 244 | - }); | |
| 245 | - }); | |
| 246 | - </script> | |
| 247 | - <?php | |
| 248 | - } | |
| 249 | - | |
| 250 | - /** | |
| 251 | - * Import forms | |
| 252 | - * | |
| 253 | - * @return void | |
| 254 | - */ | |
| 255 | - public function import_forms() { | |
| 256 | - check_ajax_referer( 'weforms' ); | |
| 257 | - | |
| 258 | - $this->check_caps(); | |
| 259 | - | |
| 260 | - // check if plugin installed | |
| 261 | - if ( !$this->plugin_exists() ) { | |
| 262 | - wp_send_json_error( [ | |
| 263 | - 'title' => __( 'Uh oh!', 'weforms' ), | |
| 264 | - 'message' => sprintf( __( '%s is not installed.', 'weforms' ), $this->get_importer_name() ), | |
| 265 | - ] ); | |
| 266 | - } | |
| 267 | - | |
| 268 | - $imported = 0; | |
| 269 | - $refs = []; | |
| 270 | - $forms = $this->get_forms(); | |
| 271 | - | |
| 272 | - if ( !$forms ) { | |
| 273 | - wp_send_json_error( [ | |
| 274 | - 'title' => __( 'Uh oh!', 'weforms' ), | |
| 275 | - 'message' => __( 'No forms found!', 'weforms' ), | |
| 276 | - ] ); | |
| 277 | - | |
| 278 | - $this->dismiss_prompt(); | |
| 279 | - } | |
| 280 | - | |
| 281 | - if ( $forms ) { | |
| 282 | - foreach ( $forms as $form ) { | |
| 283 | - $form_name = $this->get_form_name( $form ); | |
| 284 | - $form_fields = $this->get_form_fields( $form ); | |
| 285 | - $settings = $this->get_form_settings( $form ); | |
| 286 | - $notifications = $this->get_form_notifications( $form ); | |
| 287 | - | |
| 288 | - if ( $form_fields ) { | |
| 289 | - $form_id = $this->insert_form( $form_name ); | |
| 290 | - | |
| 291 | - if ( is_wp_error( $form_id ) ) { | |
| 292 | - continue; | |
| 293 | - } | |
| 294 | - | |
| 295 | - foreach ( $form_fields as $menu_order => $form_field ) { | |
| 296 | - $this->insert_form_field( $form_field, $form_id, $menu_order ); | |
| 297 | - } | |
| 298 | - | |
| 299 | - $this->update_settings( $form_id, $settings ); | |
| 300 | - $this->update_notification( $form_id, $notifications ); | |
| 301 | - | |
| 302 | - $imported++; | |
| 303 | - | |
| 304 | - $refs[ $this->get_form_id( $form ) ] = [ | |
| 305 | - 'imported_id' => $this->get_form_id( $form ), | |
| 306 | - 'weforms_id' => $form_id, | |
| 307 | - 'title' => $form_name, | |
| 308 | - ]; | |
| 309 | - } | |
| 310 | - } | |
| 311 | - } | |
| 312 | - | |
| 313 | - $this->dismiss_prompt(); | |
| 314 | - $this->update_refs( $refs ); | |
| 315 | - | |
| 316 | - wp_send_json_success( [ | |
| 317 | - 'title' => sprintf( _n( '%s form imported', '%s forms imported', $imported, 'weforms' ), $imported ), | |
| 318 | - 'message' => __( 'We have successfully imported these forms into weForms. You could check and edit in-case anything weird happended.', 'weforms' ), | |
| 319 | - 'action' => sprintf( __( 'Do you want to <strong>replace</strong> %s shortcodes with weForms?', 'weforms' ), $this->get_importer_name() ), | |
| 320 | - 'refs' => $refs, | |
| 321 | - ] ); | |
| 322 | - } | |
| 323 | - | |
| 324 | - /** | |
| 325 | - * Replace contact form 7 shortcodes | |
| 326 | - * | |
| 327 | - * @return void | |
| 328 | - */ | |
| 329 | - public function replace_action() { | |
| 330 | - check_ajax_referer( 'weforms' ); | |
| 331 | - | |
| 332 | - $this->check_caps(); | |
| 333 | - | |
| 334 | - if ( ! in_array( isset( $_POST['type'] ), array( 'skip', 'replace' ) ) ) { | |
| 335 | - wp_send_json_error( __( 'Not a valid action type.', 'weforms' ) ); | |
| 336 | - } | |
| 337 | - | |
| 338 | - if ( 'skip' == $_POST['type'] ) { | |
| 339 | - wp_send_json_success(); | |
| 340 | - } | |
| 341 | - | |
| 342 | - $pages_query = new WP_Query( [ | |
| 343 | - 'post_type' => 'page', | |
| 344 | - 'posts_per_page' => -1, | |
| 345 | - 's' => '[' . $this->get_shortcode(), | |
| 346 | - ] ); | |
| 347 | - | |
| 348 | - if ( !$pages_query->found_posts ) { | |
| 349 | - wp_send_json_error( __( 'No pages found with shortcode. Skipped!', 'weforms' ) ); | |
| 350 | - } | |
| 351 | - | |
| 352 | - $count = 0; | |
| 353 | - $refs = $this->get_refs(); | |
| 354 | - $pages = $pages_query->get_posts(); | |
| 355 | - | |
| 356 | - foreach ( $pages as $page ) { | |
| 357 | - preg_match_all( '/\[(\[?)(' . $this->get_shortcode( true ) . '|)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)/s', $page->post_content, $matches, PREG_SET_ORDER ); | |
| 358 | - | |
| 359 | - if ( empty( $matches ) ) { | |
| 360 | - continue; | |
| 361 | - } | |
| 362 | - | |
| 363 | - foreach ( $matches as $shortcode ) { | |
| 364 | - $atts = shortcode_parse_atts( $shortcode[0] ); | |
| 365 | - | |
| 366 | - if ( isset( $atts['id'] ) && array_key_exists( $atts['id'], $refs ) ) { | |
| 367 | - $replace = sprintf( '[weforms id="%d"]', $refs[ $atts['id'] ]['weforms_id'] ); | |
| 368 | - | |
| 369 | - $post_content = str_replace( $shortcode[0], $replace, $page->post_content ); | |
| 370 | - | |
| 371 | - wp_update_post( [ | |
| 372 | - 'ID' => $page->ID, | |
| 373 | - 'post_content' => $post_content, | |
| 374 | - ] ); | |
| 375 | - | |
| 376 | - $count++; | |
| 377 | - } | |
| 378 | - } | |
| 379 | - } | |
| 380 | - | |
| 381 | - wp_send_json_success( sprintf( _n( 'Replaced %d form', 'Replaced %d forms', $count, 'weforms' ), $count ) ); | |
| 382 | - } | |
| 383 | - | |
| 384 | - /** | |
| 385 | - * Check capability if able to process | |
| 386 | - * | |
| 387 | - * @return void | |
| 388 | - */ | |
| 389 | - public function check_caps() { | |
| 390 | - if ( !current_user_can( 'manage_options' ) ) { | |
| 391 | - wp_send_json_error( __( 'You are not allowed.', 'weforms' ) ); | |
| 392 | - } | |
| 393 | - } | |
| 394 | - | |
| 395 | - /** | |
| 396 | - * If the prompt is dismissed | |
| 397 | - * | |
| 398 | - * @return bool | |
| 399 | - */ | |
| 400 | - public function is_dimissed() { | |
| 401 | - return 'yes' == get_option( 'weforms_dismiss_xnotice_' . $this->id ); | |
| 402 | - } | |
| 403 | - | |
| 404 | - /** | |
| 405 | - * Dismiss the prompt | |
| 406 | - * | |
| 407 | - * @return void | |
| 408 | - */ | |
| 409 | - public function dismiss_prompt() { | |
| 410 | - update_option( 'weforms_dismiss_xnotice_' . $this->id, 'yes' ); | |
| 411 | - } | |
| 412 | - | |
| 413 | - /** | |
| 414 | - * Dismiss the notice | |
| 415 | - * | |
| 416 | - * @return void | |
| 417 | - */ | |
| 418 | - public function dismiss_notice() { | |
| 419 | - $this->dismiss_prompt(); | |
| 420 | - | |
| 421 | - wp_send_json_success(); | |
| 422 | - } | |
| 423 | - | |
| 424 | - /** | |
| 425 | - * Get the imported form refs | |
| 426 | - * | |
| 427 | - * @return array | |
| 428 | - */ | |
| 429 | - public function get_refs() { | |
| 430 | - return get_option( 'weforms_imported_xforms_' . $this->id, [] ); | |
| 431 | - } | |
| 432 | - | |
| 433 | - /** | |
| 434 | - * Update the imported reference forms | |
| 435 | - * | |
| 436 | - * @param array $refs | |
| 437 | - * | |
| 438 | - * @return void | |
| 439 | - */ | |
| 440 | - public function update_refs( $refs ) { | |
| 441 | - update_option( 'weforms_imported_xforms_' . $this->id, $refs ); | |
| 442 | - } | |
| 443 | - | |
| 444 | - /** | |
| 445 | - * Get form field | |
| 446 | - * | |
| 447 | - * @param string $type | |
| 448 | - * @param array $args [description] | |
| 449 | - * | |
| 450 | - * @return array | |
| 451 | - */ | |
| 452 | - public function get_form_field( $type, $args = [] ) { | |
| 453 | - $defaults = [ | |
| 454 | - 'required' => 'no', | |
| 455 | - 'label' => '', | |
| 456 | - 'name' => '', | |
| 457 | - 'help' => '', | |
| 458 | - 'css_class' => '', | |
| 459 | - 'placeholder' => '', | |
| 460 | - 'value' => '', | |
| 461 | - 'default' => '', | |
| 462 | - 'options' => [], | |
| 463 | - 'step' => '', | |
| 464 | - 'min' => '', | |
| 465 | - 'max' => '', | |
| 466 | - 'extension' => '', | |
| 467 | - 'max_size' => '', // file size | |
| 468 | - 'size' => '', // file size | |
| 469 | - 'first_placeholder' => '', | |
| 470 | - 'first_default' => '', | |
| 471 | - 'middle_placeholder' => '', | |
| 472 | - 'middle_default' => '', | |
| 473 | - 'last_placeholder' => '', | |
| 474 | - 'last_default' => '', | |
| 475 | - 'first_name' => '', | |
| 476 | - 'middle_name' => '', | |
| 477 | - 'last_name' => '', | |
| 478 | - ]; | |
| 479 | - | |
| 480 | - $args = wp_parse_args( $args, $defaults ); | |
| 481 | - | |
| 482 | - switch ( $type ) { | |
| 483 | - case 'text': | |
| 484 | - $field_content = [ | |
| 485 | - 'input_type' => 'text', | |
| 486 | - 'template' => 'text_field', | |
| 487 | - 'required' => $args['required'], | |
| 488 | - 'label' => $args['label'], | |
| 489 | - 'name' => $args['name'], | |
| 490 | - 'is_meta' => 'yes', | |
| 491 | - 'help' => $args['help'], | |
| 492 | - 'css' => $args['css_class'], | |
| 493 | - 'placeholder' => $args['placeholder'], | |
| 494 | - 'default' => $args['default'], | |
| 495 | - 'size' => $args['size'], | |
| 496 | - 'word_restriction' => '', | |
| 497 | - 'wpuf_cond' => $this->conditionals, | |
| 498 | - ]; | |
| 499 | - break; | |
| 500 | - | |
| 501 | - case 'email': | |
| 502 | - $field_content = [ | |
| 503 | - 'input_type' => 'email', | |
| 504 | - 'template' => 'email_address', | |
| 505 | - 'required' => $args['required'], | |
| 506 | - 'label' => $args['label'], | |
| 507 | - 'name' => $args['name'], | |
| 508 | - 'is_meta' => 'yes', | |
| 509 | - 'help' => $args['help'], | |
| 510 | - 'css' => $args['css_class'], | |
| 511 | - 'placeholder' => $args['placeholder'], | |
| 512 | - 'default' => $args['default'], | |
| 513 | - 'size' => $args['size'], | |
| 514 | - 'word_restriction' => '', | |
| 515 | - 'wpuf_cond' => $this->conditionals, | |
| 516 | - ]; | |
| 517 | - break; | |
| 518 | - | |
| 519 | - case 'textarea': | |
| 520 | - $field_content = [ | |
| 521 | - 'input_type' => 'textarea', | |
| 522 | - 'template' => 'textarea_field', | |
| 523 | - 'required' => $args['required'], | |
| 524 | - 'label' => $args['label'], | |
| 525 | - 'name' => $args['name'], | |
| 526 | - 'is_meta' => 'yes', | |
| 527 | - 'help' => $args['help'], | |
| 528 | - 'css' => $args['css_class'], | |
| 529 | - 'rows' => 5, | |
| 530 | - 'cols' => 25, | |
| 531 | - 'placeholder' => $args['placeholder'], | |
| 532 | - 'default' => $args['default'], | |
| 533 | - 'rich' => 'no', | |
| 534 | - 'word_restriction' => '', | |
| 535 | - 'wpuf_cond' => $this->conditionals, | |
| 536 | - ]; | |
| 537 | - break; | |
| 538 | - | |
| 539 | - case 'select': | |
| 540 | - $field_content = [ | |
| 541 | - 'input_type' => 'select', | |
| 542 | - 'template' => 'dropdown_field', | |
| 543 | - 'required' => $args['required'], | |
| 544 | - 'label' => $args['label'], | |
| 545 | - 'name' => $args['name'], | |
| 546 | - 'is_meta' => 'yes', | |
| 547 | - 'help' => '', | |
| 548 | - 'css' => $args['css_class'], | |
| 549 | - 'selected' => '', | |
| 550 | - 'inline' => 'no', | |
| 551 | - 'options' => $args['options'], | |
| 552 | - 'wpuf_cond' => $this->conditionals, | |
| 553 | - ]; | |
| 554 | - break; | |
| 555 | - | |
| 556 | - case 'multiselect': | |
| 557 | - $field_content = [ | |
| 558 | - 'input_type' => 'multiselect', | |
| 559 | - 'template' => 'multiple_select', | |
| 560 | - 'required' => $args['required'], | |
| 561 | - 'label' => $args['label'], | |
| 562 | - 'name' => $args['name'], | |
| 563 | - 'is_meta' => 'yes', | |
| 564 | - 'help' => '', | |
| 565 | - 'css' => $args['css_class'], | |
| 566 | - 'selected' => '', | |
| 567 | - 'first' => __( '- select -', 'weforms' ), | |
| 568 | - 'options' => $args['options'], | |
| 569 | - 'wpuf_cond' => $this->conditionals, | |
| 570 | - ]; | |
| 571 | - break; | |
| 572 | - | |
| 573 | - case 'date': | |
| 574 | - $field_content = [ | |
| 575 | - 'input_type' => 'date', | |
| 576 | - 'template' => 'date_field', | |
| 577 | - 'required' => $args['required'], | |
| 578 | - 'label' => $args['label'], | |
| 579 | - 'name' => $args['name'], | |
| 580 | - 'is_meta' => 'yes', | |
| 581 | - 'help' => '', | |
| 582 | - 'css' => $args['css_class'], | |
| 583 | - 'format' => 'dd/mm/yy', | |
| 584 | - 'time' => '', | |
| 585 | - 'is_publish_time' => '', | |
| 586 | - 'wpuf_cond' => $this->conditionals, | |
| 587 | - ]; | |
| 588 | - break; | |
| 589 | - | |
| 590 | - case 'range': | |
| 591 | - case 'number': | |
| 592 | - | |
| 593 | - $field_content = [ | |
| 594 | - 'input_type' => 'numeric_text', | |
| 595 | - 'template' => 'numeric_text_field', | |
| 596 | - 'required' => $args['required'], | |
| 597 | - 'label' => $args['label'], | |
| 598 | - 'name' => $args['name'], | |
| 599 | - 'is_meta' => 'yes', | |
| 600 | - 'help' => '', | |
| 601 | - 'css' => $args['css_class'], | |
| 602 | - 'placeholder' => $args['placeholder'], | |
| 603 | - 'default' => $args['value'], | |
| 604 | - 'size' => 40, | |
| 605 | - 'step_text_field' => $args['step'], | |
| 606 | - 'min_value_field' => $args['min'], | |
| 607 | - 'max_value_field' => $args['max'], | |
| 608 | - 'wpuf_cond' => $this->conditionals, | |
| 609 | - ]; | |
| 610 | - | |
| 611 | - break; | |
| 612 | - | |
| 613 | - case 'url': | |
| 614 | - $field_content = [ | |
| 615 | - 'input_type' => 'url', | |
| 616 | - 'template' => 'website_url', | |
| 617 | - 'required' => $args['required'], | |
| 618 | - 'label' => $args['label'], | |
| 619 | - 'name' => $args['name'], | |
| 620 | - 'is_meta' => 'yes', | |
| 621 | - 'help' => '', | |
| 622 | - 'css' => $args['css_class'], | |
| 623 | - 'placeholder' => '', | |
| 624 | - 'default' => '', | |
| 625 | - 'size' => 40, | |
| 626 | - 'word_restriction' => '', | |
| 627 | - 'wpuf_cond' => $this->conditionals, | |
| 628 | - ]; | |
| 629 | - | |
| 630 | - break; | |
| 631 | - | |
| 632 | - case 'checkbox': | |
| 633 | - $field_content = [ | |
| 634 | - 'input_type' => 'checkbox', | |
| 635 | - 'template' => 'checkbox_field', | |
| 636 | - 'required' => $args['required'], | |
| 637 | - 'label' => $args['label'], | |
| 638 | - 'name' => $args['name'], | |
| 639 | - 'is_meta' => 'yes', | |
| 640 | - 'help' => '', | |
| 641 | - 'css' => $args['css_class'], | |
| 642 | - 'selected' => '', | |
| 643 | - 'inline' => 'no', | |
| 644 | - 'options' => $args['options'], | |
| 645 | - 'wpuf_cond' => $this->conditionals, | |
| 646 | - ]; | |
| 647 | - break; | |
| 648 | - | |
| 649 | - case 'radio': | |
| 650 | - $field_content = [ | |
| 651 | - 'input_type' => 'radio', | |
| 652 | - 'template' => 'radio_field', | |
| 653 | - 'required' => $args['required'], | |
| 654 | - 'label' => $args['label'], | |
| 655 | - 'name' => $args['name'], | |
| 656 | - 'is_meta' => 'yes', | |
| 657 | - 'help' => '', | |
| 658 | - 'css' => $args['css_class'], | |
| 659 | - 'selected' => '', | |
| 660 | - 'inline' => 'no', | |
| 661 | - 'options' => $args['options'], | |
| 662 | - 'wpuf_cond' => $this->conditionals, | |
| 663 | - ]; | |
| 664 | - break; | |
| 665 | - | |
| 666 | - case 'hidden': | |
| 667 | - $field_content = [ | |
| 668 | - 'input_type' => 'hidden', | |
| 669 | - 'template' => 'custom_hidden_field', | |
| 670 | - 'label' => $args['label'], | |
| 671 | - 'name' => $args['name'], | |
| 672 | - 'is_meta' => 'yes', | |
| 673 | - 'wpuf_cond' => $this->conditionals, | |
| 674 | - ]; | |
| 675 | - break; | |
| 676 | - | |
| 677 | - case 'section_break': | |
| 678 | - $field_content = [ | |
| 679 | - 'input_type' => 'section_break', | |
| 680 | - 'template' => 'section_break', | |
| 681 | - 'label' => $args['label'], | |
| 682 | - 'name' => $args['name'], | |
| 683 | - 'wpuf_cond' => $this->conditionals, | |
| 684 | - ]; | |
| 685 | - break; | |
| 686 | - | |
| 687 | - case 'html': | |
| 688 | - $field_content = [ | |
| 689 | - 'input_type' => 'html', | |
| 690 | - 'template' => 'custom_html', | |
| 691 | - 'label' => $args['label'], | |
| 692 | - 'name' => $args['name'], | |
| 693 | - 'html' => $args['default'], | |
| 694 | - 'wpuf_cond' => $this->conditionals, | |
| 695 | - ]; | |
| 696 | - break; | |
| 697 | - | |
| 698 | - case 'toc': | |
| 699 | - $field_content = [ | |
| 700 | - 'input_type' => 'toc', | |
| 701 | - 'template' => 'toc', | |
| 702 | - 'required' => $args['required'], | |
| 703 | - 'name' => $args['name'], | |
| 704 | - 'description' => $args['label'], | |
| 705 | - 'is_meta' => 'yes', | |
| 706 | - 'show_checkbox' => true, | |
| 707 | - 'wpuf_cond' => $this->conditionals, | |
| 708 | - ]; | |
| 709 | - break; | |
| 710 | - | |
| 711 | - case 'recaptcha': | |
| 712 | - $field_content = [ | |
| 713 | - 'input_type' => 'recaptcha', | |
| 714 | - 'template' => 'recaptcha', | |
| 715 | - 'required' => $args['required'], | |
| 716 | - 'label' => $args['label'], | |
| 717 | - 'name' => $args['name'], | |
| 718 | - 'recaptcha_type' => 'enable_no_captcha', | |
| 719 | - 'is_meta' => 'yes', | |
| 720 | - 'help' => '', | |
| 721 | - 'css' => $args['css_class'], | |
| 722 | - 'placeholder' => '', | |
| 723 | - 'default' => '', | |
| 724 | - 'size' => 40, | |
| 725 | - 'word_restriction' => '', | |
| 726 | - 'wpuf_cond' => $this->conditionals, | |
| 727 | - ]; | |
| 728 | - break; | |
| 729 | - | |
| 730 | - case 'file': | |
| 731 | - $field_content = [ | |
| 732 | - 'input_type' => 'file_upload', | |
| 733 | - 'template' => 'file_upload', | |
| 734 | - 'required' => $args['required'], | |
| 735 | - 'label' => $args['label'], | |
| 736 | - 'name' => $args['name'], | |
| 737 | - 'is_meta' => 'yes', | |
| 738 | - 'help' => $args['help'], | |
| 739 | - 'css' => $args['css_class'], | |
| 740 | - 'max_size' => $args['max_size'], | |
| 741 | - 'count' => '1', | |
| 742 | - 'extension' => $args['extension'], | |
| 743 | - 'wpuf_cond' => $this->conditionals, | |
| 744 | - ]; | |
| 745 | - break; | |
| 746 | - | |
| 747 | - case 'name': | |
| 748 | - $field_content = [ | |
| 749 | - 'input_type' => 'name', | |
| 750 | - 'template' => 'name_field', | |
| 751 | - 'required' => $args['required'], | |
| 752 | - 'label' => $args['label'], | |
| 753 | - 'name' => $args['name'], | |
| 754 | - 'is_meta' => 'yes', | |
| 755 | - 'format' => $args['format'], | |
| 756 | - 'first_name' => $args['first_name'], | |
| 757 | - 'middle_name' => $args['middle_name'], | |
| 758 | - 'last_name' => $args['last_name'], | |
| 759 | - 'hide_subs' => false, | |
| 760 | - 'help' => $args['help'], | |
| 761 | - 'css' => $args['css_class'], | |
| 762 | - 'wpuf_cond' => $this->conditionals, | |
| 763 | - ]; | |
| 764 | - break; | |
| 765 | - | |
| 766 | - case 'ratings': | |
| 767 | - $field_content = [ | |
| 768 | - 'input_type' => 'ratings', | |
| 769 | - 'template' => 'ratings', | |
| 770 | - 'required' => $args['required'], | |
| 771 | - 'label' => $args['label'], | |
| 772 | - 'name' => $args['name'], | |
| 773 | - 'is_meta' => 'yes', | |
| 774 | - 'help' => '', | |
| 775 | - 'css' => $args['css_class'], | |
| 776 | - 'options' => $args['options'], | |
| 777 | - 'wpuf_cond' => $this->conditionals, | |
| 778 | - ]; | |
| 779 | - break; | |
| 780 | - | |
| 781 | - case 'linear_scale': | |
| 782 | - $field_content = [ | |
| 783 | - 'input_type' => 'linear_scale', | |
| 784 | - 'template' => 'linear_scale', | |
| 785 | - 'required' => $args['required'], | |
| 786 | - 'label' => $args['label'], | |
| 787 | - 'name' => $args['name'], | |
| 788 | - 'is_meta' => 'yes', | |
| 789 | - 'help' => '', | |
| 790 | - 'css' => $args['css_class'], | |
| 791 | - 'options' => $args['options'], | |
| 792 | - 'wpuf_cond' => $this->conditionals, | |
| 793 | - ]; | |
| 794 | - break; | |
| 795 | - | |
| 796 | - case 'checkbox_grid': | |
| 797 | - $field_content = [ | |
| 798 | - 'input_type' => 'checkbox_grid', | |
| 799 | - 'template' => 'checkbox_grid', | |
| 800 | - 'required' => $args['required'], | |
| 801 | - 'label' => $args['label'], | |
| 802 | - 'name' => $args['name'], | |
| 803 | - 'is_meta' => 'yes', | |
| 804 | - 'help' => '', | |
| 805 | - 'css' => $args['css_class'], | |
| 806 | - 'options' => $args['options'], | |
| 807 | - 'wpuf_cond' => $this->conditionals, | |
| 808 | - ]; | |
| 809 | - break; | |
| 810 | - | |
| 811 | - case 'multiple_choice_grid': | |
| 812 | - $field_content = [ | |
| 813 | - 'input_type' => 'multiple_choice_grid', | |
| 814 | - 'template' => 'multiple_choice_grid', | |
| 815 | - 'required' => $args['required'], | |
| 816 | - 'label' => $args['label'], | |
| 817 | - 'name' => $args['name'], | |
| 818 | - 'is_meta' => 'yes', | |
| 819 | - 'help' => '', | |
| 820 | - 'css' => $args['css_class'], | |
| 821 | - 'options' => $args['options'], | |
| 822 | - 'wpuf_cond' => $this->conditionals, | |
| 823 | - ]; | |
| 824 | - break; | |
| 825 | - | |
| 826 | - case 'single_product': | |
| 827 | - $field_content = [ | |
| 828 | - 'input_type' => 'single_product', | |
| 829 | - 'template' => 'single_product', | |
| 830 | - 'required' => $args['required'], | |
| 831 | - 'label' => $args['label'], | |
| 832 | - 'name' => $args['name'], | |
| 833 | - 'is_meta' => 'yes', | |
| 834 | - 'help' => '', | |
| 835 | - 'css' => $args['css_class'], | |
| 836 | - 'options' => $args['options'], | |
| 837 | - 'wpuf_cond' => $this->conditionals, | |
| 838 | - ]; | |
| 839 | - break; | |
| 840 | - | |
| 841 | - case 'multiple_product': | |
| 842 | - $field_content = [ | |
| 843 | - 'input_type' => 'multiple_product', | |
| 844 | - 'template' => 'multiple_product', | |
| 845 | - 'required' => $args['required'], | |
| 846 | - 'label' => $args['label'], | |
| 847 | - 'name' => $args['name'], | |
| 848 | - 'is_meta' => 'yes', | |
| 849 | - 'help' => '', | |
| 850 | - 'css' => $args['css_class'], | |
| 851 | - 'options' => $args['options'], | |
| 852 | - 'wpuf_cond' => $this->conditionals, | |
| 853 | - ]; | |
| 854 | - break; | |
| 855 | - | |
| 856 | - case 'payment_method': | |
| 857 | - $field_content = [ | |
| 858 | - 'input_type' => 'payment_method', | |
| 859 | - 'template' => 'payment_method', | |
| 860 | - 'required' => $args['required'], | |
| 861 | - 'label' => $args['label'], | |
| 862 | - 'name' => $args['name'], | |
| 863 | - 'is_meta' => 'yes', | |
| 864 | - 'help' => '', | |
| 865 | - 'css' => $args['css_class'], | |
| 866 | - 'options' => $args['options'], | |
| 867 | - 'wpuf_cond' => $this->conditionals, | |
| 868 | - ]; | |
| 869 | - break; | |
| 870 | - | |
| 871 | - case 'total': | |
| 872 | - $field_content = [ | |
| 873 | - 'input_type' => 'total', | |
| 874 | - 'template' => 'total', | |
| 875 | - 'required' => $args['required'], | |
| 876 | - 'label' => $args['label'], | |
| 877 | - 'name' => $args['name'], | |
| 878 | - 'is_meta' => 'yes', | |
| 879 | - 'help' => '', | |
| 880 | - 'css' => $args['css_class'], | |
| 881 | - 'options' => $args['options'], | |
| 882 | - 'wpuf_cond' => $this->conditionals, | |
| 883 | - ]; | |
| 884 | - break; | |
| 885 | - } | |
| 886 | - | |
| 887 | - return $field_content; | |
| 888 | - } | |
| 889 | - | |
| 890 | - /** | |
| 891 | - * Default form settings | |
| 892 | - * | |
| 893 | - * @return array | |
| 894 | - */ | |
| 895 | - public function get_default_form_settings() { | |
| 896 | - $form_settings = [ | |
| 897 | - 'redirect_to' => 'same', | |
| 898 | - 'message' => __( 'Thanks for contacting us! We will get in touch with you shortly.', 'weforms' ), | |
| 899 | - 'page_id' => '', | |
| 900 | - 'url' => '', | |
| 901 | - 'submit_text' => __( 'Submit Query', 'weforms' ), | |
| 902 | - 'schedule_form' => 'false', | |
| 903 | - 'schedule_start' => '', | |
| 904 | - 'schedule_end' => '', | |
| 905 | - 'sc_pending_message' => __( 'Form submission hasn\'t been started yet', 'weforms' ), | |
| 906 | - 'sc_expired_message' => __( 'Form submission is now closed.', 'weforms' ), | |
| 907 | - 'require_login' => 'false', | |
| 908 | - 'req_login_message' => __( 'You need to login to submit a query.', 'weforms' ), | |
| 909 | - 'limit_entries' => 'false', | |
| 910 | - 'limit_number' => '1000', | |
| 911 | - 'limit_message' => __( 'Sorry, we have reached the maximum number of submissions.', 'weforms' ), | |
| 912 | - 'label_position' => 'above', | |
| 913 | - ]; | |
| 914 | - | |
| 915 | - return $form_settings; | |
| 916 | - } | |
| 917 | - | |
| 918 | - /** | |
| 919 | - * Insert a form | |
| 920 | - * | |
| 921 | - * @param string $form_name | |
| 922 | - * | |
| 923 | - * @return ID|WP_Error | |
| 924 | - */ | |
| 925 | - public function insert_form( $form_name ) { | |
| 926 | - $weforms_form = [ | |
| 927 | - 'post_title' => sprintf( '[%s] %s', strtoupper( $this->id ), $form_name ), | |
| 928 | - 'post_type' => 'wpuf_contact_form', | |
| 929 | - 'post_status' => 'publish', | |
| 930 | - 'post_author' => get_current_user_id(), | |
| 931 | - ]; | |
| 932 | - | |
| 933 | - return wp_insert_post( $weforms_form ); | |
| 934 | - } | |
| 935 | - | |
| 936 | - /** | |
| 937 | - * Insert a form field | |
| 938 | - * | |
| 939 | - * @param array $field | |
| 940 | - * @param int $form_id | |
| 941 | - * @param int $menu_order | |
| 942 | - * | |
| 943 | - * @return int|WP_Error | |
| 944 | - */ | |
| 945 | - public function insert_form_field( $field, $form_id, $menu_order ) { | |
| 946 | - $form_field = [ | |
| 947 | - 'post_type' => 'wpuf_input', | |
| 948 | - 'post_status' => 'publish', | |
| 949 | - 'post_content' => maybe_serialize( $field ), | |
| 950 | - 'post_parent' => $form_id, | |
| 951 | - 'menu_order' => $menu_order, | |
| 952 | - ]; | |
| 953 | - | |
| 954 | - return wp_insert_post( $form_field ); | |
| 955 | - } | |
| 956 | - | |
| 957 | - /** | |
| 958 | - * Update form settings | |
| 959 | - * | |
| 960 | - * @param int $form_id | |
| 961 | - * @param array $settings | |
| 962 | - * | |
| 963 | - * @return void | |
| 964 | - */ | |
| 965 | - public function update_settings( $form_id, $settings ) { | |
| 966 | - update_post_meta( $form_id, 'wpuf_form_settings', $settings ); | |
| 967 | - } | |
| 968 | - | |
| 969 | - /** | |
| 970 | - * Update Notification | |
| 971 | - * | |
| 972 | - * @param int $form_id | |
| 973 | - * @param array $notification | |
| 974 | - * | |
| 975 | - * @return void | |
| 976 | - */ | |
| 977 | - public function update_notification( $form_id, $notifications ) { | |
| 978 | - update_post_meta( $form_id, 'notifications', $notifications ); | |
| 979 | - } | |
| 980 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Importer Abstract Class | |
| 5 | + */ | |
| 6 | +abstract class WeForms_Importer_Abstract { | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * The importer ID | |
| 10 | + * | |
| 11 | + * @var string | |
| 12 | + */ | |
| 13 | + public $id = ''; | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * The importer title | |
| 17 | + * | |
| 18 | + * @var string | |
| 19 | + */ | |
| 20 | + public $title = ''; | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * The shortcode (without bracket) | |
| 24 | + * | |
| 25 | + * @var string | |
| 26 | + */ | |
| 27 | + public $shortcode = ''; | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * The ajax action string | |
| 31 | + * | |
| 32 | + * @var string | |
| 33 | + */ | |
| 34 | + protected $action = ''; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Conditional stub | |
| 38 | + * | |
| 39 | + * @var array | |
| 40 | + */ | |
| 41 | + public $conditionals = [ | |
| 42 | + 'condition_status' => 'no', | |
| 43 | + 'cond_field' => [], | |
| 44 | + 'cond_operator' => [ '=' ], | |
| 45 | + 'cond_option' => [ '- select -' ], | |
| 46 | + 'cond_logic' => 'all', | |
| 47 | + ]; | |
| 48 | + | |
| 49 | + public function __construct() { | |
| 50 | + add_action( 'admin_notices', [ $this, 'maybe_show_notice' ] ); | |
| 51 | + | |
| 52 | + add_action( 'wp_ajax_weforms_import_xdismiss_' . $this->id, [ $this, 'dismiss_notice' ] ); | |
| 53 | + add_action( 'wp_ajax_weforms_import_xforms_' . $this->id, [ $this, 'import_forms' ] ); | |
| 54 | + add_action( 'wp_ajax_weforms_import_xreplace_' . $this->id, [ $this, 'replace_action' ] ); | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Get the importer name | |
| 59 | + * | |
| 60 | + * @return string | |
| 61 | + */ | |
| 62 | + public function get_importer_name() { | |
| 63 | + return $this->title; | |
| 64 | + } | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * Get the shortcode ready for regular expression | |
| 68 | + * | |
| 69 | + * @return string | |
| 70 | + */ | |
| 71 | + public function get_shortcode( $regex = false ) { | |
| 72 | + if ( $regex ) { | |
| 73 | + return preg_quote( $this->shortcode ); | |
| 74 | + } | |
| 75 | + | |
| 76 | + return $this->shortcode; | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Get all the forms | |
| 81 | + * | |
| 82 | + * @return array | |
| 83 | + */ | |
| 84 | + abstract protected function get_forms(); | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Get the form name | |
| 88 | + * | |
| 89 | + * @param mixed $form | |
| 90 | + * | |
| 91 | + * @return string | |
| 92 | + */ | |
| 93 | + abstract protected function get_form_name( $form ); | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * Get the form id | |
| 97 | + * | |
| 98 | + * @param mixed $form | |
| 99 | + * | |
| 100 | + * @return int | |
| 101 | + */ | |
| 102 | + abstract protected function get_form_id( $form ); | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Get all form fields of a form | |
| 106 | + * | |
| 107 | + * @param mixed $form | |
| 108 | + * | |
| 109 | + * @return array | |
| 110 | + */ | |
| 111 | + abstract protected function get_form_fields( $form ); | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Get form settings of a form | |
| 115 | + * | |
| 116 | + * @param mixed $form | |
| 117 | + * | |
| 118 | + * @return array | |
| 119 | + */ | |
| 120 | + abstract protected function get_form_settings( $form ); | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * Get form notifications of a form | |
| 124 | + * | |
| 125 | + * @param mixed $form | |
| 126 | + * | |
| 127 | + * @return array | |
| 128 | + */ | |
| 129 | + abstract protected function get_form_notifications( $form ); | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * Check if the plugin exists | |
| 133 | + * | |
| 134 | + * @return bool | |
| 135 | + */ | |
| 136 | + abstract protected function plugin_exists(); | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * Show notice if the plugin found | |
| 140 | + * | |
| 141 | + * @return void | |
| 142 | + */ | |
| 143 | + public function maybe_show_notice() { | |
| 144 | + if ( !$this->plugin_exists() ) { | |
| 145 | + return; | |
| 146 | + } | |
| 147 | + | |
| 148 | + if ( $this->is_dimissed() || !current_user_can( 'manage_options' ) ) { | |
| 149 | + return; | |
| 150 | + } ?> | |
| 151 | + <div class="notice notice-info"> | |
| 152 | + <p><strong><?php printf( esc_html_e( '%s Detected', 'weforms' ), wp_kses_post( $this->get_importer_name() ) ); ?></strong></p> | |
| 153 | + <p><?php printf( wp_kses_post( 'Hey, looks like you have <strong>%s</strong> installed. Would you like to <strong>import</strong> the forms into weForms?', 'weforms' ), wp_kses_post ( $this->get_importer_name() ) ); ?></p> | |
| 154 | + | |
| 155 | + <p> | |
| 156 | + <a href="#" class="button button-primary weforms-import-<?php echo esc_attr( $this->id );?>" id="weforms-import-<?php echo esc_attr( $this->id );?>"><?php esc_html_e( 'Import Forms', 'weforms' ); ?></a> | |
| 157 | + <a href="#" class="button weforms-import-<?php echo esc_attr( $this->id ) ;?>" id="weforms-dimiss-<?php echo esc_attr( $this->id );?>"><?php esc_html_e( 'No Thanks', 'weforms' ); ?></a> | |
| 158 | + </p> | |
| 159 | + </div> | |
| 160 | + | |
| 161 | + <script type="text/javascript"> | |
| 162 | + jQuery(function($) { | |
| 163 | + $('.notice').on('click', 'a#weforms-import-<?php echo esc_attr( $this->id );?>', function(e) { | |
| 164 | + e.preventDefault(); | |
| 165 | + | |
| 166 | + var self = $(this); | |
| 167 | + self.addClass('updating-message'); | |
| 168 | + wp.ajax.send( 'weforms_import_xforms_<?php echo esc_attr( $this->id );?>', { | |
| 169 | + data: { | |
| 170 | + type: self.data('type'), | |
| 171 | + _wpnonce: '<?php echo esc_attr( wp_create_nonce( 'weforms' ) ); ?>' | |
| 172 | + }, | |
| 173 | + | |
| 174 | + success: function(response) { | |
| 175 | + var html = '<p><strong>' + response.title + '</strong></p>' + | |
| 176 | + '<p>' + response.message + '</p>' + | |
| 177 | + '<p>' + response.action + '</p>'; | |
| 178 | + | |
| 179 | + html += '<ul>'; | |
| 180 | + _.each(response.refs, function(el, index) { | |
| 181 | + html += '<li><a target="_blank" href="admin.php?page=weforms#/form/' + el.weforms_id + '/edit">' + el.title + '</a> - <a href="admin.php?page=weforms#/form/' + el.weforms_id + '/edit" target="_blank" class="button button-small"><span class="dashicons dashicons-external"></span> Edit</a></li>'; | |
| 182 | + }); | |
| 183 | + | |
| 184 | + html += '</ul>'; | |
| 185 | + | |
| 186 | + html += '<p>' + '<a href="#" class="button button-primary weforms-<?php echo esc_attr( $this->id );?>-replace-action" data-type="replace"><?php esc_html_e( 'Replace Shortcodes', 'weforms' ); ?></a> ' + | |
| 187 | + '<a href="#" class="button weforms-<?php echo esc_attr( $this->id );?>-replace-action" data-type="skip"><?php esc_html_e( 'No Thanks', 'weforms' ); ?></a></p>'; | |
| 188 | + | |
| 189 | + self.closest('.notice').removeClass('notice-info').addClass('notice-success').html( html ); | |
| 190 | + }, | |
| 191 | + | |
| 192 | + error: function(error) { | |
| 193 | + var html = '<p><strong>' + error.title + '</strong></p>' + | |
| 194 | + '<p>' + error.message + '</p>'; | |
| 195 | + | |
| 196 | + self.closest('.notice').removeClass('notice-info').addClass('notice-error').html( html ); | |
| 197 | + }, | |
| 198 | + | |
| 199 | + complete: function() { | |
| 200 | + self.removeClass('updating-message'); | |
| 201 | + } | |
| 202 | + }); | |
| 203 | + }); | |
| 204 | + | |
| 205 | + $('.notice').on('click', '#weforms-dimiss-<?php echo esc_attr( $this->id ) ;?>', function(e) { | |
| 206 | + e.preventDefault(); | |
| 207 | + | |
| 208 | + $(this).closest('.notice').remove(); | |
| 209 | + wp.ajax.send('weforms_import_xdismiss_<?php echo esc_attr( $this->id );?>'); | |
| 210 | + }); | |
| 211 | + | |
| 212 | + $('.notice').on('click', 'a.weforms-<?php echo esc_attr( $this->id );?>-replace-action', function(e) { | |
| 213 | + e.preventDefault(); | |
| 214 | + | |
| 215 | + var self = $(this); | |
| 216 | + var notice = self.closest('.notice'); | |
| 217 | + | |
| 218 | + self.addClass('updating-message'); | |
| 219 | + | |
| 220 | + wp.ajax.send( 'weforms_import_xreplace_<?php echo esc_attr( $this->id ) ;?>', { | |
| 221 | + data: { | |
| 222 | + type: self.data('type'), | |
| 223 | + _wpnonce: '<?php echo esc_attr ( wp_create_nonce( 'weforms' ) ); ?>' | |
| 224 | + }, | |
| 225 | + | |
| 226 | + success: function(response) { | |
| 227 | + notice.remove(); | |
| 228 | + | |
| 229 | + if ( 'replace' === self.data('type') ) { | |
| 230 | + alert( response ); | |
| 231 | + } | |
| 232 | + }, | |
| 233 | + | |
| 234 | + error: function(error) { | |
| 235 | + notice.remove(); | |
| 236 | + alert( error ); | |
| 237 | + }, | |
| 238 | + | |
| 239 | + complete: function() { | |
| 240 | + self.removeClass('updating-message'); | |
| 241 | + } | |
| 242 | + }); | |
| 243 | + | |
| 244 | + }); | |
| 245 | + }); | |
| 246 | + </script> | |
| 247 | + <?php | |
| 248 | + } | |
| 249 | + | |
| 250 | + /** | |
| 251 | + * Import forms | |
| 252 | + * | |
| 253 | + * @return void | |
| 254 | + */ | |
| 255 | + public function import_forms() { | |
| 256 | + check_ajax_referer( 'weforms' ); | |
| 257 | + | |
| 258 | + $this->check_caps(); | |
| 259 | + | |
| 260 | + // check if plugin installed | |
| 261 | + if ( !$this->plugin_exists() ) { | |
| 262 | + wp_send_json_error( [ | |
| 263 | + 'title' => __( 'Uh oh!', 'weforms' ), | |
| 264 | + 'message' => sprintf( __( '%s is not installed.', 'weforms' ), $this->get_importer_name() ), | |
| 265 | + ] ); | |
| 266 | + } | |
| 267 | + | |
| 268 | + $imported = 0; | |
| 269 | + $refs = []; | |
| 270 | + $forms = $this->get_forms(); | |
| 271 | + | |
| 272 | + if ( !$forms ) { | |
| 273 | + wp_send_json_error( [ | |
| 274 | + 'title' => __( 'Uh oh!', 'weforms' ), | |
| 275 | + 'message' => __( 'No forms found!', 'weforms' ), | |
| 276 | + ] ); | |
| 277 | + | |
| 278 | + $this->dismiss_prompt(); | |
| 279 | + } | |
| 280 | + | |
| 281 | + if ( $forms ) { | |
| 282 | + foreach ( $forms as $form ) { | |
| 283 | + $form_name = $this->get_form_name( $form ); | |
| 284 | + $form_fields = $this->get_form_fields( $form ); | |
| 285 | + $settings = $this->get_form_settings( $form ); | |
| 286 | + $notifications = $this->get_form_notifications( $form ); | |
| 287 | + | |
| 288 | + if ( $form_fields ) { | |
| 289 | + $form_id = $this->insert_form( $form_name ); | |
| 290 | + | |
| 291 | + if ( is_wp_error( $form_id ) ) { | |
| 292 | + continue; | |
| 293 | + } | |
| 294 | + | |
| 295 | + foreach ( $form_fields as $menu_order => $form_field ) { | |
| 296 | + $this->insert_form_field( $form_field, $form_id, $menu_order ); | |
| 297 | + } | |
| 298 | + | |
| 299 | + $this->update_settings( $form_id, $settings ); | |
| 300 | + $this->update_notification( $form_id, $notifications ); | |
| 301 | + | |
| 302 | + $imported++; | |
| 303 | + | |
| 304 | + $refs[ $this->get_form_id( $form ) ] = [ | |
| 305 | + 'imported_id' => $this->get_form_id( $form ), | |
| 306 | + 'weforms_id' => $form_id, | |
| 307 | + 'title' => $form_name, | |
| 308 | + ]; | |
| 309 | + } | |
| 310 | + } | |
| 311 | + } | |
| 312 | + | |
| 313 | + $this->dismiss_prompt(); | |
| 314 | + $this->update_refs( $refs ); | |
| 315 | + | |
| 316 | + wp_send_json_success( [ | |
| 317 | + 'title' => sprintf( _n( '%s form imported', '%s forms imported', $imported, 'weforms' ), $imported ), | |
| 318 | + 'message' => __( 'We have successfully imported these forms into weForms. You could check and edit in-case anything weird happended.', 'weforms' ), | |
| 319 | + 'action' => sprintf( __( 'Do you want to <strong>replace</strong> %s shortcodes with weForms?', 'weforms' ), $this->get_importer_name() ), | |
| 320 | + 'refs' => $refs, | |
| 321 | + ] ); | |
| 322 | + } | |
| 323 | + | |
| 324 | + /** | |
| 325 | + * Replace contact form 7 shortcodes | |
| 326 | + * | |
| 327 | + * @return void | |
| 328 | + */ | |
| 329 | + public function replace_action() { | |
| 330 | + check_ajax_referer( 'weforms' ); | |
| 331 | + | |
| 332 | + $this->check_caps(); | |
| 333 | + | |
| 334 | + if ( ! in_array( isset( $_POST['type'] ), array( 'skip', 'replace' ) ) ) { | |
| 335 | + wp_send_json_error( __( 'Not a valid action type.', 'weforms' ) ); | |
| 336 | + } | |
| 337 | + | |
| 338 | + if ( 'skip' == $_POST['type'] ) { | |
| 339 | + wp_send_json_success(); | |
| 340 | + } | |
| 341 | + | |
| 342 | + $pages_query = new WP_Query( [ | |
| 343 | + 'post_type' => 'page', | |
| 344 | + 'posts_per_page' => -1, | |
| 345 | + 's' => '[' . $this->get_shortcode(), | |
| 346 | + ] ); | |
| 347 | + | |
| 348 | + if ( !$pages_query->found_posts ) { | |
| 349 | + wp_send_json_error( __( 'No pages found with shortcode. Skipped!', 'weforms' ) ); | |
| 350 | + } | |
| 351 | + | |
| 352 | + $count = 0; | |
| 353 | + $refs = $this->get_refs(); | |
| 354 | + $pages = $pages_query->get_posts(); | |
| 355 | + | |
| 356 | + foreach ( $pages as $page ) { | |
| 357 | + preg_match_all( '/\[(\[?)(' . $this->get_shortcode( true ) . '|)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)/s', $page->post_content, $matches, PREG_SET_ORDER ); | |
| 358 | + | |
| 359 | + if ( empty( $matches ) ) { | |
| 360 | + continue; | |
| 361 | + } | |
| 362 | + | |
| 363 | + foreach ( $matches as $shortcode ) { | |
| 364 | + $atts = shortcode_parse_atts( $shortcode[0] ); | |
| 365 | + | |
| 366 | + if ( isset( $atts['id'] ) && array_key_exists( $atts['id'], $refs ) ) { | |
| 367 | + $replace = sprintf( '[weforms id="%d"]', $refs[ $atts['id'] ]['weforms_id'] ); | |
| 368 | + | |
| 369 | + $post_content = str_replace( $shortcode[0], $replace, $page->post_content ); | |
| 370 | + | |
| 371 | + wp_update_post( [ | |
| 372 | + 'ID' => $page->ID, | |
| 373 | + 'post_content' => $post_content, | |
| 374 | + ] ); | |
| 375 | + | |
| 376 | + $count++; | |
| 377 | + } | |
| 378 | + } | |
| 379 | + } | |
| 380 | + | |
| 381 | + wp_send_json_success( sprintf( _n( 'Replaced %d form', 'Replaced %d forms', $count, 'weforms' ), $count ) ); | |
| 382 | + } | |
| 383 | + | |
| 384 | + /** | |
| 385 | + * Check capability if able to process | |
| 386 | + * | |
| 387 | + * @return void | |
| 388 | + */ | |
| 389 | + public function check_caps() { | |
| 390 | + if ( !current_user_can( 'manage_options' ) ) { | |
| 391 | + wp_send_json_error( __( 'You are not allowed.', 'weforms' ) ); | |
| 392 | + } | |
| 393 | + } | |
| 394 | + | |
| 395 | + /** | |
| 396 | + * If the prompt is dismissed | |
| 397 | + * | |
| 398 | + * @return bool | |
| 399 | + */ | |
| 400 | + public function is_dimissed() { | |
| 401 | + return 'yes' == get_option( 'weforms_dismiss_xnotice_' . $this->id ); | |
| 402 | + } | |
| 403 | + | |
| 404 | + /** | |
| 405 | + * Dismiss the prompt | |
| 406 | + * | |
| 407 | + * @return void | |
| 408 | + */ | |
| 409 | + public function dismiss_prompt() { | |
| 410 | + update_option( 'weforms_dismiss_xnotice_' . $this->id, 'yes' ); | |
| 411 | + } | |
| 412 | + | |
| 413 | + /** | |
| 414 | + * Dismiss the notice | |
| 415 | + * | |
| 416 | + * @return void | |
| 417 | + */ | |
| 418 | + public function dismiss_notice() { | |
| 419 | + $this->dismiss_prompt(); | |
| 420 | + | |
| 421 | + wp_send_json_success(); | |
| 422 | + } | |
| 423 | + | |
| 424 | + /** | |
| 425 | + * Get the imported form refs | |
| 426 | + * | |
| 427 | + * @return array | |
| 428 | + */ | |
| 429 | + public function get_refs() { | |
| 430 | + return get_option( 'weforms_imported_xforms_' . $this->id, [] ); | |
| 431 | + } | |
| 432 | + | |
| 433 | + /** | |
| 434 | + * Update the imported reference forms | |
| 435 | + * | |
| 436 | + * @param array $refs | |
| 437 | + * | |
| 438 | + * @return void | |
| 439 | + */ | |
| 440 | + public function update_refs( $refs ) { | |
| 441 | + update_option( 'weforms_imported_xforms_' . $this->id, $refs ); | |
| 442 | + } | |
| 443 | + | |
| 444 | + /** | |
| 445 | + * Get form field | |
| 446 | + * | |
| 447 | + * @param string $type | |
| 448 | + * @param array $args [description] | |
| 449 | + * | |
| 450 | + * @return array | |
| 451 | + */ | |
| 452 | + public function get_form_field( $type, $args = [] ) { | |
| 453 | + $defaults = [ | |
| 454 | + 'required' => 'no', | |
| 455 | + 'label' => '', | |
| 456 | + 'name' => '', | |
| 457 | + 'help' => '', | |
| 458 | + 'css_class' => '', | |
| 459 | + 'placeholder' => '', | |
| 460 | + 'value' => '', | |
| 461 | + 'default' => '', | |
| 462 | + 'options' => [], | |
| 463 | + 'step' => '', | |
| 464 | + 'min' => '', | |
| 465 | + 'max' => '', | |
| 466 | + 'extension' => '', | |
| 467 | + 'max_size' => '', // file size | |
| 468 | + 'size' => '', // file size | |
| 469 | + 'first_placeholder' => '', | |
| 470 | + 'first_default' => '', | |
| 471 | + 'middle_placeholder' => '', | |
| 472 | + 'middle_default' => '', | |
| 473 | + 'last_placeholder' => '', | |
| 474 | + 'last_default' => '', | |
| 475 | + 'first_name' => '', | |
| 476 | + 'middle_name' => '', | |
| 477 | + 'last_name' => '', | |
| 478 | + ]; | |
| 479 | + | |
| 480 | + $args = wp_parse_args( $args, $defaults ); | |
| 481 | + | |
| 482 | + switch ( $type ) { | |
| 483 | + case 'text': | |
| 484 | + $field_content = [ | |
| 485 | + 'input_type' => 'text', | |
| 486 | + 'template' => 'text_field', | |
| 487 | + 'required' => $args['required'], | |
| 488 | + 'label' => $args['label'], | |
| 489 | + 'name' => $args['name'], | |
| 490 | + 'is_meta' => 'yes', | |
| 491 | + 'help' => $args['help'], | |
| 492 | + 'css' => $args['css_class'], | |
| 493 | + 'placeholder' => $args['placeholder'], | |
| 494 | + 'default' => $args['default'], | |
| 495 | + 'size' => $args['size'], | |
| 496 | + 'word_restriction' => '', | |
| 497 | + 'wpuf_cond' => $this->conditionals, | |
| 498 | + ]; | |
| 499 | + break; | |
| 500 | + | |
| 501 | + case 'email': | |
| 502 | + $field_content = [ | |
| 503 | + 'input_type' => 'email', | |
| 504 | + 'template' => 'email_address', | |
| 505 | + 'required' => $args['required'], | |
| 506 | + 'label' => $args['label'], | |
| 507 | + 'name' => $args['name'], | |
| 508 | + 'is_meta' => 'yes', | |
| 509 | + 'help' => $args['help'], | |
| 510 | + 'css' => $args['css_class'], | |
| 511 | + 'placeholder' => $args['placeholder'], | |
| 512 | + 'default' => $args['default'], | |
| 513 | + 'size' => $args['size'], | |
| 514 | + 'word_restriction' => '', | |
| 515 | + 'wpuf_cond' => $this->conditionals, | |
| 516 | + ]; | |
| 517 | + break; | |
| 518 | + | |
| 519 | + case 'textarea': | |
| 520 | + $field_content = [ | |
| 521 | + 'input_type' => 'textarea', | |
| 522 | + 'template' => 'textarea_field', | |
| 523 | + 'required' => $args['required'], | |
| 524 | + 'label' => $args['label'], | |
| 525 | + 'name' => $args['name'], | |
| 526 | + 'is_meta' => 'yes', | |
| 527 | + 'help' => $args['help'], | |
| 528 | + 'css' => $args['css_class'], | |
| 529 | + 'rows' => 5, | |
| 530 | + 'cols' => 25, | |
| 531 | + 'placeholder' => $args['placeholder'], | |
| 532 | + 'default' => $args['default'], | |
| 533 | + 'rich' => 'no', | |
| 534 | + 'word_restriction' => '', | |
| 535 | + 'wpuf_cond' => $this->conditionals, | |
| 536 | + ]; | |
| 537 | + break; | |
| 538 | + | |
| 539 | + case 'select': | |
| 540 | + $field_content = [ | |
| 541 | + 'input_type' => 'select', | |
| 542 | + 'template' => 'dropdown_field', | |
| 543 | + 'required' => $args['required'], | |
| 544 | + 'label' => $args['label'], | |
| 545 | + 'name' => $args['name'], | |
| 546 | + 'is_meta' => 'yes', | |
| 547 | + 'help' => '', | |
| 548 | + 'css' => $args['css_class'], | |
| 549 | + 'selected' => '', | |
| 550 | + 'inline' => 'no', | |
| 551 | + 'options' => $args['options'], | |
| 552 | + 'wpuf_cond' => $this->conditionals, | |
| 553 | + ]; | |
| 554 | + break; | |
| 555 | + | |
| 556 | + case 'multiselect': | |
| 557 | + $field_content = [ | |
| 558 | + 'input_type' => 'multiselect', | |
| 559 | + 'template' => 'multiple_select', | |
| 560 | + 'required' => $args['required'], | |
| 561 | + 'label' => $args['label'], | |
| 562 | + 'name' => $args['name'], | |
| 563 | + 'is_meta' => 'yes', | |
| 564 | + 'help' => '', | |
| 565 | + 'css' => $args['css_class'], | |
| 566 | + 'selected' => '', | |
| 567 | + 'first' => __( '- select -', 'weforms' ), | |
| 568 | + 'options' => $args['options'], | |
| 569 | + 'wpuf_cond' => $this->conditionals, | |
| 570 | + ]; | |
| 571 | + break; | |
| 572 | + | |
| 573 | + case 'date': | |
| 574 | + $field_content = [ | |
| 575 | + 'input_type' => 'date', | |
| 576 | + 'template' => 'date_field', | |
| 577 | + 'required' => $args['required'], | |
| 578 | + 'label' => $args['label'], | |
| 579 | + 'name' => $args['name'], | |
| 580 | + 'is_meta' => 'yes', | |
| 581 | + 'help' => '', | |
| 582 | + 'css' => $args['css_class'], | |
| 583 | + 'format' => 'dd/mm/yy', | |
| 584 | + 'time' => '', | |
| 585 | + 'is_publish_time' => '', | |
| 586 | + 'wpuf_cond' => $this->conditionals, | |
| 587 | + ]; | |
| 588 | + break; | |
| 589 | + | |
| 590 | + case 'range': | |
| 591 | + case 'number': | |
| 592 | + | |
| 593 | + $field_content = [ | |
| 594 | + 'input_type' => 'numeric_text', | |
| 595 | + 'template' => 'numeric_text_field', | |
| 596 | + 'required' => $args['required'], | |
| 597 | + 'label' => $args['label'], | |
| 598 | + 'name' => $args['name'], | |
| 599 | + 'is_meta' => 'yes', | |
| 600 | + 'help' => '', | |
| 601 | + 'css' => $args['css_class'], | |
| 602 | + 'placeholder' => $args['placeholder'], | |
| 603 | + 'default' => $args['value'], | |
| 604 | + 'size' => 40, | |
| 605 | + 'step_text_field' => $args['step'], | |
| 606 | + 'min_value_field' => $args['min'], | |
| 607 | + 'max_value_field' => $args['max'], | |
| 608 | + 'wpuf_cond' => $this->conditionals, | |
| 609 | + ]; | |
| 610 | + | |
| 611 | + break; | |
| 612 | + | |
| 613 | + case 'url': | |
| 614 | + $field_content = [ | |
| 615 | + 'input_type' => 'url', | |
| 616 | + 'template' => 'website_url', | |
| 617 | + 'required' => $args['required'], | |
| 618 | + 'label' => $args['label'], | |
| 619 | + 'name' => $args['name'], | |
| 620 | + 'is_meta' => 'yes', | |
| 621 | + 'help' => '', | |
| 622 | + 'css' => $args['css_class'], | |
| 623 | + 'placeholder' => '', | |
| 624 | + 'default' => '', | |
| 625 | + 'size' => 40, | |
| 626 | + 'word_restriction' => '', | |
| 627 | + 'wpuf_cond' => $this->conditionals, | |
| 628 | + ]; | |
| 629 | + | |
| 630 | + break; | |
| 631 | + | |
| 632 | + case 'checkbox': | |
| 633 | + $field_content = [ | |
| 634 | + 'input_type' => 'checkbox', | |
| 635 | + 'template' => 'checkbox_field', | |
| 636 | + 'required' => $args['required'], | |
| 637 | + 'label' => $args['label'], | |
| 638 | + 'name' => $args['name'], | |
| 639 | + 'is_meta' => 'yes', | |
| 640 | + 'help' => '', | |
| 641 | + 'css' => $args['css_class'], | |
| 642 | + 'selected' => '', | |
| 643 | + 'inline' => 'no', | |
| 644 | + 'options' => $args['options'], | |
| 645 | + 'wpuf_cond' => $this->conditionals, | |
| 646 | + ]; | |
| 647 | + break; | |
| 648 | + | |
| 649 | + case 'radio': | |
| 650 | + $field_content = [ | |
| 651 | + 'input_type' => 'radio', | |
| 652 | + 'template' => 'radio_field', | |
| 653 | + 'required' => $args['required'], | |
| 654 | + 'label' => $args['label'], | |
| 655 | + 'name' => $args['name'], | |
| 656 | + 'is_meta' => 'yes', | |
| 657 | + 'help' => '', | |
| 658 | + 'css' => $args['css_class'], | |
| 659 | + 'selected' => '', | |
| 660 | + 'inline' => 'no', | |
| 661 | + 'options' => $args['options'], | |
| 662 | + 'wpuf_cond' => $this->conditionals, | |
| 663 | + ]; | |
| 664 | + break; | |
| 665 | + | |
| 666 | + case 'hidden': | |
| 667 | + $field_content = [ | |
| 668 | + 'input_type' => 'hidden', | |
| 669 | + 'template' => 'custom_hidden_field', | |
| 670 | + 'label' => $args['label'], | |
| 671 | + 'name' => $args['name'], | |
| 672 | + 'is_meta' => 'yes', | |
| 673 | + 'wpuf_cond' => $this->conditionals, | |
| 674 | + ]; | |
| 675 | + break; | |
| 676 | + | |
| 677 | + case 'section_break': | |
| 678 | + $field_content = [ | |
| 679 | + 'input_type' => 'section_break', | |
| 680 | + 'template' => 'section_break', | |
| 681 | + 'label' => $args['label'], | |
| 682 | + 'name' => $args['name'], | |
| 683 | + 'wpuf_cond' => $this->conditionals, | |
| 684 | + ]; | |
| 685 | + break; | |
| 686 | + | |
| 687 | + case 'html': | |
| 688 | + $field_content = [ | |
| 689 | + 'input_type' => 'html', | |
| 690 | + 'template' => 'custom_html', | |
| 691 | + 'label' => $args['label'], | |
| 692 | + 'name' => $args['name'], | |
| 693 | + 'html' => $args['default'], | |
| 694 | + 'wpuf_cond' => $this->conditionals, | |
| 695 | + ]; | |
| 696 | + break; | |
| 697 | + | |
| 698 | + case 'toc': | |
| 699 | + $field_content = [ | |
| 700 | + 'input_type' => 'toc', | |
| 701 | + 'template' => 'toc', | |
| 702 | + 'required' => $args['required'], | |
| 703 | + 'name' => $args['name'], | |
| 704 | + 'description' => $args['label'], | |
| 705 | + 'is_meta' => 'yes', | |
| 706 | + 'show_checkbox' => true, | |
| 707 | + 'wpuf_cond' => $this->conditionals, | |
| 708 | + ]; | |
| 709 | + break; | |
| 710 | + | |
| 711 | + case 'recaptcha': | |
| 712 | + $field_content = [ | |
| 713 | + 'input_type' => 'recaptcha', | |
| 714 | + 'template' => 'recaptcha', | |
| 715 | + 'required' => $args['required'], | |
| 716 | + 'label' => $args['label'], | |
| 717 | + 'name' => $args['name'], | |
| 718 | + 'recaptcha_type' => 'enable_no_captcha', | |
| 719 | + 'is_meta' => 'yes', | |
| 720 | + 'help' => '', | |
| 721 | + 'css' => $args['css_class'], | |
| 722 | + 'placeholder' => '', | |
| 723 | + 'default' => '', | |
| 724 | + 'size' => 40, | |
| 725 | + 'word_restriction' => '', | |
| 726 | + 'wpuf_cond' => $this->conditionals, | |
| 727 | + ]; | |
| 728 | + break; | |
| 729 | + | |
| 730 | + case 'file': | |
| 731 | + $field_content = [ | |
| 732 | + 'input_type' => 'file_upload', | |
| 733 | + 'template' => 'file_upload', | |
| 734 | + 'required' => $args['required'], | |
| 735 | + 'label' => $args['label'], | |
| 736 | + 'name' => $args['name'], | |
| 737 | + 'is_meta' => 'yes', | |
| 738 | + 'help' => $args['help'], | |
| 739 | + 'css' => $args['css_class'], | |
| 740 | + 'max_size' => $args['max_size'], | |
| 741 | + 'count' => '1', | |
| 742 | + 'extension' => $args['extension'], | |
| 743 | + 'wpuf_cond' => $this->conditionals, | |
| 744 | + ]; | |
| 745 | + break; | |
| 746 | + | |
| 747 | + case 'name': | |
| 748 | + $field_content = [ | |
| 749 | + 'input_type' => 'name', | |
| 750 | + 'template' => 'name_field', | |
| 751 | + 'required' => $args['required'], | |
| 752 | + 'label' => $args['label'], | |
| 753 | + 'name' => $args['name'], | |
| 754 | + 'is_meta' => 'yes', | |
| 755 | + 'format' => $args['format'], | |
| 756 | + 'first_name' => $args['first_name'], | |
| 757 | + 'middle_name' => $args['middle_name'], | |
| 758 | + 'last_name' => $args['last_name'], | |
| 759 | + 'hide_subs' => false, | |
| 760 | + 'help' => $args['help'], | |
| 761 | + 'css' => $args['css_class'], | |
| 762 | + 'wpuf_cond' => $this->conditionals, | |
| 763 | + ]; | |
| 764 | + break; | |
| 765 | + | |
| 766 | + case 'ratings': | |
| 767 | + $field_content = [ | |
| 768 | + 'input_type' => 'ratings', | |
| 769 | + 'template' => 'ratings', | |
| 770 | + 'required' => $args['required'], | |
| 771 | + 'label' => $args['label'], | |
| 772 | + 'name' => $args['name'], | |
| 773 | + 'is_meta' => 'yes', | |
| 774 | + 'help' => '', | |
| 775 | + 'css' => $args['css_class'], | |
| 776 | + 'options' => $args['options'], | |
| 777 | + 'wpuf_cond' => $this->conditionals, | |
| 778 | + ]; | |
| 779 | + break; | |
| 780 | + | |
| 781 | + case 'linear_scale': | |
| 782 | + $field_content = [ | |
| 783 | + 'input_type' => 'linear_scale', | |
| 784 | + 'template' => 'linear_scale', | |
| 785 | + 'required' => $args['required'], | |
| 786 | + 'label' => $args['label'], | |
| 787 | + 'name' => $args['name'], | |
| 788 | + 'is_meta' => 'yes', | |
| 789 | + 'help' => '', | |
| 790 | + 'css' => $args['css_class'], | |
| 791 | + 'options' => $args['options'], | |
| 792 | + 'wpuf_cond' => $this->conditionals, | |
| 793 | + ]; | |
| 794 | + break; | |
| 795 | + | |
| 796 | + case 'checkbox_grid': | |
| 797 | + $field_content = [ | |
| 798 | + 'input_type' => 'checkbox_grid', | |
| 799 | + 'template' => 'checkbox_grid', | |
| 800 | + 'required' => $args['required'], | |
| 801 | + 'label' => $args['label'], | |
| 802 | + 'name' => $args['name'], | |
| 803 | + 'is_meta' => 'yes', | |
| 804 | + 'help' => '', | |
| 805 | + 'css' => $args['css_class'], | |
| 806 | + 'options' => $args['options'], | |
| 807 | + 'wpuf_cond' => $this->conditionals, | |
| 808 | + ]; | |
| 809 | + break; | |
| 810 | + | |
| 811 | + case 'multiple_choice_grid': | |
| 812 | + $field_content = [ | |
| 813 | + 'input_type' => 'multiple_choice_grid', | |
| 814 | + 'template' => 'multiple_choice_grid', | |
| 815 | + 'required' => $args['required'], | |
| 816 | + 'label' => $args['label'], | |
| 817 | + 'name' => $args['name'], | |
| 818 | + 'is_meta' => 'yes', | |
| 819 | + 'help' => '', | |
| 820 | + 'css' => $args['css_class'], | |
| 821 | + 'options' => $args['options'], | |
| 822 | + 'wpuf_cond' => $this->conditionals, | |
| 823 | + ]; | |
| 824 | + break; | |
| 825 | + | |
| 826 | + case 'single_product': | |
| 827 | + $field_content = [ | |
| 828 | + 'input_type' => 'single_product', | |
| 829 | + 'template' => 'single_product', | |
| 830 | + 'required' => $args['required'], | |
| 831 | + 'label' => $args['label'], | |
| 832 | + 'name' => $args['name'], | |
| 833 | + 'is_meta' => 'yes', | |
| 834 | + 'help' => '', | |
| 835 | + 'css' => $args['css_class'], | |
| 836 | + 'options' => $args['options'], | |
| 837 | + 'wpuf_cond' => $this->conditionals, | |
| 838 | + ]; | |
| 839 | + break; | |
| 840 | + | |
| 841 | + case 'multiple_product': | |
| 842 | + $field_content = [ | |
| 843 | + 'input_type' => 'multiple_product', | |
| 844 | + 'template' => 'multiple_product', | |
| 845 | + 'required' => $args['required'], | |
| 846 | + 'label' => $args['label'], | |
| 847 | + 'name' => $args['name'], | |
| 848 | + 'is_meta' => 'yes', | |
| 849 | + 'help' => '', | |
| 850 | + 'css' => $args['css_class'], | |
| 851 | + 'options' => $args['options'], | |
| 852 | + 'wpuf_cond' => $this->conditionals, | |
| 853 | + ]; | |
| 854 | + break; | |
| 855 | + | |
| 856 | + case 'payment_method': | |
| 857 | + $field_content = [ | |
| 858 | + 'input_type' => 'payment_method', | |
| 859 | + 'template' => 'payment_method', | |
| 860 | + 'required' => $args['required'], | |
| 861 | + 'label' => $args['label'], | |
| 862 | + 'name' => $args['name'], | |
| 863 | + 'is_meta' => 'yes', | |
| 864 | + 'help' => '', | |
| 865 | + 'css' => $args['css_class'], | |
| 866 | + 'options' => $args['options'], | |
| 867 | + 'wpuf_cond' => $this->conditionals, | |
| 868 | + ]; | |
| 869 | + break; | |
| 870 | + | |
| 871 | + case 'total': | |
| 872 | + $field_content = [ | |
| 873 | + 'input_type' => 'total', | |
| 874 | + 'template' => 'total', | |
| 875 | + 'required' => $args['required'], | |
| 876 | + 'label' => $args['label'], | |
| 877 | + 'name' => $args['name'], | |
| 878 | + 'is_meta' => 'yes', | |
| 879 | + 'help' => '', | |
| 880 | + 'css' => $args['css_class'], | |
| 881 | + 'options' => $args['options'], | |
| 882 | + 'wpuf_cond' => $this->conditionals, | |
| 883 | + ]; | |
| 884 | + break; | |
| 885 | + } | |
| 886 | + | |
| 887 | + return $field_content; | |
| 888 | + } | |
| 889 | + | |
| 890 | + /** | |
| 891 | + * Default form settings | |
| 892 | + * | |
| 893 | + * @return array | |
| 894 | + */ | |
| 895 | + public function get_default_form_settings() { | |
| 896 | + $form_settings = [ | |
| 897 | + 'redirect_to' => 'same', | |
| 898 | + 'message' => __( 'Thanks for contacting us! We will get in touch with you shortly.', 'weforms' ), | |
| 899 | + 'page_id' => '', | |
| 900 | + 'url' => '', | |
| 901 | + 'submit_text' => __( 'Submit Query', 'weforms' ), | |
| 902 | + 'schedule_form' => 'false', | |
| 903 | + 'schedule_start' => '', | |
| 904 | + 'schedule_end' => '', | |
| 905 | + 'sc_pending_message' => __( 'Form submission hasn\'t been started yet', 'weforms' ), | |
| 906 | + 'sc_expired_message' => __( 'Form submission is now closed.', 'weforms' ), | |
| 907 | + 'require_login' => 'false', | |
| 908 | + 'req_login_message' => __( 'You need to login to submit a query.', 'weforms' ), | |
| 909 | + 'limit_entries' => 'false', | |
| 910 | + 'limit_number' => '1000', | |
| 911 | + 'limit_message' => __( 'Sorry, we have reached the maximum number of submissions.', 'weforms' ), | |
| 912 | + 'label_position' => 'above', | |
| 913 | + ]; | |
| 914 | + | |
| 915 | + return $form_settings; | |
| 916 | + } | |
| 917 | + | |
| 918 | + /** | |
| 919 | + * Insert a form | |
| 920 | + * | |
| 921 | + * @param string $form_name | |
| 922 | + * | |
| 923 | + * @return ID|WP_Error | |
| 924 | + */ | |
| 925 | + public function insert_form( $form_name ) { | |
| 926 | + $weforms_form = [ | |
| 927 | + 'post_title' => sprintf( '[%s] %s', strtoupper( $this->id ), $form_name ), | |
| 928 | + 'post_type' => 'wpuf_contact_form', | |
| 929 | + 'post_status' => 'publish', | |
| 930 | + 'post_author' => get_current_user_id(), | |
| 931 | + ]; | |
| 932 | + | |
| 933 | + return wp_insert_post( $weforms_form ); | |
| 934 | + } | |
| 935 | + | |
| 936 | + /** | |
| 937 | + * Insert a form field | |
| 938 | + * | |
| 939 | + * @param array $field | |
| 940 | + * @param int $form_id | |
| 941 | + * @param int $menu_order | |
| 942 | + * | |
| 943 | + * @return int|WP_Error | |
| 944 | + */ | |
| 945 | + public function insert_form_field( $field, $form_id, $menu_order ) { | |
| 946 | + $form_field = [ | |
| 947 | + 'post_type' => 'wpuf_input', | |
| 948 | + 'post_status' => 'publish', | |
| 949 | + 'post_content' => maybe_serialize( $field ), | |
| 950 | + 'post_parent' => $form_id, | |
| 951 | + 'menu_order' => $menu_order, | |
| 952 | + ]; | |
| 953 | + | |
| 954 | + return wp_insert_post( $form_field ); | |
| 955 | + } | |
| 956 | + | |
| 957 | + /** | |
| 958 | + * Update form settings | |
| 959 | + * | |
| 960 | + * @param int $form_id | |
| 961 | + * @param array $settings | |
| 962 | + * | |
| 963 | + * @return void | |
| 964 | + */ | |
| 965 | + public function update_settings( $form_id, $settings ) { | |
| 966 | + update_post_meta( $form_id, 'wpuf_form_settings', $settings ); | |
| 967 | + } | |
| 968 | + | |
| 969 | + /** | |
| 970 | + * Update Notification | |
| 971 | + * | |
| 972 | + * @param int $form_id | |
| 973 | + * @param array $notification | |
| 974 | + * | |
| 975 | + * @return void | |
| 976 | + */ | |
| 977 | + public function update_notification( $form_id, $notifications ) { | |
| 978 | + update_post_meta( $form_id, 'notifications', $notifications ); | |
| 979 | + } | |
| 980 | +} | |