Skip to content

Commit 6fe561f

Browse files
Merge pull request #736 from godaddy-wordpress/feature/page-helper
Add a `PageHelper` class to check for current admin page
2 parents 48080d5 + 1c35f14 commit 6fe561f

File tree

5 files changed

+256
-1
lines changed

5 files changed

+256
-1
lines changed

tests/unit/Helpers/ArrayHelperTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,92 @@ public function providerCanDetermineKeyExists(): Generator
251251
];
252252
}
253253

254+
/**
255+
* Tests that the helper will return default with key not found.
256+
*
257+
* @covers ::get()
258+
*/
259+
public function testCanReturnDefaultWhenGetArrayValueByKeyNotFound() : void
260+
{
261+
$this->assertNull(ArrayHelper::get([], 'key'), 'ArrayHelper::get() does not return null by default as expected');
262+
$this->assertEquals('myDefault', ArrayHelper::get([], 'key', 'myDefault'));
263+
$this->assertNotEquals('myDefault', ArrayHelper::get(['key' => 'value'], 'key', 'myDefault'));
264+
}
265+
266+
/**
267+
* Tests that can retrieve array value by key without PHP error/warning.
268+
*
269+
* @covers ::get()
270+
* @dataProvider providerCanGetArrayValueByKey
271+
*
272+
* @param mixed $array
273+
* @param int|string $key
274+
* @param mixed $expected
275+
* @return void
276+
*/
277+
public function testCanGetArrayValueByKey($array, $key, $expected) : void
278+
{
279+
$this->assertSame($expected, ArrayHelper::get($array, $key));
280+
}
281+
282+
/** @see testCanGetArrayValueByKey() */
283+
public function providerCanGetArrayValueByKey() : Generator
284+
{
285+
yield 'Existing string key in a key/value array' => [
286+
'array' => ['key' => 'found'],
287+
'key' => 'key',
288+
'expected' => 'found',
289+
];
290+
291+
yield 'Nonexistent string key in a key/value array' => [
292+
'array' => ['key' => 'notfound'],
293+
'key' => 'value',
294+
'expected' => null,
295+
];
296+
297+
yield 'Existing nested key with dot notation' => [
298+
'array' => ['key' => ['nested' => ['deeply' => 'found']]],
299+
'key' => 'key.nested.deeply',
300+
'expected' => 'found',
301+
];
302+
303+
yield 'Nonexistent nested key with dot notation' => [
304+
'array' => ['key' => ['nested' => ['deeply' => 'notfound']]],
305+
'key' => 'key.nested.more.deeply',
306+
'expected' => null,
307+
];
308+
309+
yield 'Existing dot-notated key as key is returned without iteration' => [
310+
'array' => ['dot.notated.key' => 'found'],
311+
'key' => 'dot.notated.key',
312+
'expected' => 'found',
313+
];
314+
315+
yield 'Existing numeric index' => [
316+
'array' => ['foo', 'bar', 'baz'],
317+
'key' => 1,
318+
'expected' => 'bar',
319+
];
320+
321+
yield 'Nonexistent numeric index' => [
322+
'array' => ['foo', 'bar', 'baz'],
323+
'key' => 3,
324+
'expected' => null,
325+
];
326+
327+
yield 'Existing numeric string index' => [
328+
'array' => ['foo', 'bar', 'baz'],
329+
'key' => '2',
330+
'expected' => 'baz',
331+
];
332+
333+
yield 'Nonexistent numeric string index' => [
334+
'array' => ['foo', 'bar', 'baz'],
335+
'key' => '3',
336+
'expected' => null,
337+
];
338+
}
339+
254340
protected function getArrayAccessObject(): ArrayAccess
255341
{
256342
return new class implements ArrayAccess
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace SkyVerge\WooCommerce\PluginFramework\v5_15_4\Tests\Unit\Helpers;
4+
5+
use Generator;
6+
use Mockery;
7+
use SkyVerge\WooCommerce\PluginFramework\v5_15_4\Helpers\PageHelper;
8+
use SkyVerge\WooCommerce\PluginFramework\v5_15_4\Tests\TestCase;
9+
10+
/**
11+
* @coversDefaultClass \SkyVerge\WooCommerce\PluginFramework\v5_15_4\Helpers\PageHelper
12+
*/
13+
final class PageHelperTest extends TestCase
14+
{
15+
/**
16+
* @covers ::isWooCommerceAnalyticsPage()
17+
* @dataProvider providerCanDetermineIsWooCommerceAnalyticsPage
18+
* @throws \ReflectionException
19+
*/
20+
public function testCanDetermineIsWooCommerceAnalyticsPage($pageData, bool $expected) : void
21+
{
22+
$pageController = Mockery::mock(\Automattic\WooCommerce\Admin\PageController::class);
23+
$pageController->expects('get_current_page')
24+
->once()
25+
->andReturn($pageData);
26+
27+
$this->mockStaticMethod(PageHelper::class, 'getWooCommercePageController')
28+
->once()
29+
->andReturn($pageController);
30+
31+
$this->assertSame($expected, PageHelper::isWooCommerceAnalyticsPage());
32+
}
33+
34+
/** @see testCanDetermineIsWooCommerceAnalyticsPage */
35+
public function providerCanDetermineIsWooCommerceAnalyticsPage() : Generator
36+
{
37+
yield 'no page data' => [
38+
'pageData' => false,
39+
'expected' => false,
40+
];
41+
42+
yield 'woocommerce home' => [
43+
'pageData' => [
44+
'id' => 'woocommerce-home',
45+
'parent' => 'woocommerce',
46+
],
47+
'expected' => false,
48+
];
49+
50+
yield 'orders page' => [
51+
'pageData' => [
52+
'id' => 'woocommerce-custom-orders',
53+
],
54+
'expected' => false,
55+
];
56+
57+
yield 'analytics overview' => [
58+
'pageData' => [
59+
'id' => 'woocommerce-analytics',
60+
'parent' => null,
61+
],
62+
'expected' => true,
63+
];
64+
65+
yield 'analytics revenue' => [
66+
'pageData' => [
67+
'id' => 'woocommerce-analytics-revenue',
68+
'parent' => 'woocommerce-analytics',
69+
],
70+
'expected' => true,
71+
];
72+
73+
yield 'analytics products' => [
74+
'pageData' => [
75+
'id' => 'woocommerce-analytics-products',
76+
'parent' => 'woocommerce-analytics',
77+
],
78+
'expected' => true,
79+
];
80+
}
81+
}

woocommerce/Helpers/ArrayHelper.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,33 @@ public static function exists($array, $key) : bool
123123

124124
return array_key_exists($key, self::wrap($array));
125125
}
126+
127+
/**
128+
* Gets an array value from a dot notated key.
129+
*
130+
* @param mixed $array
131+
* @param int|string $key
132+
* @param mixed $default
133+
* @return mixed
134+
*/
135+
public static function get($array, $key, $default = null)
136+
{
137+
if (! self::accessible($array)) {
138+
return $default;
139+
}
140+
141+
if (self::exists($array, $key)) {
142+
return $array[$key];
143+
}
144+
145+
foreach (explode('.', (string) $key) as $segment) {
146+
if (! self::exists($array, $segment)) {
147+
return $default;
148+
}
149+
150+
$array = $array[$segment];
151+
}
152+
153+
return $array;
154+
}
126155
}

woocommerce/Helpers/PageHelper.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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
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 [email protected] so we can send you a copy immediately.
12+
*
13+
* DISCLAIMER
14+
*
15+
* Do not edit or add to this file if you wish to upgrade the plugin to newer
16+
* versions in the future. If you wish to customize the plugin for your
17+
* needs please refer to http://www.skyverge.com
18+
*
19+
* @package SkyVerge/WooCommerce/Plugin/Classes
20+
* @author SkyVerge
21+
* @copyright Copyright (c) 2013-2025, SkyVerge, Inc.
22+
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
23+
*/
24+
25+
namespace SkyVerge\WooCommerce\PluginFramework\v5_15_4\Helpers;
26+
27+
class PageHelper
28+
{
29+
/**
30+
* Determines whether the current page is the WooCommerce "Analytics" page.
31+
*
32+
* @since 5.15.4
33+
*/
34+
public static function isWooCommerceAnalyticsPage() : bool
35+
{
36+
if (! $controller = static::getWooCommercePageController()) {
37+
return false;
38+
}
39+
40+
$pageData = $controller->get_current_page();
41+
42+
return ArrayHelper::get($pageData, 'id') === 'woocommerce-analytics' ||
43+
ArrayHelper::get($pageData, 'parent') === 'woocommerce-analytics';
44+
}
45+
46+
/**
47+
* @codeCoverageIgnore
48+
*/
49+
protected static function getWooCommercePageController() : ?\Automattic\WooCommerce\Admin\PageController
50+
{
51+
if (! class_exists(\Automattic\WooCommerce\Admin\PageController::class)) {
52+
return null;
53+
}
54+
55+
return \Automattic\WooCommerce\Admin\PageController::get_instance();
56+
}
57+
}

woocommerce/changelog.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
*** SkyVerge WooCommerce Plugin Framework Changelog ***
22

3-
2024.nn.nn - version 5.15.4
3+
2025.nn.nn - version 5.15.4
4+
* New: Added PageHelper class to assist in determining page contexts.
5+
* New: Add a helper method to get WooCommerce object meta values.
46

57
2025.01.24 - version 5.15.3
68
* Fix - Add Merchant ID to Google Pay, distinguishing it from Gateway merchant ID

0 commit comments

Comments
 (0)