Making WordPress.org


Ignore:
Timestamp:
07/07/2022 09:55:43 PM (4 years ago)
Author:
iandunn
Message:

GP Profiles: Ignore updates from outside GP to avoid duplicates.

There are some custom plugins that create/update translations outside of the normal core GlotPress user flows, like WordPressdotorg\GlotPress\Plugin_Directory\Sync\Translation_Sync\sync_translations(). This shouldn't fire for those, because it would be counted as two actions for the same string.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-profiles/profiles.php

    r11943 r11950  
    2727function register_callbacks() : void {
    2828        // Ignore programmatic actions, only notify for user-initiated actions.
    29         if ( empty( $_POST ) || ! is_user_logged_in() ) {
     29        if ( empty( $_POST ) || ! is_user_logged_in() || ! wp_using_themes() ) {
    3030                return;
    3131        }
     
    4040 */
    4141function add_single_translation_activity( GP_Translation $new_translation, GP_Translation $previous_translation = null ) : void {
    42         $bulk_request   = gp_post( 'bulk', null );
    43         $import_request = isset( $_FILES['import-file'] );
    44 
    45         // Bulk actions are handled by `add_bulk_translation_activity()`. Importing is blocked by
    46         // https://github.com/GlotPress/GlotPress/issues/1467.
    47         if ( $bulk_request || $import_request ) {
    48                 return;
    49         }
    50 
    51         if ( ! get_userdata( $new_translation->user_id ) ) {
     42        if ( ! should_notify( $new_translation->user_id ) ) {
    5243                return;
    5344        }
     
    8677
    8778        Profiles_API\api( $request_body );
     79}
     80
     81/**
     82 * Determine if a notification should be sent for the current `gp_translation_{created|saved}` action.
     83 *
     84 * This isn't needed for `gp_translation_set_bulk_action_post` callbacks, since that only gets triggered by the
     85 * normal UI flow.
     86 */
     87function should_notify( int $user_id ) : bool {
     88        $notify = true;
     89
     90        /*
     91         * `GP_Route_Translation::translations_post()` runs at `template_redirect:10`, and processes user-initiated
     92         * actions. After that point, other code -- like
     93         * `WordPressdotorg\GlotPress\Plugin_Directory\Sync\Translation_Sync\sync_translations` -- may create
     94         * translations programmatically.
     95         */
     96        if ( did_action( 'get_header' ) || did_action( 'shutdown' ) ) {
     97                $notify = false;
     98        }
     99
     100        $bulk_request   = gp_post( 'bulk', null );
     101        $import_request = isset( $_FILES['import-file'] );
     102
     103        // Bulk actions are handled by `add_bulk_translation_activity()`. Importing is blocked by
     104        // https://github.com/GlotPress/GlotPress/issues/1467.
     105        if ( $bulk_request || $import_request ) {
     106                $notify = false;
     107        }
     108
     109        if ( ! get_userdata( $user_id ) ) {
     110                $notify = false;
     111        }
     112
     113        return $notify;
    88114}
    89115
Note: See TracChangeset for help on using the changeset viewer.