Completed
Push — suppress-initial-full-sync ( b2fcb0...689669 )
by
unknown
31:08 queued 22:05
created

Jetpack_JSON_API_Endpoint::callback()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 10
nop 3
dl 0
loc 22
rs 8.9457
c 0
b 0
f 0
1
<?php
2
3
include JETPACK__PLUGIN_DIR . '/modules/module-info.php';
4
5
/**
6
 * Base class for Jetpack Endpoints, has the validate_call helper function.
7
 */
8
abstract class Jetpack_JSON_API_Endpoint extends WPCOM_JSON_API_Endpoint {
9
10
	protected $needed_capabilities;
11
	protected $expected_actions = array();
12
	protected $action;
13
14
15
	public function callback( $path = '', $blog_id = 0, $object = null ) {
16
		$debug = debug_backtrace();
17
18
		for ( $i = 0; $i < 5; $i++ ) {
19
			error_log("{$debug[$i]['function']} was called from line {$debug[$i]['line']} of {$debug[$i]['file']}");
20
		}
21
		if ( is_wp_error( $error = $this->validate_call( $blog_id, $this->needed_capabilities ) ) ) {
22
			return $error;
23
		}
24
25
		if ( is_wp_error( $error = $this->validate_input( $object ) ) ) {
26
			return $error;
27
		}
28
29
		if ( ! empty( $this->action ) ) {
30
			if( is_wp_error( $error = call_user_func( array( $this, $this->action ) ) ) ) {
31
				return $error;
32
			}
33
		}
34
35
		return $this->result();
36
	}
37
38
	abstract protected function result();
39
40
	protected function validate_input( $object ) {
41
		$args = $this->input();
42
43
		if( isset( $args['action'] ) && $args['action'] == 'update' ) {
44
			$this->action = 'update';
45
		}
46
47
		if ( preg_match( "/\/update\/?$/", $this->path ) ) {
48
			$this->action = 'update';
49
50
		} elseif( preg_match( "/\/install\/?$/", $this->path ) ) {
51
			$this->action = 'install';
52
53
		} elseif( ! empty( $args['action'] ) ) {
54
			if( ! in_array( $args['action'], $this->expected_actions ) ) {
55
				return new WP_Error( 'invalid_action', __( 'You must specify a valid action', 'jetpack' ) );
56
			}
57
			$this->action =  $args['action'];
58
		}
59
		return true;
60
	}
61
62
	/**
63
	 * Switches to the blog and checks current user capabilities.
64
	 * @return bool|WP_Error a WP_Error object or true if things are good.
65
	 */
66
	protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) {
67
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $_blog_id ) );
68
		if ( is_wp_error( $blog_id ) ) {
69
			return $blog_id;
70
		}
71
72
		if ( is_wp_error( $error = $this->check_capability( $capability ) ) ) {
73
			return $error;
74
		}
75
76
		if ( $check_manage_active &&  'GET' !== $this->method && ! Jetpack::is_module_active( 'manage' ) ) {
77
			return new WP_Error( 'unauthorized_full_access', __( 'Full management mode is off for this site.', 'jetpack' ), 403 );
78
		}
79
80
		return true;
81
	}
82
83
	/**
84
	 * @param $capability
85
	 *
86
	 * @return bool|WP_Error
87
	 */
88
	protected function check_capability( $capability ) {
89
		if ( is_array( $capability ) ) {
90
			// the idea is that the we can pass in an array of capabilitie that the user needs to have before we allowing them to do something
91
			$capabilities = ( isset( $capability['capabilities'] ) ? $capability['capabilities'] : $capability );
92
93
			// We can pass in the number of conditions we must pass by default it is all.
94
			$must_pass = ( isset( $capability['must_pass'] ) && is_int( $capability['must_pass'] ) ? $capability['must_pass'] : count( $capabilities ) );
95
96
			$failed = array(); // store the failed capabilities
97
			$passed = 0; //
98
99
			foreach ( $capabilities as $cap ) {
100
				if ( current_user_can( $cap ) ) {
101
					$passed ++;
102
				} else {
103
					$failed[] = $cap;
104
				}
105
			}
106
			// Check that must have conditions is less then
107 View Code Duplication
			if ( $passed < $must_pass ) {
108
				return new WP_Error( 'unauthorized', sprintf( __( 'This user is not authorized to %s on this blog.', 'jetpack' ), implode( ', ', $failed ), 403 ) );
109
			}
110
111 View Code Duplication
		} else {
112
			if ( !current_user_can( $capability ) ) {
113
				return new WP_Error( 'unauthorized', sprintf( __( 'This user is not authorized to %s on this blog.', 'jetpack' ), $capability ), 403 );
114
			}
115
		}
116
117
		return true;
118
	}
119
120
}
121