| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || exit(); |
| 4 |
|
| 5 |
if ( ! class_exists( 'LP_Statistic_Status' ) ) { |
| 6 |
class LP_Statistic_Status { |
| 7 |
|
| 8 |
/** |
| 9 |
* LearnPress statistic layout |
| 10 |
* |
| 11 |
* @since 2.0 |
| 12 |
*/ |
| 13 |
public static function render() { |
| 14 |
$order_statuses = learn_press_get_order_statuses( true, true ); |
| 15 |
$eduma_data = self::get_eduma_info( 14058034 ); |
| 16 |
$specific_statuses = array( 'lp-completed', 'lp-failed', 'lp-on-hold' ); |
| 17 |
|
| 18 |
foreach ( $order_statuses as $status ) { |
| 19 |
if ( ! in_array( $status, $specific_statuses ) ) { |
| 20 |
$specific_statuses[] = $status; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
$counts = learn_press_count_orders( array( 'status' => $specific_statuses ) ); |
| 25 |
?> |
| 26 |
|
| 27 |
<ul class="learnpress-statistic-status"> |
| 28 |
<li class="full-width"> |
| 29 |
<a href="#" class="total-raised"> |
| 30 |
<span> |
| 31 |
<?php echo _learn_press_total_raised(); ?> |
| 32 |
<?php _e( 'Total Raised', 'learnpress' ); ?> |
| 33 |
</span> |
| 34 |
</a> |
| 35 |
</li> |
| 36 |
|
| 37 |
<?php foreach ( $specific_statuses as $status ) : ?> |
| 38 |
<?php |
| 39 |
$status_object = get_post_status_object( $status ); |
| 40 |
|
| 41 |
if ( ! $status_object ) { |
| 42 |
continue; |
| 43 |
} |
| 44 |
|
| 45 |
$count = $counts[ $status ]; |
| 46 |
?> |
| 47 |
|
| 48 |
<li> |
| 49 |
<?php if ( $count ) : ?> |
| 50 |
<a href="<?php echo esc_url_raw( admin_url( 'edit.php?post_status=' . LP_ORDER_CPT . '&post_type=' . $status ) ); ?>" class="<?php echo esc_attr( $status ); ?>"> |
| 51 |
<span><?php printf( translate_nooped_plural( _n_noop( '%d order', '%d orders' ), $count, 'learnpress' ), $count ); ?></span> |
| 52 |
<?php printf( '%s', $status_object->label ); ?> |
| 53 |
</a> |
| 54 |
<?php else : ?> |
| 55 |
<span class="<?php echo esc_attr( $status ); ?>"> |
| 56 |
<span><?php printf( translate_nooped_plural( _n_noop( '%d order', '%d orders' ), $count, 'learnpress' ), $count ); ?></span> |
| 57 |
<?php printf( '%s', $status_object->label ); ?> |
| 58 |
</span> |
| 59 |
<?php endif; ?> |
| 60 |
</li> |
| 61 |
<?php endforeach; ?> |
| 62 |
|
| 63 |
<li class="full-width featured-theme"> |
| 64 |
<p> |
| 65 |
<a href="<?php echo esc_url_raw( $eduma_data['item']['url'] ); ?>"> |
| 66 |
<?php echo esc_html( $eduma_data['item']['item'] ); ?> |
| 67 |
</a> - <?php printf( '%s%s', '$', $eduma_data['item']['cost'] ); ?> |
| 68 |
</p> |
| 69 |
<p> |
| 70 |
<?php _e( 'Created by: ', 'learnpress' ); ?> |
| 71 |
<a href="https://thimpress.com/" class="author"><?php echo esc_html( $eduma_data['item']['user'] ); ?></a> |
| 72 |
</p> |
| 73 |
</li> |
| 74 |
</ul> |
| 75 |
<?php |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @param String $item_id - The ID of an Envato Marketplace item |
| 80 |
* |
| 81 |
* @returns Array - The item informations |
| 82 |
*/ |
| 83 |
public static function get_eduma_info( $item_id ) { |
| 84 |
|
| 85 |
/* Data cache timeout in seconds - It send a new request each hour instead of each page refresh */ |
| 86 |
$CACHE_EXPIRATION = 3600; |
| 87 |
|
| 88 |
/* Set the transient ID for caching */ |
| 89 |
$transient_id = 'learnpress_envato_item_data'; |
| 90 |
|
| 91 |
/* Get the cached data */ |
| 92 |
$cached_item = get_transient( $transient_id ); |
| 93 |
|
| 94 |
/* Check if the function has to send a new API request */ |
| 95 |
if ( ! $cached_item || ( $cached_item->item_id != $item_id ) ) { |
| 96 |
|
| 97 |
/* Set the API URL, %s will be replaced with the item ID */ |
| 98 |
$api_url = 'http://marketplace.envato.com/api/edge/item:%s.json'; |
| 99 |
|
| 100 |
/* Fetch data using the WordPress function wp_remote_get() */ |
| 101 |
$response = wp_safe_remote_get( sprintf( $api_url, $item_id ) ); |
| 102 |
|
| 103 |
/* Check for errors, if there are some errors return false */ |
| 104 |
if ( is_wp_error( $response ) or ( wp_remote_retrieve_response_code( $response ) != 200 ) ) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
/* Transform the JSON string into a PHP array */ |
| 109 |
$item_data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 110 |
|
| 111 |
/* Check for incorrect data */ |
| 112 |
if ( ! is_array( $item_data ) ) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
/* Prepare data for caching */ |
| 116 |
$data_to_cache = new stdClass(); |
| 117 |
$data_to_cache->item_id = $item_id; |
| 118 |
$data_to_cache->item_info = $item_data; |
| 119 |
|
| 120 |
/* Set the transient - cache item data */ |
| 121 |
set_transient( $transient_id, $data_to_cache, $CACHE_EXPIRATION ); |
| 122 |
|
| 123 |
/* Return item info array */ |
| 124 |
return $item_data; |
| 125 |
} |
| 126 |
|
| 127 |
/* If the item is already cached return the cached info */ |
| 128 |
return $cached_item->item_info; |
| 129 |
} |
| 130 |
|
| 131 |
} |
| 132 |
} |
| 133 |
|