| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* class used for APIs and Webhooks |
| 8 |
* |
| 9 |
* @since zohoflow 2.5.0 |
| 10 |
* @since weforms 1.6.23 |
| 11 |
*/ |
| 12 |
class Zoho_Flow_WeForms extends Zoho_Flow_Service{ |
| 13 |
|
| 14 |
/** |
| 15 |
* webhook events supported |
| 16 |
*/ |
| 17 |
public static $supported_events = array( "submission_added" ); |
| 18 |
|
| 19 |
/** |
| 20 |
* list forms |
| 21 |
* |
| 22 |
* @param WP_REST_Request $request WP_REST_Request onject. |
| 23 |
* |
| 24 |
* request params Optional. Arguments for querying forms. |
| 25 |
* @type int per_page Number of results per call. Default 10. |
| 26 |
* @type int page Index of page. Default 1. |
| 27 |
* @type string order Sort order. DESC | ASC. Default DESC. |
| 28 |
* @type string orderby Sort field. Default post_date. |
| 29 |
* @type string status Status of the form. Default any. |
| 30 |
* @type string search search query string. |
| 31 |
* |
| 32 |
* @return array Array of form details. |
| 33 |
*/ |
| 34 |
public function list_forms( $request ){ |
| 35 |
$form_controller = New Weforms_Forms_Controller(); |
| 36 |
$forms = $form_controller->get_items( $request ); |
| 37 |
return rest_ensure_response( $forms ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* list form fields |
| 42 |
* |
| 43 |
* @param WP_REST_Request $request WP_REST_Request onject. |
| 44 |
* |
| 45 |
* request path param Mandatory. |
| 46 |
* @type int form_id Form ID to retrive the fields for. |
| 47 |
* |
| 48 |
* @return array|WP_Error array of form field details | WP_Error object with error details. |
| 49 |
*/ |
| 50 |
public function list_form_fields( $request ){ |
| 51 |
$form_obj = New WeForms_Form( $request['form_id'] ); |
| 52 |
if( 0 === $form_obj->get_id() ){ |
| 53 |
return new WP_Error( 'rest_bad_request', 'Invalid form ID', array( 'status' => 404 ) ); |
| 54 |
} |
| 55 |
return rest_ensure_response( $form_obj->get_fields() ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Check whether the Form ID is valid or not. |
| 60 |
* |
| 61 |
* @param int $form_id Form ID. |
| 62 |
* |
| 63 |
* @return bool true if the form exists | false for others. |
| 64 |
*/ |
| 65 |
private function is_valid_form_id( $form_id ){ |
| 66 |
if( isset( $form_id ) ){ |
| 67 |
$form_obj = New WeForms_Form( $form_id ); |
| 68 |
if( ( 0 === $form_obj->get_id() ) || ( 'wpuf_contact_form' != $form_obj->data->post_type ) ){ |
| 69 |
return false; |
| 70 |
} |
| 71 |
return true; |
| 72 |
} |
| 73 |
else{ |
| 74 |
return false; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Creates a webhook entry |
| 80 |
* The events available in $supported_events array only accepted |
| 81 |
* |
| 82 |
* @param WP_REST_Request $request WP_REST_Request onject. |
| 83 |
* |
| 84 |
* @return array|WP_Error Array with Webhook ID | WP_Error object with error details. |
| 85 |
*/ |
| 86 |
public function create_webhook( $request ){ |
| 87 |
$entry = json_decode( $request->get_body() ); |
| 88 |
if( ( !isset( $entry->form_id ) ) || !$this->is_valid_form_id( $entry->form_id ) ){ |
| 89 |
return new WP_Error( 'rest_bad_request', "Form does not exist!", array( 'status' => 400 ) ); |
| 90 |
} |
| 91 |
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 ) ) ){ |
| 92 |
$args = array( |
| 93 |
'name' => $entry->name, |
| 94 |
'url' => $entry->url, |
| 95 |
'event' => $entry->event, |
| 96 |
'form_id' => $entry->form_id |
| 97 |
); |
| 98 |
$post_name = "Happyforms "; |
| 99 |
$post_id = $this->create_webhook_post( $post_name, $args ); |
| 100 |
if( is_wp_error( $post_id ) ){ |
| 101 |
$errors = $post_id->get_error_messages(); |
| 102 |
return new WP_Error( 'rest_bad_request', $errors, array( 'status' => 400 ) ); |
| 103 |
} |
| 104 |
return rest_ensure_response( |
| 105 |
array( |
| 106 |
'webhook_id' => $post_id |
| 107 |
) |
| 108 |
); |
| 109 |
} |
| 110 |
else{ |
| 111 |
return new WP_Error( 'rest_bad_request', 'Data validation failed', array( 'status' => 400 ) ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Deletes a webhook entry |
| 117 |
* Webhook ID returned from webhook create event should be used. Use minimum user scope. |
| 118 |
* |
| 119 |
* @param WP_REST_Request $request WP_REST_Request onject. |
| 120 |
* |
| 121 |
* @return array|WP_Error Array with success message | WP_Error object with error details. |
| 122 |
*/ |
| 123 |
public function delete_webhook( $request ){ |
| 124 |
$webhook_id = $request['webhook_id']; |
| 125 |
if( is_numeric( $webhook_id ) ){ |
| 126 |
$webhook_post = $this->get_webhook_post( $webhook_id ); |
| 127 |
if( !empty( $webhook_post[0]->ID ) ){ |
| 128 |
$delete_webhook = $this->delete_webhook_post( $webhook_id ); |
| 129 |
if( is_wp_error( $delete_webhook ) ){ |
| 130 |
$errors = $delete_webhook->get_error_messages(); |
| 131 |
return new WP_Error( 'rest_bad_request', $errors, array( 'status' => 400 ) ); |
| 132 |
} |
| 133 |
else{ |
| 134 |
return rest_ensure_response( array( 'message' => 'Success' ) ); |
| 135 |
} |
| 136 |
} |
| 137 |
else{ |
| 138 |
return new WP_Error( 'rest_bad_request', 'Invalid webhook ID', array( 'status' => 400 ) ); |
| 139 |
} |
| 140 |
} |
| 141 |
else{ |
| 142 |
return new WP_Error( 'rest_bad_request', 'Invalid webhook ID', array( 'status' => 400 ) ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Fires once a message is succesfully submitted. |
| 148 |
* |
| 149 |
* @param int $entry_id Form entry ID. |
| 150 |
* @param int $form_id Form ID. |
| 151 |
* @param int $page_id Page ID. |
| 152 |
* |
| 153 |
* @return array|null Webhook payload. event, and submission details | null if criteria not match. |
| 154 |
*/ |
| 155 |
public function payload_submission_added( $entry_id, $form_id, $page_id, $form_settings ){ |
| 156 |
$args = array( |
| 157 |
'event' => 'submission_added', |
| 158 |
'form_id' => $form_id |
| 159 |
); |
| 160 |
$webhooks = $this->get_webhook_posts( $args ); |
| 161 |
if( !empty( $webhooks ) ){ |
| 162 |
$form = New WeForms_Form($form_id); |
| 163 |
$form_entry = New WeForms_Form_Entry($entry_id, $form); |
| 164 |
$form_fields = $form_entry->get_fields(); |
| 165 |
$form_field_details = array(); |
| 166 |
foreach ($form_fields as $field_obj) { |
| 167 |
$field_key = $field_obj['name'].'_'.$field_obj['id']; |
| 168 |
$form_field_details[$field_key] = $field_obj['value']; |
| 169 |
} |
| 170 |
$entry_details = $form_entry->get_metadata(); |
| 171 |
$entry_details['page_id'] = $page_id; |
| 172 |
$event_data = array( |
| 173 |
'event' => 'submission_added', |
| 174 |
'data' => array( |
| 175 |
'entry_data' => $entry_details, |
| 176 |
'field_data' => $form_field_details |
| 177 |
) |
| 178 |
); |
| 179 |
foreach( $webhooks as $webhook ){ |
| 180 |
$url = $webhook->url; |
| 181 |
zoho_flow_execute_webhook( $url, $event_data, array() ); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* default API |
| 188 |
* Get user and system info. |
| 189 |
* |
| 190 |
* @return array|WP_Error System and logged in user details | WP_Error object with error details. |
| 191 |
*/ |
| 192 |
public function get_system_info(){ |
| 193 |
$system_info = parent::get_system_info(); |
| 194 |
if( ! function_exists( 'get_plugin_data' ) ){ |
| 195 |
require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 196 |
} |
| 197 |
$plugin_dir = ABSPATH . 'wp-content/plugins/weforms/weforms.php'; |
| 198 |
if(file_exists( $plugin_dir ) ){ |
| 199 |
$plugin_data = get_plugin_data( $plugin_dir ); |
| 200 |
$system_info['weforms'] = $plugin_data['Version']; |
| 201 |
} |
| 202 |
return rest_ensure_response( $system_info ); |
| 203 |
} |
| 204 |
} |
| 205 |
|