fix additional URLs

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

View file

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

View file

@ -28,10 +28,21 @@ class StaticHtmlOutput_UrlRequest
*/
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;
$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;
}
}
/**
@ -43,6 +54,11 @@ class StaticHtmlOutput_UrlRequest
return $this->_url;
}
public function checkResponse()
{
return $this->_response;
}
/**
* Allows to override the HTTP response body
* @param string $newBody

View file

@ -213,7 +213,11 @@ jQuery(document).ready(function($){
<p>
<strong><label for="additionalUrls"><?= __('Additional Urls', 'static-html-output-plugin');?></label></strong>
<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>
<textarea class="widefat" name="additionalUrls" id="additionalUrls" rows="5" cols="10"><?php echo esc_html($this->additionalUrls) ?></textarea>
</p>