Changeset 6545
- Timestamp:
- 02/05/2018 09:58:18 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/cli.php
r6193 r6545 43 43 'revisions', 44 44 'title', 45 46 // Needed for manual inspection/modification of the post's parent. 47 'page-attributes', 45 48 ); 46 49 register_post_type( 'command', array( … … 74 77 75 78 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' ) ) { 77 80 $query->set( 'post_parent', 0 ); 78 81 $query->set( 'orderby', 'title' ); … … 101 104 $existing = array(); 102 105 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 ); 104 107 $existing[ $cmd_path ] = array( 105 108 'post_id' => $post->ID, … … 145 148 $post = self::create_post_from_manifest_doc( $doc, $post_parent ); 146 149 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 ); 148 151 if ( ! empty( $doc['repo_url'] ) ) { 149 152 update_post_meta( $post->ID, 'repo_url', esc_url_raw( $doc['repo_url'] ) ); … … 476 479 } 477 480 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 478 509 } 479 510
Note: See TracChangeset
for help on using the changeset viewer.