Layers_API

Layers API Class.

Defined (1)

The class is defined in the following location(s).

/core/helpers/api.php  
  1. class Layers_API { 
  2.  
  3. private static $instance; 
  4.  
  5. private static $type; 
  6.  
  7. public $sort_options; 
  8.  
  9. const ENVATO_API_URL = 'https://api.envato.com/v1/'; 
  10.  
  11. /** 
  12. * Initiator 
  13. */ 
  14.  
  15. public static function get_instance() { 
  16. if ( ! isset( self::$instance ) ) { 
  17. self::$instance = new Layers_API(); 
  18. return self::$instance; 
  19.  
  20. /** 
  21. * Constructor 
  22. */ 
  23.  
  24. public function __construct() { 
  25.  
  26. // hook add_query_vars function into query_vars 
  27. add_filter('query_vars', array( $this, 'add_query_vars' ) ); 
  28.  
  29.  
  30. function add_query_vars($vars) { 
  31. $vars[] = "type"; 
  32. return $vars; 
  33.  
  34. public function get_auth_token( $method = 'basic' ) { 
  35. if( 'basic' == $method ) { 
  36. $token = 'LNjwlQxdcf95fzYWXYF8XqNlnUrofwDU'; 
  37. return $token; 
  38.  
  39. public function get_sort_options() { 
  40.  
  41. $this->sort_options = array( 
  42. 'updated-desc' => array( 
  43. 'label' => __( 'Last Updated' , 'layerswp' ),  
  44. 'excerpt-label' => __( 'by last updated' , 'layerswp' ),  
  45. ),  
  46. 'id-desc' => array( 
  47. 'label' => __( 'Newest to Oldest' , 'layerswp' ),  
  48. 'excerpt-label' => __( 'by release date' , 'layerswp' ),  
  49. ),  
  50. 'name-asc' => array( 
  51. 'label' => __( 'Item Name A - Z' , 'layerswp' ),  
  52. 'excerpt-label' => __( 'alphabetically' , 'layerswp' ),  
  53. ),  
  54. 'sales-desc' => array( 
  55. 'label' => __( 'Best Sellers' , 'layerswp' ),  
  56. 'excerpt-label' => __( 'by highest sales' , 'layerswp' ),  
  57. ),  
  58. 'rating-desc' => array( 
  59. 'label' => __( 'Best Rated' , 'layerswp' ),  
  60. 'excerpt-label' => __( 'by highest rating' , 'layerswp' ),  
  61. ),  
  62. 'price-asc' => array( 
  63. 'label' => __( 'Price: Low to Ligh' , 'layerswp' ),  
  64. 'excerpt-label' => __( 'by least expensive' , 'layerswp' ),  
  65. ),  
  66. 'price-desc' => array( 
  67. 'label' => __( 'Price: High to Low' , 'layerswp' ),  
  68. 'excerpt-label' => __( 'by most expensive' , 'layerswp' ),  
  69. ),  
  70. 'trending-desc' => array( 
  71. 'label' => __( 'Trending Items' , 'layerswp' ),  
  72. 'excerpt-label' => __( 'by trending' , 'layerswp' ),  
  73. ),  
  74. ); 
  75.  
  76. return $this->sort_options; 
  77.  
  78. private function do_envato_api_call( $endpoint = 'market/total-items.json', $query_string = NULL , $method = 'get', $timeout = 5 ) { 
  79.  
  80. $default_query_string = 'page_size=100&sort_by=updated&sort_direction=desc'; 
  81.  
  82. $query_string = ( $query_string ? '?' . $query_string . '&' . $default_query_string : '?' . $default_query_string ); 
  83.  
  84. // Set the remote URL 
  85. $remote_url = self::ENVATO_API_URL . $endpoint . $query_string; 
  86.  
  87. // Set the query transient key 
  88. $cache_key = 'lmp_' . $query_string; 
  89.  
  90. // Quick cache dumper 
  91. $dump_cache = 0; 
  92. if( 1 == $dump_cache ) delete_transient( $cache_key ); 
  93.  
  94. // Return a cached version of the query if we have one 
  95. if( FALSE !== get_transient( $cache_key ) ) { 
  96. return get_transient( $cache_key ); 
  97.  
  98. // Set the Auth token for our query 
  99. $remote_args = array( 
  100. 'timeout' => $timeout,  
  101. 'headers' => array( 
  102. 'Authorization' => 'Bearer ' . $this->get_auth_token() 
  103. ); 
  104.  
  105. // Choose a method 
  106. if( 'get' == $method ) { 
  107. $remote_query = wp_remote_get( $remote_url, $remote_args ); 
  108. } else { 
  109. $remote_query = wp_remote_post( $remote_url, $remote_args ); 
  110.  
  111. if( is_wp_error( $remote_query ) ) { 
  112.  
  113. // If there's an error, we handle it on the front end so just return it 
  114. return $remote_query; 
  115.  
  116. } else if( isset( $remote_query[ 'response' ][ 'code' ] ) && 200 == $remote_query[ 'response' ][ 'code' ] ) { 
  117.  
  118. // Cache a successful query 
  119. set_transient( $cache_key , wp_remote_retrieve_body( $remote_query ), 60 ); 
  120.  
  121. return wp_remote_retrieve_body( $remote_query ); 
  122. } else { 
  123.  
  124. // If the response code isn't right, throw an error 
  125. return new WP_Error( __( 'Error' , 'layerswp' ) , __( 'Something broke and we can\'t load the stream' , 'layerswp' ) ); 
  126.  
  127. public function get_product_list( $marketplace = 'layerswp' , $type = 'themes' ) { 
  128.  
  129. if( 'layerswp' == $marketplace ) { 
  130. $product_list = $this->get_layers_list( $type ); 
  131. } else { 
  132. $product_list = $this->get_envato_list( $type ); 
  133.  
  134. //die( '<pre>' . print_r( $product_list, true ) . '</pre>' ); 
  135.  
  136. if( is_wp_error( $product_list ) ) return $product_list; 
  137.  
  138. $response = $this->translate_list( $product_list ); 
  139.  
  140. return json_decode( $response ); 
  141.  
  142.  
  143. public function translate_list( $product_list = array() ) { 
  144.  
  145. if( empty( $product_list ) ) return; 
  146.  
  147. $response = array(); 
  148.  
  149. if( isset( $product_list->matches ) ) { 
  150.  
  151. foreach( $product_list->matches as $p_key => $p_details ) { 
  152.  
  153. $product = array(); 
  154.  
  155. if( 'themeforest.net' == $p_details->site ) { 
  156. $site_key = 'tf'; 
  157. } else { 
  158. $site_key = 'cc'; 
  159.  
  160. $envato_url = 'http://www.layerswp.com/go-envato/?id=' . esc_attr( $p_details->id ) . '&item=' . esc_attr( $p_details->name ). '&site=' . $site_key; 
  161.  
  162. $categories = explode( '/', $p_details->classification ); 
  163.  
  164. $product[ 'id' ] = (int) $p_details->id; 
  165. $product[ 'url' ] = esc_attr( $envato_url ); 
  166. $product[ 'name' ] = esc_attr( $p_details->name ); 
  167. $product[ 'description' ] = esc_attr( $p_details->description ); 
  168. $product[ 'tags' ] = strtolower( implode( ', ', $p_details->tags ) ); 
  169. $product[ 'categories' ] = strtolower( implode( ', ', $categories ) ); 
  170. $product[ 'slug' ] = sanitize_title( $p_details->name ); 
  171. $product[ 'updated' ] = strtotime( $p_details->updated_at ); 
  172. $product[ 'sales' ] = esc_attr( $p_details->number_of_sales ); 
  173. $product[ 'rating' ] = ( $p_details->rating->count > 0 ? ceil( $p_details->rating->rating ) : '' ) ; 
  174. $product[ 'rating_count' ] = $p_details->rating->count; 
  175. $product[ 'author' ] = $p_details->author_username; 
  176. $product[ 'author_image' ] = $p_details->author_image; 
  177. $product[ 'author_url' ] = $p_details->author_url; 
  178. $product[ 'price' ] = (float) ($p_details->price_cents/100); 
  179. $product[ 'trending' ] = ( isset( $p_details->trending ) && '1' == $p_details->trending ? 1 : 0 ); 
  180. $product[ 'demo_url' ] = ( isset( $p_details->previews->live_site->url ) ? $p_details->previews->live_site->url : '' ); 
  181. $product[ 'allow_demo' ] = FALSE; 
  182. /** 
  183. * Get images and/or video 
  184. **/ 
  185. $previews = $p_details->previews; 
  186.  
  187. if ( isset( $previews->icon_with_landscape_preview->landscape_url ) && strpos( $previews->icon_with_landscape_preview->landscape_url, '//' ) ) { 
  188. $product[ 'is_img' ] = 1; 
  189. $product[ 'media_src' ] = $previews->icon_with_landscape_preview->landscape_url ; 
  190. } else if ( isset( $previews->icon_with_video_preview->landscape_url ) && strpos( $previews->icon_with_video_preview->landscape_url, '//' ) ) { 
  191. $product[ 'is_img' ] = 1; 
  192. $product[ 'media_src' ] = $previews->icon_with_video_preview->landscape_url ; 
  193. } else if ( isset( $previews->icon_with_video_preview->video_url ) && strpos( $previews->icon_with_video_preview->video_url, '//' ) ) { 
  194. $product[ 'is_img' ] = 0; 
  195. $product[ 'media_src' ] = $previews->icon_with_video_preview->video_url ; 
  196.  
  197. $response[] = $product; 
  198. } else { 
  199. foreach( $product_list as $p_key => $p_details ) { 
  200.  
  201. if( isset( $p_details->status ) && 'publish' != $p_details->status ) continue; 
  202.  
  203. $product = array(); 
  204.  
  205. $utm = '?utm_source=marketplace&utm_medium=link&utm_content=' . $p_details->name . '&utm_campaign=Layers%20Marketplace'; 
  206.  
  207. $demo_utm = '?utm_source=marketplace&utm_medium=preview&utm_content=' . $p_details->name . '&utm_campaign=Layers%20Marketplace%20Preview'; 
  208.  
  209. if( isset( $p_details->sub_title ) && '' != $p_details->sub_title ) { 
  210. $p_name = $p_details->name . ' - ' . $p_details->sub_title; 
  211. } else { 
  212. $p_name = $p_details->name; 
  213.  
  214. $product[ 'id' ] = (int) $p_details->id; 
  215. $product[ 'name' ] = esc_attr( $p_name ); 
  216. $product[ 'short_description' ] = $p_details->short_description; 
  217. $product[ 'description' ] = $p_details->description; 
  218. $product[ 'url' ] = esc_attr( $p_details->permalink . $utm ); 
  219. $product[ 'slug' ] = sanitize_title( $p_details->slug ); 
  220. $product[ 'updated' ] = strtotime( $p_details->date_modified ); 
  221. $product[ 'sales' ] = 0; 
  222. $product[ 'author' ] = 'Obox'; 
  223. $product[ 'author_image' ] = 'https://0.s3.envato.com/files/86093381/tf-avatar-2.jpg'; 
  224. $product[ 'author_url' ] = 'https://layerswp.com/'; 
  225. $product[ 'price' ] = (float) ($p_details->price); 
  226. $product[ 'demo_url' ] = ( isset( $p_details->demo_url ) && '' != $p_details->demo_url ? $p_details->demo_url . $demo_utm : '' ); 
  227. $product[ 'allow_demo' ] = (bool) ( isset( $p_details->demo_url ) && '' != $p_details->demo_url ? 1 : 0 ); 
  228. $product[ 'trending' ] = 0; 
  229.  
  230.  
  231. $tags = array(); 
  232. foreach( $p_details->tags as $p_tag_key => $p_tag_details ) { 
  233. $tags[] = $p_tag_details->slug; 
  234. $product[ 'tags' ] = strtolower( implode( ', ', $tags ) ); 
  235.  
  236. $categories = array(); 
  237. foreach( $p_details->categories as $p_cat_key => $p_cat_details ) { 
  238. $categories[] = $p_cat_details->slug; 
  239. $product[ 'categories' ] = strtolower( implode( ', ', $categories ) ); 
  240. $product[ 'rating' ] = ( $p_details->rating_count > 0 ? ceil( $details->average_rating ) : '' ) ; 
  241. $product[ 'rating_count' ] = $p_details->rating_count; 
  242.  
  243. /** 
  244. * Get images and/or video 
  245. **/ 
  246. foreach( $p_details->images as $img_key => $img_detail ) { 
  247. $product[ 'is_img' ] = 1; 
  248. $product[ 'media_src' ] = $img_detail->src; 
  249.  
  250. break; 
  251.  
  252. $response[] = $product; 
  253.  
  254. return json_encode( $response ); 
  255.  
  256.  
  257. public function get_layers_list( $p_type = 'themes' ) { 
  258.  
  259. $product_types = array( 
  260. 'themes' => 83,  
  261. 'extensions' => 81 
  262. ); 
  263.  
  264. // &category=' . $product_types[ $p_type ] 
  265.  
  266. $cache_key = 'layers_wp_marketplace'; 
  267.  
  268. if( FALSE !== get_transient( $cache_key ) ) { 
  269.  
  270. return json_decode( get_transient( $cache_key ) ); 
  271.  
  272. $api_call = wp_remote_get( 'https://www.layerswp.com/wp-json/wc/v1/products/?consumer_key=ck_850f668ddbad3705ecd10fe4f010dcc6e849a5ae&consumer_secret=cs_5c46a37a8890a4c2aa2af3c0226a6d489c6e7f70' ); 
  273.  
  274. if( is_wp_error( $api_call ) ) { 
  275.  
  276. // Return an error if we have one 
  277. return $api_call; 
  278. } else { 
  279.  
  280. set_transient( $cache_key , wp_remote_retrieve_body( $api_call ), 60 ); 
  281.  
  282. // If the call is successful, well then send back decoded JSON 
  283. return json_decode( wp_remote_retrieve_body( $api_call ) ); 
  284.  
  285. /** 
  286. * Give us a list of available extensions 
  287. */ 
  288. public function get_envato_list( $p_type = 'themes' ) { 
  289. // Set the right end point to use 
  290. $endpoint = 'discovery/search/search/item'; 
  291.  
  292. // Specify a query string here we tell the API what search parameters to use 
  293. switch( $p_type ) { 
  294. case 'stylekits' : 
  295. $query_string = 'site=codecanyon.net&category=skins/layers-wp-style-kits'; 
  296. break; 
  297. case 'extensions' : 
  298. $query_string = 'site=codecanyon.net&compatible_with=Layers%20WP'; 
  299. break; 
  300. default: 
  301. $query_string = 'site=themeforest.net&compatible_with=Layers%20WP'; 
  302. break; 
  303.  
  304. // Do the API call 
  305. $api_call = $this->do_envato_api_call( $endpoint, $query_string, 'get' ); 
  306.  
  307. if( is_wp_error( $api_call ) ) { 
  308.  
  309. // Return an error if we have one 
  310. return $api_call; 
  311. } else { 
  312.  
  313. // If the call is successful, well then send back decoded JSON 
  314. return json_decode( $api_call ); 
  315.  
  316. public function get_popular( $site = 'themeforest' ) { 
  317. $endpoint = 'market/popular:' . $site . '.json'; 
  318.  
  319. // Do the API call 
  320. $api_call = $this->do_envato_api_call( $endpoint, '', 'get', 2 ); 
  321.  
  322. if( is_wp_error( $api_call ) ) { 
  323.  
  324. // Return an error if we have one 
  325. return $api_call; 
  326. } else { 
  327.  
  328. // If the call is successful, well then send back decoded JSON 
  329. return json_decode( $api_call ); 
  330.  
  331.