Skip to content

Commit

Permalink
* Fix for blank settings pages that would appear on some hosting prov…
Browse files Browse the repository at this point in the history
…iders.

* Fixed bugs when Media Cloud is being used on a C-Panel/WHM managed servers.
* Fixed background processing when "Skip DNS" is enabled on C-Panel/WHM managed servers.
* Troubleshooter tool has been renamed System Compatibility Test.
* Running the system compatibility test will automatically tweak background processing settings until it finds a configuration that works.
* Ability to sort the media to be imported when using the Migrate to Cloud tool
* Fix for some hosts that have `allow_url_fopen` disabled
* Added 'Unlink From Cloud' bulk action that will remove Media Cloud metadata from selected files in the Media Library list view
* Fix for compatibility with Offload Media where the url contained an errant '-'
  • Loading branch information
jawngee committed Jul 29, 2019
1 parent ac5d2b8 commit 397b22d
Show file tree
Hide file tree
Showing 31 changed files with 1,281 additions and 484 deletions.
114 changes: 69 additions & 45 deletions classes/Tasks/BatchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,26 @@ public function currentFile($batch) {
return get_option("ilab_media_tools_{$batch}_file");
}

/**
* Sets the current file being processed
* @param $batch
* @param string|null $file
*/
public function setCurrentFile($batch, $file) {
update_option("ilab_media_tools_{$batch}_last_update", microtime(true));
update_option("ilab_media_tools_{$batch}_file", $file);
}
/**
* Sets the current file being processed
* @param $batch
* @param string|null $file
*/
public function setCurrentFile($batch, $file) {
$this->setLastUpdateToNow($batch);
update_option("ilab_media_tools_{$batch}_file", $file);
}

/**

/**
* Sets the time the last task update occurred
* @param $batch
*/
public function setLastUpdateToNow($batch) {
update_option("ilab_media_tools_{$batch}_last_update", microtime(true));
}

/**
* Returns the count of items in this batch being processed
* @param $batch
* @return int
Expand Down Expand Up @@ -457,7 +466,7 @@ public function addToBatchAndRun($batch, $postIDs, $options = null) {
}

Logger::info("Testing connectivity.");
$testResult = $this->testConnectivity(null, true);
$testResult = $this->testConnectivity(null, 3);
Logger::info("Finished Testing connectivity.");
if ($testResult !== true) {
$error = 'Unknown';
Expand Down Expand Up @@ -532,25 +541,38 @@ public function dispatchBatchesIfNeeded() {
*
* @return bool|\Exception|ResponseInterface
*/
public static function postRequest($url, $args) {
public static function postRequest($url, $args, $timeoutOverride = false) {
$verifySSL = Environment::Option('mcloud-storage-batch-verify-ssl', null, 'default');
$connectTimeout = Environment::Option('mcloud-storage-batch-connect-timeout', null, 0);
$timeout = Environment::Option('mcloud-storage-batch-timeout', null, 0.1);
$skipDNS = Environment::Option('mcloud-storage-batch-skip-dns', null, false);

if ($skipDNS) {
$ip = getHostByName(getHostName());
if (empty($ip)) {
$ip = $_SERVER['SERVER_ADDR'];
}

if (empty($ip)) {
$ip = '127.0.0.1';
}

$host = parse_url($url, PHP_URL_HOST);
$url = str_replace($host, '127.0.0.1', $url);
$url = str_replace($host, $ip, $url);

$headers = [
'Host' => $host
'Host' => $host
];

if (isset($args['headers'])) {
$args['headers'] = array_merge($args['headers'], $headers);
$args['headers'] = array_merge($args['headers'], $headers);
} else {
$args['headers'] = $headers;
$args['headers'] = $headers;
}

$cookies = CookieJar::fromArray($args['cookies'], $ip);
} else {
$cookies = CookieJar::fromArray($args['cookies'], $_SERVER['HTTP_HOST']);
}

if ($verifySSL == 'default') {
Expand All @@ -561,64 +583,66 @@ public static function postRequest($url, $args) {

$rawUrl = esc_url_raw( $url );

$cookies = CookieJar::fromArray($args['cookies'], $_SERVER['HTTP_HOST']);
$options = [
'synchronous' => !empty($args['blocking']),
'cookies' => $cookies,
'verify' => $verifySSL
];

if (isset($args['body'])) {
if (is_array($args['body'])) {
$options['form_params'] = $args['body'];
if (is_array($args['body'])) {
$options['form_params'] = $args['body'];
} else {
$options['body'] = $args['body'];
}
}

if (!empty($headers)) {
$options['headers'] = $headers;
$options['headers'] = $headers;
}

if (!empty($connectTimeout)) {
$options['connect_timeout'] = $connectTimeout;
}

if (!empty($timeout)) {
$options['timeout'] = $connectTimeout;

if ($timeoutOverride !== false) {
$options['timeout'] = $timeoutOverride;
} else {
$options['timeout'] = max(0.01, $timeout);
}

$client = new Client();
try {
if (empty($options['synchronous'])) {
Logger::info("Async call to $rawUrl");
$options['synchronous'] = true;
$client->postSync($rawUrl, $options);
Logger::info("Async call to $rawUrl complete.");
return true;
} else {
Logger::info("Synchronous call to $rawUrl");
$result = $client->post($rawUrl, $options);
Logger::info("Synchronous call to $rawUrl complete.");
return $result;
}
$client = new Client();
try {
if (empty($options['synchronous'])) {
Logger::info("Async call to $rawUrl");
$options['synchronous'] = true;
$client->postSync($rawUrl, $options);
Logger::info("Async call to $rawUrl complete.");
return true;
} else {
Logger::info("Synchronous call to $rawUrl");
$result = $client->post($rawUrl, $options);
Logger::info("Synchronous call to $rawUrl complete.");
return $result;
}
} catch (\Exception $ex) {
if (!empty($args['blocking'])) {
return $ex;
} else {
Logger::info("Async exception: ".$ex->getMessage());
}
if (!empty($args['blocking'])) {
return $ex;
} else {
Logger::info("Async exception: ".$ex->getMessage());
}
}

return true;
return true;
}

/**
* Tests connectivity to for the bulk importer
* @param ErrorCollector|null $errorCollector
* @return bool|ResponseInterface|\Exception
* @return bool|ResponseInterface|\Exception|\WP_Error
*/
public function testConnectivity($errorCollector = null) {
public function testConnectivity($errorCollector = null, $timeoutOverride = false) {
$url = add_query_arg(['action' => 'ilab_batch_test', 'nonce' => wp_create_nonce('ilab_batch_test')], admin_url('admin-ajax.php'));
$args = [
'blocking' => true,
Expand All @@ -627,7 +651,7 @@ public function testConnectivity($errorCollector = null) {
];

/** @var bool|ResponseInterface $result */
$result = static::postRequest($url, $args);
$result = static::postRequest($url, $args, $timeoutOverride);

if ($result === true) {
return true;
Expand Down
96 changes: 96 additions & 0 deletions classes/Tools/Debugging/System/Batch/TestBatchProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

// Copyright (c) 2016 Interfacelab LLC. All rights reserved.
//
// Released under the GPLv3 license
// http://www.gnu.org/licenses/gpl-3.0.html
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// **********************************************************************

namespace ILAB\MediaCloud\Tools\Debugging\System\Batch;

use ILAB\MediaCloud\Tasks\BackgroundProcess;
use ILAB\MediaCloud\Tasks\BatchManager;
use ILAB\MediaCloud\Tools\Storage\StorageTool;
use ILAB\MediaCloud\Tools\ToolsManager;
use ILAB\MediaCloud\Utilities\Logging\Logger;

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

/**
* Class TestBatchProcess
* @package ILAB\MediaCloud\Tools\Debugging\System\Batch
*/
class TestBatchProcess extends BackgroundProcess {
protected $action = 'mcloud_system_test_process';

protected function shouldHandle() {
return !BatchManager::instance()->shouldCancel('system-testing');
}

public function task($item) {
$startTime = microtime(true);

Logger::info( 'Start Task', $item);
if (!$this->shouldHandle()) {
Logger::info( 'Task cancelled', $item);
return false;
}

$index = $item['index'];
$post_id = $item['post'];

BatchManager::instance()->setLastUpdateToNow('system-testing');
BatchManager::instance()->setCurrentID('system-testing', $post_id);
BatchManager::instance()->setCurrent('system-testing', $index + 1);

sleep(1);

$endTime = microtime(true) - $startTime;
BatchManager::instance()->incrementTotalTime('system-testing', $endTime);

return false;
}

protected function complete() {
Logger::info( 'Task complete');
BatchManager::instance()->reset('system-testing');
parent::complete();
}

public function cancel_process() {
Logger::info( 'Cancel process');

parent::cancel_process();

BatchManager::instance()->reset('system-testing');
}

public static function cancelAll() {
Logger::info( 'Cancel all processes');

wp_clear_scheduled_hook('wp_mcloud_system_test_process_cron');

global $wpdb;

$res = $wpdb->get_results("select * from {$wpdb->options} where option_name like 'wp_mcloud_system_test_process_batch_%'");
foreach($res as $batch) {
Logger::info( "Deleting batch {$batch->option_name}");
delete_option($batch->option_name);
}

$res = $wpdb->get_results("select * from {$wpdb->options} where option_name like '__wp_mcloud_system_test_process_batch_%'");
foreach($res as $batch) {
delete_option($batch->option_name);
}

BatchManager::instance()->reset('system-testing');

Logger::info( "Current cron", get_option( 'cron', []));
Logger::info( 'End cancel all processes');
}
}
104 changes: 104 additions & 0 deletions classes/Tools/Debugging/System/Batch/TestBatchTool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

// Copyright (c) 2016 Interfacelab LLC. All rights reserved.
//
// Released under the GPLv3 license
// http://www.gnu.org/licenses/gpl-3.0.html
//
// **********************************************************************
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// **********************************************************************

namespace ILAB\MediaCloud\Tools\Debugging\System\Batch;

use ILAB\MediaCloud\Tools\BatchTool;
use function ILAB\MediaCloud\Utilities\json_response;

class TestBatchTool extends BatchTool {
//region Properties
/**
* Name/ID of the batch
* @return string
*/
public static function BatchIdentifier() {
return 'system-testing';
}

/**
* Title of the batch
* @return string
*/
public function title() {
return null;
}

public function menuTitle() {
return null;
}

/**
* The prefix to use for action names
* @return string
*/
public function batchPrefix() {
return 'mcloud_system_testing';
}

/**
* Fully qualified class name for the BatchProcess class
* @return string
*/
public static function BatchProcessClassName() {
return TestBatchProcess::class;
}

/**
* The view to render for instructions
* @return string
*/
public function instructionView() {
return null;
}

/**
* The menu slug for the tool
* @return string
*/
function menuSlug() {
return null;
}

public function enabled() {
return true;
}
//endregion

//region Actions
public function manualAction() {
json_response(["status" => 'ok']);
}
//endregion


/**
* Gets the post data to process for this batch. Data is paged to minimize memory usage.
* @param $page
* @param bool $forceImages
* @param bool $allInfo
*
* @return array
*/
protected function getImportBatch($page, $forceImages = false, $allInfo = false) {
return [
'total' => 15,
'posts' => [
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
],
'options' => []
];
}
}
Loading

0 comments on commit 397b22d

Please sign in to comment.