PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.5.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.5.0
4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / app / src / WordPress / Admin / Menus / AdminMenuPageService.php
surecart / app / src / WordPress / Admin / Menus Last commit date
AdminMenuPageService.php 2 months ago AdminMenuPageServiceProvider.php 8 months ago AdminToolbarService.php 8 months ago ProductCollectionsMenuService.php 4 months ago
AdminMenuPageService.php
423 lines
1 <?php
2
3 namespace SureCart\WordPress\Admin\Menus;
4
5 use SureCart\Models\ApiToken;
6
7 /**
8 * Handles creation and enqueueing of admin menu pages and assets.
9 */
10 class AdminMenuPageService {
11 /**
12 * Top level menu slug.
13 *
14 * @var string
15 */
16 protected $slug = 'sc-dashboard';
17
18 /**
19 * Pages
20 *
21 * @var array
22 */
23 protected $pages = array();
24
25 /**
26 * Essential SureCart Pages
27 *
28 * @var array
29 */
30 const ESSENTIAL_PAGES = array(
31 'shop',
32 'checkout',
33 'dashboard',
34 );
35
36 /**
37 * Menu hidden pages.
38 *
39 * @var array
40 */
41 const MENU_CURRENT_OVERRIDES = array(
42 'sc-affiliate-payout-groups' => 'sc-affiliate-payouts',
43 );
44
45 /**
46 * Add menu items.
47 */
48 public function bootstrap() {
49 add_action( 'admin_menu', array( $this, 'registerAdminPages' ) );
50 add_action( 'admin_head', array( $this, 'adminMenuCSS' ) );
51 add_filter( 'parent_file', array( $this, 'forceSelect' ) );
52 add_filter( 'parent_file', array( $this, 'applyMenuOverrides' ) );
53 }
54
55 /**
56 * Make sure these menu items get selected.
57 *
58 * @param string $file The file string.
59 *
60 * @return string
61 */
62 public function forceSelect( $file ) {
63 global $submenu_file;
64 global $post;
65
66 if ( ! empty( $post->ID ) && in_array(
67 $post->ID,
68 array(
69 \SureCart::pages()->getId( 'cart', 'sc_cart' ),
70 \SureCart::pages()->getId( 'checkout' ),
71 \SureCart::pages()->getId( 'shop' ),
72 \SureCart::pages()->getId( 'dashboard' ),
73 )
74 ) ) {
75 $file = $this->slug;
76 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
77 $submenu_file = 'post.php?post=' . (int) $post->ID . '&action=edit';
78 }
79
80 // Check if we're editing a taxonomy that applies to sc_product post types.
81 $screen = get_current_screen();
82 $taxonomy = get_taxonomy( $screen->taxonomy );
83 if ( $screen && 'edit-tags' === $screen->base && in_array( 'sc_product', (array) $taxonomy->object_type, true ) ) {
84 $file = $this->slug;
85 $submenu_file = \SureCart::taxonomies()->editLink( $screen->taxonomy, 'sc_product' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
86 }
87
88 return $file;
89 }
90
91 /**
92 * Add some divider css.
93 *
94 * @return void
95 */
96 public function adminMenuCSS(): void {
97 echo '<style>
98 #toplevel_page_' . $this->slug . ' li {
99 clear: both;
100 }
101 #toplevel_page_' . $this->slug . ' li:not(:last-child) a:has(.sc-menu-divider):after {
102 border-bottom: 1px solid hsla(0,0%,100%,.2);
103 display: block;
104 float: left;
105 margin: 13px -15px 8px;
106 content: "";
107 width: calc(100% + 26px);
108 }
109 .sc-new-badge {
110 background: var(--wp-admin-theme-color, #007cba);
111 color: #ffffff;
112 font-size: 9px;
113 font-weight: 600;
114 letter-spacing: 0.5px;
115 width: 6px;
116 height: 6px;
117 margin-top: -2px;
118 margin-left: 6px;
119 border-radius: 8px;
120 text-transform: uppercase;
121 vertical-align: middle;
122 align-self: center;
123 display: inline-block;
124 }
125 </style>';
126 }
127
128 /**
129 * Get the menu slug.
130 *
131 * @return string
132 */
133 public function getMenuSlug() {
134 // Set the slug to the getting started page if the user has no API token.
135 if ( ! ApiToken::get() ) {
136 return 'sc-getting-started';
137 }
138
139 return $this->slug;
140 }
141
142 /**
143 * Register admin pages.
144 *
145 * @return void
146 */
147 public function registerAdminPages() {
148 // Get the menu slug.
149 $this->slug = $this->getMenuSlug();
150
151 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
152 \add_menu_page( __( 'Dashboard', 'surecart' ), __( 'SureCart', 'surecart' ), 'manage_sc_shop_settings', $this->slug, '__return_false', 'data:image/svg+xml;base64,' . base64_encode( file_get_contents( plugin_dir_path( SURECART_PLUGIN_FILE ) . 'images/icon.svg' ) ), apply_filters( 'surecart_menu_priority', 2.0001 ) );
153
154 // not yet installed.
155 if ( ! ApiToken::get() ) {
156 $this->pages = array(
157 'get-started' => \add_submenu_page( $this->slug, __( 'Get Started', 'surecart' ), __( 'Get Started', 'surecart' ), 'manage_options', $this->slug, '__return_false' ),
158 'complete-signup' => \add_submenu_page( '', __( 'Complete Signup', 'surecart' ), __( 'Complete Signup', 'surecart' ), 'manage_options', 'sc-complete-signup', '__return_false' ),
159 'settings' => \add_submenu_page( $this->slug, __( 'Settings', 'surecart' ), __( 'Settings', 'surecart' ), 'manage_options', 'sc-settings', '__return_false' ),
160 );
161 return;
162 }
163
164 $this->pages = array();
165
166 /**
167 * Dashboard
168 */
169 $this->pages += array(
170 'get-started' => \add_submenu_page( $this->slug, __( 'Dashboard', 'surecart' ), '<span class="sc-menu-divider">' . __( 'Dashboard', 'surecart' ) . '</span>', 'manage_sc_shop_settings', 'sc-dashboard', '__return_false' ),
171 'complete-signup' => \add_submenu_page( '', __( 'Complete Signup', 'surecart' ), __( 'Complete Signup', 'surecart' ), 'manage_options', 'sc-complete-signup', '__return_false' ),
172 'claim-account' => \add_submenu_page( '', __( 'Claim Account', 'surecart' ), __( 'Claim Account', 'surecart' ), 'manage_options', 'sc-claim-account', '__return_false' ),
173 );
174
175 /**
176 * Orders
177 */
178 $this->pages += array(
179 'orders' => \add_submenu_page( $this->slug, __( 'Orders', 'surecart' ), __( 'Orders', 'surecart' ), 'edit_sc_orders', 'sc-orders', '__return_false' ),
180 );
181 // Orders submenu pages.
182 if ( in_array( $_GET['page'] ?? '', [ 'sc-orders', 'sc-abandoned-checkouts', 'sc-invoices' ], true ) ) {
183 $this->pages += array(
184 'abandoned' => \add_submenu_page( $this->slug, __( 'Abandoned', 'surecart' ), '' . __( 'Abandoned', 'surecart' ), 'edit_sc_orders', 'sc-abandoned-checkouts', '__return_false' ),
185 'invoices' => \add_submenu_page( $this->slug, __( 'Invoices', 'surecart' ), '' . __( 'Invoices', 'surecart' ), 'edit_sc_invoices', 'sc-invoices', '__return_false' ),
186 );
187 }
188
189 /**
190 * Products
191 */
192 $this->pages += array(
193 'products' => \add_submenu_page( $this->slug, __( 'Products', 'surecart' ), __( 'Products', 'surecart' ), 'edit_sc_products', 'sc-products', '__return_false' ),
194 );
195
196 // Product submenu pages.
197 $taxonomies = array_diff( get_object_taxonomies( 'sc_product' ), array( 'sc_account', 'sc_collection' ) );
198 sort( $taxonomies, SORT_STRING ); // Sort the taxonomies alphabetically.
199 $is_product_menu_opened = in_array( $_GET['page'] ?? '', array( 'sc-products', 'sc-product-groups', 'sc-product-collections', 'sc-reviews' ), true ) || in_array( $_GET['taxonomy'] ?? '', array_merge( $taxonomies, array( 'sc_collection' ) ), true );
200 if ( $is_product_menu_opened ) {
201 $this->pages += array(
202 'product-collections' => \add_submenu_page( $this->slug, __( 'Product Collections', 'surecart' ), '' . __( 'Collections', 'surecart' ), 'edit_sc_products', 'sc-product-collections', '__return_false' ),
203 );
204 if ( ! empty( $taxonomies ) && is_array( $taxonomies ) ) {
205 $this->pages += array_map(
206 function ( $taxonomy ) {
207 if ( ! taxonomy_exists( $taxonomy ) ) {
208 return null;
209 }
210 $taxonomy_obj = get_taxonomy( $taxonomy );
211 return \add_submenu_page( $this->slug, $taxonomy_obj->label, '' . $taxonomy_obj->labels->menu_name ?? $taxonomy_obj->label, 'edit_sc_products', \SureCart::taxonomies()->editLink( $taxonomy_obj->name, 'sc_product' ), '' );
212 },
213 $taxonomies,
214 [ $is_product_menu_opened, $this->slug ]
215 );
216 }
217 $this->pages += array(
218 'product-groups' => \add_submenu_page( $this->slug, __( 'Upgrade Groups', 'surecart' ), '' . __( 'Upgrade Groups', 'surecart' ), 'edit_sc_products', 'sc-product-groups', '__return_false' ),
219 'reviews' => \add_submenu_page( $this->slug, __( 'Reviews', 'surecart' ), '' . __( 'Reviews', 'surecart' ) . '<span class="sc-new-badge"><span>', 'edit_sc_products', 'sc-reviews', '__return_false' ),
220 );
221 }
222
223 /**
224 * Promotions
225 */
226 $this->pages += array(
227 'promotion' => \add_submenu_page( $this->slug, __( 'Promotions', 'surecart' ), __( 'Promotions', 'surecart' ), 'edit_sc_coupons', 'sc-coupons', '__return_false' ),
228 );
229
230 // Promotions submenu pages.
231 $is_promotion_menu_opened = in_array( $_GET['page'] ?? '', array( 'sc-coupons', 'sc-bumps', 'sc-upsell-funnels', 'sc-auto-fees' ), true );
232
233 if ( $is_promotion_menu_opened ) {
234 /**
235 * Coupons
236 */
237 $this->pages += array(
238 'coupons' => \add_submenu_page( $this->slug, __( 'Coupons', 'surecart' ), '' . __( 'Coupons', 'surecart' ), 'edit_sc_coupons', 'sc-coupons', '__return_false' ),
239 'auto_fee' => \add_submenu_page( $this->slug, __( 'Dynamic Pricing', 'surecart' ), '' . __( 'Dynamic Pricing', 'surecart' ), 'edit_sc_products', 'sc-auto-fees', '__return_false' ),
240 'bumps' => \add_submenu_page( $this->slug, __( 'Order Bumps', 'surecart' ), '' . __( 'Order Bumps', 'surecart' ), 'edit_sc_products', 'sc-bumps', '__return_false' ),
241 'upsells' => \add_submenu_page( $this->slug, __( 'Upsells', 'surecart' ), '' . __( 'Upsells', 'surecart' ), 'edit_sc_products', 'sc-upsell-funnels', '__return_false' ),
242 );
243 }
244
245 /**
246 * Licenses
247 */
248 $this->pages += array(
249 'licenses' => \add_submenu_page( $this->slug, __( 'Licenses', 'surecart' ), __( 'Licenses', 'surecart' ), 'edit_sc_products', 'sc-licenses', '__return_false' ),
250 );
251
252 /**
253 * Subscriptions
254 */
255 $this->pages += array(
256 'subscriptions' => \add_submenu_page( $this->slug, __( 'Subscriptions', 'surecart' ), __( 'Subscriptions', 'surecart' ), 'edit_sc_subscriptions', 'sc-subscriptions', '__return_false' ),
257 );
258 if ( in_array( $_GET['page'] ?? '', array( 'sc-subscriptions', 'sc-cancellation-insights' ), true ) ) {
259 $this->pages += array(
260 'cancellations' => \add_submenu_page( $this->slug, __( 'Cancellation Insights', 'surecart' ), '' . __( 'Cancellations', 'surecart' ), 'edit_sc_subscriptions', 'sc-cancellation-insights', '__return_false' ),
261 );
262 }
263
264 $this->pages += array(
265 'affiliates' => \add_submenu_page( $this->slug, __( 'Affiliates', 'surecart' ), __( 'Affiliates', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliates', '__return_false' ),
266 );
267 if ( in_array( $_GET['page'] ?? '', array( 'sc-affiliates', 'sc-affiliate-requests', 'sc-affiliate-clicks', 'sc-affiliate-referrals', 'sc-affiliate-payouts', 'sc-affiliate-payout-groups' ), true ) ) {
268 $this->pages += array(
269 'affiliate-requests' => \add_submenu_page( $this->slug, __( 'Requests', 'surecart' ), '' . __( 'Requests', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliate-requests', '__return_false' ),
270 'affiliate-clicks' => \add_submenu_page( $this->slug, __( 'Clicks', 'surecart' ), '' . __( 'Clicks', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliate-clicks', '__return_false' ),
271 'affiliate-referrals' => \add_submenu_page( $this->slug, __( 'Referrals', 'surecart' ), '' . __( 'Referrals', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliate-referrals', '__return_false' ),
272 'affiliate-payouts' => \add_submenu_page( $this->slug, __( 'Payouts', 'surecart' ), '' . __( 'Payouts', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliate-payouts', '__return_false' ),
273 'affiliate-payout-groups' => \add_submenu_page( ' ', __( 'Payout Groups', 'surecart' ), '', 'edit_sc_affiliates', 'sc-affiliate-payout-groups', '__return_false' ),
274 );
275 }
276
277 /**
278 * Customers
279 */
280 $this->pages += array(
281 'customers' => \add_submenu_page( $this->slug, __( 'Customers', 'surecart' ), '<span class="sc-menu-divider">' . __( 'Customers', 'surecart' ) . '</span>', 'edit_sc_customers', 'sc-customers', '__return_false' ),
282 );
283
284 /**
285 * Restore
286 */
287 if ( 'sc-restore' === ( $_GET['page'] ?? '' ) ) {
288 $this->pages += array(
289 'restore' => \add_submenu_page( null, __( 'Restore', 'surecart' ), __( 'Restore', 'surecart' ), 'manage_options', 'sc-restore', '__return_false' ),
290 );
291 }
292
293 $this->pages += array(
294 'shop' => $this->getPage( 'shop', __( 'Shop', 'surecart' ) ),
295 'checkout' => $this->getPage( 'checkout', __( 'Checkout', 'surecart' ) ),
296 'cart' => $this->addTemplateSubMenuPage( 'cart', __( 'Cart', 'surecart' ), 'surecart/surecart//cart' ),
297 'dashboard' => $this->getPage( 'dashboard', __( 'Customer Area', 'surecart' ) ),
298 'forms' => \add_submenu_page( $this->slug, __( 'Forms', 'surecart' ), '<span class="sc-menu-divider">' . __( 'Custom Forms', 'surecart' ) . '</span>', 'manage_options', 'edit.php?post_type=sc_form', '' ),
299 'settings' => \add_submenu_page( $this->slug, __( 'Settings', 'surecart' ), __( 'Settings', 'surecart' ), 'manage_options', 'sc-settings', '__return_false' ),
300 'learn' => get_option( 'surecart_learn_admin_menu', true ) ? \add_submenu_page( $this->slug, __( 'Learn', 'surecart' ), __( 'Learn', 'surecart' ) . $this->getLearnBadge(), 'manage_options', 'sc-learn', '__return_false' ) : null,
301 );
302 }
303
304 /**
305 * Get the page link.
306 *
307 * @param string $slug The slug.
308 * @param string $name The name.
309 * @param string $post_type The post type.
310 *
311 * @return void
312 */
313 public function getPage( $slug, $name, $post_type = 'page', $divider = false ) {
314 // add filter to disable shop page menu item.
315 if ( ! get_option( 'surecart_' . $slug . '_admin_menu', true ) ) {
316 return;
317 }
318
319 $page_id = \SureCart::pages()->getId( $slug, $post_type );
320
321 $status = '';
322
323 $post_status = get_post_status( $page_id );
324 if ( 'publish' !== $post_status ) {
325 $status = '<span class="awaiting - mod">' . ( get_post_status_object( $post_status )->label ?? esc_html__( 'Deleted', 'surecart' ) ) . '</span>';
326 }
327
328 $label = $name . $status;
329 if ( $divider ) {
330 $label = '<span class="sc-menu-divider">' . $label . '</span>';
331 }
332
333 return \add_submenu_page( $this->slug, $name, $label, 'manage_options', $this->getSubMenuPageSlug( $slug, $page_id ), '' );
334 }
335
336 /**
337 * Get the page menu slug.
338 *
339 * @param string $slug The slug.
340 * @param int $page_id The page id.
341 */
342 public function getSubMenuPageSlug( $slug, $page_id ) {
343 // check if it is not an essential page.
344 if ( ! in_array( $slug, self::ESSENTIAL_PAGES, true ) ) {
345 return 'post.php?post=' . $page_id . '&action=edit';
346 }
347
348 $post_status = get_post_status( $page_id );
349
350 // check if the page is published.
351 if ( 'publish' === $post_status ) {
352 return 'post.php?post=' . $page_id . '&action=edit';
353 }
354
355 return 'admin.php?page=sc-restore&restore=' . $slug;
356 }
357
358 /**
359 * Add a submenu page for a template.
360 *
361 * @param string $slug The slug.
362 * @param string $name The name.
363 * @param string $template_slug The template slug.
364 *
365 * @return null|string|false
366 */
367 public function addTemplateSubMenuPage( $slug, $name, $template_slug ) {
368 // add filter to disable shop page menu item.
369 if ( ! get_option( 'surecart_' . $slug . '_admin_menu', true ) ) {
370 return;
371 }
372
373 return \add_submenu_page(
374 $this->slug,
375 $name,
376 $name,
377 'manage_options',
378 add_query_arg(
379 [
380 'postId' => rawurlencode( $template_slug ),
381 'postType' => 'wp_template_part',
382 'canvas' => 'edit',
383 ],
384 'site-editor.php',
385 ),
386 ''
387 );
388 }
389
390 /**
391 * Select menu item.
392 *
393 * @param string $file The file.
394 *
395 * @return string
396 */
397 public function applyMenuOverrides( $file ) {
398 global $plugin_page;
399
400 foreach ( self::MENU_CURRENT_OVERRIDES as $key => $value ) {
401 if ( $key === $plugin_page ) {
402 $plugin_page = $value; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
403 }
404 }
405
406 return $file;
407 }
408
409 /**
410 * Get the learn menu badge showing remaining steps count.
411 *
412 * Total is synced from JS via the surecart_learn_total_steps option.
413 *
414 * @return string
415 */
416 protected function getLearnBadge() {
417 $remaining = \SureCart::settings()->getLearnRemainingSteps();
418 return $remaining > 0
419 ? sprintf( ' <span class="awaiting-mod">%d</span>', $remaining )
420 : '';
421 }
422 }
423