Making WordPress.org

Changeset 6545


Ignore:
Timestamp:
02/05/2018 09:58:18 PM (6 years ago)
Author:
danielbachhuber
Message:

devhub/cli: Fix Daniel's broken importer code

Fixes #3421

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/cli.php

    r6193 r6545  
    4343            'revisions',
    4444            'title',
     45
     46            // Needed for manual inspection/modification of the post's parent.
     47            'page-attributes',
    4548        );
    4649        register_post_type( 'command', array(
     
    7477
    7578    public static function action_pre_get_posts( $query ) {
    76         if ( $query->is_main_query() && $query->is_post_type_archive( 'command' ) ) {
     79        if ( ! is_admin() && $query->is_main_query() && $query->is_post_type_archive( 'command' ) ) {
    7780            $query->set( 'post_parent', 0 );
    7881            $query->set( 'orderby', 'title' );
     
    101104        $existing = array();
    102105        foreach( $q->posts as $post ) {
    103             $cmd_path = rtrim( str_replace( home_url( 'cli/commands/' ), '', get_permalink( $post->ID ) ), '/' );
     106            $cmd_path = self::get_cmd_path( $post->ID );
    104107            $existing[ $cmd_path ] = array(
    105108                'post_id'   => $post->ID,
     
    145148        $post = self::create_post_from_manifest_doc( $doc, $post_parent );
    146149        if ( $post ) {
    147             $cmd_path = rtrim( str_replace( home_url( 'cli/commands/' ), '', get_permalink( $post->ID ) ), '/' );
     150            $cmd_path = self::get_cmd_path( $post->ID );
    148151            if ( ! empty( $doc['repo_url'] ) ) {
    149152                update_post_meta( $post->ID, 'repo_url', esc_url_raw( $doc['repo_url'] ) );
     
    476479    }
    477480
     481    /**
     482     * Gets a normalized version of the command path from the post permalink.
     483     *
     484     * This normalization is needed because CPTs share the slug namespace with
     485     * other CPTs, causing unexpected conflicts that result in changed URLs like
     486     * "command-2".
     487     *
     488     * @see https://github.com/wp-cli/handbook/issues/202
     489     *
     490     * @param int $post_id Post ID to get the normalized path for.
     491     *
     492     * @return string Normalized command path.
     493     */
     494    private static function get_cmd_path( $post_id ) {
     495        $cmd_path = rtrim( str_replace( home_url( 'cli/commands/' ), '', get_permalink( $post_id ) ), '/' );
     496        $parts = explode( '/', $cmd_path );
     497        $cleaned_parts = array();
     498        foreach ( $parts as $part ) {
     499            $matches = null;
     500            $result = preg_match( '/^(?<cmd>.*)(?<suffix>-[0-9]+)$/', $part, $matches );
     501            if ( 1 === $result ) {
     502                $part = $matches['cmd'];
     503            }
     504            $cleaned_parts[] = $part;
     505        }
     506        return implode( '/', $cleaned_parts );
     507    }
     508
    478509}
    479510
Note: See TracChangeset for help on using the changeset viewer.