Making WordPress.org


Ignore:
Timestamp:
11/23/2022 06:22:00 PM (4 years ago)
Author:
amieiro
Message:

Translate: Add mentions to comment box and Disable only Feedback <summary> events

​https://github.com/GlotPress/gp-translation-helpers/pull/113
​https://github.com/GlotPress/gp-translation-helpers/pull/143

File:
1 edited

Legend:

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

    r12063 r12274  
    9595                                2
    9696                        );
     97                        add_filter(
     98                                'gp_mentions_list',
     99                                function( $result, $comments, $locale, $original_id ) {
     100                                        $validator_email_addresses  = ( $locale && $original_id ) ? WPorg_GlotPress_Notifications::get_validator_details_for_original_id( $locale, $original_id ) : array();
     101                                        $commenters_email_addresses = array_values( GP_Notifications::get_commenters_email_addresses( $comments ) );
     102
     103                                        // Remove commenter email if it already exists as a GTE.
     104                                        $commenters_email_addresses = array_filter(
     105                                                $commenters_email_addresses,
     106                                                function( $commenter_email ) use ( $validator_email_addresses ) {
     107                                                        return ( ! in_array( $commenter_email, array_column( $validator_email_addresses, 'email' ) ) );
     108                                                }
     109                                        );
     110
     111                                        $commenters_email_role = array_map(
     112                                                function( $commenter_email ) {
     113                                                        return(
     114                                                        array(
     115                                                                'role'  => 'commenter',
     116                                                                'email' => $commenter_email,
     117                                                        )
     118                                                        );
     119                                                },
     120                                                $commenters_email_addresses
     121                                        );
     122
     123                                        $all_email_addresses = array_merge(
     124                                                $validator_email_addresses,
     125                                                $commenters_email_role
     126                                        );
     127
     128                                        $current_user       = wp_get_current_user();
     129                                        $current_user_email = $current_user->user_email;
     130
     131                                        // Find all instances of the logged_in user in the array
     132                                        $user_search_result = array_keys( array_column( $all_email_addresses, 'email' ), $current_user_email );
     133
     134                                        if ( false !== $user_search_result ) {
     135                                                foreach ( $user_search_result as $index ) {
     136                                                        unset( $all_email_addresses[ $index ] );
     137                                                }
     138                                                $all_email_addresses = array_values( $all_email_addresses );
     139                                        }
     140
     141                                        $users = array_map(
     142                                                function( $mentionable_user ) {
     143                                                        $email = $mentionable_user;
     144                                                        $role  = '';
     145                                                        if ( is_array( $mentionable_user ) ) {
     146                                                                $email = $mentionable_user['email'];
     147                                                                $role  = ! ( 'commenter' === $mentionable_user['role'] ) ? ' - ' . $mentionable_user['role'] : '';
     148                                                        }
     149
     150                                                                $user = get_user_by( 'email', $email );
     151                                                                return array(
     152                                                                        'ID'            => $user->ID,
     153                                                                        'user_login'    => $user->user_login,
     154                                                                        'user_nicename' => $user->user_nicename . $role,
     155                                                                        'display_name'  => '',
     156                                                                        'source'        => array( 'translators' ),
     157                                                                        'image_URL'     => get_avatar_url( $user->ID ),
     158                                                                );
     159                                                },
     160                                                $all_email_addresses
     161                                        );
     162                                                        return $users;
     163                                },
     164                                10,
     165                                4
     166                        );
    97167                }
    98168        }
    … …  
    118188                return array_merge( $email_addresses, self::get_clpte_email_addresses_by_project( $original->id ) );
    119189        }
     190
     191        /**
     192         * Gets the email addresses and roles(GTE/PTE/CLPTE) of all project validators: GTE, PTE and CLPTE.
     193         *
     194         * @since 0.0.2
     195         *
     196         * @param string $locale  The locale for the translation.
     197         * @param int    $original_id  The original id for the string.
     198         *
     199         * @return array    The emails and roles(GTE/PTE/CLPTE) of the validators.
     200         */
     201        public static function get_validator_details_for_original_id( $locale, $original_id ): array {
     202                $gtes_email_and_role = array_map(
     203                        function( $gte_email ) {
     204                                return array(
     205                                        'role'  => 'GTE',
     206                                        'email' => $gte_email,
     207                                );
     208                        },
     209                        self::get_gte_email_addresses( $locale )
     210                );
     211
     212                $ptes_email_and_role = array_map(
     213                        function( $pte_email ) {
     214                                return array(
     215                                        'role'  => 'GTE',
     216                                        'email' => $pte_email,
     217                                );
     218                        },
     219                        self::get_pte_email_addresses_by_project_and_locale( $original_id, $locale )
     220                );
     221
     222                $clptes_email_and_role = array_map(
     223                        function( $clpte_email ) {
     224                                return array(
     225                                        'role'  => 'GTE',
     226                                        'email' => $clpte_email,
     227                                );
     228                        },
     229                        self::get_clpte_email_addresses_by_project( $original_id )
     230                );
     231
     232                return array_merge(
     233                        array_merge( $gtes_email_and_role, $ptes_email_and_role ),
     234                        $clptes_email_and_role
     235                );
     236        }
     237
    120238
    121239        /**
Note: See TracChangeset for help on using the changeset viewer.