ID,'wp_4blit_post_status', true); $post_date = get_post_meta($post->ID, 'wp_4blit_post_date', true); $post_message = get_post_meta($post->ID,'wp_4blit_post_message', true); $t_date = date_i18n( get_option( 'date_format' ),strtotime($post_date)); if($post_status == 'published') { printf( __('Post published on %s', 'wp-4blit'), $t_date); } else { if($post_status == 'error') { /* Error */ printf( __('Error %s while publishing on %s', 'wp-4blit'), $post_message, $t_date); ?>

__('4blit')) ); } add_filter('manage_posts_columns' , 'wp_4blit_column'); /* ============================================================ */ /* */ /* PUBLISH POST */ /* */ /* =============================================================*/ function wp_4blit_excerpt($text) { $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); $text = strip_tags($text); $text = nl2br($text); $excerpt_length = apply_filters('excerpt_length', 55); $words = explode(' ', $text, $excerpt_length + 1); if (count($words) > $excerpt_length) { array_pop($words); array_push($words, '[...]'); $text = implode(' ', $words); } return $text; } function wp_4blit_do_publish($post) { $option_api_key = get_option( 'wp_4blit_api_key' ); if($option_api_key) { /* Get the post properties */ $author = $post->post_author; $post_title = $post->post_title; $post_excerpt = wp_4blit_excerpt($post->post_content); $post_author = get_the_author_meta('display_name', $author); $post_date = $post->post_date_gmt; $post_url = get_permalink($post->ID); /* Post have an image ? */ if(has_post_thumbnail($post->ID)) { $post_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); $post_image_url = $post_image[0]; } else { $post_image_url = ''; } /* Get post TAGS */ $tags_array = array(); $tags = wp_get_post_tags($post->ID); foreach($tags as $tag) { $tags_array[] = $tag->name; } $post_tags = implode(",",$tags_array); /* Build REST query */ $data = array('key' => $option_api_key, 'title' => $post_title, 'excerpt' => $post_excerpt, 'author' => $post_author, 'date' => $post_date, 'image_url' => $post_image_url, 'tags' => $post_tags, 'url' => $post_url, ); $headers = array('Accept' => 'application/json'); $body = Unirest\Request\Body::multipart($data); $result = Unirest\Request::post('https://www.4bl.it/rest/publish', $headers, $body); error_log(print_r($result, true)); /* [code] => 403 [raw_body] => { "error": { "code": 403, "message": "Error: this POST seems to be a duplicate" } */ return $result; } return false; } // add_action('transition_post_status', 'wp_4blit_post_publish', 10, 3); function wp_4blit_action_publish($new_status, $old_status, $post) { $do_publish = false; /* Check if this post was already published to avoid duplicates */ $check_is_publish = get_post_meta($post->ID,'wp_4blit_post_publish', true); error_log("FIRED: wp_4blit_action_publish($new_status,$old_status,$post->ID)"); if ($new_status == 'publish') { /* Check if this post was already published to avoid duplicates */ $post_status = get_post_meta($post->ID,'wp_4blit_post_status', true); if($post_status == 'published') { /* If already published, do nothing... */ } else { if(isset($_POST['wp_4blit_post_do_publish'])) { $do_publish = true; } } } /* Need to publish ? */ if($do_publish) { /* Publish and get the result */ $result = wp_4blit_do_publish($post); if($result) { $post_date = current_time('mysql'); $post_result = json_decode($result->raw_body,true); error_log(print_r($post_result, true)); if(isset($post_result['error'])) { // ERROR ! $post_result_code = $result->code; $post_result_message = $post_result['error']['message']; $post_is_published = false; } else if(isset($post_result['success'])) { // DONE ! $post_result_code = $result->code; $post_result_message = $post_result['success']; $post_is_published = true; } else { // Something wrong with network ? $post_is_published = false; } update_post_meta($post->ID, "wp_4blit_post_date","$post_date"); update_post_meta($post->ID, "wp_4blit_post_message", $post_result_message); if($post_is_published) { /* Success */ update_post_meta($post->ID, "wp_4blit_post_status","published"); add_action( 'admin_notices', 'wp_4blit_notice_publish_success' ); } else { update_post_meta($post->ID, "wp_4blit_post_status","error"); add_action( 'admin_notices', 'wp_4blit_notice_publish_error' ); } } else { add_action( 'admin_notices', 'wp_4blit_notice_publish_error' ); } } } add_action('transition_post_status', 'wp_4blit_action_publish', 10, 3); /* ============================================================ */ /* */ /* NOTICES */ /* */ /* =============================================================*/ function wp_4blit_notice_publish_success() { ?>