Making WordPress.org

Ticket #5344: 5344-limit-topic-creation.2.patch

File 5344-limit-topic-creation.2.patch, 3.7 KB (added by Clorith, 3 years ago)
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-hooks.php

     
    2222                add_filter( 'wp_insert_post_data',             array( $this, 'set_post_date_gmt_for_pending_posts' ) );
    2323                add_action( 'wp_print_footer_scripts',         array( $this, 'replace_quicktags_blockquote_button' ) );
    2424
     25                add_filter( 'bbp_new_topic_pre_insert',        array( $this, 'limit_topic_tag_creation' ) );
     26                add_filter( 'bbp_edit_topic_pre_insert',       array( $this, 'limit_topic_tag_creation' ) );
     27                add_filter( 'bbp_new_reply_pre_set_terms',     array( $this, 'limit_topic_reply_tag_creation' ) );
     28                add_filter( 'bbp_edit_reply_pre_set_terms',    array( $this, 'limit_topic_reply_tag_creation' ) );
     29
    2530                // Add bbPress support to the WordPress.org SEO plugin.
    2631                add_filter( 'wporg_canonical_base_url', array( $this, 'wporg_canonical_base_url' ) );
    2732                add_filter( 'wporg_canonical_url',      array( $this, 'wporg_canonical_url' ) );
     
    128133                add_filter( 'bbp_subscription_mail_message', array( $this, 'bbp_subscription_mail_message'), 5, 3 );
    129134        }
    130135
     136        /**
     137         * Limit the creation of new tags to moderators or above.
     138         *
     139         * This curates the list of tags, keeping the tag list short and relevant.
     140         *
     141         * @param array $topic Array of post details.
     142         *
     143         * @return array Filtered post details array.
     144         */
     145        public function limit_topic_tag_creation( $topic ) {
     146                // Only affect the topic post type.
     147                if ( bbp_get_topic_post_type() !== $topic['post_type'] ) {
     148                        return $topic;
     149                }
     150
     151                // Do not modify anything if the user has moderator capabilities.
     152                if ( current_user_can( 'moderate' ) ) {
     153                        return $topic;
     154                }
     155
     156                $existing_tags = array();
     157
     158                $topic_tag_slug = get_option( '_bbp_topic_tag_slug', 'topic-tag' );
     159
     160                // Loop through the proposed taxonomies
     161                foreach ( $topic['tax_input'] as $taxonomy => $terms ) {
     162                        // Do not check non-topic-tag terms.
     163                        if ( $topic_tag_slug !== $taxonomy ) {
     164                                var_dump( $topic_tag_slug . ' is not ' . $taxonomy );
     165                                continue;
     166                        }
     167
     168                        // Loop over the terms added ot this taxonomy.
     169                        foreach ( $terms as $term ) {
     170                                if ( term_exists( $term, $taxonomy ) ) {
     171                                        if ( ! isset( $existing_tags[ $taxonomy ] ) ) {
     172                                                $existing_tags[ $taxonomy ] = array();
     173                                        }
     174
     175                                        $existing_tags[ $taxonomy ][] = $term;
     176                                }
     177                        }
     178                }
     179
     180                $topic['tax_input'] = $existing_tags;
     181
     182                return $topic;
     183        }
     184
     185        /**
     186         * Limit the creation of new tags to moderators or above.
     187         *
     188         * This curates the list of tags, keeping the tag list short and relevant.
     189         *
     190         * @param array|string $terms Array of terms to apply to a topic.
     191         *
     192         * @return array|string Filtered array of terms to apply to a topic.
     193         */
     194        public function limit_topic_reply_tag_creation( $terms ) {
     195                // Do not modify anything if the user has moderator capabilities.
     196                if ( current_user_can( 'moderate' ) ) {
     197                        return $terms;
     198                }
     199
     200                if ( ! is_array( $terms ) ) {
     201                        $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
     202                }
     203
     204                $existing_terms = array();
     205
     206                // Loop through the proposed tags and keep only the existing ones.
     207                foreach ( $terms as $term ) {
     208                        if ( term_exists( $term, bbp_get_topic_tag_name() ) ) {
     209                                $existing_terms[] = $term;
     210                        }
     211                }
     212
     213                // Return a string if the input value was one.
     214                if ( ! is_array( $terms ) ) {
     215                        return implode( ', ', $terms );
     216                }
     217
     218                return $existing_terms;
     219        }
     220
    131221        /**
    132222         * Check "Notify me of follow-up replies via email" box for new topics by default.
    133223         *