WordPress: Как каждый день удалять старые IP-адреса посетителей, используя временную метку, чтобы снова вспомнить тот же IP-адрес? - PullRequest
0 голосов
/ 07 апреля 2019

У меня есть этот фрагмент ниже, чтобы получить посетителям уникальные IP-адреса, которые они просматривают / посетили страницу профиля автора и отображают их в виде счетчика, этот код сделал @butlerblog, и он сохранил уникальные IP-адреса внутри имени опции "my_author_counter_{ID}" иотображал счетчик за последние 7 дней.

Но как мне сохранить общий счетчик за последние 7 дней, удаляя старые IP-адреса каждый день!Поэтому я могу вспомнить их снова, если он / она посетил тот же профиль на следующий день, а также для того, чтобы спасти мою базу данных от огромного подсчета.

Что-то вроде подсчета посещений за последние 7 дней каждого дня.

Если кто-то знает Instagram!Вы можете видеть, как посещения профиля собирались за последние 7 дней, но каждый день вы можете видеть счетчик UP и Down в зависимости от того, сколько людей просматривали этот профиль в этот день.

Надеюсь, я все хорошо объяснил, я высоко ценю вашу помощь.

/**
 * This is your data counter retrieval/update.
 * The actual count evaluation will occur where
 * you actually display the value.
 */
add_action( 'template_redirect', 'my_author_counter' );
function my_author_counter() {

    // Is this an author page?
    if ( is_author() ) {
        global $post, $author_id, $author_counts;

        // Which author is being viewed?
        $author_id = $post->post_author;

        // Counter setting based on author ID.
        $option_name = 'my_author_counter' . $author_id;

        // Get the counter data.
        $author_counts = get_option( $option_name );

        // Check for empty counter, set as array no value exists.
        $author_counts = ( $author_counts ) ? $author_counts : array();

        // Add current visit to raw data (IP keyed by timestamp).
        $author_counts[ time() ] = $_SERVER['REMOTE_ADDR'];

        /*
         * Didn't know if this would ONLY ever display count over
         * the last 7 days, so it does not include any data removal.
         * With that in mind, this could grow over time to be too 
         * big. So depending on actual production use a expired/old 
         * data removal step should be included before the value is
         * updated in the database. 
         */

        // Save updated count data.
        update_option( $option_name, $author_counts );

    }
}

Вот как он достал и отобразил счетчик внутри страницы автора

 add_filter( 'get_the_archive_description', 'display_my_author_counter' );
function display_my_author_counter( $content ) {

    // Is this an author page?
    if ( is_author() ) {

        // Global values picked up from my_author_counter().
        global $post, $author_id, $author_counts;

        /** This is where you'd manage the raw data ($author_counts) for display ($display_value). **/

        // Handle raw $author_counts values for desired display.
        // Start with an zero for our display value.
        $display_value = 0;
        // Our counter collects all visists by IP. Get only unique IP addresses.
        $unique_counts = array_unique( $author_counts );
        // Loop through the unique IP values and check the timestamp. Only get the last 7 days.
        foreach ( $unique_counts as $timestamp => $ip ) {
            if ( $timestamp > strtotime( '-7 day' ) ) { 
                // Increment the display count by 1.
                $display_value++;
            }
        }

        /** END manage the raw data ($author_counts) for display ($display_value ). **/

        // Add the display count to the archive description.
        $content = $content . "This author page has been viewed " . $display_value . " times";
    }

    return $content;
}
...