| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly. |
| 5 |
} |
| 6 |
|
| 7 |
// Hook the post rendering to the block |
| 8 |
if ( function_exists( 'register_block_type' ) ) : |
| 9 |
register_block_type( |
| 10 |
'ew-block/ew-menu', |
| 11 |
array( |
| 12 |
'attributes' => array( |
| 13 |
'title' => array( |
| 14 |
'type' => 'string', |
| 15 |
'default' => 'Navigation', // No translation here |
| 16 |
), |
| 17 |
'menu' => array( |
| 18 |
'type' => 'string', |
| 19 |
'default' => '', |
| 20 |
), |
| 21 |
'container' => array( |
| 22 |
'type' => 'string', |
| 23 |
'default' => 'div', |
| 24 |
), |
| 25 |
'container_id' => array( |
| 26 |
'type' => 'string', |
| 27 |
'default' => '', |
| 28 |
), |
| 29 |
'container_class' => array( |
| 30 |
'type' => 'string', |
| 31 |
'default' => '', |
| 32 |
), |
| 33 |
'menu_id' => array( |
| 34 |
'type' => 'string', |
| 35 |
'default' => '', |
| 36 |
), |
| 37 |
'menu_class' => array( |
| 38 |
'type' => 'string', |
| 39 |
'default' => 'nav-menu', |
| 40 |
), |
| 41 |
'depth' => array( |
| 42 |
'type' => 'number', |
| 43 |
'default' => 0, |
| 44 |
), |
| 45 |
'before' => array( |
| 46 |
'type' => 'string', |
| 47 |
'default' => '', |
| 48 |
), |
| 49 |
'after' => array( |
| 50 |
'type' => 'string', |
| 51 |
'default' => '', |
| 52 |
), |
| 53 |
'link_before' => array( |
| 54 |
'type' => 'string', |
| 55 |
'default' => '', |
| 56 |
), |
| 57 |
'link_after' => array( |
| 58 |
'type' => 'string', |
| 59 |
'default' => '', |
| 60 |
), |
| 61 |
'fallback_cb' => array( |
| 62 |
'type' => 'string', |
| 63 |
'default' => 'wp_page_menu', |
| 64 |
), |
| 65 |
'is_block' => array( |
| 66 |
'type' => 'boolean', |
| 67 |
'default' => true, |
| 68 |
), |
| 69 |
|
| 70 |
), |
| 71 |
'render_callback' => 'ew_menu_render_shortcode', |
| 72 |
) |
| 73 |
); |
| 74 |
endif; |
| 75 |
|
| 76 |
if ( ! function_exists( 'ew_menu_render_shortcode' ) ) : |
| 77 |
add_shortcode( 'ew-menu', 'ew_menu_render_shortcode' ); |
| 78 |
function ew_menu_render_shortcode( $atts ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix; wrapped in function_exists() guard. |
| 79 |
$instance['title'] = isset( $atts['title'] ) && 'Navigation' === $atts['title'] |
| 80 |
? esc_html__( 'Navigation', 'essential-widgets' ) |
| 81 |
: sanitize_text_field( $atts['title'] ); |
| 82 |
|
| 83 |
$instance['menu'] = isset( $atts['menu'] ) ? sanitize_text_field( $atts['menu'] ) : ''; |
| 84 |
$instance['container'] = isset( $atts['container'] ) ? sanitize_key( $atts['container'] ) : 'div'; |
| 85 |
$instance['container_id'] = isset( $atts['container_id'] ) ? sanitize_html_class( $atts['container_id'] ) : ''; |
| 86 |
$instance['container_class'] = isset( $atts['container_class'] ) ? sanitize_html_class( $atts['container_class'] ) : ''; |
| 87 |
$instance['menu_id'] = isset( $atts['menu_id'] ) ? sanitize_html_class( $atts['menu_id'] ) : ''; |
| 88 |
$instance['menu_class'] = isset( $atts['menu_class'] ) ? sanitize_html_class( $atts['menu_class'] ) : 'nav-menu'; |
| 89 |
$instance['depth'] = isset( $atts['depth'] ) ? absint( $atts['depth'] ) : 0; |
| 90 |
$instance['before'] = isset( $atts['before'] ) ? wp_kses_post( $atts['before'] ) : ''; |
| 91 |
$instance['after'] = isset( $atts['after'] ) ? wp_kses_post( $atts['after'] ) : ''; |
| 92 |
$instance['link_before'] = isset( $atts['link_before'] ) ? wp_kses_post( $atts['link_before'] ) : ''; |
| 93 |
$instance['link_after'] = isset( $atts['link_after'] ) ? wp_kses_post( $atts['link_after'] ) : ''; |
| 94 |
$instance['fallback_cb'] = isset( $atts['fallback_cb'] ) && function_exists( $atts['fallback_cb'] ) ? $atts['fallback_cb'] : 'wp_page_menu'; |
| 95 |
$instance['is_block'] = !empty( $atts['is_block'] ) ? (bool) $atts['is_block'] : true; |
| 96 |
|
| 97 |
$ew_menu = new EW_Menus(); |
| 98 |
|
| 99 |
return $ew_menu->shortcode( $instance ); |
| 100 |
} |
| 101 |
endif; |
| 102 |
|
| 103 |
|
| 104 |
if ( ! function_exists( 'ew_menu_list' ) ) : |
| 105 |
/** |
| 106 |
* Get nav menus for select option via custom REST API! |
| 107 |
* |
| 108 |
* @return array|null Array of nav menus object with label and value pair, * or null if none. |
| 109 |
*/ |
| 110 |
function ew_menu_list() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix; wrapped in function_exists() guard. |
| 111 |
$menus = wp_get_nav_menus(); |
| 112 |
|
| 113 |
$menu_list = array(); |
| 114 |
|
| 115 |
foreach ( $menus as $menu ) { |
| 116 |
$object = new stdClass(); |
| 117 |
$object->label = sanitize_text_field( $menu->name ); |
| 118 |
$object->value = absint( $menu->term_id ); |
| 119 |
$menu_list[] = $object; |
| 120 |
} |
| 121 |
|
| 122 |
return $menu_list; |
| 123 |
} |
| 124 |
|
| 125 |
add_action( |
| 126 |
'rest_api_init', |
| 127 |
function () { |
| 128 |
register_rest_route( |
| 129 |
'ew-rest/v1', |
| 130 |
'ew-menu-list', |
| 131 |
array( |
| 132 |
'methods' => 'GET', |
| 133 |
'callback' => 'ew_menu_list', |
| 134 |
'permission_callback' => '__return_true', // for public use |
| 135 |
) |
| 136 |
); |
| 137 |
} |
| 138 |
); |
| 139 |
endif; |
| 140 |
|