| @@ -121,9 +121,8 @@ | ||
| 121 | 121 | add_filter( 'posts_orderby', [$this, 'rename_meta_table_name_in_query'], 99999, 2 ); |
| 122 | 122 | } |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - | |
| 126 | 125 | /** |
| 127 | 126 | * Retrieve payment meta field for a payment. |
| 128 | 127 | * |
| 129 | 128 | * @since 3.1.0 Return empty array, when request raw metadata if $single is set to false and metadata does not exist. |
| @@ -136,34 +135,40 @@ | ||
| 136 | 135 | * @return mixed Will be an array if $single is false. Will be value of meta data field if $single |
| 137 | 136 | * is true. |
| 138 | 137 | */ |
| 139 | 138 | public function get_meta( $id = 0, $meta_key = '', $single = false ) { |
| 140 | - if ( ! $this->is_filter_callback ) { | |
| 141 | - return get_metadata( $this->meta_type, $id, $meta_key, $single ); | |
| 142 | - } | |
| 139 | + $previousMetaTableName = $this->setMetaTableName(); | |
| 143 | 140 | |
| 144 | - $id = $this->sanitize_id( $id ); | |
| 141 | + try { | |
| 142 | + if ( ! $this->is_filter_callback ) { | |
| 143 | + return get_metadata( $this->meta_type, $id, $meta_key, $single ); | |
| 144 | + } | |
| 145 | 145 | |
| 146 | - // Bailout. | |
| 147 | - if ( ! $this->is_valid_post_type( $id ) ) { | |
| 148 | - return $this->check; | |
| 149 | - } | |
| 146 | + $id = $this->sanitize_id( $id ); | |
| 150 | 147 | |
| 151 | - if ( $this->raw_result ) { | |
| 152 | - if ( ! ( $value = get_metadata( $this->meta_type, $id, $meta_key, false ) ) ) { | |
| 153 | - $value = $single ? '' : array(); | |
| 148 | + // Bailout. | |
| 149 | + if ( ! $this->is_valid_post_type( $id ) ) { | |
| 150 | + return $this->check; | |
| 154 | 151 | } |
| 155 | 152 | |
| 156 | - // Reset flag. | |
| 157 | - $this->raw_result = false; | |
| 153 | + if ( $this->raw_result ) { | |
| 154 | + if ( ! ( $value = get_metadata( $this->meta_type, $id, $meta_key, false ) ) ) { | |
| 155 | + $value = $single ? '' : array(); | |
| 156 | + } | |
| 158 | 157 | |
| 159 | - } else { | |
| 160 | - $value = get_metadata( $this->meta_type, $id, $meta_key, $single ); | |
| 161 | - } | |
| 158 | + // Reset flag. | |
| 159 | + $this->raw_result = false; | |
| 162 | 160 | |
| 163 | - $this->is_filter_callback = false; | |
| 161 | + } else { | |
| 162 | + $value = get_metadata( $this->meta_type, $id, $meta_key, $single ); | |
| 163 | + } | |
| 164 | 164 | |
| 165 | - return $value; | |
| 165 | + $this->is_filter_callback = false; | |
| 166 | + | |
| 167 | + return $value; | |
| 168 | + } finally { | |
| 169 | + $this->restoreMetaTableName( $previousMetaTableName ); | |
| 170 | + } | |
| 166 | 171 | } |
| 167 | 172 | |
| 168 | 173 | |
| 169 | 174 | /** |
| @@ -181,26 +186,32 @@ | ||
| 181 | 186 | * |
| 182 | 187 | * @return int|bool False for failure. True for success. |
| 183 | 188 | */ |
| 184 | 189 | public function add_meta( $id, $meta_key, $meta_value, $unique = false ) { |
| 185 | - if ( $this->is_filter_callback ) { | |
| 186 | - $id = $this->sanitize_id( $id ); | |
| 190 | + $previousMetaTableName = $this->setMetaTableName(); | |
| 187 | 191 | |
| 188 | - // Bailout. | |
| 189 | - if ( ! $this->is_valid_post_type( $id ) ) { | |
| 190 | - return $this->check; | |
| 192 | + try { | |
| 193 | + if ( $this->is_filter_callback ) { | |
| 194 | + $id = $this->sanitize_id( $id ); | |
| 195 | + | |
| 196 | + // Bailout. | |
| 197 | + if ( ! $this->is_valid_post_type( $id ) ) { | |
| 198 | + return $this->check; | |
| 199 | + } | |
| 191 | 200 | } |
| 192 | - } | |
| 193 | 201 | |
| 194 | - $meta_id = add_metadata( $this->meta_type, $id, $meta_key, $meta_value, $unique ); | |
| 202 | + $meta_id = add_metadata( $this->meta_type, $id, $meta_key, $meta_value, $unique ); | |
| 195 | 203 | |
| 196 | - if ( $meta_id ) { | |
| 197 | - $this->delete_cache( $id ); | |
| 198 | - } | |
| 204 | + if ( $meta_id ) { | |
| 205 | + $this->delete_cache( $id ); | |
| 206 | + } | |
| 199 | 207 | |
| 200 | - $this->is_filter_callback = false; | |
| 208 | + $this->is_filter_callback = false; | |
| 201 | 209 | |
| 202 | - return $meta_id; | |
| 210 | + return $meta_id; | |
| 211 | + } finally { | |
| 212 | + $this->restoreMetaTableName( $previousMetaTableName ); | |
| 213 | + } | |
| 203 | 214 | } |
| 204 | 215 | |
| 205 | 216 | /** |
| 206 | 217 | * Update payment meta field based on Post Type ID. |
| @@ -222,26 +233,32 @@ | ||
| 222 | 233 | * |
| 223 | 234 | * @return int|bool False on failure, true if success. |
| 224 | 235 | */ |
| 225 | 236 | public function update_meta( $id, $meta_key, $meta_value, $prev_value = '' ) { |
| 226 | - if ( $this->is_filter_callback ) { | |
| 227 | - $id = $this->sanitize_id( $id ); | |
| 237 | + $previousMetaTableName = $this->setMetaTableName(); | |
| 228 | 238 | |
| 229 | - // Bailout. | |
| 230 | - if ( ! $this->is_valid_post_type( $id ) ) { | |
| 231 | - return $this->check; | |
| 239 | + try { | |
| 240 | + if ( $this->is_filter_callback ) { | |
| 241 | + $id = $this->sanitize_id( $id ); | |
| 242 | + | |
| 243 | + // Bailout. | |
| 244 | + if ( ! $this->is_valid_post_type( $id ) ) { | |
| 245 | + return $this->check; | |
| 246 | + } | |
| 232 | 247 | } |
| 233 | - } | |
| 234 | 248 | |
| 235 | - $meta_id = update_metadata( $this->meta_type, $id, $meta_key, $meta_value, $prev_value ); | |
| 249 | + $meta_id = update_metadata( $this->meta_type, $id, $meta_key, $meta_value, $prev_value ); | |
| 236 | 250 | |
| 237 | - if ( $meta_id ) { | |
| 238 | - $this->delete_cache( $id ); | |
| 239 | - } | |
| 251 | + if ( $meta_id ) { | |
| 252 | + $this->delete_cache( $id ); | |
| 253 | + } | |
| 240 | 254 | |
| 241 | - $this->is_filter_callback = false; | |
| 255 | + $this->is_filter_callback = false; | |
| 242 | 256 | |
| 243 | - return $meta_id; | |
| 257 | + return $meta_id; | |
| 258 | + } finally { | |
| 259 | + $this->restoreMetaTableName( $previousMetaTableName ); | |
| 260 | + } | |
| 244 | 261 | } |
| 245 | 262 | |
| 246 | 263 | /** |
| 247 | 264 | * Remove metadata matching criteria from a payment. |
| @@ -260,26 +277,32 @@ | ||
| 260 | 277 | * |
| 261 | 278 | * @return bool False for failure. True for success. |
| 262 | 279 | */ |
| 263 | 280 | public function delete_meta( $id = 0, $meta_key = '', $meta_value = '', $delete_all = '' ) { |
| 264 | - if ( $this->is_filter_callback ) { | |
| 265 | - $id = $this->sanitize_id( $id ); | |
| 281 | + $previousMetaTableName = $this->setMetaTableName(); | |
| 266 | 282 | |
| 267 | - // Bailout. | |
| 268 | - if ( ! $this->is_valid_post_type( $id ) ) { | |
| 269 | - return $this->check; | |
| 283 | + try { | |
| 284 | + if ( $this->is_filter_callback ) { | |
| 285 | + $id = $this->sanitize_id( $id ); | |
| 286 | + | |
| 287 | + // Bailout. | |
| 288 | + if ( ! $this->is_valid_post_type( $id ) ) { | |
| 289 | + return $this->check; | |
| 290 | + } | |
| 270 | 291 | } |
| 271 | - } | |
| 272 | 292 | |
| 273 | - $is_meta_deleted = delete_metadata( $this->meta_type, $id, $meta_key, $meta_value, $delete_all ); | |
| 293 | + $is_meta_deleted = delete_metadata( $this->meta_type, $id, $meta_key, $meta_value, $delete_all ); | |
| 274 | 294 | |
| 275 | - if ( $is_meta_deleted ) { | |
| 276 | - $this->delete_cache( $id ); | |
| 277 | - } | |
| 295 | + if ( $is_meta_deleted ) { | |
| 296 | + $this->delete_cache( $id ); | |
| 297 | + } | |
| 278 | 298 | |
| 279 | - $this->is_filter_callback = false; | |
| 299 | + $this->is_filter_callback = false; | |
| 280 | 300 | |
| 281 | - return $is_meta_deleted; | |
| 301 | + return $is_meta_deleted; | |
| 302 | + } finally { | |
| 303 | + $this->restoreMetaTableName( $previousMetaTableName ); | |
| 304 | + } | |
| 282 | 305 | } |
| 283 | 306 | |
| 284 | 307 | /** |
| 285 | 308 | * Rename query clauses of every query for new meta table |
| @@ -585,5 +608,45 @@ | ||
| 585 | 608 | } |
| 586 | 609 | |
| 587 | 610 | return $status; |
| 588 | 611 | } |
| 612 | + | |
| 613 | + /** | |
| 614 | + * Temporarily re-assert the meta table name in the global $wpdb object. | |
| 615 | + * | |
| 616 | + * Other plugins can overwrite the global meta table property. For example, both GiveWP and | |
| 617 | + * Charitable register a "donor" meta type, and Charitable re-assigns $wpdb->donormeta to its | |
| 618 | + * own table after GiveWP loads. Point the global at GiveWP's own table before each metadata | |
| 619 | + * operation so data is always written to and read from GiveWP's table, then restore the | |
| 620 | + * previous value afterwards so other plugins are unaffected. | |
| 621 | + * | |
| 622 | + * @since 4.16.8 | |
| 623 | + * | |
| 624 | + * @return string The previous meta table name to restore. | |
| 625 | + */ | |
| 626 | + private function setMetaTableName() { | |
| 627 | + global $wpdb; | |
| 628 | + | |
| 629 | + $metaTableProperty = $this->meta_type . 'meta'; | |
| 630 | + $previousMetaTableName = $wpdb->{$metaTableProperty}; | |
| 631 | + | |
| 632 | + $wpdb->{$metaTableProperty} = $this->table_name; | |
| 633 | + | |
| 634 | + return $previousMetaTableName; | |
| 635 | + } | |
| 636 | + | |
| 637 | + /** | |
| 638 | + * Restore a previously saved meta table name. | |
| 639 | + * | |
| 640 | + * @since 4.16.8 | |
| 641 | + * | |
| 642 | + * @param string $metaTableName The meta table name to restore. | |
| 643 | + * | |
| 644 | + * @return void | |
| 645 | + */ | |
| 646 | + private function restoreMetaTableName( $metaTableName ) { | |
| 647 | + global $wpdb; | |
| 648 | + | |
| 649 | + $wpdb->{$this->meta_type . 'meta'} = $metaTableName; | |
| 650 | + } | |
| 651 | + | |
| 589 | 652 | } |