PHP Best Practices

You know the worst part about focusing on function over form, and working mostly on your own? You neglect to make sure that your coding style is uniform, that you’re following best practices, and that your code is properly SPACED and INDENTED (one tab per level). A lot of times I don’t have to go back to the code or share it, so it just gets left the way it is – because it works.

I don’t even want to go into how many times I’ve had to go back over my code and space out variables inside of brackets, and move them from being in the function calls themselves to being declared outside of the function call. At the same time, you have to make sure you don’t copy extra variables – that is, instead of copying predefined variables to smaller-named variables, just use the predefined variables to keep memory usage down.

Following DRY princples (Don’t Repeat Yourself) is another one. When I am starting something from scratch, I’m famous for repeating procedures that could be condensed into a single function call, then called multiple times. At least when reviewing after the initial coding spree I will go back and condense and clean up, but it’s still not fun when you get into 1000s of lines of code. We iterate versions and find better ways to do things for a reason, I guess.

Ultimately, it’s up to the coder as to their own particular style, but realistically, if you train yourself to do it ahead of time rather than just powering through an evening getting the project completed and working (I’m guilty) as soon as possible, then you’ll save yourself time in the long run.

 

IMAP Migration PHP Script v2

A few things have changed since the last time I posted the IMAP migration script, and my wishlist of features is almost complete. Instead of a single file that you have to manually specify variables in, you now have fillable form that uses jQuery.post to send variables to another file (called process.php), which handles the actual migration. What this does is solve the memory issue, as you can specify the incremental value (number of emails it will transfer at a time) and it also makes it a little more user-friendly. I added support for unread emails (instead of just marking everything as read), and changed the imap_body command to “peek” instead of doing a full open, which marks the email as read as it’s transferring. This time around I am not going to post the code, but it will be included in the zip file.

On the todo list is directory traversing for people who organize their emails into folders, and some bug fixes.

Unzip this into a folder on your server, then run “email.php” from your browser.

Download here

 

imapmigration

WordPress Variable Monitoring Script

I ran into an issue with WP E-commerce recently where the shipping options appeared to be unchecked every day that we went to go and look at them. Turns out it was a line in one of the shipping files that reset the shipping array to just “shipwire” which made it appear as if someone was turning off shipping every day (or a cron job, in this case).

Read about it here: https://wordpress.org/support/topic/upgrade-to-43-and-problems-with-shipping-calculator

Anyways, I created a script to monitor changes to custom_shipping_options and thereby found out what the issue was. Here’s the code, hope it helps.

 

Chromecast

This is a throwback to last year;

I was watching TV with my oldest son when I heard the familiar jingle of an email on my phone, or my smartwatch, or both. It was a client – as usual – after hours, wanting a quick response. I ran downstairs and told him to finish his snack. After firing off a few (not particularly) urgent email replies, I went back upstairs to find him sitting on the couch, watching Harold and the Purple Crayon. He looks at me from the couch and says “this is my FAVOURITE, daddy!”. The slow realization that he switched the input on the TV, opened up Netflix on his iPad, and used the Chromecast all by himself hits me … this is about the extent of what my wife, parents, and in-laws know about technology. He’s 4.

Mic drop, walk away, my job is done.

Parents

You all know what I’m talking about, parents and technology. It’s been around for the better part of three decades, but when did they start to learn about computers? Oh, about the past 5 years – or less. It’s a stigma, and it means learning something new, and being afraid of not being good at it. My Mum has been quite good at it – she had to use computers all the time while working in a high school. Hand her a new iPhone and a tablet and she picks up on it pretty quickly. It’s intuitive though, they make it easier for a reason. Explaining over the phone how to switch inputs to their fancy blu-ray player and getting the sound system to work is the hard part – but I guess that’s what we have a four year old for.

Here, I’ll just send him over, he’ll do it for you! (only half joking).

Babies

That day. When you’re in a really important meeting and you realize you have dried drool on your shoulder, and don’t even know it. Then when you’re on your way home you realize “That’s how they knew I have kids!”. When this happens multiple times, including having a soother pop out of your laptop bag, and cheerios in your pocket. “I .. don’t know how that got in there…!”, I say, before realizing this is not the corporate world, and I don’t have to wear a suit and tie to a cubicle every day. #lovemyjob

(Luckily, this is a great ice breaker, and when I start to gush about how our oldest starts Kindergarten, and our youngest is walking and talking, I go from being “that computer guy” to being a personable programmer – with a smartphone full of videos and pictures – want to see him eating? Shaking his head? Blabbering about nothing? I’m that parent!)

WP Hotfix 33423 WHM Easy Rollout

WordPress 4.3 has a cron issue which may affect your sites.

There is a bug in WordPress 4.3 which can affect some sites causing high CPU loads and / or Database issues.

“Just spent a few hours debugging two sites which went down and tracked it down to wp_options cron exploding in size. There apparently is a bug in 4.3 which causes the cron to grow in size. This can cause all kinds of issues, particularly when the mysql packet size is reached.
The bug has been fixed, but won’t be released until 4.3.1. If you are experiencing issues, there is a solution here :
https://core.trac.wordpress.org/ticket/33423[1]”

For a quick way to transfer it to all of your client folders from within WHM, first install it onto a “donor” site, then SSH into your server, and run the following command – replacing codingconcepts with your donor user folder.  Activate from the dashboard and problem solved – or email your client to get them to activate it. This is a lot easier when you have MainWP installed on all of your client sites!

for i in cat /etc/trueuserdomains | cut -d: -d' ' -f2- ;do cp -r /home/codingconcepts/public_html/wp-content/plugins/wp33423-hotfix/* /home/$i/public_html/wp-content/plugins ;done

Commits and Gits

Submitted my first change request/commit for Jetpack, which I already have in use on my site. You’d think having Google+ pages as a link would be something that they’d already have on there? Strange! Find it here: https://github.com/Automattic/jetpack/pull/2654

I went a little further down the blogging road with MainWP and converted my plugin/extension over to a Class, and will also be investigating implementation of codecanyon keys into a plugin… and whether or not it would be worth it. Is there better options? Is there a paid route for plugins that works the best? I dunno, but I’ll figure it out.

Updates!

Been a very interesting couple of weeks! I got married, have started blogging for MainWP (a software company that creates a management suite for WordPress developers such as myself), I created a basic extension using their framework on top of a plugin, all part of the tutorials here: http://blog.mainwp.com/creating-a-wordpress-plugin/, and am in the process of uploading a video tutorial for my friends over at www.laptopscreen.com for a Lenovo LCD screen I just replaced. The video is here – http://youtu.be/O-PfEaS7F78

Creating a Plugin

 

I’m going to dive right into this blog post on creating a plugin for WordPress. There are countless tutorials out there and ways to code and structure a plugin, but for the purposes of these articles I’m going to make it short and simple.

Continue reading Creating a Plugin