Skip to content

Commit

Permalink
V 1.5.2 Release 🎉 (#21)
Browse files Browse the repository at this point in the history
* Compatibility with WordPress 6.2

* Update plugin description and FAQ

* Update description, upgrade notice and PRO features list

* Use echo instead of printf for upsell button

* Add import export to PRO features

* Fix CS error

* Code tidy

* Add title to upgrade notice and update the notice color

* Allow terms without page assigned

* Add info notice

* Defined shortcode constant

* Update Review notice

* Update woo-additional-terms.pot

* Dismiss ask-to-rate notice

* Generated

* Dismiss ask-to-rate admin notice for users who already rated

* Delete the already rated option

* Remove type hinting

* Update rating filter url

---------

Co-authored-by: Sajedeh Gooklani <[email protected]>
  • Loading branch information
mahdiyazdani and gooklani authored Apr 7, 2023
1 parent 2a3d165 commit ad55cf1
Show file tree
Hide file tree
Showing 8 changed files with 871 additions and 395 deletions.
22 changes: 18 additions & 4 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,40 @@
cache() {
this.vars = {};
this.vars.rate = '#woo-additional-terms-dismiss-rate .notice-dismiss';
this.vars.rate += ', #woo-additional-terms-dismiss-rate .notice-dismiss-later';
this.vars.rated = '#woo-additional-terms-dismiss-rate .notice-dismiss-rated';
this.vars.upsell = '#woo-additional-terms-dismiss-upsell .notice-dismiss';
},

init() {
this.cache();
$( document.body ).on( 'click', this.vars.rate, ( event ) => this.handleOnDismiss( event, 'rate' ) );
$( document.body ).on( 'click', this.vars.upsell, ( event ) => this.handleOnDismiss( event, 'upsell' ) );
this.bindEvents();
},

bindEvents() {
$( document.body )
.on( 'click', this.vars.rate, ( event ) => this.handleOnDismiss( event, 'rate' ) )
.on( 'click', this.vars.rated, ( event ) => this.handleOnDismiss( event, 'rated' ) )
.on( 'click', this.vars.upsell, ( event ) => this.handleOnDismiss( event, 'upsell' ) );
},

handleOnDismiss( event, action ) {
event.preventDefault();
const $this = $( event.target );

if ( ! $this.attr( 'href' ) ) {
event.preventDefault();
}

$.ajax( {
type: 'POST',
url: ajaxurl,
dataType: 'json',
data: {
_ajax_nonce: watVars.dismiss_nonce,
action: `woo_additional_terms_dismiss_${ action }`,
},
dataType: 'json',
} ).always( () => {
$this.closest( 'div.notice:visible' ).slideUp();
} );
},
};
Expand Down
2 changes: 1 addition & 1 deletion assets/js/minified/admin.js

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

135 changes: 87 additions & 48 deletions includes/class-wat-checkout-block-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
/**
* Register the "Additional Terms" block for use in the Checkout Block offered by "WooCommerce Blocks".
*
* @link https://mypreview.one/woo-additional-terms
* @author MyPreview (Github: @mahdiyazdani, @gooklani, @mypreview)
* @since 1.5.0
* @link https://mypreview.one/woo-additional-terms
*
* @package woo-additional-terms
* @subpackage woo-additional-terms/includes
* @author MyPreview (Github: @mahdiyazdani, @gooklani, @mypreview)
*
* @since 1.5.0
*
* @package woo-additional-terms
*
* @subpackage woo-additional-terms/includes
*/

use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
Expand All @@ -27,88 +30,111 @@ class WAT_Checkout_Block_Integration implements IntegrationInterface {
/**
* The name of the integration.
*
* @since 1.5.0
* @return string
* @since 1.5.0
*
* @return string
*/
public function get_name() {

return '_woo_additional_terms';
}

/**
* When called invokes any initialization/setup for the integration.
*
* @since 1.5.0
* @return void
* @since 1.5.0
*
* @return void
*/
public function initialize() {

$this->register_frontend_scripts();
$this->register_editor_scripts();
$this->register_editor_blocks();
$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_terms_acceptance' ), 10, 2 );
}

/**
* Registers all the static resources for the front-end.
*
* @since 1.5.0
* @return void
* @since 1.5.0
*
* @return void
*/
public function register_frontend_scripts() {
wp_register_script( WOO_ADDITIONAL_TERMS_SLUG . '-checkout', trailingslashit( WOO_ADDITIONAL_TERMS_DIR_URL ) . 'assets/js/' . WOO_ADDITIONAL_TERMS_MIN_DIR . 'checkout.js', array( 'react', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n', 'wc-blocks-data-store', 'wc-blocks-checkout', 'wc-settings' ), WOO_ADDITIONAL_TERMS_VERSION, true );

wp_register_script( Woo_Additional_Terms::SLUG . '-checkout', trailingslashit( WOO_ADDITIONAL_TERMS_DIR_URL ) . 'assets/js/' . WOO_ADDITIONAL_TERMS_MIN_DIR . 'checkout.js', array( 'react', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n', 'wc-blocks-data-store', 'wc-blocks-checkout', 'wc-settings' ), WOO_ADDITIONAL_TERMS_VERSION, true );
}

/**
* Registers all the static resources for the editor.
*
* @since 1.5.0
* @return void
* @since 1.5.0
*
* @return void
*/
public function register_editor_scripts() {
wp_register_script( WOO_ADDITIONAL_TERMS_SLUG . '-editor', trailingslashit( WOO_ADDITIONAL_TERMS_DIR_URL ) . 'assets/js/' . WOO_ADDITIONAL_TERMS_MIN_DIR . 'block.js', array( 'react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-primitives', 'wc-blocks-checkout', 'wc-settings' ), WOO_ADDITIONAL_TERMS_VERSION, true );

wp_register_script( Woo_Additional_Terms::SLUG . '-editor', trailingslashit( WOO_ADDITIONAL_TERMS_DIR_URL ) . 'assets/js/' . WOO_ADDITIONAL_TERMS_MIN_DIR . 'block.js', array( 'react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-primitives', 'wc-blocks-checkout', 'wc-settings' ), WOO_ADDITIONAL_TERMS_VERSION, true );
}

/**
* Returns an array containing the handles of any scripts registered by our extension.
*
* @since 1.5.0
* @return array
* @since 1.5.0
*
* @return array
*/
public function get_script_handles() {
return array( WOO_ADDITIONAL_TERMS_SLUG . '-checkout' );

return array( Woo_Additional_Terms::SLUG . '-checkout' );
}

/**
* Returns an array containing the handles of any editor scripts registered by our extension.
*
* @since 1.5.0
* @return array
* @since 1.5.0
*
* @return array
*/
public function get_editor_script_handles() {
return array( WOO_ADDITIONAL_TERMS_SLUG . '-editor' );

return array( Woo_Additional_Terms::SLUG . '-editor' );
}

/**
* Returns an associative array containing any data we want to be available to the scripts on the front-end.
*
* @since 1.5.0
* @return array
* @since 1.5.0
*
* @return array
*/
public function get_script_data() {
$notice = (string) get_option( '_woo_additional_terms_notice', '' );

$notice = get_option( '_woo_additional_terms_notice', '' );
$page_id = get_option( '_woo_additional_terms_page_id', null );

if (
$page_id
&& ! empty( $page_id )
! empty( $page_id )
&& ! empty( $notice )
&& false !== strpos( $notice, '[additional-terms]' )
&& false !== strpos( $notice, Woo_Additional_Terms::SHORTCODE )
&& get_post( $page_id )
) {
$notice = str_replace( '[additional-terms]', sprintf( '<a href="%s" class="woo-additional-terms__link" target="_blank" rel="noopener noreferrer nofollow">%s</a>', esc_url( get_permalink( $page_id ) ), esc_html( get_the_title( $page_id ) ) ), $notice );
$notice = str_replace(
Woo_Additional_Terms::SHORTCODE,
sprintf(
'<a href="%s" class="woo-additional-terms__link" target="_blank" rel="noopener noreferrer nofollow">%s</a>',
esc_url( get_permalink( $page_id ) ),
wp_kses_post( get_the_title( $page_id ) )
),
$notice
);
}

$data = array(
return array(
'notice' => wp_kses(
$notice,
array(
Expand All @@ -122,59 +148,69 @@ public function get_script_data() {
),
'content' => Woo_Additional_Terms::terms_page_content( $page_id, false ),
);

return $data;
}

/**
* Register editor block.
*
* @since 1.5.0
* @return void
* @since 1.5.0
*
* @return void
*/
public function register_editor_blocks() {}

/**
* This allows dynamic (JS) blocks to access attributes in the frontend.
*
* @since 1.5.0
* @param array $allowed_blocks List of allowed blocks.
* @return array
* @since 1.5.0
*
* @param array $allowed_blocks List of allowed blocks.
*
* @return array
*/
public function add_attributes_to_frontend_blocks( $allowed_blocks ) {

$allowed_blocks[] = 'mypreview/woo-additional-terms';

return $allowed_blocks;
}

/**
* Fires after an order saved into the database.
* We will update the post meta
*
* @since 1.5.0
* @param WC_Order $order Order ID or order object.
* @param WP_REST_Request $request The API request currently being processed.
* @return void
* @since 1.5.0
*
* @param WC_Order $order Order ID or order object.
* @param WP_REST_Request $request The API request currently being processed.
*
* @return void
*
* @phpcs:disable WordPress.Security.NonceVerification.Missing
*/
public function save_terms_acceptance( $order, $request ) {
$acceptance = null;

if ( isset( $request['extensions'], $request['extensions']['_woo_additional_terms'], $request['extensions']['_woo_additional_terms']['wat_checkbox'] ) ) {
$acceptance = '1';
if ( ! isset( $request['extensions'], $request['extensions']['_woo_additional_terms'], $request['extensions']['_woo_additional_terms']['wat_checkbox'] ) ) {
return;
}

if ( $acceptance ) {
$order->update_meta_data( '_woo_additional_terms', $acceptance );
}
$order->update_meta_data(
'_woo_additional_terms',
array(
'id' => wc_clean( get_option( '_woo_additional_terms_page_id', null ) ),
'notice' => wc_clean( get_option( '_woo_additional_terms_notice', '' ) ),
)
);
}

/**
* Add schema Store API to support posted data.
* Registers the checkout endpoint extension to inform our frontend component about the result of
* the validity of the additional terms checkbox and react accordingly.
*
* @since 1.5.0
* @return void
* @since 1.5.0
*
* @return void
*/
public function extend_store_api() {
$extend = StoreApi::container()->get(
Expand All @@ -186,16 +222,19 @@ public function extend_store_api() {
'endpoint' => CheckoutSchema::IDENTIFIER,
'namespace' => $this->get_name(),
'schema_callback' => function() {

return array(
'wat_checkbox' => array(
'type' => 'boolean',
'context' => array(),
'arg_options' => array(
'validate_callback' => function( $value ) {

if ( ! is_bool( $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 ) ) );
}

return true;
},
),
Expand Down
Loading

0 comments on commit ad55cf1

Please sign in to comment.