nonce_ajax ) ? sanitize_text_field( wp_unslash( $params->nonce_ajax ) ) : '';
if ( ! wp_verify_nonce( $nonce_ajax, 'wpaf-script-nonce' ) ) {
status_header( 403 );
wp_send_json_error( __( 'Invalid or expired security token.', 'affcoupon' ) );
}
if ( ! current_user_can( 'publish_posts' ) ) {
status_header( 403 );
wp_send_json_error( __( 'Unauthorized import request.', 'affcoupon' ) );
}
$wpaf_coupon_data = isset( $params->post_var ) ? json_decode( wp_unslash( $params->post_var ) ) : null;
if ( ! is_object( $wpaf_coupon_data ) ) {
status_header( 400 );
wp_send_json_error( __( 'Invalid import request.', 'affcoupon' ) );
}
$wpaf_coupon_templates = array(
'Default',
'Template One',
'Template Two',
'Template Three',
'Template Four',
'Template Five',
'Template Six',
'Template Seven',
'Template Eight',
);
$coupon_count = isset( $wpaf_coupon_data->coupon_count ) ? absint( $wpaf_coupon_data->coupon_count ) : 0;
$coupon_title = isset( $wpaf_coupon_data->title ) ? sanitize_text_field( $wpaf_coupon_data->title ) : '';
$coupon_code = isset( $wpaf_coupon_data->coupon_code ) ? sanitize_text_field( $wpaf_coupon_data->coupon_code ) : '';
$coupon_link = isset( $wpaf_coupon_data->link ) ? esc_url_raw( $wpaf_coupon_data->link ) : '';
$coupon_description = isset( $wpaf_coupon_data->wpaf_description ) ? wp_kses_post( $wpaf_coupon_data->wpaf_description ) : '';
$coupon_discount_text = isset( $wpaf_coupon_data->discount_text ) ? sanitize_text_field( $wpaf_coupon_data->discount_text ) : '';
$coupon_hide_coupon = isset( $wpaf_coupon_data->hide_coupon ) ? sanitize_text_field( $wpaf_coupon_data->hide_coupon ) : '';
$default_coupon_template = isset( $wpaf_coupon_data->default_coupon_template ) ? sanitize_text_field( $wpaf_coupon_data->default_coupon_template ) : 'Default';
$default_coupon_template = in_array( $default_coupon_template, $wpaf_coupon_templates, true ) ? $default_coupon_template : 'Default';
$theme_color = isset( $wpaf_coupon_data->theme_color ) ? sanitize_hex_color( $wpaf_coupon_data->theme_color ) : '';
$category = isset( $wpaf_coupon_data->category ) ? sanitize_text_field( $wpaf_coupon_data->category ) : '';
$vendor = isset( $wpaf_coupon_data->vendor ) ? sanitize_text_field( $wpaf_coupon_data->vendor ) : '';
if ( $coupon_title != '' ) {
$args = array(
'post_type' => 'wpaf_coupons',
'post_title' => $coupon_title,
'post_content' => '',
'post_status' => 'publish',
);
$post_id = wp_insert_post( $args );
if ( ! is_wp_error( $post_id ) ) {
if ( ! empty( $wpaf_coupon_data->expiry_date ) ) {
$expiry_date = sanitize_text_field( $wpaf_coupon_data->expiry_date );
$expyry_data = wpaf_datetotime( $expiry_date ) ? wpaf_datetotime( $expiry_date ) : '';
}
if ( empty( $expyry_data ) ) {
$expyry_data = '';
}
if ( ! empty( $wpaf_coupon_data->expiry_time ) ) {
$expiry_time = strtotime( sanitize_text_field( $wpaf_coupon_data->expiry_time ) );
if ( ! empty( $expiry_time ) ) {
$expiry_time = gmdate( 'g:i a', $expiry_time );
}
}
if ( empty( $expiry_time ) ) {
$expiry_time = '';
}
add_post_meta( $post_id, 'coupon_details_coupon-type', 'Coupon', true );
add_post_meta( $post_id, 'coupon_details_coupon-code-text', $coupon_code, true );
add_post_meta( $post_id, 'coupon_details_link', $coupon_link, true );
add_post_meta( $post_id, 'coupon_details_description', $coupon_description, true );
add_post_meta( $post_id, 'coupon_details_discount-text', $coupon_discount_text, true );
add_post_meta( $post_id, 'coupon_details_show-expiration', 'Show', true );
add_post_meta( $post_id, 'coupon_details_expire-date', $expyry_data, true );
add_post_meta( $post_id, 'coupon_details_expire-time', $expiry_time, true );
add_post_meta( $post_id, 'coupon_details_hide-coupon', $coupon_hide_coupon, true );
add_post_meta( $post_id, 'coupon_details_coupon-template', $default_coupon_template, true );
if ( $default_coupon_template == 'Template Five' ):
add_post_meta( $post_id, 'coupon_details_template-theme-color', $theme_color );
elseif ( $default_coupon_template == 'Template Six' ):
add_post_meta( $post_id, 'coupon_details_template-theme-color', $theme_color );
elseif ( $default_coupon_template == 'Template Seven' ):
add_post_meta( $post_id, 'coupon_details_template-theme-color', $theme_color );
elseif ( $default_coupon_template == 'Template Eight' ):
add_post_meta( $post_id, 'coupon_details_template-theme-color', $theme_color );
endif;
if ( $category != '' && $category != ' ' ) {
wp_set_object_terms( $post_id, $category, 'wpaf_coupon_category' );
}
if ( $vendor != '' && $vendor != ' ' ) {
wp_set_object_terms( $post_id, $vendor, 'wpaf_coupon_vendor' );
}
} else {
wp_send_json( esc_html( $post_id->get_error_message() ) . esc_html__( ' | On Line Number1', 'affcoupon' ) . absint( $coupon_count ) . '
' );
}
} else {
wp_send_json( esc_html__( 'Error | On Line Number 2', 'affcoupon' ) . absint( $coupon_count ) . '
' );
}
$wpaf_coupon_data->success = 'success';
wp_send_json( $wpaf_coupon_data ); // sends all the data back to js
} // End of wpaf_import_process_php