Skip to content

Commit

Permalink
closes #55 drop offline zip functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
leonstafford committed May 21, 2020
1 parent 0d2e36b commit c88044f
Show file tree
Hide file tree
Showing 11 changed files with 5 additions and 194 deletions.
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ Everyone's WordPress hosting environment and configuration is unique, with diffe
* Matching project name to wp.org slug
* Implement code quality tooling
* Performance optimization for slow hosting providers
* rm offline zip functionality
* Minor fix for setting base HREF option
* use WP's mime-types lookup for S3 deployer

Expand Down
8 changes: 1 addition & 7 deletions src/CSSProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class CSSProcessor extends StaticHTMLOutput {
* @var string
*/
public $destination_protocol_relative_url;
/**
* @var bool
*/
public $allow_offline_usage;
/**
* @var bool
*/
Expand Down Expand Up @@ -89,7 +85,6 @@ class CSSProcessor extends StaticHTMLOutput {
public $processed_urls;

public function __construct(
bool $allow_offline_usage = false,
bool $remove_conditional_head_comments = false,
bool $remove_html_comments = false,
bool $remove_wp_links = false,
Expand All @@ -102,7 +97,6 @@ public function __construct(
string $wp_site_url,
string $wp_uploads_path
) {
$this->allow_offline_usage = $allow_offline_usage;
$this->remove_conditional_head_comments = $remove_conditional_head_comments;
$this->remove_html_comments = $remove_html_comments;
$this->remove_wp_links = $remove_wp_links;
Expand Down Expand Up @@ -248,7 +242,7 @@ public function isInternalLink( string $link, string $domain = '' ) : bool {
* We don't use the CSSParser's output, as it
* mangles the output too much. We simply do placeholder URL
* rewrites to destination URL. This may need to be improved
* for URL format transformations (relative/offline URLs), at
* for URL format transformations (relative URLs), at
* which point, we may store a list of each file's URLs and how
* they need to be transformed, then do that on the raw CSS here.
*
Expand Down
5 changes: 0 additions & 5 deletions src/DBSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static function get( array $sets = [] ) {

$key_sets['processing'] = [
'removeConditionalHeadComments',
'allowOfflineUsage',
'baseHREF',
'rewrite_rules',
'rename_rules',
Expand Down Expand Up @@ -61,7 +60,6 @@ public static function get( array $sets = [] ) {

$key_sets['zip'] = [
'baseUrl-zip',
'allowOfflineUsage',
];

$key_sets['github'] = [
Expand Down Expand Up @@ -161,9 +159,6 @@ public static function get( array $sets = [] ) {
(int) $plugin->options->crawl_increment :
10;

// NOTE: any baseUrl required if creating an offline ZIP
$settings['baseUrl'] = rtrim( $plugin->options->baseUrl, '/' ) . '/';

return array_filter( $settings );
}
}
Expand Down
125 changes: 1 addition & 124 deletions src/HTMLProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@

class HTMLProcessor extends StaticHTMLOutput {

/**
* @var bool
*/
public $allow_offline_usage;
/**
* @var bool
*/
Expand Down Expand Up @@ -100,7 +96,6 @@ class HTMLProcessor extends StaticHTMLOutput {
public $processed_urls;

public function __construct(
bool $allow_offline_usage = false,
bool $remove_conditional_head_comments = false,
bool $remove_html_comments = false,
bool $remove_wp_links = false,
Expand All @@ -113,7 +108,6 @@ public function __construct(
string $wp_site_url,
string $wp_uploads_path
) {
$this->allow_offline_usage = $allow_offline_usage;
$this->remove_conditional_head_comments = $remove_conditional_head_comments;
$this->remove_html_comments = $remove_html_comments;
$this->remove_wp_links = $remove_wp_links;
Expand Down Expand Up @@ -281,7 +275,6 @@ public function processLink( DOMElement $element ) : void {
$this->rewriteWPPaths( $element );
$this->rewriteBaseURL( $element );
$this->convertToRelativeURL( $element );
$this->convertToOfflineURL( $element );

if ( $this->remove_wp_links ) {
$relative_links_to_rm = [
Expand Down Expand Up @@ -405,7 +398,6 @@ public function processImageSrcSet( DOMElement $element ) : void {
$url = $this->rewriteWPPathsSrcSetURL( (string) $url );
$url = $this->rewriteBaseURLSrcSetURL( $url );
$url = $this->convertToRelativeURLSrcSetURL( $url );
$url = $this->convertToOfflineURLSrcSetURL( $url );
}

$new_src_set[] = "{$url} {$dimension}";
Expand All @@ -421,7 +413,6 @@ public function processImage( DOMElement $element ) : void {
$this->rewriteWPPaths( $element );
$this->rewriteBaseURL( $element );
$this->convertToRelativeURL( $element );
$this->convertToOfflineURL( $element );
}

public function stripHTMLComments() : void {
Expand Down Expand Up @@ -479,7 +470,6 @@ public function processScript( DOMElement $element ) : void {
$this->rewriteWPPaths( $element );
$this->rewriteBaseURL( $element );
$this->convertToRelativeURL( $element );
$this->convertToOfflineURL( $element );
}

public function processAnchor( DOMElement $element ) : void {
Expand Down Expand Up @@ -507,7 +497,6 @@ public function processAnchor( DOMElement $element ) : void {
$this->rewriteWPPaths( $element );
$this->rewriteBaseURL( $element );
$this->convertToRelativeURL( $element );
$this->convertToOfflineURL( $element );
}

public function processMeta( DOMElement $element ) : void {
Expand Down Expand Up @@ -549,7 +538,6 @@ public function processMeta( DOMElement $element ) : void {
$this->rewriteWPPaths( $element );
$this->rewriteBaseURL( $element );
$this->convertToRelativeURL( $element );
$this->convertToOfflineURL( $element );
}

public function writeDiscoveredURLs() : void {
Expand Down Expand Up @@ -924,91 +912,6 @@ public function convertToRelativeURL( DOMElement $element ) : void {
}
}

public function convertToOfflineURLSrcSetURL( string $url_to_change ) : string {
if ( ! $this->shouldCreateOfflineURLs() ) {
return $url_to_change;
}

$current_page_path_to_root = '';
$current_page_path = parse_url( $this->page_url, PHP_URL_PATH );
$number_of_segments_in_path = explode( '/', (string) $current_page_path );
$num_dots_to_root = count( $number_of_segments_in_path ) - 2;

for ( $i = 0; $i < $num_dots_to_root; $i++ ) {
$current_page_path_to_root .= '../';
}

if ( ! $this->isInternalLink(
$url_to_change
) ) {
return $url_to_change;
}

$rewritten_url = str_replace(
$this->placeholder_url,
'',
$url_to_change
);

$offline_url = $current_page_path_to_root . $rewritten_url;

// add index.html if no extension
if ( substr( $offline_url, -1 ) === '/' ) {
// TODO: check XML/RSS case
$offline_url .= 'index.html';
}

return $offline_url;
}

public function convertToOfflineURL( DOMElement $element ) : void {
if ( ! $this->shouldCreateOfflineURLs() ) {
return;
}

if ( $element->hasAttribute( 'href' ) ) {
$attribute_to_change = 'href';
} elseif ( $element->hasAttribute( 'src' ) ) {
$attribute_to_change = 'src';
} elseif ( $element->hasAttribute( 'content' ) ) {
$attribute_to_change = 'content';
} else {
return;
}

$url_to_change = $element->getAttribute( $attribute_to_change );
$current_page_path_to_root = '';
$current_page_path = parse_url( $this->page_url, PHP_URL_PATH );
$number_of_segments_in_path = explode( '/', (string) $current_page_path );
$num_dots_to_root = count( $number_of_segments_in_path ) - 2;

for ( $i = 0; $i < $num_dots_to_root; $i++ ) {
$current_page_path_to_root .= '../';
}

if ( ! $this->isInternalLink(
$url_to_change
) ) {
return;
}

$rewritten_url = str_replace(
$this->placeholder_url,
'',
$url_to_change
);

$offline_url = $current_page_path_to_root . $rewritten_url;

// add index.html if no extension
if ( substr( $offline_url, -1 ) === '/' ) {
// TODO: check XML/RSS case
$offline_url .= 'index.html';
}

$element->setAttribute( $attribute_to_change, $offline_url );
}

// TODO: move some of these URLs into settings to avoid extra calls
public function getProtocolRelativeURL( string $url ) : string {
$this->destination_protocol_relative_url = str_replace(
Expand Down Expand Up @@ -1118,40 +1021,14 @@ public function rewriteSiteURLsToPlaceholder(
}

public function shouldUseRelativeURLs() : bool {
if ( ! $this->use_relative_urls ) {
return false;
}

// NOTE: relative URLs should not be used when creating an offline ZIP
if ( $this->allow_offline_usage ) {
return false;
}

return true;
return $this->use_relative_urls;
}

public function shouldCreateBaseHREF() : bool {
if ( empty( $this->base_href ) ) {
return false;
}

// NOTE: base HREF should not be set when creating an offline ZIP
if ( $this->allow_offline_usage ) {
return false;
}

return true;
}

public function shouldCreateOfflineURLs() : bool {
if ( ! $this->allow_offline_usage ) {
return false;
}

if ( $this->selected_deployment_option != 'zip' ) {
return false;
}

return true;
}

Expand Down
2 changes: 0 additions & 2 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Options {
*/
protected $statichtmloutput_options_keys = [
'additionalUrls',
'allowOfflineUsage',
'baseHREF',
'baseUrl',
'baseUrl-bitbucket',
Expand Down Expand Up @@ -99,7 +98,6 @@ class Options {
*/
protected $whitelisted_keys = [
'additionalUrls',
'allowOfflineUsage',
'baseHREF',
'baseUrl',
'baseUrl-bitbucket',
Expand Down
9 changes: 0 additions & 9 deletions src/PostSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static function get( array $sets = [] ) : array {

$key_sets['processing'] = [
'removeConditionalHeadComments',
'allowOfflineUsage',
'baseHREF',
'rewrite_rules',
'rename_rules',
Expand Down Expand Up @@ -59,7 +58,6 @@ public static function get( array $sets = [] ) : array {

$key_sets['zip'] = [
'baseUrl-zip',
'allowOfflineUsage',
];

$key_sets['github'] = [
Expand Down Expand Up @@ -164,13 +162,6 @@ public static function get( array $sets = [] ) : array {
(int) $_POST['crawl_increment'] :
1;

// any baseUrl required if creating an offline ZIP
$settings['baseUrl'] =
isset( $_POST['baseUrl'] ) ?
rtrim( $_POST['baseUrl'], '/' ) . '/' :
'http://OFFLINEZIP.wpsho';
// @codingStandardsIgnoreEnd

return array_filter( $settings );
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/SiteCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,6 @@ public function loadFileForProcessing() : bool {

// prepare bool settings
$bool_settings = [
'allowOfflineUsage',
'removeConditionalHeadComments',
'removeHTMLComments',
'removeWPLinks',
Expand All @@ -512,7 +511,6 @@ public function loadFileForProcessing() : bool {
}

$processor = new HTMLProcessor(
$this->settings['allowOfflineUsage'],
$this->settings['removeConditionalHeadComments'],
$this->settings['removeHTMLComments'],
$this->settings['removeWPLinks'],
Expand Down Expand Up @@ -557,7 +555,6 @@ public function loadFileForProcessing() : bool {

// prepare bool settings
$bool_settings = [
'allowOfflineUsage',
'removeConditionalHeadComments',
'removeHTMLComments',
'removeWPLinks',
Expand All @@ -571,7 +568,6 @@ public function loadFileForProcessing() : bool {
}
}
$processor = new CSSProcessor(
$this->settings['allowOfflineUsage'],
$this->settings['removeConditionalHeadComments'],
$this->settings['removeHTMLComments'],
$this->settings['removeWPLinks'],
Expand Down
1 change: 0 additions & 1 deletion tests/CSSProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function testParsingStylesheets( string $raw_css, string $parsed_css ) {
*/
$css_processor = new CSSProcessor(
false, // $allow_offline_usage = false
false, // $remove_conditional_head_comments = false
false, // $remove_html_comments = false
false, // $remove_wp_links = false
Expand Down
Loading

0 comments on commit c88044f

Please sign in to comment.