post_type ) {
$actions['export'] = '' . __( 'Export', 'cartflows' ) . '';
}
return $actions;
}
/**
* Add menus
*
* @since 1.1.4
*/
function add_to_menus() {
add_submenu_page( 'edit.php?post_type=' . CARTFLOWS_FLOW_POST_TYPE, __( 'Flow Export', 'cartflows' ), __( 'Flow Export', 'cartflows' ), 'export', 'flow_exporter', array( $this, 'exporter_markup' ) );
add_submenu_page( 'edit.php?post_type=' . CARTFLOWS_FLOW_POST_TYPE, __( 'Flow Import', 'cartflows' ), __( 'Flow Import', 'cartflows' ), 'import', 'flow_importer', array( $this, 'importer_markup' ) );
}
/**
* Export flow with steps and its meta
*
* @since 1.1.4
*/
function export_flow() {
if ( ! ( isset( $_GET['post'] ) || isset( $_POST['post'] ) || ( isset( $_REQUEST['action'] ) && 'cartflows_export_flow' == $_REQUEST['action'] ) ) ) {
wp_die( __( 'No post to export has been supplied!', 'cartflows' ) );
}
if ( ! isset( $_GET['flow_export_nonce'] ) || ! wp_verify_nonce( $_GET['flow_export_nonce'], basename( __FILE__ ) ) ) {
return;
}
// Get the original post id.
$flow_id = ( isset( $_GET['post'] ) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
$flows = array();
$flows[] = $this->get_flow_export_data( $flow_id );
$flows = apply_filters( 'cartflows_export_data', $flows );
nocache_headers();
header( 'Content-Type: application/json; charset=utf-8' );
header( 'Content-Disposition: attachment; filename=cartflows-flow-' . $flow_id . '-' . date( 'm-d-Y' ) . '.json' );
header( 'Expires: 0' );
echo json_encode( $flows );
exit;
}
/**
* Export flow markup
*
* @since 1.1.4
*/
function exporter_markup() {
include_once CARTFLOWS_DIR . 'includes/exporter.php';
}
/**
* Import flow markup
*
* @since 1.1.4
*/
function importer_markup() {
include_once CARTFLOWS_DIR . 'includes/importer.php';
}
/**
* Export flow
*
* @since 1.1.4
*/
function export_json() {
if ( empty( $_POST['cartflows-action'] ) || 'export' != $_POST['cartflows-action'] ) {
return;
}
if ( ! wp_verify_nonce( $_POST['cartflows-action-nonce'], 'cartflows-action-nonce' ) ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$flows = $this->get_all_flow_export_data();
$flows = apply_filters( 'cartflows_export_data', $flows );
nocache_headers();
header( 'Content-Type: application/json; charset=utf-8' );
header( 'Content-Disposition: attachment; filename=cartflows-flow-export-' . date( 'm-d-Y' ) . '.json' );
header( 'Expires: 0' );
echo json_encode( $flows );
exit;
}
/**
* Get flow export data
*
* @since 1.1.4
*
* @param integer $flow_id Flow ID.
* @return array
*/
function get_flow_export_data( $flow_id ) {
$export_all = apply_filters( 'cartflows_export_all', false );
$valid_step_meta_keys = array(
'_wp_page_template',
'_thumbnail_id',
'classic-editor-remember',
);
$new_steps = array();
$steps = get_post_meta( $flow_id, 'wcf-steps', true );
if ( $steps ) {
foreach ( $steps as $key => $step ) {
// Add step post meta.
$new_all_meta = array();
$all_meta = get_post_meta( $step['id'] );
if ( is_array( $all_meta ) ) {
if ( $export_all ) {
foreach ( $all_meta as $meta_key => $value ) {
$new_all_meta[ $meta_key ] = maybe_unserialize( $value[0] );
}
} else {
foreach ( $all_meta as $meta_key => $value ) {
if ( substr( $meta_key, 0, strlen( 'wcf' ) ) === 'wcf' ) {
$new_all_meta[ $meta_key ] = maybe_unserialize( $value[0] );
} elseif ( in_array( $meta_key, $valid_step_meta_keys ) ) {
$new_all_meta[ $meta_key ] = maybe_unserialize( $value[0] );
}
}
}
}
// Add single step.
$step_data_arr = array(
'title' => get_the_title( $step['id'] ),
'type' => $step['type'],
'meta' => $new_all_meta,
'post_content' => '',
);
if ( $export_all ) {
$step_post_obj = get_post( $step['id'] );
$step_data_arr['post_content'] = $step_post_obj->post_content;
}
$new_steps[] = $step_data_arr;
}
}
// Add single flow.
return array(
'title' => get_the_title( $flow_id ),
'steps' => $new_steps,
);
}
/**
* Get all flow export data
*
* @since 1.1.4
*/
function get_all_flow_export_data() {
$query_args = array(
'post_type' => CARTFLOWS_FLOW_POST_TYPE,
// Query performance optimization.
'fields' => 'ids',
'no_found_rows' => true,
'posts_per_page' => -1,
);
$query = new WP_Query( $query_args );
$flows = array();
if ( $query->posts ) {
foreach ( $query->posts as $key => $post_id ) {
$flows[] = $this->get_flow_export_data( $post_id );
}
}
return $flows;
}
/**
* Import our exported file
*
* @since 1.1.4
*/
function import_json() {
if ( empty( $_POST['cartflows-action'] ) || 'import' != $_POST['cartflows-action'] ) {
return;
}
if ( ! wp_verify_nonce( $_POST['cartflows-action-nonce'], 'cartflows-action-nonce' ) ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$filename = $_FILES['file']['name'];
$file_info = explode( '.', $filename );
$extension = end( $file_info );
if ( 'json' != $extension ) {
wp_die( __( 'Please upload a valid .json file', 'cartflows' ) );
}
$file = $_FILES['file']['tmp_name'];
if ( empty( $file ) ) {
wp_die( __( 'Please upload a file to import', 'cartflows' ) );
}
// Retrieve the settings from the file and convert the JSON object to an array.
$flows = json_decode( file_get_contents( $file ), true );
if ( $flows ) {
foreach ( $flows as $key => $flow ) {
$flow_title = $flow['title'];
if ( post_exists( $flow['title'] ) ) {
$flow_title = $flow['title'] . ' Copy';
}
// Create post object.
$new_flow_args = array(
'post_type' => CARTFLOWS_FLOW_POST_TYPE,
'post_title' => $flow_title,
'post_status' => 'draft',
);
// Insert the post into the database.
$flow_id = wp_insert_post( $new_flow_args );
if ( $flow['steps'] ) {
foreach ( $flow['steps'] as $key => $step ) {
$new_step_id = wp_insert_post(
array(
'post_type' => CARTFLOWS_STEP_POST_TYPE,
'post_title' => $step['title'],
'post_status' => 'publish',
'meta_input' => $step['meta'],
'post_content' => isset( $step['post_content'] ) ? $step['post_content'] : '',
)
);
// Insert post meta.
update_post_meta( $new_step_id, 'wcf-flow-id', $flow_id );
$step_taxonomy = CARTFLOWS_TAXONOMY_STEP_TYPE;
$current_term = term_exists( $step['type'], $step_taxonomy );
// // Set type object.
$data = get_term( $current_term['term_id'], $step_taxonomy );
$step_slug = $data->slug;
wp_set_object_terms( $new_step_id, $data->slug, $step_taxonomy );
// Set type.
update_post_meta( $new_step_id, 'wcf-step-type', $data->slug );
// Set flow.
wp_set_object_terms( $new_step_id, 'flow-' . $flow_id, CARTFLOWS_TAXONOMY_STEP_FLOW );
CartFlows_Importer::get_instance()->set_step_to_flow( $flow_id, $new_step_id, $step['title'], $step_slug );
if ( isset( $step['post_content'] ) && ! empty( $step['post_content'] ) ) {
// Download and replace images.
$content = $this->get_content( $step['post_content'] );
// Update post content.
wp_update_post(
array(
'ID' => $new_step_id,
'post_content' => $content,
)
);
}
}
}
}
}
add_action( 'admin_notices', array( $this, 'imported_successfully' ) );
}
/**
* Download and Replace hotlink images
*
* @since x.x.x
*
* @param string $content Mixed post content.
* @return array Hotlink image array.
*/
function get_content( $content = '' ) {
$all_links = wp_extract_urls( $content );
$image_links = array();
$image_map = array();
// Not have any link.
if ( empty( $all_links ) ) {
return $content;
}
foreach ( $all_links as $key => $link ) {
if ( preg_match( '/\.(jpg|jpeg|png|gif)/i', $link ) ) {
$image_links[] = $link;
}
}
// Not have any image link.
if ( empty( $image_links ) ) {
return $content;
}
foreach ( $image_links as $key => $image_url ) {
// Download remote image.
$image = array(
'url' => $image_url,
'id' => rand( 000, 999 ),
);
$downloaded_image = CartFlows_Import_Image::get_instance()->import( $image );
// Old and New image mapping links.
$image_map[ $image_url ] = $downloaded_image['url'];
}
// Replace old image links with new image links.
foreach ( $image_map as $old_url => $new_url ) {
$content = str_replace( $old_url, $new_url, $content );
}
return $content;
}
/**
* Imported notice
*
* @since 1.1.4
*/
function imported_successfully() {
?>
files_manager->clear_cache();
}
}
/**
* JS Templates
*
* @since 1.0.0
*
* @return void
*/
function js_templates() {
// Loading Templates.
?>
post_type ) ) {
return;
}
wp_enqueue_script( 'cartflows-rest-api', CARTFLOWS_URL . 'assets/js/rest-api.js', array( 'jquery' ), CARTFLOWS_VER, true );
wp_enqueue_style( 'cartflows-import', CARTFLOWS_URL . 'assets/css/import.css', null, CARTFLOWS_VER, 'all' );
wp_style_add_data( 'cartflows-import', 'rtl', 'replace' );
wp_enqueue_script( 'cartflows-import', CARTFLOWS_URL . 'assets/js/import.js', array( 'jquery', 'wp-util', 'cartflows-rest-api', 'updates' ), CARTFLOWS_VER, true );
$localize_vars = array(
'_is_pro_active' => _is_cartflows_pro(),
// Flow and its rest fields.
'flow' => CARTFLOWS_FLOW_POST_TYPE,
'flow_fields' => array(
'id',
'title',
'flow_type',
'page_builder',
'flow_steps',
'licence_status',
'featured_image_url',
'featured_media', // @required for field `featured_image_url`.
),
// Flow type and rest fields.
'flow_type' => CARTFLOWS_TAXONOMY_FLOW_CATEGORY,
'flow_type_fields' => array(
'id',
'name',
'slug',
),
// Flow page builder and rest fields.
'flow_page_builder' => CARTFLOWS_TAXONOMY_FLOW_PAGE_BUILDER,
'flow_page_builder_fields' => array(
'id',
'name',
'slug',
),
// Step page builder and rest fields.
'step_page_builder' => CARTFLOWS_TAXONOMY_STEP_PAGE_BUILDER,
'step_page_builder_fields' => array(
'id',
'name',
'slug',
),
// Step and its rest fields.
'step' => CARTFLOWS_STEP_POST_TYPE,
'step_fields' => array(
'title',
'featured_image_url',
'featured_media', // @required for field `featured_image_url`.
'id',
'flow_type',
'step_type',
'page_builder',
'licence_status',
),
// Step type and its rest fields.
'step_type' => CARTFLOWS_TAXONOMY_STEP_TYPE,
'step_type_fields' => array(
'id',
'name',
'slug',
),
'domain_url' => CARTFLOWS_DOMAIN_URL,
'server_url' => CARTFLOWS_TEMPLATES_URL,
'server_rest_url' => CARTFLOWS_TEMPLATES_URL . 'wp-json/wp/v2/',
'site_url' => site_url(),
'import_url' => admin_url( 'edit.php?post_type=' . CARTFLOWS_FLOW_POST_TYPE . '&page=flow_importer' ),
'export_url' => admin_url( 'edit.php?post_type=' . CARTFLOWS_FLOW_POST_TYPE . '&page=flow_exporter' ),
'admin_url' => admin_url(),
'licence_args' => CartFlows_API::get_instance()->get_licence_args(),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'debug' => ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || isset( $_GET['debug'] ) ) ? true : false,
'required_plugins' => Cartflows_Helper::get_plugins_groupby_page_builders(),
'default_page_builder' => Cartflows_Helper::get_common_setting( 'default_page_builder' ),
);
// var_dump(Cartflows_Helper::get_common_setting( 'default_page_builder' ));
// wp_die( );
// Add thickbox.
add_thickbox();
wp_localize_script( 'cartflows-import', 'CartFlowsImportVars', $localize_vars );
wp_localize_script( 'cartflows-rest-api', 'CartFlowsImportVars', $localize_vars );
}
/**
* Load Template
*
* @since 1.0.0
*
* @hook cartflows_load_steps
* @return void
*/
function load_templates() {
check_ajax_referer( 'cf-load-steps', 'security' );
$args = ( isset( $_POST['args'] ) ) ? array_map( 'sanitize_text_field', $_POST['args'] ) : array();
$templates = CartFlows_API::get_instance()->get_templates( $args );
if ( $templates['templates_count'] ) {
wp_send_json_success( $templates );
} else {
wp_send_json_error( $templates );
}
wp_die();
}
/**
* Import.
*
* @since 1.0.0
*
* @hook wp_ajax_cartflows_import_flow_step
* @return void
*/
function import_flow() {
check_ajax_referer( 'cf-import-flow-step', 'security' );
$flow_id = isset( $_POST['flow_id'] ) ? intval( $_POST['flow_id'] ) : '';
$template_id = isset( $_POST['template_id'] ) ? intval( $_POST['template_id'] ) : '';
wcf()->logger->import_log( '------------------------------------' );
wcf()->logger->import_log( 'STARTED! Importing FLOW' );
wcf()->logger->import_log( '------------------------------------' );
wcf()->logger->import_log( '(✓) Creating new step from remote step [' . $template_id . '] for FLOW ' . get_the_title( $flow_id ) . ' [' . $flow_id . ']' );
$response = CartFlows_API::get_instance()->get_template( $template_id );
$post_content = isset( $response['data']['content']->rendered ) ? $response['data']['content']->rendered : '';
if ( 'divi' === Cartflows_Helper::get_common_setting( 'default_page_builder' ) ) {
if ( isset( $response['data']['divi_content'] ) && ! empty( $response['data']['divi_content'] ) ) {
$post_content = $response['data']['divi_content'];
}
}
if ( false === $response['success'] ) {
wcf()->logger->import_log( '(✕) Failed to fetch remote data.' );
wp_send_json_error( $response );
}
wcf()->logger->import_log( '(✓) Successfully getting remote step response ' . json_encode( $response ) );
$new_step_id = wp_insert_post(
array(
'post_type' => CARTFLOWS_STEP_POST_TYPE,
'post_title' => $response['title'],
'post_content' => $post_content,
'post_status' => 'publish',
)
);
if ( is_wp_error( $new_step_id ) ) {
wcf()->logger->import_log( '(✕) Failed to create new step for flow ' . $flow_id );
wp_send_json_error( $new_step_id );
}
if ( 'divi' === Cartflows_Helper::get_common_setting( 'default_page_builder' ) ) {
if ( isset( $response['data']['divi_content'] ) && ! empty( $response['data']['divi_content'] ) ) {
update_post_meta( $new_step_id, 'divi_content', $response['data']['divi_content'] );
}
}
/* Imported Step */
update_post_meta( $new_step_id, 'cartflows_imported_step', 'yes' );
wcf()->logger->import_log( '(✓) Created new step ' . '"' . $response['title'] . '" id ' . $new_step_id );
// insert post meta.
update_post_meta( $new_step_id, 'wcf-flow-id', $flow_id );
wcf()->logger->import_log( '(✓) Added flow ID ' . $flow_id . ' in post meta key wcf-flow-id.' );
/**
* Import & Set type.
*/
$term = isset( $response['data']['step_type'] ) ? $response['data']['step_type'] : '';
$term_slug = '';
if ( $term ) {
$taxonomy = CARTFLOWS_TAXONOMY_STEP_TYPE;
$term_exist = term_exists( $term->name, $taxonomy );
if ( empty( $term_exist ) ) {
$terms = array(
array(
'name' => $term->name,
),
);
Cartflows_Step_Post_Type::get_instance()->add_terms( $taxonomy, $terms );
wcf()->logger->import_log( '(✓) Created new term ' . $term->name );
}
$current_term = term_exists( $term->name, $taxonomy );
// Set type object.
$data = get_term( $current_term['term_id'], $taxonomy );
$term_slug = $data->slug;
$term_name = $data->name;
wp_set_object_terms( $new_step_id, $term_slug, CARTFLOWS_TAXONOMY_STEP_TYPE );
wcf()->logger->import_log( '(✓) Assigned existing term ' . $term_name . ' to the template ' . $new_step_id );
// Set type.
update_post_meta( $new_step_id, 'wcf-step-type', $term_slug );
wcf()->logger->import_log( '(✓) Updated term ' . $term_name . ' to the post meta wcf-step-type.' );
}
// Set flow.
wp_set_object_terms( $new_step_id, 'flow-' . $flow_id, CARTFLOWS_TAXONOMY_STEP_FLOW );
wcf()->logger->import_log( '(✓) Assigned flow step flow-' . $flow_id );
/**
* Update steps for the current flow.
*/
$flow_steps = get_post_meta( $flow_id, 'wcf-steps', true );
if ( ! is_array( $flow_steps ) ) {
$flow_steps = array();
}
$flow_steps[] = array(
'id' => $new_step_id,
'title' => $response['title'],
'type' => $term_slug,
);
update_post_meta( $flow_id, 'wcf-steps', $flow_steps );
wcf()->logger->import_log( '(✓) Updated flow steps post meta key \'wcf-steps\' ' . json_encode( $flow_steps ) );
// Import Post Meta.
self::import_post_meta( $new_step_id, $response );
wcf()->logger->import_log( '(✓) Importing step "' . get_the_title( $new_step_id ) . '" [' . $new_step_id . '] for FLOW "' . get_the_title( $flow_id ) . '" [' . $flow_id . ']' );
wcf()->logger->import_log( '------------------------------------' );
wcf()->logger->import_log( 'COMPLETE! Importing FLOW' );
wcf()->logger->import_log( '------------------------------------' );
do_action( 'cartflows_import_complete' );
wcf()->logger->import_log( '(✓) BATCH STARTED for step ' . $new_step_id . ' for Blog name \'' . get_bloginfo( 'name' ) . '\' (' . get_current_blog_id() . ')' );
// Batch Process.
do_action( 'cartflows_after_template_import', $new_step_id, $response );
/**
* End
*/
wp_send_json_success( $new_step_id );
}
/**
* Import Step.
*
* @since 1.0.0
* @hook wp_ajax_cartflows_step_import
*
* @return void
*/
function create_default_flow() {
check_ajax_referer( 'cf-default-flow', 'security' );
// Create post object.
$new_flow_post = array(
'post_content' => '',
'post_status' => 'publish',
'post_type' => CARTFLOWS_FLOW_POST_TYPE,
);
// Insert the post into the database.
$flow_id = wp_insert_post( $new_flow_post );
if ( is_wp_error( $flow_id ) ) {
wp_send_json_error( $flow_id->get_error_message() );
}
$flow_steps = array();
$steps_data = array(
'landing' => __( 'Landing Page', 'cartflows' ),
'checkout' => __( 'Checkout Page', 'cartflows' ),
'thankyou' => __( 'Thank You Page', 'cartflows' ),
);
foreach ( $steps_data as $slug => $title ) {
$post_content = '';
switch ( $slug ) {
case 'checkout':
$post_content = '';
break;
case 'thankyou':
$post_content = '';
break;
default:
$post_content = '';
break;
}
$step_id = wp_insert_post(
array(
'post_type' => CARTFLOWS_STEP_POST_TYPE,
'post_title' => $title,
'post_content' => $post_content,
'post_status' => 'publish',
)
);
if ( is_wp_error( $step_id ) ) {
wp_send_json_error( $step_id->get_error_message() );
}
if ( $step_id ) {
$flow_steps[] = array(
'id' => $step_id,
'title' => $title,
'type' => $slug,
);
// insert post meta.
update_post_meta( $step_id, 'wcf-flow-id', $flow_id );
update_post_meta( $step_id, 'wcf-step-type', $slug );
wp_set_object_terms( $step_id, $slug, CARTFLOWS_TAXONOMY_STEP_TYPE );
wp_set_object_terms( $step_id, 'flow-' . $flow_id, CARTFLOWS_TAXONOMY_STEP_FLOW );
}
}
update_post_meta( $flow_id, 'wcf-steps', $flow_steps );
wp_send_json_success( $flow_id );
}
/**
* Create Flow
*
* @return void
*/
function create_flow() {
check_ajax_referer( 'cf-create-flow', 'security' );
// Create post object.
$new_flow_post = array(
'post_content' => '',
'post_status' => 'publish',
'post_type' => CARTFLOWS_FLOW_POST_TYPE,
);
// Insert the post into the database.
$flow_id = wp_insert_post( $new_flow_post );
if ( is_wp_error( $flow_id ) ) {
wp_send_json_error( $flow_id->get_error_message() );
}
/* Imported Flow */
update_post_meta( $flow_id, 'cartflows_imported_flow', 'yes' );
wp_send_json_success( $flow_id );
}
/**
* Create Step
*
* @return void
*/
function import_step() {
check_ajax_referer( 'cf-step-import', 'security' );
$template_id = isset( $_POST['template_id'] ) ? intval( $_POST['template_id'] ) : '';
$flow_id = isset( $_POST['flow_id'] ) ? intval( $_POST['flow_id'] ) : '';
$step_title = isset( $_POST['step_title'] ) ? sanitize_text_field( $_POST['step_title'] ) : '';
$step_type = isset( $_POST['step_type'] ) ? sanitize_title( $_POST['step_type'] ) : '';
$step_custom_title = isset( $_POST['step_custom_title'] ) ? sanitize_title( $_POST['step_custom_title'] ) : $step_title;
$cartflow_meta = Cartflows_Flow_Meta::get_instance();
$post_id = $cartflow_meta->create_step( $flow_id, $step_type, $step_custom_title );
wcf()->logger->import_log( '------------------------------------' );
wcf()->logger->import_log( 'STARTED! Importing STEP' );
wcf()->logger->import_log( '------------------------------------' );
if ( empty( $template_id ) || empty( $post_id ) ) {
/* translators: %s: template ID */
$data = sprintf( __( 'Invalid template id %1$s or post id %2$s.', 'cartflows' ), $template_id, $post_id );
wcf()->logger->import_log( $data );
wp_send_json_error( $data );
}
wcf()->logger->import_log( 'Remote Step ' . $template_id . ' for local flow "' . get_the_title( $post_id ) . '" [' . $post_id . ']' );
$response = CartFlows_API::get_instance()->get_template( $template_id );
if ( 'divi' === Cartflows_Helper::get_common_setting( 'default_page_builder' ) ) {
if ( isset( $response['data']['divi_content'] ) && ! empty( $response['data']['divi_content'] ) ) {
update_post_meta( $post_id, 'divi_content', $response['data']['divi_content'] );
wp_update_post(
array(
'ID' => $post_id,
'post_content' => $response['data']['divi_content'],
)
);
}
}
/* Imported Step */
update_post_meta( $post_id, 'cartflows_imported_step', 'yes' );
// Import Post Meta.
self::import_post_meta( $post_id, $response );
do_action( 'cartflows_import_complete' );
// Batch Process.
do_action( 'cartflows_after_template_import', $post_id, $response );
wcf()->logger->import_log( '------------------------------------' );
wcf()->logger->import_log( 'COMPLETE! Importing Step' );
wcf()->logger->import_log( '------------------------------------' );
wp_send_json_success( $post_id );
}
/**
* Import Step.
*
* @since 1.0.0
* @hook wp_ajax_cartflows_step_create_blank
*
* @return void
*/
function step_create_blank() {
check_ajax_referer( 'cf-step-create-blank', 'security' );
$flow_id = isset( $_POST['flow_id'] ) ? intval( $_POST['flow_id'] ) : '';
$step_type = isset( $_POST['step_type'] ) ? sanitize_text_field( $_POST['step_type'] ) : '';
$step_title = isset( $_POST['step_title'] ) ? sanitize_text_field( $_POST['step_title'] ) : '';
if ( empty( $flow_id ) || empty( $step_type ) ) {
/* translators: %s: flow ID */
$data = sprintf( __( 'Invalid flow id %1$s OR step type %2$s.', 'cartflows' ), $flow_id, $step_type );
wcf()->logger->import_log( $data );
wp_send_json_error( $data );
}
wcf()->logger->import_log( '------------------------------------' );
wcf()->logger->import_log( 'STARTED! Creating Blank STEP for Flow ' . $flow_id );
$step_type_title = str_replace( '-', ' ', $step_type );
$step_type_slug = strtolower( str_replace( '-', ' ', $step_type ) );
$new_step_id = wp_insert_post(
array(
'post_type' => CARTFLOWS_STEP_POST_TYPE,
'post_title' => $step_title,
'post_content' => '',
'post_status' => 'publish',
)
);
// insert post meta.
update_post_meta( $new_step_id, 'wcf-flow-id', $flow_id );
$taxonomy = CARTFLOWS_TAXONOMY_STEP_TYPE;
$term_exist = term_exists( $step_type_title, $taxonomy );
if ( empty( $term_exist ) ) {
$terms = array(
array(
'name' => $step_type_title,
),
);
Cartflows_Step_Post_Type::get_instance()->add_terms( $taxonomy, $terms );
wcf()->logger->import_log( '(✓) Created new term ' . $step_type_title );
}
$current_term = term_exists( $step_type_title, $taxonomy );
// Set type object.
$data = get_term( $current_term['term_id'], $taxonomy );
$step_slug = $data->slug;
wp_set_object_terms( $new_step_id, $data->slug, CARTFLOWS_TAXONOMY_STEP_TYPE );
wcf()->logger->import_log( '(✓) Assigned existing term ' . $step_type_title . ' to the template ' . $new_step_id );
// Set type.
update_post_meta( $new_step_id, 'wcf-step-type', $data->slug );
wcf()->logger->import_log( '(✓) Updated term ' . $data->name . ' to the post meta wcf-step-type.' );
// Set flow.
wp_set_object_terms( $new_step_id, 'flow-' . $flow_id, CARTFLOWS_TAXONOMY_STEP_FLOW );
wcf()->logger->import_log( '(✓) Assigned flow step flow-' . $flow_id );
CartFlows_Importer::get_instance()->set_step_to_flow( $flow_id, $new_step_id, $step_type_title, $step_slug );
wcf()->logger->import_log( 'COMPLETE! Creating Blank STEP for Flow ' . $flow_id );
wcf()->logger->import_log( '------------------------------------' );
wp_send_json_success( $new_step_id );
}
/**
* Import Post Meta
*
* @since 1.0.0
*
* @param integer $post_id Post ID.
* @param array $response Post meta.
* @return void
*/
public static function import_post_meta( $post_id, $response ) {
$metadata = (array) $response['post_meta'];
foreach ( $metadata as $meta_key => $meta_value ) {
$meta_value = isset( $meta_value[0] ) ? $meta_value[0] : '';
if ( $meta_value ) {
if ( is_serialized( $meta_value, true ) ) {
$raw_data = maybe_unserialize( stripslashes( $meta_value ) );
} elseif ( is_array( $meta_value ) ) {
$raw_data = json_decode( stripslashes( $meta_value ), true );
} else {
$raw_data = $meta_value;
}
if ( '_elementor_data' === $meta_key ) {
if ( is_array( $raw_data ) ) {
$raw_data = wp_slash( json_encode( $raw_data ) );
} else {
$raw_data = wp_slash( $raw_data );
}
}
if ( '_elementor_data' !== $meta_key && '_elementor_draft' !== $meta_key && '_fl_builder_data' !== $meta_key && '_fl_builder_draft' !== $meta_key ) {
if ( is_array( $raw_data ) ) {
wcf()->logger->import_log( '(✓) Added post meta ' . $meta_key . ' | ' . json_encode( $raw_data ) );
} else {
if ( ! is_object( $raw_data ) ) {
wcf()->logger->import_log( '(✓) Added post meta ' . $meta_key . ' | ' . $raw_data );
}
}
}
update_post_meta( $post_id, $meta_key, $raw_data );
}
}
}
/**
* Import Template for Elementor
*
* @since 1.0.0
*
* @param integer $post_id Post ID.
* @param array $response Post meta.
* @param array $page_build_data Page build data.
* @return void
*/
public static function import_template_elementor( $post_id, $response, $page_build_data ) {
if ( ! is_plugin_active( 'elementor/elementor.php' ) ) {
$data = __( 'Elementor is not activated. Please activate plugin Elementor Page Builder to import the step.', 'cartflows' );
wcf()->logger->import_log( $data );
wp_send_json_error( $data );
}
require_once CARTFLOWS_DIR . 'classes/batch-process/class-cartflows-importer-elementor.php';
wcf()->logger->import_log( '# Started "importing page builder data" for step ' . $post_id );
$obj = new \Elementor\TemplateLibrary\CartFlows_Importer_Elementor();
$obj->import_single_template( $post_id );
wcf()->logger->import_log( '# Complete "importing page builder data" for step ' . $post_id );
}
/**
* Supported post types
*
* @since 1.0.0
*
* @return array Supported post types.
*/
public static function supported_post_types() {
return apply_filters(
'cartflows_supported_post_types',
array(
CARTFLOWS_FLOW_POST_TYPE,
)
);
}
/**
* Check supported post type
*
* @since 1.0.0
*
* @param string $post_type Post type.
* @return boolean Supported post type status.
*/
public static function is_supported_post( $post_type = '' ) {
if ( in_array( $post_type, self::supported_post_types() ) ) {
return true;
}
return false;
}
/**
* Set steps to the flow
*
* @param integer $flow_id Flow ID.
* @param integer $new_step_id New step ID.
* @param string $step_title Flow Type.
* @param string $step_slug Flow Type.
*/
function set_step_to_flow( $flow_id, $new_step_id, $step_title, $step_slug ) {
// Update steps for the current flow.
$flow_steps = get_post_meta( $flow_id, 'wcf-steps', true );
if ( ! is_array( $flow_steps ) ) {
$flow_steps = array();
}
$flow_steps[] = array(
'id' => $new_step_id,
'title' => $step_title,
'type' => $step_slug,
);
update_post_meta( $flow_id, 'wcf-steps', $flow_steps );
wcf()->logger->import_log( '(✓) Updated flow steps post meta key \'wcf-steps\' ' . json_encode( $flow_steps ) );
}
/**
* Localize variables in admin
*
* @param array $vars variables.
*/
function localize_vars( $vars ) {
$ajax_actions = array(
'cf_step_import',
'cf_load_steps',
'cf_create_flow',
'cf_default_flow',
'cf_step_create_blank',
'cf_import_flow_step',
);
foreach ( $ajax_actions as $action ) {
$vars[ $action . '_nonce' ] = wp_create_nonce( str_replace( '_', '-', $action ) );
}
return $vars;
}
/**
* Ajax action to activate plugin
*/
public function activate_plugin() {
$plugin_init = isset( $_POST['plugin_init'] ) ? sanitize_text_field( $_POST['plugin_init'] ) : '';
$activate = activate_plugin( $plugin_init, '', false, true );
if ( is_wp_error( $activate ) ) {
wp_send_json_error(
array(
'success' => false,
'message' => $activate->get_error_message(),
'init' => $plugin_init,
)
);
}
wp_send_json_success(
array(
'success' => true,
'message' => __( 'Plugin Successfully Activated', 'cartflows' ),
'init' => $plugin_init,
)
);
}
}
/**
* Initialize class object with 'get_instance()' method
*/
CartFlows_Importer::get_instance();
endif;