diff --git a/assets/js/media-upload.js b/assets/js/media-upload.js index 71a4f9275..2e074c558 100644 --- a/assets/js/media-upload.js +++ b/assets/js/media-upload.js @@ -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 ) { @@ -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(); @@ -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' ), @@ -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(); } }); @@ -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(); + }); }); \ No newline at end of file diff --git a/assets/js/media-upload.min.js b/assets/js/media-upload.min.js index 4a9137621..44e1626f4 100644 --- a/assets/js/media-upload.min.js +++ b/assets/js/media-upload.min.js @@ -1 +1 @@ -jQuery(document).ready(function(a){var b;a("#wpo-wcpdf-settings, .wpo-wcpdf-setup").on("click",".wpo_upload_image_button",function(c){c.preventDefault();let d=a(this).parent();return b?void b.open():void(b=wp.media.frames.file_frame=wp.media({title:a(this).data("uploader_title"),button:{text:a(this).data("uploader_button_text")},multiple:!1}),b.on("select",function(){let c=d.find("input.media-upload-id"),e=d.find("img.media-upload-preview"),f=b.state().get("selection").first().toJSON();c.val(f.id),e.length&&e.attr("src",f.url),a(".attachment-resolution, .attachment-resolution-warning").remove(),d.css("opacity","0.25");let g={security:c.data("ajax_nonce"),action:"wpo_wcpdf_get_media_upload_setting_html",args:c.data("settings_callback_args"),attachment_id:f.id};xhr=a.ajax({type:"POST",url:wpo_wcpdf_admin.ajaxurl,data:g,success:function(b){b&&"undefined"!=typeof b.success&&!0===b.success&&d.html(b.data),d.removeAttr("style"),c=d.find("input.media-upload-id"),a(document.body).trigger("wpo-wcpdf-media-upload-setting-updated",[c])},error:function(){d.removeAttr("style")}})}),b.open())}),a("#wpo-wcpdf-settings, .wpo-wcpdf-setup").on("click",".wpo_remove_image_button",function(){let b=a(this).parent(),c=b.find("input.media-upload-id"),d=b.find("img.media-upload-preview");c.val(""),d.remove(),a(this).remove(),a(".attachment-resolution, .attachment-resolution-warning").remove()})}); \ No newline at end of file +jQuery(document).ready(function(a){var b;let c,d=function(b,c,d){let e=a("#wpo-wcpdf-settings").find(d).filter(function(){let a=b.parent("div");return a.length&&"false"===a.attr("aria-hidden")});return e.length?e:c.find(d)};a("#wpo-wcpdf-settings, .wpo-wcpdf-setup").on("click",".wpo_upload_image_button",function(e){return e.preventDefault(),c=a(this).parent(),b?void b.open():void(b=wp.media.frames.file_frame=wp.media({title:a(this).data("uploader_title"),button:{text:a(this).data("uploader_button_text")},multiple:!1}),b.on("select",function(){let e=d(a(this),c,"input.media-upload-id"),f=d(a(this),c,"img.media-upload-preview"),g=b.state().get("selection").first().toJSON();e.val(g.id),f.length&&f.attr("src",g.url),d(a(this),c,".attachment-resolution, .attachment-resolution-warning").remove(),c.parent().block({message:null,overlayCSS:{background:"#fff",opacity:.6}});let h={security:e.data("ajax_nonce"),action:"wpo_wcpdf_get_media_upload_setting_html",args:e.data("settings_callback_args"),attachment_id:g.id};xhr=a.ajax({type:"POST",url:wpo_wcpdf_admin.ajaxurl,data:h,success:function(b){b&&"undefined"!=typeof b.success&&!0===b.success&&c.html(b.data),c.removeAttr("style"),c.parent().unblock(),e=d(a(this),c,"input.media-upload-id"),a(document.body).trigger("wpo-wcpdf-media-upload-setting-updated",[e])},error:function(){c.parent().unblock()}})}),b.open())}),a("#wpo-wcpdf-settings, .wpo-wcpdf-setup").on("click",".wpo_remove_image_button",function(){let b=a(this).parent(),c=b.find("input.media-upload-id"),e=b.find("img.media-upload-preview");c.val(""),e.remove(),a(this).remove(),d(a(this),b,".attachment-resolution, .attachment-resolution-warning").remove()})}); \ No newline at end of file diff --git a/includes/class-wcpdf-settings.php b/includes/class-wcpdf-settings.php index c929cea6e..bd81614c1 100644 --- a/includes/class-wcpdf-settings.php +++ b/includes/class-wcpdf-settings.php @@ -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 ); diff --git a/includes/documents/abstract-wcpdf-order-document.php b/includes/documents/abstract-wcpdf-order-document.php index 2f11c1d96..bb6daeff0 100644 --- a/includes/documents/abstract-wcpdf-order-document.php +++ b/includes/documents/abstract-wcpdf-order-document.php @@ -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 ); } /** @@ -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( '%2$s', esc_attr( $src ), esc_attr( $company ) ); - - echo apply_filters( 'wpo_wcpdf_header_logo_img_element', $img_element, $attachment, $this ); } + + $img_element = sprintf( '%2$s', esc_attr( $src ), esc_attr( $company ) ); + + echo apply_filters( 'wpo_wcpdf_header_logo_img_element', $img_element, $attachment, $this ); } } @@ -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 diff --git a/includes/settings/class-wcpdf-settings-callbacks.php b/includes/settings/class-wcpdf-settings-callbacks.php index 07a493604..c721284e2 100644 --- a/includes/settings/class-wcpdf-settings-callbacks.php +++ b/includes/settings/class-wcpdf-settings-callbacks.php @@ -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]; @@ -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. */ @@ -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 { @@ -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; + } + } } diff --git a/includes/settings/class-wcpdf-settings-general.php b/includes/settings/class-wcpdf-settings-general.php index 47fd76a4c..d714534ae 100644 --- a/includes/settings/class-wcpdf-settings-general.php +++ b/includes/settings/class-wcpdf-settings-general.php @@ -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( diff --git a/woocommerce-pdf-invoices-packingslips.php b/woocommerce-pdf-invoices-packingslips.php index 3e2882151..5b8bb25b9 100644 --- a/woocommerce-pdf-invoices-packingslips.php +++ b/woocommerce-pdf-invoices-packingslips.php @@ -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' ) ) {