← All changes
|
jetpack_vendor/automattic/jetpack-search/src/class-cli.php
+57
-0
13.6.2
→
16.3-a.7
View file →
| @@ -10,8 +10,12 @@ | ||
| 10 | 10 | use WP_CLI; |
| 11 | 11 | use WP_CLI_Command; |
| 12 | 12 | use WP_Error; |
| 13 | 13 | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 17 | + | |
| 14 | 18 | if ( ! class_exists( 'WP_CLI_Command' ) ) { |
| 15 | 19 | return; |
| 16 | 20 | } |
| 17 | 21 | |
| @@ -45,8 +49,61 @@ | ||
| 45 | 49 | WP_CLI::line( 'Running as user ' . $ret->user_login . '…' ); |
| 46 | 50 | $blog_id = Helper::get_wpcom_site_id(); |
| 47 | 51 | Instant_Search::instance( $blog_id )->auto_config_search(); |
| 48 | 52 | WP_CLI::success( 'Auto config success!' ); |
| 53 | + } catch ( \Exception $e ) { | |
| 54 | + WP_CLI::error( $e->getMessage() ); | |
| 55 | + } | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Backfill the reserved-slot taxonomy projection for any user-facing | |
| 60 | + * taxonomies declared in `jetpack_search_custom_taxonomy_map`. | |
| 61 | + * | |
| 62 | + * Wraps `Custom_Taxonomy_Slot_Mapping::backfill()` so operators don't have | |
| 63 | + * to reach for `wp eval` and a fully-qualified static call. The auto-mirror | |
| 64 | + * on `set_object_terms` / `deleted_term_relationships` / `delete_term` | |
| 65 | + * keeps the slot in sync from the moment the mapping is declared — this | |
| 66 | + * subcommand only needs to run once after the mapping is first turned on, | |
| 67 | + * or after a gap during which the auto-mirror was inactive. | |
| 68 | + * | |
| 69 | + * ## OPTIONS | |
| 70 | + * | |
| 71 | + * [--mode=<mode>] | |
| 72 | + * : Backfill mode. | |
| 73 | + * --- | |
| 74 | + * default: mirror | |
| 75 | + * options: | |
| 76 | + * - mirror | |
| 77 | + * - rebuild | |
| 78 | + * --- | |
| 79 | + * | |
| 80 | + * ## EXAMPLES | |
| 81 | + * | |
| 82 | + * # Per-post mirror — cheap, idempotent, safe to re-run. | |
| 83 | + * wp jetpack-search backfill_taxonomy_slot_mapping | |
| 84 | + * | |
| 85 | + * # Full sweep — wipes the slot taxonomy first, then re-projects. | |
| 86 | + * wp jetpack-search backfill_taxonomy_slot_mapping --mode=rebuild | |
| 87 | + * | |
| 88 | + * @param array $args Positional args (unused). | |
| 89 | + * @param array $assoc_args Associative args. Supports `mode`. | |
| 90 | + */ | |
| 91 | + public function backfill_taxonomy_slot_mapping( $args, $assoc_args ) { | |
| 92 | + $mode = isset( $assoc_args['mode'] ) ? (string) $assoc_args['mode'] : 'mirror'; | |
| 93 | + if ( ! in_array( $mode, Custom_Taxonomy_Slot_Mapping::BACKFILL_MODES, true ) ) { | |
| 94 | + WP_CLI::error( sprintf( 'Unknown mode "%s"; expected one of: %s.', $mode, implode( ', ', Custom_Taxonomy_Slot_Mapping::BACKFILL_MODES ) ) ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + if ( empty( Custom_Taxonomy_Slot_Mapping::get_map() ) ) { | |
| 98 | + WP_CLI::warning( 'jetpack_search_custom_taxonomy_map is empty; nothing to backfill.' ); | |
| 99 | + return; | |
| 100 | + } | |
| 101 | + | |
| 102 | + WP_CLI::line( sprintf( 'Running backfill in %s mode…', $mode ) ); | |
| 103 | + try { | |
| 104 | + $count = Custom_Taxonomy_Slot_Mapping::backfill( $mode ); | |
| 105 | + WP_CLI::success( sprintf( 'Backfilled %d (post, taxonomy) pair(s).', $count ) ); | |
| 49 | 106 | } catch ( \Exception $e ) { |
| 50 | 107 | WP_CLI::error( $e->getMessage() ); |
| 51 | 108 | } |
| 52 | 109 | } |