Skip to content

Commit 9c71958

Browse files
Merge pull request #723 from godaddy-wordpress/mwc-17547/get-wc-object-meta-value-helper
Add helper method to get WooCommerce object meta value
2 parents 5ab7889 + 7ae81de commit 9c71958

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

tests/unit/HelperTest.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace SkyVerge\WooCommerce\PluginFramework\v5_15_4\Tests\Unit;
44

5+
use Generator;
6+
use Mockery;
57
use ReflectionException;
68
use SkyVerge\WooCommerce\PluginFramework\v5_15_4\SV_WC_Helper;
79
use SkyVerge\WooCommerce\PluginFramework\v5_15_4\SV_WC_Plugin_Compatibility;
810
use SkyVerge\WooCommerce\PluginFramework\v5_15_4\Tests\TestCase;
11+
use WC_Data;
12+
use WP_Mock;
913

1014
class HelperTest extends TestCase
1115
{
@@ -51,4 +55,107 @@ public function testAlwaysDetermineNavigationFeaturedDisabled() : void
5155

5256
$this->assertFalse(SV_WC_Helper::is_wc_navigation_enabled());
5357
}
58+
59+
/**
60+
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_4\SV_WC_Helper::getWooCommerceObjectMetaValue()
61+
*
62+
* @throws ReflectionException
63+
*/
64+
public function testCanGetWooCommerceDataObjectMetaValue() : void
65+
{
66+
$object = Mockery::mock('WC_Data');
67+
68+
$this->mockStaticMethod(SV_WC_Helper::class, 'getPostOrObjectMetaCompat')
69+
->once()
70+
->with(null, $object, $field = 'TEST_FIELD', true)
71+
->andReturn($value = 'WC_DATA_META_VALUE');
72+
73+
$this->assertSame($value, SV_WC_Helper::getWooCommerceObjectMetaValue($object, $field));
74+
}
75+
76+
/**
77+
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_4\SV_WC_Helper::getWooCommerceObjectMetaValue()
78+
*
79+
* @throws ReflectionException
80+
*/
81+
public function testCanGetWordPressPostMetaValue() : void
82+
{
83+
$object = Mockery::mock('WP_Post');
84+
85+
$this->mockStaticMethod(SV_WC_Helper::class, 'getPostOrObjectMetaCompat')
86+
->once()
87+
->with($object, null, $field = 'TEST_FIELD', true)
88+
->andReturn($value = 'WP_POST_META_VALUE');
89+
90+
$this->assertSame($value, SV_WC_Helper::getWooCommerceObjectMetaValue($object, $field));
91+
}
92+
93+
/**
94+
* @dataProvider providerCanGetPostOrObjectMetaCompat()
95+
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_4\SV_WC_Helper::getPostOrObjectMetaCompat()
96+
*
97+
* @param bool $hasData
98+
* @param bool $hasPostId
99+
* @param mixed $expected
100+
*
101+
* @throws ReflectionException
102+
*/
103+
public function testCanGetPostOrObjectMetaCompat(
104+
bool $hasData,
105+
bool $hasPostId,
106+
$expected
107+
) : void
108+
{
109+
$key = 'test';
110+
$object = null;
111+
112+
$post = Mockery::mock('WP_Post');
113+
114+
if ($hasPostId) {
115+
$post->ID = 123;
116+
}
117+
118+
if ($hasData) {
119+
$object = Mockery::mock('WC_Data');
120+
121+
$object->expects('get_meta')
122+
->times((int) ($hasData))
123+
->with($key, true)
124+
->andReturn('WC_DATA_META_VALUE');
125+
}
126+
127+
WP_Mock::userFunction('get_post_meta')
128+
->times((int) (! $hasData && $hasPostId))
129+
->with(123, $key, true)
130+
->andReturn('WP_POST_META_VALUE');
131+
132+
$this->assertSame($expected, SV_WC_Helper::getPostOrObjectMetaCompat($post, $object, $key, true));
133+
}
134+
135+
/** @see testCanGetPostOrObjectMetaCompat() */
136+
public function providerCanGetPostOrObjectMetaCompat() : Generator
137+
{
138+
yield 'data is set, method does not exist' => [
139+
'hasData' => true,
140+
'hasPostId' => false,
141+
'expected' => 'WC_DATA_META_VALUE',
142+
];
143+
144+
/*
145+
* Note: It seems there's no sane way to test the get$key() method
146+
* on a WC_Data mock, thus no test case for 'data is set, method exists'.
147+
*/
148+
149+
yield 'data not set, no post ID' => [
150+
'hasData' => false,
151+
'hasPostId' => false,
152+
'expected' => false,
153+
];
154+
155+
yield 'data not set, has post ID' => [
156+
'hasData' => false,
157+
'hasPostId' => true,
158+
'expected' => 'WP_POST_META_VALUE',
159+
];
160+
}
54161
}

woocommerce/class-sv-wc-helper.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
namespace SkyVerge\WooCommerce\PluginFramework\v5_15_4;
2626

2727
use SkyVerge\WooCommerce\PluginFramework\v5_15_4\Helpers\NumberHelper;
28+
use WC_Data;
29+
use WP_Post;
2830

2931
defined( 'ABSPATH' ) or exit;
3032

@@ -1197,6 +1199,50 @@ public static function get_escaped_id_list( array $ids ) {
11971199
}
11981200

11991201

1202+
/**
1203+
* Gets value of a meta key from WooCommerce object based on its data type.
1204+
*
1205+
* @param WP_Post|WC_Data $object
1206+
* @param string $field
1207+
* @param bool $single
1208+
*
1209+
* @return array|mixed|string|null
1210+
*/
1211+
public static function getWooCommerceObjectMetaValue($object, string $field, bool $single = true)
1212+
{
1213+
if ($object instanceof WP_Post) {
1214+
return static::getPostOrObjectMetaCompat($object, null, $field, $single);
1215+
}
1216+
1217+
if ($object instanceof WC_Data) {
1218+
return static::getPostOrObjectMetaCompat(null, $object, $field, $single);
1219+
}
1220+
1221+
return null;
1222+
}
1223+
1224+
/**
1225+
* Adds local compatibility for Woo's internal OrderUtil::get_post_or_object_meta() method.
1226+
*
1227+
* @param WP_Post|null $post
1228+
* @param WC_Data|null $data
1229+
* @param string $key
1230+
* @param bool $single
1231+
*
1232+
* @return array|false|mixed|string
1233+
*/
1234+
public static function getPostOrObjectMetaCompat(?\WP_Post $post, ?\WC_Data $data, string $key, bool $single)
1235+
{
1236+
if (isset($data)) {
1237+
if (method_exists($data, "get$key")) {
1238+
return $data->{"get$key"}();
1239+
}
1240+
return $data->get_meta($key, $single);
1241+
} else {
1242+
return isset($post->ID) ? get_post_meta($post->ID, $key, $single) : false;
1243+
}
1244+
}
1245+
12001246
}
12011247

12021248

0 commit comments

Comments
 (0)