| 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 | |