<commit_before><?php

namespace WC_Toolkit;

class Ajax
{
    public function __construct()
    {
        $ajax_events = [
            'add_to_cart',
            'remove_from_cart',
            'cart_set_quantity',
            'add_coupon',
            'remove_coupon',
            'cart_fragments'
        ];

        foreach ($ajax_events as $ajax_event) {
            add_action('wc_ajax_site_' . $ajax_event, [ $this, $ajax_event ]);
        }
    }


    /**
     * Returns fragments on success
     */
    public function add_to_cart()
    {
        $product_id = absint($this->_get('product_id'));
        $variation_id = absint($this->_get('variation_id'));
        $quantity = $this->_get('quantity') ? absint($this->_get('quantity')) : 1;


        if (! empty($variation_id)) {
            $cart_item = $this->add_to_cart_handler_variable($product_id);
        } else {
            $cart_item = WC()->cart->add_to_cart($product_id, $quantity);
        }

        if ($cart_item) {
            wc_clear_notices();
            $this->_send_cart_success();
        } else {
            $this->_return_first_notice();
        }
    }


    /**
     * Returns fragments on success
     */
    public function remove_from_cart()
    {
        $item_key = wc_clean($this->_get('item_key'));

        if ($item_key) {
            WC()->cart->remove_cart_item($item_key);
            $this->_send_cart_success();
        }

        exit();
    }


    /**
     * Note: Has allowance for quantity to be zero
     */
    public function cart_set_quantity()
    {
        $item_key = sanitize_text_field($this->_get('item_key'));
        $quantity = absint($this->_get('quantity'));

        if (! $item_key) {
            wp_send_json_error();
        }


        if (! $quantity) {
            // remove
            WC()->cart->remove_cart_item($item_key);
        } else {
            $cart_item = WC()->cart->get_cart_item($item_key);
            $product_data = wc_get_product($cart_item['variation_id'] ? $cart_item['variation_id'] : $cart_item['product_id']);
<commit_msg>Add validation hooks<commit_after><?php

namespace WC_Toolkit;

class Ajax
{
    public function __construct()
    {
        $ajax_events = [
            'add_to_cart',
            'remove_from_cart',
            'cart_set_quantity',
            'add_coupon',
            'remove_coupon',
            'cart_fragments'
        ];

        foreach ($ajax_events as $ajax_event) {
            add_action('wc_ajax_site_' . $ajax_event, [ $this, $ajax_event ]);
        }
    }


    /**
     * Returns fragments on success
     */
    public function add_to_cart()
    {
        $product_id = absint($this->_get('product_id'));
        $variation_id = absint($this->_get('variation_id'));
        $quantity = $this->_get('quantity') ? absint($this->_get('quantity')) : 1;
        $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );

        if ( !$passed_validation ) {
            $this->_return_first_notice();
        }


        if (! empty($variation_id)) {
            $cart_item = $this->add_to_cart_handler_variable($product_id);
        } else {
            $cart_item = WC()->cart->add_to_cart($product_id, $quantity);
        }

        if ($cart_item) {
            wc_clear_notices();
            $this->_send_cart_success();
        } else {
            $this->_return_first_notice();
        }
    }


    /**
     * Returns fragments on success
     */
    public function remove_from_cart()
    {
        $item_key = wc_clean($this->_get('item_key'));

        if ($item_key) {
            WC()->cart->remove_cart_item($item_key);
            $this->_send_cart_success();
        }

        exit();
    }


    /**
     * Note: Has allowance for quantity to be zero
     */
    public function cart_set_quantity()
    {
        $item_key = sanitize_text_field($this->_get('item_key'));
        $quantity = absint($this->_get('quantity'));

        if (! $item_key) {
            wp_send_json_error();
        }


        if (! $quantity) {
            // remove
            WC()->cart->remove_cart_item($item_key);
        } else {

            $cart_item = WC()->cart->get_cart_item($item_key);
            $product_data = wc_get_product($cart_item['variation_id'] ? $cart_item['variation_id'] : $cart_item['product_id']);

            $passed_validation  = apply_filters( 'woocommerce_update_cart_validation', true, $item_key, $cart_item, $quantity );

            if ( !$passed_validation ) {
                $this->_return_first_notice();
            }

{'fraction_non_alphanumeric': 0.1429813124728379, 'fraction_numerical': 0.000434593654932638, 'mean_word_length': 2.026298487836949, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2706879', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1396, 'n_words': 211}