Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add document link to emails for guest customers #1039

Merged
merged 21 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions includes/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ public function backend_scripts_styles( $hook ) {
'use_latest_settings',
'mark_printed',
'unmark_printed',
'include_encrypted_pdf'
'include_encrypted_pdf',
'include_email_link',
'include_email_link_placement',
) ),
'pointers' => array(
'wcpdf_document_settings_sections' => array(
Expand Down Expand Up @@ -206,7 +208,7 @@ public function backend_scripts_styles( $hook ) {
array( 'jquery' ),
WPO_WCPDF_VERSION
);

// status/debug page scripts
if ( 'debug' === $tab ) {
wp_enqueue_style(
Expand Down Expand Up @@ -252,7 +254,7 @@ public function backend_scripts_styles( $hook ) {
);

}

// ubl taxes
if ( 'ubl' === $tab ) {
wp_enqueue_script(
Expand All @@ -262,7 +264,7 @@ public function backend_scripts_styles( $hook ) {
WPO_WCPDF_VERSION,
true
);

wp_localize_script(
'wpo-wcpdf-ubl',
'wpo_wcpdf_ubl',
Expand Down
50 changes: 48 additions & 2 deletions includes/Documents/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public function init_settings() {
*/
public function get_pdf_settings_fields( $option_name ) {
$wp_filesystem = wpo_wcpdf_get_wp_filesystem();

$settings_fields = array(
array(
'type' => 'section',
Expand Down Expand Up @@ -553,6 +553,50 @@ public function get_pdf_settings_fields( $option_name ) {
),
);

if ( 'guest' === WPO_WCPDF()->endpoint->get_document_link_access_type() ) {
$settings_fields[] = array(
'type' => 'setting',
'id' => 'include_email_link',
'title' => __( 'Include document link in emails', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'select',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'include_email_link',
'options_callback' => array( $this, 'get_wc_emails' ),
'multiple' => true,
'enhanced_select' => true,
'description' => sprintf(
/* translators: 1. opening anchor tag, 2. closing anchor tag */
__( 'Select emails to include the document link. This applies only to emails sent to "Guest" customers when the "Guest" access type is selected. %1$sCheck document link access type%2$s', 'woocommerce-pdf-invoices-packing-slips' ),
'<a target="_blank" href="' . esc_url( admin_url( 'admin.php?page=wpo_wcpdf_options_page&tab=debug&section=settings' ) ) . '">',
'</a>'
)
),
);

$settings_fields[] = array(
'type' => 'setting',
'id' => 'include_email_link_placement',
'title' => __( 'Document link position in emails', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'select',
'section' => $this->type,
'args' => array(
'option_name' => $option_name,
'id' => 'include_email_link_placement',
'options' => apply_filters( 'wpo_wcpdf_document_link_guest_emails_template_hooks_options', array(
'order_details' => 'Order details',
'order_meta' => 'Order meta',
'before_order_table' => 'Before order table',
'after_order_table' => 'After order table',
'customer_address_section' => 'Customer address section',
'customer_details' => 'Customer details',
), $this ),
'description' => __( 'Select the placement of the document link in the guest customer emails.', 'woocommerce-pdf-invoices-packing-slips' ),
),
);
}

// remove/rename some fields when invoice number is controlled externally
if ( apply_filters( 'woocommerce_invoice_number_by_plugin', false ) ) {
$remove_settings = array( 'next_invoice_number', 'number_format', 'reset_number_yearly' );
Expand All @@ -579,7 +623,7 @@ public function get_pdf_settings_fields( $option_name ) {
*/
public function get_ubl_settings_fields( $option_name ) {
$wp_filesystem = wpo_wcpdf_get_wp_filesystem();

$settings_fields = array(
array(
'type' => 'section',
Expand Down Expand Up @@ -668,6 +712,8 @@ public function get_settings_categories( string $output_format ): array {
'members' => array(
'enabled',
'attach_to_email_ids',
'include_email_link',
'include_email_link_placement',
'disable_for_statuses',
'my_account_buttons',
),
Expand Down
9 changes: 5 additions & 4 deletions includes/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,21 @@ public function handle_document_requests() {
}
}

public function get_document_link( $order, $document_type, $additional_vars = array() ) {
public function get_document_link( $order, $document_type, $additional_vars = array(), bool $bypass_login_access = false ) {
if ( empty( $order ) || empty( $document_type ) ) {
return '';
}

$access_type = $this->get_document_link_access_type();
$access_type = $this->get_document_link_access_type();
$is_user_logged_in = is_user_logged_in() && ! $bypass_login_access;

switch ( $access_type ) {
case 'logged_in':
default:
$access_key = is_user_logged_in() ? wp_create_nonce( $this->actions['generate'] ) : '';
$access_key = $is_user_logged_in ? wp_create_nonce( $this->actions['generate'] ) : '';
break;
case 'guest': // 'guest' is hybrid, it can behave as 'logged_in' if the user is logged in, but if not, behaves as 'full'
$access_key = ! is_user_logged_in() ? $order->get_order_key() : wp_create_nonce( $this->actions['generate'] );
$access_key = ! $is_user_logged_in ? $order->get_order_key() : wp_create_nonce( $this->actions['generate'] );
break;
case 'full':
$access_key = $order->get_order_key();
Expand Down
88 changes: 88 additions & 0 deletions includes/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public function __construct() {
add_action( 'wpo_wcpdf_after_order_data', array( $this, 'display_due_date_table_row' ), 10, 2 );

add_action( 'wpo_wcpdf_delete_document', array( $this, 'log_document_deletion_to_order_notes' ) );

// Add document link to emails
add_action( 'init', array( $this, 'handle_document_link_in_emails' ) );
}

/**
Expand Down Expand Up @@ -1776,6 +1779,91 @@ function_exists( 'WPO_WCPDF_Templates' ) &&
</tr>';
}
}

function handle_document_link_in_emails(): void {
$email_hooks = array();
$documents = WPO_WCPDF()->documents->get_documents();

foreach ( $documents as $document ) {
$document_settings = WPO_WCPDF()->settings->get_document_settings( $document->get_type(), 'pdf' );
$email_placement = $document_settings['include_email_link_placement'] ?? '';

if ( ! empty( $email_placement ) ) {
$email_hooks[] = 'woocommerce_email_' . $email_placement;
}
}

$email_hooks = apply_filters( 'wpo_wcpdf_add_document_link_to_email_hooks', $email_hooks );

foreach ( $email_hooks as $email_hook ) {
add_action( $email_hook, array( $this, 'add_document_link_to_email' ), 10, 4 );
}
}

/**
* Add document download link to the email.
*
* @param \WC_Abstract_Order $order
* @param bool $sent_to_admin
* @param bool $plain_text
* @param \WC_Email $email
*
* @return void
*/
public function add_document_link_to_email( \WC_Abstract_Order $order, bool $sent_to_admin, bool $plain_text, \WC_Email $email ): void {
// Check if document access type is 'guest' and customer is a guest.
$is_guest_access_type = 'guest' === WPO_WCPDF()->endpoint->get_document_link_access_type();
$is_customer_guest = 0 === $order->get_customer_id();

// Early exit if the requirements are not met
if ( ! apply_filters( 'wpo_wcpdf_add_document_link_to_email_requirements_met', $is_guest_access_type && $is_customer_guest, $order, $sent_to_admin, $plain_text, $email ) ) {
return;
}

$allowed_document_types = apply_filters( 'wpo_wcpdf_add_document_link_to_email_allowed_document_types', array( 'invoice' ), $order, $sent_to_admin, $plain_text, $email );
$documents = WPO_WCPDF()->documents->get_documents();

foreach ( $documents as $document ) {
$document_settings = WPO_WCPDF()->settings->get_document_settings( $document->get_type(), 'pdf' );
$selected_emails = $document_settings['include_email_link'] ?? array();

$is_allowed = in_array( $document->get_type(), $allowed_document_types, true ) && in_array( $email->id, $selected_emails, true );

if ( ! apply_filters( 'wpo_wcpdf_add_document_link_to_email_is_allowed', $is_allowed, $order, $sent_to_admin, $plain_text, $email ) ) {
continue;
}

$document = wcpdf_get_document( $document->get_type(), $order );

if ( ! $document ) {
MohamadNateqi marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

if (
! $document->exists() &&
! apply_filters( 'wpo_wcpdf_add_document_link_to_email_allow_missing_documents', false, $document, $order, $sent_to_admin, $plain_text, $email )
MohamadNateqi marked this conversation as resolved.
Show resolved Hide resolved
MohamadNateqi marked this conversation as resolved.
Show resolved Hide resolved
) {
continue;
}

$link_text = sprintf(
/* translators: %s: Document type */
__( 'View %s (PDF)', 'woocommerce-pdf-invoices-packing-slips' ),
wp_kses_post( $document->get_type() )
);
$link_url = WPO_WCPDF()->endpoint->get_document_link( $order, $document->get_type(), array(), true );

$document_link = sprintf(
'<p><a id="%s" href="%s" target="_blank">%s</a></p>',
esc_attr( 'wpo_wcpdf_' . $document->get_type() . '_document_link' ),
esc_url( $link_url ),
esc_html( $link_text )
);

echo apply_filters( 'wpo_wcpdf_add_document_download_link_to_email', $document_link, $document, $order, $sent_to_admin, $plain_text, $email );
}
}

}

endif; // class_exists
Expand Down
6 changes: 3 additions & 3 deletions includes/Settings/SettingsCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function checkbox_text_input( $args ) {
ob_start();
$this->text_input( $input_args );
$text_input = ob_get_clean();

$allowed_html = array(
'input' => array(
'type' => true,
Expand All @@ -226,7 +226,7 @@ public function checkbox_text_input( $args ) {
if ( ! empty( $text_input_wrap ) ) {
$text_input = sprintf( $text_input_wrap, $text_input );
}

echo wp_kses( "{$checkbox} {$text_input}", $allowed_html );

// output description.
Expand Down Expand Up @@ -306,7 +306,7 @@ public function select( $args ) {
$title = ! empty( $title ) ? esc_attr( $title ) : '';
$class = 'wc-enhanced-select wpo-wcpdf-enhanced-select';
$css = 'width:400px';
printf( '<select id="%1$s" name="%2$s" data-placeholder="%3$s" title="%4$s" class="%5$s" style="%6$s" %7$s>', esc_attr( $id ), esc_attr( $setting_name ), esc_attr( $placeholder ), esc_attr( $title ), esc_attr( $class ), esc_attr( $css ), esc_attr( $multiple ) );
printf( '<select id="%1$s" name="%2$s" data-placeholder="%3$s" title="%4$s" class="%5$s" style="%6$s" %7$s %8$s>', esc_attr( $id ), esc_attr( $setting_name ), esc_attr( $placeholder ), esc_attr( $title ), esc_attr( $class ), esc_attr( $css ), esc_attr( $multiple ), ! empty( $disabled ) ? 'disabled="disabled"' : '' );
} else {
printf( '<select id="%1$s" name="%2$s">', esc_attr( $id ), esc_attr( $setting_name ) );
}
Expand Down
4 changes: 2 additions & 2 deletions includes/Settings/SettingsDebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function get_number_store_tables() {
$tables = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
"SHOW TABLES LIKE '{$wpdb->prefix}wcpdf_%'"
);

$document_titles = WPO_WCPDF()->documents->get_document_titles();
$table_names = array();

Expand Down Expand Up @@ -905,7 +905,7 @@ public function init_settings() {
'id' => 'log_missing_translations',
'description' => __( 'Enable this option to log dynamic strings that could not be translated. This can help you identify which strings need to be registered for translation.', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
),
array(
'type' => 'setting',
'id' => 'disable_preview',
Expand Down