/home/techb158/l2hypotheques.com/wp-content/plugins/wpforms-lite/src/Admin/Tools/Views
Edit: /home/techb158/l2hypotheques.com/wp-content/plugins/wpforms-lite/src/Admin/Tools/Views/Export.php (9635B)
verify_nonce()
) {
return;
}
if ( $_POST['action'] === 'export_form' && ! empty( $_POST['forms'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification
$this->process_form();
}
if ( $_POST['action'] === 'export_template' && ! empty( $_POST['form'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification
$this->process_template();
}
}
/**
* Checking user capability to view.
*
* @since 1.6.6
*
* @return bool
*/
public function check_capability() {
return wpforms_current_user_can( [ 'edit_forms', 'view_entries' ] );
}
/**
* Get available forms.
*
* @since 1.6.6
*
* @return array
*/
public function get_forms() {
$forms = wpforms()->obj( 'form' )->get( '', [ 'orderby' => 'title' ] );
return ! empty( $forms ) ? $forms : [];
}
/**
* Export view content.
*
* @since 1.6.6
*/
public function display() {
$this->forms = $this->get_forms();
if ( empty( $this->forms ) ) {
echo wpforms_render( 'admin/empty-states/no-forms' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return;
}
do_action( 'wpforms_admin_tools_export_top' );
$this->forms_export_block();
$this->form_template_export_block();
do_action( 'wpforms_admin_tools_export_bottom' );
}
/**
* Forms export block.
*
* @since 1.6.6
*/
private function forms_export_block() {
?>
'wpforms',
'nopaging' => true,
'post__in' => isset( $_POST['forms'] ) ? array_map( 'intval', $_POST['forms'] ) : [], //phpcs:ignore WordPress.Security.NonceVerification
]
);
foreach ( $forms as $form ) {
$export[] = wpforms_decode( $form->post_content );
}
ignore_user_abort( true );
wpforms_set_time_limit();
nocache_headers();
header( 'Content-Type: application/json; charset=utf-8' );
header( 'Content-Disposition: attachment; filename=wpforms-form-export-' . current_time( 'm-d-Y' ) . '.json' );
header( 'Expires: 0' );
echo wp_json_encode( $export );
exit;
}
/**
* Export template processing.
*
* @since 1.6.6
*/
private function process_template(): void {
// Nonce is checked in the caller: process() method.
//phpcs:ignore WordPress.Security.NonceVerification.Missing
$form_id = isset( $_POST['form'] ) ? absint( $_POST['form'] ) : 0;
$form_obj = wpforms()->obj( 'form' );
if ( ! $form_obj || ! $form_id ) {
return;
}
$form_data = $form_obj->get( $form_id, [ 'content_only' => true ] );
// Define basic data with strict validation.
$name = sanitize_text_field( $form_data['settings']['form_title'] ?? '' );
$desc = sanitize_text_field( $form_data['settings']['form_desc'] ?? '' );
$slug = sanitize_key( str_replace( [ ' ', '-' ], '_', trim( $name ) ) );
if ( ! $slug ) {
// Slug is always empty when the $form_data is not valid.
return;
}
$class = 'WPForms_Template_' . $slug;
$data = $this->get_template_data( $slug, $form_data );
// Build the final template string.
$this->template = <<
name = '{$name}';
// Template slug
\$this->slug = '{$slug}';
// Template description
\$this->description = '{$desc}';
// Template field and settings
\$this->data = {$data};
}
}
new {$class}();
endif;
EOT;
}
/**
* Get template data.
*
* @since 1.9.5
*
* @param string $slug Template slug.
* @param array|mixed $form_data Form data.
*
* @return string
*/
private function get_template_data( string $slug, $form_data ): string {
// Format template field and settings data.
$data = [];
$data['meta']['template'] = $slug;
$data['fields'] = isset( $form_data['fields'] ) && is_array( $form_data['fields'] )
? wpforms_array_remove_empty_strings( $form_data['fields'] )
: [];
$data['settings'] = isset( $form_data['settings'] ) && is_array( $form_data['settings'] )
? wpforms_array_remove_empty_strings( $form_data['settings'] )
: [];
$template_data = (string) var_export( $data, true ); //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
$template_data = str_replace( ' ', "\t", $template_data );
return preg_replace( '/([\t\r\n]+?)array/', 'array', $template_data );
}
}