From 446d1efbcdd9fe2df6c3cfd47ed0e6ec070790c9 Mon Sep 17 00:00:00 2001 From: Mahdi Yazdani Date: Tue, 15 Aug 2023 11:34:10 +0300 Subject: [PATCH] =?UTF-8?q?Version=201.6.1=20-=20Maintenance=20release=20?= =?UTF-8?q?=F0=9F=9A=A7=20=20=20(#23)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Error when additional terms deactivated * Generated * Add new version changelog * feat: Show the terms acceptance on the order page * Load Order class * fix: Remove unused imports * fix: Add default value to the notice field * fix: Unify saving terms acceptance * feat: Display terms acceptance status * Bump plugin version * fix: Ensure page content runs through the_content filter * Update changelog * fix: Update post-meta --- assets/js/checkout.js | 7 ++- assets/js/minified/checkout.js | 2 +- languages/woo-additional-terms.pot | 41 ++++++++------- readme.txt | 74 +++++++++++++++------------- src/Admin/Order.php | 60 ++++++++++++++++++++++ src/Plugin.php | 3 ++ src/PluginServiceProvider.php | 2 - src/Settings/Sections/General.php | 1 + src/WooCommerce/Block/Block.php | 59 ++++++++++++++++------ src/WooCommerce/Checkout.php | 35 ++++++------- src/WooCommerce/Terms.php | 3 +- templates/order/terms-acceptance.php | 31 ++++++++++++ woo-additioanl-terms.php | 4 +- 13 files changed, 230 insertions(+), 92 deletions(-) create mode 100644 src/Admin/Order.php create mode 100644 templates/order/terms-acceptance.php diff --git a/assets/js/checkout.js b/assets/js/checkout.js index 17ef38b..bceaf04 100644 --- a/assets/js/checkout.js +++ b/assets/js/checkout.js @@ -22,15 +22,18 @@ }, component: withInstanceId( ( { instanceId, checkoutExtensionData } ) => { const data = getSetting( '_woo_additional_terms_data', '' ); + const { setExtensionData } = checkoutExtensionData; // Bail early if the checkbox label is empty. if ( ! data?.checkbox_label ) { + useEffect( () => { + setExtensionData( '_woo_additional_terms', 'data', '' ); + }, [] ); return null; } const { checkbox_label, is_required, display_action, page_content, error_message } = data; const validationErrorId = `_woo_additional_terms_data_${ instanceId }`; - const { setExtensionData } = checkoutExtensionData; const [ checked, setChecked ] = useState( false ); const { setValidationErrors, clearValidationError } = useDispatch( VALIDATION_STORE_KEY ); const error = useSelect( ( select ) => @@ -39,7 +42,7 @@ const hasError = !! ( error?.message && ! error?.hidden ); useEffect( () => { - setExtensionData( '_woo_additional_terms', 'wat_checkbox', checked ); + setExtensionData( '_woo_additional_terms', 'data', checked ? 'yes' : 'no' ); if ( ! is_required ) { return; diff --git a/assets/js/minified/checkout.js b/assets/js/minified/checkout.js index 392f36f..5c3b6a1 100644 --- a/assets/js/minified/checkout.js +++ b/assets/js/minified/checkout.js @@ -1 +1 @@ -"use strict";function _typeof(obj){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(obj){return typeof obj}:function(obj){return obj&&"function"==typeof Symbol&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj},_typeof(obj)}function _defineProperty(obj,key,value){key=_toPropertyKey(key);if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}function _toPropertyKey(arg){var key=_toPrimitive(arg,"string");return _typeof(key)==="symbol"?key:String(key)}function _toPrimitive(input,hint){if(_typeof(input)!=="object"||input===null)return input;var prim=input[Symbol.toPrimitive];if(prim!==undefined){var res=prim.call(input,hint||"default");if(_typeof(res)!=="object")return res;throw new TypeError("@@toPrimitive must return a primitive value.")}return(hint==="string"?String:Number)(input)}function _slicedToArray(arr,i){return _arrayWithHoles(arr)||_iterableToArrayLimit(arr,i)||_unsupportedIterableToArray(arr,i)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(o,minLen){if(!o)return;if(typeof o==="string")return _arrayLikeToArray(o,minLen);var n=Object.prototype.toString.call(o).slice(8,-1);if(n==="Object"&&o.constructor)n=o.constructor.name;if(n==="Map"||n==="Set")return Array.from(o);if(n==="Arguments"||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return _arrayLikeToArray(o,minLen)}function _arrayLikeToArray(arr,len){if(len==null||len>arr.length)len=arr.length;for(var i=0,arr2=new Array(len);iarr.length)len=arr.length;for(var i=0,arr2=new Array(len);i\n" "Language-Team: LANGUAGE \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"POT-Creation-Date: 2023-07-25T16:05:35+00:00\n" +"POT-Creation-Date: 2023-08-11T09:10:47+00:00\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "X-Generator: WP-CLI 2.8.1\n" "X-Domain: woo-additional-terms\n" @@ -24,7 +24,7 @@ msgid "https://mypreview.one/woo-additional-terms" msgstr "" #. Description of the plugin -msgid "Add additional terms and conditions checkbox to your WooCommerce checkout page." +msgid "Improve your checkout process by adding an extra checkbox for terms and conditions. Keep track of acceptance to ensure transparency and security." msgstr "" #. Author of the plugin @@ -142,38 +142,43 @@ msgctxt "settings field placeholder" msgid "I have read and agree to the website {{additional-terms}}" msgstr "" +#: src/Settings/Sections/General.php:87 +msgctxt "settings field default" +msgid "I have read and agree to the website {{additional-terms}}" +msgstr "" + #. translators: 1: Open paragraph tag, 2: Terms smart tag, 3: Close paragraph tag. -#: src/Settings/Sections/General.php:99 +#: src/Settings/Sections/General.php:100 msgctxt "settings field description" msgid "%1$sUse the %2$s smart tag in the Terms Text to automatically display the linked terms page title in your checkbox label.%3$s" msgstr "" -#: src/Settings/Sections/General.php:106 +#: src/Settings/Sections/General.php:107 msgctxt "settings field name" msgid "Required" msgstr "" -#: src/Settings/Sections/General.php:107 +#: src/Settings/Sections/General.php:108 msgctxt "settings field description" msgid "Enable this to make the additional terms checkbox required." msgstr "" -#: src/Settings/Sections/General.php:113 +#: src/Settings/Sections/General.php:114 msgctxt "settings field name" msgid "Error Message" msgstr "" -#: src/Settings/Sections/General.php:114 +#: src/Settings/Sections/General.php:115 msgctxt "settings field description" msgid "Add an error message to show if the customer does not accept the additional terms." msgstr "" -#: src/Settings/Sections/General.php:115 +#: src/Settings/Sections/General.php:116 msgctxt "settings field placeholder" msgid "Please accept the additional terms to continue." msgstr "" -#: src/Settings/Sections/General.php:116 +#: src/Settings/Sections/General.php:117 msgctxt "settings field default" msgid "Please accept the additional terms to continue." msgstr "" @@ -188,23 +193,21 @@ msgctxt "settings tab" msgid "General" msgstr "" -#: src/WooCommerce/Block/Block.php:91 -#: src/WooCommerce/Checkout.php:201 +#: src/WooCommerce/Block/Block.php:120 msgid "Customer accepted the additional terms." msgstr "" -#: src/WooCommerce/Block/Block.php:98 -#: src/WooCommerce/Checkout.php:208 +#: src/WooCommerce/Block/Block.php:121 msgid "Customer did not accept the additional terms." msgstr "" -#: src/WooCommerce/Block/Block.php:150 -#: src/WooCommerce/Checkout.php:168 +#: src/WooCommerce/Block/Block.php:177 +#: src/WooCommerce/Checkout.php:173 msgid "Please accept the additional terms to continue." msgstr "" #. translators: %s: Render the type of the variable. -#: src/WooCommerce/Block/Block.php:182 +#: src/WooCommerce/Block/Block.php:209 msgid "Value of field %s was posted with incorrect data type." msgstr "" @@ -244,6 +247,10 @@ msgctxt "admin notice" msgid "I already did!" msgstr "" +#: templates/order/terms-acceptance.php:24 +msgid "Additional terms and conditions:" +msgstr "" + #: templates/sidebar/docs.php:16 msgctxt "upsell" msgid "Learn Settings" diff --git a/readme.txt b/readme.txt index cfe0c28..dfd6eed 100644 --- a/readme.txt +++ b/readme.txt @@ -3,9 +3,9 @@ Contributors: mahdiyazdani, mypreview, gookaani Tags: terms and conditions, I agree, age verification, terms of service, custom terms, woocommerce plugin, GDPR, terms conditions for woocommerce Donate link: https://mypreview.one/woo-additional-terms Requires at least: 5.0 -Tested up to: 6.2 +Tested up to: 6.3 Requires PHP: 7.4 -Stable tag: 1.6.0 +Stable tag: 1.6.1 License: GPLv3 or later License URI: http://www.gnu.org/licenses/gpl-3.0.txt @@ -123,6 +123,13 @@ Yes, it does. The [Woo Additional Terms PRO](https://mypreview.one/woo-additiona 5. Additional terms and condition checkbox on the WooCommerce checkout block page. == Changelog == += 1.6.1 = +* Prevented checkout error in the checkout block when additional terms & conditions are deactivated. +* Enhanced display of additional terms acceptance below the billing address. +* Ensured compatibility with the "WooCommerce PayPal Payments" plugin. +* Verified compatibility with WordPress version 6.3. +* Verified compatibility with WooCommerce version 8.0. + = 1.6.0 = * Added: Ability to define the status of the additional terms checkbox. * Added: Customization option to choose how the terms page is displayed (modal, embed, or new tab). @@ -131,78 +138,77 @@ Yes, it does. The [Woo Additional Terms PRO](https://mypreview.one/woo-additiona * Improved: Optimized the underlying code structure for better performance and maintainability. = 1.5.2 = -* Compatibility with WordPress 6.2 -* Compatibility with WooCommerce 7.5 +* Compatibility with WordPress 6.2. +* Compatibility with WooCommerce 7.5. = 1.5.1 = -* Update font-size for the checkout block -* Compatibility with WooCommerce 7.4 +* Updated font-size for the checkout block. +* Compatibility with WooCommerce 7.4. = 1.5.0 = -* Compatibility with WooCommerce Checkout block -* Compatibility with WooCommerce 7.3 +* Compatibility with WooCommerce Checkout block. +* Compatibility with WooCommerce 7.3. = 1.4.1 = -* Improved logic in displaying admin-notices. +* Improved logic in displaying admin notices. = 1.4.0 = -* Compatibility with WooCommerce 7.2 +* Compatibility with WooCommerce 7.2. = 1.3.6 = -* Compatibility with WordPress 6.1 -* Compatibility with WooCommerce 7.0 +* Compatibility with WordPress 6.1. +* Compatibility with WooCommerce 7.0. = 1.3.5 = -* Compatibility with WordPress 6.0 -* Compatibility with WooCommerce 6.9 +* Compatibility with WordPress 6.0. +* Compatibility with WooCommerce 6.9. = 1.3.4 = -* Compatibility with WordPress 5.6 -* Compatibility with WooCommerce 4.8 +* Compatibility with WordPress 5.6. +* Compatibility with WooCommerce 4.8. = 1.3.3 = * Multiple code standards improvements. -* Compatibility with WordPress 5.5 -* Compatibility with WooCommerce 4.4 +* Compatibility with WordPress 5.5. +* Compatibility with WooCommerce 4.4. = 1.3.2 = -* Compatibility with WooCommerce 4.3 +* Compatibility with WooCommerce 4.3. = 1.3.1 = * Display the acceptance of terms & conditions on the order edit page. = 1.3.0 = -* Additional terms content can now open up on the page when the link is clicked, and customers can review without leaving checkout. -* Compatibility with WooCommerce 4.2.0 +* Additional terms content can now open up on the page when the link is clicked, allowing customers to review without leaving checkout. +* Compatibility with WooCommerce 4.2.0. = 1.2.2 = * Added language configuration file to support WPML/Polylang. -* Compatibility with WordPress 5.4.1 -* Compatibility with WooCommerce 4.1.1 +* Compatibility with WordPress 5.4.1. +* Compatibility with WooCommerce 4.1.1. = 1.2.1 = * Multiple code standards improvements. -* Compatibility with WordPress 5.4.0 -* Compatibility with WooCommerce 4.0.1 +* Compatibility with WordPress 5.4.0. +* Compatibility with WooCommerce 4.0.1. = 1.2.0 = * Updated language file. -* Refactored and re-organized underlying code in plugin settings page/ controls. -* Compatibility with WordPress 5.3.1 -* Compatibility with WooCommerce 3.8.1 +* Refactored and re-organized underlying code in plugin settings page/controls. +* Compatibility with WordPress 5.3.1. +* Compatibility with WooCommerce 3.8.1. = 1.1.0 = -* Compatible with WordPress 4.9.6 -* Compatible with WordPress 3.4.2 - -= 1.0.3 = +* Compatible with WordPress 4.9.6. +* Compatible with WooCommerce 3.4.2. = 1.0.2 = -* Compatible with WordPress 4.7.0 -* Compatible with WordPress 2.6.9 +* Compatible with WordPress 4.7.0. +* Compatible with WooCommerce 2.6.9. = 1.0.1 = * Added screenshots. = 1.0.0 = * Initial release. + diff --git a/src/Admin/Order.php b/src/Admin/Order.php new file mode 100644 index 0000000..10b4216 --- /dev/null +++ b/src/Admin/Order.php @@ -0,0 +1,60 @@ +service( 'template_manager' )->echo_template( + 'order/terms-acceptance.php', + array( + 'value' => $order->get_meta( self::META_KEY ), + ) + ); + } +} diff --git a/src/Plugin.php b/src/Plugin.php index a6c0fa8..1cf622f 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -152,6 +152,9 @@ private function get_classes() { $is_admin = is_admin(); $is_frontend = ! $is_admin; $classes = array( + 'Admin\\Order' => array( + 'condition' => $is_admin, + ), 'Ajax\\OnBoarding' => array( 'condition' => $is_ajax, ), diff --git a/src/PluginServiceProvider.php b/src/PluginServiceProvider.php index dc64dee..4b65721 100644 --- a/src/PluginServiceProvider.php +++ b/src/PluginServiceProvider.php @@ -13,8 +13,6 @@ use Pimple\Container; use Pimple\ServiceProviderInterface; -use Woo_Additional_Terms\Settings; -use Woo_Additional_Terms\WooCommerce; /** * Class PluginServiceProvider. diff --git a/src/Settings/Sections/General.php b/src/Settings/Sections/General.php index 000891a..6ac8d67 100644 --- a/src/Settings/Sections/General.php +++ b/src/Settings/Sections/General.php @@ -84,6 +84,7 @@ public function get_fields() { 'name' => _x( 'Terms Text', 'settings field name', 'woo-additional-terms' ), 'desc' => _x( 'Enter the desired text that will be displayed alongside the checkbox, ensuring clarity and transparency when customers agree to the specified terms during the checkout process.', 'settings field description', 'woo-additional-terms' ), 'placeholder' => _x( 'I have read and agree to the website {{additional-terms}}', 'settings field placeholder', 'woo-additional-terms' ), + 'default' => _x( 'I have read and agree to the website {{additional-terms}}', 'settings field default', 'woo-additional-terms' ), 'type' => 'textarea', 'id' => 'woo_additional_terms_options[notice]', 'desc_tip' => true, diff --git a/src/WooCommerce/Block/Block.php b/src/WooCommerce/Block/Block.php index 2747d36..71d76ef 100644 --- a/src/WooCommerce/Block/Block.php +++ b/src/WooCommerce/Block/Block.php @@ -13,9 +13,11 @@ use WP_Error; use WC_Order; +use Exception; use WP_REST_Request; use Automattic\WooCommerce\Blocks; use Automattic\WooCommerce\StoreApi; +use Woo_Additional_Terms\Admin; /** * Checkout block class. @@ -46,7 +48,8 @@ public function initialize() { $this->extend_store_api(); add_filter( '__experimental_woocommerce_blocks_add_data_attributes_to_block', array( $this, 'add_attributes_to_frontend_blocks' ) ); - add_action( 'woocommerce_store_api_checkout_update_order_from_request', array( $this, 'save_acceptance' ), 10, 2 ); + add_action( 'woocommerce_store_api_checkout_update_order_from_request', array( $this, 'process_acceptance' ), 10, 2 ); + add_action( 'woo_additional_terms_checkout_save_acceptance', array( $this, 'save_acceptance' ), 10, 2 ); } /** @@ -67,7 +70,7 @@ public function add_attributes_to_frontend_blocks( $allowed_blocks ) { /** * Fires after an order saved into the database. - * We will update the post meta + * We will update the post meta. * * @since 1.5.0 * @@ -76,27 +79,51 @@ public function add_attributes_to_frontend_blocks( $allowed_blocks ) { * * @return void */ - public function save_acceptance( $order, $request ) { + public function process_acceptance( $order, $request ) { if ( ! isset( $request['extensions'], $request['extensions']['_woo_additional_terms'] ) ) { return; } - // Save the additional terms checkbox value as order meta. - $order->update_meta_data( '_woo_additional_terms', wc_bool_to_string( isset( $request['extensions']['_woo_additional_terms']['wat_checkbox'] ) ) ); + $has_accepted = empty( $request['extensions']['_woo_additional_terms']['data'] ) ? null : wc_string_to_bool( $request['extensions']['_woo_additional_terms']['data'] ); + + /** + * Fires after additional terms submissions is about to be saved. + * + * @since 1.6.1 + * + * @param null|bool $has_accepted Whether the additional terms has been accepted. + * @param WC_Order $order Order object. + */ + do_action( 'woo_additional_terms_checkout_save_acceptance', $has_accepted, $order ); + } - // If the additional terms checkbox is checked, add note that customer accepted if not add note tha customer didn't accept. - if ( isset( $request['extensions']['_woo_additional_terms']['wat_checkbox'] ) && wc_string_to_bool( $request['extensions']['_woo_additional_terms']['wat_checkbox'] ) ) { - $order->add_order_note( - __( 'Customer accepted the additional terms.', 'woo-additional-terms' ) - ); + /** + * Stores additional terms submissions after a new order being processed. + * + * @since 1.6.1 + * + * @param null|bool $has_accepted Whether the additional terms has been accepted. + * @param WC_Order $order Order object. + * + * @return void + */ + public function save_acceptance( $has_accepted, $order ) { + // Leave if the order is not an instance of WC_Order. + if ( ! $order instanceof WC_Order || is_null( $has_accepted ) ) { return; } - $order->add_order_note( - __( 'Customer did not accept the additional terms.', 'woo-additional-terms' ) - ); + // Add order note based on acceptance status. + $note_message = $has_accepted + ? __( 'Customer accepted the additional terms.', 'woo-additional-terms' ) + : __( 'Customer did not accept the additional terms.', 'woo-additional-terms' ); + + $order->add_order_note( $note_message ); + + // Save the additional terms checkbox value as order meta. + update_post_meta( $order->get_id(), Admin\Order::META_KEY, wc_bool_to_string( $has_accepted ) ); } /** @@ -171,13 +198,13 @@ public function extend_store_api() { 'endpoint' => Blocks\StoreApi\Schemas\CheckoutSchema::IDENTIFIER, 'namespace' => $this->get_name(), 'schema_callback' => fn() => array( - 'wat_checkbox' => array( - 'type' => 'boolean', + 'data' => array( + 'type' => 'string', 'context' => array(), 'arg_options' => array( 'validate_callback' => function( $value ) { - if ( ! is_bool( $value ) ) { + if ( ! is_string( $value ) ) { /* translators: %s: Render the type of the variable. */ return new WP_Error( 'api-error', sprintf( esc_html__( 'Value of field %s was posted with incorrect data type.', 'woo-additional-terms' ), gettype( $value ) ) ); } diff --git a/src/WooCommerce/Checkout.php b/src/WooCommerce/Checkout.php index 445371a..0fc33a8 100644 --- a/src/WooCommerce/Checkout.php +++ b/src/WooCommerce/Checkout.php @@ -70,7 +70,7 @@ public function enforce_terms() { add_filter( 'woocommerce_checkout_posted_data', array( $this, 'posted_data' ) ); add_action( 'woocommerce_checkout_after_terms_and_conditions', array( $this, 'show_checkbox' ) ); add_action( 'woocommerce_after_checkout_validation', array( $this, 'show_error' ), 10, 2 ); - add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'save_acceptance' ), 10, 2 ); + add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'process_acceptance' ), 10, 2 ); } /** @@ -83,6 +83,11 @@ public function enforce_terms() { */ public function body_classes( $classes ) { + // Bail early, in case the checkout page is not available. + if ( ! is_checkout() ) { + return $classes; + } + /** * Append a class to the body element when the additional terms are enforced. */ @@ -173,7 +178,7 @@ public function show_error( $fields, $errors ) { } /** - * Stores additional terms submissions after a new order being processed. + * Processes additional terms submissions after a new order being processed. * * @since 1.3.3 * @@ -182,7 +187,7 @@ public function show_error( $fields, $errors ) { * * @return void */ - public function save_acceptance( $order_id, $fields ) { + public function process_acceptance( $order_id, $fields ) { // Get the order object. $order = wc_get_order( $order_id ); @@ -192,20 +197,16 @@ public function save_acceptance( $order_id, $fields ) { return; } - // Save the additional terms checkbox value as order meta. - $order->update_meta_data( '_woo_additional_terms', wc_bool_to_string( isset( $fields['_woo_additional_terms'] ) ) ); - - // If the additional terms checkbox is checked, add note that customer accepted if not add note tha customer didn't accept. - if ( isset( $fields['_woo_additional_terms'] ) && wc_string_to_bool( $fields['_woo_additional_terms'] ) ) { - $order->add_order_note( - __( 'Customer accepted the additional terms.', 'woo-additional-terms' ) - ); - - return; - } + $has_accepted = isset( $fields['_woo_additional_terms'] ); - $order->add_order_note( - __( 'Customer did not accept the additional terms.', 'woo-additional-terms' ) - ); + /** + * Fires after additional terms submissions is about to be saved. + * + * @since 1.6.1 + * + * @param bool $has_accepted Whether the additional terms has been accepted. + * @param WC_Order $order Order object. + */ + do_action( 'woo_additional_terms_checkout_save_acceptance', $has_accepted, $order ); } } diff --git a/src/WooCommerce/Terms.php b/src/WooCommerce/Terms.php index 2d3e7dc..b2637ab 100644 --- a/src/WooCommerce/Terms.php +++ b/src/WooCommerce/Terms.php @@ -170,6 +170,7 @@ private function get_content() { return ''; } - return $terms_page->post_content; + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound + return apply_filters( 'the_content', $terms_page->post_content ); } } diff --git a/templates/order/terms-acceptance.php b/templates/order/terms-acceptance.php new file mode 100644 index 0000000..83761f6 --- /dev/null +++ b/templates/order/terms-acceptance.php @@ -0,0 +1,31 @@ + + + +
+

+ + + + +

+
+ +