Completed
Push — branch-4.4-built ( 706a73...7855f2 )
by
unknown
63:06 queued 56:00
created

default_action()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 0
dl 12
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_JSON_API_Themes_Modify_Endpoint extends Jetpack_JSON_API_Themes_Endpoint {
4
	// POST  /sites/%s/themes/%s
5
	// POST  /sites/%s/themes
6
7
	protected $needed_capabilities = 'update_themes';
8
	protected $action              = 'default_action';
9
	protected $expected_actions    = array( 'update' );
10
11 View Code Duplication
	public function default_action() {
12
		$args = $this->input();
13
		if ( isset( $args['autoupdate'] ) && is_bool( $args['autoupdate'] ) ) {
14
			if ( $args['autoupdate'] ) {
15
				$this->autoupdate_on();
16
			} else {
17
				$this->autoupdate_off();
18
			}
19
		}
20
21
		return true;
22
	}
23
24
	function autoupdate_on() {
25
		$autoupdate_themes = Jetpack_Options::get_option( 'autoupdate_themes', array() );
26
		$autoupdate_themes = array_unique( array_merge( $autoupdate_themes, $this->themes ) );
27
		Jetpack_Options::update_option( 'autoupdate_themes', $autoupdate_themes );
28
	}
29
30
	function autoupdate_off() {
31
		$autoupdate_themes = Jetpack_Options::get_option( 'autoupdate_themes', array() );
32
		$autoupdate_themes = array_diff( $autoupdate_themes, $this->themes );
33
		Jetpack_Options::update_option( 'autoupdate_themes', $autoupdate_themes );
34
	}
35
36
	function update() {
37
		include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
38
39
		// Clear the cache.
40
		wp_update_themes();
41
42
		foreach ( $this->themes as $theme ) {
43
			/**
44
			 * Pre-upgrade action
45
			 * 
46
			 * @since 3.9.3
47
			 * 
48
			 * @param object $theme WP_Theme object
49
			 * @param array $themes Array of theme objects
50
			 */
51
			do_action('jetpack_pre_theme_upgrade', $theme, $this->themes);
52
			// Objects created inside the for loop to clean the messages for each theme
53
			$skin = new Automatic_Upgrader_Skin();
54
			$upgrader = new Theme_Upgrader( $skin );
55
			$upgrader->init();
56
			$result   = $upgrader->upgrade( $theme );
57
			$this->log[ $theme ][] = $upgrader->skin->get_upgrade_messages();
58
		}
59
60 View Code Duplication
		if ( ! $this->bulk && ! $result ) {
0 ignored issues
show
Bug introduced by
The variable $result 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...
61
			return new WP_Error( 'update_fail', __( 'There was an error updating your theme', 'jetpack' ), 400 );
62
		}
63
64
		return true;
65
	}
66
67
}
68