EDD_API_V2

EDD_API_V2 Class.

Defined (1)

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

/includes/api/class-edd-api-v2.php  
  1. class EDD_API_V2 extends EDD_API_V1 { 
  2.  
  3. /** 
  4. * Process Get Products API Request 
  5. * @access public 
  6. * @since 2.6 
  7. * @param array $args Query arguments 
  8. * @return array $customers Multidimensional array of the products 
  9. */ 
  10. public function get_products( $args = array() ) { 
  11.  
  12. $products = array(); 
  13. $error = array(); 
  14.  
  15. if ( empty( $args['product'] ) ) { 
  16.  
  17. $products['products'] = array(); 
  18.  
  19. $query_args = array( 
  20. 'post_type' => 'download',  
  21. 'posts_per_page' => $this->per_page(),  
  22. 'suppress_filters' => true,  
  23. 'paged' => $this->get_paged(),  
  24. ); 
  25.  
  26. if( ! empty( $args['s'] ) ) { 
  27. $query_args['s'] = sanitize_text_field( $args['s'] ); 
  28.  
  29. if( ! empty( $args['category'] ) ) { 
  30. if ( strpos( $args['category'], ', ' ) ) { 
  31. $args['category'] = explode( ', ', $args['category'] ); 
  32.  
  33. if ( is_numeric( $args['category'] ) ) { 
  34. $query_args['tax_query'] = array( 
  35. array( 
  36. 'taxonomy' => 'download_category',  
  37. 'field' => 'ID',  
  38. 'terms' => (int) $args['category'] 
  39. ),  
  40. ); 
  41. } else if ( is_array( $args['category'] ) ) { 
  42.  
  43. foreach ( $args['category'] as $category ) { 
  44.  
  45.  
  46. $field = is_numeric( $category ) ? 'ID': 'slug'; 
  47.  
  48. $query_args['tax_query'][] = array( 
  49. 'taxonomy' => 'download_category',  
  50. 'field' => $field,  
  51. 'terms' => $category,  
  52. ); 
  53.  
  54.  
  55. } else { 
  56. $query_args['download_category'] = $args['category']; 
  57.  
  58. if( ! empty( $args['tag'] ) ) { 
  59. if ( strpos( $args['tag'], ', ' ) ) { 
  60. $args['tag'] = explode( ', ', $args['tag'] ); 
  61.  
  62. if ( is_numeric( $args['tag'] ) ) { 
  63. $query_args['tax_query'] = array( 
  64. array( 
  65. 'taxonomy' => 'download_tag',  
  66. 'field' => 'ID',  
  67. 'terms' => (int) $args['tag'] 
  68. ),  
  69. ); 
  70. } else if ( is_array( $args['tag'] ) ) { 
  71.  
  72. foreach ( $args['tag'] as $tag ) { 
  73.  
  74.  
  75. $field = is_numeric( $tag ) ? 'ID': 'slug'; 
  76.  
  77. $query_args['tax_query'][] = array( 
  78. 'taxonomy' => 'download_tag',  
  79. 'field' => $field,  
  80. 'terms' => $tag,  
  81. ); 
  82.  
  83.  
  84. } else { 
  85. $query_args['download_tag'] = $args['tag']; 
  86.  
  87. if ( ! empty( $query_args['tax_query'] ) ) { 
  88.  
  89. $relation = ! empty( $args['term_relation'] ) ? sanitize_text_field( $args['term_relation'] ) : 'OR'; 
  90. $query_args['tax_query']['relation'] = $relation; 
  91.  
  92.  
  93. $product_list = get_posts( $query_args ); 
  94.  
  95. if ( $product_list ) { 
  96. $i = 0; 
  97. foreach ( $product_list as $product_info ) { 
  98. $products['products'][$i] = $this->get_product_data( $product_info ); 
  99. $i++; 
  100.  
  101. } else { 
  102.  
  103. if ( get_post_type( $args['product'] ) == 'download' ) { 
  104. $product_info = get_post( $args['product'] ); 
  105.  
  106. $products['products'][0] = $this->get_product_data( $product_info ); 
  107.  
  108. } else { 
  109. $error['error'] = sprintf( __( 'Product %s not found!', 'easy-digital-downloads' ), $args['product'] ); 
  110. return $error; 
  111.  
  112. return apply_filters( 'edd_api_products', $products ); 
  113.  
  114. /** 
  115. * Given a download post object, generate the data for the API output 
  116. * @since 2.6 
  117. * @param object $product_info The Download Post Object 
  118. * @return array Array of post data to return back in the API 
  119. */ 
  120. public function get_product_data( $product_info ) { 
  121.  
  122. // Use the parent's get_product_data to reduce code duplication 
  123. $product = parent::get_product_data( $product_info ); 
  124.  
  125. if ( edd_use_skus() ) { 
  126. $product['info']['sku'] = edd_get_download_sku( $product['info']['id'] ); 
  127.  
  128. return apply_filters( 'edd_api_products_product_v2', $product ); 
  129.  
  130.  
  131. /** 
  132. * Process Get Customers API Request 
  133. * @access public 
  134. * @since 2.6 
  135. * @global object $wpdb Used to query the database using the WordPress Database API 
  136. * @param array $args Array of arguments for filters customers 
  137. * @return array $customers Multidimensional array of the customers 
  138. */ 
  139. public function get_customers( $args = array() ) { 
  140. global $wpdb; 
  141.  
  142. $paged = $this->get_paged(); 
  143. $per_page = $this->per_page(); 
  144. $offset = $per_page * ( $paged - 1 ); 
  145.  
  146. $defaults = array( 
  147. 'customer' => null,  
  148. 'date' => null,  
  149. 'startdate' => null,  
  150. 'enddate' => null,  
  151. 'number' => $per_page,  
  152. 'offset' => $offset,  
  153. ); 
  154.  
  155. $args = wp_parse_args( $args, $defaults ); 
  156. $customers = array(); 
  157. $error = array(); 
  158.  
  159. if( ! user_can( $this->user_id, 'view_shop_sensitive_data' ) && ! $this->override ) { 
  160. return $customers; 
  161.  
  162. if( is_numeric( $args['customer'] ) ) { 
  163. $field = 'id'; 
  164. } else { 
  165. $field = 'email'; 
  166.  
  167. $args[ $field ] = $args['customer']; 
  168.  
  169. $dates = $this->get_dates( $args ); 
  170.  
  171. if( $args['date'] === 'range' ) { 
  172.  
  173. // Ensure the end date is later than the start date 
  174. if( ( ! empty( $args['enddate'] ) && ! empty( $args['enddate'] ) ) && $args['enddate'] < $args['startdate'] ) { 
  175. $error['error'] = __( 'The end date must be later than the start date!', 'easy-digital-downloads' ); 
  176.  
  177. $date_range = array(); 
  178. if ( ! empty( $args['startdate'] ) ) { 
  179. $date_range['start'] = $dates['year'] . sprintf('%02d', $dates['m_start'] ) . $dates['day_start']; 
  180.  
  181. if ( ! empty( $args['enddate'] ) ) { 
  182. $date_range['end'] = $dates['year_end'] . sprintf('%02d', $dates['m_end'] ) . $dates['day_end']; 
  183.  
  184. $args['date'] = $date_range; 
  185.  
  186. } elseif( ! empty( $args['date'] ) ) { 
  187.  
  188. if( $args['date'] == 'this_quarter' || $args['date'] == 'last_quarter' ) { 
  189.  
  190. $args['date'] = array( 
  191. 'start' => $dates['year'] . sprintf('%02d', $dates['m_start'] ) . '01',  
  192. 'end' => $dates['year'] . sprintf('%02d', $dates['m_end'] ) . cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] ),  
  193. ); 
  194.  
  195. } else if ( $args['date'] == 'this_month' || $args['date'] == 'last_month' ) { 
  196. $args['date'] = array( 
  197. 'start' => $dates['year'] . sprintf( '%02d', $dates['m_start'] ) . '01',  
  198. 'end' => $dates['year'] . sprintf( '%02d', $dates['m_end'] ). cal_days_in_month( CAL_GREGORIAN, $dates['m_end'], $dates['year'] ),  
  199. ); 
  200. } else if ( $args['date'] == 'this_year' || $args['date'] == 'last_year' ) { 
  201. $args['date'] = array( 
  202. 'start' => $dates['year'] . '0101',  
  203. 'end' => $dates['year'] . '1231',  
  204. ); 
  205. } else { 
  206. $args['date'] = $dates['year'] . sprintf('%02d', $dates['m_start'] ) . $dates['day']; 
  207.  
  208. unset( $args['startdate'], $args['enddate'] ); 
  209.  
  210. $customer_query = EDD()->customers->get_customers( $args ); 
  211. $customer_count = 0; 
  212.  
  213. if( $customer_query ) { 
  214.  
  215. foreach ( $customer_query as $customer_obj ) { 
  216. // Setup a new EDD_Customer object so additional details are defined (like additional emails) 
  217. $customer_obj = new EDD_Customer( $customer_obj->id ); 
  218.  
  219. $names = explode( ' ', $customer_obj->name ); 
  220. $first_name = ! empty( $names[0] ) ? $names[0] : ''; 
  221. $last_name = ''; 
  222. if( ! empty( $names[1] ) ) { 
  223. unset( $names[0] ); 
  224. $last_name = implode( ' ', $names ); 
  225.  
  226. $customers['customers'][ $customer_count ]['info']['customer_id'] = $customer_obj->id; 
  227. $customers['customers'][ $customer_count ]['info']['user_id'] = 0; 
  228. $customers['customers'][ $customer_count ]['info']['username'] = ''; 
  229. $customers['customers'][ $customer_count ]['info']['display_name'] = ''; 
  230. $customers['customers'][ $customer_count ]['info']['first_name'] = $first_name; 
  231. $customers['customers'][ $customer_count ]['info']['last_name'] = $last_name; 
  232. $customers['customers'][ $customer_count ]['info']['email'] = $customer_obj->email; 
  233. $customers['customers'][ $customer_count ]['info']['additional_emails'] = null; 
  234. $customers['customers'][ $customer_count ]['info']['date_created'] = $customer_obj->date_created; 
  235.  
  236. if ( ! empty( $customer_obj->emails ) && count( $customer_obj->emails ) > 1 ) { 
  237. $additional_emails = $customer_obj->emails; 
  238.  
  239. $primary_email_key = array_search( $customer_obj->email, $customer_obj->emails ); 
  240. if ( false !== $primary_email_key ) { 
  241. unset( $additional_emails[ $primary_email_key ] ); 
  242.  
  243. $customers['customers'][ $customer_count ]['info']['additional_emails'] = $additional_emails; 
  244.  
  245. if ( ! empty( $customer_obj->user_id ) && $customer_obj->user_id > 0 ) { 
  246.  
  247. $user_data = get_userdata( $customer_obj->user_id ); 
  248.  
  249. // Customer with registered account 
  250.  
  251. // id is going to get deprecated in the future, user user_id or customer_id instead 
  252. $customers['customers'][ $customer_count ]['info']['user_id'] = $customer_obj->user_id; 
  253. $customers['customers'][ $customer_count ]['info']['username'] = $user_data->user_login; 
  254. $customers['customers'][ $customer_count ]['info']['display_name'] = $user_data->display_name; 
  255.  
  256.  
  257. $customers['customers'][ $customer_count ]['stats']['total_purchases'] = $customer_obj->purchase_count; 
  258. $customers['customers'][ $customer_count ]['stats']['total_spent'] = $customer_obj->purchase_value; 
  259. $customers['customers'][ $customer_count ]['stats']['total_downloads'] = edd_count_file_downloads_of_user( $customer_obj->email ); 
  260.  
  261. $customer_count++; 
  262.  
  263.  
  264. } elseif( $args['customer'] ) { 
  265.  
  266. $error['error'] = sprintf( __( 'Customer %s not found!', 'easy-digital-downloads' ), $customer ); 
  267. return $error; 
  268.  
  269. } else { 
  270.  
  271. $error['error'] = __( 'No customers found!', 'easy-digital-downloads' ); 
  272. return $error; 
  273.  
  274.  
  275. return apply_filters( 'edd_api_customers', $customers, $this ); 
  276.  
  277. /** 
  278. * Retrieves Recent Sales 
  279. * @access public 
  280. * @since 2.6 
  281. * @return array 
  282. */ 
  283. public function get_recent_sales() { 
  284. global $wp_query; 
  285.  
  286. $sales = array(); 
  287.  
  288. if( ! user_can( $this->user_id, 'view_shop_reports' ) && ! $this->override ) { 
  289. return $sales; 
  290.  
  291. if( isset( $wp_query->query_vars['id'] ) ) { 
  292. $query = array(); 
  293. $query[] = new EDD_Payment( $wp_query->query_vars['id'] ); 
  294. } elseif( isset( $wp_query->query_vars['purchasekey'] ) ) { 
  295. $query = array(); 
  296. $query[] = edd_get_payment_by( 'key', $wp_query->query_vars['purchasekey'] ); 
  297. } elseif( isset( $wp_query->query_vars['email'] ) ) { 
  298. $query = edd_get_payments( array( 'fields' => 'ids', 'meta_key' => '_edd_payment_user_email', 'meta_value' => $wp_query->query_vars['email'], 'number' => $this->per_page(), 'page' => $this->get_paged(), 'status' => 'publish' ) ); 
  299. } else { 
  300. $query = edd_get_payments( array( 'fields' => 'ids', 'number' => $this->per_page(), 'page' => $this->get_paged(), 'status' => 'publish' ) ); 
  301.  
  302. if ( $query ) { 
  303. $i = 0; 
  304. foreach ( $query as $payment ) { 
  305. if ( is_numeric( $payment ) ) { 
  306. $payment = new EDD_Payment( $payment ); 
  307.  
  308. $payment_meta = $payment->get_meta(); 
  309. $user_info = $payment->user_info; 
  310.  
  311. $sales['sales'][ $i ]['ID'] = $payment->number; 
  312. $sales['sales'][ $i ]['transaction_id'] = ( ! empty( $payment->transaction_id ) ) ? $payment->transaction_id : null; 
  313. $sales['sales'][ $i ]['key'] = $payment->key; 
  314. $sales['sales'][ $i ]['subtotal'] = $payment->subtotal; 
  315. $sales['sales'][ $i ]['tax'] = $payment->tax; 
  316. $sales['sales'][ $i ]['fees'] = ( ! empty( $payment->fees ) ? $payment->fees : null ); 
  317. $sales['sales'][ $i ]['total'] = $payment->total; 
  318. $sales['sales'][ $i ]['gateway'] = $payment->gateway; 
  319. $sales['sales'][ $i ]['email'] = $payment->email; 
  320. $sales['sales'][ $i ]['date'] = $payment->date; 
  321.  
  322. $c = 0; 
  323.  
  324. $discounts = ! empty( $payment->discounts ) ? explode( ', ', $payment->discounts ) : array(); 
  325. $discounts = array_map( 'trim', $discounts ); 
  326. $discount_values = array(); 
  327.  
  328. foreach ( $discounts as $discount ) { 
  329. if ( 'none' === $discount ) { continue; } 
  330.  
  331. $discount_values[ $discount ] = 0; 
  332.  
  333. $cart_items = array(); 
  334.  
  335. foreach ( $payment->cart_details as $key => $item ) { 
  336.  
  337. $item_id = isset( $item['id'] ) ? $item['id'] : $item; 
  338. $price = isset( $item['price'] ) ? $item['price'] : false; // The final price for the item 
  339. $item_price = isset( $item['item_price'] ) ? $item['item_price'] : false; // The price before discounts 
  340.  
  341. $price_id = isset( $item['item_number']['options']['price_id'] ) ? $item['item_number']['options']['price_id'] : null; 
  342. $quantity = isset( $item['quantity'] ) && $item['quantity'] > 0 ? $item['quantity'] : 1; 
  343.  
  344. if( ! $price ) { 
  345. // This function is only used on payments with near 1.0 cart data structure 
  346. $price = edd_get_download_final_price( $item_id, $user_info, null ); 
  347.  
  348. $price_name = ''; 
  349. if ( isset( $item['item_number'] ) && isset( $item['item_number']['options'] ) ) { 
  350. $price_options = $item['item_number']['options']; 
  351. if ( isset( $price_options['price_id'] ) ) { 
  352. $price_name = edd_get_price_option_name( $item_id, $price_options['price_id'], $payment->ID ); 
  353.  
  354. $cart_items[ $c ]['id'] = $item_id; 
  355. $cart_items[ $c ]['quantity'] = $quantity; 
  356. $cart_items[ $c ]['name'] = get_the_title( $item_id ); 
  357. $cart_items[ $c ]['price'] = $price; 
  358. $cart_items[ $c ]['price_name'] = $price_name; 
  359.  
  360. // Determine the discount amount for the item, if there is one 
  361. foreach ( $discount_values as $discount => $amount ) { 
  362.  
  363. $item_discount = edd_get_cart_item_discount_amount( $item, $discount ); 
  364. $discount_values[ $discount ] += $item_discount; 
  365.  
  366.  
  367. $c++; 
  368.  
  369. $sales['sales'][ $i ]['discounts'] = ( ! empty( $discount_values ) ? $discount_values : null );; 
  370. $sales['sales'][ $i ]['products'] = $cart_items; 
  371.  
  372. $i++; 
  373. return apply_filters( 'edd_api_sales', $sales, $this ); 
  374.