Skip to content

Commit

Permalink
introduce wp-cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mcguffin committed Dec 23, 2024
1 parent 6c5d1d3 commit 196b160
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 33 deletions.
81 changes: 51 additions & 30 deletions include/ACFFieldOpenstreetmap/Core/MapProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,28 @@ private function proxify_tileset( $tileset, $provider_key, $variant_key = '' ) {
*/
public function setup_proxies() {

$upload_dir = wp_upload_dir( null, false );
$this->setup_proxy_dir();
return $this->save_proxy_config( $upload_dir['basedir'] );

}

/**
* Save proxy config
*
* @param string $destination_path
*/
public function save_proxy_config( $destination_path ) {

if ( ! WP_Filesystem() ) {
return false;
return new \WP_Error( 'acf-osm', __( 'No Filesystem', 'acf-openstreetmap-field' ) );
}

global $wp_filesystem;

$this->setup_proxy_dir();
if ( ! $wp_filesystem->is_writable( $destination_path ) ) {
return new \WP_Error( 'acf-osm', __( 'Filesystem not writable', 'acf-openstreetmap-field' ) );
}

$proxied_providers = LeafletProviders::instance()->get_providers( ['credentials'], true );
$proxy_config = [];
Expand Down Expand Up @@ -162,8 +177,6 @@ public function setup_proxies() {
}
}

$upload_dir = wp_upload_dir( null, false );

$content = '<?php' . "\n";
$content .= '/* Generously generated by the ACF OpenStreetMap Field Plugin */' . "\n";
$content .= sprintf(
Expand All @@ -172,30 +185,9 @@ public function setup_proxies() {
);

$wp_filesystem->put_contents(
$upload_dir['basedir'] . '/acf-osm-proxy-config.php',
untrailingslashit( $destination_path ) . '/acf-osm-proxy-config.php',
$content
);

}

/**
*
*/
public function uninstall() {

if ( ! WP_Filesystem() ) {
return false;
}

global $wp_filesystem;

$wp_filesystem->unlink(
$upload_dir['basedir'] . '/acf-osm-proxy-config.php',
$content
);

$proxy_path = trailingslashit( trailingslashit( WP_CONTENT_DIR ) . $this->get_proxy_path() ) ;
$wp_filesystem->rmdir($proxy_path);
}

/**
Expand All @@ -215,13 +207,22 @@ private function generate_url( $base_url, $options ) {
/**
* Setup proxy directory in wp-content/maps/
*/
private function setup_proxy_dir() {
public function setup_proxy_dir( $force = false ) {
global $wp_filesystem;

if ( ! WP_Filesystem() ) {
return new \WP_Error( 'acf-osm', __( 'No Filesystem', 'acf-openstreetmap-field' ) );
}

$proxy_path = trailingslashit( trailingslashit( WP_CONTENT_DIR ) . $this->get_proxy_path() ) ;

wp_mkdir_p( $proxy_path );

if ( ! $wp_filesystem->exists( $proxy_path . '.htaccess' ) ) {
if ( ! $wp_filesystem->is_writable( $proxy_path ) ) {
return new \WP_Error( 'acf-osm', __( 'Filesystem not writable', 'acf-openstreetmap-field' ) );
}

if ( $force || ! $wp_filesystem->exists( $proxy_path . '.htaccess' ) ) {
$content = '# Generously generated by ACF OpenStreetMap Field Plugin' . "\n";
$content .= 'RewriteEngine On' . "\n";
$content .= 'RewriteBase /wp-content/maps' . "\n";
Expand All @@ -230,7 +231,7 @@ private function setup_proxy_dir() {
$wp_filesystem->put_contents( $proxy_path . '.htaccess', $content );
}

if ( ! $wp_filesystem->exists( $proxy_path . 'index.php' ) ) {
if ( $force || ! $wp_filesystem->exists( $proxy_path . 'index.php' ) ) {
$upload_dir = wp_upload_dir( null, false );

$content = '<?php' . "\n";
Expand All @@ -248,14 +249,34 @@ private function setup_proxy_dir() {
}
}

/**
* Remove Proxy Directory
*/
public function reset_proxy_dir() {

if ( ! WP_Filesystem() ) {
return new \WP_Error( 'acf-osm', __( 'No Filesystem', 'acf-openstreetmap-field' ) );
}

global $wp_filesystem;

$proxy_path = trailingslashit( trailingslashit( WP_CONTENT_DIR ) . $this->get_proxy_path() ) ;

if ( ! $wp_filesystem->is_writable( $proxy_path ) ) {
return new \WP_Error( 'acf-osm', __( 'Filesystem not writable', 'acf-openstreetmap-field' ) );
}

return $wp_filesystem->rmdir( $proxy_path, true );
}

/**
* @param string $provider_key
* @param string $variant_key
* @return string
*/
private function get_proxy_path( $provider_key = '', $variant_key = '' ) {
$path = 'maps';
if ( $provider_key ) {
if ( $provider_key ) {
$path .= '/' . $provider_key;
if ( $variant_key ) {
$path .= '.' . $variant_key;
Expand Down
5 changes: 3 additions & 2 deletions include/ACFFieldOpenstreetmap/Core/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ public function maybe_upgrade() {
$old_version = get_site_option( 'acf-openstreetmap-field_version' );

// call upgrade
if ( version_compare($new_version, $old_version, '>' ) ) {
if ( version_compare( $new_version, $old_version, '>' ) ) {

// $this->upgrade( $new_version, $old_version );
// Do update stuff here
MapProxy::instance()->setup_proxy_dir( true );

update_site_option( 'acf-openstreetmap-field_version', $new_version ); // TODO: store blog-wide
}
Expand Down
78 changes: 78 additions & 0 deletions include/ACFFieldOpenstreetmap/WPCLI/Commands/MapProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* @package ACFFieldOpenstreetmap\WPCLI
* @version 1.0.0
* 2018-09-22
*/

namespace ACFFieldOpenstreetmap\WPCLI\Commands;

use ACFFieldOpenstreetmap\Core;

class MapProxy extends \WP_CLI_Command {

/**
* Init proxy dir in wp-content/maps/
*
* [--force]
* : Overwrite existing files
* ---
* default: 0
* ---
*
* ## EXAMPLES
*
* wp map-proxy install
*/
public function install( $args, $assoc_args ) {
$assoc_args = wp_parse_args($assoc_args, [
'force' => false,
]);
$proxy = Core\MapProxy::instance();
$result = $proxy->setup_proxy_dir( $assoc_args['force'] );
if ( is_wp_error( $result ) ) {
\WP_CLI::error( $result->get_error_message() );
} else {
\WP_CLI::success( "Create proxy directory in wp-content/maps/" );
}
}

/**
* remove proxy dir in wp-content/maps/
*
* ## EXAMPLES
*
* wp map-proxy uninstall
*
*/
public function uninstall( $args, $assoc_args ) {
$proxy = Core\MapProxy::instance();
$result = $proxy->reset_proxy_dir();
if ( is_wp_error( $result ) ) {
\WP_CLI::error( $result->get_error_message() );
} else {
\WP_CLI::success( "Removed proxy directory" );
}
}

/**
* Generate local proxy configuration
*
* ## EXAMPLES
*
* wp map-proxy configure
*
*/
public function configure( $args, $assoc_args ) {

$proxy = Core\MapProxy::instance();
$result = $upload_dir = wp_upload_dir( null, false );
$proxy->save_proxy_config( $upload_dir['basedir'] );

if ( is_wp_error( $result ) ) {
\WP_CLI::error( $result->get_error_message() );
} else {
\WP_CLI::success( "Create proxy config in ". $upload_dir['basedir'] );
}
}
}
35 changes: 35 additions & 0 deletions include/ACFFieldOpenstreetmap/WPCLI/WPCLI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* @package ACFFieldOpenstreetmap\WPCLI
* @version 1.0.0
* 2018-09-22
*/

namespace ACFFieldOpenstreetmap\WPCLI;

if ( ! defined('ABSPATH') ) {
die('FU!');
}

use ACFFieldOpenstreetmap\Core;

class WPCLI extends Core\Singleton {

/**
* @inheritdoc
*/
protected function __construct() {
\WP_CLI::add_command( 'map-proxy install', [ new Commands\MapProxy(), 'install' ], [
'shortdesc' => 'Install map proxy directory',
'is_deferred' => false,
] );
\WP_CLI::add_command( 'map-proxy uninstall', [ new Commands\MapProxy(), 'uninstall' ], [
'shortdesc' => 'remove map proxy directory',
'is_deferred' => false,
] );
\WP_CLI::add_command( 'map-proxy configure', [ new Commands\MapProxy(), 'configure' ], [
'shortdesc' => 'Save local map proxy configuration',
'is_deferred' => false,
] );
}
}
4 changes: 3 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
Core\Core::instance( __FILE__ );

if ( is_admin() || defined( 'DOING_AJAX' ) ) {

Settings\SettingsOpenStreetMap::instance();
}

if ( defined( 'WP_CLI' ) && WP_CLI ) {
WPCLI\WPCLI::instance();
}

0 comments on commit 196b160

Please sign in to comment.