| 1 | <?php |
| 2 | |
| 3 | namespace WordPressdotorg\Forums; |
| 4 | |
| 5 | class Move_Topic_To_Extension_Forum { |
| 6 | |
| 7 | public function __construct() { |
| 8 | // The plugin and theme forums only exist on the primary forums on WordPress.org/support. |
| 9 | if ( defined( 'WPORG_SUPPORT_FORUMS_BLOGID' ) && WPORG_SUPPORT_FORUMS_BLOGID == get_current_blog_id() ) { |
| 10 | add_action( 'bbp_theme_after_topic_form_forum', array( $this, 'add_edit_topic_field' ) ); |
| 11 | |
| 12 | add_action( 'bbp_edit_topic_post_extras', array( $this, 'maybe_move_topic' ) ); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Add the form field for assigning a new plugin or theme location for a topic. |
| 18 | * |
| 19 | * @return void |
| 20 | */ |
| 21 | public function add_edit_topic_field() { |
| 22 | // Only add this field to the edit form, and only for moderators. |
| 23 | if ( ! bbp_is_topic_edit() || ! current_user_can( 'moderate' ) ) { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | ?> |
| 28 | |
| 29 | <p> |
| 30 | <label for="move_topic_to_extension"> |
| 31 | <?php esc_html_e( 'Move topic to this plugin or theme:', 'wporg-forums' ); ?> |
| 32 | </label> |
| 33 | <br> |
| 34 | <input type="text" size="40" name="move_topic_to_extension" id="move_topic_to_extension" placeholder="https://wordpress.org/plugins/gutenberg/" aria-describedby="move_topic_to_extension_description"> |
| 35 | <br> |
| 36 | <em id="move_topic_to_extension_description"> |
| 37 | <?php esc_html_e( 'Put in the link to the plugin or theme page on WordPress.org that this topic relates to.', 'wporg-forums' ); ?> |
| 38 | </em> |
| 39 | </p> |
| 40 | |
| 41 | <?php |
| 42 | |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Change the forum associated with a plugin or theme if applicable. |
| 47 | * |
| 48 | * @param int $topic_id The topic ID being edited. |
| 49 | * |
| 50 | * @return void |
| 51 | */ |
| 52 | public function maybe_move_topic( $topic_id ) { |
| 53 | if ( ! current_user_can( 'moderate' ) ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | if ( ! empty( $_POST['move_topic_to_extension'] ) ) { |
| 58 | preg_match( '/wordpress\.org\/(?P<type>plugins|themes)\/(?P<slug>.+)\/?/si', $_POST['move_topic_to_extension'], $move_attributes ); |
| 59 | |
| 60 | if ( isset( $move_attributes['type'] ) && ! empty( $move_attributes['type'] ) ) { |
| 61 | switch ( $move_attributes['type'] ) { |
| 62 | case 'plugins': |
| 63 | // Clear any theme reference, if set. |
| 64 | wp_set_post_terms( $topic_id, '', 'topic-theme', false ); |
| 65 | // Override the plugin tag with only the new one. |
| 66 | wp_set_post_terms( $topic_id, sanitize_title( $move_attributes['slug'] ), 'topic-plugin', false ); |
| 67 | break; |
| 68 | case 'themes': |
| 69 | // Clear any plugin reference, if set. |
| 70 | wp_set_post_terms( $topic_id, '', 'topic-plugin', false ); |
| 71 | // Override the theme tag with only the new one. |
| 72 | wp_set_post_terms( $topic_id, sanitize_title( $move_attributes['slug'] ), 'topic-theme', false ); |
| 73 | break; |
| 74 | } |
| 75 | } else { |
| 76 | error_log( 'No matching url '); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | } |