Plugin Directory

Changeset 3049554


Ignore:
Timestamp:
03/12/2024 07:18:55 AM (2 years ago)
Author:
giannis4
Message:

v.1.2.11: unload scripts from non checkout block pages

Location:
terms-and-conditions-per-product
Files:
262 added
4 edited

Legend:

Unmodified
Added
Removed
  • terms-and-conditions-per-product/trunk/app/main/class-tacpp4-terms-and-conditions-checkout-block.php

    r3035417 r3049554  
    1010 */
    1111if ( ! defined( 'ABSPATH' ) ) {
    12     exit;
     12    exit;
    1313}
    1414
     
    1616if ( ! class_exists( 'TACPP4_Terms_Conditions_Checkout_Block' ) ) {
    1717
    18     /**
    19     * Class for TACPP4_Terms_Conditions_Checkout_Block.
    20     */
    21     class TACPP4_Terms_Conditions_Checkout_Block {
     18    /**
     19    * Class for TACPP4_Terms_Conditions_Checkout_Block.
     20    */
     21    class TACPP4_Terms_Conditions_Checkout_Block {
    2222
    23         protected static $instance = null;
     23        protected static $instance = null;
    2424
    25         public static function get_instance() {
    26             null === self::$instance and self::$instance = new self;
     25        public static function get_instance() {
     26            null === self::$instance and self::$instance = new self;
    2727
    28             return self::$instance;
    29         }
     28            return self::$instance;
     29        }
    3030
    3131
    32         /**
    33         * Constructor for class.
    34         */
    35         public function __construct() {
     32        /**
     33        * Constructor for class.
     34        */
     35        public function __construct() {
    3636
    37             // Enqueue front-end scripts
    38             add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 100 );
     37            // Enqueue front-end scripts
     38            add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 100 );
    3939
    40             // Enqueue Back end scripts
    41             add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ), 100 );
     40            // Add product specific Terms and Conditions to WC Checkout Block
     41            add_filter( "render_block_woocommerce/checkout-terms-block",
     42                array( $this, 'add_checkout_per_product_terms_on_block' ), 10, 3 );
    4243
    43             // Add product specific Terms and Conditions to WC Checkout Block
    44             add_filter( "render_block_woocommerce/checkout-terms-block",
    45                 array( $this, 'add_checkout_per_product_terms_on_block' ), 10, 3 );
     44        }
    4645
    47         }
    48 
    49         /**
    50          * Enqueue style/script.
    51          *
    52          * @return void
    53          */
    54         public function enqueue_scripts() {
    55             wp_enqueue_style(
    56                 'terms-checkout-style',
    57                 TACPP4_PLUGIN_URL . 'assets/css/checkout.css',
    58                 '',
    59                 TACPP4_PLUGIN_VERSION
    60             );
    61             // Register plugin's JS script
    62             wp_register_script(
    63                 'terms-checkout-action-script',
    64                 TACPP4_PLUGIN_URL . 'assets/js/extensions/checkout/frontend.js',
    65                 array(
    66                     'jquery',
    67                 ),
    68                 TACPP4_PLUGIN_VERSION,
    69                 false
    70             );
     46        /**
     47         * Enqueue style/script.
     48         *
     49         * @return void
     50         */
     51        public function enqueue_scripts() {
     52            // Bailout if the Gutenberg checkout block is not present
     53            if ( is_admin() || ! self::is_wc_checkout_block() ) {
     54                return;
     55            }
     56            wp_enqueue_style(
     57                'terms-checkout-style',
     58                TACPP4_PLUGIN_URL . 'assets/css/checkout.css',
     59                '',
     60                TACPP4_PLUGIN_VERSION
     61            );
     62            // Register plugin's JS script
     63            wp_register_script(
     64                'terms-checkout-action-script',
     65                TACPP4_PLUGIN_URL . 'assets/js/extensions/checkout/frontend.js',
     66                array(
     67                    'jquery',
     68                ),
     69                TACPP4_PLUGIN_VERSION,
     70                false
     71            );
    7172
    7273            $not_checked_notice = apply_filters( 'tacpp_block_checkout_not_checked_notice',
     
    7879                'tacppChBlock',
    7980                array(
    80                     'notCheckedNotice'   => $not_checked_notice
     81                    'notCheckedNotice' => $not_checked_notice
    8182                )
    8283            );
    8384
    84             wp_enqueue_script( 'terms-checkout-action-script' );
     85            wp_enqueue_script( 'terms-checkout-action-script' );
    8586
    86         }
     87        }
    8788
    88         /**
    89          * Enqueue Admin style/script.
    90          *
    91          * @return void
    92          */
    93         public function admin_enqueue_scripts() {
    9489
    95         }
     90        public function add_checkout_per_product_terms_on_block( $block_content, $block, $block_instance ) {
     91            if ( is_admin() || ! self::is_wc_checkout_block() ) {
     92                return $block_content;
     93            }
    9694
    97         public function add_checkout_per_product_terms_on_block( $block_content, $block, $block_instance ) {
     95            $output_html = ''; // Initialize an empty string to store the HTML output
    9896
    99             $output_html = ''; // Initialize an empty string to store the HTML output
     97            ob_start();
     98            TACPP4_Terms_Conditions_Per_Product::add_checkout_per_product_terms();
     99            $output_html .= ob_get_clean();
    100100
    101             ob_start();
    102             TACPP4_Terms_Conditions_Per_Product::add_checkout_per_product_terms();
    103             $output_html     .= ob_get_clean();
     101            $admin_settings  = get_option( 'tacpp_admin_settings' );
     102            $terms_must_read = $admin_settings['terms_must_read'];
    104103
    105             $admin_settings  = get_option( 'tacpp_admin_settings' );
    106             $terms_must_read = $admin_settings['terms_must_read'];
     104            $hide_wc_terms = isset( $admin_settings['hide_default_terms'] ) ? $admin_settings['hide_default_terms'] : 0;
    107105
    108             $hide_wc_terms   = isset( $admin_settings['hide_default_terms'] ) ? $admin_settings['hide_default_terms'] : 0;
     106            if ( $hide_wc_terms > 0 ) {
     107                $block_content = $output_html;
     108            } else {
     109                $block_content = $block_content . $output_html;
     110            }
    109111
    110             if ( $hide_wc_terms > 0 ) {
    111                 $block_content = $output_html;
    112             } else {
    113                 $block_content = $block_content . $output_html;
    114             }
     112            return $block_content;
     113        }
    115114
    116             return $block_content;
    117         }
     115        /**
     116         * Function to check if the WC checkout block is present.
     117         *
     118         * @return bool   True if it is, false otherwise.
     119         */
     120        public static function is_wc_checkout_block() {
     121            // Get the ID of the current post
     122            $post_id    = get_the_ID();
     123            // Gutenberg checkout block name
     124            $block_name = 'woocommerce/checkout';
    118125
    119     }
    120126
    121     new TACPP4_Terms_Conditions_Checkout_Block();
     127            // Get the post content.
     128            $post_content = get_post_field( 'post_content', $post_id );
     129
     130            // Parse the content to extract block information.
     131            $blocks = parse_blocks( $post_content );
     132
     133            // Loop through the blocks to check for the specified block name.
     134            foreach ( $blocks as $block ) {
     135                if ( isset( $block['blockName'] ) && $block['blockName'] === $block_name ) {
     136                    return true; // Block found
     137                }
     138            }
     139
     140            return false; // Block not found
     141        }
     142
     143    }
     144
     145    new TACPP4_Terms_Conditions_Checkout_Block();
    122146}
  • terms-and-conditions-per-product/trunk/assets/js/extensions/checkout/frontend.js

    r3035417 r3049554  
    11class HandleProductsTerms {
    2   constructor(term, allTerms) {
    3     this.term = term;
    4     this.allTerms = allTerms;
    5     this.label = this.term.querySelector(".woocommerce-form__label");
    6     this.checkboxInput = this.term.querySelector(
    7       ".woocommerce-form__input-checkbox"
    8     );
    9     this.placeOrderButton = document.querySelector(
    10       ".wc-block-checkout__main .wc-block-components-checkout-place-order-button"
    11     );
    12     this.mustReadTerms = document.querySelector(".extra-terms-must-read");
     2    constructor(term, allTerms) {
     3        this.term = term;
     4        this.allTerms = allTerms;
     5        this.label = this.term.querySelector(".woocommerce-form__label");
     6        this.checkboxInput = this.term.querySelector(
     7            ".woocommerce-form__input-checkbox"
     8        );
     9        this.placeOrderButton = document.querySelector(
     10            ".wc-block-checkout__main .wc-block-components-checkout-place-order-button"
     11        );
     12        this.mustReadTerms = document.querySelector(".extra-terms-must-read");
    1313
    14     this.termLink = this.term.querySelector("a");
    15     this.isAllTermsChecked = false;
    16     this.init();
    17   }
    18   init() {
    19     this.handleCheckbox();
    20     this.handleLink();
    21     this.handleOrderButtonVisibility();
    22   }
    23   handleOrderButtonVisibility(hide = true) {
    24     if (hide) {
    25       this.placeOrderButton.disabled = true;
    26       this.placeOrderButton.classList.add("terms-not-checked");
    27     } else {
    28       this.placeOrderButton.disabled = false;
    29       if (this.placeOrderButton.classList.contains("terms-not-checked")) {
    30         this.placeOrderButton.classList.remove("terms-not-checked");
    31       }
    32     }
    33   }
    34   checkAllTerms() {
    35     const mappedTerms = Array.from(this.allTerms).filter((term) => {
    36       const checkboxInput = term.querySelector(
    37         ".woocommerce-form__input-checkbox"
    38       );
    39       return checkboxInput.checked;
    40     });
    41     return mappedTerms;
    42   }
    43   handleCheckbox() {
    44     this.label.addEventListener("click", (e) => {
    45       const isChecked = this.checkboxInput.checked;
    46       const name = this.checkboxInput.name;
    47       const getTerms = sessionStorage.getItem("extraTerms");
    48       const clickedTerms = getTerms ? JSON.parse(getTerms) : [];
    49       const checkedTerms = this.checkAllTerms();
     14        this.termLink = this.term.querySelector("a");
     15        this.isAllTermsChecked = false;
     16        this.init();
     17    }
    5018
    51       if (this.mustReadTerms) {
    52         if (clickedTerms.includes(name)) {
    53           this.checkboxInput.disabled = false;
    54           this.checkboxInput.checked = isChecked;
    55           if (checkedTerms.length >= this.allTerms.length) {
    56             this.handleOrderButtonVisibility(false);
    57           } else {
    58             this.handleOrderButtonVisibility(true);
    59           }
    60         } else {
    61           this.checkboxInput.checked = false;
    62         }
    63       } else {
    64         this.checkboxInput.checked = isChecked;
    65         if (checkedTerms.length >= this.allTerms.length) {
    66           this.handleOrderButtonVisibility(false);
    67         } else {
    68           this.handleOrderButtonVisibility(true);
    69         }
    70       }
    71     });
    72   }
    73   handleLink() {
    74     this.termLink.addEventListener("click", (e) => {
    75       const getTerms = sessionStorage.getItem("extraTerms");
    76       const clickedTerms = getTerms ? JSON.parse(getTerms) : [];
    77       if (!clickedTerms.includes(this.checkboxInput.name)) {
    78         clickedTerms.push(this.checkboxInput.name);
    79         sessionStorage.setItem("extraTerms", JSON.stringify(clickedTerms));
    80       }
    81     });
    82   }
     19    init() {
     20        this.handleCheckbox();
     21        this.handleLink();
     22        this.handleOrderButtonVisibility();
     23    }
     24
     25    handleOrderButtonVisibility(hide = true) {
     26        if(hide) {
     27            this.placeOrderButton.disabled = true;
     28            this.placeOrderButton.classList.add("terms-not-checked");
     29        } else {
     30            this.placeOrderButton.disabled = false;
     31            if(this.placeOrderButton.classList.contains("terms-not-checked")) {
     32                this.placeOrderButton.classList.remove("terms-not-checked");
     33            }
     34        }
     35    }
     36
     37    checkAllTerms() {
     38        const mappedTerms = Array.from(this.allTerms).filter((term) => {
     39            const checkboxInput = term.querySelector(
     40                ".woocommerce-form__input-checkbox"
     41            );
     42            return checkboxInput.checked;
     43        });
     44        return mappedTerms;
     45    }
     46
     47    handleCheckbox() {
     48        this.label.addEventListener("click", (e) => {
     49            const isChecked = this.checkboxInput.checked;
     50            const name = this.checkboxInput.name;
     51            const getTerms = sessionStorage.getItem("extraTerms");
     52            const clickedTerms = getTerms ? JSON.parse(getTerms) : [];
     53            const checkedTerms = this.checkAllTerms();
     54
     55            if(this.mustReadTerms) {
     56                if(clickedTerms.includes(name)) {
     57                    this.checkboxInput.disabled = false;
     58                    this.checkboxInput.checked = isChecked;
     59                    if(checkedTerms.length >= this.allTerms.length) {
     60                        this.handleOrderButtonVisibility(false);
     61                    } else {
     62                        this.handleOrderButtonVisibility(true);
     63                    }
     64                } else {
     65                    this.checkboxInput.checked = false;
     66                }
     67            } else {
     68                this.checkboxInput.checked = isChecked;
     69                if(checkedTerms.length >= this.allTerms.length) {
     70                    this.handleOrderButtonVisibility(false);
     71                } else {
     72                    this.handleOrderButtonVisibility(true);
     73                }
     74            }
     75        });
     76    }
     77
     78    handleLink() {
     79        this.termLink.addEventListener("click", (e) => {
     80            const getTerms = sessionStorage.getItem("extraTerms");
     81            const clickedTerms = getTerms ? JSON.parse(getTerms) : [];
     82            if(!clickedTerms.includes(this.checkboxInput.name)) {
     83                clickedTerms.push(this.checkboxInput.name);
     84                sessionStorage.setItem("extraTerms", JSON.stringify(clickedTerms));
     85            }
     86        });
     87    }
    8388}
    8489
    8590window.addEventListener("load", () => {
    86   const extraTerms = document.querySelectorAll(
    87     ".wc-block-checkout__main .extra-terms"
    88   );
    89   const placeOrderButton = document.querySelector(
    90     ".wc-block-checkout__main .wc-block-components-checkout-place-order-button"
    91   );
    92   extraTerms.forEach((term) => {
    93     new HandleProductsTerms(term, extraTerms);
    94   });
    95   let toolTipWrapper = document.createElement("div");
    96   toolTipWrapper.classList.add("extra-terms-place-order-tooltip-wrapper");
    97   let toolTip = document.createElement("p");
    98   toolTip.classList.add("extra-terms-place-order-tooltip");
    99   toolTip.innerHTML = tacppChBlock.notCheckedNotice;
    100   toolTipWrapper.appendChild(toolTip);
    101   placeOrderButton.appendChild(toolTipWrapper);
     91    const extraTerms = document.querySelectorAll(
     92        ".wc-block-checkout__main .extra-terms"
     93    );
     94    const placeOrderButton = document.querySelector(
     95        ".wc-block-checkout__main .wc-block-components-checkout-place-order-button"
     96    );
     97
     98    // Bailout if no order button exists
     99    if(placeOrderButton === undefined || placeOrderButton === null) {
     100        return;
     101    }
     102
     103    extraTerms.forEach((term) => {
     104        new HandleProductsTerms(term, extraTerms);
     105    });
     106    let toolTipWrapper = document.createElement("div");
     107    toolTipWrapper.classList.add("extra-terms-place-order-tooltip-wrapper");
     108    let toolTip = document.createElement("p");
     109    toolTip.classList.add("extra-terms-place-order-tooltip");
     110    toolTip.innerHTML = tacppChBlock.notCheckedNotice;
     111    toolTipWrapper.appendChild(toolTip);
     112    placeOrderButton.appendChild(toolTipWrapper);
    102113});
  • terms-and-conditions-per-product/trunk/readme.txt

    r3035417 r3049554  
    55Tested up to: 6.4.1
    66Requires PHP: 7.2
    7 Stable tag: 1.2.10
     7Stable tag: 1.2.11
    88Author: Terms and Conditions Per Product
    99License: GPLv2 or later
     
    8585
    8686== Changelog ==
     87= 1.2.11 =
     88* Fix: Remove the checkout enqueued JS and functionality from pages that do not contain the Gutenberg checkout block.
     89
    8790= 1.2.10 =
    8891* Update: Enable Block Checkout functionality
  • terms-and-conditions-per-product/trunk/terms-per-product.php

    r3035417 r3049554  
    1313 * Plugin URI:        https://tacpp-pro.com
    1414 * Description:       Set custom Terms and Conditions per WooCommerce product, category or tag and display them on the checkout page.
    15  * Version:           1.2.10
     15 * Version:           1.2.11
    1616 * Author:            Terms Per Product
    1717 * Author URI:        https://tacpp-pro.com
     
    3333     * The version of the plugin.
    3434     */
    35     define( 'TACPP4_PLUGIN_VERSION', '1.2.10' );
     35    define( 'TACPP4_PLUGIN_VERSION', '1.2.11' );
    3636}
    3737
Note: See TracChangeset for help on using the changeset viewer.