Skip to content

Commit

Permalink
Respect 'attachment' post type filtered by 'ctl_post_types'.
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Feb 20, 2021
1 parent 9b7084e commit 30d372c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
11 changes: 7 additions & 4 deletions classes/class-converter.php
Expand Up @@ -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
Expand Down
51 changes: 35 additions & 16 deletions tests/phpunit/tests/class-test-converter.php
Expand Up @@ -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 );
Expand All @@ -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'";
Expand All @@ -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";
Expand Down Expand Up @@ -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 ],
];
}

Expand Down

0 comments on commit 30d372c

Please sign in to comment.