| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Ninja Form |
| 5 |
* |
| 6 |
* Import Ninja form forms |
| 7 |
*/ |
| 8 |
class WeForms_Importer_Ninja_Forms extends WeForms_Importer_Abstract { |
| 9 |
|
| 10 |
/** |
| 11 |
* Will store submit button label |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
private $submit_text; |
| 16 |
|
| 17 |
function __construct() { |
| 18 |
$this->id = 'nf'; |
| 19 |
$this->title = 'Ninja Forms'; |
| 20 |
$this->shortcode = 'ninja_form'; |
| 21 |
|
| 22 |
parent::__construct(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* See if the plugin exists |
| 27 |
* |
| 28 |
* @return boolean |
| 29 |
*/ |
| 30 |
public function plugin_exists() { |
| 31 |
return class_exists( 'Ninja_Forms' ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Show notice if Ninja From found |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function ninja_form_field($form) { |
| 40 |
|
| 41 |
$data = array(); |
| 42 |
foreach( Ninja_Forms()->form( $form->get_id() )->get_fields() as $field ){ |
| 43 |
$data[$field->get_settings( 'order' )] = array( |
| 44 |
'type' => $field->get_setting( 'type' ), |
| 45 |
'key' => $field->get_setting( 'key' ), |
| 46 |
'label' => $field->get_setting( 'label' ), |
| 47 |
'default' => $field->get_setting( 'default' ), |
| 48 |
'required' => $field->get_setting( 'required' ) ? $field->get_setting( 'required' ) : 0 |
| 49 |
); |
| 50 |
|
| 51 |
if (in_array($field->get_setting('type'), array('listselect', 'listradio', 'listcheckbox', 'listmultiselect')) ) { |
| 52 |
foreach ($field->get_setting('options') as $option) { |
| 53 |
$data[$field->get_settings( 'order' )]['options'][] = array( |
| 54 |
'label' => $option['label'], |
| 55 |
'value' => $option['value'], |
| 56 |
); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
return $data; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* Get all the forms |
| 66 |
* |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public function get_forms() { |
| 70 |
$items = Ninja_Forms()->form()->get_forms(); |
| 71 |
|
| 72 |
return $items; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get form name |
| 77 |
* |
| 78 |
* @param object $form |
| 79 |
* |
| 80 |
* @return string |
| 81 |
*/ |
| 82 |
public function get_form_name( $form ) { |
| 83 |
return $form->get_setting( 'title' ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get the form id |
| 88 |
* |
| 89 |
* @param mixed $form |
| 90 |
* |
| 91 |
* @return int |
| 92 |
*/ |
| 93 |
protected function get_form_id( $form ) { |
| 94 |
return $form->get_id(); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get the form fields |
| 99 |
* |
| 100 |
* @param object $form |
| 101 |
* |
| 102 |
* @return array |
| 103 |
*/ |
| 104 |
public function get_form_fields( $form ) { |
| 105 |
|
| 106 |
$ninja_form_fields = $this->ninja_form_field($form); |
| 107 |
|
| 108 |
|
| 109 |
// echo "<pre>"; |
| 110 |
// print_r($ninja_form_fields); |
| 111 |
// echo "</pre>"; |
| 112 |
|
| 113 |
// die; |
| 114 |
|
| 115 |
$form_fields = array(); |
| 116 |
|
| 117 |
foreach ($ninja_form_fields as $menu_order => $field) { |
| 118 |
|
| 119 |
$field_content = array(); |
| 120 |
|
| 121 |
switch ( $field['type'] ) { |
| 122 |
case 'textbox': |
| 123 |
case 'text': |
| 124 |
case 'email': |
| 125 |
case 'textarea': |
| 126 |
case 'date': |
| 127 |
case 'url': |
| 128 |
case 'firstname': |
| 129 |
case 'lastname': |
| 130 |
case 'zip': |
| 131 |
case 'product': |
| 132 |
case 'city': |
| 133 |
case 'quiz': |
| 134 |
case 'address': |
| 135 |
case 'spam': |
| 136 |
|
| 137 |
if($field['type'] == 'firstname' || $field['type'] == 'lastname' || $field['type'] == 'zip' || $field['type'] == 'product' || $field['type'] == 'city' || $field['type'] == 'quiz' || $field['type'] == 'address' || $field['type'] == 'spam' || $field['type'] == 'textbox') { |
| 138 |
$field['type'] = 'text'; |
| 139 |
} |
| 140 |
|
| 141 |
$form_fields[] = $this->get_form_field( $field['type'], array( |
| 142 |
'required' => $field['required'] ? 'yes' : 'no', |
| 143 |
'label' => $this->find_label( $field['label'], $field['type'], $field['key'] ), |
| 144 |
'name' => $field['key'], |
| 145 |
) ); |
| 146 |
break; |
| 147 |
|
| 148 |
case 'checkbox': |
| 149 |
case 'listcheckbox': |
| 150 |
if($field['type'] == 'listcheckbox') { |
| 151 |
$field['type'] = 'checkbox'; |
| 152 |
} |
| 153 |
|
| 154 |
$form_fields[] = $this->get_form_field( $field['type'], array( |
| 155 |
'required' => $field['required'] ? 'yes' : 'no', |
| 156 |
'label' => $this->find_label( $field['label'], $field['type'], $field['key'] ), |
| 157 |
'name' => $field['key'], |
| 158 |
'options' => $this->get_options( $field['options'] ) ? $this->get_options( $field['options'] ) : array( 'label' => $field['label'], 'value' => $field['key'] ), |
| 159 |
) ); |
| 160 |
|
| 161 |
break; |
| 162 |
|
| 163 |
case 'select': |
| 164 |
case 'radio': |
| 165 |
case 'listmultiselect': |
| 166 |
case 'listradio': |
| 167 |
case 'listselect': |
| 168 |
case 'listcountry': |
| 169 |
case 'starrating': |
| 170 |
if($field['type'] == 'listcountry' || $field['type'] == 'starrating') { |
| 171 |
$field['type'] = 'select'; |
| 172 |
} else if($field['type'] == 'listmultiselect' || $field['type'] == 'listselect') { |
| 173 |
$field['type'] = 'multiselect'; |
| 174 |
} else if($field['type'] == 'listradio') { |
| 175 |
$field['type'] = 'radio'; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
$form_fields[] = $this->get_form_field( $field['type'], array( |
| 180 |
'required' => $field['required'] ? 'yes' : 'no', |
| 181 |
'label' => $this->find_label( $field['label'], $field['type'], $field['key'] ), |
| 182 |
'name' => $field['key'], |
| 183 |
'options' => $this->get_options( $field['options'] ), |
| 184 |
) ); |
| 185 |
break; |
| 186 |
|
| 187 |
case 'range': |
| 188 |
case 'number': |
| 189 |
case 'phone': |
| 190 |
case 'quantity': |
| 191 |
case 'total': |
| 192 |
case 'shipping': |
| 193 |
if($field['type'] == 'phone' || $field['type'] == 'quantity' || $field['type'] == 'total' || $field['type'] == 'shipping') { |
| 194 |
$field['type'] = 'number'; |
| 195 |
} |
| 196 |
|
| 197 |
$form_fields[] = $this->get_form_field( $field['type'], array( |
| 198 |
'required' => $field['required'] ? 'yes' : 'no', |
| 199 |
'label' => $this->find_label( $field['label'], $field['type'], $field['key'] ), |
| 200 |
'name' => $field['key'], |
| 201 |
) ); |
| 202 |
|
| 203 |
break; |
| 204 |
|
| 205 |
case 'hr': |
| 206 |
case 'hidden': |
| 207 |
case 'html': |
| 208 |
|
| 209 |
if($field['type'] == 'hr') { |
| 210 |
$field['type'] = 'section_break'; |
| 211 |
} |
| 212 |
|
| 213 |
$form_fields[] = $this->get_form_field( $field['type'], array( |
| 214 |
'label' => $this->find_label( $field['label'], $field['type'], $field['key'] ), |
| 215 |
'name' => $field['key'], |
| 216 |
'default' => $field['default'], |
| 217 |
) ); |
| 218 |
|
| 219 |
break; |
| 220 |
|
| 221 |
case 'acceptance': |
| 222 |
|
| 223 |
$form_fields[] = $this->get_form_field( 'toc', array( |
| 224 |
'required' => $field['required'] ? 'yes' : 'no', |
| 225 |
'description' => $this->find_label( $field['label'], $field['type'], $field['key'] ), |
| 226 |
'name' => $field['key'], |
| 227 |
) ); |
| 228 |
break; |
| 229 |
|
| 230 |
case 'recaptcha': |
| 231 |
|
| 232 |
$form_fields[] = $this->get_form_field( $field['type'], array( |
| 233 |
'required' => $field['required'] ? 'yes' : 'no', |
| 234 |
'label' => $this->find_label( $field['label'], $field['type'], $field['key'] ), |
| 235 |
'name' => $field['key'], |
| 236 |
) ); |
| 237 |
break; |
| 238 |
|
| 239 |
case 'submit': |
| 240 |
|
| 241 |
$this->submit_text = $field['label']; |
| 242 |
|
| 243 |
break; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
return $form_fields; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Get form settings |
| 252 |
* |
| 253 |
* @param object $form |
| 254 |
* |
| 255 |
* @return array |
| 256 |
*/ |
| 257 |
public function get_form_settings( $form ) { |
| 258 |
$all_settings = get_option( 'nf_form_' . $form->get_id(), true ); |
| 259 |
foreach ($all_settings['actions'] as $actions) { |
| 260 |
if('successmessage' == $actions['settings']['type']){ |
| 261 |
$message = $actions['settings']['message']; |
| 262 |
} |
| 263 |
} |
| 264 |
$message = str_replace(' {field:name}', '', $message); |
| 265 |
$default = $this->get_default_form_settings(); |
| 266 |
$settings = wp_parse_args( array( |
| 267 |
'message' => $message, |
| 268 |
), $default ); |
| 269 |
|
| 270 |
if ( $this->submit_text ) { |
| 271 |
$settings['submit_text'] = $this->submit_text; |
| 272 |
} |
| 273 |
|
| 274 |
return $settings; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Get form notifications |
| 279 |
* |
| 280 |
* @param object $form |
| 281 |
* |
| 282 |
* @return array |
| 283 |
*/ |
| 284 |
public function get_form_notifications( $form ) { |
| 285 |
$notifications = array(); |
| 286 |
$all_settings = get_option( 'nf_form_' . $form->get_id(), true ); |
| 287 |
foreach ($all_settings['actions'] as $actions) { |
| 288 |
if('Email Notification' == $actions['settings']['label']){ |
| 289 |
$action_settings = $actions['settings']; |
| 290 |
} |
| 291 |
else if('Email Confirmation' == $actions['settings']['label']){ |
| 292 |
$action_settings2 = $actions['settings']; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
$sub = str_replace('{field:name}', '{site_name}', $action_settings['email_subject']); |
| 297 |
|
| 298 |
$notifications = array( |
| 299 |
array( |
| 300 |
'active' => $action_settings['active'] ? 'true' : 'false', |
| 301 |
'name' => 'Admin Notification', |
| 302 |
'subject' => str_replace( '[your-subject]', '{field:your-subject}', $sub ), |
| 303 |
'to' => '{field:your-email}', |
| 304 |
'replyTo' => '{field:your-email}', |
| 305 |
'message' => '{all_fields}', |
| 306 |
'fromName' => '{site_name}', |
| 307 |
'fromAddress' => '{admin_email}', |
| 308 |
'cc' => '', |
| 309 |
'bcc' => '', |
| 310 |
), |
| 311 |
); |
| 312 |
|
| 313 |
$sender_match = $this->get_notification_sender_match( get_option( 'admin_email' ) ); |
| 314 |
|
| 315 |
if ( !empty( $sender_match['fromName'] ) ) { |
| 316 |
$form_notifications[0]['fromName'] = $sender_match['fromName']; |
| 317 |
} |
| 318 |
|
| 319 |
if ( isset( $sender_match['fromAddress'] ) ) { |
| 320 |
$form_notifications[0]['fromAddress'] = $sender_match['fromAddress']; |
| 321 |
} |
| 322 |
|
| 323 |
if ( $action_settings2['active'] ) { |
| 324 |
$notifications[] = array( |
| 325 |
'active' => $action_settings2['active'] ? 'true' : 'false', |
| 326 |
'name' => 'Admin Notification', |
| 327 |
'subject' => str_replace( '[your-subject]', $action_settings2['subject'], $action_settings2['subject'] ), |
| 328 |
'to' => '{field:your-email}', |
| 329 |
'replyTo' => '{field:your-email}', |
| 330 |
'message' => '{all_fields}', |
| 331 |
'fromName' => '{site_name}', |
| 332 |
'fromAddress' => '{admin_email}', |
| 333 |
'cc' => '', |
| 334 |
'bcc' => '', |
| 335 |
); |
| 336 |
} |
| 337 |
|
| 338 |
$sender_match = $this->get_notification_sender_match( get_option( 'admin_email' ) ); |
| 339 |
|
| 340 |
if ( !empty( $sender_match['fromName'] ) ) { |
| 341 |
$form_notifications[1]['fromName'] = $sender_match['fromName']; |
| 342 |
} |
| 343 |
|
| 344 |
if ( isset( $sender_match['fromAddress'] ) ) { |
| 345 |
$form_notifications[1]['fromAddress'] = $sender_match['fromAddress']; |
| 346 |
} |
| 347 |
|
| 348 |
return $notifications; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Match the sender |
| 353 |
* |
| 354 |
* @param array $mail |
| 355 |
* |
| 356 |
* @return array |
| 357 |
*/ |
| 358 |
public function get_notification_sender_match( $mail ) { |
| 359 |
|
| 360 |
if ( !isset( $mail['sender'] ) ) { |
| 361 |
return; |
| 362 |
} |
| 363 |
$sender = array( 'fromName' => '', 'fromAddress' => '' ); |
| 364 |
$sender_match = array(); |
| 365 |
|
| 366 |
preg_match( '/([^<"]*)"?\s*<(\S*)>/', $mail['sender'], $sender_match ); |
| 367 |
|
| 368 |
if ( isset( $sender_match[1] ) ) { |
| 369 |
$sender['fromName'] = $sender_match[1]; |
| 370 |
} |
| 371 |
|
| 372 |
if ( isset( $sender_match[2] ) ) { |
| 373 |
$sender['fromAddress'] = $sender_match[2]; |
| 374 |
} |
| 375 |
|
| 376 |
return $sender; |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Try to find out the input label |
| 381 |
* |
| 382 |
* Loop through all the label tags and try to find out |
| 383 |
* if the field is inside that tag. Then strip out the field and find out label |
| 384 |
* |
| 385 |
* @param string $content |
| 386 |
* @param string $type |
| 387 |
* @param string $fieldname |
| 388 |
* |
| 389 |
* @return string |
| 390 |
*/ |
| 391 |
private function find_label( $content, $type, $fieldname ) { |
| 392 |
|
| 393 |
return $content; // i guess, we don't need this :/ will remove later |
| 394 |
|
| 395 |
// find all enclosing label fields |
| 396 |
$pattern = '/<label>([ \w\S\r\n\t]+?)<\/label>/'; |
| 397 |
preg_match_all( $pattern, $content, $matches ); |
| 398 |
|
| 399 |
foreach ($matches[1] as $key => $match) { |
| 400 |
$match = trim( str_replace( "\n", '', $match ) ); |
| 401 |
|
| 402 |
preg_match( '/\[(?:' . preg_quote( $type ) . ') ' . $fieldname . '(?:[ ](.*?))?(?:[\r\n\t ](\/))?\]/', $match, $input_match ); |
| 403 |
|
| 404 |
if ( $input_match ) { |
| 405 |
$label = strip_tags( str_replace( $input_match[0], '', $match ) ); |
| 406 |
return trim( $label ); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
return $fieldname; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Get file type for upload files |
| 415 |
* |
| 416 |
* @param string $extension |
| 417 |
* |
| 418 |
* @return boolean|string |
| 419 |
*/ |
| 420 |
private function get_file_type( $extension ) { |
| 421 |
$allowed_extensions = weforms_allowed_extensions(); |
| 422 |
|
| 423 |
foreach ($allowed_extensions as $type => $extensions) { |
| 424 |
$_extensions = explode( ',', $extensions['ext'] ); |
| 425 |
|
| 426 |
if ( in_array( $extension, $_extensions ) ) { |
| 427 |
return $type; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
return false; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Translate to wpuf field options array |
| 436 |
* |
| 437 |
* @param object $field |
| 438 |
* |
| 439 |
* @return array |
| 440 |
*/ |
| 441 |
private function get_options( $field ) { |
| 442 |
$options = array(); |
| 443 |
|
| 444 |
if ( !is_array( $field ) ) { |
| 445 |
return $options; |
| 446 |
} |
| 447 |
|
| 448 |
foreach ($field as $value) { |
| 449 |
$options[ $value['value'] ] = $value['label']; |
| 450 |
} |
| 451 |
|
| 452 |
return $options; |
| 453 |
} |
| 454 |
} |
| 455 |
|