• Resolved willgorham

    (@willgorham)


    Hi, thanks for your hard work maintaining this plugin!

    I’ve noticed that URLs in tables are stored with escaped slashes in the database. This is causing an issue when trying to run a search and replace for a full URL (or in my case, clone a site using NS Cloner).

    I think, normally, this wouldn’t be noticed because the primary domain can still be replaced easily (e.g. https://www.example.com has no slashes). However, I came across this issue because I’m running a multisite install, where the site ID of all ‘uploads’ resources is stored in the URL (e.g. https://example.com/wp-content/uploads/sites/123/2018/02/image.png, where 123 is the site ID). When I migrate a site that has tables that contain images, the search-replace doesn’t identify the full image URL and fails because the URL in the database is stored with escaped slashes (e.g. http:\/\/example.com\/wp-content\/uploads\/sites\/123\/2018\/02\/image.png)

    I raised this issue initially with the NS Cloner developer and he advised to bring it up with you, suggesting escaped data shouldn’t be typically stored in the DB, unless for a specific reason. You can read that response here: https://www.ads-software.com/support/topic/tablepress-support-2/

    Any advice on working around this problem, or implementing a fix would be appreciated!

    Thanks!
    Will

    • This topic was modified 6 years, 11 months ago by willgorham.
Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    thanks for your question, and sorry for the trouble.

    This is by design, it’s part of the JSON format that TablePress uses. In JSON, all / and " are escaped with a \.

    Regards,
    Tobias

    Thread Starter willgorham

    (@willgorham)

    Hi,

    Thanks for the fast response here. It looks like this is an issue with PHP’s json_encode function. When using wp_json_encode(), would it be possible to add a filter to add a second options parameter, maybe just for the JSON_UNESCAPED_SLASHES option?

    For reference:
    https://developer.www.ads-software.com/reference/functions/wp_json_encode/
    https://php.net/manual/en/function.json-encode.php
    https://bugs.php.net/bug.php?id=49366

    Thanks,
    Will

    • This reply was modified 6 years, 10 months ago by willgorham.
    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    thanks for these references! I didn’t know that such an option exists, definitely good to know!

    Now, I’m very reluctant to add this though, as I’m a bit scared about compatibility issues. At this time, I’d rather not change this behavior.

    Can’t you just repeat your search/replace with both the unescaped and escaped versions?

    Regards,
    Tobias

    Thread Starter willgorham

    (@willgorham)

    Hi,

    I totally understand the concern with compatibility. What if you added a filter that defaulted to the current behavior, but could optionally be changed to allow for unescaped slashes?

    For example, in models/model-table.php line 240 (I think this is where you would do it):

    Currently: 'post_content' => wp_json_encode( $table['data'] )

    You could change to:

    'post_content' => wp_json_encode( $table['data'], apply_filters( 'tablepress_table_json_encode_unescaped_slashes', false ) ? JSON_UNESCAPED_SLASHES : 0 )

    This would default to 0, the default second parameter of wp_json_encode(), and allow a user to enable the option if needed, e.g.

    add_filter( 'tablepress_table_json_encode_unescaped_slashes', '__return_true' );

    Another option, if you wanted to make it more flexible:

    'post_content' => wp_json_encode( $table['data'], apply_filters( 'tablepress_table_json_encode_options', 0 ) )

    This would allow a user to add different options, e.g.

    add_filter( 'tablepress_table_json_encode_options', 'example_json_options', 10, 1 );
    function example_json_options( $options ) {
      return JSON_UNESCAPED_SLASHES;
    }

    I could repeat the search-replace, but the initial search-replace is done by the NS Cloner plugin and I have a client that clones sites often. I’d rather not have to tell my client to do a manual search-replace after each clone, ideally.

    Thanks for your consideration,
    Will

    • This reply was modified 6 years, 10 months ago by willgorham.
    • This reply was modified 6 years, 10 months ago by willgorham.
    • This reply was modified 6 years, 10 months ago by willgorham.
    • This reply was modified 6 years, 10 months ago by willgorham.
    • This reply was modified 6 years, 10 months ago by willgorham.
    • This reply was modified 6 years, 10 months ago by willgorham.
    Plugin Author TobiasBg

    (@tobiasbg)

    Hi Will,

    yes, the option with the filter of the options array might be an idea.
    Do you have any experience with what PHP does when decoding such JSON? Would it also need an option somewhere?

    Regards,
    Tobias

    Thread Starter willgorham

    (@willgorham)

    My understanding is that json_decode will handle it either way, no need to add any options there. However, I will give it a quick test in the next day and let you know what I find.

    Thanks,
    Will

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi Will,

    thanks, that would be great!

    Regards,
    Tobias

    Thread Starter willgorham

    (@willgorham)

    Hi Tobias,

    I just did a quick test for json_decode() to see if it behaves the same for escaped and unescaped slashes in JSON data – and it does! No difference, or need to change anything in the decoding process.

    Here is a simple example:

    $data = array(
      "This is a plain string",
      "This is a URL: href=\"https://npr.org/\"",
    );
    
    $encoded_data = json_encode( $data );
    $encoded_data_unescaped_slashes = json_encode( $data, JSON_UNESCAPED_SLASHES );
    $decoded_data = json_decode( $encoded_data );
    $decoded_data_unescaped_slashes = json_decode( $encoded_data_unescaped_slashes );
    // Same json_decode function for both types of encoded data
    
    print_r( $encoded_data );
    //-> ["This is a plain string","This is a URL: href=\"https:\/\/npr.org\/\""]
    print_r( $decoded_data );
    //-> Array ( [0] => This is a plain string [1] => This is a URL: href="https://npr.org/" )
    
    print_r( $encoded_data_unescaped_slashes );
    //-> ["This is a plain string","This is a URL: href=\"https://npr.org/\""]
    // Notice no escaped slashes here ^
    print_r( $decoded_data_unescaped_slashes );
    //-> Array ( [0] => This is a plain string [1] => This is a URL: href="https://npr.org/" )
    // Same decoded data as before

    If you want me to test anything else for you I’d be happy to.

    Thanks,
    Will

    • This reply was modified 6 years, 10 months ago by willgorham.
    • This reply was modified 6 years, 10 months ago by willgorham. Reason: Code formatting
    Plugin Author TobiasBg

    (@tobiasbg)

    Hi Will,

    great! Thanks for testing this! I’ll then gladly add a filter here, like this:

    'post_content' => wp_json_encode( $table['data'], apply_filters( 'tablepress_json_encode_options', 0 ) )
    

    (slightly different name than what you suggested).
    It will be in the next release of TablePress ??

    Regards,
    Tobias

    Thread Starter willgorham

    (@willgorham)

    Awesome, thank you! ??

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    no problem, you are very welcome! ?? Good to hear that this helped!

    Best wishes,
    Tobias

    P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Issues running search-replace on URLs in table’ is closed to new replies.