PluginProbe
Gantry 5 Framework / trunk
Gantry 5 Framework vtrunk
5.6.4 5.6.3 trunk 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.2.0 5.2.1 5.2.10 5.2.11 5.2.12 5.2.13 5.2.14 5.2.15 5.2.17 5.2.18 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 All 90 releases
gantry5 / admin / init.php

init.php in Gantry 5 Framework trunk, at admin/init.php

285 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.Security.NonceVerification.Recommended
3 defined('ABSPATH') or die;
4
5 use Gantry\Admin\Router;
6 use Gantry\Framework\Document;
7 use Gantry\Framework\Gantry;
8 use Gantry\Framework\Platform;
9 use Gantry\Framework\Theme;
10 use Gantry\WordPress\NavMenuEditWalker;
11 use Gantry5\Loader;
12
13 add_action('admin_init', 'gantry5_admin_start_buffer', -10000);
14 add_action('admin_enqueue_scripts', 'gantry5_admin_scripts');
15 add_action('wp_ajax_gantry5', 'gantry5_layout_manager');
16 add_filter('upgrader_package_options', 'gantry5_upgrader_package_options', 10000);
17 add_filter('upgrader_source_selection', 'gantry5_upgrader_source_selection', 0, 4);
18 add_action('upgrader_post_install', 'gantry5_upgrader_post_install', 10, 3);
19
20 // Custom menu type:
21 add_action('admin_head', 'gantry5_add_menu_item_types', 99);
22 add_filter('wp_setup_nav_menu_item', 'gantry5_customize_menu_item_label');
23 add_filter('wp_edit_nav_menu_walker', 'gantry5_wp_edit_nav_menu_walker');
24 add_filter('pre_wp_unique_post_slug', 'gantry5_wp_unique_post_slug', 0, 6);
25
26 // Check if Timber is active before displaying sidebar button
27 if (class_exists( 'Timber')) {
28 // Load Gantry 5 icon styling for the admin sidebar
29 add_action(
30 'admin_enqueue_scripts',
31 static function() {
32 if(is_admin()) {
33 $version = defined('GANTRY5_VERSION') ? GANTRY5_VERSION : null;
34 wp_enqueue_style('wordpress-admin-icon', Document::url('gantry-assets://css/wordpress-admin-icon.css'), [], $version);
35 }
36 }
37 );
38
39 // Adjust menu to contain Gantry stuff.
40 add_action(
41 'admin_menu',
42 static function() {
43 $gantry = Gantry::instance();
44
45 /** @var Theme $theme */
46 $theme = $gantry['theme'];
47 $name = $theme->details()['details.name'];
48 remove_submenu_page('themes.php', 'theme-editor.php');
49 add_menu_page("{$name} Theme", "{$name} Theme", 'manage_options', 'layout-manager', 'gantry5_layout_manager');
50 },
51 100
52 );
53 }
54
55 function gantry5_admin_start_buffer()
56 {
57 ob_start();
58 ob_implicit_flush(false);
59 }
60
61 function gantry5_init()
62 {
63 $gantry = Gantry::instance();
64 if (!isset($gantry['router'])) {
65 $gantry['router'] = $router = new Router($gantry);
66 $router->boot()->load();
67 }
68
69 return $gantry;
70 }
71
72 function gantry5_add_menu_item_type_particle()
73 {
74 global $_nav_menu_placeholder, $nav_menu_selected_id;
75
76 $_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
77
78 $gantry = gantry5_init();
79
80 // Get full list of particles.
81 $particles = $gantry['particles']->all();
82 ?>
83 <div class="posttypediv" id="custom-item-types">
84 <div id="tabs-panel-custom-item-types" class="tabs-panel tabs-panel-active">
85 <ul id="custom-item-types-checklist" class="categorychecklist form-no-clear">
86 <?php foreach ($particles as $name => $particle): ?>
87 <?php if ($name !== 'widget' && $particle['type'] !== 'particle') continue; ?>
88 <li>
89 <label class="menu-item-title">
90 <input type="radio" class="menu-item-checkbox" name="menu-item[<?php echo esc_attr((string) $_nav_menu_placeholder); ?>][menu-item-object-id]" value="-1">
91 <?php echo esc_html($particle['name']); ?>
92 </label>
93 <input type="hidden" class="menu-item-type" name="menu-item[<?php echo esc_attr((string) $_nav_menu_placeholder); ?>][menu-item-type]" value="custom">
94 <input type="hidden" class="menu-item-title" name="menu-item[<?php echo esc_attr((string) $_nav_menu_placeholder); ?>][menu-item-title]" value="<?php echo esc_attr($particle['name']); ?>">
95 <input type="hidden" class="menu-item-attr-title" name="menu-item[<?php echo esc_attr((string) $_nav_menu_placeholder); ?>][menu-item-attr-title]" value="gantry-particle-<?php echo esc_attr($name); ?>"/>
96 </li>
97 <?php endforeach; ?>
98 </ul>
99 </div>
100 <input type="hidden" value="custom" name="menu-item[<?php echo esc_attr((string) $_nav_menu_placeholder); ?>][menu-item-type]" />
101
102 <p class="button-controls wp-clearfix">
103 <span class="add-to-menu">
104 <input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu', 'gantry5' ); ?>" name="add-custom-menu-item" id="submit-custom-item-types" />
105 <span class="spinner"></span>
106 </span>
107 </p>
108
109 </div>
110 <?php
111 }
112
113 function gantry5_customize_menu_item_label($menu_item)
114 {
115 if ('custom' !== $menu_item->type || strpos($menu_item->attr_title, 'gantry-particle-') !== 0) {
116 return $menu_item;
117 }
118
119 $gantry = gantry5_init();
120
121 // Get full list of particles.
122 $particles = $gantry['particles']->all();
123
124 $id = substr($menu_item->attr_title, strlen('gantry-particle-'));
125
126 if (isset($particles[$id])) {
127 $menu_item->type_label = $particles[$id]['name'] . ' ' . __('Particle', 'gantry5');
128 } else {
129 $menu_item->type_label = __('Unknown Particle', 'gantry5');
130 }
131
132 return $menu_item;
133 }
134
135 function gantry5_wp_edit_nav_menu_walker()
136 {
137 gantry5_init();
138
139 return NavMenuEditWalker::class;
140 }
141
142 function gantry5_wp_unique_post_slug($override_slug, $slug, $post_ID, $post_status, $post_type, $post_parent)
143 {
144 global $wpdb;
145 if ($post_type !== 'nav_menu_item') {
146 return null;
147 }
148 if (strpos($slug, '__particle-') === 0) {
149 return $slug;
150 }
151
152 $post = $wpdb->get_row(
153 $wpdb->prepare(
154 "SELECT * FROM {$wpdb->posts} WHERE post_type = %s AND ID = %d LIMIT 1",
155 $post_type,
156 $post_ID
157 )
158 );
159 if (!isset($post->content) || strpos($post->post_excerpt, 'gantry-particle-') !== 0) {
160 return null;
161 }
162
163 if (strpos($post->content, '__particle-') === 0) {
164 return $post->content;
165 }
166
167 return "__particle-{$post_ID}";
168 }
169
170
171 function gantry5_add_menu_item_types()
172 {
173 add_meta_box('gantry_particles', __('Particles', 'gantry5'), 'gantry5_add_menu_item_type_particle', 'nav-menus', 'side', 'low');
174 }
175
176 function gantry5_admin_scripts()
177 {
178 $page = isset($_GET['page']) ? sanitize_key(wp_unslash($_GET['page'])) : '';
179 if ($page === 'layout-manager') {
180 gantry5_layout_manager();
181 }
182 }
183
184 function gantry5_layout_manager()
185 {
186 static $output = null;
187
188 if (!current_user_can('manage_options')) {
189 wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'gantry5'));
190 }
191
192 add_filter('admin_body_class', static function() {
193 return 'gantry5 gantry5-wordpress';
194 });
195
196 if ($output) {
197 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Router output is trusted admin HTML.
198 echo $output;
199 return;
200 }
201
202 // Detect Gantry Framework or fail gracefully.
203 if (!class_exists(Loader::class)) {
204 wp_die(esc_html__('Gantry 5 Framework not found.', 'gantry5'));
205 }
206
207 // Initialize administrator or fail gracefully.
208 try {
209 Loader::setup();
210
211 $gantry = Gantry::instance();
212 $router = new Router($gantry);
213 $gantry['router'] = $router;
214
215 // Dispatch to the controller.
216 $output = $router->dispatch();
217 } catch (Exception $e) {
218 throw $e;
219 }
220 }
221
222 /**
223 * SimpleXmlElement is a weird class that acts like a boolean, we are going to take advantage from that.
224 */
225 class Gantry5Truthy extends SimpleXMLElement {}
226
227 function gantry5_upgrader_package_options($options)
228 {
229 if (isset($options['hook_extra']['type']) && !$options['clear_destination']) {
230 if ($options['hook_extra']['type'] === 'theme' && $options['abort_if_destination_exists']) {
231 // Prepare for manual theme upgrade.
232 $options['abort_if_destination_exists'] = new Gantry5Truthy('<bool><true></true></bool>');
233 $options['hook_extra']['gantry5_abort'] = $options['abort_if_destination_exists'];
234 } elseif ($options['hook_extra']['type'] === 'plugin' && strpos(basename($options['package']), 'gantry5') !== false) {
235 // Allow Gantry plugin to be manually upgraded / downgraded.
236 $options['clear_destination'] = true;
237 }
238 }
239
240 return $options;
241 }
242
243 function gantry5_upgrader_source_selection($source, $remote_source, $upgrader, $options = [])
244 {
245 // Allow upgrading Gantry themes from uploader.
246 if (isset($options['gantry5_abort']) && file_exists("{$source}/gantry/theme.yaml")) {
247 $upgrader->skin->feedback('Gantry 5 theme detected.');
248 unset($options['gantry5_abort']->true);
249 }
250
251 return $source;
252 }
253
254 function gantry5_upgrader_post_install($success, $options, $result)
255 {
256 if ($success) {
257 $theme = isset($options['gantry5_abort']) && !$options['gantry5_abort'];
258 $plugin = (isset($options['plugin']) && $options['plugin'] === 'gantry5/gantry5.php')
259 || (isset($options['type']) && $options['type'] === 'plugin' && basename($result['destination']) === 'gantry5');
260
261 // Clear gantry cache after plugin / Gantry theme installs.
262 if ($theme || $plugin) {
263 global $wp_filesystem;
264
265 $gantry = Gantry::instance();
266
267 /** @var Platform $platform */
268 $platform = $gantry['platform'];
269
270 $path = $platform->getCachePath();
271 if ($wp_filesystem->is_dir($path)) {
272 $wp_filesystem->rmdir($path, true);
273 }
274
275 // Make sure that PHP has the latest data of the files.
276 clearstatcache();
277
278 // Remove all compiled files from opcode cache.
279 if (function_exists('opcache_reset')) {
280 @opcache_reset();
281 }
282 }
283 }
284 }
285