コード例 #1
0
 public static function the_content($content)
 {
     global $post;
     $medium_post = Medium_Post::get_by_wp_id($post->ID);
     if ($medium_post->id && $medium_post->cross_link) {
         $content = Medium_View::render("content-cross-linked", array("content" => $content, "medium_post" => $medium_post));
     }
     return $content;
 }
コード例 #2
0
 /**
  * Creates a post on Medium.
  */
 public static function cross_post($post, $medium_post, $medium_user)
 {
     $tag_data = wp_get_post_tags($post->ID);
     $tags = array();
     foreach ($tag_data as $tag) {
         if ($tag->taxonomy == "post_tag") {
             $tags[] = $tag->name;
         }
     }
     $permalink = get_permalink($post->ID);
     $content = Medium_View::render("content-rendered-post", array("title" => $post->post_title, "content" => self::_prepare_content($post), "cross_link" => $medium_post->cross_link == "yes", "site_name" => get_bloginfo('name'), "permalink" => $permalink), true);
     $body = array("title" => $post->post_title, "content" => $content, "tags" => $tags, "contentFormat" => "html", "canonicalUrl" => $permalink, "license" => $medium_post->license, "publishStatus" => $medium_post->status);
     $data = json_encode($body);
     $headers = array("Authorization: Bearer " . $medium_user->token, "Content-Type: application/json", "Accept: application/json", "Accept-Charset: utf-8", "Content-Length: " . strlen($data));
     $curl = curl_init();
     curl_setopt_array($curl, array(CURLOPT_URL => "https://api.medium.com/v1/users/" . $medium_user->id . "/posts", CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => $headers, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $data));
     $result = curl_exec($curl);
     curl_close($curl);
     $payload = json_decode($result);
     if ($payload->errors) {
         $error = $payload->errors[0];
         throw new Exception($error->message, $error->code);
     }
     return $payload->data;
 }
コード例 #3
0
 /**
  * Creates a post on Medium.
  */
 public static function cross_post($post, $medium_post, $medium_user)
 {
     $tag_data = wp_get_post_terms($post->ID, array("post_tag", "slug"));
     $tags = array();
     $slugs = array();
     foreach ($tag_data as $tag) {
         if ($tag->taxonomy == "post_tag") {
             $tags[] = $tag->name;
         } elseif ($tag->taxonomy == "slug") {
             // For installations that have the custom taxonomy "slug", ensure that
             // these are are the head of the tag list.
             $slugs[] = $tag->name;
         }
     }
     $tags = array_values(array_unique(array_merge($slugs, $tags)));
     if (class_exists('CoAuthors_Guest_Authors')) {
         // Handle guest-authors if the CoAuthors Plus plugin is installed.
         $coauthors = get_coauthors($post->ID);
         $primary_author = $coauthors[0];
         if ($primary_author->type == "guest-author") {
             $medium_post->byline_name = $primary_author->display_name;
             $medium_post->byline_email = $primary_author->user_email;
         }
     }
     $permalink = get_permalink($post->ID);
     $content = Medium_View::render("content-rendered-post", array("title" => $post->post_title, "content" => self::_prepare_content($post), "cross_link" => $medium_post->cross_link == "yes", "site_name" => get_bloginfo('name'), "permalink" => $permalink, "byline" => $medium_post->byline_name), true);
     $body = array("title" => $post->post_title, "content" => $content, "tags" => $tags, "contentFormat" => "html", "canonicalUrl" => $permalink, "license" => $medium_post->license, "publishStatus" => $medium_post->status, "publishedAt" => mysql2date('c', $post->post_date), "notifyFollowers" => $medium_post->follower_notification == "yes");
     $data = json_encode($body);
     if ($medium_post->publication_id != NO_PUBLICATION) {
         $path = "/v1/publications/{$medium_post->publication_id}/posts";
     } else {
         $path = "/v1/users/{$medium_user->id}/posts";
     }
     try {
         $created_medium_post = self::_medium_request("POST", $path, $medium_user->token, $data, array("Content-Type" => "application/json"));
     } catch (Exception $e) {
         // Retry once if we got a timeout
         if ($e->getCode() == -2) {
             error_log("RETRYING POST {$post->ID} '{$post->post_title}' due to timeout, delaying...");
             sleep(5);
             $created_medium_post = self::_medium_request("POST", $path, $medium_user->token, $data, array("Content-Type" => "application/json"));
         } else {
             throw $e;
         }
     }
     $medium_post->id = $created_medium_post->id;
     // Don't derail the migration just because of a claims failure
     try {
         if ($medium_post->byline_email) {
             // Create a claim for the post, if necessary.
             self::create_post_claim($medium_post, $medium_user, $medium_post->byline_email);
         }
     } catch (Exception $e) {
         error_log("ERROR: Claim call failed {$e->getMessage}(), {$e->getCode}()");
     }
     return $created_medium_post;
 }
コード例 #4
0
 public static function shortcode_cross_link($attributes)
 {
     return Medium_View::render("content-cross-linked", array("url" => $attributes["url"]), true);
 }
コード例 #5
0
 /**
  * Creates a post on Medium.
  */
 public static function cross_post($post, $medium_post, $medium_user)
 {
     $tag_data = wp_get_post_tags($post->ID);
     $tags = array();
     foreach ($tag_data as $tag) {
         if ($tag->taxonomy == "post_tag") {
             $tags[] = $tag->name;
         }
     }
     $permalink = get_permalink($post->ID);
     $content = Medium_View::render("content-rendered-post", array("title" => $post->post_title, "content" => self::_prepare_content($post), "cross_link" => $medium_post->cross_link == "yes", "site_name" => get_bloginfo('name'), "permalink" => $permalink), true);
     $body = array("title" => $post->post_title, "content" => $content, "tags" => $tags, "contentFormat" => "html", "canonicalUrl" => $permalink, "license" => $medium_post->license, "publishStatus" => $medium_post->status, "publishedAt" => mysql2date('c', $post->post_date), "notifyFollowers" => $medium_post->follower_notification == "yes");
     $data = json_encode($body);
     $headers = array("Authorization" => "Bearer " . $medium_user->token, "Content-Type" => "application/json", "Accept" => "application/json", "Accept-Charset" => "utf-8");
     if ($medium_post->publication_id != NO_PUBLICATION) {
         $path = "/publications/{$medium_post->publication_id}/posts";
     } else {
         $path = "/users/{$medium_user->id}/posts";
     }
     $response = wp_remote_post("https://api.medium.com/v1{$path}", array("headers" => $headers, "body" => $data, "user-agent" => "MonkeyMagic/1.0"));
     return self::_handle_response($response);
 }