| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmXMLController { |
| 7 |
|
| 8 |
/** |
| 9 |
* @return void |
| 10 |
*/ |
| 11 |
public static function menu() { |
| 12 |
add_submenu_page( 'formidable', 'Formidable | ' . __( 'Import/Export', 'formidable' ), __( 'Import/Export', 'formidable' ), 'frm_edit_forms', 'formidable-import', 'FrmXMLController::route' ); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
public static function add_default_templates() { |
| 19 |
if ( FrmXMLHelper::check_if_libxml_disable_entity_loader_exists() ) { |
| 20 |
// XML import is not enabled on your server. |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
$set_err = libxml_use_internal_errors( true ); |
| 25 |
$loader = FrmXMLHelper::maybe_libxml_disable_entity_loader( true ); |
| 26 |
|
| 27 |
$files = apply_filters( 'frm_default_templates_files', array() ); |
| 28 |
|
| 29 |
foreach ( (array) $files as $file ) { |
| 30 |
FrmXMLHelper::import_xml( $file ); |
| 31 |
unset( $file ); |
| 32 |
} |
| 33 |
|
| 34 |
unset( $files ); |
| 35 |
|
| 36 |
libxml_use_internal_errors( $set_err ); |
| 37 |
FrmXMLHelper::maybe_libxml_disable_entity_loader( $loader ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Use the template link to install the XML template |
| 42 |
* |
| 43 |
* @since 3.06 |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public static function install_template() { |
| 47 |
FrmAppHelper::permission_check( 'frm_edit_forms' ); |
| 48 |
check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 49 |
|
| 50 |
if ( ! function_exists( 'simplexml_load_string' ) ) { |
| 51 |
$response = array( |
| 52 |
'message' => __( 'Your server is missing the Simple XML extension. This is required to install a template.', 'formidable' ), |
| 53 |
); |
| 54 |
echo wp_json_encode( $response ); |
| 55 |
wp_die(); |
| 56 |
} |
| 57 |
|
| 58 |
$form = self::get_posted_form(); |
| 59 |
$url = FrmAppHelper::get_param( 'xml', '', 'post', 'esc_url_raw' ); |
| 60 |
self::override_url( $form, $url ); |
| 61 |
|
| 62 |
if ( ! self::validate_xml_url( $url ) ) { |
| 63 |
$response = array( |
| 64 |
'message' => __( 'The template you are trying to install could not be validated.', 'formidable' ), |
| 65 |
); |
| 66 |
echo wp_json_encode( $response ); |
| 67 |
wp_die(); |
| 68 |
} |
| 69 |
|
| 70 |
$response = wp_remote_get( $url ); |
| 71 |
$body = wp_remote_retrieve_body( $response ); |
| 72 |
$xml = simplexml_load_string( $body ); |
| 73 |
|
| 74 |
if ( ! $xml ) { |
| 75 |
$response = array( |
| 76 |
'message' => __( 'There was an error reading the form template.', 'formidable' ), |
| 77 |
); |
| 78 |
echo wp_json_encode( $response ); |
| 79 |
wp_die(); |
| 80 |
} |
| 81 |
|
| 82 |
self::set_new_form_name( $xml ); |
| 83 |
|
| 84 |
$imported = FrmXMLHelper::import_xml_now( $xml, true ); |
| 85 |
if ( ! empty( $imported['form_status'] ) ) { |
| 86 |
// Get the last form id in case there are child forms. |
| 87 |
end( $imported['form_status'] ); |
| 88 |
$form_id = key( $imported['form_status'] ); |
| 89 |
$response = array( |
| 90 |
'id' => $form_id, |
| 91 |
'redirect' => FrmForm::get_edit_link( $form_id ) . '&new_template=true', |
| 92 |
'success' => 1, |
| 93 |
); |
| 94 |
if ( ! empty( $imported['imported']['posts'] ) ) { |
| 95 |
// Return the link to the last page created. |
| 96 |
$pages = $imported['posts']; |
| 97 |
} |
| 98 |
|
| 99 |
if ( ! empty( $form ) ) { |
| 100 |
// Create selected pages with the correct shortcodes. |
| 101 |
$pages = self::create_pages_for_import( $form ); |
| 102 |
} |
| 103 |
|
| 104 |
if ( ! empty( $pages ) ) { |
| 105 |
$post_id = end( $pages ); |
| 106 |
$response['redirect'] = get_permalink( $post_id ); |
| 107 |
} |
| 108 |
} else { |
| 109 |
if ( isset( $imported['error'] ) ) { |
| 110 |
$message = $imported['error']; |
| 111 |
} else { |
| 112 |
$message = __( 'There was an error importing form', 'formidable' ); |
| 113 |
} |
| 114 |
$response = array( |
| 115 |
'message' => $message, |
| 116 |
); |
| 117 |
|
| 118 |
}//end if |
| 119 |
|
| 120 |
$response = apply_filters( 'frm_xml_response', $response, compact( 'form', 'imported' ) ); |
| 121 |
|
| 122 |
echo wp_json_encode( $response ); |
| 123 |
wp_die(); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Make sure that the XML file we're trying to load is in fact an XML file, and that it's coming from our S3 bucket. |
| 128 |
* This is to make sure that the URL can't be exploited for a SSRF attack. |
| 129 |
* |
| 130 |
* @since 5.5.5 |
| 131 |
* @param string $url |
| 132 |
* |
| 133 |
* @return bool True on success, False on error. |
| 134 |
*/ |
| 135 |
private static function validate_xml_url( $url ) { |
| 136 |
return FrmAppHelper::validate_url_is_in_s3_bucket( $url, 'xml' ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* @since 4.06.02 |
| 141 |
* |
| 142 |
* @return mixed |
| 143 |
*/ |
| 144 |
private static function get_posted_form() { |
| 145 |
$form = FrmAppHelper::get_param( 'form', '', 'post', 'wp_unslash' ); |
| 146 |
if ( empty( $form ) ) { |
| 147 |
return $form; |
| 148 |
} |
| 149 |
$form = json_decode( $form, true ); |
| 150 |
return $form; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Get a different URL depending on the selection in the form. |
| 155 |
* |
| 156 |
* @since 4.06.02 |
| 157 |
* |
| 158 |
* @return void |
| 159 |
*/ |
| 160 |
private static function override_url( $form, &$url ) { |
| 161 |
$selected_form = self::get_selected_in_form( $form, 'form' ); |
| 162 |
if ( empty( $selected_form ) ) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
$selected_xml = isset( $form['xml'] ) && isset( $form['xml'][ $selected_form ] ) ? $form['xml'][ $selected_form ] : ''; |
| 167 |
if ( empty( $selected_xml ) || strpos( $selected_xml, 'http' ) !== 0 ) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
$url = $selected_xml; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* @since 4.06.02 |
| 176 |
* |
| 177 |
* @param array $form |
| 178 |
* @param string $value |
| 179 |
*/ |
| 180 |
private static function get_selected_in_form( $form, $value = 'form' ) { |
| 181 |
if ( ! empty( $form ) && ! empty( $form[ $value ] ) ) { |
| 182 |
return $form[ $value ]; |
| 183 |
} |
| 184 |
|
| 185 |
return ''; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* @since 4.06.02 |
| 190 |
* |
| 191 |
* @param array $form The posted form values. |
| 192 |
* @return array|null The array of created pages. |
| 193 |
*/ |
| 194 |
private static function create_pages_for_import( $form ) { |
| 195 |
if ( empty( $form['pages'] ) ) { |
| 196 |
return null; |
| 197 |
} |
| 198 |
|
| 199 |
$form_key = self::get_selected_in_form( $form, 'form' ); |
| 200 |
$view_keys = self::get_selected_in_form( $form, 'view' ); |
| 201 |
|
| 202 |
$page_ids = array(); |
| 203 |
foreach ( (array) $form['pages'] as $for => $name ) { |
| 204 |
if ( empty( $name ) ) { |
| 205 |
// Don't create a page if no title is given. |
| 206 |
continue; |
| 207 |
} |
| 208 |
|
| 209 |
if ( $for === 'view' ) { |
| 210 |
$item_key = is_array( $view_keys ) ? $view_keys[ $form_key ] : $view_keys; |
| 211 |
$shortcode = '[display-frm-data id=%1$s filter=limited]'; |
| 212 |
} elseif ( $for === 'form' ) { |
| 213 |
$item_key = $form_key; |
| 214 |
$shortcode = '[formidable id=%1$s]'; |
| 215 |
} else { |
| 216 |
$item_key = self::get_selected_in_form( $form, 'form' ); |
| 217 |
$shortcode = '[' . esc_html( $for ) . ' id=%1$s]'; |
| 218 |
} |
| 219 |
|
| 220 |
if ( empty( $item_key ) ) { |
| 221 |
// Don't create it if the shortcode won't show anything. |
| 222 |
continue; |
| 223 |
} |
| 224 |
|
| 225 |
$page_ids[ $for ] = wp_insert_post( |
| 226 |
array( |
| 227 |
'post_title' => $name, |
| 228 |
'post_type' => 'page', |
| 229 |
'post_content' => sprintf( $shortcode, $item_key ), |
| 230 |
) |
| 231 |
); |
| 232 |
}//end foreach |
| 233 |
|
| 234 |
return $page_ids; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Change the name of the last form that is not a child. |
| 239 |
* This will allow for lookup fields and embedded forms |
| 240 |
* since we redirect to the last form. |
| 241 |
* |
| 242 |
* @since 3.06 |
| 243 |
* |
| 244 |
* @param object $xml The values included in the XML. |
| 245 |
* @return void |
| 246 |
*/ |
| 247 |
private static function set_new_form_name( &$xml ) { |
| 248 |
if ( ! isset( $xml->form ) ) { |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
$name = FrmAppHelper::get_param( 'name', '', 'post', 'sanitize_text_field' ); |
| 253 |
$description = FrmAppHelper::get_param( 'desc', '', 'post', 'sanitize_textarea_field' ); |
| 254 |
if ( ! $name && ! $description ) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
// Get the main form ID. |
| 259 |
$set_name = 0; |
| 260 |
foreach ( $xml->form as $form ) { |
| 261 |
if ( empty( $form->parent_form_id ) ) { |
| 262 |
$set_name = (int) $form->id; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
foreach ( $xml->form as $form ) { |
| 267 |
// Maybe set the form name if this isn't a child form. |
| 268 |
if ( $set_name === (int) $form->id ) { |
| 269 |
$form->name = $name; |
| 270 |
$form->description = $description; |
| 271 |
} |
| 272 |
|
| 273 |
// Use a unique key to prevent editing existing form. |
| 274 |
$sanitized_form_name = sanitize_title( $form->name ); |
| 275 |
$form->form_key = FrmAppHelper::get_unique_key( $sanitized_form_name, 'frm_forms', 'form_key' ); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* @return void |
| 281 |
*/ |
| 282 |
public static function route() { |
| 283 |
$action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; |
| 284 |
$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ); |
| 285 |
FrmAppHelper::include_svg(); |
| 286 |
|
| 287 |
if ( 'import_xml' === $action ) { |
| 288 |
self::import_xml(); |
| 289 |
} elseif ( 'export_xml' === $action ) { |
| 290 |
self::export_xml(); |
| 291 |
} elseif ( apply_filters( 'frm_xml_route', true, $action ) ) { |
| 292 |
self::form(); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* @param string[] $errors |
| 298 |
* @param string $message |
| 299 |
* |
| 300 |
* @return void |
| 301 |
*/ |
| 302 |
public static function form( $errors = array(), $message = '' ) { |
| 303 |
$where = array( |
| 304 |
'status' => array( null, '', 'published' ), |
| 305 |
); |
| 306 |
$forms = FrmForm::getAll( $where, 'name' ); |
| 307 |
|
| 308 |
$export_types = array( |
| 309 |
'forms' => __( 'Forms', 'formidable' ), |
| 310 |
'items' => __( 'Entries', 'formidable' ), |
| 311 |
); |
| 312 |
$export_types = apply_filters( 'frm_xml_export_types', $export_types ); |
| 313 |
|
| 314 |
$export_format = array( |
| 315 |
'xml' => array( |
| 316 |
'name' => 'XML', |
| 317 |
'support' => 'forms', |
| 318 |
'count' => 'multiple', |
| 319 |
), |
| 320 |
'csv' => array( |
| 321 |
'name' => 'CSV', |
| 322 |
'support' => 'items', |
| 323 |
'count' => 'single', |
| 324 |
), |
| 325 |
); |
| 326 |
$export_format = apply_filters( 'frm_export_formats', $export_format ); |
| 327 |
|
| 328 |
include FrmAppHelper::plugin_path() . '/classes/views/xml/import_form.php'; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* @return void |
| 333 |
*/ |
| 334 |
public static function import_xml() { |
| 335 |
$errors = array(); |
| 336 |
$message = ''; |
| 337 |
|
| 338 |
$permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'import-xml', 'import-xml-nonce' ); |
| 339 |
if ( false !== $permission_error ) { |
| 340 |
$errors[] = $permission_error; |
| 341 |
self::form( $errors ); |
| 342 |
|
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 347 |
$has_file = ! empty( $_FILES['frm_import_file'] ) && ! empty( $_FILES['frm_import_file']['name'] ) && ! empty( $_FILES['frm_import_file']['size'] ) && (int) $_FILES['frm_import_file']['size'] > 0; |
| 348 |
if ( ! $has_file ) { |
| 349 |
$errors[] = __( 'Oops, you didn\'t select a file.', 'formidable' ); |
| 350 |
self::form( $errors ); |
| 351 |
|
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.NonceVerification.Missing |
| 356 |
$file = isset( $_FILES['frm_import_file']['tmp_name'] ) ? sanitize_option( 'upload_path', $_FILES['frm_import_file']['tmp_name'] ) : ''; |
| 357 |
|
| 358 |
if ( ! is_uploaded_file( $file ) ) { |
| 359 |
unset( $file ); |
| 360 |
$errors[] = __( 'The file does not exist, please try again.', 'formidable' ); |
| 361 |
self::form( $errors ); |
| 362 |
|
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
$export_format = array( |
| 367 |
'xml' => array( |
| 368 |
'name' => 'XML', |
| 369 |
'support' => 'forms', |
| 370 |
'count' => 'multiple', |
| 371 |
), |
| 372 |
); |
| 373 |
$export_format = apply_filters( 'frm_export_formats', $export_format ); |
| 374 |
|
| 375 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.NonceVerification.Missing |
| 376 |
$file_type = sanitize_option( 'upload_path', $_FILES['frm_import_file']['name'] ); |
| 377 |
$file_type = strtolower( pathinfo( $file_type, PATHINFO_EXTENSION ) ); |
| 378 |
if ( 'xml' !== $file_type && isset( $export_format[ $file_type ] ) ) { |
| 379 |
// allow other file types to be imported |
| 380 |
do_action( 'frm_before_import_' . $file_type ); |
| 381 |
|
| 382 |
return; |
| 383 |
} |
| 384 |
unset( $file_type ); |
| 385 |
|
| 386 |
if ( FrmXMLHelper::check_if_libxml_disable_entity_loader_exists() ) { |
| 387 |
$errors[] = __( 'XML import is not enabled on your server with the libxml_disable_entity_loader function.', 'formidable' ); |
| 388 |
self::form( $errors ); |
| 389 |
|
| 390 |
return; |
| 391 |
} |
| 392 |
|
| 393 |
$set_err = libxml_use_internal_errors( true ); |
| 394 |
$loader = FrmXMLHelper::maybe_libxml_disable_entity_loader( true ); |
| 395 |
|
| 396 |
$result = FrmXMLHelper::import_xml( $file ); |
| 397 |
FrmXMLHelper::parse_message( $result, $message, $errors ); |
| 398 |
|
| 399 |
unset( $file ); |
| 400 |
|
| 401 |
libxml_use_internal_errors( $set_err ); |
| 402 |
FrmXMLHelper::maybe_libxml_disable_entity_loader( $loader ); |
| 403 |
|
| 404 |
self::form( $errors, $message ); |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* @return void |
| 409 |
*/ |
| 410 |
public static function export_xml() { |
| 411 |
$error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'export-xml', 'export-xml-nonce' ); |
| 412 |
if ( ! empty( $error ) ) { |
| 413 |
wp_die( esc_html( $error ) ); |
| 414 |
} |
| 415 |
|
| 416 |
$ids = FrmAppHelper::get_post_param( 'frm_export_forms', array(), 'sanitize_text_field' ); |
| 417 |
$type = FrmAppHelper::get_post_param( 'type', array(), 'sanitize_text_field' ); |
| 418 |
$format = FrmAppHelper::get_post_param( 'format', 'xml', 'sanitize_title' ); |
| 419 |
|
| 420 |
if ( ! headers_sent() && ! $type ) { |
| 421 |
wp_redirect( esc_url_raw( admin_url( 'admin.php?page=formidable-import' ) ) ); |
| 422 |
die(); |
| 423 |
} |
| 424 |
|
| 425 |
if ( 'xml' === $format ) { |
| 426 |
self::generate_xml( $type, compact( 'ids' ) ); |
| 427 |
} elseif ( 'csv' === $format ) { |
| 428 |
self::generate_csv( compact( 'ids' ) ); |
| 429 |
} else { |
| 430 |
do_action( 'frm_export_format_' . $format, compact( 'ids' ) ); |
| 431 |
} |
| 432 |
|
| 433 |
wp_die(); |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* @param string[] $type |
| 438 |
* @param array $args |
| 439 |
* |
| 440 |
* @psalm-param array{ids?: mixed} $args |
| 441 |
* |
| 442 |
* @return void |
| 443 |
*/ |
| 444 |
public static function generate_xml( $type, $args = array() ) { |
| 445 |
global $wpdb; |
| 446 |
|
| 447 |
self::prepare_types_array( $type ); |
| 448 |
|
| 449 |
$tables = array( |
| 450 |
'items' => $wpdb->prefix . 'frm_items', |
| 451 |
'forms' => $wpdb->prefix . 'frm_forms', |
| 452 |
'posts' => $wpdb->posts, |
| 453 |
'styles' => $wpdb->posts, |
| 454 |
'actions' => $wpdb->posts, |
| 455 |
); |
| 456 |
|
| 457 |
$defaults = array( |
| 458 |
'ids' => false, |
| 459 |
); |
| 460 |
$args = wp_parse_args( $args, $defaults ); |
| 461 |
|
| 462 |
// Make sure ids are numeric. |
| 463 |
if ( is_array( $args['ids'] ) && ! empty( $args['ids'] ) ) { |
| 464 |
$args['ids'] = array_filter( $args['ids'], 'is_numeric' ); |
| 465 |
} |
| 466 |
|
| 467 |
$records = array(); |
| 468 |
|
| 469 |
foreach ( $type as $tb_type ) { |
| 470 |
$where = array(); |
| 471 |
$join = ''; |
| 472 |
$table = $tables[ $tb_type ]; |
| 473 |
|
| 474 |
$select = $table . '.id'; |
| 475 |
$query_vars = array(); |
| 476 |
|
| 477 |
switch ( $tb_type ) { |
| 478 |
case 'forms': |
| 479 |
// Add forms. |
| 480 |
if ( $args['ids'] ) { |
| 481 |
$where[] = array( |
| 482 |
'or' => 1, |
| 483 |
$table . '.id' => $args['ids'], |
| 484 |
$table . '.parent_form_id' => $args['ids'], |
| 485 |
); |
| 486 |
} else { |
| 487 |
$where[ $table . '.status !' ] = 'draft'; |
| 488 |
} |
| 489 |
break; |
| 490 |
case 'actions': |
| 491 |
$select = $table . '.ID'; |
| 492 |
$where['post_type'] = FrmFormActionsController::$action_post_type; |
| 493 |
if ( ! empty( $args['ids'] ) ) { |
| 494 |
$where['menu_order'] = $args['ids']; |
| 495 |
} |
| 496 |
break; |
| 497 |
case 'items': |
| 498 |
// $join = "INNER JOIN {$wpdb->prefix}frm_item_metas im ON ($table.id = im.item_id)"; |
| 499 |
if ( $args['ids'] ) { |
| 500 |
$where[ $table . '.form_id' ] = $args['ids']; |
| 501 |
} |
| 502 |
break; |
| 503 |
case 'styles': |
| 504 |
// Loop through all exported forms and get their selected style IDs. |
| 505 |
$frm_style = new FrmStyle(); |
| 506 |
$default_style = $frm_style->get_default_style(); |
| 507 |
$form_ids = $args['ids']; |
| 508 |
$style_ids = array(); |
| 509 |
foreach ( $form_ids as $form_id ) { |
| 510 |
$form_data = FrmForm::getOne( $form_id ); |
| 511 |
// For forms that have not been updated while running 2.0, check if custom_style is set. |
| 512 |
if ( isset( $form_data->options['custom_style'] ) ) { |
| 513 |
if ( 1 === absint( $form_data->options['custom_style'] ) ) { |
| 514 |
$style_ids[] = $default_style->ID; |
| 515 |
} else { |
| 516 |
$style_ids[] = $form_data->options['custom_style']; |
| 517 |
} |
| 518 |
} |
| 519 |
unset( $form_id, $form_data ); |
| 520 |
} |
| 521 |
$select = $table . '.ID'; |
| 522 |
$where['post_type'] = 'frm_styles'; |
| 523 |
|
| 524 |
// Only export selected styles. |
| 525 |
if ( ! empty( $style_ids ) ) { |
| 526 |
$where['ID'] = $style_ids; |
| 527 |
} |
| 528 |
break; |
| 529 |
default: |
| 530 |
$select = $table . '.ID'; |
| 531 |
$join = ' INNER JOIN ' . $wpdb->postmeta . ' pm ON (pm.post_id=' . $table . '.ID)'; |
| 532 |
$where['pm.meta_key'] = 'frm_form_id'; |
| 533 |
|
| 534 |
if ( empty( $args['ids'] ) ) { |
| 535 |
$where['pm.meta_value >'] = 1; |
| 536 |
} else { |
| 537 |
$where['pm.meta_value'] = $args['ids']; |
| 538 |
} |
| 539 |
}//end switch |
| 540 |
|
| 541 |
$records[ $tb_type ] = FrmDb::get_col( $table . $join, $where, $select ); |
| 542 |
unset( $tb_type ); |
| 543 |
}//end foreach |
| 544 |
|
| 545 |
$filename = self::get_file_name( $args, $records ); |
| 546 |
|
| 547 |
header( 'Content-Description: File Transfer' ); |
| 548 |
header( 'Content-Disposition: attachment; filename=' . $filename ); |
| 549 |
header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true ); |
| 550 |
|
| 551 |
echo '<?xml version="1.0" encoding="' . esc_attr( get_bloginfo( 'charset' ) ) . "\" ?>\n"; |
| 552 |
include FrmAppHelper::plugin_path() . '/classes/views/xml/xml.php'; |
| 553 |
} |
| 554 |
|
| 555 |
/** |
| 556 |
* Returns an array that has parent term slugs for the terms provided. |
| 557 |
* |
| 558 |
* @since 6.8.3 |
| 559 |
* @param array $terms |
| 560 |
* @return array |
| 561 |
*/ |
| 562 |
public static function get_parent_terms_slugs( $terms ) { |
| 563 |
$parent_term_ids = array_filter( array_unique( wp_list_pluck( $terms, 'parent' ) ) ); |
| 564 |
$parent_slugs = array(); |
| 565 |
|
| 566 |
if ( ! $parent_term_ids ) { |
| 567 |
return $parent_slugs; |
| 568 |
} |
| 569 |
|
| 570 |
$results = FrmDb::get_results( 'terms', array( 'term_id' => $parent_term_ids ), 'term_id, slug' ); |
| 571 |
$parent_slugs = wp_list_pluck( $results, 'slug', 'term_id' ); |
| 572 |
|
| 573 |
return $parent_slugs; |
| 574 |
} |
| 575 |
|
| 576 |
/** |
| 577 |
* @return void |
| 578 |
*/ |
| 579 |
private static function prepare_types_array( &$type ) { |
| 580 |
$type = (array) $type; |
| 581 |
if ( ! in_array( 'forms', $type, true ) && ( in_array( 'items', $type, true ) || in_array( 'posts', $type, true ) ) ) { |
| 582 |
// make sure the form is included if there are entries |
| 583 |
$type[] = 'forms'; |
| 584 |
} |
| 585 |
|
| 586 |
if ( in_array( 'forms', $type, true ) ) { |
| 587 |
// include actions with forms |
| 588 |
$type[] = 'actions'; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* Use a generic file name if multiple items are exported. |
| 594 |
* Use the nme of the form if only one form is exported. |
| 595 |
* |
| 596 |
* @since 3.06 |
| 597 |
* |
| 598 |
* @param array $args |
| 599 |
* @param array $records |
| 600 |
* @return string |
| 601 |
*/ |
| 602 |
private static function get_file_name( $args, $records ) { |
| 603 |
$has_one_form = ! empty( $records['forms'] ) && count( $args['ids'] ) === 1; |
| 604 |
if ( $has_one_form ) { |
| 605 |
// one form is being exported |
| 606 |
$selected_form_id = reset( $args['ids'] ); |
| 607 |
$filename = 'form-' . $selected_form_id . '.xml'; |
| 608 |
|
| 609 |
foreach ( $records['forms'] as $form_id ) { |
| 610 |
$filename = 'form-' . $form_id . '.xml'; |
| 611 |
if ( $selected_form_id === $form_id ) { |
| 612 |
$form = FrmForm::getOne( $form_id ); |
| 613 |
$filename = $form->name !== '' ? $form->name : $form->form_key; |
| 614 |
$filename = sanitize_title( $filename ) . '-form.xml'; |
| 615 |
break; |
| 616 |
} |
| 617 |
} |
| 618 |
} else { |
| 619 |
$sitename = sanitize_key( get_bloginfo( 'name' ) ); |
| 620 |
|
| 621 |
if ( ! empty( $sitename ) ) { |
| 622 |
$sitename .= '.'; |
| 623 |
} |
| 624 |
$filename = $sitename . 'formidable.' . gmdate( 'Y-m-d' ) . '.xml'; |
| 625 |
}//end if |
| 626 |
|
| 627 |
/** |
| 628 |
* @since 5.3 |
| 629 |
* |
| 630 |
* @param string $filename |
| 631 |
*/ |
| 632 |
return apply_filters( 'frm_xml_filename', $filename ); |
| 633 |
} |
| 634 |
|
| 635 |
/** |
| 636 |
* @param array $atts |
| 637 |
* |
| 638 |
* @return void |
| 639 |
*/ |
| 640 |
public static function generate_csv( $atts ) { |
| 641 |
$form_ids = $atts['ids']; |
| 642 |
if ( empty( $form_ids ) ) { |
| 643 |
wp_die( esc_html__( 'Please select a form', 'formidable' ) ); |
| 644 |
} |
| 645 |
self::csv( reset( $form_ids ) ); |
| 646 |
} |
| 647 |
|
| 648 |
/** |
| 649 |
* Export to CSV |
| 650 |
* |
| 651 |
* @since 2.0.19 |
| 652 |
* |
| 653 |
* @return void |
| 654 |
*/ |
| 655 |
public static function csv( $form_id = false, $search = '', $fid = '' ) { |
| 656 |
FrmAppHelper::permission_check( 'frm_view_entries' ); |
| 657 |
|
| 658 |
if ( ! $form_id ) { |
| 659 |
$form_id = FrmAppHelper::get_param( 'form', '', 'get', 'sanitize_text_field' ); |
| 660 |
$search = FrmAppHelper::get_param( ( isset( $_REQUEST['s'] ) ? 's' : 'search' ), '', 'get', 'sanitize_text_field' ); |
| 661 |
$fid = FrmAppHelper::get_param( 'fid', '', 'get', 'sanitize_text_field' ); |
| 662 |
} |
| 663 |
|
| 664 |
// Remove time limit to execute this function. |
| 665 |
if ( function_exists( 'set_time_limit' ) ) { |
| 666 |
set_time_limit( 0 ); |
| 667 |
} |
| 668 |
$mem_limit = str_replace( 'M', '', ini_get( 'memory_limit' ) ); |
| 669 |
if ( (int) $mem_limit < 256 ) { |
| 670 |
wp_raise_memory_limit(); |
| 671 |
} |
| 672 |
|
| 673 |
global $wpdb; |
| 674 |
|
| 675 |
$form = FrmForm::getOne( $form_id ); |
| 676 |
|
| 677 |
if ( ! $form ) { |
| 678 |
esc_html_e( 'Form not found.', 'formidable' ); |
| 679 |
wp_die(); |
| 680 |
} |
| 681 |
|
| 682 |
$form_id = $form->id; |
| 683 |
$form_cols = self::get_fields_for_csv_export( $form_id, $form ); |
| 684 |
|
| 685 |
$item_id = FrmAppHelper::get_param( 'item_id', 0, 'get', 'sanitize_text_field' ); |
| 686 |
if ( ! empty( $item_id ) ) { |
| 687 |
$item_id = explode( ',', $item_id ); |
| 688 |
} |
| 689 |
|
| 690 |
$query = array( |
| 691 |
'form_id' => $form_id, |
| 692 |
); |
| 693 |
|
| 694 |
if ( $item_id ) { |
| 695 |
$query['id'] = $item_id; |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* Allows the query to be changed for fetching the entry ids to include in the export |
| 700 |
* |
| 701 |
* $query is the array of options to be filtered. It includes form_id, and maybe id (array of entry ids), |
| 702 |
* and the search query. This should return an array, but it can be handled as a string as well. |
| 703 |
*/ |
| 704 |
$query = apply_filters( 'frm_csv_where', $query, compact( 'form_id', 'search', 'fid', 'item_id' ) ); |
| 705 |
|
| 706 |
$entry_ids = FrmDb::get_col( $wpdb->prefix . 'frm_items it', $query ); |
| 707 |
unset( $query ); |
| 708 |
|
| 709 |
if ( empty( $entry_ids ) ) { |
| 710 |
esc_html_e( 'There are no entries for that form.', 'formidable' ); |
| 711 |
} else { |
| 712 |
FrmCSVExportHelper::generate_csv( compact( 'form', 'entry_ids', 'form_cols' ) ); |
| 713 |
} |
| 714 |
|
| 715 |
wp_die(); |
| 716 |
} |
| 717 |
|
| 718 |
/** |
| 719 |
* Get the fields that should be included in the CSV export |
| 720 |
* |
| 721 |
* @since 2.0.19 |
| 722 |
* @since 5.0.16 function went from private to public. |
| 723 |
* |
| 724 |
* @param int $form_id |
| 725 |
* @param object $form |
| 726 |
* |
| 727 |
* @return array $csv_fields |
| 728 |
*/ |
| 729 |
public static function get_fields_for_csv_export( $form_id, $form ) { |
| 730 |
$csv_fields = FrmField::get_all_for_form( $form_id, '', 'include', 'include' ); |
| 731 |
$no_export_fields = FrmField::no_save_fields(); |
| 732 |
foreach ( $csv_fields as $k => $f ) { |
| 733 |
if ( in_array( $f->type, $no_export_fields, true ) ) { |
| 734 |
unset( $csv_fields[ $k ] ); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
return apply_filters( 'frm_fields_for_csv_export', $csv_fields, compact( 'form' ) ); |
| 739 |
} |
| 740 |
|
| 741 |
public static function allow_mime( $mimes ) { |
| 742 |
if ( ! isset( $mimes['csv'] ) ) { |
| 743 |
// allow csv files |
| 744 |
$mimes['csv'] = 'text/csv'; |
| 745 |
} |
| 746 |
|
| 747 |
if ( ! isset( $mimes['xml'] ) ) { |
| 748 |
// allow xml |
| 749 |
$mimes['xml'] = 'text/xml'; |
| 750 |
} |
| 751 |
|
| 752 |
return $mimes; |
| 753 |
} |
| 754 |
} |
| 755 |
|