WordPress-如何获得使用Cookie发布的所有新帖子的计数器

时间:2018-12-29 11:29:32

标签: wordpress post cookies notifications

我正在尝试使其工作,我在stackoverflow上找到了这段代码here,并且工作得很好,我进行了一些更改以构建一个带有铃铛图标的下拉通知框,如下图所示从我的博客中获取。

enter image description here

此代码在发布新帖子时运行一次,如果有新帖子,则运行一次!删除旧的通知以推送新的通知,如您在图片中看到的那样,即使发布了多个帖子,我也只有一个count(1)。

所以,当新帖子发布时,如何阻止Cookies删除旧的通知!

我如何保留它们直到到期或单击“隐藏”按钮?

hide.notification.bar.js

jQuery(document).ready(function($) {

    $("#notification_hide_button").click(function(){
        $(this).hide();
        $(".notifications_bar").hide();

        if ($.cookie( 'hide_post_cookie' ) ) { 
            $.cookie( 'hide_post_cookie', null ) 
        }
        var post_id = parseInt( cookie_Data.post_id, 10 );

        $.cookie( 'hide_post_cookie', post_id, { expires: 2, path: '/' } );

    });

});

function.php>检查帖子状态。

add_action( 'transition_post_status', function ( $new_status, $old_status, $post )
{
    //Check if our post status then execute our code
    if ( $new_status == 'publish' && $old_status != 'publish' ) {
        if ( get_option( 'new_post_notification' ) !== false ) {

            // The option already exists, so we just update it.
            update_option( 'new_post_notification', $post );

        } else {

            add_option( 'new_post_notification', $post );

        }
    }

}, 10, 3 );

function.php>获取new_post_notification并将其保存在cookie中。

function get_new_post_notification_bar() {

    // Get the new_post_notification which holds the newest post
    $notification   = get_option( 'new_post_notification' );

    $counter = 1;

    // Get the post ID saved in the cookie
    $cookie_post_ID = isset( $_COOKIE['hide_post_cookie'] ) ? (int) $_COOKIE['hide_post_cookie'] : false; 

    $output = '';

    if( false != $notification ) {

        //First check if we have a cookie, if not, show the notification bar
        // If a cookie is set, do not display the notification bar
        if( false === $cookie_post_ID ) {

            //Get the post's gmt date. This can be changed to post_date
            $post_date = strtotime( $notification->post_date_gmt );

            //Get the current gmt time
            $todays_date = current_time( 'timestamp', true );

            //Set the expiry time to two days after the posts is published
            $expiry_date = strtotime( '+2 day', $post_date );

            //Show the notification bar if the expiry date is not yet reached
            if( $expiry_date > $todays_date ) { 

                $output .= '<a id="notification_hide_button">';
                $output .= '<div class="notifications_bar">';
                $output .= "$counter";
                $output .= '</div>';
                $output .= '<i class="fa fa-bell" aria-hidden="true"></i>';
                $output .= '</a>';

            }

        }else{

            /**
             * If a cookie is set, check the cookie value against the post id set as last post
             * If the two don't match, delete the cookie and show the notification bar if a new post is published
             * This code only run once, that is when a cookie is still set, and new post is published within the time
             * in which the cookie is still set
            */ 
            if( (int) $notification->ID !== $cookie_post_ID ) {

                ?>
                    <script>
                        jQuery(document).ready(function($) {

                            $.removeCookie('hide_post_cookie', { path: '/' });

                        });
                    </script>
                <?php

                $output .= '<a id="notification_hide_button">';
                $output .= '<div class="notifications_bar">';
                $output .= "$counter";
                $output .= '</div>';
                $output .= '<i class="fa fa-bell" aria-hidden="true"></i>';
                $output .= '</a>';
            }

        }   

    }
    $counter ++;
    return $output;

}

0 个答案:

没有答案