Making WordPress.org


Ignore:
Timestamp:
09/10/2021 10:25:05 PM (5 years ago)
Author:
coreymckrill
Message:

WordPress.org Learn: Sync with GitHub

https://github.com/WordPress/learn/compare/13104703090bd3b7f59cb7794a3a0ede50f4dfe8...b786efa59c12a5bb4c987f84ba5fc56e9c776c8f

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/admin.php

    r11057 r11225  
    1818add_action( 'restrict_manage_posts', __NAMESPACE__ . '\add_workshop_list_table_filters', 10, 2 );
    1919add_action( 'pre_get_posts', __NAMESPACE__ . '\handle_workshop_list_table_filters' );
     20add_filter( 'display_post_states', __NAMESPACE__ . '\add_post_states', 10, 2 );
     21foreach ( array( 'lesson-plan', 'wporg_workshop', 'course', 'lesson' ) as $pt ) {
     22        add_filter( 'views_edit-' . $pt, __NAMESPACE__ . '\list_table_views' );
     23}
     24add_action( 'pre_get_posts', __NAMESPACE__ . '\handle_list_table_views' );
    2025
    2126/**
     
    208213        }
    209214}
     215
     216/**
     217 * Custom post states for list tables.
     218 *
     219 * @param array    $post_states
     220 * @param \WP_Post $post
     221 *
     222 * @return mixed
     223 */
     224function add_post_states( $post_states, $post ) {
     225        $expiration_date = $post->expiration_date;
     226
     227        if ( $expiration_date ) {
     228                $exp = strtotime( $expiration_date );
     229                $now = strtotime( 'now' );
     230
     231                if ( $exp > $now ) {
     232                        $post_states[] = sprintf(
     233                                esc_html__( 'Expires in %s', 'wporg-learn' ),
     234                                esc_html( human_time_diff( $now, $exp ) )
     235                        );
     236                } else {
     237                        $post_states[] = sprintf(
     238                                '<span style="color: #b32d2e;">%s</span>',
     239                                esc_html__( 'Expired', 'wporg-learn' )
     240                        );
     241                }
     242        }
     243
     244        return $post_states;
     245}
     246
     247/**
     248 * Add view links to the patterns list table.
     249 *
     250 * @param array $views
     251 *
     252 * @return array
     253 */
     254function list_table_views( $views ) {
     255        global $typenow;
     256
     257        $wants_expired = filter_input( INPUT_GET, 'expired', FILTER_VALIDATE_BOOLEAN );
     258
     259        $url = add_query_arg(
     260                array(
     261                        'post_type' => $typenow,
     262                        'expired' => 1,
     263                ),
     264                admin_url( 'edit.php' )
     265        );
     266
     267        $extra_attributes = '';
     268        if ( $wants_expired ) {
     269                $extra_attributes = ' class="current" aria-current="page"';
     270        }
     271
     272        $expired_posts_query = new \WP_Query( array(
     273                'post_type'   => $typenow,
     274                'post_status' => 'any',
     275                'numberposts' => 1,
     276                'meta_query'  => array(
     277                        array(
     278                                'key'     => 'expiration_date',
     279                                'value'   => current_time( 'mysql' ),
     280                                'compare' => '<',
     281                        ),
     282                ),
     283        ) );
     284
     285        $views['expired'] = sprintf(
     286                '<a href="%s"%s>%s</a>',
     287                esc_url( $url ),
     288                $extra_attributes,
     289                sprintf(
     290                        /* translators: %s: Number of posts. */
     291                        _n(
     292                                'Expired <span class="count">(%s)</span>',
     293                                'Expired <span class="count">(%s)</span>',
     294                                $expired_posts_query->found_posts,
     295                                'wporg-learn'
     296                        ),
     297                        number_format_i18n( $expired_posts_query->found_posts )
     298                )
     299        );
     300
     301        return $views;
     302}
     303
     304
     305/**
     306 * Modify the query that populates the patterns list table.
     307 *
     308 * @param WP_Query $query
     309 *
     310 * @return void
     311 */
     312function handle_list_table_views( WP_Query $query ) {
     313        global $typenow;
     314
     315        $wants_expired = filter_input( INPUT_GET, 'expired', FILTER_VALIDATE_BOOLEAN );
     316
     317        if ( ! is_admin() || ! $query->is_main_query() ) {
     318                return;
     319        }
     320
     321        $current_screen = get_current_screen();
     322
     323        if ( 'edit-' . $typenow === $current_screen->id ) {
     324                if ( $wants_expired ) {
     325                        $meta_query = $query->get( 'meta_query', array() );
     326
     327                        $meta_query[] = array(
     328                                'key'     => 'expiration_date',
     329                                'value'   => current_time( 'mysql' ),
     330                                'compare' => '<',
     331                        );
     332
     333                        $query->set( 'meta_query', $meta_query );
     334                }
     335        }
     336}
Note: See TracChangeset for help on using the changeset viewer.