Completed
Push — update/themes-endpoint-add-tra... ( 47c167 )
by
unknown
426:32 queued 419:00
created

update_translation()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 13

Duplication

Lines 30
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 0
dl 30
loc 30
rs 8.5806
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', 'update_translations' );
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
		if ( isset( $args['autoupdate_themes_translations'] ) && is_bool( $args['autoupdate_themes_translations'] ) ) {
21
			if ( $args['autoupdate_themes_translations'] ) {
22
				$this->autoupdate_translations_on();
23
			} else {
24
				$this->autoupdate_translations_off();
25
			}
26
		}
27
28
		return true;
29
	}
30
31
	function autoupdate_on() {
32
		$autoupdate_themes = Jetpack_Options::get_option( 'autoupdate_themes', array() );
33
		$autoupdate_themes = array_unique( array_merge( $autoupdate_themes, $this->themes ) );
34
		Jetpack_Options::update_option( 'autoupdate_themes', $autoupdate_themes );
35
	}
36
37
	function autoupdate_off() {
38
		$autoupdate_themes = Jetpack_Options::get_option( 'autoupdate_themes', array() );
39
		$autoupdate_themes = array_diff( $autoupdate_themes, $this->themes );
40
		Jetpack_Options::update_option( 'autoupdate_themes', $autoupdate_themes );
41
	}
42
43
	function autoupdate_translations_on() {
44
		$autoupdate_themes_translations = Jetpack_Options::get_option( 'autoupdate_themes_translations', array() );
45
		$autoupdate_themes_translations = array_unique( array_merge( $autoupdate_themes_translations, $this->themes ) );
46
		Jetpack_Options::update_option( 'autoupdate_themes_translations', $autoupdate_themes_translations );
47
	}
48
49
	function autoupdate_translations_off() {
50
		$autoupdate_themes_translations = Jetpack_Options::get_option( 'autoupdate_themes_translations', array() );
51
		$autoupdate_themes_translations = array_diff( $autoupdate_themes_translations, $this->themes );
52
		Jetpack_Options::update_option( 'autoupdate_themes_translations', $autoupdate_themes_translations );
53
	}
54
55 View Code Duplication
	function update() {
56
		include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
57
58
		// Clear the cache.
59
		wp_update_themes();
60
61
		foreach ( $this->themes as $theme ) {
62
			/**
63
			 * Pre-upgrade action
64
			 * 
65
			 * @since 3.9.3
66
			 * 
67
			 * @param object $theme WP_Theme object
68
			 * @param array $themes Array of theme objects
69
			 */
70
			do_action('jetpack_pre_theme_upgrade', $theme, $this->themes);
71
			// Objects created inside the for loop to clean the messages for each theme
72
			$skin = new Automatic_Upgrader_Skin();
73
			$upgrader = new Theme_Upgrader( $skin );
74
			$upgrader->init();
75
			$result   = $upgrader->upgrade( $theme );
76
			$this->log[ $theme ][] = $upgrader->skin->get_upgrade_messages();
77
		}
78
79
		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...
80
			return new WP_Error( 'update_fail', __( 'There was an error updating your theme', 'jetpack' ), 400 );
81
		}
82
83
		return true;
84
	}
85
86 View Code Duplication
	function update_translation() {
87
		include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
88
89
		// Clear the cache.
90
		wp_update_themes();
91
92
		foreach ( $this->themes as $theme ) {
93
			/**
94
			 * Pre-upgrade action
95
			 *
96
			 * @since 3.9.3
97
			 *
98
			 * @param object $theme WP_Theme object
99
			 * @param array $themes Array of theme objects
100
			 */
101
			do_action('jetpack_pre_theme_upgrade', $theme, $this->themes);
102
			// Objects created inside the for loop to clean the messages for each theme
103
			$skin = new Automatic_Upgrader_Skin();
104
			$upgrader = new Language_Pack_Upgrader( $skin );
105
			$upgrader->init();
106
			$result   = $upgrader->upgrade( $theme );
107
			$this->log[ $theme ][] = $upgrader->skin->get_upgrade_messages();
108
		}
109
110
		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...
111
			return new WP_Error( 'update_fail', __( 'There was an error updating your theme', 'jetpack' ), 400 );
112
		}
113
114
		return true;
115
	}
116
117
}
118