diff --git a/classes/class-converter.php b/classes/class-converter.php index 0800113..4238020 100644 --- a/classes/class-converter.php +++ b/classes/class-converter.php @@ -187,13 +187,16 @@ protected function convert_existing_post_slugs( $args = [] ) { $regexp = $wpdb->prepare( '%s', self::ALLOWED_CHARS_REGEX ); - $post_sql = + $post_sql = 'post_status IN (' . $this->main->prepare_in( $parsed_args['post_status'] ) . ')' . ' AND post_type IN (' . $this->main->prepare_in( $parsed_args['post_type'] ) . ')'; - $media_sql = "post_status = 'inherit' AND post_type = 'attachment'"; - $all_posts_sql = '(' . $post_sql . ') OR (' . $media_sql . ')'; - $sql = "SELECT ID, post_name, post_type FROM $wpdb->posts WHERE LOWER(post_name) NOT REGEXP($regexp) AND ($all_posts_sql)"; + if ( isset( $parsed_args['post_type']['attachment'] ) ) { + $media_sql = "post_status = 'inherit' AND post_type = 'attachment'"; + $post_sql = '(' . $post_sql . ') OR (' . $media_sql . ')'; + } + + $sql = "SELECT ID, post_name, post_type FROM $wpdb->posts WHERE LOWER(post_name) NOT REGEXP($regexp) AND ($post_sql)"; // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared // phpcs:ignore WordPress.DB.DirectDatabaseQuery diff --git a/tests/phpunit/tests/class-test-converter.php b/tests/phpunit/tests/class-test-converter.php index 36f4751..fcc1c88 100644 --- a/tests/phpunit/tests/class-test-converter.php +++ b/tests/phpunit/tests/class-test-converter.php @@ -258,12 +258,13 @@ public function dp_test_process_handler() { /** * Test convert_existing_slugs() * - * @param array $posts Posts to convert. - * @param array $terms Terms to convert. + * @param array $posts Posts to convert. + * @param array $terms Terms to convert. + * @param bool $include_attachment Include attachment as post type. * * @dataProvider dp_test_convert_existing_slugs */ - public function test_convert_existing_slugs( $posts, $terms ) { + public function test_convert_existing_slugs( $posts, $terms, $include_attachment ) { global $wpdb; $main = Mockery::mock( Main::class ); @@ -278,12 +279,20 @@ public function test_convert_existing_slugs( $posts, $terms ) { $admin_notices ); - $post_types = [ - 'post' => 'post', - 'page' => 'page', - 'attachment' => 'attachment', - ]; - $post_types_in = "'post', 'page', 'attachment'"; + if ( $include_attachment ) { + $post_types = [ + 'post' => 'post', + 'page' => 'page', + 'attachment' => 'attachment', + ]; + $post_types_in = "'post', 'page', 'attachment'"; + } else { + $post_types = [ + 'post' => 'post', + 'page' => 'page', + ]; + $post_types_in = "'post', 'page'"; + } $post_statuses = [ 'publish', 'future', 'private' ]; $post_statuses_in = "'publish', 'future', 'private'"; @@ -308,11 +317,20 @@ public function test_convert_existing_slugs( $posts, $terms ) { $wpdb->terms = 'wp_terms'; $wpdb->term_taxonomy = 'wp_term_taxonomy'; - $regexp = $subject::ALLOWED_CHARS_REGEX; - $post_query = - "SELECT ID, post_name, post_type FROM $wpdb->posts " . - 'WHERE LOWER(post_name) NOT REGEXP(%s) ' . - "AND ((post_status IN ($post_statuses_in) AND post_type IN ($post_types_in)) OR (post_status = 'inherit' AND post_type = 'attachment'))"; + $regexp = $subject::ALLOWED_CHARS_REGEX; + + if ( $include_attachment ) { + $post_query = + "SELECT ID, post_name, post_type FROM $wpdb->posts " . + 'WHERE LOWER(post_name) NOT REGEXP(%s) ' . + "AND ((post_status IN ($post_statuses_in) AND post_type IN ($post_types_in)) OR (post_status = 'inherit' AND post_type = 'attachment'))"; + } else { + $post_query = + "SELECT ID, post_name, post_type FROM $wpdb->posts " . + 'WHERE LOWER(post_name) NOT REGEXP(%s) ' . + "AND (post_status IN ($post_statuses_in) AND post_type IN ($post_types_in))"; + } + $term_query = "SELECT t.term_id, slug, tt.taxonomy, tt.term_taxonomy_id FROM $wpdb->terms t, $wpdb->term_taxonomy tt WHERE LOWER(t.slug) NOT REGEXP(%s) AND tt.term_id = t.term_id"; @@ -366,8 +384,9 @@ public function test_convert_existing_slugs( $posts, $terms ) { */ public function dp_test_convert_existing_slugs() { return [ - [ null, null ], - [ [ 'post1', 'post2' ], [ 'term1', 'term2' ] ], + [ null, null, true ], + [ [ 'post1', 'post2' ], [ 'term1', 'term2' ], true ], + [ [ 'post1', 'post2' ], [ 'term1', 'term2' ], false ], ]; }