PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.6.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.6.0
4.6.1 4.6.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 5 days ago AdminMenuPageServiceProvider.php 9 months ago AdminToolbarService.php 5 days ago ProductCollectionsMenuService.php 4 months ago
AdminMenuPageService.php
424 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-bundles', '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 'bundles' => \add_submenu_page( $this->slug, __( 'Bundles', 'surecart' ), '' . __( 'Bundles', 'surecart' ) . '<span class="sc-new-badge"></span>', 'edit_sc_products', 'sc-bundles', '__return_false' ),
203 'product-collections' => \add_submenu_page( $this->slug, __( 'Product Collections', 'surecart' ), '' . __( 'Collections', 'surecart' ), 'edit_sc_products', 'sc-product-collections', '__return_false' ),
204 );
205 if ( ! empty( $taxonomies ) && is_array( $taxonomies ) ) {
206 $this->pages += array_map(
207 function ( $taxonomy ) {
208 if ( ! taxonomy_exists( $taxonomy ) ) {
209 return null;
210 }
211 $taxonomy_obj = get_taxonomy( $taxonomy );
212 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' ), '' );
213 },
214 $taxonomies,
215 [ $is_product_menu_opened, $this->slug ]
216 );
217 }
218 $this->pages += array(
219 'product-groups' => \add_submenu_page( $this->slug, __( 'Upgrade Groups', 'surecart' ), '' . __( 'Upgrade Groups', 'surecart' ), 'edit_sc_products', 'sc-product-groups', '__return_false' ),
220 'reviews' => \add_submenu_page( $this->slug, __( 'Reviews', 'surecart' ), '' . __( 'Reviews', 'surecart' ), 'edit_sc_products', 'sc-reviews', '__return_false' ),
221 );
222 }
223
224 /**
225 * Promotions
226 */
227 $this->pages += array(
228 'promotion' => \add_submenu_page( $this->slug, __( 'Promotions', 'surecart' ), __( 'Promotions', 'surecart' ), 'edit_sc_coupons', 'sc-coupons', '__return_false' ),
229 );
230
231 // Promotions submenu pages.
232 $is_promotion_menu_opened = in_array( $_GET['page'] ?? '', array( 'sc-coupons', 'sc-bumps', 'sc-upsell-funnels', 'sc-auto-fees' ), true );
233
234 if ( $is_promotion_menu_opened ) {
235 /**
236 * Coupons
237 */
238 $this->pages += array(
239 'coupons' => \add_submenu_page( $this->slug, __( 'Coupons', 'surecart' ), '' . __( 'Coupons', 'surecart' ), 'edit_sc_coupons', 'sc-coupons', '__return_false' ),
240 'auto_fee' => \add_submenu_page( $this->slug, __( 'Dynamic Pricing', 'surecart' ), '' . __( 'Dynamic Pricing', 'surecart' ), 'edit_sc_products', 'sc-auto-fees', '__return_false' ),
241 'bumps' => \add_submenu_page( $this->slug, __( 'Order Bumps', 'surecart' ), '' . __( 'Order Bumps', 'surecart' ), 'edit_sc_products', 'sc-bumps', '__return_false' ),
242 'upsells' => \add_submenu_page( $this->slug, __( 'Upsells', 'surecart' ), '' . __( 'Upsells', 'surecart' ), 'edit_sc_products', 'sc-upsell-funnels', '__return_false' ),
243 );
244 }
245
246 /**
247 * Licenses
248 */
249 $this->pages += array(
250 'licenses' => \add_submenu_page( $this->slug, __( 'Licenses', 'surecart' ), __( 'Licenses', 'surecart' ), 'edit_sc_products', 'sc-licenses', '__return_false' ),
251 );
252
253 /**
254 * Subscriptions
255 */
256 $this->pages += array(
257 'subscriptions' => \add_submenu_page( $this->slug, __( 'Subscriptions', 'surecart' ), __( 'Subscriptions', 'surecart' ), 'edit_sc_subscriptions', 'sc-subscriptions', '__return_false' ),
258 );
259 if ( in_array( $_GET['page'] ?? '', array( 'sc-subscriptions', 'sc-cancellation-insights' ), true ) ) {
260 $this->pages += array(
261 'cancellations' => \add_submenu_page( $this->slug, __( 'Cancellation Insights', 'surecart' ), '' . __( 'Cancellations', 'surecart' ), 'edit_sc_subscriptions', 'sc-cancellation-insights', '__return_false' ),
262 );
263 }
264
265 $this->pages += array(
266 'affiliates' => \add_submenu_page( $this->slug, __( 'Affiliates', 'surecart' ), __( 'Affiliates', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliates', '__return_false' ),
267 );
268 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 ) ) {
269 $this->pages += array(
270 'affiliate-requests' => \add_submenu_page( $this->slug, __( 'Requests', 'surecart' ), '' . __( 'Requests', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliate-requests', '__return_false' ),
271 'affiliate-clicks' => \add_submenu_page( $this->slug, __( 'Clicks', 'surecart' ), '' . __( 'Clicks', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliate-clicks', '__return_false' ),
272 'affiliate-referrals' => \add_submenu_page( $this->slug, __( 'Referrals', 'surecart' ), '' . __( 'Referrals', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliate-referrals', '__return_false' ),
273 'affiliate-payouts' => \add_submenu_page( $this->slug, __( 'Payouts', 'surecart' ), '' . __( 'Payouts', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliate-payouts', '__return_false' ),
274 'affiliate-payout-groups' => \add_submenu_page( ' ', __( 'Payout Groups', 'surecart' ), '', 'edit_sc_affiliates', 'sc-affiliate-payout-groups', '__return_false' ),
275 );
276 }
277
278 /**
279 * Customers
280 */
281 $this->pages += array(
282 'customers' => \add_submenu_page( $this->slug, __( 'Customers', 'surecart' ), '<span class="sc-menu-divider">' . __( 'Customers', 'surecart' ) . '</span>', 'edit_sc_customers', 'sc-customers', '__return_false' ),
283 );
284
285 /**
286 * Restore
287 */
288 if ( 'sc-restore' === ( $_GET['page'] ?? '' ) ) {
289 $this->pages += array(
290 'restore' => \add_submenu_page( null, __( 'Restore', 'surecart' ), __( 'Restore', 'surecart' ), 'manage_options', 'sc-restore', '__return_false' ),
291 );
292 }
293
294 $this->pages += array(
295 'shop' => $this->getPage( 'shop', __( 'Shop', 'surecart' ) ),
296 'checkout' => $this->getPage( 'checkout', __( 'Checkout', 'surecart' ) ),
297 'cart' => $this->addTemplateSubMenuPage( 'cart', __( 'Cart', 'surecart' ), 'surecart/surecart//cart' ),
298 'dashboard' => $this->getPage( 'dashboard', __( 'Customer Area', 'surecart' ) ),
299 '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', '' ),
300 'settings' => \add_submenu_page( $this->slug, __( 'Settings', 'surecart' ), __( 'Settings', 'surecart' ), 'manage_options', 'sc-settings', '__return_false' ),
301 '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,
302 );
303 }
304
305 /**
306 * Get the page link.
307 *
308 * @param string $slug The slug.
309 * @param string $name The name.
310 * @param string $post_type The post type.
311 *
312 * @return void
313 */
314 public function getPage( $slug, $name, $post_type = 'page', $divider = false ) {
315 // add filter to disable shop page menu item.
316 if ( ! get_option( 'surecart_' . $slug . '_admin_menu', true ) ) {
317 return;
318 }
319
320 $page_id = \SureCart::pages()->getId( $slug, $post_type );
321
322 $status = '';
323
324 $post_status = get_post_status( $page_id );
325 if ( 'publish' !== $post_status ) {
326 $status = '<span class="awaiting - mod">' . ( get_post_status_object( $post_status )->label ?? esc_html__( 'Deleted', 'surecart' ) ) . '</span>';
327 }
328
329 $label = $name . $status;
330 if ( $divider ) {
331 $label = '<span class="sc-menu-divider">' . $label . '</span>';
332 }
333
334 return \add_submenu_page( $this->slug, $name, $label, 'manage_options', $this->getSubMenuPageSlug( $slug, $page_id ), '' );
335 }
336
337 /**
338 * Get the page menu slug.
339 *
340 * @param string $slug The slug.
341 * @param int $page_id The page id.
342 */
343 public function getSubMenuPageSlug( $slug, $page_id ) {
344 // check if it is not an essential page.
345 if ( ! in_array( $slug, self::ESSENTIAL_PAGES, true ) ) {
346 return 'post.php?post=' . $page_id . '&action=edit';
347 }
348
349 $post_status = get_post_status( $page_id );
350
351 // check if the page is published.
352 if ( 'publish' === $post_status ) {
353 return 'post.php?post=' . $page_id . '&action=edit';
354 }
355
356 return 'admin.php?page=sc-restore&restore=' . $slug;
357 }
358
359 /**
360 * Add a submenu page for a template.
361 *
362 * @param string $slug The slug.
363 * @param string $name The name.
364 * @param string $template_slug The template slug.
365 *
366 * @return null|string|false
367 */
368 public function addTemplateSubMenuPage( $slug, $name, $template_slug ) {
369 // add filter to disable shop page menu item.
370 if ( ! get_option( 'surecart_' . $slug . '_admin_menu', true ) ) {
371 return;
372 }
373
374 return \add_submenu_page(
375 $this->slug,
376 $name,
377 $name,
378 'manage_options',
379 add_query_arg(
380 [
381 'postId' => rawurlencode( $template_slug ),
382 'postType' => 'wp_template_part',
383 'canvas' => 'edit',
384 ],
385 'site-editor.php',
386 ),
387 ''
388 );
389 }
390
391 /**
392 * Select menu item.
393 *
394 * @param string $file The file.
395 *
396 * @return string
397 */
398 public function applyMenuOverrides( $file ) {
399 global $plugin_page;
400
401 foreach ( self::MENU_CURRENT_OVERRIDES as $key => $value ) {
402 if ( $key === $plugin_page ) {
403 $plugin_page = $value; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
404 }
405 }
406
407 return $file;
408 }
409
410 /**
411 * Get the learn menu badge showing remaining steps count.
412 *
413 * Total is synced from JS via the surecart_learn_total_steps option.
414 *
415 * @return string
416 */
417 protected function getLearnBadge() {
418 $remaining = \SureCart::settings()->getLearnRemainingSteps();
419 return $remaining > 0
420 ? sprintf( ' <span class="awaiting-mod">%d</span>', $remaining )
421 : '';
422 }
423 }
424