| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPForms importer class |
| 4 |
*/ |
| 5 |
class WeForms_Importer_WPForms extends WeForms_Importer_Abstract { |
| 6 |
|
| 7 |
public function __construct() { |
| 8 |
$this->id = 'wpforms'; |
| 9 |
$this->title = 'WPForms'; |
| 10 |
$this->shortcode = 'wpforms'; |
| 11 |
|
| 12 |
parent::__construct(); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Check if the plugin exists |
| 17 |
* |
| 18 |
* @return bool |
| 19 |
*/ |
| 20 |
public function plugin_exists() { |
| 21 |
return function_exists( 'wpforms' ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get all the forms |
| 26 |
* |
| 27 |
* @return array |
| 28 |
*/ |
| 29 |
public function get_forms() { |
| 30 |
return wpforms()->form->get(); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Parse a form to array |
| 35 |
* |
| 36 |
* @param string $form |
| 37 |
* |
| 38 |
* @return array |
| 39 |
*/ |
| 40 |
public function parse_form( $form ) { |
| 41 |
return json_decode( $form->post_content, true ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get the form name |
| 46 |
* |
| 47 |
* @param string $form |
| 48 |
* |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public function get_form_name( $form ) { |
| 52 |
return $form->post_title; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get the form id |
| 57 |
* |
| 58 |
* @param mixed $form |
| 59 |
* |
| 60 |
* @return int |
| 61 |
*/ |
| 62 |
protected function get_form_id( $form ) { |
| 63 |
return $form->ID; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get form notifications of a form |
| 68 |
* |
| 69 |
* @param mixed $form |
| 70 |
* |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
public function get_form_notifications( $form ) { |
| 74 |
$parsed = $this->parse_form( $form ); |
| 75 |
$notification = array_pop( $parsed['settings']['notifications'] ); |
| 76 |
|
| 77 |
$form_notifications = [ |
| 78 |
[ |
| 79 |
'active' => $parsed['settings']['notification_enable'] ? true : false, |
| 80 |
'name' => 'Admin Notification', |
| 81 |
'subject' => $notification['subject'], |
| 82 |
'to' => $notification['email'], |
| 83 |
'replyTo' => $notification['replyto'], |
| 84 |
'message' => $notification['message'], |
| 85 |
'fromName' => $notification['sender_name'], |
| 86 |
'fromAddress' => $notification['sender_address'], |
| 87 |
'cc' => '', |
| 88 |
'bcc' => '', |
| 89 |
], |
| 90 |
]; |
| 91 |
|
| 92 |
return $form_notifications; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get all form fields of a form |
| 97 |
* |
| 98 |
* @param mixed $form |
| 99 |
* |
| 100 |
* @return array |
| 101 |
*/ |
| 102 |
public function get_form_fields( $form ) { |
| 103 |
$form_fields = []; |
| 104 |
$parsed = $this->parse_form( $form ); |
| 105 |
|
| 106 |
if ( empty( $parsed['fields'] ) ) { |
| 107 |
return $form_fields; |
| 108 |
} |
| 109 |
|
| 110 |
foreach ( $parsed['fields'] as $menu_order => $field ) { |
| 111 |
|
| 112 |
// avoid empty meta_key |
| 113 |
$field['id'] = $field['type'] . '_' . $field['id']; |
| 114 |
|
| 115 |
switch ( $field['type'] ) { |
| 116 |
case 'text': |
| 117 |
case 'email': |
| 118 |
case 'textarea': |
| 119 |
|
| 120 |
$form_fields[] = $this->get_form_field( $field['type'], [ |
| 121 |
'required' => !empty( $field['required'] ) ? 'yes' : 'no', |
| 122 |
'label' => $field['label'], |
| 123 |
'name' => $field['id'], |
| 124 |
'help' => $field['description'], |
| 125 |
'css_class' => $field['css'], |
| 126 |
'placeholder' => $field['placeholder'], |
| 127 |
'default' => !empty( $field['default_value'] ) ? $field['default_value'] : '', |
| 128 |
] ); |
| 129 |
|
| 130 |
break; |
| 131 |
|
| 132 |
case 'select': |
| 133 |
case 'radio': |
| 134 |
case 'checkbox': |
| 135 |
|
| 136 |
$form_fields[] = $this->get_form_field( $field['type'], [ |
| 137 |
'required' => !empty( $field['required'] ) ? 'yes' : 'no', |
| 138 |
'label' => $field['label'], |
| 139 |
'name' => $field['id'], |
| 140 |
'help' => $field['description'], |
| 141 |
'css_class' => $field['css'], |
| 142 |
'placeholder' => $field['placeholder'], |
| 143 |
'selected' => !empty( $field['default_value'] ) ? $field['default_value'] : '', |
| 144 |
'options' => $this->get_options( $field ), |
| 145 |
] ); |
| 146 |
break; |
| 147 |
|
| 148 |
case 'name': |
| 149 |
|
| 150 |
$form_fields[] = $this->get_form_field( $field['type'], [ |
| 151 |
'required' => !empty( $field['required'] ) ? 'yes' : 'no', |
| 152 |
'label' => $field['label'], |
| 153 |
'name' => $field['id'], |
| 154 |
'help' => $field['description'], |
| 155 |
'css_class' => $field['css'], |
| 156 |
'format' => ( $field['format'] === 'first-last' ) ? 'first-last' : 'first-middle-last', |
| 157 |
'hide_subs' => !empty( $field['sublabel_hide'] ) ? true : false, |
| 158 |
'first_name' => [ |
| 159 |
'placeholder' => $field['first_placeholder'], |
| 160 |
'default' => $field['first_default'], |
| 161 |
'sub' => __( 'First', 'weforms' ), |
| 162 |
], |
| 163 |
'middle_name' => [ |
| 164 |
'placeholder' => $field['middle_placeholder'], |
| 165 |
'default' => $field['middle_default'], |
| 166 |
'sub' => __( 'Middle', 'weforms' ), |
| 167 |
], |
| 168 |
'last_name' => [ |
| 169 |
'placeholder' => $field['last_placeholder'], |
| 170 |
'default' => $field['last_default'], |
| 171 |
'sub' => __( 'Last', 'weforms' ), |
| 172 |
], |
| 173 |
] ); |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return $form_fields; |
| 179 |
} |
| 180 |
|
| 181 |
public function get_form_settings( $form ) { |
| 182 |
$default = $this->get_default_form_settings(); |
| 183 |
$parsed = $this->parse_form( $form ); |
| 184 |
|
| 185 |
$settings = $parsed['settings']; |
| 186 |
|
| 187 |
// Settings mapping |
| 188 |
switch ( $settings['confirmations'][1]['type'] ) { |
| 189 |
case 'redirect': |
| 190 |
$redirect_to = 'url'; |
| 191 |
break; |
| 192 |
|
| 193 |
case 'page': |
| 194 |
$redirect_to = 'page'; |
| 195 |
break; |
| 196 |
|
| 197 |
case 'message': |
| 198 |
default: |
| 199 |
$redirect_to = 'same'; |
| 200 |
break; |
| 201 |
} |
| 202 |
|
| 203 |
$form_settings = wp_parse_args( [ |
| 204 |
'message' => $settings['confirmations'][1]['message'], |
| 205 |
'page_id' => $settings['confirmations'][1]['page'], |
| 206 |
'url' => $settings['confirmations'][1]['redirect'], |
| 207 |
'submit_text' => $settings['submit_text'], |
| 208 |
'redirect_to' => $redirect_to, |
| 209 |
], $default ); |
| 210 |
|
| 211 |
return $form_settings; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Translate to wpuf field options array |
| 216 |
* |
| 217 |
* @param object $field |
| 218 |
* |
| 219 |
* @return array |
| 220 |
*/ |
| 221 |
private function get_options( $field ) { |
| 222 |
$options = []; |
| 223 |
|
| 224 |
if ( !$field['choices'] ) { |
| 225 |
return $options; |
| 226 |
} |
| 227 |
|
| 228 |
foreach ( $field['choices'] as $choice ) { |
| 229 |
$options[ $choice['label'] ] = $choice['label']; |
| 230 |
} |
| 231 |
|
| 232 |
return $options; |
| 233 |
} |
| 234 |
} |
| 235 |
|