Completed
Push — add/vr-shortcode ( 271979...022ee9 )
by
unknown
26:28 queued 20:24
created

class.jetpack-cli.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
WP_CLI::add_command( 'jetpack', 'Jetpack_CLI' );
4
5
/**
6
 * Control your local Jetpack installation.
7
 */
8
class Jetpack_CLI extends WP_CLI_Command {
9
10
	/**
11
	 * Get Jetpack Details
12
	 *
13
	 * ## OPTIONS
14
	 *
15
	 * None. Simply returns details about whether or not your blog
16
	 * is connected, its Jetpack version, and WordPress.com blog_id.
17
	 *
18
	 * ## EXAMPLES
19
	 *
20
	 * wp jetpack status
21
	 *
22
	 */
23
	public function status( $args, $assoc_args ) {
24
		if ( Jetpack::is_active() ) {
25
			WP_CLI::success( __( 'Jetpack is currently connected to WordPress.com', 'jetpack' ) );
26
			WP_CLI::line( sprintf( __( 'The Jetpack Version is %s', 'jetpack' ), JETPACK__VERSION ) );
27
			WP_CLI::line( sprintf( __( 'The WordPress.com blog_id is %d', 'jetpack' ), Jetpack_Options::get_option( 'id' ) ) );
28
		} else {
29
			WP_CLI::line( __( 'Jetpack is not currently connected to WordPress.com', 'jetpack' ) );
30
		}
31
	}
32
33
	/**
34
	 * Disconnect Jetpack Blogs or Users
35
	 *
36
	 * ## OPTIONS
37
	 *
38
	 * blog: Disconnect the entire blog.
39
	 *
40
	 * user <user_identifier>: Disconnect a specific user from WordPress.com.
41
	 *
42
	 * Please note, the primary account that the blog is connected
43
	 * to WordPress.com with cannot be disconnected without
44
	 * disconnecting the entire blog.
45
	 *
46
	 * ## EXAMPLES
47
	 *
48
	 * wp jetpack disconnect blog
49
	 * wp jetpack disconnect user 13
50
	 * wp jetpack disconnect user username
51
	 * wp jetpack disconnect user [email protected]
52
	 *
53
	 * @synopsis blog|[user <user_id>]
54
	 */
55
	public function disconnect( $args, $assoc_args ) {
56
		if ( ! Jetpack::is_active() ) {
57
			WP_CLI::error( __( 'You cannot disconnect, without having first connected.', 'jetpack' ) );
58
		}
59
60
		$action = isset( $args[0] ) ? $args[0] : 'prompt';
61 View Code Duplication
		if ( ! in_array( $action, array( 'blog', 'user', 'prompt' ) ) ) {
62
			WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
63
		}
64
65
		if ( in_array( $action, array( 'user' ) ) ) {
66
			if ( isset( $args[1] ) ) {
67
				$user_id = $args[1];
68
				if ( ctype_digit( $user_id ) ) {
69
					$field = 'id';
70
					$user_id = (int) $user_id;
71
				} elseif ( is_email( $user_id ) ) {
72
					$field = 'email';
73
					$user_id = sanitize_user( $user_id, true );
74
				} else {
75
					$field = 'login';
76
					$user_id = sanitize_user( $user_id, true );
77
				}
78
				if ( ! $user = get_user_by( $field, $user_id ) ) {
79
					WP_CLI::error( __( 'Please specify a valid user.', 'jetpack' ) );
80
				}
81
			} else {
82
				WP_CLI::error( __( 'Please specify a user.', 'jetpack' ) );
83
			}
84
		}
85
86
		switch ( $action ) {
87
			case 'blog':
88
				Jetpack::log( 'disconnect' );
89
				Jetpack::disconnect();
90
				WP_CLI::success( __( 'Jetpack has been successfully disconnected.', 'jetpack' ) );
91
				break;
92
			case 'user':
93
				if ( Jetpack::unlink_user( $user->ID ) ) {
94
					Jetpack::log( 'unlink', $user->ID );
0 ignored issues
show
The variable $user does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
95
					WP_CLI::success( sprintf( __( '%s has been successfully disconnected.', 'jetpack' ), $action ) );
96
				} else {
97
					WP_CLI::error( sprintf( __( '%s could not be disconnected.  Are you sure they\'re connected currently?', 'jetpack' ), "{$user->login} <{$user->email}>" ) );
98
				}
99
				break;
100
			case 'prompt':
101
				WP_CLI::error( __( 'Please specify if you would like to disconnect a blog or user.', 'jetpack' ) );
102
				break;
103
		}
104
	}
105
106
	/**
107
	 * Manage Jetpack Modules
108
	 *
109
	 * ## OPTIONS
110
	 *
111
	 * list: View all available modules, and their status.
112
	 *
113
	 * activate <module_slug>: Activate a module.
114
	 *
115
	 * deactivate <module_slug>: Deactivate a module.
116
	 *
117
	 * toggle <module_slug>: Toggle a module on or off.
118
	 *
119
	 * ## EXAMPLES
120
	 *
121
	 * wp jetpack module list
122
	 * wp jetpack module activate stats
123
	 * wp jetpack module deactivate stats
124
	 * wp jetpack module toggle stats
125
	 *
126
	 * @synopsis [list|activate|deactivate|toggle [<module_name>]]
127
	 */
128
	public function module( $args, $assoc_args ) {
129
		$action = isset( $args[0] ) ? $args[0] : 'list';
130 View Code Duplication
		if ( ! in_array( $action, array( 'list', 'activate', 'deactivate', 'toggle' ) ) ) {
131
			WP_CLI::error( sprintf( __( '%s is not a valid command.', 'jetpack' ), $action ) );
132
		}
133
134
		if ( in_array( $action, array( 'activate', 'deactivate', 'toggle' ) ) ) {
135
			if ( isset( $args[1] ) ) {
136
				$module_slug = $args[1];
137
				if ( ! Jetpack::is_module( $module_slug ) ) {
138
					WP_CLI::error( sprintf( __( '%s is not a valid module.', 'jetpack' ), $module_slug ) );
139
				}
140
				if ( 'toggle' == $action ) {
141
					$action = Jetpack::is_module_active( $module_slug ) ? 'deactivate' : 'activate';
142
				}
143
			} else {
144
				WP_CLI::line( __( 'Please specify a valid module.', 'jetpack' ) );
145
				$action = 'list';
146
			}
147
		}
148
149
		switch ( $action ) {
150
			case 'list':
151
				WP_CLI::line( __( 'Available Modules:', 'jetpack' ) );
152
				$modules = Jetpack::get_available_modules();
153
				sort( $modules );
154
				foreach( $modules as $module_slug ) {
155
					$active = Jetpack::is_module_active( $module_slug ) ? __( 'Active', 'jetpack' ) : __( 'Inactive', 'jetpack' );
156
					WP_CLI::line( "\t" . str_pad( $module_slug, 24 ) . $active );
157
				}
158
				break;
159 View Code Duplication
			case 'activate':
160
				$module = Jetpack::get_module( $module_slug );
0 ignored issues
show
The variable $module_slug does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
161
				Jetpack::log( 'activate', $module_slug );
162
				Jetpack::activate_module( $module_slug, false );
163
				WP_CLI::success( sprintf( __( '%s has been activated.', 'jetpack' ), $module['name'] ) );
164
				break;
165 View Code Duplication
			case 'deactivate':
166
				$module = Jetpack::get_module( $module_slug );
167
				Jetpack::log( 'deactivate', $module_slug );
168
				Jetpack::deactivate_module( $module_slug );
169
				WP_CLI::success( sprintf( __( '%s has been deactivated.', 'jetpack' ), $module['name'] ) );
170
				break;
171
			case 'toggle':
172
				// Will never happen, should have been handled above and changed to activate or deactivate.
173
				break;
174
		}
175
	}
176
177
}
178