コード例 #1
0
ファイル: discourse.php プロジェクト: discourse/wp-discourse
 function sync_to_discourse_work($postid, $title, $raw)
 {
     $discourse_id = get_post_meta($postid, 'discourse_post_id', true);
     $options = self::get_plugin_options();
     $post = get_post($postid);
     $use_full_post = isset($options['full-post-content']) && intval($options['full-post-content']) == 1;
     if ($use_full_post) {
         $excerpt = $raw;
     } else {
         $excerpt = apply_filters('the_content', $raw);
         $excerpt = wp_trim_words($excerpt, $options['custom-excerpt-length']);
     }
     if (function_exists('discourse_custom_excerpt')) {
         $excerpt = discourse_custom_excerpt($postid);
     }
     // trim to keep the Discourse markdown parser from treating this as code.
     $baked = trim(Templates\HTMLTemplates::publish_format_html());
     $baked = str_replace("{excerpt}", $excerpt, $baked);
     $baked = str_replace("{blogurl}", get_permalink($postid), $baked);
     $author_id = $post->post_author;
     $author = get_the_author_meta('display_name', $author_id);
     $baked = str_replace("{author}", $author, $baked);
     $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($postid), 'thumbnail');
     $baked = str_replace("{thumbnail}", "![image](" . $thumb['0'] . ")", $baked);
     $featured = wp_get_attachment_image_src(get_post_thumbnail_id($postid), 'full');
     $baked = str_replace("{featuredimage}", "![image](" . $featured['0'] . ")", $baked);
     $username = get_the_author_meta('discourse_username', $post->post_author);
     if (!$username || strlen($username) < 2) {
         $username = $options['publish-username'];
     }
     // Get publish category of a post
     $publish_post_category = get_post_meta($post->ID, 'publish_post_category', true);
     $publish_post_category = $post->publish_post_category;
     $default_category = isset($options['publish-category']) ? $options['publish-category'] : '';
     $category = isset($publish_post_category) ? $publish_post_category : $default_category;
     if ($category === '') {
         $categories = get_the_category();
         foreach ($categories as $category) {
             if (in_category($category->name, $postid)) {
                 $category = $category->name;
                 break;
             }
         }
     }
     if (!$discourse_id > 0) {
         $data = array('wp-id' => $postid, 'embed_url' => get_permalink($postid), 'api_key' => $options['api-key'], 'api_username' => $username, 'title' => $title, 'raw' => $baked, 'category' => $category, 'skip_validations' => 'true', 'auto_track' => $options['auto-track'] == "1" ? 'true' : 'false');
         $url = $options['url'] . '/posts';
         // use key 'http' even if you send the request to https://...
         $post_options = array('timeout' => 30, 'method' => 'POST', 'body' => http_build_query($data));
         $result = wp_remote_post($url, $post_options);
         if ($this->response_validator->validate($result)) {
             $json = json_decode($result['body']);
             if (property_exists($json, 'id')) {
                 $discourse_id = (int) $json->id;
             }
             if (isset($discourse_id) && $discourse_id > 0) {
                 add_post_meta($postid, 'discourse_post_id', $discourse_id, true);
             }
         }
     } else {
         $data = array('api_key' => $options['api-key'], 'api_username' => $username, 'post[raw]' => $baked, 'skip_validations' => 'true');
         $url = $options['url'] . '/posts/' . $discourse_id;
         $post_options = array('timeout' => 30, 'method' => 'PUT', 'body' => http_build_query($data));
         $result = wp_remote_post($url, $post_options);
         if ($this->response_validator->validate($result)) {
             $json = json_decode($result['body']);
             if (property_exists($json, 'id')) {
                 $discourse_id = (int) $json->id;
             }
             if (isset($discourse_id) && $discourse_id > 0) {
                 add_post_meta($postid, 'discourse_post_id', $discourse_id, true);
             }
         }
     }
     if (isset($json->topic_slug)) {
         delete_post_meta($postid, 'discourse_permalink');
         add_post_meta($postid, 'discourse_permalink', $options['url'] . '/t/' . $json->topic_slug . '/' . $json->topic_id, true);
     }
 }
コード例 #2
0
ファイル: discourse.php プロジェクト: HumulusLLC/wp-discourse
 function sync_to_discourse_work($postid, $title, $raw)
 {
     $discourse_id = get_post_meta($postid, 'discourse_post_id', true);
     $options = self::get_plugin_options();
     $post = get_post($postid);
     $use_full_post = isset($options['full-post-content']) && intval($options['full-post-content']) == 1;
     if ($use_full_post) {
         $excerpt = $raw;
     } else {
         $excerpt = apply_filters('the_content', $raw);
         $excerpt = wp_trim_words($excerpt, $options['custom-excerpt-length']);
     }
     if (function_exists('discourse_custom_excerpt')) {
         $excerpt = discourse_custom_excerpt($postid);
     }
     $baked = $options['publish-format'];
     $baked = str_replace("{excerpt}", $excerpt, $baked);
     $baked = str_replace("{blogurl}", get_permalink($postid), $baked);
     $author_id = $post->post_author;
     $author = get_the_author_meta('display_name', $author_id);
     $baked = str_replace("{author}", $author, $baked);
     $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($postid), 'thumbnail');
     $baked = str_replace("{thumbnail}", "![image](" . $thumb['0'] . ")", $baked);
     $featured = wp_get_attachment_image_src(get_post_thumbnail_id($postid), 'full');
     $baked = str_replace("{featuredimage}", "![image](" . $featured['0'] . ")", $baked);
     $username = get_the_author_meta('discourse_username', $post->post_author);
     if (!$username || strlen($username) < 2) {
         $username = $options['publish-username'];
     }
     $category = $options['publish-category'];
     if ($category === '') {
         // BEGIN GOLAZO MOD
         // Description: Change category selection code to use custom team taxonomy in WordPress
         /*
         $categories = get_the_category();
         foreach ( $categories as $category ) {
           if ( in_category( $category->name, $postid ) ) {
             $category = $category->name;
             break;
           }
         }
         */
         $category = intval(get_term_meta(get_the_terms($postid, 'team')[0]->term_id, 'discourse_cat', true));
         // END GOLAZO MOD
     }
     $data = array('wp-id' => $postid, 'embed_url' => get_permalink($postid), 'api_key' => $options['api-key'], 'api_username' => $username, 'title' => $title, 'raw' => $baked, 'category' => $category, 'skip_validations' => 'true', 'auto_track' => $options['auto-track'] == "1" ? 'true' : 'false');
     if (!$discourse_id > 0) {
         $url = $options['url'] . '/posts';
         // use key 'http' even if you send the request to https://...
         $post_options = array('timeout' => 30, 'method' => 'POST', 'body' => http_build_query($data));
         $result = wp_remote_post($url, $post_options);
         if (is_wp_error($result)) {
             error_log($result->get_error_message());
         } else {
             $json = json_decode($result['body']);
             // todo may have $json->errors with list of errors
             if (property_exists($json, 'id')) {
                 $discourse_id = (int) $json->id;
             }
             if (isset($discourse_id) && $discourse_id > 0) {
                 add_post_meta($postid, 'discourse_post_id', $discourse_id, true);
             }
         }
     } else {
         // for now the updates are just causing grief, leave'em out
         return;
         $url = $options['url'] . '/posts/' . $discourse_id;
         $post_options = array('method' => 'PUT', 'body' => http_build_query($data));
         $result = wp_remote_post($url, $post_options);
         $json = json_decode($result['body']);
         if (isset($json->post)) {
             $json = $json->post;
         }
         // todo may have $json->errors with list of errors
     }
     if (isset($json->topic_slug)) {
         delete_post_meta($postid, 'discourse_permalink');
         add_post_meta($postid, 'discourse_permalink', $options['url'] . '/t/' . $json->topic_slug . '/' . $json->topic_id, true);
     }
 }
コード例 #3
0
ファイル: discourse.php プロジェクト: sitepoint/wp-discourse
 function sync_to_discourse_work($postid, $title, $raw)
 {
     $options = self::get_plugin_options();
     //check if we already have this url in discourse
     $soptions = array('http' => array('ignore_errors' => true, 'method' => 'GET', 'content' => http_build_query(array('embed_url' => get_permalink($postid), 'api_key' => $options['api-key'], 'api_username' => 'system'))));
     $url = $options['url'] . '/embed/info';
     $context = stream_context_create($soptions);
     $result = file_get_contents($url, false, $context);
     $json = json_decode($result);
     if ($json->post_id != 0) {
         add_post_meta($postid, 'discourse_post_id', (int) $json->post_id, true);
         delete_post_meta($postid, 'discourse_permalink');
         add_post_meta($postid, 'discourse_permalink', $options['url'] . '/t/' . $json->topic_slug . '/' . $json->topic_id, true);
         return;
     }
     remove_filter('the_content', 'wpautop');
     $discourse_id = get_post_meta($postid, 'discourse_post_id', true);
     $post = get_post($postid);
     $post_primary_category = get_post_meta($postid, 'primary_category', true);
     $use_full_post = isset($options['full-post-content']) && intval($options['full-post-content']) == 1;
     if ($use_full_post) {
         $excerpt = $raw;
     } else {
         $excerpt = apply_filters('the_content', $raw);
         $excerpt = wp_trim_words($excerpt, $options['custom-excerpt-length']);
     }
     if (function_exists('discourse_custom_excerpt')) {
         $excerpt = discourse_custom_excerpt($postid);
     }
     $baked = $options['publish-format'];
     $author_id = $post->post_author;
     $author = get_the_author_meta('display_name', $author_id);
     $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($postid), 'thumbnail');
     $featured = wp_get_attachment_image_src(get_post_thumbnail_id($postid), 'full');
     $replace = array("{excerpt}" => $excerpt, "{blogurl}" => get_permalink($postid), "{author}" => $author, "{thumbnail}" => "![image](" . $thumb['0'] . ")", "{featuredimage}" => "![image](" . $featured['0'] . ")", "https://www.sitepoint.com/wp-content/uploads/" => "https://dab1nmslvvntp.cloudfront.net/wp-content/uploads/");
     $baked = str_replace(array_keys($replace), array_values($replace), $baked);
     $username = get_the_author_meta('discourse_username', $post->post_author);
     if (!$username || strlen($username) < 2) {
         $username = $options['publish-username'];
     }
     // WP => Discourse category map
     $discourse_category_map = array('6523' => '25', '407' => '33', '37' => '31', '8' => '34', '410' => '29', '6131' => '48', '6132' => '42', '5849' => '30', '4386' => '47', '422' => '47', '7574' => '47');
     // check for category mapping
     if (array_key_exists($post_primary_category, $discourse_category_map)) {
         $publish_category = $discourse_category_map[$post_primary_category];
     } else {
         $publish_category = $options['publish-category'];
     }
     if ($publish_category === '') {
         $categories = get_the_category();
         foreach ($categories as $publish_category) {
             if (in_category($publish_category->name, $postid)) {
                 $publish_category = $publish_category->name;
                 break;
             }
         }
     }
     $data = array('wp-id' => $postid, 'embed_url' => get_permalink($postid), 'api_key' => $options['api-key'], 'api_username' => $username, 'title' => $title, 'raw' => $baked, 'category' => $publish_category, 'skip_validations' => 'true', 'auto_track' => $options['auto-track'] == "1" ? 'true' : 'false', 'tags' => array('article'));
     if (!$discourse_id > 0) {
         $url = $options['url'] . '/posts';
         // use key 'http' even if you send the request to https://...
         $soptions = array('http' => array('ignore_errors' => true, 'method' => 'POST', 'content' => http_build_query($data), 'header' => "Content-Type: application/x-www-form-urlencoded\r\n"));
         $context = stream_context_create($soptions);
         $result = file_get_contents($url, false, $context);
         $json = json_decode($result);
         // todo may have $json->errors with list of errors
         if (property_exists($json, 'id')) {
             $discourse_id = (int) $json->id;
         }
         if (isset($discourse_id) && $discourse_id > 0) {
             add_post_meta($postid, 'discourse_post_id', $discourse_id, true);
         }
     } else {
         // for now the updates are just causing grief, leave'em out
         return;
         $url = $options['url'] . '/posts/' . $discourse_id;
         $soptions = array('http' => array('ignore_errors' => true, 'method' => 'PUT', 'content' => http_build_query($data)));
         $context = stream_context_create($soptions);
         $result = file_get_contents($url, false, $context);
         $json = json_decode($result);
         if (isset($json->post)) {
             $json = $json->post;
         }
         // todo may have $json->errors with list of errors
     }
     if (isset($json->topic_slug)) {
         delete_post_meta($postid, 'discourse_permalink');
         add_post_meta($postid, 'discourse_permalink', $options['url'] . '/t/' . $json->topic_slug . '/' . $json->topic_id, true);
     }
 }
コード例 #4
0
 /**
  * The guts of syncing a post to Discourse
  *
  * @param int    $postid
  * @param string $title
  * @param        $raw
  */
 function sync_to_discourse_work($postid, $title, $raw)
 {
     $discourse_id = get_post_meta($postid, 'discourse_post_id', true);
     $options = self::get_plugin_options();
     $post = get_post($postid);
     $use_full_post = isset($options['full-post-content']) && intval($options['full-post-content']) == 1;
     if ($use_full_post) {
         $excerpt = $raw;
     } else {
         $excerpt = apply_filters('the_content', $raw);
         $excerpt = wp_trim_words($excerpt, $options['custom-excerpt-length']);
     }
     if (function_exists('discourse_custom_excerpt')) {
         $excerpt = discourse_custom_excerpt($postid);
     }
     $baked = self::publish_format_html();
     $baked = str_replace('{excerpt}', $excerpt, $baked);
     $baked = str_replace('{blogurl}', get_permalink($postid), $baked);
     $author_id = $post->post_author;
     $author = get_the_author_meta('display_name', $author_id);
     $baked = str_replace('{author}', $author, $baked);
     $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($postid), 'thumbnail');
     $baked = str_replace('{thumbnail}', '![image](' . $thumb['0'] . ')', $baked);
     $featured = wp_get_attachment_image_src(get_post_thumbnail_id($postid), 'full');
     $baked = str_replace('{featuredimage}', '![image](' . $featured['0'] . ')', $baked);
     $username = get_the_author_meta('discourse_username', $post->post_author);
     if (!$username || strlen($username) < 2) {
         $username = $options['publish-username'];
     }
     $data = array('wp-id' => $postid, 'embed_url' => get_permalink($postid), 'api_key' => $options['api-key'], 'api_username' => $username, 'title' => $title, 'raw' => $baked, 'category' => $options['publish-category'], 'skip_validations' => 'true', 'auto_track' => $options['auto-track'] == '1' ? 'true' : 'false');
     if (!$discourse_id > 0) {
         $url = $options['url'] . '/posts';
         $response = wp_remote_post($url, array('headers' => array('content-type' => 'application/x-www-form-urlencoded'), 'body' => $data));
         if (is_wp_error($response)) {
             // todo: handle WP HTTP API errors here
             return;
         }
         $json = json_decode(wp_remote_retrieve_body($response));
         if (!empty($json->errors)) {
             // todo: handle Discourse API errors here
             return;
         }
         if (isset($json->id)) {
             add_post_meta($postid, 'discourse_post_id', (int) $json->id, true);
         }
     } else {
         // todo: handle updating existing posts here
         return;
     }
     if (isset($json->topic_slug)) {
         delete_post_meta($postid, 'discourse_permalink');
         add_post_meta($postid, 'discourse_permalink', $options['url'] . '/t/' . $json->topic_slug . '/' . $json->topic_id, true);
     }
 }