| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Shortcode; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use StoreEngine\Classes\DownloadPermission; |
| 10 |
use StoreEngine\Utils\Formatting; |
| 11 |
use StoreEngine\Utils\Helper; |
| 12 |
use StoreEngine\Utils\Template; |
| 13 |
use WP_Error; |
| 14 |
|
| 15 |
class OrderDownloads { |
| 16 |
|
| 17 |
public function __construct() { |
| 18 |
add_shortcode( 'storeengine_order_downloads', [ $this, 'render' ] ); |
| 19 |
} |
| 20 |
|
| 21 |
public function render( $atts ) { |
| 22 |
$attributes = shortcode_atts( [ 'dummy' => false ], $atts ); |
| 23 |
$dummy = Formatting::string_to_bool( $attributes['dummy'] ); |
| 24 |
|
| 25 |
if ( ! $dummy ) { |
| 26 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 27 |
$order_hash = isset( $_GET['order_hash'] ) ? sanitize_text_field( wp_unslash( $_GET['order_hash'] ) ) : ''; |
| 28 |
$order = Helper::get_order_by_key( $order_hash ); |
| 29 |
$order = $order instanceof WP_Error ? false : $order; |
| 30 |
|
| 31 |
// Fall back to sample content when there's no real order to show |
| 32 |
// (page opened directly, previewed, or rendered in the editor). |
| 33 |
if ( ! $order ) { |
| 34 |
$dummy = true; |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
$downloadable_permissions = $dummy |
| 39 |
? self::get_dummy_permissions() |
| 40 |
: apply_filters( 'storeengine/order/downloadable_permissions', $order->get_downloadable_permissions(), $order ); |
| 41 |
|
| 42 |
ob_start(); |
| 43 |
Template::get_template( 'shortcode/order-downloads.php', [ |
| 44 |
'downloadable_permissions' => $downloadable_permissions, |
| 45 |
] ); |
| 46 |
|
| 47 |
return ob_get_clean(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* A sample download row for previews / empty-order rendering. The template |
| 52 |
* only reads the product title, file name and download URL, so a lightweight |
| 53 |
* {@see DownloadPermission} subclass overriding just those three is enough. |
| 54 |
* |
| 55 |
* @return DownloadPermission[] |
| 56 |
*/ |
| 57 |
protected static function get_dummy_permissions(): array { |
| 58 |
$permission = new class( 0 ) extends DownloadPermission { |
| 59 |
public function get_product_title(): string { |
| 60 |
return __( 'Sample product', 'storeengine' ); |
| 61 |
} |
| 62 |
|
| 63 |
public function get_file_name(): string { |
| 64 |
return __( 'sample-file.zip', 'storeengine' ); |
| 65 |
} |
| 66 |
|
| 67 |
public function get_download_url(): string { |
| 68 |
return '#'; |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
return [ $permission ]; |
| 73 |
} |
| 74 |
} |
| 75 |
|