Skip to content

Commit

Permalink
* Fix Clean Uploads task to deal with crusty databases that have URLs…
Browse files Browse the repository at this point in the history
… in the _wp_attached_file meta field.

* Added `media-cloud/storage/verify/settings` filter to enable skipping cloud storage settings verification.  Only use this
  if you are using a strict IAM policy that prevents Media Cloud from verifying your settings.
* System compatibility test now takes upload prefix into account when testing cloud storage
* Deprecate Wasabi.  Wasabi is not a suitable cloud storage provider for Media Cloud's purpose.  If you are using Wasabi currently,
  nothing will change.  If you are not using Wasabi, you will not be able to add it as a cloud storage provider.
* Massive performance improvement for multisite.
* Some fixes for PHP 8.2.  Still don't recommend using 8.2 with WordPress.  8.1 and 8.0 work fine though.
  • Loading branch information
jawngee committed Sep 27, 2023
1 parent 4a6c588 commit b437125
Show file tree
Hide file tree
Showing 26 changed files with 125 additions and 36 deletions.
6 changes: 3 additions & 3 deletions classes/Tools/Debugging/System/SystemCompatibilityTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ private function testUploadClient() {
$errors = [];

try {
$url = $storageTool->client()->upload('_troubleshooter/sample.txt',ILAB_TOOLS_DIR.'/public/text/sample-upload.txt', StorageToolSettings::privacy());
$url = $storageTool->client()->upload(StorageToolSettings::prefix() . '_troubleshooter/sample.txt',ILAB_TOOLS_DIR.'/public/text/sample-upload.txt', StorageToolSettings::privacy());
Tracker::trackView("System Test - Test Uploads - Success", "/system-test/uploads/success");
} catch (\Exception $ex) {
Tracker::trackView("System Test - Test Uploads - Error", "/system-test/uploads/error");
Expand Down Expand Up @@ -519,7 +519,7 @@ private function testPubliclyAccessible() {

try {
$result = null;
$url = $storageTool->client()->url('_troubleshooter/sample.txt');
$url = $storageTool->client()->url(StorageToolSettings::prefix() . '_troubleshooter/sample.txt');

$result = ilab_file_get_contents($url);

Expand Down Expand Up @@ -567,7 +567,7 @@ private function testDeletingFiles() {
$errors = [];

try {
$storageTool->client()->delete('_troubleshooter/sample.txt');
$storageTool->client()->delete(StorageToolSettings::prefix() . '_troubleshooter/sample.txt');
Tracker::trackView("System Test - Deleting - Success", "/system-test/delete/success");
} catch (\Exception $ex) {
$errors[] = $ex->getMessage();
Expand Down
4 changes: 4 additions & 0 deletions classes/Tools/Storage/Driver/S3/S3Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ public function validateSettings($errorCollector = null) {
if($this->enabled()) {
$client = $this->getClient($errorCollector);

if (empty(apply_filters('media-cloud/storage/verify/settings', true))) {
return true;
}

if (!empty($client)) {
$connectError = false;

Expand Down
5 changes: 5 additions & 0 deletions classes/Tools/Storage/StorageToolSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace MediaCloud\Plugin\Tools\Storage;

use MediaCloud\Plugin\Tools\ToolSettings;
use MediaCloud\Plugin\Utilities\Logging\Logger;
use function MediaCloud\Plugin\Utilities\arrayPath;
use MediaCloud\Plugin\Utilities\Environment;
use MediaCloud\Plugin\Utilities\NoticeManager;
Expand Down Expand Up @@ -176,6 +177,10 @@ public function __construct() {
public function __get($name) {
if ($name === 'expires') {
if (($this->_expires === null) && !empty($this->expireMinutes)) {
if (gettype($this->expireMinutes) === 'string') {
$this->expireMinutes = intval($this->expireMinutes);
}

$this->_expires = gmdate('D, d M Y H:i:s \G\M\T', time() + ($this->expireMinutes * 60));
}

Expand Down
38 changes: 35 additions & 3 deletions classes/Tools/Storage/Tasks/CleanUploadsTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
namespace MediaCloud\Plugin\Tools\Storage\Tasks;

use MediaCloud\Plugin\Tasks\AttachmentTask;
use MediaCloud\Plugin\Tools\Storage\StorageTool;
use MediaCloud\Plugin\Tools\Storage\StorageToolSettings;
use MediaCloud\Plugin\Tools\ToolsManager;
use MediaCloud\Plugin\Utilities\Logging\Logger;
use function MediaCloud\Plugin\Utilities\arrayPath;
use function MediaCloud\Plugin\Utilities\postIdExists;
Expand Down Expand Up @@ -111,7 +114,7 @@ public static function taskOptions() {
}

public static function warnOption() {
return 'clean-uploads-task-warning-seen';
return 'clean-uploads-task-warning-seen-'.time();
}

public static function warnConfirmationAnswer() {
Expand Down Expand Up @@ -230,6 +233,11 @@ protected function cleanEmptyDirectories() {
* @throws \Exception
*/
public function performTask($item) {
/** @var StorageTool $storageTool */
$storageTool = ToolsManager::instance()->tools['storage'];

add_filter('media-cloud/storage/should-override-attached-file', '__return_false');

$post_id = $item['id'];

if ($post_id == -1) {
Expand All @@ -246,12 +254,36 @@ public function performTask($item) {
$this->updateCurrentPost($post_id);

$file = get_attached_file($post_id, true);
$meta = wp_get_attachment_metadata($post_id, true);
if (strpos($file, 'http://') !== false || strpos($file, 'https://') !== false) {
Logger::info("File is URL", [], __METHOD__, __LINE__);

$idx = strpos($file, 'https://');
if ($idx === false) {
$idx = strpos($file, 'http://');
}

$startPath = substr($file, 0, $idx);
$fileUrl = substr($file, $idx);

$baseDir = pathinfo($file, PATHINFO_DIRNAME);
$path = ltrim(parse_url($fileUrl, PHP_URL_PATH), '/');
$parts = explode('/', $path);
$possibleBucket = array_shift($parts);

if($possibleBucket === $storageTool->client()->bucket()) {
$file = $startPath . implode('/', $parts);
Logger::info("Parsed file is bucket, new file is $file", [], __METHOD__, __LINE__);
} else {
$file = $startPath . $path;
Logger::info("Parsed file is NOT current bucket, new file is $file", [], __METHOD__, __LINE__);
}
}

$filesToDelete = [$file];

$meta = wp_get_attachment_metadata($post_id, true);

$baseDir = pathinfo($file, PATHINFO_DIRNAME);

if (isset($meta['sizes'])) {
foreach($meta['sizes'] as $size => $sizeData) {
if (empty($sizeData['file'])) {
Expand Down
4 changes: 3 additions & 1 deletion classes/Tools/Tasks/TasksTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public function setup() {

add_action('init', function() {
$role = get_role('administrator');
$role->add_cap('mcloud_heartbeat', true);
if ($role) {
$role->add_cap('mcloud_heartbeat', true);
}
});

if (is_admin()) {
Expand Down
2 changes: 1 addition & 1 deletion classes/Tools/ToolsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public static function Boot()
return;
}
static::$booted = true;
Environment::Boot();
// Environment::Boot();

if ( !is_multisite() && ($media_cloud_licensing->is_plan__premium_only( 'multisite_basic', true ) || $media_cloud_licensing->is_plan__premium_only( 'multisite_pro', true ) || $media_cloud_licensing->is_plan__premium_only( 'multisite_unlimited', true )) ) {
add_action( 'admin_notices', function () {
Expand Down
2 changes: 1 addition & 1 deletion classes/Tools/Vision/VisionDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

if (!defined('ABSPATH')) { header('Location: /'); die; }

abstract class VisionDriver {
abstract class VisionDriver extends \stdClass {
/** @var null|VisionToolSettings */
protected $settings = null;

Expand Down
32 changes: 30 additions & 2 deletions classes/Utilities/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace MediaCloud\Plugin\Utilities;

use MediaCloud\Plugin\Utilities\Logging\Logger;

if (!defined( 'ABSPATH')) { header( 'Location: /'); die; }


Expand All @@ -23,6 +25,7 @@
final class Environment {
private static $booted = false;
private static $networkMode = false;
private static $siteOptions = [];

/**
* Sets up the environment
Expand All @@ -33,7 +36,20 @@ public static function Boot() {
if ($media_cloud_licensing->is_plan('pro')) {
static::$networkMode = get_site_option('mcloud-network-mode');
if(!static::$networkMode) {
static::$networkMode = static::Option('mcloud-network-mode', null, false);
static::$networkMode = static::Option('mcloud-network-mode', null, false, false);
}
}

global $wpdb;
$options = $wpdb->get_results($wpdb->prepare("select option_name, option_value from {$wpdb->options} where option_name like %s", 'mcloud%'));
foreach($options as $option) {
static::$siteOptions[$option->option_name] = maybe_unserialize($option->option_value);
}

if (is_multisite()) {
$options = $wpdb->get_results($wpdb->prepare("select meta_key, meta_value from {$wpdb->sitemeta} where meta_key like %s and (site_id = 1 OR site_id = %d) order by site_id asc", 'mcloud%', get_current_blog_id()));
foreach($options as $option) {
static::$siteOptions[$option->meta_key] = maybe_unserialize($option->meta_value);
}
}

Expand Down Expand Up @@ -76,7 +92,7 @@ public static function UpdateNetworkMode($enabled) {
*
* @return array|false|mixed|string|null
*/
public static function Option($optionName = null, $envVariableName = null, $default = false) {
public static function Option($optionName = null, $envVariableName = null, $default = false, $useCache = true) {
if (empty($optionName) && empty($envVariableName)) {
return $default;
}
Expand Down Expand Up @@ -111,12 +127,20 @@ public static function Option($optionName = null, $envVariableName = null, $defa
return $default;
}

if ($useCache) {
if (array_key_exists($optionName, static::$siteOptions)) {
return static::$siteOptions[$optionName];
}
}

if (static::$networkMode) {
$val = get_site_option($optionName, $default);
} else {
$val = get_option($optionName, $default);
}

static::$siteOptions[$optionName] = $val;

return $val;
}

Expand All @@ -132,6 +156,8 @@ public static function UpdateOption($optionName, $value) {
} else {
update_option($optionName, $value);
}

static::$siteOptions[$optionName] = $value;
}

/**
Expand All @@ -158,6 +184,8 @@ public static function DeleteOption($optionName) {
} else {
delete_option($optionName);
}

unset(static::$siteOptions[$optionName]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
],
"config": {
"platform": {
"php": "7.1"
"php": "7.4"
}
},
"require" : {
"php": ">=7.1"
"php": ">=7.4"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion config/storage.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
]
],
'wasabi' => [
'name' => 'Wasabi',
'name' => 'Wasabi (Deprecated)',
'class' => "\\MediaCloud\\Plugin\\Tools\\Storage\\Driver\\S3\\WasabiStorage",
'config' => '/storage/wasabi.config.php',
'help' => [
Expand Down
2 changes: 1 addition & 1 deletion config/wizard.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
->option('cloudflare', 'Cloudflare R2', 'wizard.cloud-storage.providers.cloudflare.description', 'wizard-icon-cloudflare.svg', 'cloud-storage-cloudflare')
->option('do', 'DigitalOcean Spaces', 'wizard.cloud-storage.providers.do.description', 'wizard-icon-do.svg', 'cloud-storage-do')
->option('dreamhost', 'DreamHost Cloud Storage', 'wizard.cloud-storage.providers.dreamhost.description', 'wizard-icon-dreamhost.svg', 'cloud-storage-dreamhost')
->option('wasabi', 'Wasabi', 'wizard.cloud-storage.providers.wasabi.description', 'wizard-icon-wasabi.png', 'cloud-storage-wasabi')
// ->option('wasabi', 'Wasabi', 'wizard.cloud-storage.providers.wasabi.description', 'wizard-icon-wasabi.png', 'cloud-storage-wasabi')
->option('backblaze-s3', 'Backblaze', 'wizard.cloud-storage.providers.backblaze.description', 'wizard-icon-backblaze.svg', 'cloud-storage-backblaze-s3')
->option('minio', 'Minio', 'wizard.cloud-storage.providers.minio.description', 'wizard-icon-minio.png', 'cloud-storage-minio')
->option('other-s3', 'S3 Compatible', 'wizard.cloud-storage.providers.other-s3.description', 'wizard-icon-other-s3.svg', 'cloud-storage-other-s3')
Expand Down
5 changes: 3 additions & 2 deletions ilab-media-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Plugin URI: https://github.com/interfacelab/ilab-media-tools
Description: Automatically upload media to Amazon S3 and integrate with Imgix, a real-time image processing CDN. Boosts site performance and simplifies workflows.
Author: interfacelab
Version: 4.5.21
Version: 4.5.24
Requires PHP: 7.4
Author URI: http://interfacelab.io
*/
Expand Down Expand Up @@ -114,7 +114,7 @@
}

// Version Defines
define( 'MEDIA_CLOUD_VERSION', '4.5.21' );
define( 'MEDIA_CLOUD_VERSION', '4.5.24' );
define( 'MEDIA_CLOUD_INFO_VERSION', '4.0.2' );
define( 'MCLOUD_IS_BETA', false );
// Debugging
Expand Down Expand Up @@ -222,6 +222,7 @@ function ( $user, $install ) {
}

add_action( 'plugins_loaded', function () {
\MediaCloud\Plugin\Utilities\Environment::Boot();
\MediaCloud\Plugin\Tools\ToolsManager::Boot();
} );
register_activation_hook( __FILE__, [ "\\MediaCloud\\Plugin\\Tools\\ToolsManager", 'activate' ] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static function defaultProvider(array $config = [])
$configProviders[] = self::fallback();

$memo = self::memoize(
call_user_func_array('self::chain', $configProviders)
call_user_func_array([self::class, 'chain'], $configProviders)
);

if (isset($config['csm']) && $config['csm'] instanceof CacheInterface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function defaultProvider(array $config = [])
$configProviders[] = self::fallback();

$memo = self::memoize(
call_user_func_array('self::chain', $configProviders)
call_user_func_array([self::class, 'chain'], $configProviders)
);

if (isset($config['defaultsMode'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static function defaultProvider(array $config = [])
$configProviders[] = self::fallback($region);

$memo = self::memoize(
call_user_func_array('self::chain', $configProviders)
call_user_func_array([self::class, 'chain'], $configProviders)
);

if (isset($config['use_dual_stack_endpoint'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function defaultProvider(array $config = [])
$configProviders[] = self::fallback($config['region']);

$memo = self::memoize(
call_user_func_array('self::chain', $configProviders)
call_user_func_array([self::class, 'chain'], $configProviders)
);

if (isset($config['use_fips_endpoint'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static function defaultProvider(array $config = [])
$configProviders[] = self::fallback($config);

$memo = self::memoize(
call_user_func_array('self::chain', $configProviders)
call_user_func_array([self::class, 'chain'], $configProviders)
);

if (isset($config['endpoint_discovery'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static function defaultProvider(array $config = [])
$configProviders[] = self::fallback();

$memo = self::memoize(
call_user_func_array('self::chain', $configProviders)
call_user_func_array([self::class, 'chain'], $configProviders)
);

if (isset($config['retries'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static function defaultProvider(array $config = [])
$configProviders[] = self::fallback();

$memo = self::memoize(
call_user_func_array('self::chain', $configProviders)
call_user_func_array([self::class, 'chain'], $configProviders)
);

if (isset($config['s3_us_east_1_regional_endpoint'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static function defaultProvider(array $config = [])
$configProviders[] = self::fallback();

$memo = self::memoize(
call_user_func_array('self::chain', $configProviders)
call_user_func_array([self::class, 'chain'], $configProviders)
);

if (isset($config['use_arn_region'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function defaultProvider(array $config = [])
$configProviders[] = self::fallback();

$memo = self::memoize(
call_user_func_array('self::chain', $configProviders)
call_user_func_array([self::class, 'chain'], $configProviders)
);

if (isset($config['sts_regional_endpoints'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @property resource $_mh Internal use only. Lazy loaded multi-handle.
*/
class CurlMultiHandler
class CurlMultiHandler extends \stdClass
{
/** @var CurlFactoryInterface */
private $factory;
Expand Down
Loading

0 comments on commit b437125

Please sign in to comment.