PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-search / src / class-cli.php

class-cli.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.5, at jetpack_vendor/automattic/jetpack-search/src/class-cli.php

128 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CLI class exposed by WPCLI
4 *
5 * @package automattic/jetpack-search
6 */
7
8 namespace Automattic\Jetpack\Search;
9
10 use WP_CLI;
11 use WP_CLI_Command;
12 use WP_Error;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit( 0 );
16 }
17
18 if ( ! class_exists( 'WP_CLI_Command' ) ) {
19 return;
20 }
21
22 /**
23 * Provide functionality by WPCLI.
24 */
25 class CLI extends WP_CLI_Command {
26 /**
27 * Auto config instant search, including set result format, set up overlay widgets and add a search input to the home page thru widgets or blocks.
28 *
29 * ## EXAMPLES
30 *
31 * wp jetpack-search auto_config user_login
32 *
33 * wp jetpack-search auto_config user_id
34 *
35 * @param array $args - Args passsed in.
36 */
37 public function auto_config( $args ) {
38 try {
39 if ( empty( $args ) ) {
40 WP_CLI::error( 'A user login or ID is required.' );
41 }
42
43 // Some functions may require admin capabilities to run.
44 $ret = $this->set_user( $args[0] );
45 if ( is_wp_error( $ret ) ) {
46 WP_CLI::error( $ret->get_error_message() );
47 }
48
49 WP_CLI::line( 'Running as user ' . $ret->user_login . '…' );
50 $blog_id = Helper::get_wpcom_site_id();
51 Instant_Search::instance( $blog_id )->auto_config_search();
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 ) );
106 } catch ( \Exception $e ) {
107 WP_CLI::error( $e->getMessage() );
108 }
109 }
110
111 /**
112 * Set current user by ID or login
113 *
114 * @param string|int $user User ID or login.
115 */
116 protected function set_user( $user ) {
117 $get_user_by = 'login';
118 if ( filter_var( $user, FILTER_VALIDATE_INT ) > 0 ) {
119 $get_user_by = 'ID';
120 }
121 $user_info = get_user_by( $get_user_by, (string) $user );
122 if ( ! $user_info ) {
123 return new WP_Error( 'user_not_found', "Could not find user '{$user}' by {$get_user_by}." );
124 }
125 return wp_set_current_user( $user_info->ID );
126 }
127 }
128