Skip to content

Instantly share code, notes, and snippets.

Avatar

Frankie Jarrett fjarrett

View GitHub Profile
@fjarrett
fjarrett / gist:0fa79273bd879f7ab6b3
Last active Sep 3, 2021
Prevent Concurrent Logins
View gist:0fa79273bd879f7ab6b3
<?php
/**
* Detect if the current user has concurrent sessions
*
* @return bool
*/
function pcl_user_has_concurrent_sessions() {
return ( is_user_logged_in() && count( wp_get_all_sessions() ) > 1 );
}
@fjarrett
fjarrett / unautop-func.php
Last active Aug 6, 2021
Inverse behavior to the wpautop() function found in WordPress
View unautop-func.php
<?php
/**
* Replaces paragraph elements with double line-breaks.
*
* This is the inverse behavior of the wpautop() function
* found in WordPress which converts double line-breaks to
* paragraphs. Handy when you want to undo whatever it did.
*
* @see wpautop()
@fjarrett
fjarrett / webhook-wp-cli.php
Last active Jul 10, 2021
Run a WP-CLI command via an authorized HTTP POST webhook
View webhook-wp-cli.php
<?php
/**
* Run a WP-CLI command via an authorized HTTP POST webhook.
*
* curl -X POST https://mysite.com/webhook-wp-cli.php \
* -H 'Content-Type: application/json' \
* -H 'X-Authorization: Bearer MY_SUPER_SECRET_KEY_HERE' \
* -d '[ ["theme", "install"], ["https://downloads.wordpress.org/theme/go.zip"], ["force"], {"user": 1} ]'
*
@fjarrett
fjarrett / site-remove.sh
Last active Jun 7, 2021
Remove a virtual host from Apache
View site-remove.sh
#!/bin/bash
echo "Enter the domain of the site you want to remove:"
read DOMAIN
if [ ! -d /var/www/$DOMAIN ]; then
echo -e "\x1b[1;31mError:\e[0m $DOMAIN does not exist!"
@fjarrett
fjarrett / centos.sh
Created Jun 5, 2017
Bash alias to allow root to run WP-CLI commands as another user
View centos.sh
# force `wp` commands to run as the `www-data` user
wp() {
if [ $1 == "cli" ]; then
command wp $* --allow-root
elif [ $(whoami) != "www-data" ]; then
su www-data -c "wp $*"
else
command wp $*
fi
}
@fjarrett
fjarrett / Retry.php
Last active Mar 14, 2021
Trait to support retry annotations on PHPUnit tests, inspired by https://blog.forma-pro.com/retry-an-erratic-test-fc4d928c57fb
View Retry.php
<?php
namespace Tests\Traits;
use PHPUnit\Util\Test as TestUtil;
use Throwable;
trait Retry
{
/**
@fjarrett
fjarrett / async-array-walk.php
Last active Jan 26, 2021
Apply a user supplied function to every member of an array, in parallel. Similar to PHP's array_walk(), except the input array isn't passed by reference and the callbacks are ran asynchronously. The technique is highly portable and requires only PHP 5.4 and the PCNTL extension.
View async-array-walk.php
<?php
/**
* Apply a user supplied function to every member of an array, in parallel
*
* Similar to PHP's array_walk(), except the input array isn't passed by
* reference and the callbacks are ran asynchronously. The technique is highly
* portable and requires only PHP 5.4 and the PCNTL extension.
*
* The most obvious limitation is that we can't pass the input array by
@fjarrett
fjarrett / using-ceph-or-s3-bucket-for-wp-uploads.md
Last active May 22, 2020
How to use a Ceph (or S3) bucket for WordPress uploads
View using-ceph-or-s3-bucket-for-wp-uploads.md

How to use a Ceph (or S3) bucket for WordPress uploads

Listed below are the steps I took to use Ceph object store for WordPress media without a plugin. It works by mounting Ceph (or an AWS S3) bucket as a network device on the file system via s3fs and using wp-content/uploads/ as the mount path.

Since s3fs is POSIX compatible, it means you can still access (and manage) the media within wp-content/uploads/ over SFTP/SSH as if they are natively there.

WP-CLI commands such as wp media import and wp media regenerate also still work.

Although your media is being stored and fetched from a network storage bucket, your web server can still resolve all the requests to wp-content/uploads/ on the local filesystem like normal.

This means that image URLs do not have to be rewritten and will use the core format you're used to over HTTP https://mysite.com/wp-content/uploads/cat.jpg and any absolute filesystem paths in PHP that may exist `/path/to/wp-content

@fjarrett
fjarrett / mu-plugin--wildcard-redirect-to-primary-host.php
Last active May 18, 2020
Wildcard redirect to primary host from alias domains
View mu-plugin--wildcard-redirect-to-primary-host.php
<?php
add_filter( 'init', function () {
if ( empty( $_SERVER['HTTP_AKAMAI_ORIGIN_HOP'] ) && empty( $_SERVER['HTTP_X_DSA_HOST'] ) && ! empty( $_SERVER['SERVER_NAME'] ) && 'origin-garage.godaddy.net' === $_SERVER['SERVER_NAME'] ) {
$path = ! empty( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : '/garage';
wp_safe_redirect( 'https://www.godaddy.com' . $path, 301 );
@fjarrett
fjarrett / mu-plugin--block-robots-on-alias-domains.php
Last active May 18, 2020
Block robots from crawling alias domains
View mu-plugin--block-robots-on-alias-domains.php
<?php
add_filter( 'option_blog_public', function ( $blog_public ) {
if ( empty( $_SERVER['HTTP_AKAMAI_ORIGIN_HOP'] ) && empty( $_SERVER['HTTP_X_DSA_HOST'] ) ) {
$blog_public = false;
}