Support Forum

Link formatting when upgrading 4.5 to 5.1.3

23 Answers

New Answer

MP Mr Papa
Mr Papa
Member

Hey Peter, thanks for the code!!  The regex works nicely…  have added it to the upgrade script for 5.1.4 (hopefully released next weekend)…

I had to tweak a few other things to use our api and get it working… for the reference, here is our function:

function sp_fix_shortened_links() {
    $postCount = spdb_count(SFPOSTS);
    $limit = 1000;  
    for ($offset = 0; $offset < $postCount; $offset+= $limit) {
        $posts = spdb_select('set', "SELECT post_id, post_content FROM ".SFPOSTS." ORDER BY post_id ASC LIMIT $offset, $limit");
        foreach ($posts as $post) {
            /*
                Matches would be:
                0 = The entire string
                1 = The link in the <a> tag
                2 = The rest of the parameters in the <a> tag (nofollow, target, etc.)
                3 = The part after the 5 periods
    
                So then we want to replace 0 with 1 for each result
    
                We are assuming that the links have 5 consecutive periods
            */
    
            $postContent = stripslashes($post->post_content);
            preg_match_all("/<a href="(.*?)"(.*?).....(.*?)</a>/is", $postContent, $linkMatches);   
            if (!empty($linkMatches[0])) {
                foreach ($linkMatches[0] as $index => $stringMatch) {
                    $postContent = str_replace($stringMatch, $linkMatches[1][$index], $postContent);
                }
                $postContent = esc_sql($postContent);
                spdb_query("UPDATE ".SFPOSTS." SET post_content = '$postContent' WHERE post_id = $post->post_id");
            }
        }
    }
}

might be worth a quick check to see I didnt screw anything up! ;)

1000 seemed to work faster than 100 on a larger db…  had to move the update query outside the foreach for when multiple shortened links in the content…

again, THANKS!

PK pkthree
pkthree
Member

Great catch for both the different offset and having multiple shortened links.

How necessary is the stripslashes and addslashes? Is that only necessary on certain setups or are they required because of how spdb_query works? I prefer to use $wpdb->prepare and similar functions for sanitizing queries (if necessary), as typically stripslashes/addslashes introduce problems on setups that don’t need them.

MP Mr Papa
Mr Papa
Member

well wpdb prepare does the same thing… its just hidden from you…  could have done an esc_sql() vs addslashes()…

when you pull it from db, you need to remove any escapes (slashes)… and before you put it back in, you have to escape (slashes) it again…  not worried about security here since it came from the db, but the escaping handles any single quotes (and few other chars) that might be in the post content…

but the main thing to note is that wpdb prepare will do that too (actually via walker escape by ref)…

MP Mr Papa
Mr Papa
Member

btw, I did change ours to esc_sql() vs addslashes()… pretty much the same thing, but use mysql real escape…  updated code above too….

PK pkthree
pkthree
Member

You’re right on the insertion; sorry for confusing the issue there. But on grabbing the data from the database, I don’t understand why you would need to strip any slashes, unless they were double encoded. The slashes that are added for escaping purposes should only be for escaping purposes and wouldn’t have been stored in the database… does the forum application itself have a need for the slashes to be stored in the database?

MP Mr Papa
Mr Papa
Member

I will do some more testing, but for if, for example, you have a post content with a in it…  it gets escaped on the way in and if you dont remove slashes on the way out, you will end up with …  those are stored in the db vs quotes, for example…

PK pkthree
pkthree
Member

Sorry for bringing up this topic again, but I had to share what I just learned since it didn’t seem right that slashes were automatically being added (and then having to be removed): WordPress forces “addslashes” on all GET and POST variables. It runs this function on every page load in wp-settings.php and has been doing so since WP 2.5 (or even earlier — I don’t have any test installs from before then).

function wp_magic_quotes() { // If already slashed, strip. if ( get_magic_quotes_gpc() ) { $_GET = stripslashes_deep( $_GET ); $_POST = stripslashes_deep( $_POST ); $_COOKIE = stripslashes_deep( $_COOKIE ); }

// Escape with wpdb. $_GET = add_magic_quotes( $_GET ); $_POST = add_magic_quotes( $_POST ); $_COOKIE = add_magic_quotes( $_COOKIE ); $_SERVER = add_magic_quotes( $_SERVER );

// Force REQUEST to be GET + POST. $_REQUEST = array_merge( $_GET, $_POST );}

So… we have to strip slashes because WordPress forced the slashes in there. Apparently they’d like to get rid of this behavior in the future, but because of the sheer number of plugins and servers and still use magic quotes, they continue to do this in order to “inject” some consistency across the board, even though it is generally accepted as undesirable behavior now.

YS Yellow Swordfish
Yellow Swordfish
Member

Gosh – I had to re-read this whole thread just to get the context!

Yes – I know they would like to get rid of it. I say bring it on… Steve may have a comment…

MP Mr Papa
Mr Papa
Member

yes, have been watching that wp discussion on removing that…  will get very interesting if they do…  actually would be best if they did, but could be nightmare, as you say, for folks – especially those who notoriously dont keep their plugins up to date…  lol, have to roll my eyes when we help someone and I see 20 or 30 plugins with updates available… ;)