Making WordPress.org

Changeset 11587


Ignore:
Timestamp:
02/21/2022 07:33:21 AM (5 years ago)
Author:
dd32
Message:

Make: Automatically correct mis-typed cross-posts to the full URI that make requires.

This allows crossposts to be written in three formats, and converts it to the first format:

  • +make.wordpress.org/meta/ (correct, and auto-populated by the dropdowns)
  • +make.wordpress.org/meta (common, when being manually typed outside of the o2 post editor)
  • +meta (by those who are used to P2 on WordPress.com)

This should reduce some confusion with xposts not working correctly.
See #6052.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-o2-posting-access/wporg-o2-allow-crossposting.php

    r10645 r11587  
    4242                // List all sites in the auto-complete.
    4343                add_action( 'init', [ $this, 'override_o2_xposts_get_data' ], 9 ); // before o2_Xposts::get_data();
     44
     45                // Filter new posts & comments to fix/expand short xposts.
     46                add_filter( 'preprocess_comment', [ $this, 'preprocess_comment' ] );
     47                add_filter( 'o2_create_post', [ $this, 'o2_create_post' ] );
    4448        }
    4549
    … …  
    140144                return $allcaps;
    141145        }
     146
     147
     148        /**
     149         * Filter newly created comments to correct any mis-typed crossposts.
     150         */
     151        public function preprocess_comment( $comment_data ) {
     152                $comment_data['comment_content'] = $this->correct_xposts( $comment_data['comment_content'] );
     153
     154                return $comment_data;
     155        }
     156
     157        /**
     158         * Filter newly created posts to correct any mis-typed crossposts.
     159         */
     160        public function o2_create_post( $post ) {
     161                $post->post_content = addslashes( $this->correct_xposts( stripslashes( $post->post_content ) ) );
     162
     163                return $post;
     164        }
     165
     166        /**
     167         * Corrects xposts from either +site or +domain/site to +domain/site/ as required on make.wordpress.org.
     168         */
     169        protected function correct_xposts( $string ) {
     170                $current_site = get_blog_details();
     171
     172                return preg_replace_callback(
     173                        o2_Xposts::XPOSTS_REGEX,
     174                        function( $m ) use( $current_site ) {
     175                                $site = $m[1];
     176                                $site = str_replace( $current_site->domain, '', $site );
     177                                $site = trim( $site, '/' );
     178
     179                                $sites = get_sites( [
     180                                        'fields'     => 'ids',
     181                                        'network_id' => $current_site->site_id,
     182                                        'domain'     => $current_site->domain,
     183                                        'path'       => "/{$site}/",
     184                                ] );
     185
     186                                // If site could not be found, or there's multiple, let o2 deal with it.
     187                                if ( count( $sites ) !== 1 ) {
     188                                        return $m[0];
     189                                }
     190
     191                                $correct = "make.wordpress.org/{$site}/";
     192                                if ( $m[1] !== $correct ) {
     193                                        return str_replace( $m[1], $correct, $m[0] );
     194                                }
     195
     196                                return $m[0];
     197                        },
     198                        $string
     199                );
     200        }
     201
    142202}
    143203
Note: See TracChangeset for help on using the changeset viewer.