| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * Create WordPress REST API functionality |
||||||
| 4 | * |
||||||
| 5 | * @class Object_Sync_Sf_Rest |
||||||
| 6 | * @package Object_Sync_Salesforce |
||||||
| 7 | */ |
||||||
| 8 | |||||||
| 9 | defined( 'ABSPATH' ) || exit; |
||||||
| 10 | |||||||
| 11 | /** |
||||||
| 12 | * Object_Sync_Sf_Rest class. |
||||||
| 13 | */ |
||||||
| 14 | class Object_Sync_Sf_Rest { |
||||||
| 15 | |||||||
| 16 | /** |
||||||
| 17 | * Current version of the plugin |
||||||
| 18 | * |
||||||
| 19 | * @var string |
||||||
| 20 | */ |
||||||
| 21 | public $version; |
||||||
| 22 | |||||||
| 23 | /** |
||||||
| 24 | * The main plugin file |
||||||
| 25 | * |
||||||
| 26 | * @var string |
||||||
| 27 | */ |
||||||
| 28 | public $file; |
||||||
| 29 | |||||||
| 30 | /** |
||||||
| 31 | * Global object of `$wpdb`, the WordPress database |
||||||
| 32 | * |
||||||
| 33 | * @var object |
||||||
| 34 | */ |
||||||
| 35 | public $wpdb; |
||||||
| 36 | |||||||
| 37 | /** |
||||||
| 38 | * The plugin's slug so we can include it when necessary |
||||||
| 39 | * |
||||||
| 40 | * @var string |
||||||
| 41 | */ |
||||||
| 42 | public $slug; |
||||||
| 43 | |||||||
| 44 | /** |
||||||
| 45 | * The plugin's prefix when saving options to the database |
||||||
| 46 | * |
||||||
| 47 | * @var string |
||||||
| 48 | */ |
||||||
| 49 | public $option_prefix; |
||||||
| 50 | |||||||
| 51 | /** |
||||||
| 52 | * Object_Sync_Sf_Mapping class |
||||||
| 53 | * |
||||||
| 54 | * @var object |
||||||
| 55 | */ |
||||||
| 56 | public $mappings; |
||||||
| 57 | |||||||
| 58 | /** |
||||||
| 59 | * Object_Sync_Sf_WordPress class |
||||||
| 60 | * |
||||||
| 61 | * @var object |
||||||
| 62 | */ |
||||||
| 63 | public $wordpress; |
||||||
| 64 | |||||||
| 65 | /** |
||||||
| 66 | * Object_Sync_Sf_Salesforce class |
||||||
| 67 | * This contains Salesforce API methods |
||||||
| 68 | * |
||||||
| 69 | * @var array |
||||||
| 70 | */ |
||||||
| 71 | public $salesforce; |
||||||
| 72 | |||||||
| 73 | /** |
||||||
| 74 | * Object_Sync_Sf_Salesforce_Push class |
||||||
| 75 | * |
||||||
| 76 | * @var object |
||||||
| 77 | */ |
||||||
| 78 | public $push; |
||||||
| 79 | |||||||
| 80 | /** |
||||||
| 81 | * Object_Sync_Sf_Salesforce_Pull class |
||||||
| 82 | * |
||||||
| 83 | * @var object |
||||||
| 84 | */ |
||||||
| 85 | public $pull; |
||||||
| 86 | |||||||
| 87 | /** |
||||||
| 88 | * Object_Sync_Sf_WordPress_Transient class |
||||||
| 89 | * |
||||||
| 90 | * @var object |
||||||
| 91 | */ |
||||||
| 92 | private $sfwp_transients; |
||||||
| 93 | |||||||
| 94 | /** |
||||||
| 95 | * The namespace for the REST endpoints |
||||||
| 96 | * |
||||||
| 97 | * @var string |
||||||
| 98 | */ |
||||||
| 99 | private $namespace; |
||||||
| 100 | |||||||
| 101 | /** |
||||||
| 102 | * Constructor for rest class |
||||||
| 103 | */ |
||||||
| 104 | public function __construct() { |
||||||
| 105 | $this->version = object_sync_for_salesforce()->version; |
||||||
| 106 | $this->file = object_sync_for_salesforce()->file; |
||||||
| 107 | $this->wpdb = object_sync_for_salesforce()->wpdb; |
||||||
| 108 | $this->slug = object_sync_for_salesforce()->slug; |
||||||
| 109 | $this->option_prefix = object_sync_for_salesforce()->option_prefix; |
||||||
| 110 | |||||||
| 111 | $this->mappings = object_sync_for_salesforce()->mappings; |
||||||
| 112 | $this->wordpress = object_sync_for_salesforce()->wordpress; |
||||||
| 113 | $this->salesforce = object_sync_for_salesforce()->salesforce; |
||||||
| 114 | $this->push = object_sync_for_salesforce()->push; |
||||||
| 115 | $this->pull = object_sync_for_salesforce()->pull; |
||||||
| 116 | |||||||
| 117 | $this->sfwp_transients = object_sync_for_salesforce()->wordpress->sfwp_transients; |
||||||
| 118 | |||||||
| 119 | $this->namespace = $this->slug; |
||||||
| 120 | |||||||
| 121 | $this->add_actions(); |
||||||
| 122 | |||||||
| 123 | } |
||||||
| 124 | |||||||
| 125 | /** |
||||||
| 126 | * Create the action hooks to create the reset methods |
||||||
| 127 | */ |
||||||
| 128 | public function add_actions() { |
||||||
| 129 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 130 | } |
||||||
| 131 | |||||||
| 132 | /** |
||||||
| 133 | * Register REST API routes |
||||||
| 134 | */ |
||||||
| 135 | public function register_routes() { |
||||||
| 136 | $namespace = $this->namespace; |
||||||
| 137 | $method_list = WP_REST_Server::ALLMETHODS; |
||||||
|
0 ignored issues
–
show
The type
WP_REST_Server was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 138 | register_rest_route( |
||||||
|
0 ignored issues
–
show
The function
register_rest_route was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 139 | $namespace, |
||||||
| 140 | '/(?P<class>([\w-])+)/', |
||||||
| 141 | array( |
||||||
| 142 | array( |
||||||
| 143 | 'methods' => $method_list, |
||||||
| 144 | 'args' => array( |
||||||
| 145 | 'class' => array( |
||||||
| 146 | 'validate_callback' => array( $this, 'check_class' ), |
||||||
| 147 | 'required' => true, |
||||||
| 148 | ), |
||||||
| 149 | 'salesforce_object_type' => array( |
||||||
| 150 | 'type' => 'string', |
||||||
| 151 | ), |
||||||
| 152 | 'salesforce_id' => array( |
||||||
| 153 | 'type' => 'string', |
||||||
| 154 | ), |
||||||
| 155 | 'wordpress_object_type' => array( |
||||||
| 156 | 'type' => 'string', |
||||||
| 157 | ), |
||||||
| 158 | ), |
||||||
| 159 | 'permission_callback' => array( $this, 'can_process' ), |
||||||
| 160 | 'callback' => array( $this, 'process' ), |
||||||
| 161 | ), |
||||||
| 162 | ) |
||||||
| 163 | ); |
||||||
| 164 | |||||||
| 165 | } |
||||||
| 166 | |||||||
| 167 | /** |
||||||
| 168 | * Check for a valid class from the parameter |
||||||
| 169 | * |
||||||
| 170 | * @param string $class check if the class is a real object. |
||||||
| 171 | * @return bool |
||||||
| 172 | */ |
||||||
| 173 | public function check_class( $class ) { |
||||||
| 174 | if ( is_object( $this->{ $class } ) ) { |
||||||
| 175 | return true; |
||||||
| 176 | } |
||||||
| 177 | return false; |
||||||
| 178 | } |
||||||
| 179 | |||||||
| 180 | /** |
||||||
| 181 | * Check for a valid ID from the parameter. This one is not in use yet. |
||||||
| 182 | * |
||||||
| 183 | * @param string $id check if the ID from the parameter is a real object. |
||||||
| 184 | * @return bool |
||||||
| 185 | */ |
||||||
| 186 | public function check_id( $id ) { |
||||||
| 187 | if ( is_object( $id ) ) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 188 | return true; |
||||||
| 189 | } |
||||||
| 190 | return false; |
||||||
| 191 | } |
||||||
| 192 | |||||||
| 193 | /** |
||||||
| 194 | * Check to see if the user has permission to do this |
||||||
| 195 | * |
||||||
| 196 | * @param WP_REST_Request $request the request object sent to the API. |
||||||
| 197 | */ |
||||||
| 198 | public function can_process( WP_REST_Request $request ) { |
||||||
|
0 ignored issues
–
show
The type
WP_REST_Request was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 199 | // unless we specify it here, the method will not be allowed unless the user has configure_salesforce capability. |
||||||
| 200 | $http_method = $request->get_method(); |
||||||
| 201 | $class = $request->get_url_params()['class']; |
||||||
| 202 | switch ( $class ) { |
||||||
| 203 | case 'salesforce': |
||||||
| 204 | if ( ! in_array( $http_method, explode( ',', WP_REST_Server::ALLMETHODS ), true ) ) { |
||||||
| 205 | return new WP_Error( 'rest_forbidden', esc_html__( 'This kind of request is not allowed.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) ); |
||||||
|
0 ignored issues
–
show
The function
esc_html__ was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The type
WP_Error was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 206 | } |
||||||
| 207 | if ( ! current_user_can( 'configure_salesforce' ) ) { |
||||||
|
0 ignored issues
–
show
The function
current_user_can was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 208 | return new WP_Error( 'rest_forbidden', esc_html__( 'You do not have permissions to view this data.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) ); |
||||||
| 209 | } |
||||||
| 210 | break; |
||||||
| 211 | case 'mappings': |
||||||
| 212 | if ( ! in_array( $http_method, explode( ',', WP_REST_Server::ALLMETHODS ), true ) ) { |
||||||
| 213 | return new WP_Error( 'rest_forbidden', esc_html__( 'This kind of request is not allowed.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) ); |
||||||
| 214 | } |
||||||
| 215 | if ( ! current_user_can( 'configure_salesforce' ) ) { |
||||||
| 216 | return new WP_Error( 'rest_forbidden', esc_html__( 'You do not have permissions to view this data.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) ); |
||||||
| 217 | } |
||||||
| 218 | break; |
||||||
| 219 | case 'pull': |
||||||
| 220 | if ( ! in_array( $http_method, array( 'GET', 'POST', 'PUT' ), true ) ) { |
||||||
| 221 | return new WP_Error( 'rest_forbidden', esc_html__( 'This kind of request is not allowed.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) ); |
||||||
| 222 | } |
||||||
| 223 | break; |
||||||
| 224 | case 'push': |
||||||
| 225 | if ( ! in_array( $http_method, array( 'POST', 'PUT' ), true ) ) { |
||||||
| 226 | return new WP_Error( 'rest_forbidden', esc_html__( 'This kind of request is not allowed.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) ); |
||||||
| 227 | } |
||||||
| 228 | break; |
||||||
| 229 | default: |
||||||
| 230 | if ( ! current_user_can( 'configure_salesforce' ) ) { |
||||||
| 231 | return new WP_Error( 'rest_forbidden', esc_html__( 'You do not have permissions to view this data.', 'object-sync-for-salesforce' ), array( 'status' => 401 ) ); |
||||||
| 232 | } |
||||||
| 233 | break; |
||||||
| 234 | } |
||||||
| 235 | return true; |
||||||
| 236 | } |
||||||
| 237 | |||||||
| 238 | /** |
||||||
| 239 | * Process the REST API request |
||||||
| 240 | * |
||||||
| 241 | * @param WP_REST_Request $request the request that was made. |
||||||
| 242 | * @return array $result |
||||||
| 243 | */ |
||||||
| 244 | public function process( WP_REST_Request $request ) { |
||||||
| 245 | // see methods: https://developer.wordpress.org/reference/classes/wp_rest_request/ |
||||||
| 246 | // use error_log( 'request is ' . print_r( $request, true ) ); to log the request. |
||||||
| 247 | $http_method = $request->get_method(); |
||||||
| 248 | $route = $request->get_route(); |
||||||
| 249 | $url_params = $request->get_url_params(); |
||||||
| 250 | $body_params = $request->get_body_params(); |
||||||
| 251 | $class = $request->get_url_params()['class']; |
||||||
| 252 | $api_call = str_replace( '/' . $this->namespace . $this->version . '/', '', $route ); |
||||||
| 253 | // use error_log( 'api call is ' . $api_call . ' and params are ' . print_r( $params, true ) ); to log more of the api call. |
||||||
| 254 | $result = ''; |
||||||
| 255 | switch ( $class ) { |
||||||
| 256 | case 'salesforce': |
||||||
| 257 | break; |
||||||
| 258 | case 'mappings': |
||||||
| 259 | break; |
||||||
| 260 | case 'pull': |
||||||
| 261 | if ( 'GET' === $http_method ) { |
||||||
| 262 | $result = $this->pull->salesforce_pull_webhook( $request ); |
||||||
| 263 | } |
||||||
| 264 | if ( 'POST' === $http_method && isset( $body_params['salesforce_object_type'] ) && isset( $body_params['salesforce_id'] ) ) { |
||||||
| 265 | $result = $this->pull->manual_pull( $body_params['salesforce_object_type'], $body_params['salesforce_id'] ); |
||||||
| 266 | } |
||||||
| 267 | break; |
||||||
| 268 | case 'push': |
||||||
| 269 | if ( ( 'POST' === $http_method || 'PUT' === $http_method || 'DELETE' === $http_method ) && isset( $body_params['wordpress_object_type'] ) && isset( $body_params['wordpress_id'] ) ) { |
||||||
| 270 | $result = $this->push->manual_push( $body_params['wordpress_object_type'], $body_params['wordpress_id'], $http_method ); |
||||||
| 271 | } |
||||||
| 272 | break; |
||||||
| 273 | } |
||||||
| 274 | |||||||
| 275 | return $result; |
||||||
| 276 | } |
||||||
| 277 | |||||||
| 278 | } |
||||||
| 279 |