Der in den Partner-Shops installierte WooCommerce-Connector, der Bestellungen per REST an /api/wp/* uebertraegt (Kunde anlegen, Warenkorb, Status-Abgleich). Grundlage fuer AP-13 (Shop-Bestandsabzug beim Versand). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
156 lines
4.5 KiB
PHP
156 lines
4.5 KiB
PHP
<?php
|
|
|
|
// Exit if accessed directly
|
|
if (! defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/*
|
|
* Class to add a custom endpoint for vendor decisions
|
|
*
|
|
* @since 1.1.0
|
|
*/
|
|
|
|
class PARTNERCENTER_API_Custom_Document_Download_Endpoint_Class
|
|
{
|
|
/**
|
|
* Custom endpoint name.
|
|
*
|
|
* @since 1.1.0
|
|
*
|
|
* @var string
|
|
*/
|
|
public static $endpoint = 'partnercenter-document-download';
|
|
|
|
/*
|
|
* Setup the class.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
add_action('init', [$this, 'rewrite']);
|
|
add_filter('query_vars', [$this, 'query_vars']);
|
|
add_filter('template_redirect', [$this, 'change_template'], 1, 0);
|
|
}
|
|
|
|
/*
|
|
* Allow custom url parameter.
|
|
*
|
|
* @since 1.1.0
|
|
*/
|
|
public function rewrite()
|
|
{
|
|
// Add some custom query vars
|
|
add_rewrite_tag('%order-id%', '([^&]+)');
|
|
add_rewrite_tag('%document-id%', '([^&]+)');
|
|
add_rewrite_tag('%request-token%', '([^&]+)');
|
|
// Add rewrite rule to map own query vars
|
|
add_rewrite_rule('^'.self::$endpoint.'/([^/]*)/([^/]*)/([^/]*)/?', 'index.php?'.self::$endpoint.'=1&order-id=$matches[1]&document-id=$matches[2]&request-token=$matches[3]', 'top');
|
|
}
|
|
|
|
/*
|
|
* Allow custom query var.
|
|
*
|
|
* @since 1.1.0
|
|
*/
|
|
public function query_vars($vars)
|
|
{
|
|
$vars[] = self::$endpoint;
|
|
|
|
return $vars;
|
|
}
|
|
|
|
/**
|
|
* Fetch and display the template for this virtual page.
|
|
*
|
|
* @since 1.1.0
|
|
*/
|
|
public function show_response(array $values)
|
|
{
|
|
|
|
/*
|
|
* Check for empty values
|
|
*/
|
|
if (empty($values['order-id'])) {
|
|
wp_die(__('Missing order ID', 'partnercenter-api'));
|
|
}
|
|
if (empty($values['document-id'])) {
|
|
wp_die(__('Missing document ID', 'partnercenter-api'));
|
|
}
|
|
if (empty($values['request-token'])) {
|
|
wp_die(__('Missing Request token', 'partnercenter-api'));
|
|
}
|
|
|
|
// Fetch order from DB
|
|
$order = wc_get_order($values['order-id']);
|
|
if ($order === false) {
|
|
wp_die(__('Invalid order', 'partnercenter-api'));
|
|
}
|
|
|
|
/*
|
|
* Validate token
|
|
*/
|
|
$token_from_db = get_post_meta($order->get_id(), '_partnercenter_invoice_token', true);
|
|
if ($token_from_db !== $values['request-token']) {
|
|
wp_die(__('Invalid token', 'partnercenter-api'));
|
|
}
|
|
|
|
// Get invoice from DB
|
|
$invoice_from_db = wc_gzdp_get_order_last_invoice($order->get_id());
|
|
|
|
// Check if the right invoice id was requested
|
|
if (intval($invoice_from_db->get_id()) !== intval($values['document-id'])) {
|
|
wp_die(__('Mismatching invoice number', 'partnercenter-api'));
|
|
}
|
|
|
|
// Check if file exists
|
|
if (! file_exists($invoice_from_db->get_pdf_path())) {
|
|
wp_die(__('File not found', 'partnercenter-api'));
|
|
}
|
|
|
|
// Send headers to download file
|
|
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
|
|
header('Cache-Control: public'); // needed for internet explorer
|
|
header('Content-Type: application/pdf');
|
|
header('Content-Transfer-Encoding: Binary');
|
|
header('Content-Length:'.filesize($invoice_from_db->get_pdf_path()));
|
|
header('Content-Disposition: attachment; filename= '.$invoice_from_db->get_filename());
|
|
readfile($invoice_from_db->get_pdf_path());
|
|
|
|
// Stop the rest of the process
|
|
exit();
|
|
}
|
|
|
|
/*
|
|
* Load right template for custom virtual page.
|
|
*
|
|
* @since 1.1.0
|
|
*/
|
|
public function change_template()
|
|
{
|
|
/** @var $wp_query WP_Query */
|
|
global $wp_query;
|
|
|
|
if (! empty($wp_query->query_vars[self::$endpoint])) {
|
|
|
|
// Set some data
|
|
$wp_query->is_404 = false;
|
|
$wp_query->is_feed = false;
|
|
$wp_query->is_page = true;
|
|
$wp_query->is_singular = true;
|
|
$wp_query->is_home = false;
|
|
$wp_query->is_archive = false;
|
|
$wp_query->is_category = false;
|
|
|
|
// Collect values to pass on
|
|
$values = [
|
|
'order-id' => sanitize_title($wp_query->query_vars['order-id']),
|
|
'document-id' => sanitize_title($wp_query->query_vars['document-id']),
|
|
'request-token' => sanitize_title($wp_query->query_vars['request-token']),
|
|
];
|
|
|
|
self::show_response($values);
|
|
}
|
|
}
|
|
}
|
|
|
|
new PARTNERCENTER_API_Custom_Document_Download_Endpoint_Class;
|