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

New: adds a document denied redirect page setting #587

Merged
merged 12 commits into from
Sep 1, 2023
5 changes: 5 additions & 0 deletions assets/css/debug-tools.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ form#wpo-wcpdf-settings {
padding: 0 24px 0 8px;
min-height: 30px;
max-width: 25rem;
}

#document_custom_redirect_page,
#document_custom_redirect_page + .description {
display: none;
}
2 changes: 1 addition & 1 deletion assets/css/debug-tools.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion assets/css/settings-styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ body.woocommerce_page_wpo_wcpdf_options_page {

#wpo-wcpdf-preview-wrapper .sidebar .form-table > tr > td > input[type="text"],
#wpo-wcpdf-preview-wrapper .sidebar .form-table > tbody > tr > td > input[type="text"],
#wpo-wcpdf-preview-wrapper .sidebar .form-table > tr > td > input[type="url"],
#wpo-wcpdf-preview-wrapper .sidebar .form-table > tbody > tr > td > input[type="url"],
#wpo-wcpdf-preview-wrapper .sidebar .form-table > tr > td > select,
#wpo-wcpdf-preview-wrapper .sidebar .form-table > tbody > tr > td > select,
#wpo-wcpdf-preview-wrapper .sidebar .form-table > tr > td > textarea,
Expand All @@ -348,7 +350,8 @@ body.woocommerce_page_wpo_wcpdf_options_page {
width: 100%;
}

#wpo-wcpdf-preview-wrapper input[type="text"][size] {
#wpo-wcpdf-preview-wrapper input[type="text"][size],
#wpo-wcpdf-preview-wrapper input[type="url"][size] {
width: auto !important;
max-width: 100% !important;
}
Expand Down
2 changes: 1 addition & 1 deletion assets/css/settings-styles.min.css

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions assets/js/debug-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,18 @@ jQuery( function( $ ) {
}
}

// toggle custom redirect page
$( "[name='wpo_wcpdf_settings_debug[document_access_denied_redirect_page]']" ).on( 'change', function( event ) {
let $custom_page_field = $( this ).closest( 'table' ).find( '#document_custom_redirect_page' );
let $field_description = $custom_page_field.closest( 'td' ).find( '.description' );

if ( 'custom_page' === $( this ).val() ) {
$custom_page_field.show();
$field_description.show();
} else {
$custom_page_field.hide();
$field_description.hide();
}
} ).trigger( 'change' );

} );
2 changes: 1 addition & 1 deletion assets/js/debug-script.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions includes/class-wcpdf-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,37 @@ public function get_document_link_access_type() {
return apply_filters( 'wpo_wcpdf_document_link_access_type', $access_type, $this );
}

/**
* Get document denied frontend redirect URL
*
* @return string
*/
public function get_document_denied_frontend_redirect_url() {
$redirect_url = '';
$debug_settings = get_option( 'wpo_wcpdf_settings_debug', array() );

if ( isset( $debug_settings['document_access_denied_redirect_page'] ) ) {
switch ( $debug_settings['document_access_denied_redirect_page'] ) {
case 'login_page':
$redirect_url = wp_sanitize_redirect( wp_login_url() );
break;
case 'myaccount_page':
$redirect_url = wp_sanitize_redirect( wc_get_page_permalink( 'myaccount' ) );
break;
case 'custom_page':
if ( isset( $debug_settings['document_custom_redirect_page'] ) && ! empty( $debug_settings['document_custom_redirect_page'] ) ) {
$redirect_url = wp_sanitize_redirect( $debug_settings['document_custom_redirect_page'] );
}
break;
case 'blank_page':
default:
break;
}
}

return apply_filters( 'wpo_wcpdf_document_denied_frontend_redirect_url', $redirect_url, $debug_settings, $this );
}

}

endif; // class_exists
40 changes: 28 additions & 12 deletions includes/class-wcpdf-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ public function get_documents_for_email( $email_id, $order ) {
* Load and generate the template output with ajax
*/
public function generate_pdf_ajax() {
$access_type = WPO_WCPDF()->endpoint->get_document_link_access_type();
$access_type = WPO_WCPDF()->endpoint->get_document_link_access_type();
$redirect_url = WPO_WCPDF()->endpoint->get_document_denied_frontend_redirect_url();

// handle legacy access keys
if ( empty( $_REQUEST['access_key'] ) ) {
Expand All @@ -285,17 +286,20 @@ public function generate_pdf_ajax() {

// check if we have the access key set
if ( empty( $_REQUEST['access_key'] ) ) {
wp_die( esc_attr__( 'You do not have sufficient permissions to access this page. Reason: empty access key', 'woocommerce-pdf-invoices-packing-slips' ) );
$message = esc_attr__( 'You do not have sufficient permissions to access this page. Reason: empty access key', 'woocommerce-pdf-invoices-packing-slips' );
wcpdf_safe_redirect_or_die( $redirect_url, $message );
}

// check if we have the action
if ( empty( $_REQUEST['action'] ) ) {
wp_die( esc_attr__( 'You do not have sufficient permissions to access this page. Reason: empty action', 'woocommerce-pdf-invoices-packing-slips' ) );
$message = esc_attr__( 'You do not have sufficient permissions to access this page. Reason: empty action', 'woocommerce-pdf-invoices-packing-slips' );
wcpdf_safe_redirect_or_die( $redirect_url, $message );
}

// Check the nonce - guest access can use nonce if user is logged in
if ( is_user_logged_in() && in_array( $access_type, array( 'logged_in', 'guest' ) ) && ! $valid_nonce ) {
wp_die( esc_attr__( 'You do not have sufficient permissions to access this page. Reason: invalid nonce', 'woocommerce-pdf-invoices-packing-slips' ) );
$message = esc_attr__( 'You do not have sufficient permissions to access this page. Reason: invalid nonce', 'woocommerce-pdf-invoices-packing-slips' );
wcpdf_safe_redirect_or_die( $redirect_url, $message );
}

// Check if all parameters are set
Expand All @@ -304,11 +308,13 @@ public function generate_pdf_ajax() {
}

if ( empty( $_REQUEST['order_ids'] ) ) {
wp_die( esc_attr__( "You haven't selected any orders", 'woocommerce-pdf-invoices-packing-slips' ) );
$message = esc_attr__( "You haven't selected any orders", 'woocommerce-pdf-invoices-packing-slips' );
wcpdf_safe_redirect_or_die( null, $message );
}

if ( empty( $_REQUEST['document_type'] ) ) {
wp_die( esc_attr__( 'Some of the export parameters are missing.', 'woocommerce-pdf-invoices-packing-slips' ) );
$message = esc_attr__( 'Some of the export parameters are missing.', 'woocommerce-pdf-invoices-packing-slips' );
wcpdf_safe_redirect_or_die( null, $message );
}

// debug enabled by URL
Expand All @@ -326,10 +332,15 @@ public function generate_pdf_ajax() {
$order = wc_get_order( $order_id );

if ( $order && $order->get_status() == 'auto-draft' ) {
wp_die( esc_attr__( 'You have to save the order before generating a PDF document for it.', 'woocommerce-pdf-invoices-packing-slips' ) );
$message = esc_attr__( 'You have to save the order before generating a PDF document for it.', 'woocommerce-pdf-invoices-packing-slips' );
wcpdf_safe_redirect_or_die( null, $message );
} elseif ( ! $order ) {
/* translators: %s: Order ID */
wp_die( sprintf( esc_attr__( 'Could not find the order #%s.', 'woocommerce-pdf-invoices-packing-slips' ), $order_id ) );
$message = sprintf(
/* translators: %s: Order ID */
esc_attr__( 'Could not find the order #%s.', 'woocommerce-pdf-invoices-packing-slips' ),
$order_id
);
wcpdf_safe_redirect_or_die( null, $message );
}
}

Expand Down Expand Up @@ -403,7 +414,8 @@ public function generate_pdf_ajax() {
$allowed = apply_filters( 'wpo_wcpdf_check_privs', $allowed, $order_ids );

if ( ! $allowed ) {
wp_die( esc_attr__( 'You do not have sufficient permissions to access this page.', 'woocommerce-pdf-invoices-packing-slips' ) );
$message = esc_attr__( 'You do not have sufficient permissions to access this page.', 'woocommerce-pdf-invoices-packing-slips' );
wcpdf_safe_redirect_or_die( $redirect_url, $message );
}

// if we got here, we're safe to go!
Expand Down Expand Up @@ -455,8 +467,12 @@ public function generate_pdf_ajax() {
break;
}
} else {
/* translators: document type */
wp_die( sprintf( esc_html__( "Document of type '%s' for the selected order(s) could not be generated", 'woocommerce-pdf-invoices-packing-slips' ), $document_type ) );
$message = sprintf(
/* translators: document type */
esc_html__( "Document of type '%s' for the selected order(s) could not be generated", 'woocommerce-pdf-invoices-packing-slips' ),
$document_type
);
wcpdf_safe_redirect_or_die( null, $message );
}
} catch ( \Dompdf\Exception $e ) {
$message = 'DOMPDF Exception: '.$e->getMessage();
Expand Down
29 changes: 29 additions & 0 deletions includes/settings/class-wcpdf-settings-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,35 @@ public function text_input( $args ) {
printf( '<p class="description">%s</p>', wp_kses_post( $description ) );
}
}

/**
* URL input callback.
*
* args:
* option_name - name of the main option
* id - key of the setting
* size - size of the text input (em)
* default - default setting (optional)
* description - description (optional)
* type - type (optional)
*
* @return void.
*/
public function url_input( $args ) {
extract( $this->normalize_settings_args( $args ) );

if ( empty( $type ) ) {
$type = 'url';
}

$size = ! empty( $size ) ? sprintf( 'size="%s"', esc_attr( $size ) ) : '';
printf( '<input type="%1$s" id="%2$s" name="%3$s" value="%4$s" %5$s placeholder="%6$s" %7$s/>', esc_attr( $type ), esc_attr( $id ), esc_attr( $setting_name ), sanitize_url( $current ), $size, esc_attr( $placeholder ), ! empty( $disabled ) ? 'disabled="disabled"' : '' );

// output description.
if ( ! empty( $description ) ) {
printf( '<p class="description">%s</p>', wp_kses_post( $description ) );
}
}

/**
* Combined checkbox & text input callback.
Expand Down
32 changes: 32 additions & 0 deletions includes/settings/class-wcpdf-settings-debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,38 @@ public function init_settings() {
'option_name' => $option_name,
),
),
array(
'type' => 'setting',
'id' => 'document_access_denied_redirect_page',
'title' => __( 'Document access denied redirect page', 'woocommerce-pdf-invoices-packing-slips' ),
'callback' => 'select',
'section' => 'debug_settings',
'args' => array(
'option_name' => $option_name,
'id' => 'document_access_denied_redirect_page',
'default' => 'blank',
'options' => array(
'blank_page' => __( 'Blank page with message (default)', 'woocommerce-pdf-invoices-packing-slips' ),
'login_page' => __( 'Login page', 'woocommerce-pdf-invoices-packing-slips' ),
'myaccount_page' => __( 'My Account page', 'woocommerce-pdf-invoices-packing-slips' ),
'custom_page' => __( 'Custom page (enter below)', 'woocommerce-pdf-invoices-packing-slips' ),
),
'description' => __( 'Select a frontend page to be used to redirect users when the document access is denied.', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
'type' => 'setting',
'id' => 'document_custom_redirect_page',
'title' => '',
'callback' => 'url_input',
'section' => 'debug_settings',
'args' => array(
'option_name' => $option_name,
'id' => 'document_custom_redirect_page',
'placeholder' => esc_url_raw( wc_get_page_permalink( 'shop' ) ),
'description' => __( 'Custom external URLs not allowed.', 'woocommerce-pdf-invoices-packing-slips' ),
)
),
array(
'type' => 'setting',
'id' => 'pretty_document_links',
Expand Down
20 changes: 20 additions & 0 deletions includes/wcpdf-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,23 @@ function wcpdf_convert_encoding( $string, $tool = 'mb_convert_encoding' ) {

return $string;
}

/**
* Safe redirect or die.
*
* @param string $url
* @param string|WP_Error $message
* @return void
*/
function wcpdf_safe_redirect_or_die( $url = '', $message = '' ) {
if ( ! empty( $url ) ) {
wp_safe_redirect( $url );
alexmigf marked this conversation as resolved.
Show resolved Hide resolved
exit;
} else {
wp_die( $message );
}
}

function WPO_WCPDF_Legacy() {
return \WPO\WC\PDF_Invoices\Legacy\WPO_WCPDF_Legacy::instance();
}