Powered By Blogger

Monday, September 17, 2018

Create POST REST API in wordpress and send request by cURL and cURL not wait for response when post publish send email


//Send Email on Publish Post
add_action('transition_post_status','st_send_email', 10, 3 );

function st_set_html_content_type() {
return 'text/html';
}

//send notification e-mail on story publish
function st_send_email($new_status, $old_status, $post){

if ( 'publish' !== $new_status or 'publish' === $old_status or 'trash' === $old_status )
        return;

    if ( 'post' !== $post->post_type )
        return; // restrict the filter to a specific post type

$getpage= site_url().'/wp-json/gic/v1/subscriber';
$c = curl_init($getpage);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $post);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($c, CURLOPT_TIMEOUT, 1);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, false);
curl_setopt($c, CURLOPT_FORBID_REUSE, true);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($c, CURLOPT_DNS_CACHE_TIMEOUT, 10);
curl_setopt($c, CURLOPT_FRESH_CONNECT, true);

$html = curl_exec($c);
curl_close($c);

return true;
}


/******************************* Register Custom endpoint API ***************************************/
function gic_register_subscriber_route() {
    register_rest_route('gic/v1', '/subscriber', [
        'methods' => WP_REST_Server::CREATABLE,
        'callback' => 'gic_ajax_subscribe_email',
    ]);
}

add_action( 'rest_api_init', 'gic_register_subscriber_route');



/**
 * Use the request data to find the posts we
 * are looking for and prepare them for use
 * on the front end.
 */
function gic_ajax_subscribe_email( $request ) {

$post = $request->get_params();

$post_ID = $post['ID'];
global $wpdb;
$error = 0;

$share_with_subscriber = get_post_meta( $post_ID, 'share_with_subscriber', true );
if ($share_with_subscriber == 0) {

$send_email = get_option( 'st_category_email_send_email' );
$from_name = get_option( 'st_category_email_from_name' );

//From Name <Email>
$headers[] = 'From: '.$from_name.' <'.$send_email.'>';

$post = get_post($post_ID);
// Post Title
$subject = $post->post_title;
$post_detail['post_title'] = $post->post_title;
//Post Link
$post_detail['post_link'] = get_permalink( $post_ID );
//Blog Name
$post_detail['blog_name']  = get_option( 'st_category_email_from_name' );
//Blog Name
$post_detail['blog_url']  = get_bloginfo('url');

// Post Content
$post_content = strip_shortcodes( $post->post_content );
$post_content = apply_filters( 'the_content', $post_content );
$post_content = str_replace(']]>', ']]&gt;', $post_content);
$excerpt_length = apply_filters( 'excerpt_length', 55 );
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
$post_content = wp_trim_words( $post_content, $excerpt_length, $excerpt_more );

$post_detail['post_excerpt'] = $post_content;

$post_detail['thumbnail'] = get_the_post_thumbnail_url( $post_ID, medium);

$post_detail['logo']  = get_option('email_logo');

$post_detail['heading_clr']  = get_option('heading_color');

$post_detail['heading_bg']  = get_option('heading_bg');

$post_detail['text_clr']  = get_option('text_color');

// get header for email template
$part = 'header.php';
if ( file_exists( WP_CATEGORY_THEME_TEMPLATE_DIR . $part ) ) {
$header = WP_CATEGORY_THEME_TEMPLATE_DIR . $part;
}
else {
$header = st_apply_template($post_detail,'templates/header.php');
}

// get footer for email template
$part = 'footer.php';
if ( file_exists( WP_CATEGORY_THEME_TEMPLATE_DIR . $part ) ) {
$footer = WP_CATEGORY_THEME_TEMPLATE_DIR . $part;
}
else {
$footer = st_apply_template($post_detail,'templates/footer.php');
}

// get content for email template
$part = 'template1.php';
if ( file_exists( WP_CATEGORY_THEME_TEMPLATE_DIR . $part ) ) {
$content = WP_CATEGORY_THEME_TEMPLATE_DIR . $part;
}
else {
$content = st_apply_template($post_detail,'templates/template1.php');
}

//Get all the email address who have subscribed to this categories
$table_result = $wpdb->get_results("SELECT * FROM ".st_email_table." where status=1");
add_filter('wp_mail_content_type', 'st_set_html_content_type');

foreach ( $table_result as $table_row )
{
//get e-mail address from post meta field
$email_address = $table_row->st_email;

if(wp_mail($email_address, $subject, $content, $headers)){
//mail sent!
} else {
$error = 1;
}

}

}

    if( $error==1 ) {
       return new WP_Error( 'front_end_subscribe_email', 'Email Not send');
    }else{
    return true;
    }

}




?>

No comments:

Post a Comment