Skip to content

Instantly share code, notes, and snippets.

View fjarrett's full-sized avatar

Frankie Jarrett fjarrett

View GitHub Profile
@fjarrett
fjarrett / using-ceph-or-s3-bucket-for-wp-uploads.md
Last active December 16, 2021 23:00
How to use a Ceph (or S3) bucket for WordPress uploads

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 / gist:0fa79273bd879f7ab6b3
Last active September 3, 2021 20:19
Prevent Concurrent Logins
<?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 / site-remove.sh
Last active June 7, 2021 09:55
Remove a virtual host from Apache
#!/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 / Retry.php
Last active March 14, 2021 17:18
Trait to support retry annotations on PHPUnit tests, inspired by https://blog.forma-pro.com/retry-an-erratic-test-fc4d928c57fb
<?php
namespace Tests\Traits;
use PHPUnit\Util\Test as TestUtil;
use Throwable;
trait Retry
{
/**
@fjarrett
fjarrett / async-array-walk.php
Last active January 26, 2021 17:38
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.
<?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 / mu-plugin--wildcard-redirect-to-primary-host.php
Last active May 18, 2020 15:52
Wildcard redirect to primary host from alias domains
<?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 15:48
Block robots from crawling alias domains
<?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;
}
@fjarrett
fjarrett / atomic_deploy.sh
Created May 9, 2020 01:07
Just an idea for a "Capistrano style" deploy setup
#!/usr/bin/env bash
# BASE (required) - Releases are stored here.
# TARGET (required) - This will be a symlink to the current release.
# REPO (required) - The address to your Git repo.
# KEEP - How many latest releases to keep around (default is 10).
#
# Example:
#
# BASE=/var/www/foo/releases TARGET=/var/www/foo/public_html REPO=git@github.com:foo/bar.git ./atomic_deploy.sh 2b5d4a
@fjarrett
fjarrett / lock-down.php
Last active October 28, 2019 17:59
Simple must-use plugin to lock down WordPress and keep it up-to-date. Useful for canary sites.
<?php
/**
* Place this file in `wp-content/mu-plugins/lock-down.php` and it will be automatically loaded by WordPress.
*/
// Auto-update all the things.
add_filter( 'auto_update_core', '__return_true' );
add_filter( 'auto_update_plugin', '__return_true' );
add_filter( 'auto_update_theme', '__return_true' );
@fjarrett
fjarrett / wait-for-php-server-start.php
Created August 30, 2019 16:45
Use the Requests lib to wait for the built-in PHP server to start up
<?php
$process = new Process( sprintf( 'php -S localhost:8080 -t %s', __DIR__ ) );
$process->start();
$start = time();
// Wait for the server to start up (5 seconds max).
do {
try {