fix additional URLs

This commit is contained in:
Leon 2017-03-16 21:41:25 +13:00
parent b3402c08cf
commit 764f12323d
3 changed files with 48 additions and 24 deletions

View file

@ -141,7 +141,6 @@ class StaticHtmlOutput {
public function saveOptions() { public function saveOptions() {
if (!isset($_POST['action']) || 'generate' != $_POST['action']) { if (!isset($_POST['action']) || 'generate' != $_POST['action']) {
error_log('didnt detect the generate action');
return; return;
} }
@ -154,8 +153,6 @@ class StaticHtmlOutput {
->setOption('static-export-settings', filter_input(INPUT_POST, 'staticExportSettings', FILTER_SANITIZE_URL)) ->setOption('static-export-settings', filter_input(INPUT_POST, 'staticExportSettings', FILTER_SANITIZE_URL))
->save(); ->save();
error_log('saving options!!!');
$message = 'Options have been updated successfully.'; $message = 'Options have been updated successfully.';
$this->_view->setTemplate('message') $this->_view->setTemplate('message')
@ -205,23 +202,26 @@ class StaticHtmlOutput {
$urlsQueue = array_unique(array_merge( $urlsQueue = array_unique(array_merge(
array(trailingslashit($baseUrl)), array(trailingslashit($baseUrl)),
$this->_getListOfLocalFilesByUrl(array(get_template_directory_uri())), $this->_getListOfLocalFilesByUrl(array(get_template_directory_uri())),
$this->_getListOfLocalFilesByUrl(explode("\n", filter_input(INPUT_POST, 'additionalUrls'))) explode("\n", filter_input(INPUT_POST, 'additionalUrls'))
)); ));
$this->_exportLog = array(); $this->_exportLog = array();
while (count($urlsQueue)) while (count($urlsQueue))
{ {
$currentUrl = array_shift($urlsQueue); $currentUrl = array_shift($urlsQueue);
//echo "Processing ". $currentUrl."<br />";
$urlResponse = new StaticHtmlOutput_UrlRequest($currentUrl, filter_input(INPUT_POST, 'cleanMeta')); $urlResponse = new StaticHtmlOutput_UrlRequest($currentUrl, filter_input(INPUT_POST, 'cleanMeta'));
$urlResponse->cleanup();
if ($urlResponse->checkResponse() == 'FAIL') {
// Add current url to the list of processed urls // that sucks, should add to error log...
$this->_exportLog[$currentUrl] = true; } else {
// Add current url to the list of processed urls
$this->_exportLog[$currentUrl] = true;
}
$urlResponse->cleanup();
foreach ($urlResponse->extractAllUrls($baseUrl) as $newUrl) { foreach ($urlResponse->extractAllUrls($baseUrl) as $newUrl) {
if (!isset($this->_exportLog[$newUrl]) && $newUrl != $currentUrl && !in_array($newUrl,$urlsQueue)) { if (!isset($this->_exportLog[$newUrl]) && $newUrl != $currentUrl && !in_array($newUrl,$urlsQueue)) {
//echo "Adding ".$newUrl." to the list<br />"; //echo "Adding ".$newUrl." to the list<br />";
@ -291,9 +291,6 @@ class StaticHtmlOutput {
// Where the files will be transferred to // Where the files will be transferred to
$dest = 's3://' . filter_input(INPUT_POST, 's3Bucket'); $dest = 's3://' . filter_input(INPUT_POST, 's3Bucket');
error_log($source);
error_log($dest);
// Create a transfer object. // Create a transfer object.
$manager = new \Aws\S3\Transfer($s3Client, $source, $dest); $manager = new \Aws\S3\Transfer($s3Client, $source, $dest);
@ -367,13 +364,15 @@ class StaticHtmlOutput {
protected function _getListOfLocalFilesByUrl(array $urls) protected function _getListOfLocalFilesByUrl(array $urls)
{ {
$files = array(); $files = array();
foreach ($urls as $url) { foreach ($urls as $url) {
$directory = str_replace(home_url('/'), ABSPATH, $url); $directory = str_replace(home_url('/'), ABSPATH, $url);
// checking if url contains the WP site url at first position and is a directory
if (stripos($url, home_url('/')) === 0 && is_dir($directory)) { if (stripos($url, home_url('/')) === 0 && is_dir($directory)) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
foreach ($iterator as $fileName => $fileObject) { foreach ($iterator as $fileName => $fileObject) {
if (is_file($fileName)) { if (is_file($fileName)) {
$pathinfo = pathinfo($fileName); $pathinfo = pathinfo($fileName);
if (isset($pathinfo['extension']) && !in_array($pathinfo['extension'], array('php', 'phtml', 'tpl'))) { if (isset($pathinfo['extension']) && !in_array($pathinfo['extension'], array('php', 'phtml', 'tpl'))) {
@ -381,9 +380,14 @@ class StaticHtmlOutput {
} }
} }
} }
} } else {
// if is not empty, add full path to $files list
if ($url != '') {
array_push($files, $url);
}
}
} }
return $files; return $files;
} }

View file

@ -28,10 +28,21 @@ class StaticHtmlOutput_UrlRequest
*/ */
public function __construct($url, $cleanMeta = false) public function __construct($url, $cleanMeta = false)
{ {
$this->_url = filter_var($url, FILTER_VALIDATE_URL); $this->_url = filter_var(trim($url), FILTER_VALIDATE_URL);
$this->_cleanMeta = $cleanMeta; $this->_cleanMeta = $cleanMeta;
$response = wp_remote_get($this->_url,array('timeout'=>300)); //set a long time out $response = wp_remote_get($this->_url,array('timeout'=>300)); //set a long time out
$this->_response = (is_wp_error($response) ? '' : $response);
$this->_response = '';
if (is_wp_error($response)) {
error_log('WP_ERROR');
error_log(print_r($response, true));
$this->_response = 'FAIL';
} else {
$this->_response = $response;
}
} }
/** /**
@ -42,6 +53,11 @@ class StaticHtmlOutput_UrlRequest
{ {
return $this->_url; return $this->_url;
} }
public function checkResponse()
{
return $this->_response;
}
/** /**
* Allows to override the HTTP response body * Allows to override the HTTP response body

View file

@ -213,7 +213,11 @@ jQuery(document).ready(function($){
<p> <p>
<strong><label for="additionalUrls"><?= __('Additional Urls', 'static-html-output-plugin');?></label></strong> <strong><label for="additionalUrls"><?= __('Additional Urls', 'static-html-output-plugin');?></label></strong>
<p> <p>
<i>For a few themes, certain files may not be detected as part of the export process. Enter any such files here, one per line. </i> <i>For a few themes, certain files may not be detected as part of the export process. Enter any such files here, one per line.
Enter them as the full URL, including your WordPress site url, ie:</i>
<pre>http://localhost/wp-content/themes/twentyseventeen/banana.jpg
http://172.17.0.3/my_pricelist.pdf</pre>
</p> </p>
<textarea class="widefat" name="additionalUrls" id="additionalUrls" rows="5" cols="10"><?php echo esc_html($this->additionalUrls) ?></textarea> <textarea class="widefat" name="additionalUrls" id="additionalUrls" rows="5" cols="10"><?php echo esc_html($this->additionalUrls) ?></textarea>
</p> </p>