$quoted_types, 'statuses' => $quoted_statuses, 'order' => $order, 'exclude' => $exclude_sql, ); } /** * The removable rows: every post in a group of two or more published * posts of one type with exactly the same title (case and spacing * included, hence BINARY), except the group's keeper. * * The keeper is the oldest (or newest) by publish date, with the ID * breaking ties, so imported or backdated posts are judged by their * date rather than by when they were inserted. * * @param array{types:string,statuses:string,order:string,exclude:string} $sql Fragments from sql_fragments(). * @return string SELECT over alias `d` (ID, post_title, post_type, post_status, post_date, post_author, keeper_id). */ private static function removable_sql( array $sql ): string { global $wpdb; return " SELECT d.* FROM ( SELECT t1.ID, t1.post_title, t1.post_type, t1.post_status, t1.post_date, t1.post_author, ( SELECT k.ID FROM {$wpdb->posts} k WHERE BINARY k.post_title = BINARY t1.post_title AND k.post_type = t1.post_type AND k.post_status IN ({$sql['statuses']}) ORDER BY k.post_date {$sql['order']}, k.ID {$sql['order']} LIMIT 1 ) AS keeper_id FROM {$wpdb->posts} t1 INNER JOIN ( SELECT BINARY post_title AS title_key, post_type FROM {$wpdb->posts} WHERE post_type IN ({$sql['types']}) AND post_status IN ({$sql['statuses']}) AND post_title != '' GROUP BY BINARY post_title, post_type HAVING COUNT(*) > 1 ) t2 ON BINARY t1.post_title = t2.title_key AND t1.post_type = t2.post_type WHERE t1.post_status IN ({$sql['statuses']}) ) d WHERE d.ID != d.keeper_id {$sql['exclude']} "; } /** * Whether removal should force-delete instead of trashing. * * Free always trashes. Pro adds a "delete permanently" setting and * hooks this filter to honor it. * * @return bool */ public static function force_delete(): bool { /** * Filters whether duplicate removal should permanently delete * instead of moving to Trash. * * @since 2.0.0 * * @param bool $force_delete Default false (Free always trashes). */ return (bool) apply_filters( 'aiou_duplicate_posts_force_delete', false ); } /** * Count duplicate (removable) posts matching current settings. * * Matching by title only; Pro hooks `aiou_duplicate_posts_count` to * take over the count entirely when a non-title comparison method * (content, excerpt, custom field) is active. * * @return int */ public static function count(): int { /** * Short-circuits the duplicate count. Return an int to take over * entirely (used by Pro for its non-title comparison methods); * return null (the default) to run Free's own title-match count. * * @since 2.0.0 * * @param int|null $override Null to use Free's own count. */ $override = apply_filters( 'aiou_duplicate_posts_count', null ); if ( is_int( $override ) ) { return $override; } global $wpdb; $sql = self::sql_fragments(); if ( null === $sql ) { return 0; } $query = 'SELECT COUNT(*) FROM (' . self::removable_sql( $sql ) . ') removable'; return (int) $wpdb->get_var( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $query is this method's own fixed-shape string above, built entirely from esc_sql()'d/absint()'d settings values, never raw user input. } /** * Find duplicate/keeper pairs matching current settings. * * @param int $limit Max pairs to return (0 = no limit). * @param int $offset Offset for pagination. * @return array */ public static function find_pairs( int $limit = 0, int $offset = 0 ): array { /** * Short-circuits the duplicate pair list, the counterpart to the * `aiou_duplicate_posts_count` filter above -- see its docblock. * * @since 2.0.0 * * @param array|null $override Null to use Free's own title-match pairs. * @param int $limit Max pairs requested. * @param int $offset Offset requested. */ $override = apply_filters( 'aiou_duplicate_posts_pairs', null, $limit, $offset ); if ( is_array( $override ) ) { return $override; } global $wpdb; $sql = self::sql_fragments(); if ( null === $sql ) { return array(); } $limit_sql = $limit > 0 ? $wpdb->prepare( ' LIMIT %d OFFSET %d', $limit, $offset ) : ''; $query = self::removable_sql( $sql ) . ' ORDER BY d.post_title ASC, d.ID ASC' . $limit_sql; $rows = $wpdb->get_results( $query, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- $query is this method's own fixed-shape string above, built entirely from esc_sql()'d/absint()'d settings values or $wpdb->prepare() itself (the LIMIT clause), never raw user input. $pairs = array(); foreach ( $rows as $row ) { $pairs[] = array( 'id' => (int) $row['ID'], 'org_id' => (int) $row['keeper_id'], 'title' => (string) $row['post_title'], 'type' => (string) $row['post_type'], 'status' => (string) $row['post_status'], 'date' => (string) $row['post_date'], 'author' => get_the_author_meta( 'display_name', (int) $row['post_author'] ), ); } return $pairs; } /** * Re-validate that an ID/keeper pair still matches current settings -- * used right before deleting, since settings (or the posts themselves) * may have changed since the list was last rendered. * * @param int $dupe_id Post id to remove. * @param int $org_id Post id to keep. * @return bool */ public static function is_valid_pair( int $dupe_id, int $org_id ): bool { if ( $dupe_id < 1 || $org_id < 1 || $dupe_id === $org_id ) { return false; } $dupe = get_post( $dupe_id ); $org = get_post( $org_id ); if ( ! $dupe || ! $org ) { return false; } if ( in_array( $dupe_id, self::excluded_ids(), true ) ) { return false; } /** * Short-circuits pair re-validation for a non-title comparison * method -- the counterpart to `aiou_duplicate_posts_pairs` above. * * @since 2.0.0 * * @param bool|null $override Null to use Free's own title-match validation. * @param \WP_Post $dupe The post that would be removed. * @param \WP_Post $org The post that would be kept. */ $override = apply_filters( 'aiou_duplicate_posts_valid_pair', null, $dupe, $org ); if ( is_bool( $override ) ) { return $override; } $post_types = self::selected_post_types(); if ( ! in_array( $dupe->post_type, $post_types, true ) || $dupe->post_type !== $org->post_type ) { return false; } $statuses = self::allowed_statuses( $post_types ); if ( ! in_array( $dupe->post_status, $statuses, true ) || ! in_array( $org->post_status, $statuses, true ) ) { return false; } if ( $dupe->post_title !== $org->post_title || '' === $dupe->post_title ) { return false; } global $wpdb; $order = 'newest' === self::keep_preference() ? 'DESC' : 'ASC'; // The same keeper rule as removable_sql(). $status_placeholders = implode( ', ', array_fill( 0, count( $statuses ), '%s' ) ); $keeper_id = (int) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- the replacements are one array: title, type, then one per status placeholder. // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $order is one of the two fixed ASC/DESC tokens above; $status_placeholders is only a run of %s placeholders. "SELECT ID FROM {$wpdb->posts} WHERE BINARY post_title = BINARY %s AND post_type = %s AND post_status IN ({$status_placeholders}) ORDER BY post_date {$order}, ID {$order} LIMIT 1", array_merge( array( $dupe->post_title, $dupe->post_type ), $statuses ) ) ); return $keeper_id === $org_id; } /** * Remove the "duplicate" half of a pair. * * @param int $dupe_id Post id to remove. * @param bool $permanent Force-delete instead of trashing. Callers should * pass `force_delete()`'s value rather than a * literal, so Pro's "delete permanently" setting * is honored; Free's own callers always get * false back from that method. * @param int $org_id The kept post's id, passed through to the * `aiou_duplicate_posts_removed` action only * (e.g. for Pro's redirect-preservation feature). * Optional -- omitting it just omits that context. * @return bool */ public static function remove( int $dupe_id, bool $permanent = false, int $org_id = 0 ): bool { // Captured before deletion: a permanent delete removes the row // entirely, so get_permalink( $dupe_id ) would return false if // read only afterward (trashing alone leaves it resolvable, but a // permanent delete must not depend on that difference). $old_permalink = get_permalink( $dupe_id ); if ( 'attachment' === get_post_type( $dupe_id ) ) { $result = false !== wp_delete_attachment( $dupe_id, $permanent ); } elseif ( $permanent ) { $result = false !== wp_delete_post( $dupe_id, true ); } else { $result = false !== wp_trash_post( $dupe_id ); } if ( $result ) { /** * Fires after a duplicate post has been removed (trashed or * permanently deleted). * * @since 2.0.0 * * @param int $dupe_id The removed post's id. * @param int $org_id The kept post's id, or 0 if the caller didn't supply it. * @param bool $permanent Whether it was permanently deleted (true) or trashed (false). * @param string|false $old_permalink The removed post's permalink, captured before deletion (false if it couldn't be resolved). */ do_action( 'aiou_duplicate_posts_removed', $dupe_id, $org_id, $permanent, $old_permalink ); } return $result; } }