Completed
Push — add/sync-rest-options ( 79f0db...12319b )
by
unknown
08:38
created

Jetpack_JSON_API_Get_Options_Endpoint::get()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
rs 8.8571
cc 6
eloc 14
nc 5
nop 1
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
	function result() {
24
		$args = $this->query_args();
25
		$data = array();
26
		if ( isset( $args['options'] ) && is_array( $args['options'] ) ) {
27
			$all_settings = $args['options'];
28
		} else {
29
			$type = isset( $args['options'] ) ? $args['options'] : 'all';
30
			switch( $type ) {
31
				case 'options':
32
					$all_settings = array_keys( self::$options );
33
					break;
34
				case 'mock_options':
35
					$all_settings = array_keys( self::$mock_options );
36
					break;
37
				case 'all':
38
				default:
39
					$all_settings = array_merge( array_keys( self::$options ), array_keys( self::$mock_options ), array_keys( self::$constants ) );
40
					break;
41
			}
42
		}
43
		foreach ( $all_settings as $name  ) {
44
			$data[ $name ] = self::get( $name );
45
		}
46
		return array( 'options' => $data );
47
	}
48
49
	/**
50
	 * Get individual setting
51
	 *
52
	 * @param  sting $name
53
	 * @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...
54
	 * @param  string or array $callback
55
	 * @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...
56
	 * @return value of the setting
57
	 */
58
	static function get( $name ) {
59
		// Options
60
		if ( array_key_exists( $name, self::$options ) ) {
61
			return self::validate_option( get_option( $name ), self::$options[ $name ] );
62
			// Mock options
63
		} elseif ( array_key_exists( $name, self::$mock_options ) ) {
64
			if ( is_callable( self::$mock_options[ $name ][ 'callback' ] ) ) {
65
				$args = array();
66
				if ( isset( self::$mock_options[ $name ][ 'callback_args' ] ) &&
67
				     is_array( self::$mock_options[ $name ][ 'callback_args' ] ) ) {
68
					$args = self::$mock_options[ $name ][ 'callback_args' ];
69
				}
70
				$data = call_user_func_array( self::$mock_options[ $name ][ 'callback' ], $args );
71
				return self::validate_option( $data, self::$mock_options[ $name ][ 'type' ] );
72
			} else {
73
				return new WP_Error( json_encode( self::$mock_options[ $name ][ 'callback' ] ) . ' can not be called' );
74
			}
75
		}
76
		return null;
77
	}
78
79 View Code Duplication
	static function validate( $data, $type = null ) {
80
		if ( is_null( $data ) ) {
81
			return $data;
82
		}
83
		switch( $type ) {
84
			case 'bool':
85
				return boolval( $data );
86
			case 'url':
87
				return esc_url( $data );
88
			case 'on':
89
				return ( 'on' == $data ? true : false );
90
				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...
91
			case 'closed':
92
				return ( 'closed' != $data ? true : false );
93
			case 'string':
94
				return strval( $data );
95
			case 'int':
96
				return ( is_numeric( $data ) ? intval( $data ) : 0 );
97
			case 'float':
98
				return ( is_numeric( $data ) ? floatval( $data ) : 0 );
99
			case 'array':
100
				return ( is_array( $data ) ? $data : array() );
101
			case 'rtrim-slash':
102
				return strval( rtrim( $data, '/' ) );
103
		}
104
		if (  is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) {
105
			return ( preg_match( substr( $type, 6 ), $data ) ? $data : null );
106
		} elseif ( is_array( $type ) ) {
107
			// Is the array associative?
108
			if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) {
109
				foreach ( $type as $item => $check ) {
110
					$data[ $item ] = self::validate( $data[ $item ], $check );
111
				}
112
				return $data;
113
			} else {
114
				// check if the value exists in the array if not return the first value.
115
				// Ex $type = array( 'open', 'closed' ); defaults to 'open'
116
				return ( in_array( $data, $type ) ? $data: $type[0] );
117
			}
118
		}
119
		// Don't check for validity here
120
		if ( 'no-validation' == $type ) {
121
			return $data;
122
		}
123
		return null;
124
	}
125
126 View Code Duplication
	static function validate_option( $data, $type = null ) {
127
		if ( is_null( $data ) ) {
128
			return $data;
129
		}
130
		switch( $type ) {
131
			case 'bool':
132
				return boolval( $data );
133
			case 'url':
134
				return esc_url( $data );
135
			case 'on':
136
				return ( 'on' == $data ? true : false );
137
				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...
138
			case 'closed':
139
				return ( 'closed' != $data ? true : false );
140
			case 'string':
141
				return strval( $data );
142
			case 'int':
143
				return ( is_numeric( $data ) ? intval( $data ) : 0 );
144
			case 'float':
145
				return ( is_numeric( $data ) ? floatval( $data ) : 0 );
146
			case 'array':
147
				return ( is_array( $data ) ? $data : array() );
148
			case 'rtrim-slash':
149
				return strval( rtrim( $data, '/' ) );
150
		}
151
		if (  is_string( $type ) && 'regex:' == substr( $type, 0, 6 ) ) {
152
			return ( preg_match( substr( $type, 6 ), $data ) ? $data : null );
153
		} elseif ( is_array( $type ) ) {
154
			// Is the array associative?
155
			if ( count( array_filter( array_keys( $type ), 'is_string' ) ) ) {
156
				foreach ( $type as $item => $check ) {
157
					$data[ $item ] = self::validate( $data[ $item ], $check );
158
				}
159
				return $data;
160
			} else {
161
				// check if the value exists in the array if not return the first value.
162
				// Ex $type = array( 'open', 'closed' ); defaults to 'open'
163
				return ( in_array( $data, $type ) ? $data: $type[0] );
164
			}
165
		}
166
		// Don't check for validity here
167
		if ( 'no-validation' == $type ) {
168
			return $data;
169
		}
170
		return null;
171
	}
172
}
173