让WordPress作者关注按钮,需要缺失的成分

时间:2015-03-06 作者:Julian

所以我尝试在wordpress中创建一个follow按钮,用户可以按一个按钮来跟随他们喜欢的作者。(不,我对buddypress不感兴趣)

到目前为止,我所做的是使用Jon Masterson为WordPress开发的类帖子系统中的代码http://hofmannsven.com/2013/laboratory/wordpress-post-like-system/ 当用户喜欢这篇文章时,它几乎会添加一个+1 update\\u post\\u meta。

我遇到的问题是,如果你一直走到底,你会看到like系统使用update\\u post\\u meta给post meta的计数加1,这正是我需要的follow按钮。问题是我找不到函数update\\u author\\u meta来存储该特定作者的++$author\\u follow\\u计数。如果您知道我如何存储和更新特定作者的follow count,或者为我指出正确的方向,我们将不胜感激。

<?php
function follow_scripts() {
wp_localize_script( \'jk_like_post\', \'ajax_var\', array(
    \'url\' => admin_url( \'admin-ajax.php\' ),
    \'nonce\' => wp_create_nonce( \'ajax-nonce\' )
        )
    );
}
add_action( \'init\', \'follow_scripts\' );

/**
* (2) Save follow data
*/
add_action( \'wp_ajax_nopriv_jk-author-follow\', \'jk_author_follow\' );
add_action( \'wp_ajax_jk-author-follow\', \'jk_author_follow\' );
function jk_author_follow() {
   $nonce = $_POST[\'nonce\'];
if ( ! wp_verify_nonce( $nonce, \'ajax-nonce\' ) )
        die ( \'Nope!\' );

if ( isset( $_POST[\'jk_author_follow\'] ) ) {

    $author_id = $_POST[\'author_id\']; // author id
    $author_follow_count = get_author_meta( $author_id,        "_author_follow_count", true ); // author follow count

    if ( function_exists ( \'wp_cache_post_change\' ) ) { // invalidate WP Super Cache if exists
                $GLOBALS["super_cache_enabled"]=1;
                wp_cache_post_change( $post_id );
    }

    if ( is_user_logged_in() ) { // user is logged in
        $user_id = get_current_user_id(); // current user
        $meta_AUTHORS = get_user_option( "_followed_authors", $user_id ); // author ids from user meta
        $meta_USERS = get_author_meta( $author_id, "_user_followed" ); //user ids from author meta
        $followed_AUTHORS = NULL; // setup array variable
        $followed_USERS = NULL; // setup array variable

        if ( count( $meta_Authors ) !=0 ) { // meta exists, set up values
            $followed_AUTHORS = $meta_AUTHORS;
        }

        if ( !is_array( $followed_AUTHORS ) )  // make array just in case
            $followed_AUTHORS = array();

        if ( count( $meta_USERS ) !=0 ) { // meta exists, set up values
            $followed_USERS = $meta_USERS[0];
        }

        if ( !is_array( $followed_USERS ) ) //make an array just in case
            $followed_USERS = array();

        $followed_AUTHORS[\'author-\'.$author_id, "_user_followed"] = $author_id; // Add author id to user meta array
        $followed_USERS[\'user-\'.$user_id] = $user_id; // add user id to author meta array
        $user_follows = count( $followed_AUTHORS ); // count user follows

 // *** Where the snag is ****
        if ( !AlreadyFollowed( $author_id ) ) { // follow the author
            update_post_meta( $author_id, "_user_followed", $followed_USERS ); // Add user ID to author meta
            update_post_meta( $author_id, "_author_follow_count", ++$author_follow_count ); // +1 count author meta
            update_user_option( $user_id, "_followed_authors", $followed_Authors ); // Add author ID to user meta
            update_user_option( $author_id, "_author_follow_count", $user_follows ); // +1 count user meta
            echo $author_follow_count; // update count on front end

Update

好的,我摆弄了(4)并使用了你的代码。出于某种原因,默认的跟随者计数仍然是1。还有,ajax的模具应该放在哪里?谢谢

/**
* (1) Enqueue scripts for follow system
*/
function follow_scripts() {
wp_enqueue_script( \'jk_follow_post\', get_template_directory_uri().\'/js/post-   like.min.js\', array(\'jquery\'), \'1.0\', 1 );
 wp_localize_script( \'jk_like_post\', \'ajax_var\', array(
    \'url\' => admin_url( \'admin-ajax.php\' ),
    \'nonce\' => wp_create_nonce( \'ajax-nonce\' )
        )
    );
}
/**
* (2) Save follow data
*/
add_action( \'wp_ajax_nopriv_jk-author-follow\', \'jk_author_follow\' );
add_action( \'wp_ajax_jk-author-follow\', \'jk_author_follow\' );
function jk_author_follow() {
$nonce = $_POST[\'nonce\'];
if ( ! wp_verify_nonce( $nonce, \'ajax-nonce\' ) )
        die ( \'Nope!\' );

if ( isset( $_POST[\'jk_author_follow\'] ) ) {

    $author_id = $_POST[\'author_id\']; // author id


    if ( is_user_logged_in() ) { // user is logged in
        $user_id = get_current_user_id(); // current user

        // create the term if it doesn\'t exist
        if ( !term_exists( $author_id, \'wpse_180398_followers\' ) ) {
            wp_insert_term( $author_id, \'wpse_180398_followers\' );
        }

        // follow the user, note the last argument is true
        $term_ids = wp_set_object_terms( $user_id, $author_id, \'wpse_180398_followers\', true );

        if ( is_wp_error( $term_ids ) ) {
            // There was an error somewhere and the terms couldn\'t be set.
            echo "error";
        } else {
            // Success!
        }
    }
}
}
/**
 * (3) Test if user already liked post
 */
function AlreadyFollowed( $author_id ) { // test if user is following author
$user_id = get_current_user_id(); // current user
$author_id = $_POST[\'author_id\']; // author id
if ( is_user_logged_in() ) { // user is logged in
    if ( has_term( $author_id, \'wpse_180398_followers\', $user_id ) ) {
        // user_id is following author_id!
    }
}
}
/**
  * (4) Front end button
*/
function getFollowLink( $author_id ) {
$following = \'\';
$author_id = get_query_var(\'author\');
$user_id = get_current_user_id(); // current user
if (has_term( $author_id, \'wpse_180398_followers\', $user_id )) {
    $following = \' following\';
}
echo \'<a class="follow-button\' .$following. \'" data-author="\' .$author_id.  \'">Follow</a>\';
}
/**
 * (5) Add a shortcode to your posts instead
 * type [jkfollower] in your post to output the button
*/
function jk_follow_shortcode() {
return getFollowLink( get_the_ID() );
}
add_shortcode(\'jkfollower\', \'jk_follow_shortcode\');
另一个问题是,我手动执行下面的代码,看看计数是否受到影响,由于某种原因,计数没有受到影响,只是保持在1。

$user_id = 1;
$author_id = 118;
wp_set_object_terms( $user_id, $author_id, \'wpse_180398_followers\', true );

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

我建议您不要使用用户元。尽管您的问题是由以下原因引起的:

update_post_meta( $author_id, "_user_followed", $followed_USERS ); // Add user ID to author meta
update_post_meta( $author_id, "_author_follow_count", ++$author_follow_count );
请注意,您调用了update_post_元数据未更新_user_meta,所以在某个地方,有一篇帖子的ID与作者的用户ID相同,这篇帖子添加了奇怪的meta。

然而,这仍然是一种效率低下的方法,并且容易出现可能给出不准确计数的竞争条件。当你试图找出谁追随作者的时候,你也会有一段非常昂贵的代码,因为你必须检查每个用户,看看他们是否追随作者。

Instead, 使用自定义分类法。

使用自定义用户分类法不仅仅适用于帖子。在这个分类法中,术语是作者,被标记的对象是用户(特别是我们使用用户ID作为标记)。

假设我们称之为分类法wpse_180398_followers, 跟随作者:

// follow the user, note the last argument is true
$term_ids = wp_set_object_terms( $user_id, $author_id, \'wpse_180398_followers\', true );

if ( is_wp_error( $term_ids ) ) {
    // There was an error somewhere and the terms couldn\'t be set.
} else {
    // Success!
}
To unfollow someone:

wp_remove_object_terms( $user_id, $author_id, \'wpse_180398_followers\' );
check if a user is following an author:

if ( has_term( $author_id, \'wpse_180398_followers\', $user_id ) ) {
    // user_id is following author_id!
}
get the followers of an author:

$followers = get_objects_in_term( $author_id, \'wpse_180398_followers\' );
get an authors follower count:

count( $followers );
get the authors a user follows:

$author_terms = wp_get_object_terms( $user_id, \'wpse_180398_followers\' );
foreach ( $author_terms as $term ) {
    echo $term->slug; // the slug is the author ID
}
其他优势:

这些是用于类别和标记的相同API,它们将比使用用户元快得多。术语计数没有竞争条件。有时,术语计数会被缓存。您将有一个分类法,这意味着每个术语的存档模板和免费的URL结构您希望分类法不分等级,您可能希望将我给它的名称上的前缀更改为更独特的名称(不要只称它为followers),如果您希望在管理界面中有一个用户界面you\'ll want to read this

另一个注意事项是,您可能希望在删除与用户关联的术语时将其删除。到delete the term or remove all of a users followers:

wp_delete_term( $author_id, \'wpse_180398_followers\' );
reset who a user follows to nobody:

wp_delete_object_term_relationships( $user_id, \'wpse_180398_followers\' );
按钮本身的提示由于您已经有了一个AJAX端点,您可以删除其中的大部分代码,并用上面的代码片段替换它。对于按钮,您需要一个元素:

<a class="follow-button">Follow</a>
这也是一个切换,因此需要一些东西来指示您是否已经在跟踪:

<a class="follow-button following">Follow</a>
当然,如果它有following 类,更改当您看到twitter关注按钮时的外观。如果你真的在跟踪某人,那么你只需要以下类:

<?php
$following = \'\';
if ( has_term.. etc as above ) {
    $following = \' following\';
}
?>
<a class="follow-button<?php echo $following; ?>">Follow</a>
当然,它需要作者的ID,让我们使用一个数据属性:

<?php
$following = \'\';
if ( has_term.. etc as above ) {
    $following = \' following\';
}
?>
<a class="follow-button<?php echo $following; ?>" data-author="<?php echo $author_id; ?>">Follow</a>
我相信您知道如何根据您的问题获取作者ID。

最后,您需要一些javascript,以便当用户单击follow按钮时(jQuery( \'a.follow-button\').click, 信息技术:

检查元素是否具有following

  • 如果它这样做,它将激发一个AJAX来展开作者
  • 如果它不这样做,它将激发一个AJAX来跟随作者
  • 它将传递元素(.data( \'author\' ) 在jQuery中)它切换following 在元素上初始化,以便用户获得一些反馈(.toggle(\'following\') )follow-in-progress 类,该类在css中添加一个微调器,并在拾取javascript时使其退出,这样就不会重复请求

  • 结束