Пример #1
0
 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
            $comment_html = str_replace('{username}', esc_html($post->username), $comment_html);
            $comment_html = str_replace('{fullname}', esc_html($post->name), $comment_html);
            $comment_body = Discourse::convert_relative_img_src_to_absolute($discourse_url, $post->cooked);
            $comment_html = str_replace('{comment_body}', wp_kses_post($comment_body), $comment_html);
            $comment_html = str_replace('{comment_created_at}', mysql2date($datetime_format, get_date_from_gmt($post->created_at)), $comment_html);
            $comments_html .= $comment_html;
        }
        foreach ($discourse_info->participants as &$participant) {
            $participant_html = wp_kses_post(Templates\HTMLTemplates::participant_html());
            $participant_html = str_replace('{discourse_url}', $discourse_url, $participant_html);
            $participant_html = str_replace('{discourse_url_name}', $discourse_url_name, $participant_html);
            $participant_html = str_replace('{topic_url}', $permalink, $participant_html);
            $avatar_url = Discourse::avatar($participant->avatar_template, 64);
            $participant_html = str_replace('{avatar_url}', esc_url($avatar_url), $participant_html);
            $user_url = Discourse::homepage($options['url'], $participant);
            $participant_html = str_replace('{user_url}', esc_url($user_url), $participant_html);
            $participant_html = str_replace('{username}', esc_html($participant->username), $participant_html);
            $participants_html .= $participant_html;
        }
        $discourse_html = wp_kses_post(Templates\HTMLTemplates::replies_html());
        $discourse_html = str_replace('{more_replies}', $more_replies, $discourse_html);
    } else {
        $discourse_html = wp_kses_post(Templates\HTMLTemplates::no_replies_html());
    }
    $discourse_html = str_replace('{discourse_url}', $discourse_url, $discourse_html);
    $discourse_html = str_replace('{discourse_url_name}', $discourse_url_name, $discourse_html);
    $discourse_html = str_replace('{topic_url}', $permalink, $discourse_html);
    $discourse_html = str_replace('{comments}', $comments_html, $discourse_html);
    $discourse_html = str_replace('{participants}', $participants_html, $discourse_html);
    echo $discourse_html;
}