Skip to content

Commit

Permalink
New: improvements to shop logo to be translatable (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoPavlinic98 authored Apr 15, 2024
1 parent bb14362 commit bac99d9
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 53 deletions.
43 changes: 32 additions & 11 deletions assets/js/media-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ jQuery(document).ready(function($) {

// Uploading files
var file_frame;
let $settings_wrapper;

// This function returns the translatable media input field in case translation is present.
// If the translation is not present, the function will return the media input field.
let get_media_field = function( self, settings_wrapper, element_id ) {
let $input = $( '#wpo-wcpdf-settings' ).find( element_id ).filter( function() {
let parent = self.parent( 'div' );
return parent.length && parent.attr( 'aria-hidden' ) === 'false';
} );

return $input.length ? $input : settings_wrapper.find( element_id );
};

$( '#wpo-wcpdf-settings, .wpo-wcpdf-setup' ).on( 'click', '.wpo_upload_image_button', function( event ){
event.preventDefault();

// get input wrapper
let $settings_wrapper = $(this).parent();
$settings_wrapper = $( this ).parent();

// If the media frame already exists, reopen it.
if ( file_frame ) {
Expand All @@ -29,9 +42,9 @@ jQuery(document).ready(function($) {
// When an image is selected, run a callback.
file_frame.on( 'select', function() {
// get target elements
let $input = $settings_wrapper.find( 'input.media-upload-id' );
let $preview = $settings_wrapper.find( 'img.media-upload-preview' );

let $input = get_media_field( $( this ), $settings_wrapper, 'input.media-upload-id' );
let $preview = get_media_field( $( this ), $settings_wrapper, 'img.media-upload-preview' );
// We set multiple to false so only get one image from the uploader
let attachment = file_frame.state().get( 'selection' ).first().toJSON();

Expand All @@ -40,10 +53,17 @@ jQuery(document).ready(function($) {
if ( $preview.length ) {
$preview.attr( 'src', attachment.url );
}
$( '.attachment-resolution, .attachment-resolution-warning' ).remove();

// dim until we have a response
$settings_wrapper.css( 'opacity', '0.25' );
get_media_field( $( this ), $settings_wrapper, '.attachment-resolution, .attachment-resolution-warning' ).remove();

// Block the media upload UI until we have a response.
$settings_wrapper.parent().block( {
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
} );

let data = {
security: $input.data( 'ajax_nonce' ),
Expand All @@ -61,13 +81,14 @@ jQuery(document).ready(function($) {
$settings_wrapper.html( response.data );
}
$settings_wrapper.removeAttr( 'style' );
$settings_wrapper.parent().unblock();

// custom trigger
$input = $settings_wrapper.find( 'input.media-upload-id' );
$input = get_media_field( $( this ), $settings_wrapper, 'input.media-upload-id' );
$( document.body ).trigger( 'wpo-wcpdf-media-upload-setting-updated', [ $input ] );
},
error: function (xhr, ajaxOptions, thrownError) {
$settings_wrapper.removeAttr( 'style' );
$settings_wrapper.parent().unblock();
}
});

Expand All @@ -87,6 +108,6 @@ jQuery(document).ready(function($) {
$input.val( '' );
$preview.remove();
$( this ).remove();
$( '.attachment-resolution, .attachment-resolution-warning' ).remove();
});
get_media_field( $( this ), $settings_wrapper, '.attachment-resolution, .attachment-resolution-warning' ).remove();
});
});
2 changes: 1 addition & 1 deletion assets/js/media-upload.min.js

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

4 changes: 4 additions & 0 deletions includes/class-wcpdf-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,10 @@ public function get_media_upload_setting_html() {
$args = $_POST['args'];
$args['current'] = absint( $_POST['attachment_id'] );

if ( isset( $args['translatable'] ) ) {
$args['translatable'] = wc_string_to_bool( $args['translatable'] );
}

// get settings HTML
ob_start();
$this->callbacks->media_upload( $args );
Expand Down
76 changes: 43 additions & 33 deletions includes/documents/abstract-wcpdf-order-document.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,11 +816,21 @@ public function has_header_logo() {

/**
* Return logo id
*
* @return int|bool
*/
public function get_header_logo_id() {
if ( !empty( $this->settings['header_logo'] ) ) {
return apply_filters( 'wpo_wcpdf_header_logo_id', $this->settings['header_logo'], $this );
$header_logo_id = false;

if ( ! empty( $this->settings['header_logo'] ) ) {
$header_logo_id = absint( $this->get_settings_text( 'header_logo', '', false ) );

if ( 0 === $header_logo_id ) {
$header_logo_id = false;
}
}

return apply_filters( 'wpo_wcpdf_header_logo_id', $header_logo_id, $this );
}

/**
Expand All @@ -836,43 +846,41 @@ public function get_header_logo_height() {
* Show logo html
*/
public function header_logo() {
if ( $this->get_header_logo_id() ) {
$attachment_id = $this->get_header_logo_id();
$company = $this->get_shop_name();
$attachment_id = $this->get_header_logo_id();

if ( $attachment_id ) {
$attachment = wp_get_attachment_image_src( $attachment_id, 'full', false );
$attachment_path = get_attached_file( $attachment_id );
if ( $attachment_id ) {
$company = $this->get_shop_name();
$attachment = wp_get_attachment_image_src( $attachment_id, 'full', false );
$attachment_path = get_attached_file( $attachment_id );

if ( empty( $attachment ) || empty( $attachment_path ) ) {
return;
}
$attachment_src = $attachment[0];
$attachment_width = $attachment[1];
$attachment_height = $attachment[2];
if ( empty( $attachment ) || empty( $attachment_path ) ) {
return;
}

$attachment_src = $attachment[0];
$attachment_width = $attachment[1];
$attachment_height = $attachment[2];

if ( apply_filters( 'wpo_wcpdf_use_path', true ) && file_exists( $attachment_path ) ) {
$src = $attachment_path;
} else {
$head = wp_remote_head( $attachment_src, [ 'sslverify' => false ] );
if ( is_wp_error( $head ) ) {
$errors = $head->get_error_messages();
foreach ( $errors as $error ) {
wcpdf_log_error( $error, 'critical' );
}
return;
} elseif ( isset( $head['response']['code'] ) && $head['response']['code'] === 200 ) {
$src = $attachment_src;
} else {
return;
if ( apply_filters( 'wpo_wcpdf_use_path', true ) && file_exists( $attachment_path ) ) {
$src = $attachment_path;
} else {
$head = wp_remote_head( $attachment_src, [ 'sslverify' => false ] );
if ( is_wp_error( $head ) ) {
$errors = $head->get_error_messages();
foreach ( $errors as $error ) {
wcpdf_log_error( $error, 'critical' );
}
return;
} elseif ( isset( $head['response']['code'] ) && $head['response']['code'] === 200 ) {
$src = $attachment_src;
} else {
return;
}

$img_element = sprintf( '<img src="%1$s" alt="%2$s" />', esc_attr( $src ), esc_attr( $company ) );

echo apply_filters( 'wpo_wcpdf_header_logo_img_element', $img_element, $attachment, $this );
}

$img_element = sprintf( '<img src="%1$s" alt="%2$s" />', esc_attr( $src ), esc_attr( $company ) );

echo apply_filters( 'wpo_wcpdf_header_logo_img_element', $img_element, $attachment, $this );
}
}

Expand All @@ -884,6 +892,8 @@ public function get_settings_text( $settings_key, $default = false, $autop = tru
// fallback to first array element if default is not present
} elseif( ! empty( $setting ) && is_array( $setting ) ) {
$text = reset( $setting );
} else {
$text = $setting;
}

// fallback to default
Expand Down
29 changes: 28 additions & 1 deletion includes/settings/class-wcpdf-settings-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ public function multiple_checkboxes( $args ) {
public function media_upload( $args ) {
extract( $this->normalize_settings_args( $args ) );

$setting_name = $this->append_language( $setting_name, $args );

if( ! empty( $current ) && $attachment = wp_get_attachment_image_src( $current, 'full', false ) ) {
$general_settings = get_option('wpo_wcpdf_settings_general');
$attachment_src = $attachment[0];
Expand All @@ -446,7 +448,7 @@ public function media_upload( $args ) {
// don't display resolution
}

/*
/**
* .webp support can be disabled but still showing the image in settings.
* We should add a notice because this will display an error when redering the PDF using DOMPDF.
*/
Expand Down Expand Up @@ -652,6 +654,8 @@ public function normalize_settings_args ( $args ) {
$args['current'] = $option[$args['id']][$args['lang']];
} elseif (isset( $option[$args['id']]['default'] )) {
$args['current'] = $option[$args['id']]['default'];
} elseif ( isset( $option[$args['id']] ) ) {
$args['current'] = $option[$args['id']];
}
}
} else {
Expand Down Expand Up @@ -714,6 +718,29 @@ public function validate( $input ) {
// Return the array processing any additional functions filtered by this action.
return apply_filters( 'wpo_wcpdf_validate_input', $output, $input );
}

/**
* Appends language at the end of the setting provided, in case the setting is translatable
* and it does not have a language set.
*
* @param string $setting Settings field that needs a language.
* @param array $args Setting arguments.
*
* @return string
*/
public function append_language( string $setting, array $args ): string {
if (
isset( $args['translatable'] ) &&
true === $args['translatable'] &&
isset( $args['lang'] ) &&
'default' !== $args['lang'] &&
! ( substr( $setting, -strlen( "[{$args['lang']}]" ) ) === "[{$args['lang']}]" )
) {
return $setting .= "[{$args['lang']}]";
} else {
return $setting;
}
}
}


Expand Down
12 changes: 6 additions & 6 deletions includes/settings/class-wcpdf-settings-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ public function init_settings() {
'callback' => 'media_upload',
'section' => 'general_settings',
'args' => array(
'option_name' => $option_name,
'id' => 'header_logo',
'uploader_title' => __( 'Select or upload your invoice header/logo', 'woocommerce-pdf-invoices-packing-slips' ),
'uploader_button_text' => __( 'Set image', 'woocommerce-pdf-invoices-packing-slips' ),
'remove_button_text' => __( 'Remove image', 'woocommerce-pdf-invoices-packing-slips' ),
//'description' => __( '...', 'woocommerce-pdf-invoices-packing-slips' ),
'option_name' => $option_name,
'id' => 'header_logo',
'uploader_title' => __( 'Select or upload your invoice header/logo', 'woocommerce-pdf-invoices-packing-slips' ),
'uploader_button_text' => __( 'Set image', 'woocommerce-pdf-invoices-packing-slips' ),
'remove_button_text' => __( 'Remove image', 'woocommerce-pdf-invoices-packing-slips' ),
'translatable' => true,
)
),
array(
Expand Down
2 changes: 1 addition & 1 deletion woocommerce-pdf-invoices-packingslips.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* License URI: https://opensource.org/licenses/gpl-license.php
* Text Domain: woocommerce-pdf-invoices-packing-slips
* WC requires at least: 3.3
* WC tested up to: 8.7
* WC tested up to: 8.8
*/

if ( ! defined( 'ABSPATH' ) ) {
Expand Down

0 comments on commit bac99d9

Please sign in to comment.