PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.10.0
Subscriptions for WooCommerce with Stripe Recurring Payments v1.10.0
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Illuminate / Gateways / Paypal / PaypalDB.php

PaypalDB.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.10.0, at includes/Illuminate/Gateways/Paypal/PaypalDB.php

150 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * PaypalDB - Database helper for PayPal gateway mappings.
4 *
5 * @package SpringDevs\Subscription\Illuminate\Gateways\Paypal
6 */
7
8 namespace SpringDevs\Subscription\Illuminate\Gateways\Paypal;
9
10 /**
11 * Database helper for PayPal gateway mappings.
12 *
13 * Responsible for ensuring the mapping table exists and providing
14 * basic CRUD helpers for paypal_id <-> order_id / subscription_id mappings.
15 */
16 class PaypalDB {
17
18 /**
19 * Get the table name for the mapping table.
20 *
21 * @return string
22 */
23 public static function table_name(): string {
24 global $wpdb;
25 return $wpdb->prefix . 'subscrpt_paypal_map';
26 }
27
28 /**
29 * Ensure the mapping table exists. If not, create it using dbDelta.
30 * Safe to call repeatedly.
31 *
32 * @return void
33 */
34 public static function maybe_create_tables(): void {
35 global $wpdb;
36
37 $table = self::table_name();
38
39 // Direct query required: SHOW TABLES has no WP abstraction. No caching: result must reflect actual DB state to avoid skipping table creation.
40 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
41 $exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) );
42
43 if ( $exists === $table ) {
44 return;
45 }
46
47 // Create table if missing.
48 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
49
50 $charset_collate = $wpdb->get_charset_collate();
51
52 $sql = "CREATE TABLE {$table} (
53 paypal_id varchar(191) NOT NULL,
54 order_id bigint unsigned NOT NULL DEFAULT 0,
55 subscription_id bigint unsigned NOT NULL DEFAULT 0,
56 PRIMARY KEY (paypal_id),
57 KEY order_id (order_id),
58 KEY subscription_id (subscription_id)
59 ) {$charset_collate};";
60
61 dbDelta( $sql );
62 }
63
64 /**
65 * Insert or replace a mapping row.
66 *
67 * @param string $paypal_id PayPal billing agreement / subscription ID.
68 * @param int $subscription_id WPSubscription post ID.
69 * @param int $order_id WooCommerce order ID.
70 * @return void
71 */
72 public static function upsert_mapping( string $paypal_id, int $subscription_id = 0, int $order_id = 0 ): void {
73 global $wpdb;
74 $table = self::table_name();
75
76 // Direct query required: no WP API for REPLACE INTO. No caching: write operation, caching not applicable.
77 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
78 $wpdb->replace(
79 $table,
80 [
81 'paypal_id' => $paypal_id,
82 'order_id' => $order_id,
83 'subscription_id' => $subscription_id,
84 ],
85 [ '%s', '%d', '%d' ]
86 );
87
88 // Invalidate read caches for this paypal_id.
89 $hash = md5( $paypal_id );
90 wp_cache_delete( 'subscrpt_paypal_sub_' . $hash, 'subscrpt_paypal' );
91 wp_cache_delete( 'subscrpt_paypal_order_' . $hash, 'subscrpt_paypal' );
92 }
93
94 /**
95 * Get subscription_id by paypal_id.
96 *
97 * @param string $paypal_id PayPal billing agreement / subscription ID.
98 * @return int|null
99 */
100 public static function get_subscription_by_paypal_id( string $paypal_id ): ?int {
101 global $wpdb;
102 $table = self::table_name();
103 $cache_key = 'subscrpt_paypal_sub_' . md5( $paypal_id );
104
105 $cached = wp_cache_get( $cache_key, 'subscrpt_paypal' );
106 if ( false !== $cached ) {
107 return (int) $cached;
108 }
109
110 // Direct query required: plugin-owned mapping table has no WP abstraction layer.
111 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
112 $val = $wpdb->get_var( $wpdb->prepare( 'SELECT subscription_id FROM %i WHERE paypal_id = %s LIMIT 1', $table, $paypal_id ) );
113
114 if ( $val ) {
115 wp_cache_set( $cache_key, (int) $val, 'subscrpt_paypal' );
116 return (int) $val;
117 }
118
119 return null;
120 }
121
122 /**
123 * Get order_id by paypal_id.
124 *
125 * @param string $paypal_id PayPal billing agreement / subscription ID.
126 * @return int|null
127 */
128 public static function get_order_by_paypal_id( string $paypal_id ): ?int {
129 global $wpdb;
130 $table = self::table_name();
131 $cache_key = 'subscrpt_paypal_order_' . md5( $paypal_id );
132
133 $cached = wp_cache_get( $cache_key, 'subscrpt_paypal' );
134 if ( false !== $cached ) {
135 return (int) $cached;
136 }
137
138 // Direct query required: plugin-owned mapping table has no WP abstraction layer.
139 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
140 $val = $wpdb->get_var( $wpdb->prepare( 'SELECT order_id FROM %i WHERE paypal_id = %s LIMIT 1', $table, $paypal_id ) );
141
142 if ( $val ) {
143 wp_cache_set( $cache_key, (int) $val, 'subscrpt_paypal' );
144 return (int) $val;
145 }
146
147 return null;
148 }
149 }
150