VVV provision single site

For the last month and a half I’ve started using vvv, and it works like a charm.
As I’m setting up a lot of sites I have to run provision a lot.
And that is quite a heavy operation in vvv because it pretty much checks every thing.

Luckily there is also a way to provision a single site.

vagrant provision --provision-with=site-example

Where site-example is the same as the site key in vvv-custom.yml.

There are limitations to this. *Note to self investigate this and write it down.

wp cli output format with –porcelain

wp db export

#output
Succes: Exported to 'dbname-2019-09-16-2790c11.sql'

Often you need the filename. And if you only need the file name that’s annoying to parse.

--porcelain flag to the rescue!

wp db export --porcelain

#output
dbname-2019-09-16-2790c11.sql

This will work on a lot of commands that have one item output.
Stuff like wp post create ....
Here is a complete list of commands that have the –porcelain flag

Commands that output more items usually have a --format flag to handle output.

Declare a variable to a bash script runtime

In the past I’ve did a post about checking if a bash argument is set.
This is different, This way you can pass through a named variable.

    TEST='pass it on' ./testscript.sh

If you do this $TEST will be set inside the ./testscript.sh bash script.
Like the normal arguments you probably would like to validate them inside the bash script.
The main plus side is that you can put arguments in any order and exclude arguments.
It might also be easier to read as you can see the arguments name.

A bit more advanced. You can override global variables for that script run.
Variables like $PWD and $PATH, be careful.

Example script: ./testscript.sh

#!/usr/bin/env bash

if [ ! -z $TEST ]; then
    echo "var is set to '$TEST'";
else
    echo "var is unset";
fi

Global git ignore

A new laptop a new change to optimize stuff. And this is one I should have done years ago.
There are files you always want to avoid. The infamous .DS_Store file comes to mind.

On ubuntu just add whatever you want to exclude to ~/.config/git/ignore

Copy files/folders over ssh

Sometimes you just want to copy files from a remote server, and you want it often and quick. Stuff like a DB dump or the whole uploads folder.
You could startup your (s)ftp client and browse to it. Or you could just grab it with ssh.

Download one file

rsync -rvz --progress user@server:/var/www/janw.me/uploads.tar.gz ~/Downloads

To upload flip the source and destination

rsync -rvz --progress ~/Downloads/uploads.tar.gz user@server:/var/www/janw.me/ 

Download a whole folder

scp -r [email protected]:/full/path/to/folder /local/path/to/Downloads

Alternate ports

Sometimes the remote server had alternate ssh ports.

  • On rsync: -e 'ssh -p 2223'
  • on csp: -P 2223

Composer Include one folder

I wanted to include one directory form an svn repo.
With help of stackoverflow I found the solution.

"require-dev": {
    "wordpress/phpunit": "*"
},
"repositories": [
    {
        "type": "package",
        "package": {
            "name": "wordpress/phpunit",
            "version": "1.0.0",
            "source": {
                "url": "https://develop.svn.wordpress.org",
                "type": "svn",
                "reference": "trunk/tests/phpunit/includes/@43534"
            }
        }
    }
]

There is a downside, it won’t update. Unless you up the version inside the repositories block.
The upside is that this is lightweight, especially if the rest of the repo is big.
This is rarely the right solution.

Bash edit command

I like doing a lot in one command. Downside it that stuff can get messy. One big line. you can’t easily edit it.
A shortcut to make it easier is ctrl-x-e That will open the current command in an editor.

Few thinks to keep in mind.

  • Multiple lines will work, but it will execute it as separate commands.
  • using && at the end of lines will fix that.
  • Which of course means you get it in the bash history at full.
    This is my main use to chain commands.

Meta query order of arrays

The site had 20K plus posts/pages/ect, freshly put live.

The hoster called. It had slow queries, 9 seconds on average. A lot, multiple per minute. The same query over and over again.
It was a pretty normal post table query. It had one part extra.
A meta query was to exclude all spot_closed posts. When I removed that it was quick. It didn’t show anything special.

The meta query was added as such:

<?php
$meta_query_spots = [
    'relation'     => 'OR',
    [
        'key'     => 'spot_closed',
        'value'   => 1,
        'compare' => '!=',
    ],
    [
        'key'     => 'spot_closed',
        'compare' => 'NOT EXISTS',
    ],
];

Check if the spot is closed, and if it a spot does not have a close status.
The fix, flip the checks. And bam query less then half a second.

<?php
$meta_query_spots = [
    'relation'     => 'OR',
    [
        'key'     => 'spot_closed',
        'compare' => 'NOT EXISTS',
    ],
    [
        'key'     => 'spot_closed',
        'value'   => 1,
        'compare' => '!=',
    ],
];

WordPress can’t use number as page slugs

Where is what we wanted. A page example.com/404
So we had a page in WordPress so the content was controllable.

But on save the slug was changed to 404-2.

I figured maybe that slug was preserved. So after some digging I found this beautiful *ahem* if statement in core.

<?php
if (
    $post_name_check || in_array( $slug, $feeds )
    || 'embed' === $slug
    || preg_match( "@^($wp_rewrite->pagination_base)?d+$@", $slug )
    || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent )
)
{ //....

The part it fails on is the preg_match. No number is accepted.
So it’s not just the slug 404 but every number.

The reason for this because it preserves numbers for pagination (including in-page pagination).

So the solution was to rename the page to example.com/404-page

WordPress Action Priority

I wanted to add_action between 2 different actions.
These actions where priority 3 and 4. So I tried 3.5. That didn’t work.

Decimals get rounded down. What did work was the string “3.5”

This got me curious about strings and the sorting in general. Under the hood WP just uses array_keys
To make things easier here is a list of use cases:

WordPress action how a priority is sorted