Composer based on path

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.

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 history at full.
    My main use to chain commands.

Meta query order

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:

$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.

wp-cli eval-file

wp-cli can be used to execute a php file with the eval-file command.
Biggest plus; WordPress is loaded when you execute that file.
The point it will call this file is after the init action and after the later wp_loaded action.

WordPress can’t use number as page names

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.

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

Source: wp_unique_post_slug

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

Bash argument set

Check if a bash argument is set in a script

File varcheck.sh

#!/usr/bin/env bash

if [ ! -z ${1+x} ]; then
    echo "var is set to '$1'";
else
    echo "var is unset";
fi

varcheck.sh will output: var is unset varcheck.sh TADA will output: var is set to 'TADA'

Nginx change default user

Default nginx runs on the user www, www-data of something alike.

You can change it. Be sure that you are aware of the security risks!
Don’t change it to a user that has access to sudo!

In /etc/nginx/nginx.conf on line #1 change the user.

#user www-data;
user deployment;

And change the user for php-fpm (If you’re using php-fpm 😉)

Create a file in /etc/php/7.2/fpm/pool.d/ (check the php version)
I suggest something like zzz-custom-user.conf so it’s loaded last. To that file add.

user = deployment
group = deployment
listen.owner = deployment
listen.group = deployment

Next reload everything, agian check php version.

sudo nginx -t && sudo service php7.2-fpm restart && sudo service nginx restart

Finally change the owner of the webfiles

sudo chown pi:pi /var/www/ -R

All done.

git reflog

If you lost a commit in the sea of commits or accidentally made a commit to a detached HEAD

git reflog

(half a year later, I have no clue what the problem was, nor how this solved it 🤷)

Posted in TIL