|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once __DIR__ . "/../bootstrap.php"; |
| 4 | + |
| 5 | +if ( $argc < 2 ) { |
| 6 | + echo "Usage: php script.php <command> --file <input-file> --current-site-url <current site url> --new-site-url <target url>\n"; |
| 7 | + echo "Commands:\n"; |
| 8 | + echo " list_urls: List all the URLs found in the input file.\n"; |
| 9 | + echo " migrate_urls: Migrate all the URLs found in the input file from the current site to the target site.\n"; |
| 10 | + exit( 1 ); |
| 11 | +} |
| 12 | + |
| 13 | +$command = $argv[1]; |
| 14 | +$options = []; |
| 15 | + |
| 16 | +for ( $i = 2; $i < $argc; $i ++ ) { |
| 17 | + if ( str_starts_with( $argv[ $i ], '--' ) && isset( $argv[ $i + 1 ] ) ) { |
| 18 | + $options[ substr( $argv[ $i ], 2 ) ] = $argv[ $i + 1 ]; |
| 19 | + $i ++; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +if ( ! isset( $options['file'] ) ) { |
| 24 | + echo "The file option is required.\n"; |
| 25 | + exit( 1 ); |
| 26 | +} |
| 27 | + |
| 28 | +$inputFile = $options['file']; |
| 29 | +if ( ! file_exists( $inputFile ) ) { |
| 30 | + echo "The file $inputFile does not exist.\n"; |
| 31 | + exit( 1 ); |
| 32 | +} |
| 33 | +$block_markup = file_get_contents( $inputFile ); |
| 34 | + |
| 35 | +// @TODO: Decide – should the current site URL be always required to |
| 36 | +// populate $base_url? |
| 37 | +$base_url = $options['current-site-url'] ?? 'https://playground.internal'; |
| 38 | +$p = new WP_Block_Markup_Url_Processor( $block_markup, $base_url ); |
| 39 | + |
| 40 | +switch ( $command ) { |
| 41 | + case 'list_urls': |
| 42 | + echo "URLs found in the markup:\n\n"; |
| 43 | + wp_list_urls_in_block_markup( [ 'block_markup' => $block_markup, 'base_url' => $base_url ]); |
| 44 | + echo "\n"; |
| 45 | + break; |
| 46 | + case 'migrate_urls': |
| 47 | + if ( ! isset( $options['current-site-url'] ) ) { |
| 48 | + echo "The --current-site-url option is required for the migrate_urls command.\n"; |
| 49 | + exit( 1 ); |
| 50 | + } |
| 51 | + if ( ! isset( $options['new-site-url'] ) ) { |
| 52 | + echo "The --new-site-url option is required for the migrate_urls command.\n"; |
| 53 | + exit( 1 ); |
| 54 | + } |
| 55 | + |
| 56 | + echo "Replacing $base_url with " . $options['new-site-url'] . " in the input.\n\n"; |
| 57 | + if (!is_dir('./assets')) { |
| 58 | + mkdir('./assets/', 0777, true); |
| 59 | + } |
| 60 | + $result = wp_rewrite_urls( array( |
| 61 | + 'block_markup' => $block_markup, |
| 62 | + 'base_url' => $base_url, |
| 63 | + 'current-site-url' => $options['current-site-url'], |
| 64 | + 'new-site-url' => $options['new-site-url'], |
| 65 | + ) ); |
| 66 | + if(!is_string($result)) { |
| 67 | + echo "Error! \n"; |
| 68 | + print_r($result); |
| 69 | + exit( 1 ); |
| 70 | + } |
| 71 | + echo $result; |
| 72 | + break; |
| 73 | +} |
| 74 | + |
| 75 | +function wp_list_urls_in_block_markup( $options ) { |
| 76 | + $block_markup = $options['block_markup']; |
| 77 | + $base_url = $options['base_url'] ?? 'https://playground.internal'; |
| 78 | + $p = new WP_Block_Markup_Url_Processor( $block_markup, $base_url ); |
| 79 | + while ( $p->next_url() ) { |
| 80 | + // Skip empty relative URLs. |
| 81 | + if ( ! trim( $p->get_raw_url() ) ) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + echo '* '; |
| 85 | + switch ( $p->get_token_type() ) { |
| 86 | + case '#tag': |
| 87 | + echo 'In <' . $p->get_tag() . '> tag attribute "' . $p->get_inspected_attribute_name() . '": '; |
| 88 | + break; |
| 89 | + case '#block-comment': |
| 90 | + echo 'In a ' . $p->get_block_name() . ' block attribute "' . $p->get_block_attribute_key() . '": '; |
| 91 | + break; |
| 92 | + case '#text': |
| 93 | + echo 'In #text: '; |
| 94 | + break; |
| 95 | + } |
| 96 | + echo $p->get_raw_url() . "\n"; |
| 97 | + } |
| 98 | +} |
0 commit comments