| 1 |
<?php |
| 2 |
|
| 3 |
namespace NotificationX\Core\Rest; |
| 4 |
|
| 5 |
use NotificationX\Core\PostType; |
| 6 |
use NotificationX\Core\REST; |
| 7 |
use NotificationX\Extensions\ExtensionFactory; |
| 8 |
use NotificationX\Extensions\GlobalFields; |
| 9 |
use NotificationX\GetInstance; |
| 10 |
use NotificationX\NotificationX; |
| 11 |
use WP_REST_Controller; |
| 12 |
use WP_REST_Response; |
| 13 |
use WP_REST_Server; |
| 14 |
use WP_Error; |
| 15 |
|
| 16 |
/** |
| 17 |
* @method static Integration get_instance($args = null) |
| 18 |
*/ |
| 19 |
class Integration { |
| 20 |
/** |
| 21 |
* Instance of NotificationX |
| 22 |
* |
| 23 |
* @var NotificationX |
| 24 |
*/ |
| 25 |
use GetInstance; |
| 26 |
|
| 27 |
public $namespace; |
| 28 |
public $rest_base; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor. |
| 32 |
* |
| 33 |
* @since 4.7.0 |
| 34 |
* |
| 35 |
* @param string $post_type Post type. |
| 36 |
*/ |
| 37 |
public function __construct() { |
| 38 |
$this->namespace = 'notificationx/v1'; |
| 39 |
$this->rest_base = 'notification'; |
| 40 |
add_action('rest_api_init', [$this, 'register_routes']); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Registers the routes for the objects of the controller. |
| 45 |
* |
| 46 |
* @since 4.7.0 |
| 47 |
* |
| 48 |
* @see register_rest_route() |
| 49 |
*/ |
| 50 |
public function register_routes() { |
| 51 |
// Settings Integration |
| 52 |
register_rest_route( $this->namespace, '/api-connect', array( |
| 53 |
'methods' => WP_REST_Server::EDITABLE, |
| 54 |
'callback' => array( $this, 'api_connect' ), |
| 55 |
'permission_callback' => array($this, 'settings_permission'), |
| 56 |
)); |
| 57 |
|
| 58 |
// calls from integration provider. |
| 59 |
register_rest_route( |
| 60 |
$this->namespace, |
| 61 |
'/' . $this->rest_base . '/(?P<id>[\d]+)', |
| 62 |
array( |
| 63 |
array( |
| 64 |
'methods' => WP_REST_Server::READABLE, |
| 65 |
'callback' => array($this, 'get_response'), |
| 66 |
'permission_callback' => '__return_true', |
| 67 |
'args' => array( |
| 68 |
'id' => array( |
| 69 |
'required' => true, |
| 70 |
'description' => __('Unique identifier for the object.', 'notificationx'), |
| 71 |
'type' => 'integer', |
| 72 |
), |
| 73 |
'api_key' => array( |
| 74 |
'required' => true, |
| 75 |
'description' => __('Unique identifier for the site.', 'notificationx'), |
| 76 |
'type' => 'string', |
| 77 |
), |
| 78 |
), |
| 79 |
), |
| 80 |
array( |
| 81 |
'methods' => WP_REST_Server::CREATABLE, |
| 82 |
'callback' => array($this, 'save_response'), |
| 83 |
'permission_callback' => '__return_true', |
| 84 |
'args' => array( |
| 85 |
'id' => array( |
| 86 |
'required' => true, |
| 87 |
'description' => __('Unique identifier for the object.', 'notificationx'), |
| 88 |
'type' => 'integer', |
| 89 |
), |
| 90 |
'api_key' => array( |
| 91 |
'required' => true, |
| 92 |
'description' => __('Unique identifier for the site.', 'notificationx'), |
| 93 |
'type' => 'string', |
| 94 |
), |
| 95 |
), |
| 96 |
), |
| 97 |
) |
| 98 |
); |
| 99 |
// OLD Fallback for Zapier |
| 100 |
register_rest_route( |
| 101 |
"notificationx", |
| 102 |
'/' . $this->rest_base . '/(?P<id>[\d]+)', |
| 103 |
array( |
| 104 |
array( |
| 105 |
'methods' => WP_REST_Server::READABLE, |
| 106 |
'callback' => array($this, 'get_response'), |
| 107 |
'permission_callback' => '__return_true', |
| 108 |
'args' => array( |
| 109 |
'id' => array( |
| 110 |
'required' => true, |
| 111 |
'description' => __('Unique identifier for the object.', 'notificationx'), |
| 112 |
'type' => 'integer', |
| 113 |
), |
| 114 |
'api_key' => array( |
| 115 |
'required' => true, |
| 116 |
'description' => __('Unique identifier for the site.', 'notificationx'), |
| 117 |
'type' => 'string', |
| 118 |
), |
| 119 |
), |
| 120 |
), |
| 121 |
array( |
| 122 |
'methods' => WP_REST_Server::CREATABLE, |
| 123 |
'callback' => array($this, 'save_response'), |
| 124 |
'permission_callback' => '__return_true', |
| 125 |
'args' => array( |
| 126 |
'id' => array( |
| 127 |
'required' => true, |
| 128 |
'description' => __('Unique identifier for the object.', 'notificationx'), |
| 129 |
'type' => 'integer', |
| 130 |
), |
| 131 |
'api_key' => array( |
| 132 |
'required' => true, |
| 133 |
'description' => __('Unique identifier for the site.', 'notificationx'), |
| 134 |
'type' => 'string', |
| 135 |
), |
| 136 |
), |
| 137 |
), |
| 138 |
) |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
public function get_response( \WP_REST_Request $request ){ |
| 143 |
$id = $request['id']; |
| 144 |
$api_key = $request['api_key']; |
| 145 |
$error = []; |
| 146 |
|
| 147 |
if( $api_key === md5( home_url( '', 'http' ) ) || $api_key === md5( home_url( '', 'https' ) ) ) { |
| 148 |
$notificationx = PostType::get_instance()->get_post( $id ); |
| 149 |
if( $notificationx ) { |
| 150 |
return wp_send_json( true ); |
| 151 |
} |
| 152 |
$error['message'] = sprintf( __( 'There is no notification created with this id: %s', 'notificationx' ), $id ); |
| 153 |
return wp_send_json_error( $error, 401 ); |
| 154 |
} else { |
| 155 |
$error['message'] = __( 'Error: API Key Invalid!', 'notificationx' ); |
| 156 |
return wp_send_json_error( $error, 401 ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Undocumented function |
| 162 |
* |
| 163 |
* @param \WP_REST_Request $request |
| 164 |
* @return void |
| 165 |
*/ |
| 166 |
public function save_response( \WP_REST_Request $request ){ |
| 167 |
$response_data = array( |
| 168 |
'data' => '', |
| 169 |
'error' => false |
| 170 |
); |
| 171 |
|
| 172 |
if ( ! isset( $request['api_key'] ) ) { |
| 173 |
$response_data['error'] = __('Error: You should provide an API key.', 'notificationx'); |
| 174 |
} else { |
| 175 |
if( md5( home_url( '', 'http' ) ) != $request['api_key'] && md5( home_url( '', 'https' ) ) != $request['api_key'] ) { |
| 176 |
$response_data['error'] = __('Error: Invalid API key.', 'notificationx'); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if ( ! $response_data['error'] ) { |
| 181 |
$response_data['data'] = $request->get_params(); |
| 182 |
if ( isset( $response_data['data']['api_key'] ) ) { |
| 183 |
unset( $response_data['data']['api_key'] ); |
| 184 |
} |
| 185 |
if (isset($response_data['data']['id'])){ |
| 186 |
$post = PostType::get_instance()->get_post($response_data['data']['id']); |
| 187 |
if($post['source']){ |
| 188 |
do_action( "nx_api_response_success_{$post['source']}", $response_data['data'] ); |
| 189 |
} |
| 190 |
} |
| 191 |
do_action( 'nx_api_response_success', $response_data['data'] ); |
| 192 |
} |
| 193 |
|
| 194 |
return apply_filters( 'nx_api_response', $response_data ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Undocumented function |
| 199 |
* |
| 200 |
* @param \WP_REST_Request $request |
| 201 |
* @return |
| 202 |
*/ |
| 203 |
public function api_connect( \WP_REST_Request $request ){ |
| 204 |
$params = $request->get_params(); |
| 205 |
$source = !empty($params['source']) ? $params['source'] : ''; |
| 206 |
/** |
| 207 |
* @var Extension |
| 208 |
*/ |
| 209 |
$ext = ExtensionFactory::get_instance()->get($source); |
| 210 |
if($ext && method_exists($ext, 'connect')){ |
| 211 |
return $ext->connect($params); |
| 212 |
} |
| 213 |
else{ |
| 214 |
$result = apply_filters("nx_api_connect_$source", null, $params); |
| 215 |
if($result){ |
| 216 |
return $result; |
| 217 |
} |
| 218 |
} |
| 219 |
return REST::get_instance()->error(); |
| 220 |
} |
| 221 |
|
| 222 |
public function settings_permission( $request ) { |
| 223 |
return current_user_can('edit_notificationx_settings'); |
| 224 |
} |
| 225 |
} |
| 226 |
|