Making WordPress.org

Changeset 11830


Ignore:
Timestamp:
05/10/2022 11:25:37 PM (2 years ago)
Author:
iandunn
Message:

Profiles: Use exceptions to simplify error flows and centralize logs.

Adding logging to the previous structure would have created a lot of redundant code.

File:
1 edited

Legend:

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

    r11829 r11830  
    159159         */
    160160        public function handle_activity() {
    161             /*
    162              * This is useful for testing on your sandbox.
    163              *
    164              * e.g., Edit `$_POST['user_id']` so that activity goes to a test account rather than a real one.
    165              */
    166             do_action( 'wporg_profiles_before_handle_activity' );
    167 
    168             // Return error if not a valid activity request.
    169             if ( true !== apply_filters( 'wporg_is_valid_activity_request', false ) ) {
    170                 die( '-1 Not a valid activity request' );
    171             }
    172 
    173             // Return error if activities are not enabled.
    174             if ( ! bp_is_active( 'activity' ) ) {
    175                 die( '-1 Activity component not activated' );
    176             }
    177 
    178161            try {
    179                 $activity = $this->sanitize_activity( $_POST );
     162                /*
     163                 * This is useful for testing on your sandbox.
     164                 *
     165                 * e.g., Edit `$_POST['user_id']` so that activity goes to a test account rather than a real one.
     166                 */
     167                do_action( 'wporg_profiles_before_handle_activity' );
     168
     169                // Return error if not a valid activity request.
     170                if ( true !== apply_filters( 'wporg_is_valid_activity_request', false ) ) {
     171                    throw new Exception( '-1 Not a valid activity request' );
     172                }
     173
     174                // Return error if activities are not enabled.
     175                if ( ! bp_is_active( 'activity' ) ) {
     176                    throw new Exception( '-1 Activity component not activated' );
     177                }
     178
     179                $source = sanitize_text_field( $_POST['source'] ?? $_POST['component'] );
     180
     181
     182                // The `slack` source requires multiples users, so the parameters are named differently.
     183                if ( empty( $_POST['user'] ) && empty( $_POST['user_id'] ) && 'slack' !== $source ) {
     184                    throw new Exception( '-1 No user specified.' );
     185                }
     186
     187                // Disable default BP moderation
     188                remove_action( 'bp_activity_before_save', 'bp_activity_check_moderation_keys', 2 );
     189                remove_action( 'bp_activity_before_save', 'bp_activity_check_blacklist_keys',  2 );
     190
     191                // Disable requirement that user have a display_name set
     192                remove_filter( 'bp_activity_before_save', 'bporg_activity_requires_display_name' );
     193
     194                // If an activity doesn't require special logic, then `add_activity()` can be called directly. Compare
     195                // Learn and Slack to see the difference.
     196                switch ( $source ) {
     197                    case 'forum':
     198                        $activity_id = $this->handle_forum_activity();
     199                        break;
     200                    case 'learn':
     201                        $activity_id = $this->add_activity( $activity );
     202                        break;
     203                    case 'plugin':
     204                        $activity_id = $this->handle_plugin_activity();
     205                        break;
     206                    case 'theme':
     207                        $activity_id = $this->handle_theme_activity();
     208                        break;
     209                    case 'trac':
     210                        $activity_id = $this->handle_trac_activity();
     211                        break;
     212                    case 'wordcamp':
     213                        $activity_id = $this->handle_wordcamp_activity();
     214                        break;
     215                    case 'wordpress':
     216                        $activity_id = $this->handle_wordpress_activity();
     217                        break;
     218                    case 'slack':
     219                        $activity_id = $this->handle_slack_activity();
     220                        break;
     221                    default:
     222                        throw new Exception( '-1 Unrecognized activity source' );
     223                        break;
     224                }
     225
     226                if ( is_wp_error( $activity_id ) ) {
     227                    throw new Exception( '-1 Unable to save activity: ' . $activity_id->get_error_message() );
     228                } elseif ( str_starts_with( $activity_id, '-1' ) ) {
     229                    throw new Exception( $activity_id );
     230                } elseif ( false === $activity_id || intval( $activity_id ) <= 0 ) {
     231                    throw new Exception( '-1 Unable to save activity' );
     232                }
     233
     234                $response = '1';
     235
    180236            } catch ( Exception $exception ) {
    181                 die( wp_kses_post( $exception->getMessage() ) );
    182             }
    183 
    184             $source = sanitize_text_field( $_POST['source'] );
    185             // The `slack` source requires multiples users, so the parameters are named differently.
    186             if ( empty( $_POST['user'] ) && empty( $_POST['user_id'] ) && 'slack' !== $source ) {
    187                 die( '-1 No user specified.' );
    188             }
    189 
    190             // Disable default BP moderation
    191             remove_action( 'bp_activity_before_save', 'bp_activity_check_moderation_keys', 2 );
    192             remove_action( 'bp_activity_before_save', 'bp_activity_check_blacklist_keys',  2 );
    193 
    194             // Disable requirement that user have a display_name set
    195             remove_filter( 'bp_activity_before_save', 'bporg_activity_requires_display_name' );
    196 
    197             // If an activity doesn't require special logic, then `add_activity()` can be called directly. Compare
    198             // Learn and Slack to see the difference.
    199             switch ( $source ) {
    200                 case 'forum':
    201                     $activity_id = $this->handle_forum_activity();
    202                     break;
    203                 case 'learn':
    204                     $activity_id = $this->add_activity( $activity );
    205                     break;
    206                 case 'plugin':
    207                     $activity_id = $this->handle_plugin_activity();
    208                     break;
    209                 case 'theme':
    210                     $activity_id = $this->handle_theme_activity();
    211                     break;
    212                 case 'trac':
    213                     $activity_id = $this->handle_trac_activity();
    214                     break;
    215                 case 'wordcamp':
    216                     $activity_id = $this->handle_wordcamp_activity();
    217                     break;
    218                 case 'wordpress':
    219                     $activity_id = $this->handle_wordpress_activity();
    220                     break;
    221                 case 'slack':
    222                     $activity_id = $this->handle_slack_activity();
    223                     break;
    224                 default:
    225                     $activity_id = '-1 Unrecognized activity source';
    226                     break;
    227             }
    228 
    229             if ( false === $activity_id ) {
    230                 $activity_id = '-1 Unable to save activity';
    231             }
    232 
    233             $success = intval( $activity_id ) > 0 ? '1' : $activity_id;
    234             die( $success );
     237                trigger_error( $exception->getMessage(), E_USER_WARNING );
     238
     239                $response = $exception->getMessage();
     240            }
     241
     242            die( $response );
    235243        }
    236244
Note: See TracChangeset for help on using the changeset viewer.