| 1 |
<?php |
| 2 |
/** |
| 3 |
* CartFlows API |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! class_exists( 'CartFlows_API' ) ) : |
| 10 |
|
| 11 |
/** |
| 12 |
* CartFlows API |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
class CartFlows_API { |
| 17 |
|
| 18 |
/** |
| 19 |
* Instance |
| 20 |
* |
| 21 |
* @access private |
| 22 |
* @var object Class object. |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
private static $instance; |
| 26 |
|
| 27 |
/** |
| 28 |
* Initiator |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* @return object initialized object of class. |
| 32 |
*/ |
| 33 |
public static function get_instance() { |
| 34 |
if ( ! isset( self::$instance ) ) { |
| 35 |
self::$instance = new self; |
| 36 |
} |
| 37 |
return self::$instance; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
*/ |
| 45 |
public function __construct() { |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get site URL. |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* |
| 53 |
* @return string Site URL. |
| 54 |
*/ |
| 55 |
public static function get_site_url() { |
| 56 |
return apply_filters( 'cartflows_templates_url', CARTFLOWS_TEMPLATES_URL ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get Client Site Templates Rest API URL. |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* |
| 64 |
* @return string API site URL. |
| 65 |
*/ |
| 66 |
public static function get_step_endpoint_url() { |
| 67 |
return self::get_site_url() . 'wp-json/wp/v2/' . CARTFLOWS_STEP_POST_TYPE . '/'; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get Client Site Category Rest API URL. |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
* |
| 75 |
* @return string API site URL. |
| 76 |
*/ |
| 77 |
public static function get_category_endpoint_url() { |
| 78 |
return self::get_site_url() . 'wp-json/wp/v2/' . CARTFLOWS_TAXONOMY_STEP_PAGE_BUILDER . '/'; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get API request URL. |
| 83 |
* |
| 84 |
* @since 1.0.0 |
| 85 |
* |
| 86 |
* @param string $api_base base of api request. |
| 87 |
* @return string API site URL. |
| 88 |
*/ |
| 89 |
public static function get_request_api_url( $api_base = '' ) { |
| 90 |
return self::get_site_url() . 'wp-json/' . CARTFLOWS_STEP_POST_TYPE . '/v1/' . $api_base; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* License Args. |
| 95 |
* |
| 96 |
* @return array License arguments. |
| 97 |
*/ |
| 98 |
public static function get_licence_args() { |
| 99 |
return apply_filters( 'cartflows_licence_args', array() ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get single demo. |
| 104 |
* |
| 105 |
* @since 1.0.0 |
| 106 |
* |
| 107 |
* @param string $site_id Template ID of the site. |
| 108 |
* @return array Template data. |
| 109 |
*/ |
| 110 |
public static function get_template( $site_id ) { |
| 111 |
// @codingStandardsIgnoreStart |
| 112 |
$request_params = array( |
| 113 |
'licence_args' => self::get_licence_args(), |
| 114 |
); |
| 115 |
// @codingStandardsIgnoreEnd |
| 116 |
|
| 117 |
$url = add_query_arg( $request_params, self::get_step_endpoint_url() . $site_id ); |
| 118 |
|
| 119 |
$api_args = array( |
| 120 |
'timeout' => 15, |
| 121 |
); |
| 122 |
|
| 123 |
$response = self::remote_get( $url, $api_args ); |
| 124 |
if ( $response['success'] ) { |
| 125 |
$template = $response['data']; |
| 126 |
return array( |
| 127 |
'title' => ( isset( $template['title']->rendered ) ) ? $template['title']->rendered : '', |
| 128 |
'post_meta' => ( isset( $template['post_meta'] ) ) ? $template['post_meta'] : '', |
| 129 |
'data' => $template, |
| 130 |
'message' => $response['message'], // __( 'Your API Key is not valid. Please add valid API Key.', 'cartflows' ), |
| 131 |
'success' => $response['success'], |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
return array( |
| 136 |
'title' => '', |
| 137 |
'post_meta' => array(), |
| 138 |
'message' => $response['message'], |
| 139 |
'data' => $response['data'], |
| 140 |
'success' => $response['success'], |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get Cloud Templates |
| 146 |
* |
| 147 |
* @since 1.0.0 |
| 148 |
* |
| 149 |
* @param array $args For selecting the demos (Search terms, pagination etc). |
| 150 |
* @return array CartFlows list. |
| 151 |
*/ |
| 152 |
public static function get_templates( $args = array() ) { |
| 153 |
|
| 154 |
$request_params = wp_parse_args( |
| 155 |
$args, |
| 156 |
array( |
| 157 |
'page' => '1', |
| 158 |
'per_page' => '15', |
| 159 |
) |
| 160 |
); |
| 161 |
|
| 162 |
$url = add_query_arg( $request_params, self::get_step_endpoint_url() ); |
| 163 |
|
| 164 |
$api_args = array( |
| 165 |
'timeout' => 15, |
| 166 |
); |
| 167 |
|
| 168 |
$response = self::remote_get( $url, $api_args ); |
| 169 |
|
| 170 |
if ( $response['success'] ) { |
| 171 |
$templates_data = $response['data']; |
| 172 |
$templates = array(); |
| 173 |
foreach ( $templates_data as $key => $template ) { |
| 174 |
|
| 175 |
if ( ! isset( $template->id ) ) { |
| 176 |
continue; |
| 177 |
} |
| 178 |
|
| 179 |
$templates[ $key ]['id'] = isset( $template->id ) ? esc_attr( $template->id ) : ''; |
| 180 |
$templates[ $key ]['slug'] = isset( $template->slug ) ? esc_attr( $template->slug ) : ''; |
| 181 |
$templates[ $key ]['link'] = isset( $template->link ) ? esc_url( $template->link ) : ''; |
| 182 |
$templates[ $key ]['date'] = isset( $template->date ) ? esc_attr( $template->date ) : ''; |
| 183 |
$templates[ $key ]['title'] = isset( $template->title->rendered ) ? esc_attr( $template->title->rendered ) : ''; |
| 184 |
$templates[ $key ]['featured_image_url'] = isset( $template->featured_image_url ) ? esc_url( $template->featured_image_url ) : ''; |
| 185 |
$templates[ $key ]['content'] = isset( $template->content->rendered ) ? strip_tags( $template->content->rendered ) : ''; |
| 186 |
$templates[ $key ]['post_meta'] = isset( $template->post_meta ) ? $template->post_meta : ''; |
| 187 |
} |
| 188 |
|
| 189 |
return array( |
| 190 |
'templates' => $templates, |
| 191 |
'templates_count' => $response['count'], |
| 192 |
'data' => $response, |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
return array( |
| 197 |
'templates' => array(), |
| 198 |
'templates_count' => 0, |
| 199 |
'data' => $response, |
| 200 |
); |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Get categories. |
| 206 |
* |
| 207 |
* @since 1.0.0 |
| 208 |
* @param array $args Arguments. |
| 209 |
* @return array Category data. |
| 210 |
*/ |
| 211 |
public static function get_categories( $args = array() ) { |
| 212 |
|
| 213 |
$request_params = apply_filters( |
| 214 |
'cartflows_categories_api_params', |
| 215 |
wp_parse_args( |
| 216 |
$args, |
| 217 |
array( |
| 218 |
'page' => '1', |
| 219 |
'per_page' => '15', |
| 220 |
) |
| 221 |
) |
| 222 |
); |
| 223 |
|
| 224 |
$url = add_query_arg( $request_params, self::get_category_endpoint_url() ); |
| 225 |
|
| 226 |
$api_args = apply_filters( |
| 227 |
'cartflows_api_args', |
| 228 |
array( |
| 229 |
'timeout' => 15, |
| 230 |
) |
| 231 |
); |
| 232 |
|
| 233 |
$response = self::remote_get( $url, $api_args ); |
| 234 |
|
| 235 |
if ( $response['success'] ) { |
| 236 |
$categories_data = $response['data']; |
| 237 |
$categories = array(); |
| 238 |
|
| 239 |
foreach ( $categories_data as $key => $category ) { |
| 240 |
if ( isset( $category->count ) && ! empty( $category->count ) ) { |
| 241 |
$categories[] = array( |
| 242 |
'id' => isset( $category->id ) ? absint( $category->id ) : 0, |
| 243 |
'count' => isset( $category->count ) ? absint( $category->count ) : 0, |
| 244 |
'description' => isset( $category->description ) ? $category->description : '', |
| 245 |
'link' => isset( $category->link ) ? esc_url( $category->link ) : '', |
| 246 |
'name' => isset( $category->name ) ? $category->name : '', |
| 247 |
'slug' => isset( $category->slug ) ? sanitize_text_field( $category->slug ) : '', |
| 248 |
'taxonomy' => isset( $category->taxonomy ) ? $category->taxonomy : '', |
| 249 |
'parent' => isset( $category->parent ) ? $category->parent : '', |
| 250 |
); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return array( |
| 255 |
'categories' => $categories, |
| 256 |
'categories_count' => $response['count'], |
| 257 |
'data' => $response, |
| 258 |
); |
| 259 |
} |
| 260 |
|
| 261 |
return array( |
| 262 |
'categories' => array(), |
| 263 |
'categories_count' => 0, |
| 264 |
'data' => $response, |
| 265 |
); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Remote GET API Request |
| 270 |
* |
| 271 |
* @since 1.0.0 |
| 272 |
* |
| 273 |
* @param string $url Target server API URL. |
| 274 |
* @param array $args Array of arguments for the API request. |
| 275 |
* @return mixed Return the API request result. |
| 276 |
*/ |
| 277 |
public static function remote_get( $url = '', $args = array() ) { |
| 278 |
$request = wp_remote_get( $url, $args ); |
| 279 |
return self::request( $request ); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Remote POST API Request |
| 284 |
* |
| 285 |
* @since 1.0.0 |
| 286 |
* |
| 287 |
* @param string $url Target server API URL. |
| 288 |
* @param array $args Array of arguments for the API request. |
| 289 |
* @return mixed Return the API request result. |
| 290 |
*/ |
| 291 |
public static function remote_post( $url = '', $args = array() ) { |
| 292 |
$request = wp_remote_post( $url, $args ); |
| 293 |
|
| 294 |
return self::request( $request ); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Site API Request |
| 299 |
* |
| 300 |
* @since 1.0.0 |
| 301 |
* |
| 302 |
* @param boolean $api_base Target server API URL. |
| 303 |
* @param array $args Array of arguments for the API request. |
| 304 |
* @return mixed Return the API request result. |
| 305 |
*/ |
| 306 |
public static function site_request( $api_base = '', $args = array() ) { |
| 307 |
|
| 308 |
$api_url = self::get_request_api_url( $api_base ); |
| 309 |
|
| 310 |
return self::remote_post( $api_url, $args ); |
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* API Request |
| 315 |
* |
| 316 |
* Handle the API request and return the result. |
| 317 |
* |
| 318 |
* @since 1.0.0 |
| 319 |
* |
| 320 |
* @param array $request Array of arguments for the API request. |
| 321 |
* @return mixed Return the API request result. |
| 322 |
*/ |
| 323 |
public static function request( $request ) { |
| 324 |
|
| 325 |
// Is WP Error? |
| 326 |
if ( is_wp_error( $request ) ) { |
| 327 |
return array( |
| 328 |
'success' => false, |
| 329 |
'message' => $request->get_error_message(), |
| 330 |
'data' => $request, |
| 331 |
'count' => 0, |
| 332 |
); |
| 333 |
} |
| 334 |
|
| 335 |
// Invalid response code. |
| 336 |
if ( wp_remote_retrieve_response_code( $request ) != 200 ) { |
| 337 |
return array( |
| 338 |
'success' => false, |
| 339 |
'message' => $request['response'], |
| 340 |
'data' => $request, |
| 341 |
'count' => 0, |
| 342 |
); |
| 343 |
} |
| 344 |
|
| 345 |
// Get body data. |
| 346 |
$body = wp_remote_retrieve_body( $request ); |
| 347 |
|
| 348 |
// Is WP Error? |
| 349 |
if ( is_wp_error( $body ) ) { |
| 350 |
return array( |
| 351 |
'success' => false, |
| 352 |
'message' => $body->get_error_message(), |
| 353 |
'data' => $request, |
| 354 |
'count' => 0, |
| 355 |
); |
| 356 |
} |
| 357 |
|
| 358 |
// Decode body content. |
| 359 |
$body_decoded = json_decode( $body ); |
| 360 |
|
| 361 |
return array( |
| 362 |
'success' => true, |
| 363 |
'message' => __( 'Request successfully processed!', 'cartflows' ), |
| 364 |
'data' => (array) $body_decoded, |
| 365 |
'count' => wp_remote_retrieve_header( $request, 'x-wp-total' ), |
| 366 |
); |
| 367 |
} |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Initialize class object with 'get_instance()' method |
| 373 |
*/ |
| 374 |
CartFlows_API::get_instance(); |
| 375 |
|
| 376 |
endif; |
| 377 |
|