PluginProbe ʕ •ᴥ•ʔ
TaxCloud for WooCommerce / 8.4.11
TaxCloud for WooCommerce v8.4.11
8.4.11 8.4.10 8.4.9 trunk 6.0.11 6.0.12 6.0.13 6.0.14 6.1.0 6.1.1 6.1.2 6.2.0 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.2.6 6.3.0 6.3.1 6.3.10 6.3.11 6.3.12 6.3.13 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.3.8 6.3.9 7.0.0 7.0.1 7.0.10 7.0.11 7.0.12 7.0.13 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 8.0.0 8.0.1 8.0.10 8.0.11 8.0.12 8.0.13 8.0.14 8.0.15 8.0.16 8.0.17 8.0.2 8.0.3 8.0.4 8.0.5 8.0.6 8.0.7 8.0.8 8.0.9 8.1.0 8.1.1 8.2.0 8.2.1 8.2.2 8.2.3 8.2.4 8.3.0 8.3.1 8.3.2 8.3.3 8.3.4 8.3.5 8.3.6 8.3.7 8.3.8 8.4.0 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.4.6 8.4.7 8.4.8
simple-sales-tax / includes / class-sst-product.php
simple-sales-tax / includes Last commit date
abstracts 3 months ago admin 3 months ago frontend 1 month ago integrations 1 month ago v3 3 months ago vendor 6 days ago views 8 months ago class-simplesalestax.php 6 days ago class-sst-addresses.php 1 month ago class-sst-ajax.php 3 months ago class-sst-assets.php 6 months ago class-sst-blocks-integration.php 1 month ago class-sst-blocks.php 1 year ago class-sst-certificates.php 6 days ago class-sst-install.php 6 months ago class-sst-logger.php 5 months ago class-sst-marketplaces.php 6 months ago class-sst-order-controller.php 3 months ago class-sst-order.php 1 month ago class-sst-origin-address.php 8 months ago class-sst-product.php 3 months ago class-sst-rate-limit.php 5 months ago class-sst-settings.php 3 months ago class-sst-shipping.php 3 years ago class-sst-taxcloud-v3-api.php 3 months ago class-sst-taxcloud-v3.php 3 months ago class-sst-tic.php 2 years ago class-sst-updater.php 3 years ago sst-compatibility-functions.php 4 months ago sst-functions.php 6 days ago sst-message-functions.php 3 years ago sst-update-functions.php 11 months ago
class-sst-product.php
268 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly.
5 }
6
7 /**
8 * SST Product.
9 *
10 * Contains methods for working with products.
11 *
12 * @author Simple Sales Tax
13 * @package SST
14 * @since 5.0
15 */
16 class SST_Product {
17
18 /**
19 * Register action hooks.
20 *
21 * @since 5.0
22 */
23 public static function init() {
24 if ( ! is_admin() ) {
25 return;
26 }
27
28 add_action( 'woocommerce_product_options_shipping', array( __CLASS__, 'output_origin_select_box' ) );
29 add_action( 'woocommerce_product_bulk_edit_start', array( __CLASS__, 'output_bulk_edit_fields' ) );
30 add_action( 'woocommerce_product_bulk_edit_save', array( __CLASS__, 'save_bulk_edit_fields' ) );
31 add_action( 'woocommerce_product_options_tax', array( __CLASS__, 'display_tic_field' ) );
32 add_action( 'woocommerce_product_after_variable_attributes', array( __CLASS__, 'display_tic_field' ), 10, 3 );
33 add_action( 'woocommerce_ajax_save_product_variations', array( __CLASS__, 'ajax_save_variation_tics' ) );
34 add_action( 'save_post_product', array( __CLASS__, 'save_product_meta' ) );
35 }
36
37 /**
38 * Returns an array of origin addresses for a given product. If no origin
39 * addresses have been configured, returns set of default origin addresses.
40 *
41 * @param int $product_id WooCommerce product ID.
42 *
43 * @return SST_Origin_Address[]
44 * @since 5.0
45 */
46 public static function get_origin_addresses( $product_id ) {
47 $raw_addresses = get_post_meta( $product_id, '_wootax_origin_addresses', true );
48
49 if ( ! is_array( $raw_addresses ) || empty( $raw_addresses ) ) {
50 return SST_Addresses::get_default_addresses();
51 }
52
53 $addresses = SST_Addresses::get_origin_addresses();
54 $return = array();
55
56 foreach ( $raw_addresses as $address_id ) {
57 if ( isset( $addresses[ $address_id ] ) ) {
58 $return[ $address_id ] = $addresses[ $address_id ];
59 }
60 }
61
62 return !empty( $return ) ? $return : SST_Addresses::get_default_addresses();
63 }
64
65 /**
66 * Get the TIC for a product.
67 *
68 * @param int $product_id WooCommerce product ID.
69 * @param int $variation_id WooCommerce product variation ID (default: 0).
70 *
71 * @return mixed TIC for product, or null if none set.
72 * @since 5.0
73 */
74 public static function get_tic( $product_id, $variation_id = 0 ) {
75 $product_tic = get_post_meta( $product_id, 'wootax_tic', true );
76 $variation_tic = ! $variation_id ? false : get_post_meta( $variation_id, 'wootax_tic', true );
77 $tic = $variation_tic ? $variation_tic : $product_tic;
78
79 /* Fall back to default TIC for product category */
80 if ( empty( $tic ) ) {
81 $categories = get_the_terms( $product_id, 'product_cat' );
82
83 if ( ! is_wp_error( $categories ) && $categories ) {
84 foreach ( $categories as $category ) {
85 $cat_tic = get_term_meta( $category->term_id, 'tic', true );
86 if ( ! empty( $cat_tic ) ) {
87 $tic = $cat_tic;
88 break;
89 }
90 }
91 }
92 }
93
94 /* Let devs adjust TIC */
95 $final_tic = apply_filters( 'wootax_product_tic', $tic, $product_id, $variation_id );
96
97 return empty( $final_tic ) ? null : $final_tic;
98 }
99
100 /**
101 * Is the given TIC a guns or ammo TIC?
102 *
103 * @param int $tic TIC.
104 *
105 * @return bool
106 * @since 8.4.4
107 */
108 public static function is_gun_or_ammo_tic( $tic ) {
109 return in_array( (int) $tic, array( 90505, 90506 ) );
110 }
111
112 /**
113 * Output "Shipping Origin Addresses" select box.
114 *
115 * @since 4.6
116 */
117 public static function output_origin_select_box() {
118 global $post;
119
120 $addresses = SST_Addresses::get_origin_addresses();
121
122 // Do not display if there is less than 2 origin addresses to select from.
123 $show_dropdown = is_array( $addresses ) && count( $addresses ) >= 2;
124
125 if ( apply_filters( 'sst_show_origin_address_dropdown', $show_dropdown ) ) {
126 include __DIR__ . '/admin/views/html-origin-select.php';
127 }
128 }
129
130 /**
131 * Output bulk edit fields.
132 *
133 * @since 5.0
134 */
135 public static function output_bulk_edit_fields() {
136 $field_args = array(
137 'default_text' => __( 'No Change', 'simple-sales-tax' ),
138 );
139
140 ?>
141 <label class="alignleft">
142 <span class="title"><?php esc_html_e( 'TIC', 'simple-sales-tax' ); ?></span>
143 <span class="input-text-wrap">
144 <?php sst_output_tic_select_field( $field_args ); ?>
145 </span>
146 </label>
147 <?php
148 }
149
150 /**
151 * Handle bulk TIC updates.
152 *
153 * @param WC_Product $product The product being saved.
154 *
155 * @since 4.2
156 */
157 public static function save_bulk_edit_fields( $product ) {
158 $tic = '';
159
160 if ( isset( $_REQUEST['wootax_tic'] ) ) {
161 $tic = sanitize_text_field( wp_unslash( $_REQUEST['wootax_tic'] ) ); // phpcs:ignore WordPress.CSRF.NonceVerification
162 }
163
164 if ( '' !== $tic ) {
165 update_post_meta( $product->get_id(), 'wootax_tic', $tic );
166 }
167 }
168
169 /**
170 * Output TIC select box on "Edit Product" screen.
171 *
172 * @param int $loop Loop counter used by WooCommerce when displaying variation attributes (default:
173 * null).
174 * @param array $variation_data Unused parameter (default: null).
175 * @param WP_Post $variation WP_Post for variation if variation is being displayed (default: null).
176 *
177 * @since 5.0
178 */
179 public static function display_tic_field( $loop = null, $variation_data = null, $variation = null ) {
180 global $post;
181
182 $is_variation = ! empty( $variation );
183
184 if ( $is_variation ) {
185 $product_id = $variation->ID;
186 $class = 'form-row form-field form-row-full';
187 } else {
188 $product_id = $post->ID;
189 $class = 'form-field';
190 }
191
192 ?>
193 <p class="<?php echo esc_attr( $class ); ?> wootax_tic">
194 <label for="wootax_tic[<?php echo esc_attr( $product_id ); ?>]">
195 <?php esc_html_e( 'TIC', 'simple-sales-tax' ); ?>
196 </label>
197 <?php if ( $is_variation ): ?>
198 <br>
199 <?php endif; ?>
200 <?php sst_output_tic_select_field( compact( 'product_id' ) ); ?>
201 </p>
202 <?php
203 }
204
205 /**
206 * Update variation TICs.
207 *
208 * @since 4.6
209 */
210 public static function ajax_save_variation_tics() {
211 if ( ! isset( $_POST['variable_post_id'], $_POST['wootax_tic'] ) ) { // phpcs:ignore WordPress.CSRF.NonceVerification
212 return;
213 }
214
215 $variable_post_id = array_map( 'absint', $_POST['variable_post_id'] ); // phpcs:ignore WordPress.CSRF.NonceVerification
216 $tic_selections = array_map( 'sanitize_text_field', wp_unslash( $_POST['wootax_tic'] ) ); // phpcs:ignore WordPress.CSRF.NonceVerification
217 $max_loop = max( array_keys( $variable_post_id ) );
218
219 for ( $i = 0; $i <= $max_loop; $i++ ) {
220 if ( ! isset( $variable_post_id[ $i ] ) ) {
221 continue;
222 }
223
224 $variation_id = $variable_post_id[ $i ];
225
226 if ( isset( $tic_selections[ $variation_id ] ) ) {
227 update_post_meta( $variation_id, 'wootax_tic', $tic_selections[ $variation_id ] );
228 }
229 }
230 }
231
232 /**
233 * Save the TIC and origin addresses for a product.
234 *
235 * @param int $product_id The ID of the product being saved.
236 *
237 * @since 4.2
238 */
239 public static function save_product_meta( $product_id ) {
240 if ( isset( $_REQUEST['_inline_edit'] ) || isset( $_REQUEST['bulk_edit'] ) ) { // phpcs:ignore WordPress.CSRF.NonceVerification
241 return;
242 }
243
244 // Save product origin addresses.
245 $origins = array();
246
247 if ( isset( $_REQUEST['_wootax_origin_addresses'] ) ) { // phpcs:ignore WordPress.CSRF.NonceVerification
248 $origins = array_map( 'sanitize_title', wp_unslash( $_REQUEST['_wootax_origin_addresses'] ) ); // phpcs:ignore WordPress.CSRF.NonceVerification
249 }
250
251 update_post_meta( $product_id, '_wootax_origin_addresses', $origins );
252
253 // Save product and variation TICs.
254 $selected_tics = array();
255
256 if ( isset( $_REQUEST['wootax_tic'] ) ) { // phpcs:ignore WordPress.CSRF.NonceVerification
257 $selected_tics = array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['wootax_tic'] ) ); // phpcs:ignore WordPress.CSRF.NonceVerification
258 }
259
260 foreach ( $selected_tics as $product_id => $tic ) {
261 update_post_meta( $product_id, 'wootax_tic', $tic );
262 }
263 }
264
265 }
266
267 SST_Product::init();
268