Data_Compatibility.php
133 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Plugin Framework |
| 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 GNU General Public License v3.0 or later |
| 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@skyverge.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * @since 3.0.0 |
| 14 | * @author WooCommerce / SkyVerge |
| 15 | * @copyright Copyright (c) 2013-2019, SkyVerge, Inc. |
| 16 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later |
| 17 | * |
| 18 | * Modified by WooCommerce on 01 December 2021. |
| 19 | */ |
| 20 | |
| 21 | namespace WooCommerce\Square\Framework\Compatibility; |
| 22 | use WooCommerce\Square\Framework\Plugin_Compatibility; |
| 23 | |
| 24 | defined( 'ABSPATH' ) or exit; |
| 25 | |
| 26 | /** |
| 27 | * WooCommerce data compatibility class. |
| 28 | * |
| 29 | * @since 3.0.0 |
| 30 | */ |
| 31 | abstract class Data_Compatibility { |
| 32 | |
| 33 | /** |
| 34 | * Gets an object property. |
| 35 | * |
| 36 | * @since 3.0.0 |
| 37 | * @param \WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 38 | * @param string $prop the property name |
| 39 | * @param string $context if 'view' then the value will be filtered |
| 40 | * @param array $compat_props Compatibility properties. |
| 41 | * @return mixed |
| 42 | */ |
| 43 | public static function get_prop( $object, $prop, $context = 'edit', $compat_props = array() ) { |
| 44 | |
| 45 | $value = ''; |
| 46 | |
| 47 | if ( is_callable( array( $object, "get_{$prop}" ) ) ) { |
| 48 | $value = $object->{"get_{$prop}"}( $context ); |
| 49 | } |
| 50 | |
| 51 | return $value; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Sets an object's properties. |
| 57 | * |
| 58 | * Note that this does not save any data to the database. |
| 59 | * |
| 60 | * @since 3.0.0 |
| 61 | * @param \WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 62 | * @param array $props the new properties as $key => $value |
| 63 | * @param array $compat_props Compatibility properties. |
| 64 | * @return \WC_Data |
| 65 | */ |
| 66 | public static function set_props( $object, $props, $compat_props = array() ) { |
| 67 | $object->set_props( $props ); |
| 68 | |
| 69 | return $object; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Gets an object's stored meta value. |
| 75 | * |
| 76 | * @since 3.0.0 |
| 77 | * @param \WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 78 | * @param string $key the meta key |
| 79 | * @param bool $single whether to get the meta as a single item. Defaults to `true` |
| 80 | * @param string $context if 'view' then the value will be filtered |
| 81 | * @return mixed |
| 82 | */ |
| 83 | public static function get_meta( $object, $key = '', $single = true, $context = 'edit' ) { |
| 84 | $value = $object->get_meta( $key, $single, $context ); |
| 85 | |
| 86 | return $value; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Stores an object meta value. |
| 92 | * |
| 93 | * @since 3.0.0 |
| 94 | * @param \WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 95 | * @param string $key the meta key |
| 96 | * @param string $value the meta value |
| 97 | * @param bool $unique Optional. Whether the meta should be unique. |
| 98 | */ |
| 99 | public static function add_meta_data( $object, $key, $value, $unique = false ) { |
| 100 | $object->add_meta_data( $key, $value, $unique ); |
| 101 | $object->save_meta_data(); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * Updates an object's stored meta value. |
| 107 | * |
| 108 | * @since 3.0.0 |
| 109 | * @param \WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 110 | * @param string $key the meta key |
| 111 | * @param string $value the meta value |
| 112 | * @param int|string $meta_id Optional. The specific meta ID to update |
| 113 | */ |
| 114 | public static function update_meta_data( $object, $key, $value, $meta_id = '' ) { |
| 115 | $object->update_meta_data( $key, $value, $meta_id ); |
| 116 | $object->save_meta_data(); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * Deletes an object's stored meta value. |
| 122 | * |
| 123 | * @since 3.0.0 |
| 124 | * @param \WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 125 | * @param string $key the meta key |
| 126 | */ |
| 127 | public static function delete_meta_data( $object, $key ) { |
| 128 | $object->delete_meta_data( $key ); |
| 129 | $object->save_meta_data(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 |