PluginProbe
FakerPress / 0.8.0
FakerPress v0.8.0
0.9.2 trunk 0.1.0 0.1.1 0.1.2 0.1.3 0.5.2 0.5.3 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.7.0 0.7.1 0.7.2 0.8.0 0.9.0 0.9.1
fakerpress / src / FakerPress / Admin / Menu.php

Menu.php in FakerPress 0.8.0, at src/FakerPress/Admin/Menu.php

235 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FakerPress\Admin;
4
5 use FakerPress\Admin\View\Factory as View_Factory;
6 use function FakerPress\get_request_var;
7 use function FakerPress\make;
8
9 use FakerPress\Plugin;
10 use FakerPress\Admin;
11
12 /**
13 * Class Menu
14 *
15 * @since 0.6.4
16 *
17 */
18 class Menu {
19 /**
20 * Variable holding the submenus objects
21 *
22 * @since 0.6.0
23 *
24 * @var array
25 */
26 protected static $items = [];
27
28 /**
29 * Determines if the list of menus needs sorting.
30 *
31 * @since 0.6.0
32 *
33 * @var bool
34 */
35 protected static $needs_sorting = false;
36
37 /**
38 * Creating submenus easier to be extent from outside
39 *
40 * @since 0.6.0
41 *
42 * @param string $slug Translatable string for the title of the page
43 * @param string $title Translatable string that will go on the menu
44 * @param string $label Translatable string that will go on the menu
45 * @param string $capability WordPress capability that will be required to access this view
46 * @param integer $priority The priority to show this submenu
47 * @param callable|null $callback A callback to render this menu item.
48 * @param string|null $parent What is the parent menu.
49 *
50 */
51 public static function add( string $slug, $title, $label, string $capability = 'manage_options', int $priority = 10, $callback = null, $parent = null ): void {
52 $slug = sanitize_key( $slug );
53
54 static::$items[ $slug ] = $menu = (object) [
55 'slug' => $slug,
56 'title' => esc_attr( $title ),
57 'hook' => null, // Will be set later from WP.
58 'label' => esc_attr( $label ),
59 'capability' => sanitize_key( $capability ),
60 'priority' => absint( $priority ),// Used to set up error page.
61 'callback' => $callback, // Used to set up error page.
62 'parent' => $parent, // Used to set up error page.
63 ];
64
65 if ( ! is_callable( $menu->callback ) ) {
66 $menu->callback = static function () use ( $menu ) {
67 $menu = make( Menu::class )->get_current();
68
69 return make( Admin::class )->render( $menu->slug, [ 'view' => $menu ] );
70 };
71 }
72
73 static::$needs_sorting = true;
74 }
75
76 /**
77 * Given a slug updates that menu object with the new variables.
78 *
79 * @since 0.6.0
80 *
81 * @param string $slug
82 * @param array $update_with
83 *
84 * @return bool
85 */
86 public function update( string $slug, array $update_with ) {
87 $menu = $this->get( $slug );
88
89 if ( empty( $menu ) ) {
90 return false;
91 }
92
93 $menu = (object) array_merge( (array) $menu, (array) $update_with );
94
95 static::$items[ $slug ] = $menu;
96
97 return true;
98 }
99
100 /**
101 * Gets all the menu items sorted by their priority.
102 *
103 * @since 0.6.0
104 *
105 * @return array
106 */
107 public function get_all(): array {
108 if ( static::$needs_sorting ) {
109 uasort( static::$items, 'FakerPress\sort_by_priority' );
110 static::$needs_sorting = false;
111 }
112
113 return static::$items;
114 }
115
116 /**
117 * Gets a specific item from the menu, using its slug.
118 *
119 * @since 0.6.0
120 *
121 * @param string $slug
122 *
123 * @return object|null
124 */
125 public function get( string $slug ) {
126 $all = $this->get_all();
127
128 return $all[ $slug ] ?? null;
129 }
130
131 /**
132 * Filter the `$submenu_file` global right before WordPress builds the Administration Menu
133 *
134 * @since 0.6.0
135 *
136 * @param string $submenu_file Which is the current submenu file.
137 *
138 * @return string
139 */
140 public function filter_submenu_file( $submenu_file ) {
141 if ( ! make( Admin::class )->is_active() ) {
142 return $submenu_file;
143 }
144
145 $view = make( Admin\View\Factory::class )->get_current_view();
146
147 if (
148 0 !== $view->get_menu_priority()
149 ) {
150 $submenu_file = Plugin::$slug . '&view=' . $view::get_slug();
151 }
152
153 return $submenu_file;
154 }
155
156 /**
157 * Method triggered to add the menu to WordPress administration
158 *
159 * @since 0.6.0
160 *
161 * @uses \FakerPress\Plugin::$slug
162 * @uses esc_attr__
163 * @uses add_menu_page
164 * @uses add_submenu_page
165 * @uses current_user_can
166 *
167 * @return null Actions do not return
168 */
169 public function register_menus_to_wp() {
170 foreach ( $this->get_all() as $menu ) {
171 if ( ! current_user_can( $menu->capability ) ) {
172 continue;
173 }
174
175 $update = [];
176
177 if ( empty( $menu->parent ) ) {
178 $update['hook'] = add_menu_page(
179 $menu->title,
180 $menu->label,
181 $menu->capability,
182 $menu->slug,
183 $menu->callback,
184 'none'
185 );
186 } else {
187 $update['hook'] = add_submenu_page(
188 $menu->parent,
189 $menu->title,
190 $menu->label,
191 $menu->capability,
192 "{$menu->parent}&view={$menu->slug}",
193 $menu->callback // The submenus will likely never be called due to howe register.
194 );
195 }
196
197 $this->update( $menu->slug, $update );
198 }
199
200 // Change the Default Submenu for FakerPress menus
201 if ( ! empty( $GLOBALS['submenu'][ Plugin::$slug ] ) ) {
202 $GLOBALS['submenu'][ Plugin::$slug ][0][0] = esc_attr__( 'Settings', 'fakerpress' );
203 }
204 }
205
206 /**
207 * Properly sets the current screen, this is a hacky solution because of how poorly WordPress handles Admin menus not
208 * using the page param.
209 *
210 * @since 0.6.4
211 *
212 * @param \WP_Screen $screen Which screen are we in?
213 *
214 * @return void
215 */
216 public function correctly_set_current_screen( $screen ) {
217 $view = make( View_Factory::class )->get_current_view();
218 if ( ! $view ) {
219 return;
220 }
221
222 if ( ! $view->has_menu() ) {
223 return;
224 }
225
226 $menu = $view->get_menu();
227
228 set_current_screen( $menu->hook );
229
230 global $page_hook;
231
232 $page_hook = $menu->hook;
233 }
234 }
235