Making WordPress.org

Changeset 4043


Ignore:
Timestamp:
09/13/2016 01:49:12 AM (8 years ago)
Author:
jmdodd
Message:

Support Forums: Allow plugin/theme authors to set topics as sticky on their support forum.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-directory-compat.php

    r4041 r4043  
    184184                if ( class_exists( 'WPORG_Ratings' ) && class_exists( 'WordPressdotorg\Forums\Ratings_Compat' ) ) {
    185185                    $this->ratings = new Ratings_Compat( $this->compat(), $this->slug(), $this->taxonomy(), $this->get_object( $this->slug() ) );
     186                }
     187
     188                // Instantiate WPORG_Stickies mode for topic view.
     189                if ( class_exists( 'WordPressdotorg\Forums\Stickies_Compat' ) ) {
     190                    $this->stickies = new Stickies_Compat( $this->compat(), $this->slug(), $this->taxonomy(), $this->get_object( $this->slug() ), $this->term, $this->authors );
    186191                }
    187192
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-stickies-compat.php

    r4040 r4043  
    1313    var $term     = null;
    1414
    15     public function __construct( $compat, $slug, $taxonomy, $object, $term ) {
     15    public function __construct( $compat, $slug, $taxonomy, $object, $term, $authors = array() ) {
    1616        if ( empty( $compat ) || empty( $slug ) || empty( $taxonomy ) || empty( $object ) || empty( $term ) ) {
    1717            return;
     
    2323        $this->object   = $object;
    2424        $this->term     = $term;
    25 
     25        $this->authors  = $authors;
     26
     27        // Add compat stickies to sticky array.
    2628        add_filter( 'bbp_get_stickies', array( $this, 'get_compat_stickies' ), 10, 2 );
     29
     30        // Add topic toggle for compat authors.
     31        add_action( 'bbp_get_request', array( $this, 'sticky_handler' ) );
     32
     33        // Add link to topic admin.
     34        add_filter( 'bbp_topic_admin_links', array( $this, 'admin_links' ), 10, 2 );
    2735    }
    2836
     
    4250
    4351    /**
     52     */
     53    public function sticky_handler( $action = '' ) {
     54        // Bail if the action isn't meant for this function.
     55        if ( ! in_array( $action, array(
     56            'wporg_bbp_stick_topic',
     57            'wporg_bbp_unstick_topic',
     58        ) ) ) {
     59            return;
     60        }
     61
     62        // Bail if no topic id or term id are passed.
     63        if ( empty( $_GET['topic_id'] ) || empty( $_GET['term_id'] ) ) {
     64            return;
     65        }
     66
     67        // Get required data.
     68        $topic   = get_post( absint( $_GET['topic_id'] ) );
     69        $term    = get_term( absint( $_GET['term_id'] ) );
     70        $user_id = get_current_user_id();
     71
     72        // Check for empty topic or term id.
     73        if ( ! $topic || ! $term ) {
     74            bbp_add_error( 'wporg_bbp_sticky_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you sticking?', 'wporg-forums' ) );
     75
     76        // Check user.
     77        } elseif ( ! $this->user_can_stick( $user_id, $term->term_id, $topic->ID ) ) {
     78            bbp_add_error( 'wporg_bbp_sticky_logged_in', __( '<strong>ERROR</strong>: You do not have permission to do this!', 'wporg-forums' ) );
     79
     80        // Check nonce.
     81        } elseif( ! bbp_verify_nonce_request( 'toggle-topic-sticky_' . $topic->ID . '_' . $term->term_id ) ) {
     82            bbp_add_error( 'wporg_bbp_sticky_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'wporg-forums' ) );
     83        }
     84
     85        if ( bbp_has_errors() ) {
     86            return;
     87        }
     88
     89        $is_sticky = self::is_sticky( $term->term_id, $topic->ID );
     90        $success = false;
     91
     92        // Stick/unstick the topic.
     93        if ( 'wporg_bbp_stick_topic' == $action ) {
     94            $success = self::add_sticky( $term->term_id, $topic->ID );
     95        } elseif ( 'wporg_bbp_unstick_topic' == $action ) {
     96            $success = self::remove_sticky( $term->term_id, $topic->ID );
     97        }
     98
     99        $permalink = get_permalink( $topic->ID );
     100
     101        if ( $success && ! is_wp_error( $success ) ) {
     102            bbp_redirect( $permalink );
     103        } elseif ( true === $is_sticky && 'wporg_bbp_stick_topic' == $action ) {
     104            bbp_add_error( 'wporg_bbp_stick_topic', __( '<strong>ERROR<strong>: There was a problem sticking that topic!', 'wporg-forums' ) );
     105        } elseif ( false === $is_sticky && 'wporg_bbp_unstick_topic' == $action ) {
     106            bbp_add_error( 'wporg_bbp_unstick_topic', __( '<strong>ERROR</strong>: There was a problem unsticking that topic!', 'wporg-forums' ) );
     107        }
     108    }
     109
     110    /**
     111     * Replace the bbPress stick link with the custom stick link in compat topics.
     112     *
     113     * @param array $r The array of admin links
     114     * @param int $topic_id The topic id
     115     * @return array The filtered array of admin links
     116     */
     117    public function admin_links( $r, $topic_id ) {
     118        if ( ! bbp_is_single_topic() ) {
     119            return $r;
     120        }
     121        $user_id = get_current_user_id();
     122        if ( $this->user_can_stick( $user_id, $this->term->term_id, $topic_id ) ) {
     123            $r['stick'] = self::get_stick_link( array( 'topic_id' => $topic_id, 'term_id' => $this->term->term_id ) );
     124        } else {
     125            unset( $r['stick'] );
     126        }
     127        return $r;
     128    }
     129
     130    /**
     131     * Get the link to stick/unstick a compat topic.
     132     *
     133     * @param array $args The link arguments
     134     * @return string The linked URL
     135     */
     136    public static function get_stick_link( $args = array() ) {
     137        $user_id = get_current_user_id();
     138
     139        $r = bbp_parse_args( $args, array(
     140            'topic_id' => get_the_ID(),
     141            'term_id'  => 0,
     142            'stick'    => esc_html__( 'Stick', 'wporg-forums' ),
     143            'unstick'  => esc_html__( 'Unstick', 'wporg-forums' ),
     144        ), 'get_topic_stick_link' );
     145        if ( empty( $r['topic_id'] ) || empty( $r['term_id'] ) ) {
     146            return false;
     147        }
     148
     149        $topic = get_post( $r['topic_id'] );
     150        $term  = get_term( $r['term_id'] );
     151        if ( ! $topic || ! $term ) {
     152            return false;
     153        }
     154
     155        if ( self::is_sticky( $term->term_id, $topic->ID ) ) {
     156            $text = $r['unstick'];
     157            $query_args = array( 'action' => 'wporg_bbp_unstick_topic', 'topic_id' => $topic->ID, 'term_id' => $term->term_id );
     158        } else {
     159            $text = $r['stick'];
     160            $query_args = array( 'action' => 'wporg_bbp_stick_topic', 'topic_id' => $topic->ID, 'term_id' => $term->term_id );
     161        }
     162
     163        $permalink = get_permalink( $topic->ID );
     164        $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-topic-sticky_' . $topic->ID . '_' . $term->term_id ) );
     165        return sprintf( "<a href='%s'>%s</a>", $url, esc_html( $text ) );
     166    }
     167
     168    /**
     169     * Is a given user allowed to stick/unstick a topic?
     170     *
     171     * @param int $user_id The user id
     172     * @param int $term_id The term id
     173     * @param int $topic_id The topic id
     174     * @return bool True if allowed, false if not
     175     */
     176    public function user_can_stick( $user_id = 0, $term_id = 0, $topic_id = 0 ) {
     177        $retval = $topic = $term = false;
     178
     179        if ( empty( $user_id ) ) {
     180            $user_id = get_current_user_id();
     181        }
     182
     183        if ( $topic_id ) {
     184            $topic = get_post( $topic_id );
     185        }
     186        if ( $term_id ) {
     187            $term = get_term( $term_id );
     188        }
     189
     190        if ( $user_id && $topic && $term ) {
     191            // Moderators.
     192            if ( user_can( $user_id, 'moderate' ) ) {
     193                $retval = true;
     194            }
     195
     196            // Compat authors.
     197            if ( $this->authors && in_array( $user_id, $this->authors ) ) {
     198                $retval = true;
     199            }
     200        }
     201        return $retval;
     202    }
     203
     204    /**
     205     * Is a topic sticky for this term?
     206     *
     207     * @param int $term_id The term id
     208     * @param int $topic_id The topic id
     209     * @return bool True if sticky, false if not
     210     */
     211    public static function is_sticky( $term_id, $topic_id ) {
     212        $stickies = self::get_stickies( $term_id );
     213        return in_array( $topic_id, $stickies );
     214    }
     215
     216    /**
     217     * Add a topic id to a term's list of sticky topic ids.
     218     *
     219     * @param int $term_id The term id
     220     * @param int $topic_id The topic id
     221     */
     222    public static function add_sticky( $term_id, $topic_id ) {
     223        $stickies = self::get_stickies( $term_id );
     224        if ( ! in_array( $topic_id, $stickies ) ) {
     225            $stickies[] = $topic_id;
     226        }
     227        return self::set_stickies( $term_id, $stickies );
     228    }
     229
     230    /**
     231     * Remove a topic id from a term's list of sticky topic ids.
     232     *
     233     * @param int $topic_id The topic id
     234     * @param int $term_id The term id
     235     */
     236    public static function remove_sticky( $term_id, $topic_id ) {
     237        $stickies = self::get_stickies( $term_id );
     238        if ( ( $key = array_search( $topic_id, $stickies ) ) !== false ) {
     239            unset( $stickies[ $key ] );
     240        }
     241        return self::set_stickies( $term_id, $stickies );
     242    }
     243
     244    /**
    44245     * Return an array of topic sticky ids for a given term.
    45246     *
     
    56257        return $retval;
    57258    }
     259
     260    /**
     261     * Set the topic sticky ids for a given term.
     262     *
     263     * @param int $term_id The term id
     264     * @param array $stickies The sticky topic ids
     265     */
     266    public static function set_stickies( $term_id, $stickies = array() ) {
     267        return update_term_meta( $term_id, self::META, implode( ',', $stickies ) );
     268    }
    58269}
Note: See TracChangeset for help on using the changeset viewer.