( $order ) ) { if ( is_callable( [ $document, 'get_type' ] ) && $document->get_type() == 'credit-note' && is_callable( [ $order, 'get_parent_id' ] ) ) { $order = wc_get_order( $order->get_parent_id() ); } if ( empty( $order ) ) { continue; } $status = $order->get_meta( "_wcpdf_{$document->slug}_creation_trigger" ); if ( true == $force || empty( $status ) ) { $order->update_meta_data( "_wcpdf_{$document->slug}_creation_trigger", $trigger ); $order->save_meta_data(); } } } } } /** * Get the document triggers * * @return array */ public function get_document_triggers() { return apply_filters( 'wpo_wcpdf_document_triggers', [ 'single' => __( 'single order action', 'woocommerce-pdf-invoices-packing-slips' ), 'bulk' => __( 'bulk order action', 'woocommerce-pdf-invoices-packing-slips' ), 'my_account' => __( 'my account', 'woocommerce-pdf-invoices-packing-slips' ), 'email_attachment' => __( 'email attachment', 'woocommerce-pdf-invoices-packing-slips' ), 'document_data' => __( 'order document data (number and/or date set manually)', 'woocommerce-pdf-invoices-packing-slips' ), ] ); } /** * Mark document printed * * @return void */ public function mark_document_printed( $document, $trigger ) { $triggers = isset( $document->latest_settings['mark_printed'] ) && is_array( $document->latest_settings['mark_printed'] ) ? $document->latest_settings['mark_printed'] : []; if ( ! empty( $document ) && ! $this->is_document_printed( $document ) ) { if ( ! empty( $order = $document->order ) && ! empty( $trigger ) && in_array( $trigger, $triggers ) && apply_filters( 'wpo_wcpdf_allow_mark_document_printed', true, $document, $trigger ) ) { if ( 'shop_order' === $order->get_type() ) { $data = [ 'date' => time(), 'trigger' => $trigger, ]; $order->update_meta_data( "_wcpdf_{$document->slug}_printed", $data ); $order->save_meta_data(); $this->log_document_printed_to_order_notes( $document, $trigger ); } } } } /** * Unmark document printed * * @return void */ public function unmark_document_printed( $document ) { if ( ! empty( $document ) && $this->is_document_printed( $document ) ) { if ( ! empty( $order = $document->order ) && apply_filters( 'wpo_wcpdf_allow_unmark_document_printed', true, $document ) ) { $meta_key = "_wcpdf_{$document->slug}_printed"; if ( 'shop_order' === $order->get_type() && ! empty( $order->get_meta( $meta_key ) ) ) { $order->delete_meta_data( $meta_key ); $order->save_meta_data(); $this->log_unmark_document_printed_to_order_notes( $document ); } } } } /** * AJAX request for mark/unmark document printed * * @return void */ public function document_printed_ajax() { check_ajax_referer( 'printed_wpo_wcpdf', 'security' ); $data = stripslashes_deep( $_REQUEST ); $error = 0; if ( ! empty( $data['action'] ) && $data['action'] == "printed_wpo_wcpdf" && ! empty( $data['event'] ) && ! empty( $data['document_type'] ) && ! empty( $data['order_id'] ) && ! empty( $data['trigger'] ) ) { $document = wcpdf_get_document( esc_attr( $data['document_type'] ), esc_attr( $data['order_id'] ) ); $full_permission = WPO_WCPDF()->admin->user_can_manage_document( esc_attr( $data['document_type'] ) ); if ( ! empty( $document ) && ! empty( $order = $document->order ) && $full_permission ) { switch ( esc_attr( $data['event'] ) ) { case 'mark': $this->mark_document_printed( $document, esc_attr( $data['trigger'] ) ); break; case 'unmark': $this->unmark_document_printed( $document ); break; } if ( is_callable( [ $order, 'get_edit_order_url' ] ) ) { wp_redirect( $order->get_edit_order_url() ); } else { wp_redirect( admin_url( 'post.php?action=edit&post=' . esc_attr( $data['order_id'] ) ) ); } } else { $error++; } } else { $error++; } if ( $error > 0 ) { /* translators: 1. document type, 2. mark/unmark */ wp_die( sprintf( esc_html__( 'Document of type %1$s for the selected order could not be %2$s as printed.', 'woocommerce-pdf-invoices-packing-slips' ), esc_attr( $data['document_type'] ), $event_type ) ); } } /** * Check if a document is printed * * @return bool */ public function is_document_printed( $document ) { $is_printed = false; if ( ! empty( $document ) && ! empty( $order = $document->order ) ) { if ( 'shop_order' === $order->get_type() && ! empty( $printed_data = $order->get_meta( "_wcpdf_{$document->slug}_printed" ) ) ) { $is_printed = true; } } return $is_printed; } /** * Check if a document can be manually marked as printed * * @return bool */ public function document_can_be_manually_marked_printed( $document ) { $can_be_manually_marked_printed = false; if ( empty( $document ) || ( property_exists( $document, 'is_bulk' ) && $document->is_bulk ) ) { return $can_be_manually_marked_printed; } $document->save_settings(); $can_be_manually_marked_printed = false; $document_exists = is_callable( array( $document, 'exists' ) ) ? $document->exists() : false; $document_printed = $document_exists && is_callable( array( $document, 'printed' ) ) ? $document->printed() : false; $triggers = isset( $document->latest_settings['mark_printed'] ) && is_array( $document->latest_settings['mark_printed'] ) ? $document->latest_settings['mark_printed'] : []; $manually_print_enabled = in_array( 'manually', $triggers ) ? true : false; if ( $document_exists && ! $document_printed && $manually_print_enabled ) { $can_be_manually_marked_printed = true; } return apply_filters( 'wpo_wcpdf_document_can_be_manually_marked_printed', $can_be_manually_marked_printed, $document ); } /** * Get document printed data * * @return array */ public function get_document_printed_data( $document ) { $data = []; if ( ! empty( $document ) && $this->is_document_printed( $document ) && ! empty( $order = $document->order ) ) { if ( 'shop_order' === $order->get_type() && ! empty( $printed_data = $order->get_meta( "_wcpdf_{$document->slug}_printed" ) ) ) { $data = $printed_data; } } return apply_filters( 'wpo_wcpdf_document_printed_data', $data, $document ); } /** * Enable PHP error output */ public function enable_debug () { error_reporting( E_ALL ); ini_set( 'display_errors', 1 ); } public function wc_webhook_topic_hooks( $topic_hooks, $wc_webhook ) { $documents = WPO_WCPDF()->documents->get_documents(); foreach ($documents as $document) { $topic_hooks["order.{$document->type}-saved"] = array( "wpo_wcpdf_webhook_order_{$document->slug}_saved", ); } return $topic_hooks; } public function wc_webhook_topic_events( $topic_events ) { $documents = WPO_WCPDF()->documents->get_documents(); foreach ($documents as $document) { $topic_events[] = "{$document->type}-saved"; } return $topic_events; } public function wc_webhook_topics( $topics ) { $documents = WPO_WCPDF()->documents->get_documents(); foreach ($documents as $document) { /* translators: document title */ $topics["order.{$document->type}-saved"] = esc_html( sprintf( __( 'Order %s Saved', 'woocommerce-pdf-invoices-packing-slips' ), $document->get_title() ) ); } return $topics; } public function wc_webhook_trigger( $document, $order ) { do_action( "wpo_wcpdf_webhook_order_{$document->slug}_saved", $order->get_id() ); } /** * @param null|string $document_type * @param null|\WC_Order|\WC_Order_Refund $order * * @return void */ public function display_due_date( string $document_type = null, $order = null ): void { if ( empty( $order ) || empty( $document_type ) ) { return; } $document = wcpdf_get_document( $document_type, $order ); if ( ! $document ) { return; } $due_date_timestamp = is_callable( array( $document, 'get_due_date' ) ) ? $document->get_due_date() : 0; if ( 0 >= $due_date_timestamp ) { return; } $due_date = apply_filters( 'wpo_wcpdf_due_date_display', date_i18n( wcpdf_date_format( $this, 'due_date' ), $due_date_timestamp ), $due_date_timestamp, $document_type, $document ); $due_date_title = is_callable( array( $document, 'get_due_date_title' ) ) ? $document->get_due_date_title() : __( 'Due Date:', 'woocommerce-pdf-invoices-packing-slips' ); if ( ! empty( $due_date ) ) { echo ' ', $due_date_title, ' ', $due_date, ' '; } } } endif; // class_exists