Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ ln -sf /My-Versions/cloudflareonion global-plugin-cfo
ln -s /My-Versions/cloudflareonion /app/web/wp-content/plugins/prcfx
```

### Commands

This plugin creates multiple commands to be used through WP CLI

```bash
# Purge by prefix
wp cfo purgeprefix '/en/prefix/'
# Purge all markets
wp cfo purgemarkets

# Inside the env if using lando !
lando wp cfo ####
```
2 changes: 1 addition & 1 deletion cfo-info.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
define('GLOBAL_CFO_VERSION', '0.0.10');
define('GLOBAL_CFO_VERSION', '0.0.11');
define('GLOBAL_CFO_NAME', 'global-cfo');
define('GLOBAL_CFO_NAMESPACE', 'GlobalCfo');
define('GLOBAL_CFO_PLUGIN_FOLDER', __DIR__);
Expand Down
2 changes: 1 addition & 1 deletion global-cfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Plugin Name: Global CFO
* Plugin URI: https://github.com/TotalOnion/cloudflareonion
* Description: Cloudflare cache handling plugin
* Version: 0.0.10
* Version: 0.0.11
* Author: Johann Biteghe
* Author URI: https://totalonion.com
* License: GPL-2.0+
Expand Down
45 changes: 45 additions & 0 deletions src/Controllers/Admin/CfoCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace GlobalCfo\Controllers\Admin;

use GlobalCfo\Controllers\Admin;
use GlobalCfo\Controllers\AbstractController;
use GlobalCfo\Controllers\Admin\Logger;
use GlobalCfo\Controllers\Admin\CfoManager;
use \WP_CLI as CLI;


class CfoCommand extends AbstractController
{
private Logger $logger;
private CfoManager $manager;

public function __construct($pluginName, $version, $cfoManager)
{
$this->logger = new Logger(GLOBAL_CFO_VERSION, GLOBAL_CFO_NAME);
$this->manager = $cfoManager;
parent::__construct($pluginName, $version);
}

public function purgemarkets()
{
CLI::log( sprintf( 'Purging marktets' ) );
$purge = $this->manager->purgeMarkets();
if ($purge) {
CLI::success( 'Request successfully sent with response : ' . $purge);
} else {
CLI::error( 'Wpml is not enabled on this site.' );
}
}

public function purgeprefix($args)
{
$prefix = $args[0];
CLI::log( 'Clearing prefix : ' . $prefix);
$purge = $this->manager->purgePrefix($prefix);
if ($purge) {
CLI::success( 'Request successfully sent with response : ' . $purge);
} else {
CLI::error( 'An error occured' );
}
}
}
44 changes: 36 additions & 8 deletions src/Controllers/Admin/CfoManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public function __construct($pluginName, $version)

public function registerSavedItem($url)
{
$url = $this->getDomainReplacedURL($url);

$this->sendPurgeRequest($this->getPurgeBodyUrl($url));

if ($this->getTrailingSlashOption()) {
Expand Down Expand Up @@ -46,20 +44,39 @@ public function registerSavedPost($postID)
public function purgeMarket($marketId)
{
$marketURL = cfoGetWPMLLanguageById($marketId)['url'];
if($marketURL) {
$marketURI = parse_url($marketURL, PHP_URL_HOST) . parse_url($marketURL, PHP_URL_PATH);
$body = json_encode([
'prefixes' => [$marketURI]
], JSON_UNESCAPED_SLASHES);
if ($marketURL) {
$body = $this->getPurgeBodyPrefix($marketURL);
return $this->sendPurgeRequest($body);
}
}

public function purgePrefix($prefix)
{
if ($prefix) {
$fullURL = home_url() . $prefix;
$body = $this->getPurgeBodyPrefix($fullURL);
return $this->sendPurgeRequest($body);
}
}

public function purgeMarkets()
{
if ( in_array( 'sitepress-multilingual-cms/sitepress.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
$languages = apply_filters( 'wpml_active_languages', NULL );
foreach ($languages as $language) {
$this->purgeMarket($language['id']);
}
return true;
} else {
return false;
}
}

public function getDomainReplacedURL($url): string
{
$newDomain = $this->getDomainReplace();
if ($newDomain) {
if($url){
if ($url){
$parsedURL = parse_url($url);
$url = $parsedURL['scheme']. '://' . $newDomain . $parsedURL['path'];
}
Expand Down Expand Up @@ -87,12 +104,23 @@ private function sendPurgeRequest($body): string

private function getPurgeBodyUrl($url): string
{
$url = $this->getDomainReplacedURL($url);
$body = json_encode([
'files' => [$url]
]);
return $body;
}

private function getPurgeBodyPrefix($url): string
{
$url = $this->getDomainReplacedURL($url);
$uri = parse_url($url, PHP_URL_HOST) . parse_url($url, PHP_URL_PATH);
$body = json_encode([
'prefixes' => [$uri]
], JSON_UNESCAPED_SLASHES);
return $body;
}

private function getAPIKey(): string
{
return cfoDecryptInput(get_option(GLOBAL_CFO_NAME.'_tokenCF'));
Expand Down
6 changes: 6 additions & 0 deletions src/GlobalCfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ private function defineAdminHooks()
$API = new Admin\CfoAPI($this->getPluginName(), $this->getVersion(), $cfoManager);
$this->loader->addAction('rest_api_init', $API, 'registerEndpoints');

// Add the commands
if ( defined( 'WP_CLI' ) && WP_CLI ) {
$command = new Admin\CfoCommand($this->getPluginName(), $this->getVersion(), $cfoManager);
\WP_CLI::add_command( 'cfo', $command );
}

// Enqueue scripts
$enqueue = new Frontend\Enqueue($this->getPluginName(), $this->getVersion());
$this->loader->addAction('admin_enqueue_scripts', $enqueue, 'enqueueScripts');
Expand Down