Created
January 8, 2020 16:14
-
-
Save mklasen/3ad87f180e105dc4c80087cc83745fb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Sample { | |
public function register_invoice_endpoint() { | |
register_rest_route( 'prefix/v1/endoint', '/invoice/(?P<id>\d+)', array( | |
'methods' => 'GET', | |
'callback' => array( $this, 'get_invoice' ), | |
'permission_callback' => array( $this, 'get_items_permissions_check' ) | |
) ); | |
} | |
public function get_items_permissions_check() { | |
if ( ! current_user_can( 'manage_options' ) ) { | |
return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to download invoices' ), array( 'status' => rest_authorization_required_code() ) ); | |
} | |
return true; | |
} | |
public function get_invoice( $request ) { | |
$params = $request->get_params(); | |
$document_type = 'packing-slip'; // can be invoice, proforma, credit-note, packing-slip.. | |
$id = ( isset( $params['id'] ) && ! empty( $params['id'] ) ? (int) $params['id'] : false ); | |
if ( ! $id ) { | |
return array( 'status' => 400, 'message' => __( 'The ID provided is invalid.', 'stroopwafels-templates' ) ); | |
} | |
$order = wc_get_order( $id ); | |
if ( is_wp_error( $order ) ) { | |
return array( 'status' => 400, 'message' => __( 'The order ID does not exist.', 'stroopwafels-templates' ) ); | |
} | |
// if we got here, we're safe to go! | |
try { | |
$document = wcpdf_get_document( $document_type, $order, true ); | |
if ( $document ) { | |
$output_format = WPO_WCPDF()->settings->get_output_format( $document_type ); | |
// allow URL override | |
if ( isset( $_GET['output'] ) && in_array( $_GET['output'], array( 'html', 'pdf' ) ) ) { | |
$output_format = $_GET['output']; | |
} | |
switch ( $output_format ) { | |
case 'html': | |
add_filter( 'wpo_wcpdf_use_path', '__return_false' ); | |
$document->output_html(); | |
break; | |
case 'pdf': | |
default: | |
if ( has_action( 'wpo_wcpdf_created_manually' ) ) { | |
do_action( 'wpo_wcpdf_created_manually', $document->get_pdf(), $document->get_filename() ); | |
} | |
$output_mode = WPO_WCPDF()->settings->get_output_mode( $document_type ); | |
$document->output_pdf( $output_mode ); | |
break; | |
} | |
} else { | |
wp_die( sprintf( __( "Document of type '%s' for the selected order(s) could not be generated", 'woocommerce-pdf-invoices-packing-slips' ), $document_type ) ); | |
} | |
} catch ( \Dompdf\Exception $e ) { | |
$message = 'DOMPDF Exception: ' . $e->getMessage(); | |
wcpdf_log_error( $message, 'critical', $e ); | |
wcpdf_output_error( $message, 'critical', $e ); | |
} catch ( \Exception $e ) { | |
$message = 'Exception: ' . $e->getMessage(); | |
wcpdf_log_error( $message, 'critical', $e ); | |
wcpdf_output_error( $message, 'critical', $e ); | |
} catch ( \Error $e ) { | |
$message = 'Fatal error: ' . $e->getMessage(); | |
wcpdf_log_error( $message, 'critical', $e ); | |
wcpdf_output_error( $message, 'critical', $e ); | |
} | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment