Making WordPress.org

Changeset 10599


Ignore:
Timestamp:
01/19/2021 07:09:47 AM (4 years ago)
Author:
dd32
Message:

Support Forums: Limit new tag creation to moderators.

Props Clorith.
See #5344.

File:
1 edited

Legend:

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

    r9952 r10599  
    5353        add_filter( 'bbp_get_subscribers', array( $this, 'bbp_get_subscribers' ), 10, 3 ); // bbPress 2.6
    5454        add_filter( 'bbp_get_user_subscribe_link', array( $this, 'bbp_get_user_subscribe_link' ), 10, 4 ); // Remove link.
     55
     56        // Disable new tag creation for non-moderators.
     57        add_filter( 'bbp_new_topic_pre_insert',        array( $this, 'limit_topic_tag_creation' ) );
     58        add_filter( 'bbp_edit_topic_pre_insert',       array( $this, 'limit_topic_tag_creation' ) );
     59        add_filter( 'bbp_new_reply_pre_set_terms',     array( $this, 'limit_topic_reply_tag_creation' ) );
     60        add_filter( 'bbp_edit_reply_pre_set_terms',    array( $this, 'limit_topic_reply_tag_creation' ) );
     61
    5562    }
    5663
     
    466473        return $html;
    467474    }
     475
     476    /**
     477     * Limit the creation of new tags to moderators or above.
     478     *
     479     * This curates the list of tags, keeping the tag list short and relevant.
     480     *
     481     * @param array $topic Array of post details.
     482     *
     483     * @return array Filtered post details array.
     484     */
     485    public function limit_topic_tag_creation( $topic ) {
     486        // Only affect the topic post type.
     487        if ( bbp_get_topic_post_type() !== $topic['post_type'] ) {
     488            return $topic;
     489        }
     490
     491        // Do not modify anything if the user has moderator capabilities.
     492        if ( current_user_can( 'moderate' ) ) {
     493            return $topic;
     494        }
     495
     496        $existing_tags = array();
     497
     498        $taxonomy = bbp_get_topic_tag_tax_slug();
     499
     500        if ( ! empty( $topic['tax_input'][ $taxonomy ] ) ) {
     501            // Loop through the proposed terms
     502            foreach ( $topic['tax_input'][ $taxonomy ] as $i => $term ) {
     503                if ( ! term_exists( $term, $taxonomy ) ) {
     504                    // ..and remove anything that doesn't exist.
     505                    unset( $topic['tax_input'][ $taxonomy ][ $i ] );
     506                }
     507            }
     508        }
     509
     510        return $topic;
     511    }
     512
     513    /**
     514     * Limit the creation of new tags to moderators or above.
     515     *
     516     * This curates the list of tags, keeping the tag list short and relevant.
     517     *
     518     * @param array|string $terms Array of terms to apply to a topic.
     519     *
     520     * @return array|string Filtered array of terms to apply to a topic.
     521     */
     522    public function limit_topic_reply_tag_creation( $terms ) {
     523        // Do not modify anything if the user has moderator capabilities.
     524        if ( current_user_can( 'moderate' ) ) {
     525            return $terms;
     526        }
     527
     528        if ( ! is_array( $terms ) ) {
     529            $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
     530        }
     531
     532        $existing_terms = array();
     533
     534        // Loop through the proposed tags and keep only the existing ones.
     535        foreach ( $terms as $term ) {
     536            if ( term_exists( $term, bbp_get_topic_tag_tax_id() ) ) {
     537                $existing_terms[] = $term;
     538            }
     539        }
     540
     541        // Return a string if the input value was one.
     542        if ( ! is_array( $terms ) ) {
     543            return implode( ', ', $terms );
     544        }
     545
     546        return $existing_terms;
     547    }
     548
    468549}
Note: See TracChangeset for help on using the changeset viewer.