Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bedrock support: Use home URLs instead of site URL or paths #840

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
- [#837](https://github.com/leonstafford/wp2static/pull/837): Require PHP 7.4 or later; bump dependencies @leonstafford
- [#811](https://github.com/leonstafford/wp2static/issues/811): Optimize FilesHelper::getListOfLocalFilesByDir @bookwyrm
- [#805](https://github.com/leonstafford/wp2static/issues/805): Fix warning on cache page when there are no deployment namespaces @john-shaffer
- [#840](https://github.com/leonstafford/wp2static/pull/840): Add preliminary support for [Bedrock](https://roots.io/bedrock/) by using home URL instead of site URL in several places. This is a potentially breaking change for anyone with `WP_HOME` differing from `WP_SITEURL`. @oriolarcas, @john-shaffer
- Use `wp2static_set_wordpress_home_url` filter in post-processing instead of `wp2static_set_wordpress_site_url`.
- Add integration tests for Bedrock.

## WP2Static 7.1.7 (2021-09-04)

Expand Down
27 changes: 23 additions & 4 deletions integration-tests/clj/wp2static_test/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,18 @@
(str "bedrock.sh exited with code " code)
{:code code}))))
bedrock)
:name "Bedrock Initializer"
:name "Bedrock"
:join? true
:open-f (fn [_] (popen ["bash" "bedrock.sh"]))}))
:open-f (fn [_] (popen ["bash" "bedrock.sh"]))

:features {:sitemaps? false}
:paths {:cli "bedrock/web/wp"
:doc-root "bedrock/web"
:plugins "bedrock/web/app/plugins"
:site "/wp/"
:uploads "bedrock/web/app/uploads"
:wp-content "bedrock/web/wp/wp-content"}
:urls {:home "http://localhost:7001"}}))

(defn wordpress []
(shell-process
Expand All @@ -106,9 +115,18 @@
(str "wordpress.sh exited with code " code)
{:code code}))))
wordpress)
:name "WordPress Initializer"
:name "WordPress"
:join? true
:open-f (fn [_] (popen ["bash" "wordpress.sh"]))}))
:open-f (fn [_] (popen ["bash" "wordpress.sh"]))

:features {:sitemaps? true}
:paths {:cli "wordpress"
:doc-root "wordpress"
:plugins "wordpress/wp-content/plugins"
:site "/"
:uploads "wordpress/wp-content/uploads"
:wp-content "wordpress/wp-content"}
:urls {:home "http://localhost:7000"}}))

(defn system-map []
(component/system-map
Expand All @@ -117,6 +135,7 @@
:nginx (component/using (nginx) [:wordpress])
:php-fpm (component/using (php-fpm) [:mariadb])
:wordpress (component/using (wordpress) [:mariadb :php-fpm])
:wordpresses (component/using {} [:bedrock :wordpress])
:wp2static (component/using (wp2static) [:bedrock :wordpress])))

(defonce system (atom nil))
Expand Down
7 changes: 6 additions & 1 deletion integration-tests/clj/wp2static_test/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
result))

(defn wp-cli! [opts & args]
(apply sh! opts "wp" (concat args ["--path=wordpress"])))
(apply sh! opts "wp" (concat args [(str "--path=" (:path opts "wordpress"))])))

(defn test [opts]
;; https://clojureverse.org/t/why-doesnt-my-program-exit/3754/8
Expand All @@ -42,3 +42,8 @@
`(let [~name (main/start!)]
(core/clean-wp2static-cache!)
~@body))

(defmacro testing [[wp string] & body]
`(clojure.test/testing (str "[" (:name ~wp) "] " ~string)
~@body))

19 changes: 12 additions & 7 deletions integration-tests/test/wp2static_test/crawl_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
[clojure.test :refer :all]
[wp2static-test.test :as test]))

(defn get-crawled-file [path]
(slurp (str "wordpress/wp-content/uploads/wp2static-crawled-site/" path)))
(defn get-crawled-file [wp path]
(slurp (str (get-in wp [:paths :uploads]) "/wp2static-crawled-site/" path)))

(deftest test-crawled-site
(test/with-test-system [_]
(test/wp-cli! {} "wp2static" "detect")
(test/wp-cli! {} "wp2static" "crawl")
(is (str/includes? (get-crawled-file "index.html") "Welcome to WordPress"))
(is (str/includes? (get-crawled-file "robots.txt") "Sitemap: http://localhost:7000/wp-sitemap.xml"))))
(test/with-test-system [system {}]
(doseq [wp (vals (:wordpresses system))
:let [wp-cli! #(apply test/wp-cli! {:path (get-in wp [:paths :cli])} %&)]]
(test/testing [wp "Crawling works"]
(wp-cli! "wp2static" "detect")
(wp-cli! "wp2static" "crawl")
(is (str/includes? (get-crawled-file wp "index.html")
"Welcome to WordPress"))
(is (str/includes? (get-crawled-file wp "robots.txt")
(str "Disallow: " (get-in wp [:paths :site]) "wp-admin/")))))))
56 changes: 35 additions & 21 deletions integration-tests/test/wp2static_test/detect_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
[clojure.test :refer :all]
[wp2static-test.test :as test]))

(defn get-crawled-file [path]
(slurp (str "wordpress/wp-content/uploads/wp2static-crawled-site/" path)))
(defn get-crawled-file [wp path]
(slurp (str (get-in wp [:paths :uploads]) "/wp2static-crawled-site/" path)))

(defmacro with-robots-txt [wp s & body]
`(let [path# (str (get-in ~wp [:paths :doc-root]) "/robots.txt")]
(try
(spit path# ~s)
(do ~@body)
(finally
(test/sh! {} "rm" "-f" path#)))))

(def robots-sitemap-404
"User-agent: *
Expand All @@ -20,16 +28,21 @@ Sitemap: http://localhost:7000/does-not-exist.xml")
<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"><sitemap><loc>http://localhost:7000/does-not-exist.xml</loc></sitemap></sitemapindex>")

(deftest test-robots-404
(testing "robots.txt sitemap URLs that return 404s are ignored"
(test/with-test-system [_]
(try
(spit "wordpress/robots.txt" robots-sitemap-404)
(spit "wordpress/wp-content/sitemap.xml" sitemap-with-404)
(is (zero? (:exit (test/wp-cli!
{:expect-warnings {#".*Got 404 for sitemap.*" 1}}
"wp2static" "detect"))))
(finally
(test/sh! {} "rm" "wordpress/robots.txt" "wordpress/wp-content/sitemap.xml"))))))
(test/with-test-system [system {}]
(doseq [wp (vals (:wordpresses system))
:let [wp-cli! #(apply test/wp-cli! {:path (get-in wp [:paths :cli])} %&)]]
(when (get-in wp [:features :sitemaps?])
(test/testing [wp "robots.txt sitemap URLs that return 404s are ignored"]
(with-robots-txt wp robots-sitemap-404
(let [sitemap-path (str (get-in wp [:paths :wp-content]) "/sitemap.xml")]
(try
(spit sitemap-path sitemap-with-404)
(is (zero? (:exit (test/wp-cli!
{:expect-warnings {#".*Got 404 for sitemap.*" 1}
:path (get-in wp [:paths :cli])}
"wp2static" "detect"))))
(finally
(test/sh! {} "rm" "-f" sitemap-path))))))))))

(def robots-sitemap-slashes
"User-agent: *
Expand All @@ -39,12 +52,13 @@ Allow: /wp-admin/admin-ajax.php
Sitemap: http://localhost:7000//wp-sitemap.xml")

(deftest test-robots-sitemap-slashes
(testing "robots.txt sitemap URLs with double slashes are processed"
(test/with-test-system [_]
(try
(spit "wordpress/robots.txt" robots-sitemap-slashes)
(test/wp-cli! {} "wp2static" "detect")
(test/wp-cli! {} "wp2static" "crawl")
(is (str/includes? (get-crawled-file "wp-sitemap-posts-post-1.xml") "http://localhost:7000/hello-world/"))
(finally
(test/sh! {} "rm" "wordpress/robots.txt"))))))
(test/with-test-system [system {}]
(doseq [wp (vals (:wordpresses system))
:let [wp-cli! #(apply test/wp-cli! {:path (get-in wp [:paths :cli])} %&)]]
(when (get-in wp [:features :sitemaps?])
(test/testing [wp "robots.txt sitemap URLs with double slashes are processed"]
(with-robots-txt wp robots-sitemap-slashes
(wp-cli! "wp2static" "detect")
(wp-cli! "wp2static" "crawl")
(is (str/includes? (get-crawled-file wp "wp-sitemap-posts-post-1.xml")
(str (get-in wp [:paths :home] "hello-world/"))))))))))
33 changes: 19 additions & 14 deletions integration-tests/test/wp2static_test/options_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,24 @@ function processQueueImmediately_filter ( $val ) {
add_filter( 'wp2static_option_processQueueImmediately', 'processQueueImmediately_filter' );")

(deftest test-option-filters
(test/with-test-system [_]
(testing "wp2static_option_* filters work"
(is (= "0" (-> (test/wp-cli! {} "wp2static" "options" "get" "processQueueImmediately")
:out
str/trim)))
(try
(test/sh! {} "mkdir" "wordpress/wp-content/plugins/options-test")
(spit "wordpress/wp-content/plugins/options-test/options-test.php"
options-test-filters)
(test/wp-cli! {} "plugin" "activate" "options-test")
(is (= "1" (-> (test/wp-cli! {} "wp2static" "options" "get" "processQueueImmediately")
(test/with-test-system [system {}]
(doseq [wp (vals (:wordpresses system))
:let [wp-cli! #(apply test/wp-cli! {:path (get-in wp [:paths :cli])} %&)
plugins-dir (get-in wp [:paths :plugins])]]
(test/testing [wp "wp2static_option_* filters work"]
(is (= "0" (-> (test/wp-cli! {} "wp2static" "options" "get" "processQueueImmediately")
:out
str/trim)))
(test/wp-cli! {} "plugin" "deactivate" "options-test")
(finally
(test/sh! {} "rm" "-rf" "wordpress/wp-content/plugins/options-test"))))))
(try
(test/sh! {} "mkdir" "options-test"
:dir plugins-dir)
(spit (str plugins-dir "/options-test/options-test.php")
options-test-filters)
(wp-cli! "plugin" "activate" "options-test")
(is (= "1" (-> (wp-cli! "wp2static" "options" "get" "processQueueImmediately")
:out
str/trim)))
(wp-cli! "plugin" "deactivate" "options-test")
(finally
(test/sh! {} "rm" "-rf" "options-test"
:dir plugins-dir)))))))
26 changes: 15 additions & 11 deletions integration-tests/test/wp2static_test/post_process_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
[clojure.test :refer :all]
[wp2static-test.test :as test]))

(defn get-processed-file [path]
(slurp (str "wordpress/wp-content/uploads/wp2static-processed-site/" path)))
(defn get-processed-file [wp path]
(slurp (str (get-in wp [:paths :uploads]) "/wp2static-processed-site/" path)))

(deftest test-processed-site
(test/with-test-system [_]
(test/wp-cli! {} "wp2static" "detect")
(test/wp-cli! {} "wp2static" "crawl")
(test/wp-cli! {} "wp2static" "post_process")
(let [index (get-processed-file "index.html")]
(is (str/includes? index "Welcome to WordPress"))
(testing "Rewrites work"
(is (str/includes? index "<a href=\"https://example.com/hello-world/\">"))
(is (str/includes? (get-processed-file "robots.txt") "Sitemap: https://example.com/wp-sitemap.xml"))))))
(test/with-test-system [system {}]
(doseq [wp (vals (:wordpresses system))
:let [wp-cli! #(apply test/wp-cli! {:path (get-in wp [:paths :cli])} %&)]]
(test/testing [wp "Post-processing works"]
(wp-cli! "wp2static" "detect")
(wp-cli! "wp2static" "crawl")
(wp-cli! "wp2static" "post_process")
(let [index (get-processed-file wp "index.html")]
(is (str/includes? index "Welcome to WordPress"))
(testing "& URL rewriting works"
(is (str/includes? index "<a href=\"https://example.com/hello-world/\">"))
(when (get-in wp [:features :sitemaps?])
(is (str/includes? (get-processed-file wp "robots.txt") "Sitemap: https://example.com/wp-sitemap.xml")))))))))
4 changes: 3 additions & 1 deletion src/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public function crawlSite( string $static_site_path ) : void {
$urls = [];

foreach ( $crawlable_paths as $root_relative_path ) {
$absolute_uri = new URL( $this->site_path . $root_relative_path );
$absolute_uri = new URL(
rtrim( SiteInfo::getURL( 'home' ), '/' ) . $root_relative_path
);
$urls[] = [
'url' => $absolute_uri->get(),
'path' => $root_relative_path,
Expand Down
7 changes: 3 additions & 4 deletions src/DetectSitemapsURLs.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DetectSitemapsURLs {
* @return string[] list of URLs
* @throws WP2StaticException
*/
public static function detect( string $wp_site_url ) : array {
public static function detect() : array {
$sitemaps_urls = [];
$parser = new SitemapParser(
'WP2Static.com',
Expand All @@ -27,14 +27,13 @@ public static function detect( string $wp_site_url ) : array {
]
);

$site_path = rtrim( SiteInfo::getURL( 'site' ), '/' );

$port_override = apply_filters(
'wp2static_curl_port',
null
);

$base_uri = $site_path;
$wp_site_url = SiteInfo::getURL( 'home' );
$base_uri = rtrim( $wp_site_url, '/' );

if ( $port_override ) {
$base_uri = "{$base_uri}:{$port_override}";
Expand Down
11 changes: 9 additions & 2 deletions src/DetectThemeAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ public static function detect( string $theme_type ) : array {

$detected_filename =
str_replace(
$site_path,
'/',
$template_path,
$template_url,
$filename
);

$detected_filename =
str_replace(
get_home_url(),
'/',
$detected_filename
);

if ( $path_crawlable ) {
if ( is_string( $detected_filename ) ) {
array_push(
Expand Down
22 changes: 15 additions & 7 deletions src/FilesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,17 @@ public static function deleteDirWithFiles( string $dir ) : void {
* Get public URLs for all files in a local directory.
*
* @param string $dir
* @param string $base_url
* @param array<string> $filenames_to_ignore
* @param array<string> $file_extensions_to_ignore
* @return string[] list of relative, urlencoded URLs
*/
public static function getListOfLocalFilesByDir(
string $dir,
string $base_url,
array $filenames_to_ignore,
array $file_extensions_to_ignore
) : array {
$site_path = SiteInfo::getPath( 'site' );

if ( ! is_string( $site_path ) ) {
return [];
}

$files = [];

if ( is_dir( $dir ) ) {
Expand All @@ -72,7 +68,19 @@ public static function getListOfLocalFilesByDir(
);

if ( $path_crawlable ) {
$url = str_replace( $site_path, '/', $filename );
$url =
str_replace(
$dir,
$base_url,
$filename
);

$url =
str_replace(
get_home_url(),
'/',
$url
);

if ( is_string( $url ) ) {
$files[] = $url;
Expand Down
14 changes: 7 additions & 7 deletions src/SimpleRewriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ public static function rewriteFileContents( string $file_contents ) : string
CoreOptions::getValue( 'deploymentURL' )
);

$wordpress_site_url = apply_filters(
'wp2static_set_wordpress_site_url',
untrailingslashit( SiteInfo::getUrl( 'site' ) )
$wordpress_home_url = apply_filters(
'wp2static_set_wordpress_home_url',
untrailingslashit( SiteInfo::getUrl( 'home' ) )
);

$wordpress_site_url = untrailingslashit( $wordpress_site_url );
$wordpress_home_url = untrailingslashit( $wordpress_home_url );
$destination_url = untrailingslashit( $destination_url );
$destination_url_rel = URLHelper::getProtocolRelativeURL( $destination_url );
$destination_url_rel_c = addcslashes( $destination_url_rel, '/' );

$replacement_patterns = [
$wordpress_site_url => $destination_url,
URLHelper::getProtocolRelativeURL( $wordpress_site_url ) =>
$wordpress_home_url => $destination_url,
URLHelper::getProtocolRelativeURL( $wordpress_home_url ) =>
URLHelper::getProtocolRelativeURL( $destination_url ),
addcslashes( URLHelper::getProtocolRelativeURL( $wordpress_site_url ), '/' ) =>
addcslashes( URLHelper::getProtocolRelativeURL( $wordpress_home_url ), '/' ) =>
addcslashes( URLHelper::getProtocolRelativeURL( $destination_url ), '/' ),
];

Expand Down
3 changes: 2 additions & 1 deletion src/URLDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public static function detectURLs() : string {
$arrays_to_merge[] =
FilesHelper::getListOfLocalFilesByDir(
SiteInfo::getPath( 'uploads' ),
SiteInfo::getUrl( 'uploads' ),
$filenames_to_ignore,
$file_extensions_to_ignore
);
Expand All @@ -90,7 +91,7 @@ public static function detectURLs() : string {
$detect_sitemaps = apply_filters( 'wp2static_detect_sitemaps', 1 );

if ( $detect_sitemaps ) {
$arrays_to_merge[] = DetectSitemapsURLs::detect( SiteInfo::getURL( 'site' ) );
$arrays_to_merge[] = DetectSitemapsURLs::detect();
}

$detect_parent_theme = apply_filters( 'wp2static_detect_parent_theme', 1 );
Expand Down
Loading