| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Site Navigation Controller |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Agent\Controllers; |
| 8 |
|
| 9 |
/** |
| 10 |
* Site Navigation Controller |
| 11 |
*/ |
| 12 |
class SiteNavigationController |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Initialize the controller to add filters and actions. |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public static function init() |
| 20 |
{ |
| 21 |
\add_filter('render_block', function ($block_content, $block) { |
| 22 |
if ($block['blockName'] === 'core/navigation') { |
| 23 |
// Add the id of the menu to the menu itself |
| 24 |
$block_content = str_replace( |
| 25 |
'<nav', |
| 26 |
'<nav data-extendify-menu-id="' . $block['attrs']['ref'] . '"', |
| 27 |
$block_content |
| 28 |
); |
| 29 |
} |
| 30 |
return $block_content; |
| 31 |
}, 10, 2); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get a list of published navigation items from the site. |
| 36 |
* |
| 37 |
* @param \WP_REST_Request $request The REST API request |
| 38 |
* @return \WP_REST_Response. |
| 39 |
*/ |
| 40 |
public static function getSiteNavigation($request): \WP_REST_Response |
| 41 |
{ |
| 42 |
$navigation = get_posts([ |
| 43 |
'numberposts' => -1, |
| 44 |
'post_status' => 'publish', |
| 45 |
'post_type' => 'wp_navigation', |
| 46 |
'include' => $request->get_param('only') ? explode(',', $request->get_param('only')) : [] |
| 47 |
]); |
| 48 |
|
| 49 |
return new \WP_REST_Response(array_map(function ($item) { |
| 50 |
return [ |
| 51 |
"id" => $item->ID, |
| 52 |
"name" => $item->post_title, |
| 53 |
"content" => $item->post_content |
| 54 |
]; |
| 55 |
}, $navigation), 200); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Render the navigation menu content based on the provided menu ID. |
| 60 |
* |
| 61 |
* @param \WP_REST_Request $request The REST API request |
| 62 |
* @return string The rendered navigation menu content. |
| 63 |
*/ |
| 64 |
public static function renderNavigationMenu($request) |
| 65 |
{ |
| 66 |
if (!$request->get_param('content')) { |
| 67 |
return ''; |
| 68 |
} |
| 69 |
|
| 70 |
add_filter('render_block_core/navigation', function ($content, $block) { |
| 71 |
if (empty($block['attrs']['extendifyMenuId'])) { |
| 72 |
return $content; |
| 73 |
} |
| 74 |
$menu_id = $block['attrs']['extendifyMenuId']; |
| 75 |
|
| 76 |
return str_replace( |
| 77 |
'data-extendify-menu-id=""', |
| 78 |
'data-extendify-menu-id="' . esc_attr($menu_id) . '"', |
| 79 |
$content |
| 80 |
); |
| 81 |
}, 10, 2); |
| 82 |
|
| 83 |
|
| 84 |
$nav_block = [ |
| 85 |
"blockName" => "core/navigation", |
| 86 |
"attrs" => [ |
| 87 |
"icon" => "menu", |
| 88 |
"overlayBackgroundColor" => "background", |
| 89 |
"overlayTextColor" => "foreground", |
| 90 |
"extendifyMenuId" => 5, |
| 91 |
], |
| 92 |
"innerBlocks" => parse_blocks($request->get_param('content')), |
| 93 |
"innerHTML" => "", |
| 94 |
"innerContent" => [] |
| 95 |
]; |
| 96 |
|
| 97 |
return render_block($nav_block); |
| 98 |
} |
| 99 |
} |
| 100 |
|