is_rest_api_request() && empty( $GLOBALS['wp']->query_vars['rest_route'] ) ) { return false; } $rest_route = $GLOBALS['wp']->query_vars['rest_route']; // Use regex to check any route that has "wc/store" with any of these text : "cart", "checkout", or "batch" // Example : wc/store/v3/batch preg_match( '/wc\/store\/v[0-9]{1,}\/(batch|cart|checkout)/', $rest_route, $route_matches, PREG_OFFSET_CAPTURE ); return ( ! empty( $route_matches ) ); } /** * Check if current page is a cart page or has woocommerce cart block. * * @return bool */ public static function is_cart() { if ( is_cart() || self::has_cart_block() ) { return true; } return false; } /** * Check if current page is a checkout page or has woocommerce checkout block. * * @return bool */ public static function is_checkout() { if ( is_checkout() || self::has_checkout_block() ) { return true; } return false; } /** * Check if current page has woocommerce cart block. * * @return bool */ public static function has_cart_block() { // To support WP < 5.0.0, we need to check if `has_block` exists first as has_block only being introduced on WP 5.0.0. if ( function_exists( 'has_block' ) ) { return has_block( 'woocommerce/cart' ); } return false; } /** * Check if current page has woocommerce checkout block. * * @return bool */ public static function has_checkout_block() { // To support WP < 5.0.0, we need to check if `has_block` exists first as has_block only being introduced on WP 5.0.0. if ( function_exists( 'has_block' ) ) { return has_block( 'woocommerce/checkout' ); } return false; } /** * Check if current page has woocommerce cart or checkout block. * * @return bool */ public static function has_cart_or_checkout_block() { if ( self::has_checkout_block() || self::has_cart_block() ) { return true; } return false; } /** * Checks whether the current user has permissions to manage shipping labels. * * @return boolean */ public static function user_can_manage_labels() { /** * @since 1.25.14 */ return apply_filters( 'wcship_user_can_manage_labels', current_user_can( 'manage_woocommerce' ) || current_user_can( 'wcship_manage_labels' ) ); } /** * Exports existing tax rates to a CSV and clears the table. * * Ported from TaxJar's plugin. * See: https://github.com/taxjar/taxjar-woocommerce-plugin/blob/42cd4cd0/taxjar-woocommerce.php#L75 * * @return boolean */ public static function backup_existing_tax_rates() { global $wpdb; // Export Tax Rates $rates = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates ORDER BY tax_rate_order LIMIT %d, %d ", 0, 10000 ) ); if ( empty( $rates ) ) { return false; } ob_start(); $header = __( 'Country Code', 'woocommerce' ) . ',' . __( 'State Code', 'woocommerce' ) . ',' . __( 'ZIP/Postcode', 'woocommerce' ) . ',' . __( 'City', 'woocommerce' ) . ',' . __( 'Rate %', 'woocommerce' ) . ',' . __( 'Tax Name', 'woocommerce' ) . ',' . __( 'Priority', 'woocommerce' ) . ',' . __( 'Compound', 'woocommerce' ) . ',' . __( 'Shipping', 'woocommerce' ) . ',' . __( 'Tax Class', 'woocommerce' ) . "\n"; echo $header; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped foreach ( $rates as $rate ) { if ( $rate->tax_rate_country ) { echo esc_attr( $rate->tax_rate_country ); } else { echo '*'; } echo ','; if ( $rate->tax_rate_state ) { echo esc_attr( $rate->tax_rate_state ); } else { echo '*'; } echo ','; $locations = $wpdb->get_col( $wpdb->prepare( "SELECT location_code FROM {$wpdb->prefix}woocommerce_tax_rate_locations WHERE location_type='postcode' AND tax_rate_id = %d ORDER BY location_code", $rate->tax_rate_id ) ); if ( $locations ) { echo esc_attr( implode( '; ', $locations ) ); } else { echo '*'; } echo ','; $locations = $wpdb->get_col( $wpdb->prepare( "SELECT location_code FROM {$wpdb->prefix}woocommerce_tax_rate_locations WHERE location_type='city' AND tax_rate_id = %d ORDER BY location_code", $rate->tax_rate_id ) ); if ( $locations ) { echo esc_attr( implode( '; ', $locations ) ); } else { echo '*'; } echo ','; if ( $rate->tax_rate ) { echo esc_attr( $rate->tax_rate ); } else { echo '0'; } echo ','; if ( $rate->tax_rate_name ) { echo esc_attr( $rate->tax_rate_name ); } else { echo '*'; } echo ','; if ( $rate->tax_rate_priority ) { echo esc_attr( $rate->tax_rate_priority ); } else { echo '1'; } echo ','; if ( $rate->tax_rate_compound ) { echo esc_attr( $rate->tax_rate_compound ); } else { echo '0'; } echo ','; if ( $rate->tax_rate_shipping ) { echo esc_attr( $rate->tax_rate_shipping ); } else { echo '0'; } echo ','; echo "\n"; } // End foreach(). $csv = ob_get_clean(); $upload_dir = wp_upload_dir(); $backup_dir = $upload_dir['basedir'] . '/woocommerce_uploads/taxes'; // Create the protected backup directory if it doesn't exist. if ( ! file_exists( $backup_dir ) ) { if ( ! wp_mkdir_p( $backup_dir ) ) { // Directory could not be created; fall back to the uploads root. $backup_dir = $upload_dir['basedir']; } else { self::protect_backup_directory( $backup_dir ); } } elseif ( ! file_exists( $backup_dir . '/.htaccess' ) ) { // Re-create protection files if they were removed. self::protect_backup_directory( $backup_dir ); } // Build filename with wp_hash() suffix to prevent URL guessing (same pattern as WC log files). $base_name = 'taxjar-wc_tax_rates-' . gmdate( 'Y-m-d' ) . '-' . time(); $hash_suffix = wp_hash( $base_name ); $backed_up = file_put_contents( $backup_dir . '/' . $base_name . '-' . $hash_suffix . '.csv', $csv ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents return (bool) $backed_up; } /** * Creates protection files in the backup directory to prevent direct HTTP access. * * Follows the same pattern used by WooCommerce's ReportCSVExporter::maybe_create_directory(). * * @param string $dir The directory to protect. */ private static function protect_backup_directory( $dir ) { $files = array( array( 'base' => $dir, 'file' => '.htaccess', 'content' => 'DirectoryIndex index.php index.html' . PHP_EOL . 'deny from all', ), array( 'base' => $dir, 'file' => 'index.html', 'content' => '', ), array( 'base' => $dir, 'file' => 'index.php', 'content' => '