| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
use Jet_Engine\Modules\Custom_Content_Types\Module; |
| 7 |
|
| 8 |
/** |
| 9 |
* class used for APIs and Webhooks |
| 10 |
* |
| 11 |
* @since zohoflow 2.10.0 |
| 12 |
* @since jetengine 3.5.8 |
| 13 |
*/ |
| 14 |
class Zoho_Flow_JetEngine extends Zoho_Flow_Service{ |
| 15 |
|
| 16 |
/** |
| 17 |
* @var array Webhook events supported. |
| 18 |
*/ |
| 19 |
public static $supported_events = array( |
| 20 |
"cct_entry_added", |
| 21 |
"cct_entry_updated", |
| 22 |
"cct_entry_added_or_updated", |
| 23 |
"form_entry_submitted", |
| 24 |
"cpt_entry_added_or_updated", |
| 25 |
"cpt_entry_status_changed" |
| 26 |
); |
| 27 |
|
| 28 |
/** |
| 29 |
* List custom content types |
| 30 |
* |
| 31 |
* @param WP_REST_Request $request WP_REST_Request object. |
| 32 |
* |
| 33 |
* Request path param Mandatory. |
| 34 |
* @type int $limit Number of results. Default: 200. |
| 35 |
* @type string $order_by List order by the field. Default: id. |
| 36 |
* @type string $order List order Values: ASC|DESC. Default: DESC. |
| 37 |
* |
| 38 |
* @return WP_REST_Response WP_REST_Response array with CCT details |
| 39 |
*/ |
| 40 |
public function list_cct( $request ){ |
| 41 |
|
| 42 |
global $wpdb; |
| 43 |
|
| 44 |
$order_by_allowed = array( |
| 45 |
'id', |
| 46 |
'title' |
| 47 |
); |
| 48 |
$order_allowed = array('ASC', 'DESC'); |
| 49 |
|
| 50 |
$order_by = ( isset( $request['order_by'] ) && in_array( $request['order_by'], $order_by_allowed, true ) ) ? $request['order_by'] : 'id'; |
| 51 |
$order = ( isset( $request['order'] ) && in_array( $request['order'], $order_allowed, true ) ) ? $request['order'] : 'DESC'; |
| 52 |
$limit = isset( $request['limit'] ) ? absint( $request['limit'] ) : 200; |
| 53 |
$order_by_sql = esc_sql( $order_by ); |
| 54 |
$order_sql = esc_sql( $order ); |
| 55 |
|
| 56 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table read is required here and must return live content type data. |
| 57 |
$results = $wpdb->get_results( |
| 58 |
$wpdb->prepare( |
| 59 |
"SELECT id, slug, status, labels, args FROM {$wpdb->prefix}jet_post_types WHERE status = %s ORDER BY " . $order_by_sql . ' ' . $order_sql . ' LIMIT %d', // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Dynamic ORDER BY identifiers are allowlisted and escaped. |
| 60 |
'content-type', |
| 61 |
$limit |
| 62 |
), 'ARRAY_A' |
| 63 |
); |
| 64 |
|
| 65 |
foreach ( $results as $index => $field ){ |
| 66 |
|
| 67 |
$results[ $index ]['labels'] = maybe_unserialize( $field['labels'], true ); |
| 68 |
$results[ $index ]['args'] = maybe_unserialize( $field['args'], true ); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
return rest_ensure_response( $results ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Fetch custom content type details |
| 77 |
* |
| 78 |
* @param WP_REST_Request $request WP_REST_Request object. |
| 79 |
* @return WP_REST_Response WP_REST_Response CCT details |
| 80 |
*/ |
| 81 |
public function fetch_cct( $request ){ |
| 82 |
|
| 83 |
$cct_slug = $request->get_url_params()['cct_slug']; |
| 84 |
$cct = $this->is_valid_cct( $cct_slug ); |
| 85 |
|
| 86 |
if( $cct ){ |
| 87 |
return rest_ensure_response( $cct ); |
| 88 |
} |
| 89 |
|
| 90 |
return new WP_Error( 'rest_bad_request', "Content type does not exist!", array( 'status' => 404 ) ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Add new CCT entry |
| 95 |
* |
| 96 |
* @param WP_REST_Request $request WP_REST_Request object. |
| 97 |
* @return WP_REST_Response|WP_Error WP_REST_Response Item details. |
| 98 |
*/ |
| 99 |
public function add_cct_entry( $request ){ |
| 100 |
|
| 101 |
$content_type = Module::instance()->manager->get_content_types( $request->get_url_params()['cct_slug'] ); |
| 102 |
|
| 103 |
if( $content_type ){ |
| 104 |
|
| 105 |
$entry_data = $request->get_params(); |
| 106 |
unset( $entry_data['cct_slug'] ); |
| 107 |
$handler = $content_type->get_item_handler(); |
| 108 |
$item_id = $handler->update_item( $entry_data ); |
| 109 |
|
| 110 |
if( $item_id ){ |
| 111 |
|
| 112 |
$item_details = $content_type->db->get_item( $item_id ); |
| 113 |
$item_details['id'] = $item_details['_ID']; |
| 114 |
|
| 115 |
return rest_ensure_response( $item_details ); |
| 116 |
} |
| 117 |
|
| 118 |
return new WP_Error( 'rest_bad_request', "Content type item not added!", array( 'status' => 400 ) ); |
| 119 |
|
| 120 |
} |
| 121 |
return new WP_Error( 'rest_bad_request', "Content type does not exist!", array( 'status' => 404 ) ); |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Update CCT entry |
| 127 |
* |
| 128 |
* @param WP_REST_Request $request WP_REST_Request object. |
| 129 |
* @return WP_REST_Response|WP_Error WP_REST_Response Item details. |
| 130 |
*/ |
| 131 |
public function update_cct_entry( $request ){ |
| 132 |
|
| 133 |
$content_type = Module::instance()->manager->get_content_types( $request->get_url_params()['cct_slug'] ); |
| 134 |
|
| 135 |
if( $content_type ){ |
| 136 |
|
| 137 |
$entry_data = $request->get_params(); |
| 138 |
unset( $entry_data['cct_slug'] ); |
| 139 |
$handler = $content_type->get_item_handler(); |
| 140 |
$item_id = $handler->update_item( $entry_data ); |
| 141 |
|
| 142 |
if( $item_id ){ |
| 143 |
|
| 144 |
$item_details = $content_type->db->get_item( $item_id ); |
| 145 |
$item_details['id'] = $item_details['_ID']; |
| 146 |
|
| 147 |
return rest_ensure_response( $item_details ); |
| 148 |
} |
| 149 |
|
| 150 |
return new WP_Error( 'rest_bad_request', "Content type item not updated!", array( 'status' => 400 ) ); |
| 151 |
|
| 152 |
} |
| 153 |
return new WP_Error( 'rest_bad_request', "Content type does not exist!", array( 'status' => 404 ) ); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Fetch CCT entry |
| 159 |
* |
| 160 |
* @param WP_REST_Request $request WP_REST_Request object. |
| 161 |
* @return WP_REST_Response|WP_Error WP_REST_Response Item details. |
| 162 |
*/ |
| 163 |
public function get_cct_entry( $request ){ |
| 164 |
|
| 165 |
$content_type = Module::instance()->manager->get_content_types( $request->get_url_params()['cct_slug'] ); |
| 166 |
|
| 167 |
if( $content_type ){ |
| 168 |
|
| 169 |
$item_details = $content_type->db->get_item( $request->get_url_params()['_ID'] ); |
| 170 |
|
| 171 |
if( $item_details ){ |
| 172 |
|
| 173 |
$item_details['id'] = $item_details['_ID']; |
| 174 |
|
| 175 |
return rest_ensure_response( $item_details ); |
| 176 |
} |
| 177 |
|
| 178 |
return new WP_Error( 'rest_bad_request', "Item does not exist!", array( 'status' => 404 ) ); |
| 179 |
|
| 180 |
} |
| 181 |
return new WP_Error( 'rest_bad_request', "Content type does not exist!", array( 'status' => 404 ) ); |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
private function is_valid_cct( $cct_slug ){ |
| 186 |
|
| 187 |
if( isset( $cct_slug ) && is_string( $cct_slug )){ |
| 188 |
|
| 189 |
global $wpdb; |
| 190 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table read is required here to validate the current content type. |
| 191 |
$result = $wpdb->get_row( |
| 192 |
$wpdb->prepare( |
| 193 |
"SELECT * FROM {$wpdb->prefix}jet_post_types WHERE `slug` = %s AND `status` = %s", |
| 194 |
$cct_slug, |
| 195 |
'content-type' |
| 196 |
), 'ARRAY_A' |
| 197 |
); |
| 198 |
|
| 199 |
if( !empty( $result ) ){ |
| 200 |
|
| 201 |
$result['labels'] = maybe_unserialize( $result['labels'], true ); |
| 202 |
$result['args'] = maybe_unserialize( $result['args'], true ); |
| 203 |
$result['meta_fields'] = maybe_unserialize( $result['meta_fields'], true ); |
| 204 |
return $result; |
| 205 |
|
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* List custom post type details |
| 214 |
* |
| 215 |
* @param WP_REST_Request $request WP_REST_Request object. |
| 216 |
* |
| 217 |
* Request path param Mandatory. |
| 218 |
* @type int $limit Number of results. Default: 200. |
| 219 |
* @type string $order_by List order by the field. Default: id. |
| 220 |
* @type string $order List order Values: ASC|DESC. Default: DESC. |
| 221 |
* |
| 222 |
* @return WP_REST_Response WP_REST_Response array with CPT details |
| 223 |
*/ |
| 224 |
public function list_cpt( $request ){ |
| 225 |
|
| 226 |
global $wpdb; |
| 227 |
|
| 228 |
$order_by_allowed = array( |
| 229 |
'id', |
| 230 |
'title' |
| 231 |
); |
| 232 |
$order_allowed = array('ASC', 'DESC'); |
| 233 |
|
| 234 |
$order_by = ( isset( $request['order_by'] ) && in_array( $request['order_by'], $order_by_allowed, true ) ) ? $request['order_by'] : 'id'; |
| 235 |
$order = ( isset( $request['order'] ) && in_array( $request['order'], $order_allowed, true ) ) ? $request['order'] : 'DESC'; |
| 236 |
$limit = isset( $request['limit'] ) ? absint( $request['limit'] ) : 200; |
| 237 |
$order_by_sql = esc_sql( $order_by ); |
| 238 |
$order_sql = esc_sql( $order ); |
| 239 |
|
| 240 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table read is required here and must return live post type data. |
| 241 |
$results = $wpdb->get_results( |
| 242 |
$wpdb->prepare( |
| 243 |
"SELECT id, slug, status, labels, args FROM {$wpdb->prefix}jet_post_types WHERE status = %s ORDER BY " . $order_by_sql . ' ' . $order_sql . ' LIMIT %d', // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Dynamic ORDER BY identifiers are allowlisted and escaped. |
| 244 |
'publish', |
| 245 |
$limit |
| 246 |
), 'ARRAY_A' |
| 247 |
); |
| 248 |
|
| 249 |
foreach ( $results as $index => $field ){ |
| 250 |
|
| 251 |
$results[ $index ]['labels'] = maybe_unserialize( $field['labels'], true ); |
| 252 |
$results[ $index ]['args'] = maybe_unserialize( $field['args'], true ); |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
return rest_ensure_response( $results ); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Fetch custom post type details |
| 261 |
* |
| 262 |
* @param WP_REST_Request $request WP_REST_Request object. |
| 263 |
* @return WP_REST_Response WP_REST_Response CPT details |
| 264 |
*/ |
| 265 |
public function fetch_cpt( $request ){ |
| 266 |
|
| 267 |
$cpt_slug = $request->get_url_params()['cpt_slug']; |
| 268 |
$cpt = $this->is_valid_cpt( $cpt_slug ); |
| 269 |
|
| 270 |
if( $cpt ){ |
| 271 |
|
| 272 |
return rest_ensure_response( $cpt ); |
| 273 |
|
| 274 |
} |
| 275 |
|
| 276 |
return new WP_Error( 'rest_bad_request', "Post type does not exist!", array( 'status' => 404 ) ); |
| 277 |
} |
| 278 |
|
| 279 |
private function is_valid_cpt( $cpt_slug ){ |
| 280 |
|
| 281 |
if( isset( $cpt_slug ) && is_string( $cpt_slug )){ |
| 282 |
|
| 283 |
global $wpdb; |
| 284 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table read is required here to validate the current post type. |
| 285 |
$result = $wpdb->get_row( |
| 286 |
$wpdb->prepare( |
| 287 |
"SELECT * FROM {$wpdb->prefix}jet_post_types WHERE `slug` = %s AND `status` = %s", |
| 288 |
$cpt_slug, |
| 289 |
'publish' |
| 290 |
), 'ARRAY_A' |
| 291 |
); |
| 292 |
|
| 293 |
if( !empty( $result ) ){ |
| 294 |
|
| 295 |
$result['labels'] = maybe_unserialize( $result['labels'], true ); |
| 296 |
$result['args'] = maybe_unserialize( $result['args'], true ); |
| 297 |
$result['meta_fields'] = maybe_unserialize( $result['meta_fields'], true ); |
| 298 |
return $result; |
| 299 |
|
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
return false; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* List forms |
| 308 |
* |
| 309 |
* @param WP_REST_Request $request WP_REST_Request object. |
| 310 |
* |
| 311 |
* request params Optional. Arguments for querying forms. |
| 312 |
* @type int limit Number of forms to query for. Default: 200. |
| 313 |
* @type string $order_by Forms list order by the field. Default: post_modified. |
| 314 |
* @type string $order Form list order Values: ASC|DESC. Default: DESC. |
| 315 |
* |
| 316 |
* @return WP_REST_Response|WP_Error WP_REST_Response array of form details | WP_Error object with error details. |
| 317 |
*/ |
| 318 |
public function list_forms( $request ){ |
| 319 |
|
| 320 |
$args = array( |
| 321 |
"post_type" => "jet-engine-booking", |
| 322 |
"numberposts" => ($request['limit']) ? $request['limit'] : '200', |
| 323 |
"orderby" => ($request['order_by']) ? $request['order_by'] : 'post_modified', |
| 324 |
"order" => ($request['order']) ? $request['order'] : 'DESC', |
| 325 |
); |
| 326 |
|
| 327 |
$forms_list = get_posts( $args ); |
| 328 |
$forms_return_list = array(); |
| 329 |
|
| 330 |
foreach ( $forms_list as $form ){ |
| 331 |
|
| 332 |
$forms_return_list[] = array( |
| 333 |
"ID" => $form->ID, |
| 334 |
"post_title" => $form->post_title, |
| 335 |
"post_status" => $form->post_status, |
| 336 |
"post_author" => $form->post_author, |
| 337 |
"post_date" => $form->post_date, |
| 338 |
"post_modified" => $form->post_modified |
| 339 |
); |
| 340 |
} |
| 341 |
|
| 342 |
return rest_ensure_response( $forms_return_list ); |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* List form fields |
| 347 |
* |
| 348 |
* @param WP_REST_Request $request WP_REST_Request object. |
| 349 |
* |
| 350 |
* Request path param Mandatory. |
| 351 |
* @type int form_id Form ID to retrive the fields for. |
| 352 |
* |
| 353 |
* @return WP_REST_Response|WP_Error WP_REST_Response array with form field details | WP_Error object with error details. |
| 354 |
*/ |
| 355 |
public function list_form_fields( $request ){ |
| 356 |
|
| 357 |
$form_id = $request['form_id']; |
| 358 |
|
| 359 |
if( $this->is_valid_form( $form_id ) ){ |
| 360 |
|
| 361 |
$form_fields = get_post_meta( $form_id, '_form_data' ); |
| 362 |
$form_data = $form_fields[0]; |
| 363 |
$parsed_data = json_decode( wp_unslash( $form_data ), true ); |
| 364 |
|
| 365 |
if ( ! $parsed_data ) { |
| 366 |
$parsed_data = json_decode( $form_data, true ); |
| 367 |
} |
| 368 |
|
| 369 |
if ( ! $parsed_data ) { |
| 370 |
$parsed_data = array(); |
| 371 |
} |
| 372 |
|
| 373 |
foreach ( $parsed_data as $index => $value ) { |
| 374 |
$parsed_data[ $index ]['i'] = '' . $value['i']; |
| 375 |
} |
| 376 |
|
| 377 |
return rest_ensure_response( $parsed_data ); |
| 378 |
} |
| 379 |
|
| 380 |
return new WP_Error( 'rest_bad_request', "Form does not exist!", array( 'status' => 404 ) ); |
| 381 |
} |
| 382 |
|
| 383 |
private function is_valid_form( $form_id ){ |
| 384 |
|
| 385 |
if( isset( $form_id ) ){ |
| 386 |
|
| 387 |
if( "jet-engine-booking" === get_post_type( $form_id ) ){ |
| 388 |
return true; |
| 389 |
} |
| 390 |
|
| 391 |
return false; |
| 392 |
} |
| 393 |
else{ |
| 394 |
|
| 395 |
return false; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
private function get_cpt_entry( $post_id ){ |
| 400 |
|
| 401 |
if( isset( $post_id ) && is_numeric( $post_id ) ){ |
| 402 |
|
| 403 |
$post = get_post( $post_id, 'ARRAY_A'); |
| 404 |
|
| 405 |
if( $post ){ |
| 406 |
|
| 407 |
unset( $post['post_password'] ); |
| 408 |
$post['meta'] = array(); |
| 409 |
$post_meta = get_post_meta($post_id); |
| 410 |
|
| 411 |
foreach ($post_meta as $meta_key => $meta_value ) { |
| 412 |
if( is_array( $meta_value ) ){ |
| 413 |
if( 1 < sizeof( $meta_value ) ){ |
| 414 |
|
| 415 |
$post['meta'][$meta_key] = array(); |
| 416 |
|
| 417 |
foreach ($meta_value as $value) { |
| 418 |
$post['meta'][$meta_key][] = maybe_unserialize( $value ); |
| 419 |
} |
| 420 |
} |
| 421 |
else{ |
| 422 |
$post['meta'][$meta_key] = maybe_unserialize( $meta_value[0] ); |
| 423 |
} |
| 424 |
} |
| 425 |
else{ |
| 426 |
$post['meta'][$meta_key] = maybe_unserialize( $meta_value ); |
| 427 |
} |
| 428 |
|
| 429 |
} |
| 430 |
|
| 431 |
return $post; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
return false; |
| 436 |
} |
| 437 |
|
| 438 |
private function upsert_option( $option_name, $option_value ){ |
| 439 |
|
| 440 |
if( isset( $option_name ) && isset( $option_value ) && is_string( $option_name ) && is_string( $option_value ) ){ |
| 441 |
|
| 442 |
$option = get_option( $option_name ); |
| 443 |
|
| 444 |
if( $option ){ |
| 445 |
|
| 446 |
if( is_array( $option ) ){ |
| 447 |
|
| 448 |
if( !in_array( $option_value, $option ) ){ |
| 449 |
$option[] = $option_value; |
| 450 |
} |
| 451 |
} |
| 452 |
else{ |
| 453 |
|
| 454 |
$option = array( $option_value ); |
| 455 |
} |
| 456 |
|
| 457 |
update_option( $option_name, $option ); |
| 458 |
} |
| 459 |
else{ |
| 460 |
|
| 461 |
$option = array( $option_value ); |
| 462 |
add_option( $option_name, $option ); |
| 463 |
} |
| 464 |
|
| 465 |
return get_option( $option_name ); |
| 466 |
} |
| 467 |
return false; |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* Creates a webhook entry |
| 472 |
* The events available in $supported_events array only accepted |
| 473 |
* |
| 474 |
* @param WP_REST_Request $request WP_REST_Request object. |
| 475 |
* @return WP_REST_Response|WP_Error WP_REST_Response array with Webhook ID | WP_Error object with error details. |
| 476 |
*/ |
| 477 |
public function create_webhook( $request ){ |
| 478 |
$entry = json_decode( $request->get_body() ); |
| 479 |
if( ( isset( $entry->name ) ) && ( isset( $entry->url ) ) && ( isset( $entry->event ) ) && ( in_array( $entry->event, self::$supported_events ) ) && ( preg_match( "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $entry->url ) ) ){ |
| 480 |
$args = array( |
| 481 |
'name' => $entry->name, |
| 482 |
'url' => $entry->url, |
| 483 |
'event' => $entry->event |
| 484 |
); |
| 485 |
|
| 486 |
if( 'cct_entry_added' === $entry->event || 'cct_entry_updated' === $entry->event || 'cct_entry_added_or_updated' === $entry->event ){ |
| 487 |
|
| 488 |
if( ( !isset( $entry->cct_slug ) ) || !$this->is_valid_cct( $entry->cct_slug ) ){ |
| 489 |
return new WP_Error( 'rest_bad_request', "CCT does not exist!", array( 'status' => 400 ) ); |
| 490 |
} |
| 491 |
|
| 492 |
$options = false; |
| 493 |
|
| 494 |
if( 'cct_entry_added' === $entry->event ){ |
| 495 |
$options = $this->upsert_option( 'ZF_JetEngine_CCT_item_added', 'jet-engine/custom-content-types/created-item/'.$entry->cct_slug ); |
| 496 |
} |
| 497 |
elseif( 'cct_entry_updated' === $entry->event ){ |
| 498 |
$options = $this->upsert_option( 'ZF_JetEngine_CCT_item_updated', 'jet-engine/custom-content-types/updated-item/'.$entry->cct_slug ); |
| 499 |
} |
| 500 |
elseif( 'cct_entry_added_or_updated' === $entry->event ){ |
| 501 |
$options = $this->upsert_option( 'ZF_JetEngine_CCT_item_added_or_updated', 'jet-engine/custom-content-types/updated-item/'.$entry->cct_slug ); |
| 502 |
} |
| 503 |
|
| 504 |
if( !$options ){ |
| 505 |
return new WP_Error( 'rest_bad_request', "Option not added", array( 'status' => 400 ) ); |
| 506 |
} |
| 507 |
|
| 508 |
$args['cct_slug'] = $entry->cct_slug; |
| 509 |
} |
| 510 |
|
| 511 |
elseif( 'form_entry_submitted' === $entry->event ){ |
| 512 |
|
| 513 |
if( ( !isset( $entry->form_id ) ) || !$this->is_valid_form( $entry->form_id ) ){ |
| 514 |
return new WP_Error( 'rest_bad_request', "Form does not exist!", array( 'status' => 400 ) ); |
| 515 |
} |
| 516 |
|
| 517 |
$args['form_id'] = $entry->form_id; |
| 518 |
} |
| 519 |
|
| 520 |
elseif( 'cpt_entry_added_or_updated' === $entry->event || 'cpt_entry_status_changed' === $entry->event ){ |
| 521 |
|
| 522 |
if( ( !isset( $entry->cpt_slug ) ) || !$this->is_valid_cpt( $entry->cpt_slug ) ){ |
| 523 |
return new WP_Error( 'rest_bad_request', "CPT does not exist!", array( 'status' => 400 ) ); |
| 524 |
} |
| 525 |
|
| 526 |
$options = false; |
| 527 |
|
| 528 |
if( 'cpt_entry_added_or_updated' === $entry->event ){ |
| 529 |
$options = $this->upsert_option( 'ZF_JetEngine_CPT_item_added_or_updated', 'save_post_'.$entry->cpt_slug ); |
| 530 |
if( !$options ){ |
| 531 |
return new WP_Error( 'rest_bad_request', "Option not added", array( 'status' => 400 ) ); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
$args['cpt_slug'] = $entry->cpt_slug; |
| 536 |
} |
| 537 |
$post_name = "JetEngine "; |
| 538 |
$post_id = $this->create_webhook_post( $post_name, $args ); |
| 539 |
if( is_wp_error( $post_id ) ){ |
| 540 |
$errors = $post_id->get_error_messages(); |
| 541 |
return new WP_Error( 'rest_bad_request', $errors, array( 'status' => 400 ) ); |
| 542 |
} |
| 543 |
return rest_ensure_response( |
| 544 |
array( |
| 545 |
'webhook_id' => $post_id |
| 546 |
) ); |
| 547 |
} |
| 548 |
else{ |
| 549 |
return new WP_Error( 'rest_bad_request', 'Data validation failed', array( 'status' => 400 ) ); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
/** |
| 554 |
* Deletes a webhook entry |
| 555 |
* Webhook ID returned from webhook create event should be used. Use minimum user scope. |
| 556 |
* |
| 557 |
* @param WP_REST_Request $request WP_REST_Request object. |
| 558 |
* @return WP_REST_Response|WP_Error WP_REST_Response array with success message | WP_Error object with error details. |
| 559 |
*/ |
| 560 |
public function delete_webhook( $request ){ |
| 561 |
$webhook_id = $request->get_url_params()['webhook_id']; |
| 562 |
if( is_numeric( $webhook_id ) ){ |
| 563 |
$webhook_post = $this->get_webhook_post( $webhook_id ); |
| 564 |
if( !empty( $webhook_post[0]->ID ) ){ |
| 565 |
$delete_webhook = $this->delete_webhook_post( $webhook_id ); |
| 566 |
if( is_wp_error( $delete_webhook ) ){ |
| 567 |
$errors = $delete_webhook->get_error_messages(); |
| 568 |
return new WP_Error( 'rest_bad_request', $errors, array( 'status' => 400 ) ); |
| 569 |
} |
| 570 |
else{ |
| 571 |
return rest_ensure_response( array( 'message' => 'Success' ) ); |
| 572 |
} |
| 573 |
} |
| 574 |
else{ |
| 575 |
return new WP_Error( 'rest_bad_request', 'Invalid webhook ID', array( 'status' => 400 ) ); |
| 576 |
} |
| 577 |
} |
| 578 |
else{ |
| 579 |
return new WP_Error( 'rest_bad_request', 'Invalid webhook ID', array( 'status' => 400 ) ); |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
/** |
| 584 |
* Fires after cct entry is added. |
| 585 |
* |
| 586 |
* @param array $item Entry details. |
| 587 |
* @param int $item_id Entry ID. |
| 588 |
* @param Jet_Engine\Modules\Custom_Content_Types\Item_Handler $item_handler Item_Handler |
| 589 |
*/ |
| 590 |
public function payload_cct_entry_added( $item, $item_id, $item_handler ){ |
| 591 |
$cct_slug = ($item_handler->get_factory())->args['slug']; |
| 592 |
$args = array( |
| 593 |
'event' => 'cct_entry_added', |
| 594 |
'cct_slug' => $cct_slug |
| 595 |
); |
| 596 |
$webhooks = $this->get_webhook_posts( $args ); |
| 597 |
if( !empty( $webhooks ) ){ |
| 598 |
$item['id'] = $item_id; |
| 599 |
$event_data = array( |
| 600 |
'event' => 'cct_entry_added', |
| 601 |
'data' => $item |
| 602 |
); |
| 603 |
foreach( $webhooks as $webhook ){ |
| 604 |
$event_data['id'] = $webhook->ID; |
| 605 |
$url = $webhook->url; |
| 606 |
zoho_flow_execute_webhook( $url, $event_data, array() ); |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
/** |
| 612 |
* Fires after cct entry is updated. |
| 613 |
* |
| 614 |
* @param array $item Entry details. |
| 615 |
* @param array $prev_item Entry previous details. |
| 616 |
* @param Jet_Engine\Modules\Custom_Content_Types\Item_Handler $item_handler Item_Handler |
| 617 |
*/ |
| 618 |
public function payload_cct_entry_updated( $item, $prev_item, $item_handler ){ |
| 619 |
if( is_array( $prev_item ) && !empty( $prev_item ) ){ |
| 620 |
$cct_slug = ($item_handler->get_factory())->args['slug']; |
| 621 |
$args = array( |
| 622 |
'event' => 'cct_entry_updated', |
| 623 |
'cct_slug' => $cct_slug |
| 624 |
); |
| 625 |
$webhooks = $this->get_webhook_posts( $args ); |
| 626 |
if( !empty( $webhooks ) ){ |
| 627 |
$item['id'] = $item['_ID']; |
| 628 |
$event_data = array( |
| 629 |
'event' => 'cct_entry_updated', |
| 630 |
'data' => $item |
| 631 |
); |
| 632 |
foreach( $webhooks as $webhook ){ |
| 633 |
$event_data['id'] = $webhook->ID; |
| 634 |
$url = $webhook->url; |
| 635 |
zoho_flow_execute_webhook( $url, $event_data, array() ); |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* Fires after cct entry is added or updated. |
| 643 |
* |
| 644 |
* @param array $item Entry details. |
| 645 |
* @param array $prev_item Entry previous details. |
| 646 |
* @param Jet_Engine\Modules\Custom_Content_Types\Item_Handler $item_handler Item_Handler |
| 647 |
*/ |
| 648 |
public function payload_cct_entry_added_or_updated( $item, $prev_item, $item_handler ){ |
| 649 |
$cct_slug = ($item_handler->get_factory())->args['slug']; |
| 650 |
$args = array( |
| 651 |
'event' => 'cct_entry_added_or_updated', |
| 652 |
'cct_slug' => $cct_slug |
| 653 |
); |
| 654 |
$webhooks = $this->get_webhook_posts( $args ); |
| 655 |
if( !empty( $webhooks ) ){ |
| 656 |
$item['id'] = $item['_ID']; |
| 657 |
$event_data = array( |
| 658 |
'event' => 'cct_entry_added_or_updated', |
| 659 |
'data' => $item |
| 660 |
); |
| 661 |
foreach( $webhooks as $webhook ){ |
| 662 |
$event_data['id'] = $webhook->ID; |
| 663 |
$url = $webhook->url; |
| 664 |
zoho_flow_execute_webhook( $url, $event_data, array() ); |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
/** |
| 670 |
* Fires after entry is processed. |
| 671 |
* |
| 672 |
* @param array $handler Form handler. |
| 673 |
*/ |
| 674 |
public function payload_form_entry_submitted( $handler ){ |
| 675 |
$form_id = $handler->form; |
| 676 |
$args = array( |
| 677 |
'event' => 'form_entry_submitted', |
| 678 |
'form_id' => $form_id |
| 679 |
); |
| 680 |
$webhooks = $this->get_webhook_posts( $args ); |
| 681 |
if( !empty( $webhooks ) ){ |
| 682 |
$entry_details = $handler->notifcations->data; |
| 683 |
$event_data = array( |
| 684 |
'event' => 'form_entry_submitted', |
| 685 |
'data' => $entry_details |
| 686 |
); |
| 687 |
foreach( $webhooks as $webhook ){ |
| 688 |
$event_data['id'] = $webhook->ID; |
| 689 |
$url = $webhook->url; |
| 690 |
zoho_flow_execute_webhook( $url, $event_data, array() ); |
| 691 |
} |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
/** |
| 696 |
* Fires after cpt entry is added or updated. |
| 697 |
* |
| 698 |
* @param int $post_id CPT post ID. |
| 699 |
* @param WP_Post $post CPT post object. |
| 700 |
* @param boolean $update Flag for update. |
| 701 |
*/ |
| 702 |
public function payload_cpt_entry_added_or_updated( $post_id, $post, $update ){ |
| 703 |
$args = array( |
| 704 |
'event' => 'cpt_entry_added_or_updated', |
| 705 |
'cpt_slug' => get_post_type( $post ) |
| 706 |
); |
| 707 |
$webhooks = $this->get_webhook_posts( $args ); |
| 708 |
if( !empty( $webhooks ) ){ |
| 709 |
$entry_details = $this->get_cpt_entry( $post_id ); |
| 710 |
$event_data = array( |
| 711 |
'event' => 'cpt_entry_added_or_updated', |
| 712 |
'data' => $entry_details |
| 713 |
); |
| 714 |
foreach( $webhooks as $webhook ){ |
| 715 |
$event_data['id'] = $webhook->ID; |
| 716 |
$url = $webhook->url; |
| 717 |
zoho_flow_execute_webhook( $url, $event_data, array() ); |
| 718 |
} |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
/** |
| 723 |
* Fires after cpt entry status is updated. |
| 724 |
* |
| 725 |
* @param string $new_status New status of the post. |
| 726 |
* @param string $old_status Old status of the post. |
| 727 |
* @param WP_Post $post CPT post object. |
| 728 |
*/ |
| 729 |
public function payload_cpt_entry_status_changed( $new_status, $old_status, $post ){ |
| 730 |
if( $new_status !== $old_status ){ |
| 731 |
$args = array( |
| 732 |
'event' => 'cpt_entry_status_changed', |
| 733 |
'cpt_slug' => get_post_type( $post ) |
| 734 |
); |
| 735 |
$webhooks = $this->get_webhook_posts( $args ); |
| 736 |
if( !empty( $webhooks ) ){ |
| 737 |
$entry_details = $this->get_cpt_entry( $post->ID ); |
| 738 |
$entry_details['old_status'] = $old_status; |
| 739 |
$event_data = array( |
| 740 |
'event' => 'cpt_entry_status_changed', |
| 741 |
'data' => $entry_details |
| 742 |
); |
| 743 |
foreach( $webhooks as $webhook ){ |
| 744 |
$event_data['id'] = $webhook->ID; |
| 745 |
$url = $webhook->url; |
| 746 |
zoho_flow_execute_webhook( $url, $event_data, array() ); |
| 747 |
} |
| 748 |
} |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
/** |
| 753 |
* Default API |
| 754 |
* Get user and system info. |
| 755 |
* |
| 756 |
* @return array|WP_Error System and logged in user details | WP_Error object with error details. |
| 757 |
*/ |
| 758 |
public function get_system_info(){ |
| 759 |
$system_info = parent::get_system_info(); |
| 760 |
if( ! function_exists( 'get_plugin_data' ) ){ |
| 761 |
require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 762 |
} |
| 763 |
$plugin_dir = ABSPATH . 'wp-content/plugins/jet-engine/jet-engine.php'; |
| 764 |
if(file_exists( $plugin_dir ) ){ |
| 765 |
$plugin_data = get_plugin_data( $plugin_dir ); |
| 766 |
$system_info['jetengine'] = $plugin_data['Version']; |
| 767 |
} |
| 768 |
return rest_ensure_response( $system_info ); |
| 769 |
} |
| 770 |
} |