Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use $wpdb->remove_placeholder_escape() when exporting in WP 4.8.3 and greater #43

Merged
merged 6 commits into from Nov 10, 2017
15 changes: 15 additions & 0 deletions features/search-replace-export.feature
Expand Up @@ -333,3 +333,18 @@ Feature: Search / replace with file export
wp_users display_name 0 PHP
"""
And STDERR should be empty

Scenario: Search / replace should remove placeholder escape on export
Given a WP install
And I run `wp post create --post_title=test-remove-placeholder-escape% --porcelain`
Then save STDOUT as {POST_ID}

When I run `wp search-replace baz bar --export`
Then STDOUT should contain:
"""
'test-remove-placeholder-escape%'
"""
And STDOUT should contain:
"""
'test-remove-placeholder-escape{'
"""
9 changes: 8 additions & 1 deletion src/Search_Replace_Command.php
Expand Up @@ -547,7 +547,14 @@ private function write_sql_row_fields( $table, $rows ) {
if( ( $index % $export_insert_size == 0 && $index > 0 ) || $index == $count ) {
$sql .= ";\n";

$sql = $wpdb->prepare( $sql, array_values( $values ) );
if( method_exists( $wpdb, 'remove_placeholder_escape' ) ) {
// since 4.8.3
$sql = $wpdb->remove_placeholder_escape( $wpdb->prepare( $sql, array_values( $values ) ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we can't internalize remove_placeholder_escape and use it with earlier WP versions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need it for earlier WP versions. WP internal behavior changed in 4.8.3 and the method remove_placeholder_escape was provided at the same time to help plugin developers:
https://make.wordpress.org/core/2017/10/31/changed-behaviour-of-esc_sql-in-wordpress-4-8-3/

} else {
// 4.8.2 or less
$sql = $wpdb->prepare( $sql, array_values( $values ) );
}

fwrite( $this->export_handle, $sql );

// If there is still rows to loop, reset $sql and $values variables.
Expand Down