Making WordPress.org


Ignore:
Timestamp:
11/23/2022 07:03:09 PM (3 years ago)
Author:
amieiro
Message:

Translate: Add list of links for filters

https://github.com/GlotPress/gp-translation-helpers/pull/137

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/includes/class-gp-route-translation-helpers.php

    r12063 r12275  
    3939            $this->die_with_404();
    4040        }
    41         $all_comments_count  = count(
    42             get_comments(
    43                 array(
    44                     'meta_key'   => 'locale',
    45                     'meta_value' => $locale_slug,
    46                 )
    47             )
    48         );
     41        $user_id = wp_get_current_user()->ID;
     42
     43        $all_comments_post_ids = $this->get_comment_post_ids( $locale_slug );
     44        $comments_count        = count( $all_comments_post_ids );
     45        $comment_post_ids      = $all_comments_post_ids;
     46
     47        $participating          = $this->get_user_comments( $locale_slug, $user_id );
     48        $participating_post_ids = array_unique( array_column( $participating, 'comment_post_ID' ) );
     49
     50        $not_participating_post_ids = array_diff( $all_comments_post_ids, $participating_post_ids );
     51
    4952        $comments_per_page   = 12;
    50         $total_pages         = (int) ceil( $all_comments_count / $comments_per_page );
    51         $page_num_from_query = get_query_var( 'page' );
     53        $page_num_from_query = (int) get_query_var( 'page' );
     54        $offset              = $page_num_from_query > 0 ? ( $page_num_from_query - 1 ) * $comments_per_page : 0;
     55        $filter              = isset( $_GET['filter'] ) ? esc_html( $_GET['filter'] ) : '';
    5256        $page_number         = ( ! empty( $page_num_from_query ) && is_int( $page_num_from_query ) ) ? $page_num_from_query : 1;
    5357        $gp_locale           = GP_Locales::by_slug( $locale_slug );
    54         $args                = array(
    55             'number'     => $comments_per_page,
     58        if ( 'participating' == $filter ) {
     59            $comment_post_ids = $participating_post_ids;
     60            $comments_count   = count( $participating_post_ids );
     61        }
     62        if ( 'not_participating' == $filter ) {
     63            $comment_post_ids = $not_participating_post_ids;
     64            $comments_count   = count( $not_participating_post_ids );
     65        }
     66        $total_pages = (int) ceil( $comments_count / $comments_per_page );
     67
     68        $post_ids = array();
     69
     70        $post_ids       = array_splice( $comment_post_ids, $offset, $comments_per_page );
     71        $args           = array(
    5672            'meta_key'   => 'locale',
    5773            'meta_value' => $locale_slug,
    58             'paged'      => $page_number,
    59         );
    60 
     74            'post__in'   => $post_ids,
     75        );
    6176        $comments_query = new WP_Comment_Query( $args );
    6277        $comments       = $comments_query->comments;
     
    374389        return $translation_permalink;
    375390    }
     391
     392    /**
     393     * Gets distinct post_ids for all comments made by user
     394     *
     395     * @param      string $locale_slug           The locale slug.
     396     * @param      int    $user_id           The user id.
     397     *
     398     * @return     array    The array of comment_post_IDs.
     399     */
     400    private function get_user_comments( $locale_slug, $user_id ) {
     401        $args     = array(
     402            'meta_key'   => 'locale',
     403            'meta_value' => $locale_slug,
     404            'user_id'    => $user_id,
     405        );
     406        $query    = new WP_Comment_Query( $args );
     407        $comments = $query->comments;
     408
     409        return $comments;
     410    }
     411
     412    /**
     413     * Run a query to fetch comment_post_ID of all comments
     414     *
     415     * @param string $locale_slug           The locale slug.
     416     *
     417     * @return array Array of unique comment_post_IDs for all comments.
     418     */
     419    private function get_comment_post_ids( $locale_slug ) {
     420        global $wpdb;
     421        return $wpdb->get_col(
     422            $wpdb->prepare(
     423                "SELECT DISTINCT {$wpdb->comments}.comment_post_ID FROM {$wpdb->comments} INNER JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id WHERE meta_key='locale' AND meta_value = %s ORDER BY comment_date DESC",
     424                $locale_slug
     425            )
     426        );
     427    }
    376428}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/gp-translation-helpers/templates/discussions-dashboard.php

    r12071 r12275  
    8989);
    9090
    91 
    92 
    9391?>
    9492
     93<div class="filter-toolbar">
     94    <a class="<?php echo ( ! $filter ) ? 'filter-current' : ''; ?>" href="<?php echo esc_url( remove_query_arg( array( 'filter', 'page' ) ) ); ?>">All&nbsp;(<?php echo esc_html( count( $all_comments_post_ids ) ); ?>)</a> <span class="separator">•</span>
     95    <a class="<?php echo ( 'participating' === $filter ) ? 'filter-current' : ''; ?>" href="<?php echo esc_url( add_query_arg( 'filter', 'participating', $_SERVER['REQUEST_URI'] ) ); ?>">Participating&nbsp;(<?php echo esc_html( count( $participating_post_ids ) ); ?>)</a> <span class="separator">•</span>
     96    <a class="<?php echo ( 'not_participating' === $filter ) ? 'filter-current' : ''; ?>" href="<?php echo esc_url( add_query_arg( 'filter', 'not_participating', $_SERVER['REQUEST_URI'] ) ); ?>">Not participating&nbsp;(<?php echo esc_html( count( $not_participating_post_ids ) ); ?>)</a>
     97</div>
    9598<table id="translations" class="translations clear">
    9699    <thead class="discussions-table-head">
Note: See TracChangeset for help on using the changeset viewer.