Making WordPress.org

Changeset 12062


Ignore:
Timestamp:
09/14/2022 04:56:00 PM (4 years ago)
Author:
renyot
Message:

Profiles: Add activity when added as a presenter for Learn tutorial.

See https://github.com/WordPress/five-for-the-future/issues/179

File:
1 edited

Legend:

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

    r12020 r12062  
    1111class WPOrg_WP_Activity_Notifier {
    1212    /**
    13      * @var WPOrg_WP_Activity_Notifier The singleton instance.
     13     * The singleton instance.
     14     *
     15     * @var WPOrg_WP_Activity_Notifier
    1416     */
    1517    private static $instance;
     
    154156        }
    155157
     158        /**
     159         * Gutenberg sends two requests when we hit the Publish/Update button.
     160         * https://github.com/WordPress/wordpress.org/pull/84#discussion_r919290748
     161         *
     162         * For the first request, $old_status would be different from $new_status,
     163         * if the post, for example, is changed from draft to published.
     164         * For the second request (from the same Publish/Update button hit),
     165         * $old_status would be the same as $new_status in the same example,
     166         * both their values would be 'publish'.
     167         *
     168         * This brings the result that only the first request from Gutenberg could
     169         * pass the condition if ($old_status == $new_status) { return; }.
     170         *
     171         * However, only the second request would carry the data from meta boxes,
     172         * which is what we need here, so we need to put this logic above that
     173         * condition.
     174         */
     175        if ( 'wporg_workshop' === $post->post_type ) {
     176            $this->notify_workshop_presenter( $post );
     177        }
     178
    156179        if ( $old_status == $new_status ) {
    157180            return;
     
    165188    }
    166189
     190    /**
     191     * Sends activity notification for workshop presenter.
     192     *
     193     * @param WP_Post $post Post object.
     194     */
     195    private function notify_workshop_presenter( $post ) {
     196        if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
     197            return;
     198        }
     199
     200        $presenter_wporg_username = filter_input( INPUT_POST, 'presenter-wporg-username' );
     201
     202        if ( empty( $presenter_wporg_username ) ) {
     203            return;
     204        }
     205
     206        $unique_presenter_wporg_username = array_unique( array_map( 'trim', explode( ',', $presenter_wporg_username ) ) );
     207        $permalink                       = get_permalink( $post );
     208        $title                           = wp_kses_data( $post->post_title );
     209        $content                         = wp_trim_words(
     210            strip_shortcodes( has_excerpt( $post ) ? $post->post_excerpt : $post->post_content ),
     211            55
     212        );
     213
     214        foreach ( $unique_presenter_wporg_username as $username ) {
     215            $user_id = get_user_by( 'slug', strtolower( $username ) )->ID;
     216
     217            if ( ! $user_id ) {
     218                continue;
     219            }
     220
     221            $args = array(
     222                'action'       => 'wporg_handle_activity',
     223                'component'    => 'learn',
     224                'type'         => 'workshop_presenter_assign',
     225                'user_id'      => $user_id,
     226                'primary_link' => $permalink,
     227                'item_id'      => $post->ID,
     228                'content'      => $content,
     229                'message'      => sprintf(
     230                    'Assigned as a presenter on the Learn WordPress tutorial, <i><a href="%s">%s</a></i>',
     231                    $permalink,
     232                    $title,
     233                ),
     234            );
     235
     236            Profiles\api( $args );
     237        }
     238    }
    167239
    168240    /**
     
    294366     * @param int    $topic_id Topic ID.
    295367     */
    296     private function _notify_forum_topic_payload( $activity, $topic_id ) {
     368    private function notify_forum_topic_payload( $activity, $topic_id ) {
    297369        if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
    298370            return;
     
    303375        }
    304376
    305         if ( ! in_array( $activity, array( 'create-topic', 'remove-topic' ) ) ) {
     377        if ( ! in_array( $activity, array( 'create-topic', 'remove-topic' ), true ) ) {
    306378            return;
    307379        }
     
    316388
    317389        $args = array(
    318             'action'    => 'wporg_handle_activity',
    319             'activity'  => $activity,
    320             'source'    => 'forum',
    321             'user'      => get_user_by( 'id', bbp_get_topic_author_id( $topic_id ) )->user_login,
    322             'post_id'   => '',
    323             'topic_id'  => $topic_id,
    324             'forum_id'  => bbp_get_topic_forum_id( $topic_id ),
    325             'title'     => strip_tags( bbp_get_topic_title( $topic_id ) ),
    326             'url'       => $url,
    327             'message'   => bbp_get_topic_excerpt( $topic_id, 55 ),
    328             'site'      => get_bloginfo( 'name' ),
    329             'site_url'  => site_url(),
     390            'action'   => 'wporg_handle_activity',
     391            'activity' => $activity,
     392            'source'   => 'forum',
     393            'user'     => get_user_by( 'id', bbp_get_topic_author_id( $topic_id ) )->user_login,
     394            'post_id'  => '',
     395            'topic_id' => $topic_id,
     396            'forum_id' => bbp_get_topic_forum_id( $topic_id ),
     397            'title'    => strip_tags( bbp_get_topic_title( $topic_id ) ),
     398            'url'      => $url,
     399            'message'  => bbp_get_topic_excerpt( $topic_id, 55 ),
     400            'site'     => get_bloginfo( 'name' ),
     401            'site_url' => site_url(),
    330402        );
    331403
     
    339411     */
    340412    public function notify_forum_new_topic( $topic_id ) {
    341         $this->_notify_forum_topic_payload( 'create-topic', $topic_id );
     413        $this->notify_forum_topic_payload( 'create-topic', $topic_id );
    342414    }
    343415
     
    348420     */
    349421    public function notify_forum_remove_topic( $topic_id ) {
    350         $this->_notify_forum_topic_payload( 'remove-topic', $topic_id );
     422        $this->notify_forum_topic_payload( 'remove-topic', $topic_id );
    351423    }
    352424
     
    359431     * @param int    $reply_id Reply ID.
    360432     */
    361     private function _notify_forum_reply_payload( $activity, $reply_id ) {
     433    private function notify_forum_reply_payload( $activity, $reply_id ) {
    362434        if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
    363435            return;
     
    368440        }
    369441
    370         if ( ! in_array( $activity, array( 'create-reply', 'remove-reply' ) ) ) {
     442        if ( ! in_array( $activity, array( 'create-reply', 'remove-reply' ), true ) ) {
    371443            return;
    372444        }
     
    381453
    382454        $args = array(
    383             'action'    => 'wporg_handle_activity',
    384             'activity'  => $activity,
    385             'source'    => 'forum',
    386             'user'      => get_user_by( 'id', bbp_get_reply_author_id( $reply_id ) )->user_login,
    387             'post_id'   => $reply_id,
    388             'topic_id'  => bbp_get_reply_topic_id( $reply_id ),
    389             'forum_id'  => bbp_get_reply_forum_id( $reply_id ),
    390             'title'     => strip_tags( bbp_get_reply_topic_title( $reply_id ) ),
    391             'url'       => $url,
    392             'message'   => $this->get_reply_excerpt( $reply_id, 15 ),
    393             'site'      => get_bloginfo( 'name' ),
    394             'site_url'  => site_url(),
     455            'action'   => 'wporg_handle_activity',
     456            'activity' => $activity,
     457            'source'   => 'forum',
     458            'user'     => get_user_by( 'id', bbp_get_reply_author_id( $reply_id ) )->user_login,
     459            'post_id'  => $reply_id,
     460            'topic_id' => bbp_get_reply_topic_id( $reply_id ),
     461            'forum_id' => bbp_get_reply_forum_id( $reply_id ),
     462            'title'    => strip_tags( bbp_get_reply_topic_title( $reply_id ) ),
     463            'url'      => $url,
     464            'message'  => $this->get_reply_excerpt( $reply_id, 15 ),
     465            'site'     => get_bloginfo( 'name' ),
     466            'site_url' => site_url(),
    395467        );
    396468
     
    404476     */
    405477    public function notify_forum_new_reply( $reply_id ) {
    406         $this->_notify_forum_reply_payload( 'create-reply', $reply_id );
     478        $this->notify_forum_reply_payload( 'create-reply', $reply_id );
    407479    }
    408480
     
    413485     */
    414486    public function notify_forum_remove_reply( $reply_id ) {
    415         $this->_notify_forum_reply_payload( 'remove-reply', $reply_id );
     487        $this->notify_forum_reply_payload( 'remove-reply', $reply_id );
    416488    }
    417489
     
    452524    public function trim_text( $text, $length = 15, $trim_style = 'words' ) {
    453525        $length     = (int) $length;
    454         $trim_style = in_array( $trim_style, array( 'chars', 'words' ) ) ? $trim_style : 'words';
     526        $trim_style = in_array( $trim_style, array( 'chars', 'words' ), true ) ? $trim_style : 'words';
    455527
    456528        // Remove blockquoted text since the text isn't original.
     
    479551                $text .= '&hellip;';
    480552            }
    481         } else {
    482             // Else trim by words.
     553        } else { // Else trim by words.
    483554            $text = wp_trim_words( $text, $length );
    484555        }
Note: See TracChangeset for help on using the changeset viewer.