Plugin::POST_TYPE, 'post_status' => array( 'publish', 'draft', 'private', 'pending' ), 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC' ) );
echo '
';
return;
}
global $wpdb;
$p = $wpdb->prefix;
$rows = $wpdb->get_results( "SELECT id, title, status, created_at FROM {$p}fluentform_forms ORDER BY id ASC", ARRAY_A ); // phpcs:ignore
$count = array();
foreach ( (array) $wpdb->get_results( "SELECT form_id, COUNT(*) n FROM {$p}fluentform_submissions GROUP BY form_id", ARRAY_A ) as $c ) { // phpcs:ignore
$count[ (int) $c['form_id'] ] = (int) $c['n'];
}
$map = self::map();
echo '' . esc_html__( 'Each Fluent Forms form becomes a block form with its fields, conditional logic, notifications, confirmation, double opt-in, custom CSS/JS and landing page settings. Payment fields and integrations are not imported. Fluent Forms data is only read, never changed.', 'fieldwren' ) . '
';
echo '';
echo '' . ( self::ff_active() ? esc_html__( 'Fluent Forms is active, so its [fluentform] shortcode keeps working as before.', 'fieldwren' ) : esc_html__( 'Fluent Forms is not active: [fluentform id="…"] shortcodes on your pages show the imported forms.', 'fieldwren' ) ) . '
';
echo '';
}
/** Print and clear the last report. */
private static function print_report() {
$key = 'fwren_tools_report_' . get_current_user_id();
$rep = get_transient( $key );
if ( ! is_array( $rep ) ) {
return;
}
delete_transient( $key );
echo '' . esc_html( (string) ( $rep['title'] ?? '' ) ) . '
';
foreach ( (array) ( $rep['forms'] ?? array() ) as $f ) {
echo '
';
if ( ! empty( $f['new'] ) ) {
echo '' . esc_html( (string) $f['title'] ) . '';
} else {
echo esc_html( (string) $f['title'] );
}
if ( ! empty( $f['note'] ) ) {
echo ' — ' . esc_html( (string) $f['note'] );
}
if ( isset( $f['entries'] ) ) {
/* translators: 1: imported, 2: skipped */
echo ' · ' . esc_html( sprintf( __( '%1$d entries imported, %2$d skipped', 'fieldwren' ), (int) $f['entries'], (int) ( $f['skipped'] ?? 0 ) ) );
}
echo '
';
if ( ! empty( $f['unmapped'] ) ) {
echo '
';
foreach ( (array) $f['unmapped'] as $u ) {
echo '- ' . esc_html( (string) $u ) . '
';
}
echo '
';
}
echo '
';
}
echo '
';
}
/** Store a report for the next page view. */
private static function report( array $rep ) {
set_transient( 'fwren_tools_report_' . get_current_user_id(), $rep, 10 * MINUTE_IN_SECONDS );
wp_safe_redirect( admin_url( 'admin.php?page=fwren-tools' ) );
exit;
}
/** Capability + nonce. */
private static function guard( $action ) {
if ( ! current_user_can( 'fwren_manage_forms' ) || ! check_admin_referer( $action ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'fieldwren' ), 403 );
}
}
/* ------------------------------------------------------------------ *
* Export / import
* ------------------------------------------------------------------ */
/** Download JSON of the selected forms. */
public static function handle_export() {
self::guard( 'fwren_tools_export' );
$ids = array_filter( array_map( 'absint', (array) ( $_POST['ids'] ?? array() ) ) ); // phpcs:ignore WordPress.Security.NonceVerification -- guard() checked it.
self::download( $ids );
}
/** Row action export (?action=fwren_export_form&form_id=). */
public static function handle_row_export() {
if ( ! current_user_can( 'fwren_manage_forms' ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ?? '' ) ), 'fwren_export_form' ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'fieldwren' ), 403 );
}
self::download( array( absint( $_GET['form_id'] ?? 0 ) ) );
}
/** Send the export file. */
private static function download( array $ids ) {
$forms = array();
foreach ( $ids as $id ) {
if ( Form::exists( $id ) ) {
$forms[] = Form::export( $id );
}
}
if ( ! $forms ) {
self::report( array( 'error' => true, 'title' => __( 'Choose at least one form to export.', 'fieldwren' ) ) );
}
$name = 1 === count( $forms ) ? sanitize_file_name( strtolower( $forms[0]['title'] ? $forms[0]['title'] : 'form' ) ) : 'fieldwren';
nocache_headers();
header( 'Content-Type: application/json; charset=utf-8' );
header( 'Content-Disposition: attachment; filename="' . $name . '-' . gmdate( 'Y-m-d' ) . '.json"' );
echo wp_json_encode(
array(
'plugin' => 'fieldwren',
'version' => FWREN_VERSION,
'site' => home_url( '/' ),
'date' => gmdate( 'c' ),
'forms' => $forms,
),
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
);
exit;
}
/** Import a JSON file. */
public static function handle_import() {
self::guard( 'fwren_tools_import' );
$file = $_FILES['fwren_import_file'] ?? null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput, WordPress.Security.NonceVerification
if ( ! is_array( $file ) || ! empty( $file['error'] ) || empty( $file['tmp_name'] ) || ! is_uploaded_file( $file['tmp_name'] ) || (int) $file['size'] > 20 * MB_IN_BYTES ) {
self::report( array( 'error' => true, 'title' => __( 'Please upload a JSON export file (max 20 MB).', 'fieldwren' ) ) );
}
$pkg = json_decode( (string) file_get_contents( $file['tmp_name'] ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions
if ( ! is_array( $pkg ) ) {
self::report( array( 'error' => true, 'title' => __( 'The file is not valid JSON.', 'fieldwren' ) ) );
}
if ( isset( $pkg['forms'] ) && is_array( $pkg['forms'] ) ) {
$list = $pkg['forms'];
} elseif ( isset( $pkg['content'] ) || isset( $pkg['title'] ) ) {
$list = array( $pkg );
} else {
$list = array_values( array_filter( $pkg, 'is_array' ) );
}
$out = array();
foreach ( $list as $one ) {
if ( ! is_array( $one ) ) {
continue;
}
$one = self::strip_unsafe( $one );
$id = Form::import( $one );
$out[] = array( 'title' => (string) ( $one['title'] ?? '' ), 'new' => $id, 'note' => $id ? '' : __( 'skipped (empty)', 'fieldwren' ) );
}
/* translators: %d forms */
self::report( array( 'title' => sprintf( _n( '%d form imported.', '%d forms imported.', count( array_filter( wp_list_pluck( $out, 'new' ) ) ), 'fieldwren' ), count( array_filter( wp_list_pluck( $out, 'new' ) ) ) ), 'forms' => $out ) );
}
/** Custom JS needs unfiltered_html. */
private static function strip_unsafe( array $pkg ) {
if ( ! current_user_can( 'unfiltered_html' ) && isset( $pkg['settings']['layout']['custom_js'] ) ) {
$pkg['settings']['layout']['custom_js'] = '';
}
return $pkg;
}
/* ------------------------------------------------------------------ *
* Fluent Forms
* ------------------------------------------------------------------ */
/** @return bool Whether the FF tables exist. */
public static function ff_tables() {
global $wpdb;
$t = $wpdb->prefix . 'fluentform_forms';
return $t === $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $t ) ) ); // phpcs:ignore
}
/** @return bool Fluent Forms active. */
public static function ff_active() {
return defined( 'FLUENTFORM' ) || defined( 'FLUENTFORM_VERSION' );
}
/** @return array FF id → our id. */
public static function map() {
$m = get_option( self::MAP_OPTION, array() );
return is_array( $m ) ? array_map( 'intval', $m ) : array();
}
/** Import the selected FF forms. */
public static function handle_ff_import() {
self::guard( 'fwren_ff_import' );
if ( ! self::ff_tables() ) {
self::report( array( 'error' => true, 'title' => __( 'No Fluent Forms data found.', 'fieldwren' ) ) );
}
// phpcs:disable WordPress.Security.NonceVerification -- guard() checked it.
$ids = array_filter( array_map( 'absint', (array) ( $_POST['ff_ids'] ?? array() ) ) );
$entries = ! empty( $_POST['ff_entries'] );
$skip = ! empty( $_POST['ff_skip'] );
// phpcs:enable
if ( ! $ids ) {
self::report( array( 'error' => true, 'title' => __( 'Choose at least one form.', 'fieldwren' ) ) );
}
if ( function_exists( 'set_time_limit' ) ) {
@set_time_limit( 300 ); // phpcs:ignore
}
$out = array();
foreach ( $ids as $ff_id ) {
$out[] = self::import_ff_form( $ff_id, $entries, $skip );
}
/* translators: %d forms */
self::report( array( 'title' => sprintf( __( 'Fluent Forms import finished (%d forms).', 'fieldwren' ), count( $out ) ), 'forms' => $out ) );
}
/**
* Import one FF form.
*
* @param int $ff_id FF form id.
* @param bool $entries Import entries.
* @param bool $skip Skip when already imported.
* @return array Report row.
*/
public static function import_ff_form( $ff_id, $entries = false, $skip = true ) {
global $wpdb;
$p = $wpdb->prefix;
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$p}fluentform_forms WHERE id = %d", $ff_id ), ARRAY_A ); // phpcs:ignore
if ( ! $row ) {
/* translators: %d id */
return array( 'title' => sprintf( __( 'Fluent Forms #%d', 'fieldwren' ), $ff_id ), 'note' => __( 'not found', 'fieldwren' ) );
}
$meta = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, value FROM {$p}fluentform_form_meta WHERE form_id = %d AND meta_key <> 'revision' ORDER BY id ASC", $ff_id ), ARRAY_A ); // phpcs:ignore
$conv = new FF_Converter( $row, (array) $meta );
$blocks = $conv->blocks();
$settings = $conv->settings_override();
$map = self::map();
$existing = isset( $map[ $ff_id ] ) && Form::exists( $map[ $ff_id ] ) ? (int) $map[ $ff_id ] : 0;
if ( $existing && $skip ) {
$rep = array( 'title' => (string) $row['title'], 'new' => $existing, 'note' => __( 'already imported', 'fieldwren' ) );
} else {
$pkg = self::strip_unsafe(
array(
'title' => (string) $row['title'],
'content' => serialize_blocks( $blocks ),
'settings' => $settings,
'feeds' => array(),
)
);
$new = Form::import( $pkg );
if ( ! $new ) {
return array( 'title' => (string) $row['title'], 'note' => __( 'could not be created', 'fieldwren' ) );
}
if ( 'published' !== ( $row['status'] ?? 'published' ) ) {
wp_update_post( array( 'ID' => $new, 'post_status' => 'draft' ) );
}
update_post_meta( $new, '_fwren_ff_id', (int) $ff_id );
$map[ $ff_id ] = $new;
update_option( self::MAP_OPTION, $map, false );
$existing = $new;
$rep = array( 'title' => (string) $row['title'], 'new' => $new, 'unmapped' => array_values( array_unique( $conv->report ) ) );
}
if ( $entries ) {
list( $done, $skipped ) = self::import_entries( (int) $ff_id, $existing, $conv );
$rep['entries'] = $done;
$rep['skipped'] = $skipped;
}
do_action( 'fwren/importer/ff_form', $existing, $ff_id, $rep );
return $rep;
}
/**
* Copy FF submissions into entries.
*
* @return int[] [ imported, skipped ]
*/
private static function import_entries( $ff_id, $form_id, FF_Converter $conv ) {
global $wpdb;
$p = $wpdb->prefix;
$schema = Schema::get( $form_id );
$rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$p}fluentform_submissions WHERE form_id = %d ORDER BY id ASC LIMIT %d", $ff_id, self::ENTRY_LIMIT * 2 ), ARRAY_A ); // phpcs:ignore
$status = array( 'unread' => 'unread', 'read' => 'read', 'trashed' => 'trash', 'spam' => 'spam', 'unconfirmed' => 'unconfirmed', 'confirmed' => 'read' );
$done = 0;
$skip = 0;
foreach ( $rows as $r ) {
if ( $done >= self::ENTRY_LIMIT ) {
break;
}
if ( Entries::find_by_meta( '_ff_submission_id', (string) $r['id'] ) ) {
++$skip;
continue;
}
$resp = json_decode( (string) $r['response'], true );
$values = $conv->map_entry( is_array( $resp ) ? $resp : array(), $schema );
$id = Entries::insert(
array(
'form_id' => $form_id,
'status' => $status[ $r['status'] ?? 'read' ] ?? 'read',
'user_id' => (int) ( $r['user_id'] ?? 0 ),
'ip' => (string) ( $r['ip'] ?? '' ),
'user_agent' => substr( trim( (string) ( $r['browser'] ?? '' ) . ' ' . (string) ( $r['device'] ?? '' ) ), 0, 250 ),
'source_url' => (string) ( $r['source_url'] ?? '' ),
'payment_status' => (string) ( $r['payment_status'] ?? '' ),
'payment_total' => (int) ( $r['payment_total'] ?? 0 ),
'currency' => (string) ( $r['currency'] ?? '' ),
),
$values,
$schema
);
if ( ! $id ) {
++$skip;
continue;
}
$upd = array( 'is_favorite' => ! empty( $r['is_favourite'] ) ? 1 : 0 );
if ( ! empty( $r['created_at'] ) ) {
$upd['created_at'] = get_gmt_from_date( (string) $r['created_at'] );
}
if ( ! empty( $r['serial_number'] ) ) {
$upd['serial'] = (int) $r['serial_number'];
}
Entries::update( $id, $upd );
Entries::add_meta( $id, '_ff_submission_id', (string) $r['id'], $form_id );
++$done;
}
return array( $done, $skip );
}
/* ------------------------------------------------------------------ *
* [fluentform] compatibility
* ------------------------------------------------------------------ */
/** Register [fluentform] while Fluent Forms is inactive. */
public static function compat_shortcode() {
if ( self::ff_active() || shortcode_exists( 'fluentform' ) || ! self::map() ) {
return;
}
add_shortcode( 'fluentform', array( __CLASS__, 'render_compat' ) );
}
/** [fluentform id="X" title=""] → the imported form. */
public static function render_compat( $atts ) {
$atts = shortcode_atts( array( 'id' => 0, 'title' => '', 'theme' => '' ), (array) $atts, 'fluentform' );
$map = self::map();
$id = $map[ absint( $atts['id'] ) ] ?? 0;
if ( ! $id || ! Form::exists( $id ) ) {
return current_user_can( 'fwren_manage_forms' ) ? '' . esc_html__( 'This Fluent Forms form has not been imported.', 'fieldwren' ) . '
' : '';
}
return Renderer::render( (int) $id, array( 'title' => $atts['title'], 'theme' => '', 'class' => 'fwren-from-fluentform' ) );
}
}