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