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);
}
}