Completed
Push — update/api-get-site-endpoint ( 87e1bc )
by
unknown
09:51
created

SAL_Site::format_date()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 43
Code Lines 27

Duplication

Lines 43
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 43
loc 43
rs 5.3846
c 1
b 0
f 0
cc 8
eloc 27
nc 9
nop 2
1
<?php
2
3
4
/**
5
 * Base class for the Site Abstraction Layer (SAL)
6
 **/
7
8
abstract class SAL_Site {
9
	public $blog_id;
10
11
	public function __construct( $blog_id ) {
12
		$this->blog_id = $blog_id;
13
	}
14
15
	abstract public function has_videopress();
16
	abstract public function upgraded_filetypes_enabled();
17
	abstract public function is_mapped_domain();
18
	abstract public function is_redirect();
19
	abstract public function featured_images_enabled();
20
	abstract public function has_wordads();
21
	abstract public function get_frame_nonce();
22
	abstract public function allowed_file_types();
23
	abstract public function get_post_formats();
24
	abstract public function is_private();
25
	abstract public function is_following();
26
	abstract public function get_subscribers_count();
27
	abstract public function get_locale();
28
	abstract public function is_jetpack();
29
	abstract public function get_jetpack_modules();
30
31
	abstract public function before_render();
32
	abstract public function after_render( &$response );
33
	abstract public function after_render_options( &$options );
34
35
	function get_registered_date() {
36
		if ( function_exists( 'get_blog_details' ) ) {
37
			$blog_details = get_blog_details();
38
			if ( ! empty( $blog_details->registered ) ) {
39
				return $this->format_date( $blog_details->registered );
40
			}
41
		}
42
43
		return '0000-00-00T00:00:00+00:00';
44
	}
45
46
	function is_visible() {
47
		if ( is_user_logged_in() ){
48
			$current_user = wp_get_current_user();
49
			$visible = (array) get_user_meta( $current_user->ID, 'blog_visibility', true );
50
51
			$is_visible = true;
52
			if ( isset( $visible[$this->blog_id] ) ) {
53
				$is_visible = (bool) $visible[$this->blog_id];
54
			}
55
			// null and true are visible
56
			return $is_visible;
57
		}
58
		return null;
59
	}
60
61
	function get_logo() {
62
63
		// Set an empty response array.
64
		$logo_setting = array(
65
			'id'  => (int) 0,
66
			'sizes' => array(),
67
			'url' => '',
68
		);
69
70
		// Get current site logo values.
71
		$logo = get_option( 'site_logo' );
72
73
		// Update the response array if there's a site logo currenty active.
74
		if ( $logo && 0 != $logo['id'] ) {
75
			$logo_setting['id']  = $logo['id'];
76
			$logo_setting['url'] = $logo['url'];
77
78
			foreach ( $logo['sizes'] as $size => $properties ) {
79
				$logo_setting['sizes'][$size] = $properties;
80
			}
81
		}
82
83
		return $logo_setting;
84
	}
85
86
	/**
87
	 * Returns ISO 8601 formatted datetime: 2011-12-08T01:15:36-08:00
88
	 *
89
	 * @param $date_gmt (string) GMT datetime string.
90
	 * @param $date (string) Optional.  Used to calculate the offset from GMT.
91
	 *
92
	 * @return string
93
	 */
94 View Code Duplication
	function format_date( $date_gmt, $date = null ) {
95
		$timestamp_gmt = strtotime( "$date_gmt+0000" );
96
97
		if ( null === $date ) {
98
			$timestamp = $timestamp_gmt;
99
			$hours     = $minutes = $west = 0;
100
		} else {
101
			$date_time = date_create( "$date+0000" );
102
			if ( $date_time ) {
103
				$timestamp = date_format(  $date_time, 'U' );
104
			} else {
105
				$timestamp = 0;
106
			}
107
108
			// "0000-00-00 00:00:00" == -62169984000
109
			if ( -62169984000 == $timestamp_gmt ) {
110
				// WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts
111
				// WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts
112
113
				// Try to guess the correct offset from the blog's options.
114
				$timezone_string = get_option( 'timezone_string' );
115
116
				if ( $timezone_string && $date_time ) {
117
					$timezone = timezone_open( $timezone_string );
118
					if ( $timezone ) {
119
						$offset = $timezone->getOffset( $date_time );
120
					}
121
				} else {
122
					$offset = 3600 * get_option( 'gmt_offset' );
123
				}
124
			} else {
125
				$offset = $timestamp - $timestamp_gmt;
126
			}
127
128
			$west      = $offset < 0;
0 ignored issues
show
Bug introduced by
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...
129
			$offset    = abs( $offset );
130
			$hours     = (int) floor( $offset / 3600 );
131
			$offset   -= $hours * 3600;
132
			$minutes   = (int) floor( $offset / 60 );
133
		}
134
135
		return (string) gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes );
136
	}
137
}