示例#1
0
/**
 * 发布
 * @since 1.9.10
 */
function wp_connect_publish($post_ID)
{
    global $wptm_options, $siteurl;
    @ini_set("max_execution_time", 60);
    $time = time();
    $post = get_post($post_ID);
    $title = wp_replace($post->post_title);
    $content = $post->post_content;
    $excerpt = $post->post_excerpt;
    $postlink = get_permalink($post_ID);
    $post_author_ID = $post->post_author;
    $post_date = strtotime($post->post_date);
    $post_modified = strtotime($post->post_modified);
    $post_content = wp_replace($content);
    // 匹配视频、图片
    $pic = wp_multi_media_url($content);
    // 是否有摘要
    if ($excerpt) {
        $post_content = wp_replace($excerpt);
    }
    if ($wptm_options['multiple_authors']) {
        $wptm_profile = get_user_meta($post_author_ID, 'wptm_profile', true);
        $account = wp_usermeta_account($post_author_ID);
    }
    // 是否开启了多作者博客
    if ($account && $wptm_profile['sync_option']) {
        $sync_option = $wptm_profile['sync_option'];
        $new_prefix = $wptm_profile['new_prefix'];
        $update_prefix = $wptm_profile['update_prefix'];
        $update_days = $wptm_profile['update_days'] * 60 * 60 * 24;
    } else {
        if (!$wptm_options['sync_option']) {
            return;
        }
        $account = wp_option_account();
        $sync_option = $wptm_options['sync_option'];
        $new_prefix = $wptm_options['new_prefix'];
        $update_prefix = $wptm_options['update_prefix'];
        $update_days = $wptm_options['update_days'] * 60 * 60 * 24;
    }
    // 是否绑定了帐号
    if (!$account) {
        return;
    }
    $cat_ids = $wptm_options['cat_ids'];
    $enable_cats = $wptm_options['enable_cats'];
    $enable_tags = $wptm_options['enable_tags'];
    if ($enable_cats || $cat_ids) {
        if ($postcats = get_the_category($post_ID)) {
            foreach ($postcats as $cat) {
                $cat_id .= $cat->cat_ID . ',';
                $cat_name .= $cat->cat_name . ',';
            }
            // 不想同步的文章分类ID
            if ($cat_ids && wp_in_array($cat_ids, $cat_id)) {
                return;
            }
            // 是否将文章分类当成话题
            if ($enable_cats) {
                $cats = $cat_name;
            }
        }
    }
    // 是否将文章标签当成话题
    if (substr_count($cats, ',') < 2 && $enable_tags) {
        if ($posttags = get_the_tags($post_ID)) {
            foreach ($posttags as $tag) {
                $tags .= $tag->name . ',';
            }
        }
    }
    $tags = $cats . $tags;
    if ($tags) {
        $tags = explode(',', rtrim($tags, ','));
        if (count($tags) == 1) {
            $tags = '#' . $tags[0] . '# ';
        } elseif (count($tags) >= 2) {
            $tags = '#' . $tags[0] . '# #' . $tags[1] . '# ';
        }
    }
    // 是否为新发布
    if (($post->post_status == 'publish' || $_POST['publish'] == 'Publish') && ($_POST['prev_status'] == 'draft' || $_POST['original_post_status'] == 'draft' || $_POST['original_post_status'] == 'auto-draft' || $_POST['prev_status'] == 'pending' || $_POST['original_post_status'] == 'pending')) {
        if ($_POST['publish_no_sync']) {
            return;
        }
        $title = $new_prefix . $title;
    } elseif ($_POST['originalaction'] == "editpost" && ($_POST['prev_status'] == 'publish' || $_POST['original_post_status'] == 'publish') && $post->post_status == 'publish') {
        // 是否已发布
        if (!$_POST['publish_update_sync']) {
            if ($time - $post_date < $update_days || $update_days == 0) {
                // 判断当前时间与文章发布时间差
                return;
            }
        }
        if ($_POST['publish_new_sync']) {
            $update_prefix = $new_prefix;
        }
        $title = $update_prefix . $title;
    } elseif ($_POST['_inline_edit']) {
        // 是否是快速编辑
        $quicktime = $_POST['aa'] . '-' . $_POST['mm'] . '-' . $_POST['jj'] . ' ' . $_POST['hh'] . ':' . $_POST['mn'] . ':00';
        $post_date = strtotime($quicktime);
        if ($time - $post_date < $update_days || $update_days == 0) {
            // 判断当前时间与文章发布时间差
            return;
        }
        $title = $update_prefix . $title;
    } elseif (defined('DOING_CRON')) {
        // 定时发布
        $title = $new_prefix . $title;
    } else {
        // 后台快速发布,xmlrpc等发布
        if ($post->post_status == 'publish') {
            if ($post_modified == $post_date || $time - $post_date <= 30) {
                // 新文章(包括延迟<=30秒)
                $title = $new_prefix . $title;
            } else {
                $title = $title;
            }
        } else {
            $title = $title;
        }
    }
    if ($wptm_options['enable_shorten']) {
        // 是否使用博客默认短网址
        if ($post->post_type == 'page') {
            $postlink = $siteurl . "/?page_id=" . $post_ID;
        } else {
            $postlink = $siteurl . "/?p=" . $post_ID;
        }
    }
    if ($sync_option == '2') {
        // 同步 前缀+标题+摘要/内容+链接
        $title = $tags . $title . " - " . $post_content;
    } elseif ($sync_option == '3') {
        // 同步 文章摘要/内容
        $title = $tags . $post_content;
        $postlink = "";
    } elseif ($sync_option == '4') {
        // 同步 文章摘要/内容+链接
        $title = $tags . $post_content;
    } elseif ($sync_option == '5') {
        // 同步 标题 + 内容
        $title = $tags . $title . $post_content;
        $postlink = "";
    } else {
        $title = $tags . $title;
    }
    if ($pic[0] == "image" && $wptm_options['disable_pic']) {
        $pic = '';
    }
    wp_update_list($title, $postlink, $pic, $account);
}
示例#2
0
文件: sync.php 项目: xxf1995/alphaV
function wp_connect_publish($post_ID)
{
    global $sync_loaded, $wptm_options;
    $sync_loaded += 1;
    if (isset($_POST['publish_no_sync']) || $sync_loaded > 1 || $_POST['post_password']) {
        return;
    }
    @ini_set("max_execution_time", 120);
    $time = time();
    $post = get_post($post_ID);
    if ($wptm_options['post_types']) {
        if (in_array($post->post_type, explode(',', $wptm_options['post_types']))) {
            return;
        }
    }
    $title = wp_replace($post->post_title);
    $content = $post->post_content;
    $excerpt = $post->post_excerpt;
    $post_author_ID = $post->post_author;
    $post_date = strtotime($post->post_date);
    $post_modified = strtotime($post->post_modified);
    $post_content = wp_replace($content);
    if ($wptm_options['multiple_authors']) {
        $wptm_profile = get_user_meta($post_author_ID, 'wptm_profile', true);
        if ($wptm_profile['sync_option']) {
            $account = wp_usermeta_account($post_author_ID);
        }
    }
    // 是否开启了多作者博客
    if ($account) {
        $sync_option = $wptm_profile['sync_option'];
        $new_prefix = $wptm_profile['new_prefix'];
        $update_prefix = $wptm_profile['update_prefix'];
        $update_days = $wptm_profile['update_days'] * 60 * 60 * 24;
        $is_author = true;
    } else {
        if (!$wptm_options['sync_option']) {
            return;
        }
        $account = wp_option_account();
        $sync_option = $wptm_options['sync_option'];
        $new_prefix = $wptm_options['new_prefix'];
        $update_prefix = $wptm_options['update_prefix'];
        $update_days = $wptm_options['update_days'] * 60 * 60 * 24;
    }
    // 是否绑定了帐号
    if (!$account) {
        return;
    }
    // 新浪微博授权码过期检查 V1.9.22
    if (!empty($account['sina']['expires_in'])) {
        $expires = $account['sina']['expires_in'] - BJTIMESTAMP;
        if ($expires < 20 * 3600) {
            if ($is_author) {
                if (get_current_user_id() == $post_author_ID) {
                    $into = "<a href=" . admin_url('profile.php') . " target=\"_blank\">我的个人资料</a>";
                } else {
                    $into = "<a href=" . admin_url("user-edit.php?user_id={$post_author_ID}") . " target=\"_blank\">用户资料</a>";
                }
            } else {
                $into = "<a href=" . admin_url('options-general.php?page=wp-connect') . " target=\"_blank\">WordPress连接微博插件</a>";
            }
            if ($expires < 60) {
                return setcookie('sina_access_token_expires', "您的 新浪微博授权码已经过期了,刚刚发布的文章已经取消了所有微博同步,请先到 " . $into . " 重新绑定下新浪微博帐号之后再同步吧,本条提示5分钟后自动消失。", BJTIMESTAMP + 600);
            } elseif ($expires < 3600) {
                $expires_in = (int) ($expires / 60) . '分钟';
            } else {
                $expires_in = (int) ($expires / 3600) . '小时';
            }
            setcookie('sina_access_token_expires', "您的 新浪微博授权码再过 " . $expires_in . " 就过期了,咱们先到 " . $into . " 重新绑定下吧,否则不能同步到新浪微博噢,本条提示5分钟后自动消失。", BJTIMESTAMP + 600);
        } elseif ($_COOKIE['sina_access_token_expires']) {
            setcookie('sina_access_token_expires', "", BJTIMESTAMP - (BJTIMESTAMP + 600));
        }
    }
    // 是否为新发布
    if (($post->post_status == 'publish' || $_POST['publish'] == 'Publish') && ($_POST['prev_status'] == 'draft' || $_POST['original_post_status'] == 'draft' || $_POST['original_post_status'] == 'auto-draft' || $_POST['prev_status'] == 'pending' || $_POST['original_post_status'] == 'pending')) {
        $prefix = $new_prefix;
    } elseif ($_POST['originalaction'] == "editpost" && ($_POST['prev_status'] == 'publish' || $_POST['original_post_status'] == 'publish') && $post->post_status == 'publish') {
        // 是否已发布
        if (isset($_POST['publish_new_sync'])) {
            $prefix = $new_prefix;
        } elseif (!isset($_POST['publish_update_sync'])) {
            if ($update_days == 0 || $time - $post_date < $update_days) {
                // 判断当前时间与文章发布时间差
                return;
            }
        } else {
            $prefix = $update_prefix;
        }
    } elseif (isset($_POST['_inline_edit'])) {
        // 是否是快速编辑
        $quicktime = $_POST['aa'] . '-' . $_POST['mm'] . '-' . $_POST['jj'] . ' ' . $_POST['hh'] . ':' . $_POST['mn'] . ':00';
        $post_date = strtotime($quicktime);
        if ($update_days == 0 || $time - $post_date < $update_days) {
            // 判断当前时间与文章发布时间差
            return;
        }
        $prefix = $update_prefix;
    } elseif (defined('DOING_CRON')) {
        // 定时发布
        $prefix = $new_prefix;
    } else {
        // 后台快速发布,xmlrpc等发布
        if ($post->post_status == 'publish') {
            if ($post_modified == $post_date || $time - $post_date <= 30) {
                // 新文章(包括延迟<=30秒)
                $prefix = $new_prefix;
            }
        }
    }
    // 微博话题
    $cat_ids = $wptm_options['cat_ids'];
    $enable_cats = $wptm_options['enable_cats'];
    $enable_tags = $wptm_options['enable_tags'];
    if ($enable_cats || $cat_ids) {
        if ($postcats = get_the_category($post_ID)) {
            foreach ($postcats as $cat) {
                $cat_id .= $cat->cat_ID . ',';
                $cat_name .= $cat->cat_name . ',';
            }
            // 不想同步的文章分类ID
            if ($cat_ids && wp_in_array($cat_ids, $cat_id)) {
                return;
            }
            // 是否将文章分类当成话题
            if ($enable_cats) {
                $cats = $cat_name;
            }
        }
    }
    // 是否将文章标签当成话题
    if (substr_count($cats, ',') < 2 && $enable_tags) {
        if ($posttags = get_the_tags($post_ID)) {
            foreach ($posttags as $tag) {
                $tags .= $tag->name . ',';
            }
        }
    }
    $tags = $cats . $tags;
    if ($tags) {
        $tags = explode(',', rtrim($tags, ','));
        if (count($tags) == 1) {
            $tags = '#' . $tags[0] . '# ';
        } elseif (count($tags) >= 2) {
            $tags = '#' . $tags[0] . '# #' . $tags[1] . '# ';
        }
    }
    // 文章URL
    if ($wptm_options['enable_shorten']) {
        // 是否使用博客默认短网址
        $siteurl = get_bloginfo('url');
        if ($post->post_type == 'post') {
            $postlink = $siteurl . "/?p=" . $post_ID;
        } elseif ($post->post_type == 'page') {
            $postlink = $siteurl . "/?page_id=" . $post_ID;
        } else {
            $postlink = get_permalink($post_ID);
        }
    } else {
        $postlink = get_permalink($post_ID);
    }
    $url = $postlink;
    if ($excerpt) {
        // 是否有摘要
        $post_content = wp_replace($excerpt);
    }
    $format = $wptm_options['format'];
    if ($format && strpos($format, '%title%') !== false) {
        $format_title = true;
        $title2 = str_replace('%title%', $title, $format);
    } else {
        $title2 = $title . ' | ';
    }
    if ($sync_option == '2') {
        // 同步 前缀+标题+摘要/内容+链接
        $text = $tags . $prefix . $title2 . $post_content;
    } elseif ($sync_option == '3') {
        // 同步 文章摘要/内容
        $text = $tags . $prefix . $post_content;
        $url = "";
    } elseif ($sync_option == '4') {
        // 同步 文章摘要/内容+链接
        $text = $tags . $prefix . $post_content;
    } elseif ($sync_option == '5') {
        // 同步 标题 + 内容
        $text = $tags . $prefix . $title2 . $post_content;
        $url = "";
    } elseif ($sync_option == '6') {
        // 同步 标题
        $text = $tags . $prefix . $title2;
        $url = "";
    } else {
        // 同步 标题 + 链接
        $title2 = $format_title ? $title2 : $title;
        $text = $tags . $prefix . $title2;
    }
    $richMedia = wp_multi_media_url($content, $post_ID);
    $list = array('title' => $title, 'content' => $content, 'excerpt' => $excerpt, 'postlink' => $postlink, 'tags' => $tags, 'text' => str_replace(array("[embed]", "[/embed]", $richMedia[1]), "", $text), 'url' => $url, 'richMedia' => $richMedia, 'is_author' => $is_author);
    $list = apply_filters('post_sync_weibo', $list, $post_ID, $post_author_ID);
    // return var_dump($list);
    // $other = array('is_author'=>$is_author, 'uid'=>$post_author_ID);
    // $account = array_merge($account, $other);
    if (is_array($list)) {
        wp_update_list($list['text'], $list['url'], $list['richMedia'], $account, $post_ID);
    }
}