Making WordPress.org


Ignore:
Timestamp:
08/31/2016 06:16:14 PM (10 years ago)
Author:
coffee2code
Message:

Profiles Activity Handler: Improve forum activity handler.

  • Add explicit 'activity' argument and remove the limited 'newType' argument.
  • Handle requests to remove activity related to a topic or reply (such as when trashed, unapproved, or marked as spam).
  • Activity removal requests simply mark the activity as spam, which facilitate revival if untrashed/approved/unspammed.
  • Check for existence of matching activity entry. For instances where activity was to be created but already exists, then simply unspam it.
  • Check that all required request fields are supplied.
  • More thorough sanitization of submitted data.

See #129.

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

    r3806 r3893  
    113113
    114114                        return $activated;
     115                }
     116
     117                /**
     118                 * Checks that required POST arguments are defined and have a value.
     119                 *
     120                 * Ends request and reports on any missing arguments, if necessary.
     121                 *
     122                 * @param array $args The array of required POST arguments.
     123                 */
     124                protected function require_args( $args ) {
     125                        $missing = array();
     126
     127                        foreach ( $args as $arg ) {
     128                                if ( empty( $_POST[ $arg ] ) ) {
     129                                        $missing[] = $arg;
     130                                }
     131                        }
     132
     133                        if ( $missing ) {
     134                                die( '-1 Required argument(s) are missing: ' . implode( ', ', $missing ) );
     135                        }
    115136                }
    116137
     
    195216                 *  - Creating new topic
    196217                 *  - Replying to a topic
     218                 *  - Removing a topic
     219                 *  - Removing a topic reply
    197220                 */
    198221                private function handle_forum_activity() {
    199222                        $user = $this->get_user( $_POST['user'] );
    200 
     223                        $type = '';
     224
     225                        // Check for valid user.
    201226                        if ( ! $user ) {
    202227                                return '-1 Activity reported for unrecognized user : ' . sanitize_text_field( $_POST['user'] );
    203228                        }
    204229
    205                         if ( '1' == $_POST['newTopic'] ) {
    206                                 $action = sprintf(
    207                                         __( 'Created a topic, <a href="%s">%s</a>, on the site %s', 'wporg' ),
    208                                         esc_url( $_POST['url'] ),
    209                                         $_POST['title'],
    210                                         $_POST['site']
    211                                 );
    212                                 $type = 'forum_topic_create';
     230                        // Check for valid forum activities.
     231                        $activities = array(
     232                                'create-topic' => 'forum_topic_create',
     233                                'remove-topic' => 'forum_topic_remove',
     234                                'create-reply' => 'forum_reply_create',
     235                                'remove-reply' => 'forum_reply_remove',
     236                        );
     237                        if ( ! empty( $_POST['activity'] ) && ! empty( $activities[ $_POST['activity'] ] ) ) {
     238                                $type = $activities[ $_POST['activity'] ];
    213239                        } else {
    214                                 $action = sprintf(
    215                                         __( 'Posted a <a href="%s">reply</a> to %s, on the site %s', 'wporg' ),
    216                                         esc_url( $_POST['url'] ),
    217                                         $_POST['title'],
    218                                         $_POST['site']
    219                                 );
    220                                 $type = 'forum_reply_create';
    221                         }
    222 
     240                                return '-1 Unrecognized forum activity.';
     241                        }
     242
     243                        // Check for required args.
     244                        $required_args = array( 'forum_id' );
     245                        if ( in_array( $type, array( 'forum_topic_create', 'forum_reply_create' ) ) ) {
     246                                $required_args[] = 'message';
     247                                $required_args[] = 'url';
     248                                $required_args = array_merge( $required_args, array( 'title', 'site', 'message', 'url' ) );
     249                        }
     250                        if ( in_array( $type, array( 'forum_topic_create', 'forum_topic_remove' ) ) ) {
     251                                $required_args[] = 'topic_id';
     252                        } else {
     253                                $required_args[] = 'post_id';
     254                        }
     255                        $this->require_args( $required_args );
     256
     257                        // Determine 'item_id' value based on context.
     258                        $item_id = in_array( $type, array( 'forum_topic_create', 'forum_topic_remove' ) ) ?
     259                                intval( $_POST['topic_id'] ) :
     260                                intval( $_POST['post_id'] );
     261
     262                        // Find an existing activity uniquely identified by the reported criteria.
     263                        // For a creation, this prevents duplication and permits a previously
     264                        // trashed/spammed/unapproved activity to be restored.
     265                        // For a removal, this is used to find the activity to mark it as spam.
     266                        // Note: It's unlikely, but possible, that this is a non-unique request.
    223267                        $args = array(
    224268                                'user_id'           => $user->ID,
    225                                 'action'            => $action,
    226                                 'content'           => $_POST['message'],
    227                                 'primary_link'      => $_POST['url'],
    228269                                'component'         => 'forums',
    229                                 'type'              => $type,
    230                                 'item_id'           => 'forum_topic_create' ? intval( $_POST['topic_id'] ) : intval( $_POST['post_id'] ),
     270                                'type'              => str_replace( 'remove', 'create', $type ),
     271                                'item_id'           => $item_id,
    231272                                'secondary_item_id' => intval( $_POST['forum_id'] ),
    232                                 'hide_sitewide'     => false,
    233273                        );
    234 
    235                         return bp_activity_add( $args );
     274                        $activity_id = bp_activity_get_activity_id( $args );
     275                        $activity_obj = $activity_id ? new BP_Activity_Activity( $activity_id ) : false;
     276
     277                        // Record the creation of a topic or reply.
     278                        if ( in_array( $type, array( 'forum_topic_create', 'forum_reply_create' ) ) ) {
     279                                if ( $activity_obj ) {
     280                                        bp_activity_mark_as_ham( $activity_obj, 'by_source' );
     281                                        $activity_obj->save();
     282
     283                                        return true;
     284                                }
     285
     286                                // Action message for topic creation.
     287                                if ( 'forum_topic_create' === $type ) {
     288                                        $action = sprintf(
     289                                                __( 'Created a topic, <i><a href="%s">%s</a></i>, on the site %s', 'wporg' ),
     290                                                esc_url( $_POST['url'] ),
     291                                                esc_html( $_POST['title'] ),
     292                                                esc_html( $_POST['site'] )
     293                                        );
     294                                }
     295                                // Action message for reply creation.
     296                                else {
     297                                        $action = sprintf(
     298                                                __( 'Posted a <a href="%s">reply</a> to <i>%s</i>, on the site %s', 'wporg' ),
     299                                                esc_url( $_POST['url'] ),
     300                                                esc_html( $_POST['title'] ),
     301                                                esc_html( $_POST['site'] )
     302                                        );
     303                                }
     304
     305                                $args = array(
     306                                        'user_id'           => $user->ID,
     307                                        'action'            => $action,
     308                                        'content'           => esc_html( $_POST['message'] ),
     309                                        'primary_link'      => esc_url( $_POST['url'] ),
     310                                        'component'         => 'forums',
     311                                        'type'              => $type,
     312                                        'item_id'           => $item_id,
     313                                        'secondary_item_id' => intval( $_POST['forum_id'] ),
     314                                        'hide_sitewide'     => false,
     315                                );
     316
     317                                return bp_activity_add( $args );
     318                        }
     319                        // Remove activity related to a topic or reply.
     320                        elseif ( in_array( $type, array( 'forum_topic_remove', 'forum_reply_remove' ) ) ) {
     321                                if ( ! $activity_obj ) {
     322                                        return '-1 Activity not previously reported.';
     323                                }
     324
     325                                bp_activity_mark_as_spam( $activity_obj, 'by_source' );
     326                                $activity_obj->save();
     327
     328                                return true;
     329                        }
    236330                }
    237331
Note: See TracChangeset for help on using the changeset viewer.