From 780f438f41262180de9ea8995c1ff9a1c10eebd7 Mon Sep 17 00:00:00 2001 From: Joaquim d'Souza Date: Thu, 14 Dec 2023 16:26:20 +0100 Subject: [PATCH] feat: add command to fix broken links --- composer.json | 3 +- composer.lock | 20 +++++++- web/app/themes/awasqa/src/import.php | 68 ++++++++++++++++++++++++++++ web/app/themes/awasqa/src/wpml.php | 3 ++ 4 files changed, 92 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 169f3b0..a61fda5 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,8 @@ "wpackagist-plugin/pdfjs-viewer-shortcode": "2.1.8", "wpackagist-plugin/disable-full-site-editing": "1.2.0", "wpackagist-plugin/bbp-style-pack": "^5.6", - "wpackagist-plugin/redirection": "^5.3" + "wpackagist-plugin/redirection": "^5.3", + "wpackagist-plugin/broken-link-checker": "^2.2" }, "require-dev": { "squizlabs/php_codesniffer": "^3.7.2", diff --git a/composer.lock b/composer.lock index ac58167..c71bc08 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9b39a24b0c507e9a9d9257fa85a792f8", + "content-hash": "78971b3586c87a48fa4e241e80fcf552", "packages": [ { "name": "composer/installers", @@ -1350,6 +1350,24 @@ "type": "wordpress-plugin", "homepage": "https://wordpress.org/plugins/bbpress/" }, + { + "name": "wpackagist-plugin/broken-link-checker", + "version": "2.2.3", + "source": { + "type": "svn", + "url": "https://plugins.svn.wordpress.org/broken-link-checker/", + "reference": "tags/2.2.3" + }, + "dist": { + "type": "zip", + "url": "https://downloads.wordpress.org/plugin/broken-link-checker.2.2.3.zip" + }, + "require": { + "composer/installers": "^1.0 || ^2.0" + }, + "type": "wordpress-plugin", + "homepage": "https://wordpress.org/plugins/broken-link-checker/" + }, { "name": "wpackagist-plugin/co-authors-plus", "version": "3.5.15", diff --git a/web/app/themes/awasqa/src/import.php b/web/app/themes/awasqa/src/import.php index 850bca8..7ff5fec 100644 --- a/web/app/themes/awasqa/src/import.php +++ b/web/app/themes/awasqa/src/import.php @@ -1223,3 +1223,71 @@ function fix_post_categories($slug, $post_xmls) } \WP_CLI::add_command('fix_categories', 'CommonKnowledge\WordPress\Awasqa\Import\fix_categories'); + + +function fix_links() +{ + $replacements = [ + 'perus-constitutional-courts-racist-disregard-for-ilo-169' => 'perus-constitutional-courts-racist-disregard-for-the-right-to-consultation-and-the-ilo-convention-no-169-2', + '3910' => 'pensar-el-mundo-desde-bolivia', + 'indigenous-rights-and-wisdom' => '', + 'derechos-y-sabiduria-indigenas' => '', + 'en' => '', + 'es' => '', + 'post.php' => '' + ]; + $skips = [ + 'about:blank' + ]; + $posts = get_posts([ + 'post_type' => 'post', + 'posts_per_page' => -1 + ]); + foreach ($posts as $post) { + $content = $post->post_content; + preg_match_all('#]*>#', $content, $matches); + if ($matches) { + $urls = $matches[1] ?? []; + foreach ($urls as $url) { + $url = trim($url); + if (str_starts_with($url, 'mailto:')) { + continue; + } + if (in_array($url, $skips)) { + continue; + } + if (str_starts_with($url, 'www')) { + $url = 'https://' . $url; + } + $parsed_url = parse_url($url); + $host = $parsed_url['host'] ?? ''; + if (!$host || $host === 'awasqa.org' || $host === 'greennetworkproject.org') { + $found = @file_get_contents($url); + if (!$found) { + $path = trim($parsed_url['path'] ?? '', '/'); + $parts = explode('/', $path); + $lang = in_array($parts[0], ['en', 'es']) ? $parts[0] : ''; + $slug = array_pop($parts); + $slug = $replacements[$slug] ?? $slug; + if (str_ends_with($slug, '.pdf')) { + $new_path = "https://awasqa.org/wp-content/uploads/$slug"; + } else { + $new_path = $slug ? "https://awasqa.org/$slug/" : "https://awasqa.org/"; + if ($lang) { + $new_path .= "?lang=$lang"; + } + } + echo "Replacing $url with $new_path\n"; + $found = @file_get_contents($new_path); + if (!$found) { + echo "MISSING PAGE $url $new_path\n"; + //exit(1); + } + } + } + } + } + } +} + +\WP_CLI::add_command('fix_links', 'CommonKnowledge\WordPress\Awasqa\Import\fix_links'); diff --git a/web/app/themes/awasqa/src/wpml.php b/web/app/themes/awasqa/src/wpml.php index 55ad127..0f3e974 100644 --- a/web/app/themes/awasqa/src/wpml.php +++ b/web/app/themes/awasqa/src/wpml.php @@ -48,6 +48,9 @@ function get_translated_page_by_slug($slug, $lang) // Clear SitePress state after it has parsed any query add_action("parse_query", function () { global $wpml_query_filter; + if (empty($wpml_query_filter)) { + return; + } $r = new \ReflectionObject($wpml_query_filter); $p = $r->getProperty('name_filter'); $p->setAccessible(true);