Completed
Push — master-stable ( 2e33eb...2dc49c )
by
unknown
16:43 queued 09:14
created

class.jetpack-json-api-themes-modify-endpoint.php (1 issue)

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
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
	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 View Code Duplication
		if ( isset( $args['autoupdate_translations'] ) && is_bool( $args['autoupdate_translations'] ) ) {
21
			if ( $args['autoupdate_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
	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 View Code Duplication
		if ( ! $this->bulk && ! $result ) {
80
			return new WP_Error( 'update_fail', __( 'There was an error updating your theme', 'jetpack' ), 400 );
81
		}
82
83
		return true;
84
	}
85
86
	function update_translations() {
87
		include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
88
89
		// Clear the cache.
90
		wp_update_themes();
91
	
92
		$available_themes_updates = get_site_transient( 'update_themes' );
93
		
94
		if ( ! isset( $available_themes_updates->translations ) || empty( $available_themes_updates->translations ) ) {
95
			return new WP_Error( 'nothing_to_translate' );
96
		}
97
98
		foreach( $available_themes_updates->translations as $translation ) {
99
			$theme = $translation['slug'] ;
100
			if ( ! in_array( $translation['slug'], $this->themes )  ) {
101
				$this->log[ $theme ][] = __( 'No update needed', 'jetpack' );
102
				continue;
103
			}
104
105
			/**
106
			 * Pre-upgrade action
107
			 *
108
			 * @since 4.4
109
			 *
110
			 * @param object $theme WP_Theme object
111
			 * @param array $themes Array of theme objects
112
			 */
113
			do_action( 'jetpack_pre_theme_upgrade_translations', $theme, $this->themes );
114
			// Objects created inside the for loop to clean the messages for each theme
115
			$skin = new Automatic_Upgrader_Skin();
116
			$upgrader = new Language_Pack_Upgrader( $skin );
117
			$upgrader->init();
118
119
			$result   = $upgrader->upgrade( (object) $translation );
120
			$this->log[ $theme ] = $upgrader->skin->get_upgrade_messages();
121
		}
122
123
		if ( ! $this->bulk && ! $result ) {
0 ignored issues
show
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...
124
			return new WP_Error( 'update_fail', __( 'There was an error updating your theme', 'jetpack' ), 400 );
125
		}
126
127
		return true;
128
	}
129
130
}
131