diff --git a/assets/css/admin.scss b/assets/css/admin.scss index d9ae0e3a2..8d22b7393 100644 --- a/assets/css/admin.scss +++ b/assets/css/admin.scss @@ -608,4 +608,23 @@ table.wc-gzd-tax-example tr th:first-child, table.wc-gzd-tax-example tr td:first } } } +} + +.wc-gzd-checkbox-log-list { + max-width: 400px; + + .wc-gzd-log-checkbox { + display: flex; + flex-wrap: wrap; + justify-content: space-between; + margin: 1em 0; + + p.checkbox-title, p.checkbox-status { + margin: 0; + } + + &:last-child { + margin-bottom: 0; + } + } } \ No newline at end of file diff --git a/assets/js/static/admin-settings.js b/assets/js/static/admin-settings.js index 8e0ab0453..c39a91bd4 100644 --- a/assets/js/static/admin-settings.js +++ b/assets/js/static/admin-settings.js @@ -7,13 +7,10 @@ window.germanized = window.germanized || {}; * Order Data Panel */ germanized.settings = { - params: {}, init: function() { - var self = this; - this.params = wc_gzd_admin_settings_params; try { @@ -25,7 +22,6 @@ window.germanized = window.germanized || {}; $( document ) .on( 'change', 'input[name=woocommerce_gzd_dispute_resolution_type]', this.onChangeDisputeResolutionType ) - .on( 'click', 'a.woocommerce-gzd-input-toggle-trigger', this.onInputToogleClick ) .on( 'change', '.wc-gzd-setting-tabs input.woocommerce-gzd-tab-status-checkbox', this.onChangeTabStatus ) .on( 'change', '.wc-gzd-setting-tab-enabled :input', this.preventWarning ) .on( 'click', 'a.wc-gzd-install-extension-btn', this.onInstallExtension ) @@ -352,28 +348,6 @@ window.germanized = window.germanized || {}; $( '#woocommerce_gzd_alternative_complaints_text_' + val ).parents( 'tr' ).show(); }, - onInputToogleClick: function() { - var $toggle = $( this ).find( 'span.woocommerce-gzd-input-toggle' ), - $row = $toggle.parents( 'fieldset' ), - $checkbox = $row.find( 'input[type=checkbox]' ), - $enabled = $toggle.hasClass( 'woocommerce-input-toggle--enabled' ); - - $toggle.removeClass( 'woocommerce-input-toggle--enabled' ); - $toggle.removeClass( 'woocommerce-input-toggle--disabled' ); - - if ( $enabled ) { - $checkbox.prop( 'checked', false ); - $toggle.addClass( 'woocommerce-input-toggle--disabled' ); - } else { - $checkbox.prop( 'checked', true ); - $toggle.addClass( 'woocommerce-input-toggle--enabled' ); - } - - $checkbox.trigger( 'change' ); - - return false; - }, - initMailSortable: function() { if ( $( '#woocommerce_gzd_mail_attach_imprint' ).length > 0 ) { var table = $( '#woocommerce_gzd_mail_attach_imprint' ).parents( 'table' ); diff --git a/assets/js/static/admin.js b/assets/js/static/admin.js new file mode 100644 index 000000000..0dfb53d48 --- /dev/null +++ b/assets/js/static/admin.js @@ -0,0 +1,46 @@ +/*global woocommerce_admin_meta_boxes, woocommerce_admin, accounting, woocommerce_admin_meta_boxes_order */ +window.germanized = window.germanized || {}; + +( function( $, germanized ) { + + /** + * Order Data Panel + */ + germanized.admin = { + params: {}, + + init: function() { + var self = this; + this.params = wc_gzd_admin_params; + + $( document ).on( 'click', 'a.woocommerce-gzd-input-toggle-trigger', this.onInputToogleClick ); + }, + + onInputToogleClick: function() { + var $toggle = $( this ).find( 'span.woocommerce-gzd-input-toggle' ), + $row = $toggle.parents( 'fieldset' ), + $checkbox = $row.find( 'input[type=checkbox]' ).length > 0 ? $row.find( 'input[type=checkbox]' ) : $toggle.parent().nextAll( 'input[type=checkbox]:first' ), + $enabled = $toggle.hasClass( 'woocommerce-input-toggle--enabled' ); + + $toggle.removeClass( 'woocommerce-input-toggle--enabled' ); + $toggle.removeClass( 'woocommerce-input-toggle--disabled' ); + + if ( $enabled ) { + $checkbox.prop( 'checked', false ); + $toggle.addClass( 'woocommerce-input-toggle--disabled' ); + } else { + $checkbox.prop( 'checked', true ); + $toggle.addClass( 'woocommerce-input-toggle--enabled' ); + } + + $checkbox.trigger( 'change' ); + + return false; + } + }; + + $( document ).ready( function() { + germanized.admin.init(); + }); + +})( jQuery, window.germanized ); \ No newline at end of file diff --git a/includes/admin/class-wc-gzd-admin-order.php b/includes/admin/class-wc-gzd-admin-order.php index b899e0d0f..b269c9e99 100644 --- a/includes/admin/class-wc-gzd-admin-order.php +++ b/includes/admin/class-wc-gzd-admin-order.php @@ -25,6 +25,9 @@ public static function instance() { } public function __construct() { + add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 35 ); + add_action( 'woocommerce_process_shop_order_meta', array( $this, 'save_editable_checkboxes' ), 45 ); + if ( wc_gzd_enable_additional_costs_split_tax_calculation() || wc_gzd_calculate_additional_costs_taxes_based_on_main_service() ) { add_action( 'woocommerce_order_item_shipping_after_calculate_taxes', @@ -117,6 +120,89 @@ public function __construct() { } } + public function get_order_screen_id() { + return function_exists( 'wc_get_page_screen_id' ) ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order'; + } + + protected function init_order_object( $post ) { + if ( is_callable( array( '\Automattic\WooCommerce\Utilities\OrderUtil', 'init_theorder_object' ) ) ) { + \Automattic\WooCommerce\Utilities\OrderUtil::init_theorder_object( $post ); + } else { + global $post, $thepostid, $theorder; + + if ( ! is_int( $thepostid ) ) { + $thepostid = $post->ID; + } + + if ( ! is_object( $theorder ) ) { + $theorder = wc_get_order( $thepostid ); + } + } + } + + public function save_editable_checkboxes( $order_id ) { + $checkboxes = WC_GZD_Legal_Checkbox_Manager::instance()->get_editable_checkboxes( 'order' ); + $visible = isset( $_POST['_checkboxes_visible'] ) ? (array) wc_clean( wp_unslash( $_POST['_checkboxes_visible'] ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing + + if ( $order = wc_get_order( $order_id ) ) { + $updated = false; + if ( ! empty( $checkboxes ) ) { + foreach ( $checkboxes as $checkbox_id ) { + if ( ! in_array( $checkbox_id, $visible, true ) ) { + continue; + } + + $is_checked = isset( $_POST[ "_checkbox_{$checkbox_id}" ] ) ? wc_string_to_bool( wc_clean( wp_unslash( $_POST[ "_checkbox_{$checkbox_id}" ] ) ) ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing + + if ( 'parcel_delivery' === $checkbox_id ) { + $order->update_meta_data( '_parcel_delivery_opted_in', wc_bool_to_string( $is_checked ) ); + } elseif ( 'photovoltaic_systems' === $checkbox_id ) { + $order->update_meta_data( '_photovoltaic_systems_opted_in', wc_bool_to_string( $is_checked ) ); + } else { + $order->update_meta_data( "_checkbox_{$checkbox_id}", wc_bool_to_string( $is_checked ) ); + } + + $updated = true; + } + } + + if ( $updated ) { + $order->save(); + } + } + } + + public function add_meta_boxes() { + $order_type_screen_ids = array_merge( wc_get_order_types( 'order-meta-boxes' ), array( $this->get_order_screen_id() ) ); + + // Orders. + foreach ( $order_type_screen_ids as $type ) { + add_meta_box( + 'woocommerce-gzd-order-checkboxes', + __( 'Checkboxes', 'woocommerce-germanized' ), + function( $post ) { + global $theorder; + $this->init_order_object( $post ); + $order = $theorder; + + $this->render_checkboxes_meta_box( $order ); + }, + $type, + 'side', + 'default' + ); + } + } + + /** + * @param WC_Order $order + * + * @return void + */ + protected function render_checkboxes_meta_box( $order ) { + WC_GZD_Legal_Checkbox_Manager::instance()->render_checkbox_log( $order, 'order' ); + } + /** * @param WC_Order_Item $item * diff --git a/includes/admin/class-wc-gzd-admin.php b/includes/admin/class-wc-gzd-admin.php index 6216ba74d..f85c1a558 100644 --- a/includes/admin/class-wc-gzd-admin.php +++ b/includes/admin/class-wc-gzd-admin.php @@ -59,16 +59,6 @@ public function __construct() { add_action( 'admin_init', array( $this, 'check_internetmarke_import' ) ); add_filter( 'woocommerce_addons_section_data', array( $this, 'set_addon' ), 10, 2 ); - add_action( - 'woocommerce_admin_order_data_after_shipping_address', - array( - $this, - 'show_checkbox_status', - ), - 10, - 1 - ); - add_filter( 'woocommerce_order_actions', array( $this, 'order_actions' ), 10, 1 ); add_action( 'woocommerce_order_action_order_confirmation', array( $this, 'resend_order_confirmation' ), 10, 1 ); add_action( @@ -529,34 +519,57 @@ public function toggle_input_field( $value ) { + render_toggle( $value ); ?> + + + + + '', + 'css' => '', + 'value' => '', + 'class' => '', + 'name' => '', + 'suffix' => '', + 'desc_tip' => false, + 'desc' => '', + 'custom_attributes' => array(), + ) + ); + $args['value'] = wc_bool_to_string( $args['value'] ); + $args['name'] = empty( $args['name'] ) ? $args['id'] : $args['name']; + // Description handling. + $field_description_data = WC_Admin_Settings::get_field_description( $args ); + ?> - + + class="" + $attribute_value ) { + if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) { + foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) { echo esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '" '; } } ?> - /> - - + /> - - - get_meta( '_parcel_delivery_opted_in' ) ) { - ?> -
get_id() ) ? '' . esc_html__( 'Allowed', 'woocommerce-germanized' ) . '' : '' . esc_html__( 'Not Allowed', 'woocommerce-germanized' ) . '' ); ?>
- get_meta( '_photovoltaic_systems_opted_in' ) ) { - ?> -get_id() ) ? '' . esc_html__( 'Allowed', 'woocommerce-germanized' ) . '' : '' . esc_html__( 'Not Allowed', 'woocommerce-germanized' ) . '' ); ?>
- get_assets_build_url( 'static/admin.js' ), array( 'jquery', 'woocommerce_admin' ), WC_GERMANIZED_VERSION ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter wp_register_script( 'wc-gzd-admin-product', $gzd->get_assets_build_url( 'static/admin-product.js' ), array( 'wc-admin-product-meta-boxes', 'media-models' ), WC_GERMANIZED_VERSION ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter - wp_register_script( 'wc-gzd-admin-product-variations', $gzd->get_assets_build_url( 'static/admin-product-variations.js' ), array( 'wc-gzd-admin-product', 'wc-admin-variation-meta-boxes' ), WC_GERMANIZED_VERSION ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter - - wp_localize_script( - 'wc-gzd-admin-product-variations', - 'wc_gzd_admin_product_variations_params', - array( - 'i18n_set_delivery_time' => __( 'Insert delivery time name, slug or id.', 'woocommerce-germanized' ), - 'i18n_set_product_unit' => __( 'Insert product units amount.', 'woocommerce-germanized' ), - ) - ); - wp_register_script( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter 'wc-gzd-admin-legal-checkboxes', $gzd->get_assets_build_url( 'static/admin-legal-checkboxes.js' ), @@ -827,17 +813,30 @@ public function add_scripts() { ), WC_GERMANIZED_VERSION ); - wp_register_script( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter 'wc-gzd-admin-settings', $gzd->get_assets_build_url( 'static/admin-settings.js' ), - array( - 'jquery', - 'woocommerce_admin', - ), + array( 'wc-gzd-admin' ), WC_GERMANIZED_VERSION ); + wp_localize_script( + 'wc-gzd-admin-product-variations', + 'wc_gzd_admin_product_variations_params', + array( + 'i18n_set_delivery_time' => __( 'Insert delivery time name, slug or id.', 'woocommerce-germanized' ), + 'i18n_set_product_unit' => __( 'Insert product units amount.', 'woocommerce-germanized' ), + ) + ); + + wp_localize_script( + 'wc-gzd-admin', + 'wc_gzd_admin_params', + array( + 'ajax_url' => admin_url( 'admin-ajax.php' ), + ) + ); + wp_localize_script( 'wc-gzd-admin-settings', 'wc_gzd_admin_settings_params', @@ -854,6 +853,10 @@ public function add_scripts() { wp_enqueue_script( 'wc-gzd-admin-product-variations' ); } + if ( function_exists( 'wc_get_screen_ids' ) && in_array( $screen->id, wc_get_screen_ids(), true ) ) { + wp_enqueue_script( 'wc-gzd-admin' ); + } + /** * After admin assets. * diff --git a/includes/admin/settings/views/html-admin-settings-tabs.php b/includes/admin/settings/views/html-admin-settings-tabs.php index 133989da4..024148890 100644 --- a/includes/admin/settings/views/html-admin-settings-tabs.php +++ b/includes/admin/settings/views/html-admin-settings-tabs.php @@ -55,17 +55,18 @@ supports_disabling() ) : ?> is_enabled() ? esc_attr__( 'Yes', 'woocommerce-germanized' ) : esc_attr__( 'No', 'woocommerce-germanized' ) ); ?> diff --git a/includes/class-wc-gzd-legal-checkbox-manager.php b/includes/class-wc-gzd-legal-checkbox-manager.php index 68a3a8e48..1b41fd688 100644 --- a/includes/class-wc-gzd-legal-checkbox-manager.php +++ b/includes/class-wc-gzd-legal-checkbox-manager.php @@ -1133,6 +1133,147 @@ function( $checkbox1, $checkbox2 ) { return $checkboxes; } + /** + * @param WC_Data|WP_User|WP_Comment $object + * @param string $location + * + * @return boolean + */ + public function is_logged( $checkbox_id, $object ) { + $cb_value = $this->get_logged_value( $checkbox_id, $object ); + $is_logged = ! empty( $cb_value ); + + return apply_filters( 'woocommerce_gzd_checkbox_is_logged', $is_logged, $checkbox_id, $object ); + } + + /** + * @param WC_Data|WP_User|WP_Comment $object + * @param string $location + * + * @return mixed + */ + protected function get_logged_value( $checkbox_id, $object ) { + $meta_key = "_checkbox_{$checkbox_id}"; + $cb_value = ''; + + if ( is_a( $object, 'WC_Order' ) ) { + if ( 'parcel_delivery' === $checkbox_id ) { + $cb_value = $object->get_meta( '_parcel_delivery_opted_in' ); + } elseif ( 'photovoltaic_systems' === $checkbox_id ) { + $cb_value = $object->get_meta( '_photovoltaic_systems_opted_in' ); + } else { + $cb_value = $object->get_meta( $meta_key ); + } + } elseif ( is_a( $object, 'WP_User' ) ) { + $cb_value = get_user_meta( $object->ID, $meta_key, true ); + } elseif ( is_a( $object, 'WP_Comment' ) ) { + $cb_value = get_comment_meta( $object->comment_ID, $meta_key, true ); + } elseif ( is_callable( array( $object, 'get_meta' ) ) ) { + $cb_value = $object->get_meta( $meta_key ); + } + + return apply_filters( 'woocommerce_gzd_checkbox_value', $cb_value, $checkbox_id, $object ); + } + + /** + * @param WC_Data|WP_User|WP_Comment $object + * @param string $location + * + * @return boolean + */ + public function is_checked( $checkbox_id, $object ) { + $cb_value = $this->get_logged_value( $checkbox_id, $object ); + $is_checked = wc_string_to_bool( $cb_value ); + + if ( is_a( $object, 'WC_Order' ) ) { + if ( 'parcel_delivery' === $checkbox_id ) { + $is_checked = wc_gzd_order_supports_parcel_delivery_reminder( $object->get_id() ); + } elseif ( 'photovoltaic_systems' === $checkbox_id ) { + $is_checked = wc_gzd_order_applies_for_photovoltaic_system_vat_exemption( $object->get_id() ); + } + } + + $is_checked = wc_string_to_bool( $is_checked ); + + return apply_filters( 'woocommerce_gzd_checkbox_is_checked', $is_checked, $checkbox_id, $object ); + } + + /** + * @param $location + * + * @return string[] + */ + public function get_loggable_checkboxes( $location ) { + $checkboxes = apply_filters( 'woocommerce_gzd_loggable_checkboxes', array( 'parcel_delivery', 'photovoltaic_systems' ), $location ); + + return $checkboxes; + } + + public function get_editable_checkboxes( $location ) { + $checkboxes = array(); + + if ( 'order' === $location ) { + $checkboxes = array( 'parcel_delivery' ); + } + + $checkboxes = apply_filters( 'woocommerce_gzd_editable_checkboxes', $checkboxes, $location ); + + return $checkboxes; + } + + /** + * @param WC_Data|WP_User|WP_Comment $object + * @param string $location + * + * @return void + */ + public function render_checkbox_log( $object, $location = 'order' ) { + $checkboxes = $this->get_loggable_checkboxes( $location ); + $checkboxes_to_list = array(); + + foreach ( $checkboxes as $checkbox_id ) { + if ( $checkbox = wc_gzd_get_legal_checkbox( $checkbox_id ) ) { + $is_checked = $this->is_checked( $checkbox_id, $object ); + $is_logged = $this->is_logged( $checkbox_id, $object ); + + if ( $is_logged ) { + $checkboxes_to_list[ $checkbox_id ] = $is_checked; + } + } + } + ?> + +get_admin_name() ); ?> get_admin_desc() ) ? wc_help_tip( $checkbox->get_admin_desc() ) : '' ); ?>
++ get_editable_checkboxes( $location ), true ) ) : ?> + render_toggle( + array( + 'id' => "_checkbox_{$checkbox_id}", + 'value' => wc_bool_to_string( $checkbox_status ), + ) + ); + ?> + + + ' : '' ); ?> + +
+