Skip to content

Commit 42ee0de

Browse files
committed
update
1 parent 03f0c07 commit 42ee0de

File tree

5 files changed

+49
-45
lines changed

5 files changed

+49
-45
lines changed

src/AdminMenus/AbstractAdminMenu.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@
1010

1111
namespace EightshiftLibs\AdminMenus;
1212

13-
use EightshiftLibs\Helpers\Helpers;
1413
use EightshiftLibs\Services\ServiceInterface;
15-
use EightshiftLibs\Blocks\RenderableBlockInterface;
1614
use Exception;
1715

1816
/**
1917
* Abstract class AbstractAdminMenu class.
2018
*
2119
* Class responsible for creating admin menus, separately from CPT admin menus.
2220
*/
23-
abstract class AbstractAdminMenu implements ServiceInterface, RenderableBlockInterface
21+
abstract class AbstractAdminMenu implements ServiceInterface
2422
{
2523
/**
2624
* Register all the hooks
@@ -78,29 +76,7 @@ public function processAdminMenu($attr): void
7876
$attr['adminMenuSlug'] = $this->getMenuSlug();
7977
$attr['nonceField'] = $this->renderNonce();
8078

81-
echo $this->render((array)$attr); // phpcs:ignore
82-
}
83-
84-
/**
85-
* Render the current view.
86-
*
87-
* @param array<string, mixed> $attributes Array of attributes passed to the view.
88-
* @param string $innerBlockContent Not used here.
89-
*
90-
* @return string Rendered HTML.
91-
* @throws Exception On missing attributes OR missing template.
92-
*/
93-
public function render(array $attributes = [], string $innerBlockContent = ''): string
94-
{
95-
try {
96-
return Helpers::render($this->getViewComponent(), $attributes);
97-
} catch (Exception $exception) { // To do: once new libs are released, replace with ComponentException.
98-
// Don't let exceptions bubble up. Just render the exception message into the admin menu.
99-
return \sprintf(
100-
'<pre>%s</pre>',
101-
$exception->getMessage()
102-
);
103-
}
79+
echo $this->getViewComponent($attr); // phpcs:ignore
10480
}
10581

10682
/**

src/AdminMenus/AbstractAdminSubMenu.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,36 @@ abstract class AbstractAdminSubMenu extends AbstractAdminMenu
2727
*/
2828
public function register(): void
2929
{
30-
\add_action(
31-
'admin_menu',
32-
function () {
33-
\add_submenu_page(
34-
$this->getParentMenu(),
35-
$this->getTitle(),
36-
$this->getMenuTitle(),
37-
$this->getCapability(),
38-
$this->getMenuSlug(),
39-
[$this, 'processAdminSubmenu']
40-
);
41-
}
30+
\add_action('admin_menu', [$this, 'callback'], $this->getPriorityOrder());
31+
}
32+
33+
/**
34+
* Return action callback method.
35+
*
36+
* @return void
37+
*/
38+
public function callback(): void
39+
{
40+
\add_submenu_page(
41+
$this->getParentMenu(),
42+
$this->getTitle(),
43+
$this->getMenuTitle(),
44+
$this->getCapability(),
45+
$this->getMenuSlug(),
46+
[$this, 'processAdminSubmenu']
4247
);
4348
}
4449

50+
/**
51+
* Return hook priority order.
52+
*
53+
* @return integer
54+
*/
55+
public function getPriorityOrder(): int
56+
{
57+
return 200;
58+
}
59+
4560
/**
4661
* Process the admin submenu attributes and prepare rendering.
4762
*
@@ -60,7 +75,7 @@ public function processAdminSubmenu($attr): void
6075
$attr['adminMenuSlug'] = $this->getMenuSlug();
6176
$attr['nonceField'] = $this->renderNonce();
6277

63-
echo $this->render((array)$attr); // phpcs:ignore
78+
echo $this->getViewComponent($attr); // phpcs:ignore
6479
}
6580

6681
/**

src/AdminMenus/AdminMenuExample.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace %g_namespace%\AdminMenus;
1212

1313
use %g_use_libs%\AdminMenus\AbstractAdminMenu;
14+
use %g_use_libs%\Helpers\Helpers;
1415

1516
/**
1617
* AdminMenuExample class.
@@ -126,11 +127,16 @@ protected function getPosition(): int
126127
/**
127128
* Get the view component that will render correct view.
128129
*
130+
* @param array<string, mixed> $attributes Attributes passed to the view.
131+
*
129132
* @return string View uri.
130133
*/
131-
protected function getViewComponent(): string
134+
protected function getViewComponent(array $attributes): string
132135
{
133-
return '%view_component%';
136+
return Helpers::render(
137+
'%view_component%',
138+
$attributes
139+
);
134140
}
135141

136142
/**

src/AdminMenus/AdminSubMenuExample.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace %g_namespace%\AdminMenus;
1212

1313
use %g_use_libs%\AdminMenus\AbstractAdminSubMenu;
14+
use %g_use_libs%\Helpers\Helpers;
1415

1516
/**
1617
* AdminSubMenuExample class.
@@ -93,11 +94,16 @@ protected function getParentMenu(): string
9394
/**
9495
* Get the view component that will render correct view.
9596
*
97+
* @param array<string, mixed> $attributes Attributes passed to the view.
98+
*
9699
* @return string View uri.
97100
*/
98-
protected function getViewComponent(): string
101+
protected function getViewComponent(array $attributes): string
99102
{
100-
return '%view_component%';
103+
return Helpers::render(
104+
'%view_component%',
105+
$attributes
106+
);
101107
}
102108

103109
/**

src/Geolocation/GeolocationExample.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace %g_namespace%\Geolocation;
1212

1313
use %g_use_libs%\Geolocation\AbstractGeolocation;
14+
use %g_use_libs%\Helpers\Helpers;
1415

1516
/**
1617
* Class Geolocation
@@ -42,7 +43,7 @@ public function getGeolocationCookieName(): string
4243
*/
4344
public function getGeolocationPharLocation(): string
4445
{
45-
return __DIR__ . \DIRECTORY_SEPARATOR . 'geoip2.phar';
46+
return Helpers::getEightshiftOutputPath('geoip2.phar');
4647
}
4748

4849
/**
@@ -52,6 +53,6 @@ public function getGeolocationPharLocation(): string
5253
*/
5354
public function getGeolocationDbLocation(): string
5455
{
55-
return __DIR__ . \DIRECTORY_SEPARATOR . 'GeoLite2-Country.mmdb';
56+
return Helpers::getEightshiftOutputPath('GeoLite2-Country.mmdb');
5657
}
5758
}

0 commit comments

Comments
 (0)