Commit 9c52b703 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Get the media upload dir programmatically

This means that any volumes mounted for persistance of the media are
not required to be at a hard-coded location (subject to the location
configuration being modifiable, work yet to do).
parent f0a9fc96
Loading
Loading
Loading
Loading
Loading

plugins/media-url.php

0 → 100644
+40 −0
Original line number Diff line number Diff line
<?php
/**
 * Plugin Name: Media URL Fix
 * Plugin URI: https://code.kodo.org.uk/singing-chimes.co.uk/wordpress/tree/master/plugins
 * Description: Adjusts the media URL path base to /media, where the Nginx instance is hosting it.
 * Licence: MPL-2.0
 * Licence URI: https://www.mozilla.org/en-US/MPL/2.0/
 * Author: Dominik Sekotill
 * Author URI: https://code.kodo.org.uk/dom
 */

add_action( 'plugins_loaded', function() {
	add_filter( 'upload_dir', function( $paths ) {
		$baseurl = parse_url( $paths['baseurl'] );
		$fullurl = parse_url( $paths['url'] );
		$subdir = $paths['subdir'];

		$baseurl['path'] = '/media';
		$fullurl['path'] = '/media' . ($subdir ? "/{$subdir}" : '');

		$paths['baseurl'] = unparse_url( $baseurl );
		$paths['url']     = unparse_url( $fullurl );

		return $paths;
	});
});

function unparse_url( array $parts ) {
	return (
		(isset($parts['scheme'])   ?  "{$parts['scheme']}://" : '') .
		(isset($parts['user'])     ?    $parts['user']        : '') .
		(isset($parts['pass'])     ? ":{$parts['pass']}"      : '') .
		(isset($parts['user'] || isset($parts['pass'])) ? '@' : '') .
		(isset($parts['host'])     ?    $parts['host']        : '') .
		(isset($parts['port'])     ? ":{$parts['port']}"      : '') .
		(isset($parts['path'])     ?    $parts['path']        : '') .
		(isset($parts['query'])    ? "?{$parts['query']}"     : '') .
		(isset($parts['fragment']) ? "#{$parts['fragment']}"  : '')
	);
}
+17 −3
Original line number Diff line number Diff line
@@ -122,15 +122,29 @@ setup_components() {
	return 0
}

get_media_dir()
{
	[[ -v MEDIA ]] && return
	MEDIA=$(
		wp config get UPLOADS --type=constant ||
		wp option get upload_path
	)
	[[ -n "${MEDIA}" ]] && return
	local _wp_content=$(wp config get WP_CONTENT_DIR --type=constant)
	MEDIA=${_wp_content:-wp-content}/uploads
}

setup_media()
{
	# UID values change on every run, ensure the owner and group are set 
	# correctly on ./media
	chown -R $WORKER_USER:$WORKER_USER ./media
	# correctly on the media directory/volume.
	get_media_dir
	chown -R ${WORKER_USER}:${WORKER_USER} "${MEDIA}"
}

collect_static()
{
	get_media_dir
	local IFS=,
	declare -a flags=(flist stats remove del)
	test -t 1 && flags+=(progress2)
@@ -141,7 +155,7 @@ collect_static()
		--exclude-from=- \
		--exclude='*.php' \
		--exclude=static/ \
		--exclude=media/ \
		--exclude="${MEDIA}" \
		--force \
		--info="${flags[*]}" \
		--times \