API
6 years ago
API.php
6 years ago
Card_Handler.php
6 years ago
Customer_Helper.php
6 years ago
Payment_Form.php
6 years ago
Customer_Helper.php
199 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Square |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@woocommerce.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * DISCLAIMER |
| 14 | * |
| 15 | * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer |
| 16 | * versions in the future. If you wish to customize WooCommerce Square for your |
| 17 | * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/ |
| 18 | * |
| 19 | * @author WooCommerce |
| 20 | * @copyright Copyright: (c) 2019, Automattic, Inc. |
| 21 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square\Gateway; |
| 25 | |
| 26 | defined( 'ABSPATH' ) || exit; |
| 27 | |
| 28 | use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework; |
| 29 | |
| 30 | class Customer_Helper { |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Adds customers to the local index. |
| 35 | * |
| 36 | * @since 2.0.0 |
| 37 | * |
| 38 | * @param \SquareConnect\Model\Customer[] $customers Square API customers |
| 39 | */ |
| 40 | public static function add_customers( array $customers ) { |
| 41 | global $wpdb; |
| 42 | |
| 43 | $placeholders = array(); |
| 44 | $values = array(); |
| 45 | |
| 46 | $query = "INSERT INTO {$wpdb->prefix}woocommerce_square_customers (square_id, email_address) VALUES "; |
| 47 | |
| 48 | foreach ( $customers as $customer ) { |
| 49 | |
| 50 | // skip any bad data |
| 51 | if ( ! $customer instanceof \SquareConnect\Model\Customer ) { |
| 52 | continue; |
| 53 | } |
| 54 | |
| 55 | $placeholders[] = '(%s, %s)'; |
| 56 | |
| 57 | $values[] = wc_clean( $customer->getId() ); |
| 58 | $values[] = wc_clean( $customer->getEmailAddress() ); |
| 59 | } |
| 60 | |
| 61 | $query .= implode( ', ', $placeholders ); |
| 62 | |
| 63 | // update the Square ID value when duplicate email addresses are present |
| 64 | $query .= " ON DUPLICATE KEY UPDATE email_address = VALUES(email_address)"; |
| 65 | |
| 66 | $wpdb->query( $wpdb->prepare( $query, $values ) ); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * Adds a customer to the index. |
| 72 | * |
| 73 | * @since 2.0.0 |
| 74 | * |
| 75 | * @param string $square_id Square customer ID |
| 76 | * @param string $email_address customer email address |
| 77 | * @param int $user_id WordPress user ID |
| 78 | */ |
| 79 | public static function add_customer( $square_id, $email_address, $user_id = 0 ) { |
| 80 | global $wpdb; |
| 81 | |
| 82 | if ( is_email( $email_address ) ) { |
| 83 | |
| 84 | $params = array( |
| 85 | 'square_id' => wc_clean( $square_id ), |
| 86 | 'email_address' => wc_clean( $email_address ), |
| 87 | ); |
| 88 | |
| 89 | if ( $user_id && is_numeric( $user_id ) ) { |
| 90 | $params['user_id'] = (int) $user_id; |
| 91 | } |
| 92 | |
| 93 | $wpdb->insert( |
| 94 | "{$wpdb->prefix}woocommerce_square_customers", |
| 95 | $params |
| 96 | ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * Gets a Square customer ID from an email address. |
| 103 | * |
| 104 | * @param string $email_address customer email address |
| 105 | * @return string|null |
| 106 | */ |
| 107 | public static function get_square_id( $email_address ) { |
| 108 | global $wpdb; |
| 109 | |
| 110 | $square_id = null; |
| 111 | |
| 112 | if ( is_email( $email_address ) ) { |
| 113 | |
| 114 | $square_id = $wpdb->get_var( |
| 115 | $wpdb->prepare( |
| 116 | "SELECT square_id FROM {$wpdb->prefix}woocommerce_square_customers WHERE email_address = %s", |
| 117 | $email_address |
| 118 | ) |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | return $square_id; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | public static function get_customers_by_email( $email_address ) { |
| 127 | global $wpdb; |
| 128 | |
| 129 | $square_ids = array(); |
| 130 | |
| 131 | if ( is_email( $email_address ) ) { |
| 132 | |
| 133 | $square_ids = $wpdb->get_col( |
| 134 | $wpdb->prepare( |
| 135 | "SELECT square_id FROM {$wpdb->prefix}woocommerce_square_customers WHERE email_address = %s", |
| 136 | $email_address |
| 137 | ) |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | return $square_ids; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Determines if a customer exists in the index. |
| 147 | * |
| 148 | * @since 2.0.0 |
| 149 | * |
| 150 | * @param string $square_id Square customer ID |
| 151 | * @return bool |
| 152 | */ |
| 153 | public static function is_customer_indexed( $square_id ) { |
| 154 | global $wpdb; |
| 155 | |
| 156 | $result = $wpdb->get_var( |
| 157 | $wpdb->prepare( |
| 158 | "SELECT * FROM {$wpdb->prefix}woocommerce_square_customers WHERE square_id = %s", |
| 159 | $square_id |
| 160 | ) |
| 161 | ); |
| 162 | |
| 163 | return (bool) $result; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * Creates the db table for the customer index. |
| 169 | * |
| 170 | * @since 2.0.0 |
| 171 | */ |
| 172 | public static function create_table() { |
| 173 | global $wpdb; |
| 174 | |
| 175 | $wpdb->hide_errors(); |
| 176 | |
| 177 | $collate = ''; |
| 178 | |
| 179 | if ( $wpdb->has_cap( 'collation' ) ) { |
| 180 | $collate = $wpdb->get_charset_collate(); |
| 181 | } |
| 182 | |
| 183 | $schema = " |
| 184 | CREATE TABLE IF NOT EXISTS {$wpdb->prefix}woocommerce_square_customers ( |
| 185 | square_id varchar(191) NOT NULL, |
| 186 | email_address varchar(200) NOT NULL, |
| 187 | user_id BIGINT UNSIGNED NOT NULL, |
| 188 | PRIMARY KEY (square_id) |
| 189 | ) $collate; |
| 190 | "; |
| 191 | |
| 192 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 193 | |
| 194 | dbDelta( $schema ); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | } |
| 199 |