post_type && 'woocommerce_page_wc-orders' !== $screen->id ) ) { return; } wp_enqueue_style( 'lmcc-admin-order-pdf', plugin_dir_url( LMCC_PLUGIN_FILE ) . 'assets/css/order-pdf-admin.css', array( 'dashicons' ), LMCC_VERSION ); } private function can_view( int $order_id ): bool { // phpcs:ignore WordPress.WP.Capabilities.Unknown -- WooCommerce registers these order capabilities and maps the per-order meta capability. return is_user_logged_in() && current_user_can( 'edit_shop_orders' ) && current_user_can( 'edit_shop_order', $order_id ); } public function render_list_action( $order ): void { if ( ! $order instanceof \WC_Order || ! $this->can_view( $order->get_id() ) ) { return; } $label = $this->label( 'list' ); ?> label( 'list_print' ) ); ?> resolve_order( $order_data ); if ( ! $order || ! $this->can_view( $order->get_id() ) ) { return; } add_meta_box( 'lmcc-order-documents', $this->label( 'panel' ), array( $this, 'render_documents_box' ), $screen_id, 'side', 'high' ); } private function resolve_order( $order_data ): ?\WC_Order { if ( $order_data instanceof \WC_Order ) { return $order_data; } if ( $order_data instanceof \WP_Post && 'shop_order' === $order_data->post_type && function_exists( 'wc_get_order' ) ) { $order = wc_get_order( $order_data->ID ); return $order instanceof \WC_Order ? $order : null; } return null; } public function render_documents_box( $order_data ): void { $order = $this->resolve_order( $order_data ); if ( ! $order instanceof \WC_Order || ! $this->can_view( $order->get_id() ) ) { return; } ?>

label( 'description' ) . ' #' . $order->get_order_number() ); ?>

label( 'detail_open' ) ); ?> label( 'detail_print' ) ); ?>

array( 'list' => 'Otvori PDF potvrdu narudžbe', 'detail' => 'Otvori PDF potvrdu', 'description' => 'Potvrda narudžbe', 'panel' => 'Dokumenti', 'list_print' => 'Štampaj potvrdu narudžbe', 'detail_open' => 'Otvori PDF', 'detail_print' => 'Štampaj', 'print_heading' => 'Štampanje potvrde narudžbe', 'print_fallback' => 'Ako se dijalog za štampanje ne otvori, otvorite PDF i odaberite Štampaj u pregledniku.', ), 'hr' => array( 'list' => 'Otvori PDF potvrdu narudžbe', 'detail' => 'Otvori PDF potvrdu', 'description' => 'Potvrda narudžbe', 'panel' => 'Dokumenti', 'list_print' => 'Ispiši potvrdu narudžbe', 'detail_open' => 'Otvori PDF', 'detail_print' => 'Ispiši', 'print_heading' => 'Ispis potvrde narudžbe', 'print_fallback' => 'Ako se dijalog za ispis ne otvori, otvorite PDF i odaberite Ispis u pregledniku.', ), 'sr' => array( 'list' => 'Otvori PDF potvrdu porudžbine', 'detail' => 'Otvori PDF potvrdu', 'description' => 'Potvrda porudžbine', 'panel' => 'Dokumenti', 'list_print' => 'Štampaj potvrdu porudžbine', 'detail_open' => 'Otvori PDF', 'detail_print' => 'Štampaj', 'print_heading' => 'Štampanje potvrde porudžbine', 'print_fallback' => 'Ako se dijalog za štampanje ne otvori, otvorite PDF i izaberite Štampaj u pregledaču.', ), 'en' => array( 'list' => 'Open order confirmation PDF', 'detail' => 'Open confirmation PDF', 'description' => 'Order confirmation', 'panel' => 'Documents', 'list_print' => 'Print order confirmation', 'detail_open' => 'Open PDF', 'detail_print' => 'Print', 'print_heading' => 'Print order confirmation', 'print_fallback' => 'If the print dialog does not open, open the PDF and choose Print in your browser.', ), ); $settings = $this->settings->get_settings(); $language = (string) ( $settings['languages']['admin_language'] ?? 'bs' ); return $copy[ $language ][ $context ] ?? $copy['bs'][ $context ] ?? ''; } public static function url( int $order_id ): string { return wp_nonce_url( add_query_arg( array( 'action' => 'lmcc_admin_order_pdf', 'order_id' => $order_id, ), admin_url( 'admin-post.php' ) ), 'lmcc_admin_order_pdf_' . $order_id ); } public function handle(): void { $order = $this->authorize_order( 'pdf' ); try { $pdf = $this->service->render_order( $order ); } catch ( \Throwable $throwable ) { unset( $throwable ); wp_die( esc_html__( 'The PDF could not be generated safely.', 'leanmerce-commerce-assistant' ), '', array( 'response' => 500 ) ); } header( 'Content-Type: application/pdf' ); header( 'Content-Disposition: inline; filename="' . $this->service->filename( $order ) . '"' ); header( 'Content-Length: ' . strlen( $pdf ) ); echo $pdf; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Binary PDF generated by the existing central Free service. exit; } public static function print_url( int $order_id ): string { return wp_nonce_url( add_query_arg( array( 'action' => 'lmcc_admin_order_print', 'order_id' => $order_id, ), admin_url( 'admin-post.php' ) ), 'lmcc_admin_order_print_' . $order_id ); } public function handle_print(): void { $order = $this->authorize_order( 'print' ); header( 'Content-Type: text/html; charset=UTF-8' ); header( 'X-Frame-Options: SAMEORIGIN' ); header( "Content-Security-Policy: default-src 'none'; script-src 'self'; style-src 'self'; frame-src 'self'; object-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'self'" ); wp_enqueue_style( 'lmcc-order-pdf-print', plugin_dir_url( LMCC_PLUGIN_FILE ) . 'assets/css/order-pdf-print.css', array(), LMCC_VERSION ); wp_enqueue_script( 'lmcc-order-pdf-print', plugin_dir_url( LMCC_PLUGIN_FILE ) . 'assets/js/order-pdf-print.js', array(), LMCC_VERSION, true ); $settings = $this->settings->get_settings(); $language = (string) ( $settings['languages']['admin_language'] ?? 'bs' ); $language = in_array( $language, array( 'bs', 'hr', 'sr', 'en' ), true ) ? $language : 'bs'; ?> <?php echo esc_html( $this->label( 'print_heading' ) . ' #' . $order->get_order_number() ); ?>

label( 'print_heading' ) . ' #' . $order->get_order_number() ); ?>

label( 'detail_open' ) ); ?>

label( 'print_fallback' ) ); ?>

403 ) ); } // Reject arrays, negatives, zero, suffixes and overflowing IDs, not just absint coercions. RequestGuard::unique_query( array( 'order_id', '_wpnonce' ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Strict original decimal parser rejects corrupted IDs rather than sanitizing them into another valid order ID. $order_id = RequestGuard::positive_id( $_GET['order_id'] ?? null ); if ( null === $order_id ) { wp_die( esc_html__( 'Invalid order.', 'leanmerce-commerce-assistant' ), '', array( 'response' => 400 ) ); } if ( ! wp_verify_nonce( RequestGuard::nonce( 'lmcc_admin_order_' . $action . '_' . $order_id ), 'lmcc_admin_order_' . $action . '_' . $order_id ) ) { RequestGuard::reject( 403 ); } $order = function_exists( 'wc_get_order' ) ? wc_get_order( $order_id ) : false; if ( ! $order instanceof \WC_Order || ! $this->can_view( $order_id ) ) { wp_die( esc_html__( 'You are not allowed to download this document.', 'leanmerce-commerce-assistant' ), '', array( 'response' => 403 ) ); } return $order; } }