| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\EditorOne\Classes; |
| 4 |
|
| 5 |
use Elementor\Core\Admin\EditorOneMenu\Interfaces\Menu_Item_Interface; |
| 6 |
use Elementor\Core\Admin\EditorOneMenu\Interfaces\Menu_Item_Third_Level_Interface; |
| 7 |
use Elementor\Core\Admin\EditorOneMenu\Interfaces\Menu_Item_With_Custom_Url_Interface; |
| 8 |
use Elementor\Plugin; |
| 9 |
use Elementor\Utils; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class Menu_Data_Provider { |
| 16 |
public const THIRD_LEVEL_EDITOR_FLYOUT = 'editor_flyout'; |
| 17 |
public const THIRD_LEVEL_FLYOUT_MENU = 'flyout_menu'; |
| 18 |
|
| 19 |
private static ?Menu_Data_Provider $instance = null; |
| 20 |
private array $level3_items = []; |
| 21 |
private array $level4_items = []; |
| 22 |
private ?string $theme_builder_url = null; |
| 23 |
private ?array $cached_level3_sidebar_data = null; |
| 24 |
private ?array $cached_level4_sidebar_data = null; |
| 25 |
private ?array $cached_flyout_menu_data = null; |
| 26 |
private Slug_Normalizer $slug_normalizer; |
| 27 |
|
| 28 |
public static function instance(): self { |
| 29 |
if ( null === self::$instance ) { |
| 30 |
self::$instance = new self(); |
| 31 |
} |
| 32 |
|
| 33 |
return self::$instance; |
| 34 |
} |
| 35 |
|
| 36 |
private function __construct() { |
| 37 |
$this->slug_normalizer = new Slug_Normalizer(); |
| 38 |
} |
| 39 |
|
| 40 |
public function get_slug_normalizer(): Slug_Normalizer { |
| 41 |
return $this->slug_normalizer; |
| 42 |
} |
| 43 |
|
| 44 |
public function register_menu( Menu_Item_Interface $item ): void { |
| 45 |
if ( ! ( $item instanceof Menu_Item_Third_Level_Interface ) ) { |
| 46 |
$this->register_level4_item( $item ); |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$group_id = $item->get_group_id(); |
| 51 |
$collapsible_groups = [ |
| 52 |
Menu_Config::TEMPLATES_GROUP_ID, |
| 53 |
Menu_Config::CUSTOM_ELEMENTS_GROUP_ID, |
| 54 |
Menu_Config::SYSTEM_GROUP_ID, |
| 55 |
]; |
| 56 |
|
| 57 |
if ( in_array( $group_id, $collapsible_groups, true ) && ! $item->has_children() ) { |
| 58 |
$this->register_level4_item( $item ); |
| 59 |
} else { |
| 60 |
$this->register_level3_item( $item ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public function register_level3_item( Menu_Item_Third_Level_Interface $item ): void { |
| 65 |
$group_id = $item->get_group_id(); |
| 66 |
$item_slug = $item->get_slug(); |
| 67 |
|
| 68 |
if ( ! isset( $this->level3_items[ $group_id ] ) ) { |
| 69 |
$this->level3_items[ $group_id ] = []; |
| 70 |
} |
| 71 |
|
| 72 |
$this->level3_items[ $group_id ][ $item_slug ] = $item; |
| 73 |
|
| 74 |
$this->invalidate_cache(); |
| 75 |
} |
| 76 |
|
| 77 |
public function register_level4_item( Menu_Item_Interface $item ): void { |
| 78 |
$group_id = $item->get_group_id(); |
| 79 |
$item_slug = $item->get_slug(); |
| 80 |
|
| 81 |
if ( ! isset( $this->level4_items[ $group_id ] ) ) { |
| 82 |
$this->level4_items[ $group_id ] = []; |
| 83 |
} |
| 84 |
|
| 85 |
$this->level4_items[ $group_id ][ $item_slug ] = $item; |
| 86 |
$this->invalidate_cache(); |
| 87 |
} |
| 88 |
|
| 89 |
public function get_level3_items(): array { |
| 90 |
return $this->level3_items; |
| 91 |
} |
| 92 |
|
| 93 |
public function get_level4_items(): array { |
| 94 |
return $this->level4_items; |
| 95 |
} |
| 96 |
|
| 97 |
public function is_item_already_registered( string $item_slug ): bool { |
| 98 |
$all_items = array_merge( $this->level3_items, $this->level4_items ); |
| 99 |
|
| 100 |
foreach ( $all_items as $group_items ) { |
| 101 |
if ( isset( $group_items[ $item_slug ] ) ) { |
| 102 |
return true; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
public function get_third_level_data( string $variant ): array { |
| 110 |
if ( self::THIRD_LEVEL_EDITOR_FLYOUT === $variant ) { |
| 111 |
return $this->get_third_level_data_from_cache( |
| 112 |
$this->cached_level3_sidebar_data, |
| 113 |
[ $this, 'build_level3_flyout_items' ] |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
if ( self::THIRD_LEVEL_FLYOUT_MENU === $variant ) { |
| 118 |
return $this->get_third_level_data_from_cache( |
| 119 |
$this->cached_flyout_menu_data, |
| 120 |
[ $this, 'build_flyout_items_with_expanded_third_party' ] |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
return []; |
| 125 |
} |
| 126 |
|
| 127 |
public function get_level4_flyout_data(): array { |
| 128 |
if ( null !== $this->cached_level4_sidebar_data ) { |
| 129 |
return $this->cached_level4_sidebar_data; |
| 130 |
} |
| 131 |
|
| 132 |
$groups = $this->build_level4_flyout_groups(); |
| 133 |
|
| 134 |
foreach ( $groups as $group_id => $group ) { |
| 135 |
if ( ! empty( $group['items'] ) ) { |
| 136 |
$this->sort_items_by_priority( $groups[ $group_id ]['items'] ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
$this->cached_level4_sidebar_data = $groups; |
| 141 |
|
| 142 |
return $this->cached_level4_sidebar_data; |
| 143 |
} |
| 144 |
|
| 145 |
private function get_third_level_data_from_cache( ?array &$cache, callable $items_builder ): array { |
| 146 |
if ( null !== $cache ) { |
| 147 |
return $cache; |
| 148 |
} |
| 149 |
|
| 150 |
$items = $items_builder(); |
| 151 |
|
| 152 |
$this->sort_items_by_priority( $items ); |
| 153 |
|
| 154 |
$cache = [ |
| 155 |
'parent_slug' => Menu_Config::EDITOR_MENU_SLUG, |
| 156 |
'items' => $items, |
| 157 |
]; |
| 158 |
|
| 159 |
return $cache; |
| 160 |
} |
| 161 |
|
| 162 |
public function get_theme_builder_url(): string { |
| 163 |
if ( null === $this->theme_builder_url ) { |
| 164 |
$pro_url = Plugin::$instance->app ? Plugin::$instance->app->get_settings( 'menu_url' ) : null; |
| 165 |
$return_to = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ?? '' ) ); |
| 166 |
|
| 167 |
if ( $pro_url ) { |
| 168 |
if ( false !== strpos( $pro_url, '#' ) ) { |
| 169 |
$url = $this->add_return_to_url( $pro_url, $return_to ); |
| 170 |
} else { |
| 171 |
$url = $pro_url; |
| 172 |
} |
| 173 |
} else { |
| 174 |
$url = $this->add_return_to_url( |
| 175 |
admin_url( 'admin.php?page=elementor-app' ) . '#/site-editor/promotion', |
| 176 |
$return_to |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
$this->theme_builder_url = apply_filters( 'elementor/editor-one/menu/theme_builder_url', $url ); |
| 181 |
} |
| 182 |
|
| 183 |
return $this->theme_builder_url; |
| 184 |
} |
| 185 |
|
| 186 |
private function add_return_to_url( string $url, string $return_to ): string { |
| 187 |
$hash_position = strpos( $url, '#' ); |
| 188 |
|
| 189 |
if ( false === $hash_position ) { |
| 190 |
return add_query_arg( [ 'return_to' => $return_to ], $url ); |
| 191 |
} |
| 192 |
|
| 193 |
$base_url = substr( $url, 0, $hash_position ); |
| 194 |
$hash_fragment = substr( $url, $hash_position ); |
| 195 |
|
| 196 |
return add_query_arg( [ 'return_to' => $return_to ], $base_url ) . $hash_fragment; |
| 197 |
} |
| 198 |
|
| 199 |
public function get_all_sidebar_page_slugs(): array { |
| 200 |
$base_slugs = [ |
| 201 |
Menu_Config::ELEMENTOR_MENU_SLUG, |
| 202 |
Menu_Config::EDITOR_MENU_SLUG, |
| 203 |
]; |
| 204 |
|
| 205 |
$slugs = array_merge( |
| 206 |
$base_slugs, |
| 207 |
$this->get_dynamic_page_slugs() |
| 208 |
); |
| 209 |
|
| 210 |
return array_values( array_unique( $slugs ) ); |
| 211 |
} |
| 212 |
|
| 213 |
public function is_elementor_editor_page(): bool { |
| 214 |
if ( ! get_current_screen() ) { |
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
$page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? ''; |
| 219 |
|
| 220 |
if ( Menu_Config::ELEMENTOR_HOME_MENU_SLUG === $page ) { |
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
if ( in_array( $page, $this->get_all_sidebar_page_slugs(), true ) ) { |
| 225 |
return true; |
| 226 |
} |
| 227 |
|
| 228 |
$post_type = filter_input( INPUT_GET, 'post_type', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ?? ''; |
| 229 |
|
| 230 |
return $this->is_elementor_post_type( $post_type ); |
| 231 |
} |
| 232 |
|
| 233 |
public function is_editor_one_post_edit_screen(): bool { |
| 234 |
$screen = get_current_screen(); |
| 235 |
|
| 236 |
if ( ! $screen || empty( $screen->post_type ) ) { |
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
if ( 'post' !== $screen->base ) { |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
$post_types = apply_filters( 'elementor/editor-one/admin-edit-post-types', [] ); |
| 245 |
|
| 246 |
return in_array( $screen->post_type, $post_types, true ); |
| 247 |
} |
| 248 |
|
| 249 |
public function is_editor_one_admin_page(): bool { |
| 250 |
return $this->is_elementor_editor_page() || $this->is_editor_one_post_edit_screen(); |
| 251 |
} |
| 252 |
|
| 253 |
private function is_elementor_post_type( string $post_type ): bool { |
| 254 |
if ( empty( $post_type ) ) { |
| 255 |
return false; |
| 256 |
} |
| 257 |
|
| 258 |
return isset( Menu_Config::get_elementor_post_types()[ $post_type ] ); |
| 259 |
} |
| 260 |
|
| 261 |
public static function get_elementor_post_types(): array { |
| 262 |
return Menu_Config::get_elementor_post_types(); |
| 263 |
} |
| 264 |
|
| 265 |
private function get_dynamic_page_slugs(): array { |
| 266 |
$slugs = []; |
| 267 |
|
| 268 |
foreach ( $this->level3_items as $group_items ) { |
| 269 |
$slugs = array_merge( $slugs, array_keys( $group_items ) ); |
| 270 |
} |
| 271 |
|
| 272 |
foreach ( $this->level4_items as $group_items ) { |
| 273 |
foreach ( $group_items as $item_slug => $item ) { |
| 274 |
$slugs[] = $item_slug; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
$allowed_prefixes = [ 'elementor', 'e-', 'popup_templates' ]; |
| 279 |
|
| 280 |
return array_values( array_filter( $slugs, function( string $slug ) use ( $allowed_prefixes ): bool { |
| 281 |
foreach ( $allowed_prefixes as $prefix ) { |
| 282 |
if ( 0 === strpos( $slug, $prefix ) ) { |
| 283 |
return true; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
return false; |
| 288 |
} ) ); |
| 289 |
} |
| 290 |
|
| 291 |
private function build_level3_flyout_items(): array { |
| 292 |
return $this->build_flyout_items( false ); |
| 293 |
} |
| 294 |
|
| 295 |
private function build_flyout_items_with_expanded_third_party(): array { |
| 296 |
$items = $this->build_flyout_items( true ); |
| 297 |
|
| 298 |
if ( ! Utils::has_pro() ) { |
| 299 |
$items[] = $this->build_theme_builder_flyout_item(); |
| 300 |
} |
| 301 |
|
| 302 |
return $items; |
| 303 |
} |
| 304 |
|
| 305 |
private function build_theme_builder_flyout_item(): array { |
| 306 |
return [ |
| 307 |
'slug' => 'elementor-theme-builder', |
| 308 |
'label' => esc_html__( 'Theme Builder', 'elementor' ), |
| 309 |
'url' => $this->get_theme_builder_url(), |
| 310 |
'icon' => 'theme-builder', |
| 311 |
'group_id' => '', |
| 312 |
'priority' => 50, |
| 313 |
'has_divider_before' => false, |
| 314 |
]; |
| 315 |
} |
| 316 |
|
| 317 |
private function build_flyout_items( bool $expand_third_party ): array { |
| 318 |
$items = []; |
| 319 |
$existing_slugs = []; |
| 320 |
$excluded_slugs = Menu_Config::get_excluded_level3_slugs(); |
| 321 |
$excluded_level4_slugs = $expand_third_party ? Menu_Config::get_excluded_level4_slugs() : []; |
| 322 |
|
| 323 |
foreach ( $this->level3_items as $group_items ) { |
| 324 |
foreach ( $group_items as $item_slug => $item ) { |
| 325 |
if ( ! $this->should_include_flyout_item( $item, $item_slug, $existing_slugs, $excluded_slugs ) ) { |
| 326 |
continue; |
| 327 |
} |
| 328 |
|
| 329 |
if ( $expand_third_party && $this->is_third_party_parent_with_children( $item ) ) { |
| 330 |
$children = $this->level4_items[ Menu_Config::THIRD_PARTY_GROUP_ID ] ?? []; |
| 331 |
$is_first_child = true; |
| 332 |
|
| 333 |
foreach ( $children as $child_slug => $child ) { |
| 334 |
if ( ! $this->is_item_accessible( $child ) ) { |
| 335 |
continue; |
| 336 |
} |
| 337 |
|
| 338 |
if ( $this->is_slug_excluded( $child_slug, $excluded_level4_slugs, true ) ) { |
| 339 |
continue; |
| 340 |
} |
| 341 |
|
| 342 |
if ( in_array( $child_slug, $existing_slugs, true ) ) { |
| 343 |
continue; |
| 344 |
} |
| 345 |
|
| 346 |
$child_data = $this->create_expanded_child_item_data( $child, $child_slug, $is_first_child ); |
| 347 |
$items[] = $child_data; |
| 348 |
$existing_slugs[] = $child_slug; |
| 349 |
$is_first_child = false; |
| 350 |
} |
| 351 |
continue; |
| 352 |
} |
| 353 |
|
| 354 |
$items[] = $this->create_flyout_item_data( $item, $item_slug ); |
| 355 |
$existing_slugs[] = $item_slug; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
return $items; |
| 360 |
} |
| 361 |
|
| 362 |
private function is_third_party_parent_with_children( Menu_Item_Interface $item ): bool { |
| 363 |
if ( Menu_Config::THIRD_PARTY_GROUP_ID !== $item->get_group_id() ) { |
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
if ( ! $item->has_children() ) { |
| 368 |
return false; |
| 369 |
} |
| 370 |
|
| 371 |
$children = $this->level4_items[ Menu_Config::THIRD_PARTY_GROUP_ID ] ?? []; |
| 372 |
|
| 373 |
return ! empty( $children ); |
| 374 |
} |
| 375 |
|
| 376 |
private function create_expanded_child_item_data( Menu_Item_Interface $item, string $item_slug, bool $is_first ): array { |
| 377 |
$url = $this->resolve_item_url( $item, $item_slug ); |
| 378 |
|
| 379 |
return [ |
| 380 |
'slug' => $item_slug, |
| 381 |
'label' => $this->title_case( $item->get_label() ), |
| 382 |
'url' => $url, |
| 383 |
'group_id' => '', |
| 384 |
'priority' => $this->get_item_priority( $item ), |
| 385 |
'has_divider_before' => $is_first, |
| 386 |
]; |
| 387 |
} |
| 388 |
|
| 389 |
private function should_include_flyout_item( Menu_Item_Interface $item, string $item_slug, array $existing_slugs, array $excluded_slugs ): bool { |
| 390 |
if ( ! $this->is_item_accessible( $item ) ) { |
| 391 |
return false; |
| 392 |
} |
| 393 |
|
| 394 |
if ( in_array( $item_slug, $existing_slugs, true ) ) { |
| 395 |
return false; |
| 396 |
} |
| 397 |
|
| 398 |
if ( $this->is_slug_excluded( $item_slug, $excluded_slugs ) ) { |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
if ( empty( trim( wp_strip_all_tags( $item->get_label() ) ) ) ) { |
| 403 |
return false; |
| 404 |
} |
| 405 |
|
| 406 |
return true; |
| 407 |
} |
| 408 |
|
| 409 |
private function create_flyout_item_data( Menu_Item_Interface $item, string $item_slug ): array { |
| 410 |
$has_children = $item->has_children(); |
| 411 |
$group_id = $has_children ? $item->get_group_id() : ''; |
| 412 |
$is_third_party_parent = Menu_Config::THIRD_PARTY_GROUP_ID === $item->get_group_id(); |
| 413 |
|
| 414 |
return [ |
| 415 |
'slug' => $item_slug, |
| 416 |
'label' => $this->title_case( $item->get_label() ), |
| 417 |
'url' => $this->resolve_flyout_item_url( $item, $item_slug ), |
| 418 |
'icon' => $item->get_icon(), |
| 419 |
'group_id' => $group_id, |
| 420 |
'priority' => $this->get_item_priority( $item ), |
| 421 |
'has_divider_before' => $is_third_party_parent, |
| 422 |
]; |
| 423 |
} |
| 424 |
|
| 425 |
private function resolve_flyout_item_url( Menu_Item_Interface $item, string $item_slug ): string { |
| 426 |
$url = $this->resolve_item_url( $item, $item_slug ); |
| 427 |
|
| 428 |
if ( ! $item->has_children() ) { |
| 429 |
return $url; |
| 430 |
} |
| 431 |
|
| 432 |
$children = $this->get_level4_items()[ $item->get_group_id() ] ?? []; |
| 433 |
|
| 434 |
if ( empty( $children ) ) { |
| 435 |
return $url; |
| 436 |
} |
| 437 |
|
| 438 |
$first_child_url = $this->get_first_accessible_child_url( $children ); |
| 439 |
|
| 440 |
return $first_child_url ?? $url; |
| 441 |
} |
| 442 |
|
| 443 |
private function get_first_accessible_child_url( array $children ): ?string { |
| 444 |
$children_data = []; |
| 445 |
|
| 446 |
foreach ( $children as $child_slug => $child_item ) { |
| 447 |
if ( ! $this->is_item_accessible( $child_item ) ) { |
| 448 |
continue; |
| 449 |
} |
| 450 |
|
| 451 |
$children_data[] = [ |
| 452 |
'url' => $this->resolve_item_url( $child_item, $child_slug ), |
| 453 |
'priority' => $this->get_item_priority( $child_item ), |
| 454 |
]; |
| 455 |
} |
| 456 |
|
| 457 |
if ( empty( $children_data ) ) { |
| 458 |
return null; |
| 459 |
} |
| 460 |
|
| 461 |
$this->sort_items_by_priority( $children_data ); |
| 462 |
|
| 463 |
return $children_data[0]['url']; |
| 464 |
} |
| 465 |
|
| 466 |
private function build_level4_flyout_groups(): array { |
| 467 |
$groups = []; |
| 468 |
$excluded_slugs = Menu_Config::get_excluded_level4_slugs(); |
| 469 |
|
| 470 |
foreach ( $this->level4_items as $group_id => $items ) { |
| 471 |
$groups[ $group_id ] = [ 'items' => [] ]; |
| 472 |
$existing_labels = []; |
| 473 |
|
| 474 |
foreach ( $items as $item_slug => $item ) { |
| 475 |
if ( ! $this->is_item_accessible( $item ) ) { |
| 476 |
continue; |
| 477 |
} |
| 478 |
|
| 479 |
if ( $this->is_slug_excluded( $item_slug, $excluded_slugs, true ) ) { |
| 480 |
continue; |
| 481 |
} |
| 482 |
|
| 483 |
$label = $item->get_label(); |
| 484 |
$label_lower = strtolower( $label ); |
| 485 |
|
| 486 |
if ( in_array( $label_lower, $existing_labels, true ) ) { |
| 487 |
continue; |
| 488 |
} |
| 489 |
|
| 490 |
$url = $this->resolve_item_url( $item, $item_slug ); |
| 491 |
|
| 492 |
$groups[ $group_id ]['items'][] = [ |
| 493 |
'slug' => $item_slug, |
| 494 |
'label' => $this->title_case( $item->get_label() ), |
| 495 |
'url' => $url, |
| 496 |
'priority' => $this->get_item_priority( $item ), |
| 497 |
]; |
| 498 |
|
| 499 |
$existing_labels[] = $label_lower; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
return $groups; |
| 504 |
} |
| 505 |
|
| 506 |
public function is_item_accessible( Menu_Item_Interface $item ): bool { |
| 507 |
return $item->is_visible() && current_user_can( $item->get_capability() ); |
| 508 |
} |
| 509 |
|
| 510 |
private function get_item_url( string $item_slug, ?string $parent_slug = null ): string { |
| 511 |
$admin_path_prefixes = [ 'edit.php', 'post-new.php', 'admin.php' ]; |
| 512 |
|
| 513 |
foreach ( $admin_path_prefixes as $prefix ) { |
| 514 |
if ( 0 === strpos( $item_slug, $prefix ) ) { |
| 515 |
return admin_url( $item_slug ); |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
if ( 0 === strpos( $item_slug, 'http' ) ) { |
| 520 |
return $item_slug; |
| 521 |
} |
| 522 |
|
| 523 |
if ( $parent_slug && 0 === strpos( $parent_slug, 'edit.php' ) ) { |
| 524 |
return admin_url( $parent_slug . '&page=' . $item_slug ); |
| 525 |
} |
| 526 |
|
| 527 |
return admin_url( 'admin.php?page=' . $item_slug ); |
| 528 |
} |
| 529 |
|
| 530 |
private function resolve_item_url( Menu_Item_Interface $item, string $item_slug ): string { |
| 531 |
if ( $item instanceof Menu_Item_With_Custom_Url_Interface ) { |
| 532 |
return $item->get_menu_url(); |
| 533 |
} |
| 534 |
|
| 535 |
return $this->get_item_url( $item_slug, $item->get_parent_slug() ); |
| 536 |
} |
| 537 |
|
| 538 |
private function get_item_priority( Menu_Item_Interface $item ): int { |
| 539 |
return $item->get_position() ?? 100; |
| 540 |
} |
| 541 |
|
| 542 |
private function is_slug_excluded( string $item_slug, array $excluded_slugs, bool $use_normalizer = false ): bool { |
| 543 |
if ( $use_normalizer ) { |
| 544 |
return $this->slug_normalizer->is_excluded( $item_slug, $excluded_slugs ); |
| 545 |
} |
| 546 |
|
| 547 |
return in_array( $item_slug, $excluded_slugs, true ); |
| 548 |
} |
| 549 |
|
| 550 |
private function sort_items_by_priority( array &$items ): void { |
| 551 |
usort( $items, function ( array $a, array $b ): int { |
| 552 |
return ( $a['priority'] ?? 100 ) <=> ( $b['priority'] ?? 100 ); |
| 553 |
} ); |
| 554 |
} |
| 555 |
|
| 556 |
private function title_case( string $text ): string { |
| 557 |
if ( function_exists( 'mb_convert_case' ) ) { |
| 558 |
return mb_convert_case( $text, MB_CASE_TITLE, 'UTF-8' ); |
| 559 |
} |
| 560 |
|
| 561 |
return ucwords( strtolower( $text ) ); |
| 562 |
} |
| 563 |
|
| 564 |
private function invalidate_cache(): void { |
| 565 |
$this->cached_level3_sidebar_data = null; |
| 566 |
$this->cached_level4_sidebar_data = null; |
| 567 |
$this->cached_flyout_menu_data = null; |
| 568 |
} |
| 569 |
|
| 570 |
public static function get_current_user_capabilities(): array { |
| 571 |
$user = wp_get_current_user(); |
| 572 |
|
| 573 |
if ( ! $user || ! $user->exists() ) { |
| 574 |
return [ |
| 575 |
'user' => null, |
| 576 |
'has_edit_posts' => false, |
| 577 |
'has_manage_options' => false, |
| 578 |
'is_edit_posts_user' => false, |
| 579 |
]; |
| 580 |
} |
| 581 |
|
| 582 |
$has_edit_posts = isset( $user->allcaps[ Menu_Config::CAPABILITY_EDIT_POSTS ] ) && $user->allcaps[ Menu_Config::CAPABILITY_EDIT_POSTS ]; |
| 583 |
$has_manage_options = isset( $user->allcaps[ Menu_Config::CAPABILITY_MANAGE_OPTIONS ] ) && $user->allcaps[ Menu_Config::CAPABILITY_MANAGE_OPTIONS ]; |
| 584 |
$is_edit_posts_user = $has_edit_posts && ! $has_manage_options; |
| 585 |
|
| 586 |
return [ |
| 587 |
'user' => $user, |
| 588 |
'has_edit_posts' => $has_edit_posts, |
| 589 |
'has_manage_options' => $has_manage_options, |
| 590 |
'is_edit_posts_user' => $is_edit_posts_user, |
| 591 |
]; |
| 592 |
} |
| 593 |
} |
| 594 |
|