PHP 7.2 and the 502 error

My site was running fine and dandy for a few years.  I made the necessary adjustments to make it compatible with PHP 7.2 and upgraded the server.  A few days later I started receiving random 502 errors.

The problem apparently was a simple else if statement within a WordPress filter.  Here is the important parts of the code:

function php72_killer() {
  for($hour = 8; $hour < 17; $hour++) :
    $display_hour = $hour;
    $ampm = 'am';
    if ( $hour == 0 ) {
      $display_hour = 12;
    }
    else if ( $hour > 12 ) {
      $display_hour = $hour - 12;
      $ampm = 'pm';
    }
    else if ($hour==12) {
      $ampm = 'pm';
    }
  endfor;
}
add_meta_box( 'some_id', 'Section Title', 'php72_killer', 'post_type', 'normal' );

That last else if turned out to be the problem?  Why?  I don’t fully understand, but I found PHP 7.2 introduced optimized else ifs:  https://derickrethans.nl/php7.2-switch.html

It’s important to note that this for loop with if-statements outside of the WordPress callback doesn’t cause random 502 errors.

So what fixed my 502 errors?  not having a 2nd else if:

function php72_killer() {
  for($hour = 8; $hour < 17; $hour++) :
    $display_hour = $hour;
    $ampm = 'am';
    if ( $hour == 0 ) {
      $display_hour = 12;
    }
    else if ( $hour > 12 ) {
      $display_hour = $hour - 12;
      $ampm = 'pm';
    }
    if ($hour==12) {
      $ampm = 'pm';
    }
  endfor;
}
add_meta_box( 'some_id', 'Section Title', 'php72_killer', 'post_type', 'normal' );

Go figure.

Tagged with: , ,

SVG as Background

When using an SVG as background with background-size: cover, the background would sometimes appear with a space on the left side.  Depending on the browser width it would be anywhere from 1px to 5px.   To solve this the SVG needs the preserveAspectRatio=”none” property.

Found the solution to a similar problem here: https://stackoverflow.com/questions/9185434/using-svg-as-background-image

Tagged with:

Print a trace of the call stack

In javascript you can print the call stack with something like this:

function print_call_stack() {
  var stack = new Error().stack;
  console.log("PRINTING CALL STACK");
  console.log( stack );
}

from: https://stackoverflow.com/questions/4671031/print-function-log-stack-trace-for-entire-program-using-firebug

Tagged with:

Javascript Done Resize

Normally code in the resize event is meant to just run once when the user is done resizing.  This code below, from css-tricks and seen elsewhere will do the trick:


var resizeTimer;
$(window).on('resize', function(e) {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
// Run code here, resizing has "stopped"
}, 250);
});

Tagged with:

PHPUnit and PHP 7 problems

Problem

I’m using PHPUnit 5.5, upgraded my PHP version to 7.2.  Two problems actually occur.  First there’s this weird error:

Fatal error: Declaration of SebastianBergmann\Comparator\DOMNodeComparator::assertEquals…

Then figuring PHPUnit needs to be upgraded, I ran

phpunit –self-upgrade

And that failed with the same error.  Changing my PHP version back down to 5.6 and running then causes this error:

internal corruption of phar “…phpunit-temp.phar” (truncated entry)

 

Solution

Unfortunately PHPUnit 5.5 only supports up to version 7.1 and any old version of PHPUnit that has an “old” certificate needs to be downloaded/updated manually.

https://github.com/sebastianbergmann/phpunit/issues/1688

 

How to stop redirect from http:// to https:// in Chrome

Problem

I’ve been trying like crazy to get to my local website at site.dev, but I keep getting redirected to https://site.dev and of course it doesn’t work because I don’t have a SSL certificate for that domain.

Solution

As of December 2017, Chrome 63, Chrome is forcing all .dev domains to be redirected to HTTPS via a preloaded HTTP Strict Transport Security (HSTS) header.  The .dev TLD is an actual legitimate TLD so you will need to change your local development setup to use something like http://site.localhost .

A more detailed explanation can be found here:

Chrome to force .dev domains to HTTPS via preloaded HSTS

Tagged with: ,

SVG height not scaling properly on IE 10

Problem

IE 10 shows a skewed svg image who’s width is set to a specific value and height is auto.

 

Solution

The problem is the width and height in the SVG file is set and IE is following those specs if you don’t set it.  So if you’re image is 100×100 and you set only the width to 50px, the height will remain at 100px.  The solution is to remove the width and height attributes in the <svg> tag.

Caveats

A solution on a github gist suggests that removing the width and height attributes force the image to occupy the full width of its container in non-IE browers.  All my current images with SVG are supposed to fill the width so this isn’t a problem for me.
Link to the gist: https://gist.github.com/larrybotha/7881691

Tagged with: ,

wpdb insert returning false

Problem

When running wpdb->insert the result returns false.  All the values seem to check out to be fine.

 

Solution

The problem might be due to this bug in WordPress.  Basically one of your fields is too long and WordPress is not completing the insert.  Reading through the ticket, it seems it is a WordPress error and not a MySql error so it would be pretty difficult to go through all the fields, determine the column field size limits, and then return an error of some sort.  Anyway, this is one possible reason to wpdb->insert returning false.

Tagged with: ,

WP Cron with Basic Authentication

Problem

WordPress’ cron doesn’t work when a site has basic authentication.  You basically would get a 401 error.

 

Solution

Thanks to Nick Ohrn (in 2014) this problem is solved with a simple mu-plugin.  You just need to set the username and password in a constant in wp-config and add some code to the mu-plugins directory.  Here is his quick tip.

Tagged with:

Where is wp_capabilities?

Problem

After creating a user with a certain role I need to do a custom sql query where it only selected users of a specific role.  This failed to find anything:

select u.ID, u.display_name, u.user_login
from $wpdb->users as u
inner join $wpdb->usermeta as umr
on ( umr.user_id = u.ID )
and ( umr.meta_key = 'wp_capabilities' )

$user = get_userinfo( $user_id );
var_dump( $user->roles );

get_userinfo got the right role, so saving was correct.

$caps = get_user_meta( $user_id, 'wp_capabilities' );
var_dump( $caps );

$caps was null.  weird.

 

Solution

You learn something new everyday.  After looking through the WordPress core code I found that the meta key is actually composed of the table prefix and ‘capabilities’.  So the correct query would be:

select u.ID, u.display_name, u.user_login
from $wpdb->users as u
inner join $wpdb->usermeta as umr
on ( umr.user_id = u.ID )
and ( umr.meta_key = '" . $wpdb->prefix . "capabilities' )

Tagged with: , ,
Top