| 1 |
<?php |
| 2 |
/** |
| 3 |
* Derived from SkyVerge WooCommerce Plugin Framework https://github.com/skyverge/wc-plugin-framework/ |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WPO\WC\PostNL\Compatibility; |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) or exit; |
| 9 |
|
| 10 |
if ( ! class_exists( '\\WPO\\WC\\PostNL\\Compatibility\\Data' ) ) : |
| 11 |
|
| 12 |
/** |
| 13 |
* WooCommerce data compatibility class. |
| 14 |
* |
| 15 |
* @since 4.6.0-dev |
| 16 |
*/ |
| 17 |
abstract class Data { |
| 18 |
|
| 19 |
/** |
| 20 |
* Creates aliases for add_meta_data, update_meta_data and delete_meta_data without the _data suffix |
| 21 |
* |
| 22 |
* @param string $name static function name |
| 23 |
* @param array $arguments function arguments |
| 24 |
*/ |
| 25 |
public static function __callStatic( $name, $arguments ) { |
| 26 |
if ( substr( $name, -strlen('_meta') ) == '_meta' && method_exists( __CLASS__, $name.'_data' ) ) { |
| 27 |
call_user_func_array( array( __CLASS__, $name.'_data' ), $arguments ); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* Gets an object property. |
| 34 |
* |
| 35 |
* @since 4.6.0-dev |
| 36 |
* @param \WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 37 |
* @param string $prop the property name |
| 38 |
* @param string $context if 'view' then the value will be filtered |
| 39 |
* @param array $compat_props Compatibility properties. |
| 40 |
* @return mixed |
| 41 |
*/ |
| 42 |
public static function get_prop( $object, $prop, $context = 'edit', $compat_props = array() ) { |
| 43 |
|
| 44 |
$value = ''; |
| 45 |
|
| 46 |
if ( WC_Core::is_wc_version_gte_3_0() ) { |
| 47 |
|
| 48 |
if ( is_callable( array( $object, "get_{$prop}" ) ) ) { |
| 49 |
$value = $object->{"get_{$prop}"}( $context ); |
| 50 |
} |
| 51 |
|
| 52 |
} else { |
| 53 |
|
| 54 |
// backport the property name |
| 55 |
if ( isset( $compat_props[ $prop ] ) ) { |
| 56 |
$prop = $compat_props[ $prop ]; |
| 57 |
} |
| 58 |
|
| 59 |
// if this is the 'view' context and there is an accessor method, use it |
| 60 |
if ( is_callable( array( $object, "get_{$prop}" ) ) && 'view' === $context ) { |
| 61 |
$value = $object->{"get_{$prop}"}(); |
| 62 |
} else { |
| 63 |
$value = $object->$prop; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return $value; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* Sets an object's properties. |
| 73 |
* |
| 74 |
* Note that this does not save any data to the database. |
| 75 |
* |
| 76 |
* @since 4.6.0-dev |
| 77 |
* @param \WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 78 |
* @param array $props the new properties as $key => $value |
| 79 |
* @param array $compat_props Compatibility properties. |
| 80 |
* @return \WC_Data |
| 81 |
*/ |
| 82 |
public static function set_props( $object, $props, $compat_props = array() ) { |
| 83 |
|
| 84 |
if ( WC_Core::is_wc_version_gte_3_0() ) { |
| 85 |
|
| 86 |
$object->set_props( $props ); |
| 87 |
|
| 88 |
} else { |
| 89 |
|
| 90 |
foreach ( $props as $prop => $value ) { |
| 91 |
|
| 92 |
if ( isset( $compat_props[ $prop ] ) ) { |
| 93 |
$prop = $compat_props[ $prop ]; |
| 94 |
} |
| 95 |
|
| 96 |
$object->$prop = $value; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return $object; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Gets an object's stored meta value. |
| 106 |
* |
| 107 |
* @since 4.6.0-dev |
| 108 |
* @param \WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 109 |
* @param string $key the meta key |
| 110 |
* @param bool $single whether to get the meta as a single item. Defaults to `true` |
| 111 |
* @param string $context if 'view' then the value will be filtered |
| 112 |
* @return mixed |
| 113 |
*/ |
| 114 |
public static function get_meta( $object, $key = '', $single = true, $context = 'edit' ) { |
| 115 |
|
| 116 |
if ( WC_Core::is_wc_version_gte_3_0() ) { |
| 117 |
$value = $object->get_meta( $key, $single, $context ); |
| 118 |
} else { |
| 119 |
$object_id = is_callable( array( $object, 'get_id' ) ) ? $object->get_id() : $object->id; |
| 120 |
$value = get_post_meta( $object_id, $key, $single ); |
| 121 |
} |
| 122 |
|
| 123 |
return $value; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Stores an object meta value. |
| 129 |
* |
| 130 |
* @since 4.6.0-dev |
| 131 |
* @param \WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 132 |
* @param string $key the meta key |
| 133 |
* @param string $value the meta value |
| 134 |
* @param string $meta_id Optional. The specific meta ID to update |
| 135 |
* @param bool $unique Optional. Whether the meta should be unique. |
| 136 |
*/ |
| 137 |
public static function add_meta_data( $object, $key, $value, $unique = false ) { |
| 138 |
|
| 139 |
if ( WC_Core::is_wc_version_gte_3_0() ) { |
| 140 |
|
| 141 |
$object->add_meta_data( $key, $value, $unique ); |
| 142 |
|
| 143 |
$object->save_meta_data(); |
| 144 |
|
| 145 |
} else { |
| 146 |
|
| 147 |
$object_id = is_callable( array( $object, 'get_id' ) ) ? $object->get_id() : $object->id; |
| 148 |
add_post_meta( $object_id, $key, $value, $unique ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
/** |
| 154 |
* Updates an object's stored meta value. |
| 155 |
* |
| 156 |
* @since 4.6.0-dev |
| 157 |
* @param \WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 158 |
* @param string $key the meta key |
| 159 |
* @param string $value the meta value |
| 160 |
* @param int|string $meta_id Optional. The specific meta ID to update |
| 161 |
*/ |
| 162 |
public static function update_meta_data( $object, $key, $value, $meta_id = '' ) { |
| 163 |
|
| 164 |
if ( WC_Core::is_wc_version_gte_3_0() ) { |
| 165 |
|
| 166 |
$object->update_meta_data( $key, $value, $meta_id ); |
| 167 |
|
| 168 |
$object->save_meta_data(); |
| 169 |
|
| 170 |
} else { |
| 171 |
|
| 172 |
$object_id = is_callable( array( $object, 'get_id' ) ) ? $object->get_id() : $object->id; |
| 173 |
update_post_meta( $object_id, $key, $value ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Deletes an object's stored meta value. |
| 180 |
* |
| 181 |
* @since 4.6.0-dev |
| 182 |
* @param \WC_Data $object the data object, likely \WC_Order or \WC_Product |
| 183 |
* @param string $key the meta key |
| 184 |
*/ |
| 185 |
public static function delete_meta_data( $object, $key ) { |
| 186 |
|
| 187 |
if ( WC_Core::is_wc_version_gte_3_0() ) { |
| 188 |
|
| 189 |
$object->delete_meta_data( $key ); |
| 190 |
|
| 191 |
$object->save_meta_data(); |
| 192 |
|
| 193 |
} else { |
| 194 |
|
| 195 |
$object_id = is_callable( array( $object, 'get_id' ) ) ? $object->get_id() : $object->id; |
| 196 |
delete_post_meta( $object_id, $key ); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
endif; // Class exists check |
| 205 |
|