Completed
Push — add/sync-rest-options ( b51d37...82a04d )
by
unknown
09:12
created

Jetpack_JSON_API_Get_Options_Endpoint::result()   D

Complexity

Conditions 9
Paths 22

Size

Total Lines 27
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 4.909
cc 9
eloc 23
nc 22
nop 0
1
<?php
2
3
class Jetpack_JSON_API_Get_Options_Endpoint extends Jetpack_JSON_API_Endpoint {
4
5
	protected $needed_capabilities = 'manage_options';
6
7
	static $options = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $options.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
8
		'blogname' => 'string'
9
	);
10
11
	/**
12
	 * Data that we want to sync.
13
	 * @var array
14
	 */
15
	static $mock_options = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $mock_options.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
16
		'admin_url' => array(
17
			'type' => 'url',
18
			'callback' => 'get_admin_url'
19
		)
20
	);
21
22
	/**
23
	 * Constants that we want to sync
24
	 * @var array
25
	 */
26
	static $constants = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $constants.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
27
		'EMPTY_TRASH_DAYS' => 'int',
28
	);
29
30
	function result() {
31
		$data = array();
32
		if ( isset( $args['options'] ) && is_array( $args['options'] ) ) {
33
			$all_settings = $args['options'];
34
		} else {
35
			$type = isset( $args['options'] ) ? $args['options'] : 'all';
0 ignored issues
show
Bug introduced by
The variable $args seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
36
			switch( $type ) {
37
				case 'options':
38
					$all_settings = array_keys( self::$options );
39
					break;
40
				case 'mock_options':
41
					$all_settings = array_keys( self::$mock_options );
42
					break;
43
				case 'constants':
44
					$all_settings = array_keys( self::$constants );
45
					break;
46
				case 'all':
47
				default:
48
					$all_settings = array_merge( array_keys( self::$options ), array_keys( self::$mock_options ), array_keys( self::$constants ) );
49
					break;
50
			}
51
		}
52
		foreach ( $all_settings as $name  ) {
53
			$data[ $name ] = self::get( $name );
54
		}
55
		return array( 'options', $data );
56
	}
57
58
	/**
59
	 * Get individual setting
60
	 *
61
	 * @param  sting $name
62
	 * @param  string $type
0 ignored issues
show
Bug introduced by
There is no parameter named $type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
63
	 * @param  string or array $callback
64
	 * @param  boolean $is_constant
0 ignored issues
show
Bug introduced by
There is no parameter named $is_constant. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
65
	 * @return value of the setting
66
	 */
67
	static function get( $name ) {
68
		// Options
69
		if ( array_key_exists( $name, self::$options ) ) {
70
			return self::validate_option( get_option( $name ), self::$options[ $name ] );
71
			// Mock options
72
		} elseif ( array_key_exists( $name, self::$mock_options ) ) {
73
			if ( is_callable( self::$mock_options[ $name ][ 'callback' ] ) ) {
74
				$args = array();
75
				if ( isset( self::$mock_options[ $name ][ 'callback_args' ] ) &&
76
				     is_array( self::$mock_options[ $name ][ 'callback_args' ] ) ) {
77
					$args = self::$mock_options[ $name ][ 'callback_args' ];
78
				}
79
				$data = call_user_func_array( self::$mock_options[ $name ][ 'callback' ], $args );
80
				return self::validate_option( $data, self::$mock_options[ $name ][ 'type' ] );
81
			} else {
82
				return new WP_Error( json_encode( self::$mock_options[ $name ][ 'callback' ] ) . ' can not be called' );
83
			}
84
			// Constants
85
		} elseif ( array_key_exists( $name, self::$constants) ) {
86
			if ( defined( $name ) ) {
87
				return self::validate_option( constant( $name ) , self::$constants[ $name ] );
88
			}
89
		}
90
		return null;
91
	}
92
93 View Code Duplication
	static function validate( $data, $type = null ) {
94
		if ( is_null( $data ) ) {
95
			return $data;
96
		}
97
		switch( $type ) {
98
			case 'bool':
99
				return boolval( $data );
100
			case 'url':
101
				return esc_url( $data );
102
			case 'on':
103
				return ( 'on' == $data ? true : false );
104
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
105
			case 'closed':
106
				return ( 'closed' != $data ? true : false );
107
			case 'string':
108
				return strval( $data );
109
			case 'int':
110
				return ( is_numeric( $data ) ? intval( $data ) : 0 );
111
			case 'float':
112
				return ( is_numeric( $data ) ? floatval( $data ) : 0 );
113
			case 'array':
114
				return ( is_array( $data ) ? $data : array() );
115
			case 'rtrim-slash':
116
				return strval( rtrim( $data, '/' ) );
117
		}
118
		if (  is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) {
119
			return ( preg_match( substr( $type, 6 ), $data ) ? $data : null );
120
		} elseif ( is_array( $type ) ) {
121
			// Is the array associative?
122
			if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) {
123
				foreach ( $type as $item => $check ) {
124
					$data[ $item ] = self::validate( $data[ $item ], $check );
125
				}
126
				return $data;
127
			} else {
128
				// check if the value exists in the array if not return the first value.
129
				// Ex $type = array( 'open', 'closed' ); defaults to 'open'
130
				return ( in_array( $data, $type ) ? $data: $type[0] );
131
			}
132
		}
133
		// Don't check for validity here
134
		if ( 'no-validation' == $type ) {
135
			return $data;
136
		}
137
		return null;
138
	}
139
140 View Code Duplication
	static function validate_option( $data, $type = null ) {
141
		if ( is_null( $data ) ) {
142
			return $data;
143
		}
144
		switch( $type ) {
145
			case 'bool':
146
				return boolval( $data );
147
			case 'url':
148
				return esc_url( $data );
149
			case 'on':
150
				return ( 'on' == $data ? true : false );
151
				break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
152
			case 'closed':
153
				return ( 'closed' != $data ? true : false );
154
			case 'string':
155
				return strval( $data );
156
			case 'int':
157
				return ( is_numeric( $data ) ? intval( $data ) : 0 );
158
			case 'float':
159
				return ( is_numeric( $data ) ? floatval( $data ) : 0 );
160
			case 'array':
161
				return ( is_array( $data ) ? $data : array() );
162
			case 'rtrim-slash':
163
				return strval( rtrim( $data, '/' ) );
164
		}
165
		if (  is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) {
166
			return ( preg_match( substr( $type, 6 ), $data ) ? $data : null );
167
		} elseif ( is_array( $type ) ) {
168
			// Is the array associative?
169
			if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) {
170
				foreach ( $type as $item => $check ) {
171
					$data[ $item ] = self::validate( $data[ $item ], $check );
172
				}
173
				return $data;
174
			} else {
175
				// check if the value exists in the array if not return the first value.
176
				// Ex $type = array( 'open', 'closed' ); defaults to 'open'
177
				return ( in_array( $data, $type ) ? $data: $type[0] );
178
			}
179
		}
180
		// Don't check for validity here
181
		if ( 'no-validation' == $type ) {
182
			return $data;
183
		}
184
		return null;
185
	}
186
}
187