| 1 |
<?php |
| 2 |
/** |
| 3 |
* The Plan class. |
| 4 |
* |
| 5 |
* @package automattic/jetpack-videopress |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Automattic\Jetpack\VideoPress; |
| 9 |
|
| 10 |
/** |
| 11 |
* The Plan class. |
| 12 |
*/ |
| 13 |
class Plan { |
| 14 |
/** |
| 15 |
* The meta name used to store the cache date |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
const CACHE_DATE_META_NAME = 'videopress-cache-date'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Valid pediord for the cache: One week. |
| 23 |
*/ |
| 24 |
const CACHE_VALIDITY_PERIOD = 7 * DAY_IN_SECONDS; |
| 25 |
|
| 26 |
/** |
| 27 |
* The meta name used to store the cache |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
const CACHE_META_NAME = 'videopress-cache'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Checks if the cache is old, meaning we need to fetch new data from WPCOM |
| 35 |
*/ |
| 36 |
private static function is_cache_old() { |
| 37 |
if ( empty( self::get_product_from_cache() ) ) { |
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
$cache_date = get_user_meta( get_current_user_id(), self::CACHE_DATE_META_NAME, true ); |
| 42 |
return time() - (int) $cache_date > ( self::CACHE_VALIDITY_PERIOD ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Gets the product list from the user cache |
| 47 |
*/ |
| 48 |
private static function get_product_from_cache() { |
| 49 |
return get_user_meta( get_current_user_id(), self::CACHE_META_NAME, true ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Gets the product data |
| 54 |
* |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
public static function get_product() { |
| 58 |
$request_url = 'https://public-api.wordpress.com/rest/v1.1/products?locale=' . get_user_locale() . '&type=jetpack'; |
| 59 |
$wpcom_request = wp_remote_get( esc_url_raw( $request_url ) ); |
| 60 |
$response_code = wp_remote_retrieve_response_code( $wpcom_request ); |
| 61 |
|
| 62 |
if ( 200 === $response_code ) { |
| 63 |
$products = json_decode( wp_remote_retrieve_body( $wpcom_request ) ); |
| 64 |
if ( ! isset( $products->jetpack_videopress ) || ! isset( $products->jetpack_videopress_monthly ) ) { |
| 65 |
return array(); |
| 66 |
} |
| 67 |
|
| 68 |
// Pick the desired product... |
| 69 |
$product = $products->jetpack_videopress; |
| 70 |
|
| 71 |
// ... and store it into the cache. |
| 72 |
update_user_meta( get_current_user_id(), self::CACHE_DATE_META_NAME, time() ); |
| 73 |
update_user_meta( get_current_user_id(), self::CACHE_META_NAME, $product ); |
| 74 |
|
| 75 |
return $product; |
| 76 |
} |
| 77 |
|
| 78 |
return new \WP_Error( |
| 79 |
'failed_to_fetch_data', |
| 80 |
esc_html__( 'Unable to fetch the requested data.', 'jetpack-videopress-pkg' ), |
| 81 |
array( |
| 82 |
'status' => $response_code, |
| 83 |
'request' => $wpcom_request, |
| 84 |
) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Populate the pricing array with the discount information. |
| 90 |
* |
| 91 |
* @param object $product - The product object. |
| 92 |
* @return int|false Discount percentage. |
| 93 |
*/ |
| 94 |
public static function get_coupon_discount( $product ) { |
| 95 |
// Check whether the product has a coupon. |
| 96 |
if ( ! isset( $product->sale_coupon ) ) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
$product_id = $product->product_id; |
| 101 |
$coupon = $product->sale_coupon; |
| 102 |
|
| 103 |
// Check product is covered by the coupon. |
| 104 |
if ( ! in_array( $product_id, $coupon->product_ids, true ) ) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
// Check whether it is still valid. |
| 109 |
$coupon_start_date = strtotime( $coupon->start_date ); |
| 110 |
$coupon_expires = strtotime( $coupon->expires ); |
| 111 |
if ( $coupon_start_date > time() || $coupon_expires < time() ) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
if ( ! isset( $coupon->discount ) ) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
return intval( $coupon->discount ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Return details about the VideoPress product price |
| 124 |
* |
| 125 |
* @return array Produce price details |
| 126 |
*/ |
| 127 |
public static function get_product_price() { |
| 128 |
$request_url = 'https://public-api.wordpress.com/rest/v1.1/products?locale=' . get_user_locale() . '&type=jetpack'; |
| 129 |
$wpcom_request = wp_remote_get( esc_url_raw( $request_url ) ); |
| 130 |
$response_code = wp_remote_retrieve_response_code( $wpcom_request ); |
| 131 |
|
| 132 |
if ( 200 === $response_code ) { |
| 133 |
$products = json_decode( wp_remote_retrieve_body( $wpcom_request ) ); |
| 134 |
|
| 135 |
$products_list = array(); |
| 136 |
|
| 137 |
if ( isset( $products->jetpack_videopress ) ) { |
| 138 |
$videopress_yearly = $products->jetpack_videopress; |
| 139 |
// get_coupon_discount |
| 140 |
$products_list['yearly'] = array( |
| 141 |
'name' => $videopress_yearly->product_name, |
| 142 |
'slug' => $videopress_yearly->product_slug, |
| 143 |
'price' => $videopress_yearly->cost, |
| 144 |
'priceByMonth' => round( $videopress_yearly->cost / 12, 2 ), |
| 145 |
'currency' => $videopress_yearly->currency_code, |
| 146 |
); |
| 147 |
|
| 148 |
$discount = self::get_coupon_discount( $videopress_yearly ); |
| 149 |
|
| 150 |
if ( $discount ) { |
| 151 |
$products_list['yearly']['discount'] = $discount; |
| 152 |
$products_list['yearly']['salePrice'] = round( $videopress_yearly->cost * ( 1 - $discount / 100 ), 2 ); |
| 153 |
$products_list['yearly']['salePriceByMonth'] = round( ( $videopress_yearly->cost * ( 1 - $discount / 100 ) / 12 ), 2 ); |
| 154 |
} else { |
| 155 |
$products_list['yearly']['salePrice'] = $videopress_yearly->cost; |
| 156 |
$products_list['yearly']['salePriceByMonth'] = round( $videopress_yearly->cost / 12, 2 ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
if ( isset( $products->jetpack_videopress_monthly ) ) { |
| 161 |
$videopress_monthly = $products->jetpack_videopress_monthly; |
| 162 |
|
| 163 |
$products_list['monthly'] = array( |
| 164 |
'name' => $videopress_monthly->product_name, |
| 165 |
'slug' => $videopress_monthly->product_slug, |
| 166 |
'price' => $videopress_monthly->cost, |
| 167 |
'currency' => $videopress_monthly->currency_code, |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
return $products_list; |
| 172 |
} |
| 173 |
|
| 174 |
return new \WP_Error( |
| 175 |
'failed_to_fetch_data', |
| 176 |
esc_html__( 'Unable to fetch the requested data.', 'jetpack-videopress-pkg' ), |
| 177 |
array( |
| 178 |
'status' => $response_code, |
| 179 |
'request' => $wpcom_request, |
| 180 |
) |
| 181 |
); |
| 182 |
} |
| 183 |
} |
| 184 |
|