:::: MENU ::::
Browsing posts in: PHP

5 Reasons why Pressmatic is the Ultimate MAMP/Vagrant/DesktopServer Replacement Tool

I’ve had the privilege of being a part of an early testing group for the brand new Mac (and soon for Windows) application called Pressmatic.  While Pressmatic has only been around a short while, it instantly competes with all the other players in the game.  There are already a few in-depth reviews available, so I just want to go over some killer features that will hopefully convince you to give Pressmatic a chance.  Let’s begin.

1. ngrok

What in the world is ngrok? That was my first thought too.  ngrok is a tunneler that allows you to share your local development sites with clients, or friends without the need to deploy.  Ever wanted to check out a local site on your smartphone, only to spend the next couple hours trying to figure out a solution locally? (See my squidman post) And then you usually just deploy to a live server anyway in order to test. ngrok changes that.  It’s super simple to use with Pressmatic.  All you need to do is enable the add-on:

ngrok-enable

And then in the site settings, under Utilites, enable the tunnel:

ngrok-enable

Once the tunnel is enabled, you can just click on the link to view the site, or type it into your smartphone’s browser and voila! You can view your local site from anywhere.

ngrok-enabled

2. MailCatcher

4 years ago, I wrote a post on how to send mail from Max OS X using Postfix.  That was 4 years ago (light years in tech terms) and yet not much has changed.  All I usually need is to make sure the site is sending emails properly, before deploying to production.  Enter MailCatcher.  MailCatcher runs a super simple SMTP server which catches any message sent to it to display in a web interface.  While this does not allow outbound emails, it does create a simpler solution for my needs than Postfix and all that mess.

To use MailCatcher, simply navigate to the site settings > utilities tab and click on “Open Mailcatcher”.

mailcatcher-launch

Once opened, you will be able to see all emails sent through your Pressmatic WordPress install.

mailcatcher-launched

3. Hot-Swapping PHP Versions

So, in a perfect world, everybody would be using the latest version of PHP, and all their code would be compatible.  But we don’t live in a perfect world and with WordPress officially supporting PHP 5.2.4 still, we have to write code that works for everyone.  With that being said, Pressmatic makes that super simple by allowing you to change out PHP versions on the fly.  After your site is created, just go to the main site settings page and choose which PHP version you want from the list (I hear more PHP versions will be available soon!):

php-versions

Once you’ve made your change, you can confirm that the correct PHP version is working by pressing on information icon right next to the versions drop-down.  That will open up a phpinfo() page.

php-info

4. One-Click MySQL Admin for Sequel Pro or Adminer

Do you ever find yourself scratching your head trying to remember what the login info is for your MySQL database?  Well with Pressmatic, not only do you get all that information readily available in the Database tab, you can also use the one-click buttons to login to the database using Sequel Pro or the built in Adminer (who uses phpmyadmin any more anyway?).

mysql

5. Simple SSH

For those of you who are advanced developers, this one is for you.  Since Pressmatic is built using docker containers, you have to run any scripts or commands you want inside that virtual machine.  For SSH access, simply right click on the site and select “Open Site SSH”:

ssh-open

That will open up terminal and log you in directly to the virtual machine as the root user.  Simple (and way faster than vagrant ssh).

ssh-terminal

You can even set your default SSH client in the main settings:

terminal-app

As you probably have figured out by now, the theme of this post is: Pressmatic is simple. Enough talk, do yourself a favor and try it out today!

pressmatic-raster


Using/Updating wp_localize_script() Dynamically

One of the greatest features of WordPress is its built-in function library that allows so much flexibility in web design.  Whether you are a plugin developer or theme designer, WordPress has functions that make your life a heck of a lot easier.

On occasion though, some of the functions that are super cool, could sometimes use a little tweaking. One of those functions is wp_localize_script().

Simply put, wp_localize_script: “Localizes a script, but only if script has already been added. Can also be used to include arbitrary Javascript data in a page.”

I usually find myself adding in “arbitrary Javascript data” in the page alongside my localization strings if they are needed at all.  A sample of the code used to generate the localized script looks like this:

[cc lang=”php”] __(‘Hello’),
‘world’ => __(‘world’)
);
wp_localize_script(‘enqueued-script’, ‘obj’, $localized_data);
?>[/cc]

Note that this function calls a previously enqueued script which is not covered in this tutorial.

WordPress will then print onto the final page a Javascript object that will look like this:

[cc lang=”html”][/cc]

From time to time, I have wanted to add in an element to this object dynamically, but if I decided to localize the script again prior to the page load, the output would look like this:

[cc lang=”html”][/cc]

wp_localize_script just adds the new data as a new line item regardless of whether or not the objects are the same. This is not ideal especially if there is the chance that it could grow any larger than just three elements in the object.

After much searching, I came up with a solution that does not require any hacking or changing of the WordPress core (yay!) and is actually fairly simple to implement.

First, before we call our wp_localize_script function, we need to check and see if any localized data exists in our script that we have already enqueued. To do that, we must access the global $wp_scripts; object and use the get_data() function:

[cc lang=”php”]
global $wp_scripts;
$data = $wp_scripts->get_data(‘obj’, ‘data’);
[/cc]

If the data is empty, we can go ahead and localize our script:

[cc lang=”php”]
if(empty($data)) {
wp_localize_script(‘enqueued-script’, ‘obj’, $localized_data);
}
[/cc]

However, if we have already localized the script and some data exists, we need to convert that into some useable data and add to it before putting it back. First, we are going to check if $data is an array. If not, lets convert it from the JSON format that it exists in. We will need to remove the last semicolon character and the “var obj = ” intro in order to convert it:

[cc lang=”php”]
if(!is_array($data)) {
$data = json_decode(str_replace(‘var obj = ‘, ”, substr($data, 0, -1)), true);
}
[/cc]

Then we can add the existing elements to our new array:

[cc lang=”php”]
foreach($data as $key => $value) {
$localized_data[$key] = $value;
}
[/cc]

Finally, we need to empty out the data that is currently localized and then localize our new array:

[cc lang=”php”]
$wp_scripts->add_data(‘enqueued-script’, ‘data’, ”);
wp_localize_script(‘enqueued-script’, ‘obj’, $localized_data);
[/cc]

Altogether, this looks like:

[cc lang=”php”]get_data(‘enqueued-script’, ‘data’);
if(empty($data)) {
wp_localize_script(‘enqueued-script’, ‘obj’, $localized_data);
}
else {
if(!is_array($data)) {
$data = json_decode(str_replace(‘var obj = ‘, ”, substr($data, 0, -1)), true);
}
foreach($data as $key => $value) {
$localized_data[$key] = $value;
}
$wp_scripts->add_data(‘enqueued-script’, ‘data’, ”);
wp_localize_script(‘enqueued-script’, ‘obj’, $localized_data);
}
?>[/cc]

In my example, the new Javascript object would look like this:

[cc lang=”html”][/cc]

Let me know if this helps you out in any way!