Skip to content

Commit

Permalink
feat: add command to fix broken links
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquimds committed Dec 14, 2023
1 parent e9bfe77 commit 780f438
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 19 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions web/app/themes/awasqa/src/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('#<a[^h]*href="([^"]*)"[^>]*>#', $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');
3 changes: 3 additions & 0 deletions web/app/themes/awasqa/src/wpml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 780f438

Please sign in to comment.