apis
1 year ago
purchase-orders
1 year ago
zoho-crm
1 year ago
zoho-inventory
1 year ago
class-api-handler-zoho.php
1 year ago
class-auth-zoho.php
1 year ago
class-common.php
1 year ago
class-plugin.php
1 year ago
class-wc-api.php
1 year ago
index.php
1 year ago
class-common.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Common class function. |
| 5 | */ |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | exit; |
| 8 | } |
| 9 | |
| 10 | if ( ! class_exists( 'CMBIRD_Common_Functions' ) ) { |
| 11 | class CMBIRD_Common_Functions { |
| 12 | |
| 13 | |
| 14 | public function __construct() { |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Function to clear all orphan data. |
| 19 | */ |
| 20 | public function clear_orphan_data() { |
| 21 | global $wpdb; |
| 22 | // Delete orphaned product variations |
| 23 | $deleted_variations = absint( |
| 24 | $wpdb->query( |
| 25 | "DELETE products |
| 26 | FROM {$wpdb->posts} products |
| 27 | LEFT JOIN {$wpdb->posts} wp ON wp.ID = products.post_parent |
| 28 | WHERE wp.ID IS NULL AND products.post_type = 'product_variation';" |
| 29 | ) |
| 30 | ); |
| 31 | // Delete orphaned postmeta |
| 32 | $deleted_postmeta = absint( |
| 33 | $wpdb->query( |
| 34 | "DELETE pm |
| 35 | FROM {$wpdb->postmeta} pm |
| 36 | LEFT JOIN {$wpdb->posts} wp ON wp.ID = pm.post_id |
| 37 | WHERE wp.ID IS NULL;" |
| 38 | ) |
| 39 | ); |
| 40 | // Return the number of deleted entries (orphaned variations + orphaned postmeta) |
| 41 | return $deleted_variations + $deleted_postmeta; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the tax class based on the tax percentage. |
| 46 | * |
| 47 | * @param float $percentage The tax percentage. |
| 48 | * @return string|false The tax class if found, or standard if not found. |
| 49 | */ |
| 50 | public function get_tax_class_by_percentage( $percentage ) { |
| 51 | // $fd = fopen( __DIR__ . '/get_tax_class_by_percentage.txt', 'a+' ); |
| 52 | |
| 53 | global $wpdb; |
| 54 | // Determine the number of decimal places in the provided percentage |
| 55 | $decimal_places = strlen( substr( strrchr( $percentage, '.' ), 1 ) ); |
| 56 | |
| 57 | // Round the percentage to the determined number of decimal places |
| 58 | $rounded_percentage = round( $percentage, $decimal_places ); |
| 59 | $tax_rates = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE ROUND(tax_rate, %d) = %f", $decimal_places, $rounded_percentage ) ); |
| 60 | |
| 61 | // If tax rates are found |
| 62 | if ( $tax_rates ) { |
| 63 | // Get the tax class from the first matching tax rate |
| 64 | $tax_class = $tax_rates[0]->tax_rate_class; |
| 65 | return $tax_class; |
| 66 | } else { |
| 67 | // Return null if no tax rates match the provided percentage |
| 68 | return 'standard'; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Send email to admin. |
| 74 | * |
| 75 | * @param string $subject The subject of the email. |
| 76 | * @param string $message The message body of the email. |
| 77 | * |
| 78 | * @return void |
| 79 | */ |
| 80 | public function send_email( $subject, $message ) { |
| 81 | $admin_email = get_option( 'admin_email' ); |
| 82 | $headers = array( 'Content-Type: text/html; charset=UTF-8' ); |
| 83 | |
| 84 | // Send the email |
| 85 | wp_mail( $admin_email, $subject, $message, $headers ); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 |