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

Changes to camptix-invoices plugin #965

Open
wants to merge 15 commits into
base: production
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
add_filter( 'camptix_metabox_questions_default_fields_list', __NAMESPACE__ . '\modify_default_fields_list' );
add_filter( 'camptix_capabilities', __NAMESPACE__ . '\modify_capabilities' );
add_filter( 'camptix_default_options', __NAMESPACE__ . '\modify_default_options' );
add_filter( 'camptix_options', __NAMESPACE__ . '\handle_invoices_company' );
add_filter( 'camptix_options', __NAMESPACE__ . '\modify_email_templates' );
add_filter( 'camptix_email_tickets_template', __NAMESPACE__ . '\switch_email_template' );
add_filter( 'camptix_html_message', __NAMESPACE__ . '\render_html_emails', 10, 2 );
Expand Down Expand Up @@ -611,10 +612,106 @@ function modify_default_options( $options ) {
$options['payment_options_stripe'] = array( 'api_predef' => 'wpcs-sandbox' );
$options['payment_methods'] = array( 'paypal' => false );
$options['payment_options_paypal'] = array( 'api_predef' => 'wordcamp-sandbox' );
$options['invoice-active'] = true;
$options['invoice-vat-number'] = true;
$options['invoice-company'] = get_bloginfo( 'name' );

return $options;
}

/**
* Set Camptix Invoices company address based on paymenth methods enabled.
*
* @param array $options Camptix options.
*
* @return array Camptix options.
*/
function handle_invoices_company( $options ) {
if ( ! $options['invoice-active'] ) {
return $options;
}

$can_use_wpcs = array();

$wpcs_address = "WordPress Community Support, PBC\r\n660 4th Street #119\r\nSan Francisco CA 94107\r\nTax ID: 81-0896291";

// List of allowed predefined payment method api accounts.
$wpcs_allowed_predef = array(
'wpcs-sandbox',
'wordcamp-sandbox',
'foundation',
);

foreach ( $options['payment_methods'] as $method => $status ) {
// Default to payment method cannot use WPCS as company.
$can_use_wpcs[ $method ] = false;

// Skip if payment method is not enabled.
if ( ! $status ) {
continue;
}

// Skip if payment method is not configured.
if ( ! isset( $options[ 'payment_options_' . $method ] ) ) {
continue;
}

// Skip if payment method is not using WPCS account.
if ( ! in_array( $options[ 'payment_options_' . $method ]['api_predef'], $wpcs_allowed_predef ) ) {
continue;
}

$can_use_wpcs[ $method ] = true;
}

/**
* Check that all payment methods enable statuses match with payment methods that can use WPCS information as a company.
* If all enabled payment methods can use WPCS, force the invoice company details.
*/
if ( $options['payment_methods'] === $can_use_wpcs ) {
$options['invoice-company'] = $wpcs_address;

// Hide the field, since we are forcing the value to it.
add_filter( 'camptix_invoices_company_override', '__return_true' );
} else {
// Reset company if it is WPCS.
if ( $options['invoice-company'] === $wpcs_address ) {
$options['invoice-company'] = get_bloginfo( 'name' );
}

// Show notice about updating company info.
add_action( 'admin_notices', __NAMESPACE__ . '\show_invoices_company_notice' );
}

return $options;
}

/**
* Show notice about updating company info when Camptix payment methods are not using WPCS.
*/
function show_invoices_company_notice() {
global $camptix;

$options = $camptix->get_options();

if ( ! $options['invoice-active'] ) {
return;
}

// When not using WPCS, site name is default. If the site name isn't the value, assume that it has been updated.
if ( get_bloginfo( 'name' ) !== $options['invoice-company'] ) {
return;
}

// Prevent multiple notices.
remove_action( 'admin_notices', __NAMESPACE__ . '\show_invoices_company_notice' );

$class = 'notice notice-warning';
$message = __( 'CampTix Invoices are enabled and you don\'t use WPCS accounts on payment methods. Make sure to update your company address for invoices!', 'wordcamporg' );

printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
}

/**
* Append a footer string to some email templates.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ function _get_network_plugin_state_list( $state ) {
'activated' => array(
'akismet/akismet.php',
'bbpress-network-templates/bbpress-network-templates.php',
'camptix/camptix.php',
'camptix-admin-flags/camptix-admin-flags.php',
'camptix-attendance/camptix-attendance.php',
'camptix-badge-generator/bootstrap.php',
'camptix/camptix.php',
'camptix-invoices/camptix-invoices.php',
'camptix-network-tools/camptix-network-tools.php',
'classic-editor/classic-editor.php',
'custom-content-width/custom-content-width.php',
Expand Down Expand Up @@ -66,7 +67,6 @@ function _get_network_plugin_state_list( $state ) {
'deactivated' => array(
'bbpress/bbpress.php',
'campt-indian-payment-gateway/campt-indian-payment-gateway.php',
'camptix-invoices/camptix-invoices.php',
'camptix-mailchimp/camptix-mailchimp.php',
'camptix-mercadopago/camptix-mercadopago.php',
'camptix-pagseguro/camptix-pagseguro.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
jQuery( document ).ready( function( $ ) {
function toggleInvoiceDetailsForm( showForm ) {
const $camptixInvoiceDetailsForm = $( '.camptix-invoice-details' );
const $camptixInvoiceDetailsFormFields = $camptixInvoiceDetailsForm.find( 'input,textarea,select' );
const $camptixInvoiceDetailsFormFields = $camptixInvoiceDetailsForm.find( '#invoice-email,#invoice-name' );

if ( showForm ) {
$camptixInvoiceDetailsForm.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,7 @@ public static function invoice_settings( $section ) {
__( 'Activate invoice requests', 'wordcamporg' ),
'field_yesno',
'invoice',
// translators: %1$s is a date.
sprintf( __( 'Allow ticket buyers to ask for an invoice when purchasing their tickets.', 'wordcamporg' ), date( 'Y' ) )
);

$camptix->add_settings_field_helper(
'invoice-new-year-reset',
__( 'Yearly reset', 'wordcamporg' ),
'field_yesno',
'invoice',
// translators: %1$s is a date.
sprintf( __( 'Invoice numbers are prefixed with the year, and will be reset on the 1st of January (e.g. %1$s-125)', 'wordcamporg' ), date( 'Y' ) )
__( 'Allow ticket buyers to ask for an invoice when purchasing their tickets.', 'wordcamporg' )
);

add_settings_field(
Expand All @@ -86,7 +76,7 @@ public static function invoice_settings( $section ) {
'invoice',
array(
'id' => 'invoice-date-format',
'value' => ! empty( $opt['invoice-date-format'] ) ? $opt['invoice-date-format'] : 'd F Y',
'value' => ! empty( $opt['invoice-date-format'] ) ? $opt['invoice-date-format'] : '',
)
);

Expand All @@ -95,8 +85,16 @@ public static function invoice_settings( $section ) {
__( 'VAT number', 'wordcamporg' ),
'field_yesno',
'invoice',
// translators: %1$s is a date.
sprintf( __( 'Add a "VAT Number" field to the invoice request form', 'wordcamporg' ), date( 'Y' ) )
__( 'Add a "VAT Number" field to the invoice request form', 'wordcamporg' )
);

$camptix->add_settings_field_helper(
'invoice-new-year-reset',
__( 'Yearly reset', 'wordcamporg' ),
'field_yesno',
'invoice',
// translators: %1$s is a year.
sprintf( __( 'Invoice numbers are prefixed with the year, and will be reset on the 1st of January (e.g. %1$s-125)', 'wordcamporg' ), wp_date( 'Y' ) )
);

add_settings_field(
Expand All @@ -111,7 +109,10 @@ public static function invoice_settings( $section ) {
)
);

$camptix->add_settings_field_helper( 'invoice-company', __( 'Company address', 'wordcamporg' ), 'field_textarea', 'invoice' );
if ( ! apply_filters( 'camptix_invoices_company_override', false ) ) {
$camptix->add_settings_field_helper( 'invoice-company', __( 'Company address', 'wordcamporg' ), 'field_textarea', 'invoice' );
}

$camptix->add_settings_field_helper( 'invoice-thankyou', __( 'Note below invoice total', 'wordcamporg' ), 'field_textarea', 'invoice' );
}

Expand All @@ -124,10 +125,12 @@ public static function date_format_callback( $args ) {

$id = $args['id'];
$value = $args['value'];
$date_format = get_option( 'date_format' );
$description = sprintf(
// translators: %s is a date.
__( 'Date format to use on the invoice, as a PHP Date formatting string (default \'d F Y\' formats dates as %s)', 'wordcamporg' ),
date( 'd F Y' )
__( 'Date format to use on the invoice, as a PHP Date formatting string (default %1$s formats dates as %2$s)', 'wordcamporg' ),
$date_format,
wp_date( $date_format )
);

include CTX_INV_DIR . '/includes/views/date-format-field.php';
Expand Down Expand Up @@ -334,7 +337,12 @@ public static function send_invoice( $invoice_id ) {
if ( empty( $invoice_metas['email'] ) && is_email( $invoice_metas['email'] ) ) {
return false;
}//end if

$invoice_pdf = ctx_get_invoice( $invoice_id );
if ( empty( $invoice_pdf ) ) {
return false;
}

$attachments = array( $invoice_pdf );
$opt = get_option( 'camptix_options' );

Expand All @@ -348,6 +356,7 @@ public static function send_invoice( $invoice_id ) {
'Content-type: text/html; charset=UTF-8',
)
);

$message = array(
__( 'Hello,', 'wordcamporg' ),
// translators: event name.
Expand All @@ -359,6 +368,7 @@ public static function send_invoice( $invoice_id ) {
// translators: event name.
sprintf( __( 'The %s team', 'wordcamporg' ), sanitize_text_field( $opt['event_name'] ) ),
);

$message = implode( PHP_EOL, $message );
$message = '<p>' . nl2br( $message ) . '</p>';
wp_mail( $invoice_metas['email'], $subject, $message, $headers, $attachments );
Expand All @@ -372,8 +382,10 @@ public static function send_invoice( $invoice_id ) {
public static function create_invoice_document( $invoice_id ) {

$camptix_opts = get_option( 'camptix_options' );
$date_format = ! empty( $camptix_opts['invoice-date-format'] ) ? $camptix_opts['invoice-date-format'] : get_option( 'date_format' );

$invoice_number = get_post_meta( $invoice_id, 'invoice_number', true );
$invoice_date = get_the_date( $camptix_opts['invoice-date-format'], $invoice_id );
$invoice_date = get_the_date( $date_format, $invoice_id );
$invoice_metas = get_post_meta( $invoice_id, 'invoice_metas', true );
$invoice_order = get_post_meta( $invoice_id, 'original_order', true );

Expand Down Expand Up @@ -426,10 +438,6 @@ public static function is_invoice_incomplete( $invoice_id ) {
return true;
}

if ( empty( $invoice_metas['address'] ) ) {
return true;
}

if ( empty( $invoice_order['items'] ) ) {
return true;
}
Expand Down Expand Up @@ -535,7 +543,6 @@ public static function attendee_info( $attendee_info ) {

if ( empty( $_POST['invoice-email'] )
|| empty( $_POST['invoice-name'] )
|| empty( $_POST['invoice-address'] )
|| ! is_email( wp_unslash( $_POST['invoice-email'] ) ) ) {

$camptix->error_flag( 'nope' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<tr>
<td class="tix-left">
<label for="invoice-address">
<?php echo esc_html__( 'Recipient street address', 'wordcamporg' ); ?><span class="tix-required-star">*</span>
<?php echo esc_html__( 'Recipient street address', 'wordcamporg' ); ?>
</label>
</td>
<td class="tix-right">
Expand All @@ -53,7 +53,7 @@
<tr>
<td class="tix-left">
<label for="invoice-vat-number">
<?php echo esc_html__( 'VAT number', 'wordcamporg' ); ?><span class="tix-required-star">*</span>
<?php echo esc_html__( 'VAT number', 'wordcamporg' ); ?>
</label>
</td>
<td class="tix-right">
Expand Down
Loading