Making WordPress.org

Changeset 13788


Ignore:
Timestamp:
06/07/2024 04:56:37 AM (12 months ago)
Author:
coffee2code
Message:

Photo Directory, User: Add function to calculate how many posts were published by the contributor on the current calendar day.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/user.php

    r13784 r13788  
    5353
    5454        return count_user_posts( $user_id, Registrations::get_post_type(), true );
     55    }
     56
     57    /**
     58     * Returns a count of photos published by a user on this calendar day.
     59     *
     60     * @param int $user_id Optional. The user ID. If not defined, assumes global
     61     *                     author. Default false.
     62     * @return int
     63     */
     64    public static function count_published_photos_for_today( $user_id = false ) {
     65        if (  ! $user_id ) {
     66            global $authordata;
     67
     68            $user_id = $authordata->ID ?? 0;
     69        }
     70
     71        if ( ! $user_id ) {
     72            return 0;
     73        }
     74
     75        $today = new \DateTime( 'now', new \DateTimeZone( wp_timezone_string() ) );
     76        // Set time to beginning of today.
     77        $today->setTime( 0, 0, 0 );
     78
     79        $args = [
     80            'post_type'      => Registrations::get_post_type(),
     81            'post_status'    => 'publish',
     82            'author'         => $user_id,
     83            'date_query'     => [
     84                [
     85                    'after'     => $today->format( 'Y-m-d H:i:s' ), // After start of today.
     86                    'inclusive' => true,
     87                ],
     88            ],
     89            'posts_per_page' => -1,
     90            'fields'         => 'ids',
     91        ];
     92
     93        $query = new \WP_Query( $args );
     94        return $query->found_posts;
    5595    }
    5696
Note: See TracChangeset for help on using the changeset viewer.