Completed
Push — master-stable ( 4a8b26...01e321 )
by
unknown
09:43
created

sal/class.json-api-site-base.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
4
/**
5
 * Base class for the Site Abstraction Layer (SAL)
6
 **/
7
abstract class SAL_Site {
8
	public $blog_id;
9
10
	public function __construct( $blog_id ) {
11
		$this->blog_id = $blog_id;
12
	}
13
14
	abstract public function has_videopress();
15
16
	abstract public function upgraded_filetypes_enabled();
17
18
	abstract public function is_mapped_domain();
19
20
	abstract public function is_redirect();
21
22
	abstract public function featured_images_enabled();
23
24
	abstract public function has_wordads();
25
26
	abstract public function get_frame_nonce();
27
28
	abstract public function allowed_file_types();
29
30
	abstract public function get_post_formats();
31
32
	abstract public function is_private();
33
34
	abstract public function is_following();
35
36
	abstract public function get_subscribers_count();
37
38
	abstract public function get_locale();
39
40
	abstract public function is_jetpack();
41
42
	abstract public function get_jetpack_modules();
43
44
	abstract public function is_vip();
45
46
	abstract public function is_multisite();
47
48
	abstract public function is_single_user_site();
49
50
	abstract public function get_plan();
51
52
	abstract public function get_ak_vp_bundle_enabled();
53
54
	abstract public function before_render();
55
56
	abstract public function after_render( &$response );
57
58
	abstract public function after_render_options( &$options );
59
60
	function user_can_manage() {
61
		current_user_can( 'manage_options' ); // remove this attribute in favor of 'capabilities'
62
	}
63
64
	function get_registered_date() {
65
		if ( function_exists( 'get_blog_details' ) ) {
66
			$blog_details = get_blog_details();
67
			if ( ! empty( $blog_details->registered ) ) {
68
				return $this->format_date( $blog_details->registered );
69
			}
70
		}
71
72
		return '0000-00-00T00:00:00+00:00';
73
	}
74
75
	function get_capabilities() {
76
		return array(
77
			'edit_pages'          => current_user_can( 'edit_pages' ),
78
			'edit_posts'          => current_user_can( 'edit_posts' ),
79
			'edit_others_posts'   => current_user_can( 'edit_others_posts' ),
80
			'edit_others_pages'   => current_user_can( 'edit_others_pages' ),
81
			'delete_posts'        => current_user_can( 'delete_posts' ),
82
			'delete_others_posts' => current_user_can( 'delete_others_posts' ),
83
			'edit_theme_options'  => current_user_can( 'edit_theme_options' ),
84
			'edit_users'          => current_user_can( 'edit_users' ),
85
			'list_users'          => current_user_can( 'list_users' ),
86
			'manage_categories'   => current_user_can( 'manage_categories' ),
87
			'manage_options'      => current_user_can( 'manage_options' ),
88
			'promote_users'       => current_user_can( 'promote_users' ),
89
			'publish_posts'       => current_user_can( 'publish_posts' ),
90
			'upload_files'        => current_user_can( 'upload_files' ),
91
			'view_stats'          => stats_is_blog_user( $this->blog_id )
92
		);
93
	}
94
95
	function is_visible() {
96
		if ( is_user_logged_in() ) {
97
			$current_user = wp_get_current_user();
98
			$visible      = (array) get_user_meta( $current_user->ID, 'blog_visibility', true );
99
100
			$is_visible = true;
101
			if ( isset( $visible[ $this->blog_id ] ) ) {
102
				$is_visible = (bool) $visible[ $this->blog_id ];
103
			}
104
105
			// null and true are visible
106
			return $is_visible;
107
		}
108
109
		return null;
110
	}
111
112
	function get_logo() {
113
114
		// Set an empty response array.
115
		$logo_setting = array(
116
			'id'    => (int) 0,
117
			'sizes' => array(),
118
			'url'   => '',
119
		);
120
121
		// Get current site logo values.
122
		$logo = get_option( 'site_logo' );
123
124
		// Update the response array if there's a site logo currenty active.
125
		if ( $logo && 0 != $logo['id'] ) {
126
			$logo_setting['id']  = $logo['id'];
127
			$logo_setting['url'] = $logo['url'];
128
129
			foreach ( $logo['sizes'] as $size => $properties ) {
130
				$logo_setting['sizes'][ $size ] = $properties;
131
			}
132
		}
133
134
		return $logo_setting;
135
	}
136
137
	/**
138
	 * Returns ISO 8601 formatted datetime: 2011-12-08T01:15:36-08:00
139
	 *
140
	 * @param $date_gmt (string) GMT datetime string.
141
	 * @param $date (string) Optional.  Used to calculate the offset from GMT.
142
	 *
143
	 * @return string
144
	 */
145 View Code Duplication
	function format_date( $date_gmt, $date = null ) {
146
		$timestamp_gmt = strtotime( "$date_gmt+0000" );
147
148
		if ( null === $date ) {
149
			$timestamp = $timestamp_gmt;
150
			$hours     = $minutes = $west = 0;
151
		} else {
152
			$date_time = date_create( "$date+0000" );
153
			if ( $date_time ) {
154
				$timestamp = date_format( $date_time, 'U' );
155
			} else {
156
				$timestamp = 0;
157
			}
158
159
			// "0000-00-00 00:00:00" == -62169984000
160
			if ( - 62169984000 == $timestamp_gmt ) {
161
				// WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts
162
				// WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts
163
164
				// Try to guess the correct offset from the blog's options.
165
				$timezone_string = get_option( 'timezone_string' );
166
167
				if ( $timezone_string && $date_time ) {
168
					$timezone = timezone_open( $timezone_string );
169
					if ( $timezone ) {
170
						$offset = $timezone->getOffset( $date_time );
171
					}
172
				} else {
173
					$offset = 3600 * get_option( 'gmt_offset' );
174
				}
175
			} else {
176
				$offset = $timestamp - $timestamp_gmt;
177
			}
178
179
			$west   = $offset < 0;
0 ignored issues
show
The variable $offset 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...
180
			$offset = abs( $offset );
181
			$hours  = (int) floor( $offset / 3600 );
182
			$offset -= $hours * 3600;
183
			$minutes = (int) floor( $offset / 60 );
184
		}
185
186
		return (string) gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes );
187
	}
188
}
189