PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.0.0-beta1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.0.0-beta1
4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 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 2 years ago AdminMenuPageServiceProvider.php 2 years ago ProductCollectionsMenuService.php 2 years ago
AdminMenuPageService.php
318 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 // Admin bar menus.
55 if ( apply_filters( 'surecart_show_admin_bar_visit_store', true ) ) {
56 add_action( 'admin_bar_menu', array( $this, 'adminBarMenu' ), 31 );
57 }
58 }
59
60 /**
61 * Add the "Visit Store" link in admin bar main menu.
62 *
63 * @since 2.4.0
64 * @param WP_Admin_Bar $wp_admin_bar Admin bar instance.
65 */
66 public function adminBarMenu( $wp_admin_bar ) {
67 if ( ! is_admin() || ! is_admin_bar_showing() ) {
68 return;
69 }
70
71 // Show only when the user is a member of this site, or they're a super admin.
72 if ( ! is_user_member_of_blog() && ! is_super_admin() ) {
73 return;
74 }
75
76 // Don't display when shop page is the same of the page on front.
77 if ( intval( get_option( 'page_on_front' ) ) === \SureCart::pages()->getId( 'shop' ) ) {
78 return;
79 }
80
81 // Add an option to visit the store.
82 $wp_admin_bar->add_node(
83 array(
84 'parent' => 'site-name',
85 'id' => 'view-sc-store',
86 'title' => class_exists( 'WooCommerce' ) ? __( 'Visit SureCart Store', 'surecart' ) : __( 'Visit Store', 'surecart' ),
87 'href' => \SureCart::pages()->url( 'shop' ),
88 )
89 );
90 }
91
92 /**
93 * Make sure these menu items get selected.
94 *
95 * @param string $file The file string.
96 *
97 * @return string
98 */
99 public function forceSelect( $file ) {
100 global $submenu_file;
101 global $post;
102
103 if ( ! empty( $post->ID ) && in_array(
104 $post->ID,
105 array(
106 \SureCart::pages()->getId( 'cart', 'sc_cart' ),
107 \SureCart::pages()->getId( 'checkout' ),
108 \SureCart::pages()->getId( 'shop' ),
109 \SureCart::pages()->getId( 'dashboard' ),
110 )
111 ) ) {
112 $file = 'sc-dashboard';
113 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
114 $submenu_file = 'post.php?post=' . (int) $post->ID . '&action=edit';
115 }
116
117 return $file;
118 }
119
120 /**
121 * Add some divider css.
122 *
123 * @return void
124 */
125 public function adminMenuCSS(): void {
126 echo '<style>
127 #toplevel_page_sc-dashboard li {
128 clear: both;
129 }
130 #toplevel_page_sc-dashboard li:not(:last-child) a[href^="admin.php?page=sc-customers"]:after {
131 border-bottom: 1px solid hsla(0,0%,100%,.2);
132 display: block;
133 float: left;
134 margin: 13px -15px 8px;
135 content: "";
136 width: calc(100% + 26px);
137 }
138 #toplevel_page_sc-dashboard li:not(:last-child) a[href^="admin.php?page=sc-dashboard"]:after {
139 border-bottom: 1px solid hsla(0,0%,100%,.2);
140 display: block;
141 float: left;
142 margin: 13px -15px 8px;
143 content: "";
144 width: calc(100% + 26px);
145 }
146 #toplevel_page_sc-dashboard li:not(:last-child) a[href^="edit.php?post_type=sc_form"]:after {
147 border-bottom: 1px solid hsla(0,0%,100%,.2);
148 display: block;
149 float: left;
150 margin: 13px -15px 8px;
151 content: "";
152 width: calc(100% + 26px);
153 }
154 </style>';
155 }
156
157 /**
158 * Register admin pages.
159 *
160 * @return void
161 */
162 public function registerAdminPages() {
163 $entitlements = \SureCart::account()->entitlements;
164 if ( ! ApiToken::get() ) {
165 $this->slug = 'sc-getting-started';
166 }
167
168 $logo = file_get_contents( plugin_dir_path( SURECART_PLUGIN_FILE ) . 'images/icon.svg' );
169 \add_menu_page( __( 'Dashboard', 'surecart' ), __( 'SureCart', 'surecart' ), 'manage_sc_shop_settings', $this->slug, '__return_false', 'data:image/svg+xml;base64,' . base64_encode( $logo ), 30.6001 );
170
171 // not yet installed.
172 if ( ! ApiToken::get() ) {
173 $this->pages = array(
174 'get-started' => \add_submenu_page( $this->slug, __( 'Get Started', 'surecart' ), __( 'Get Started', 'surecart' ), 'manage_options', $this->slug, '__return_false' ),
175 'complete-signup' => \add_submenu_page( '', __( 'Complete Signup', 'surecart' ), __( 'Complete Signup', 'surecart' ), 'manage_options', 'sc-complete-signup', '__return_false' ),
176 'settings' => \add_submenu_page( $this->slug, __( 'Settings', 'surecart' ), __( 'Settings', 'surecart' ), 'manage_options', 'sc-settings', '__return_false' ),
177 );
178 return;
179 }
180
181 $affiliate_pages = array( 'sc-affiliates', 'sc-affiliate-requests', 'sc-affiliate-clicks', 'sc-affiliate-referrals', 'sc-affiliate-payouts', 'sc-affiliate-payout-groups' );
182
183 $is_product_menu_opened = in_array( $_GET['page'] ?? '', array( 'sc-products', 'sc-product-groups', 'sc-bumps', 'sc-upsell-funnels', 'sc-product-collections' ), true ) || in_array( $_GET['taxonomy'] ?? '', array( 'sc_collection' ), true );
184
185 $this->pages = array(
186 'get-started' => \add_submenu_page( $this->slug, __( 'Dashboard', 'surecart' ), __( 'Dashboard', 'surecart' ), 'manage_sc_shop_settings', $this->slug, '__return_false' ),
187 'complete-signup' => \add_submenu_page( '', __( 'Complete Signup', 'surecart' ), __( 'Complete Signup', 'surecart' ), 'manage_options', 'sc-complete-signup', '__return_false' ),
188 'claim-account' => \add_submenu_page( '', __( 'Claim Account', 'surecart' ), __( 'Claim Account', 'surecart' ), 'manage_options', 'sc-claim-account', '__return_false' ),
189 'orders' => \add_submenu_page( $this->slug, __( 'Orders', 'surecart' ), __( 'Orders', 'surecart' ), 'edit_sc_orders', 'sc-orders', '__return_false' ),
190 'checkouts' => in_array( $_GET['page'] ?? '', array( 'sc-checkouts' ) ) ? \add_submenu_page( $this->slug, __( 'Checkouts', 'surecart' ), '' . __( 'Add New', 'surecart' ), 'edit_sc_orders', 'sc-checkouts', '__return_false' ) : null,
191 'abandoned' => in_array( $_GET['page'] ?? '', array( 'sc-orders', 'sc-abandoned-checkouts', 'sc-checkouts' ), true ) ? \add_submenu_page( $this->slug, __( 'Abandoned', 'surecart' ), '' . __( 'Abandoned', 'surecart' ), 'edit_sc_orders', 'sc-abandoned-checkouts', '__return_false' ) : null,
192 'products' => \add_submenu_page( $this->slug, __( 'Products', 'surecart' ), __( 'Products', 'surecart' ), 'edit_sc_products', 'sc-products', '__return_false' ),
193 'product-collections' => $is_product_menu_opened ? \add_submenu_page( $this->slug, __( 'Product Collections', 'surecart' ), '' . __( 'Collections', 'surecart' ), 'edit_sc_products', 'sc-product-collections', '__return_false' ) : null,
194 'bumps' => $is_product_menu_opened ? \add_submenu_page( $this->slug, __( 'Order Bumps', 'surecart' ), '' . __( 'Order Bumps', 'surecart' ), 'edit_sc_products', 'sc-bumps', '__return_false' ) : null,
195 'upsells' => $is_product_menu_opened ? \add_submenu_page( $this->slug, __( 'Upsells', 'surecart' ), '' . __( 'Upsells', 'surecart' ) . '<span class="awaiting-mod" style="background-color: rgba(255,255,255,0.1);">' . esc_html__( 'Beta', 'surecart' ) . '</span>', 'edit_sc_products', 'sc-upsell-funnels', '__return_false' ) : null,
196 'product-groups' => $is_product_menu_opened ? \add_submenu_page( $this->slug, __( 'Upgrade Groups', 'surecart' ), '' . __( 'Upgrade Groups', 'surecart' ), 'edit_sc_products', 'sc-product-groups', '__return_false' ) : null,
197 'coupons' => \add_submenu_page( $this->slug, __( 'Coupons', 'surecart' ), __( 'Coupons', 'surecart' ), 'edit_sc_coupons', 'sc-coupons', '__return_false' ),
198 'licenses' => ! empty( $entitlements->licensing ) ? \add_submenu_page( $this->slug, __( 'Licenses', 'surecart' ), __( 'Licenses', 'surecart' ), 'edit_sc_products', 'sc-licenses', '__return_false' ) : null,
199 'subscriptions' => \add_submenu_page( $this->slug, __( 'Subscriptions', 'surecart' ), __( 'Subscriptions', 'surecart' ), 'edit_sc_subscriptions', 'sc-subscriptions', '__return_false' ),
200 'cancellations' => in_array( $_GET['page'] ?? '', array( 'sc-subscriptions', 'sc-cancellation-insights' ) ) ? \add_submenu_page( $this->slug, __( 'Cancellation Insights', 'surecart' ), '' . __( 'Cancellations', 'surecart' ), 'edit_sc_subscriptions', 'sc-cancellation-insights', '__return_false' ) : null,
201 'affiliates' => \add_submenu_page( $this->slug, __( 'Affiliates', 'surecart' ), __( 'Affiliates', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliates', '__return_false' ),
202 'affiliate-requests' => in_array( $_GET['page'] ?? '', $affiliate_pages, true ) ? \add_submenu_page( $this->slug, __( 'Requests', 'surecart' ), '' . __( 'Requests', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliate-requests', '__return_false' ) : null,
203 'affiliate-clicks' => in_array( $_GET['page'] ?? '', $affiliate_pages, true ) ? \add_submenu_page( $this->slug, __( 'Clicks', 'surecart' ), '' . __( 'Clicks', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliate-clicks', '__return_false' ) : null,
204 'affiliate-referrals' => in_array( $_GET['page'] ?? '', $affiliate_pages, true ) ? \add_submenu_page( $this->slug, __( 'Referrals', 'surecart' ), '' . __( 'Referrals', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliate-referrals', '__return_false' ) : null,
205 'affiliate-payouts' => in_array( $_GET['page'] ?? '', $affiliate_pages, true ) ? \add_submenu_page( $this->slug, __( 'Payouts', 'surecart' ), '' . __( 'Payouts', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliate-payouts', '__return_false' ) : null,
206 'affiliate-payout-groups' => in_array( $_GET['page'] ?? '', $affiliate_pages, true ) ? \add_submenu_page( ' ', __( 'Payout Groups', 'surecart' ), '', 'edit_sc_affiliates', 'sc-affiliate-payout-groups', '__return_false' ) : null,
207 'customers' => \add_submenu_page( $this->slug, __( 'Customers', 'surecart' ), __( 'Customers', 'surecart' ), 'edit_sc_customers', 'sc-customers', '__return_false' ),
208 'restore' => 'sc-restore' === ( $_GET['page'] ?? '' ) ? \add_submenu_page( null, __( 'Restore', 'surecart' ), __( 'Restore', 'surecart' ), 'manage_options', 'sc-restore', '__return_false' ) : null,
209 'shop' => $this->getPage( 'shop', __( 'Shop', 'surecart' ) ),
210 'checkout' => $this->getPage( 'checkout', __( 'Checkout', 'surecart' ) ),
211 'cart' => $this->addTemplateSubMenuPage( 'cart', __( 'Cart', 'surecart' ), 'surecart/surecart//cart' ),
212 'dashboard' => $this->getPage( 'dashboard', __( 'Customer Area', 'surecart' ) ),
213 'forms' => \add_submenu_page( $this->slug, __( 'Forms', 'surecart' ), __( 'Custom Forms', 'surecart' ), 'edit_posts', 'edit.php?post_type=sc_form', '' ),
214 'settings' => \add_submenu_page( $this->slug, __( 'Settings', 'surecart' ), __( 'Settings', 'surecart' ), 'manage_options', 'sc-settings', '__return_false' ),
215 );
216 }
217
218 /**
219 * Get the page link.
220 *
221 * @param string $slug The slug.
222 * @param string $name The name.
223 * @param string $post_type The post type.
224 *
225 * @return void
226 */
227 public function getPage( $slug, $name, $post_type = 'page' ) {
228 // add filter to disable shop page menu item.
229 if ( ! get_option( 'surecart_' . $slug . '_admin_menu', true ) ) {
230 return;
231 }
232
233 $page_id = \SureCart::pages()->getId( $slug, $post_type );
234
235 $status = '';
236
237 $post_status = get_post_status( $page_id );
238 if ( 'publish' !== $post_status ) {
239 $status = '<span class="awaiting-mod">' . ( get_post_status_object( $post_status )->label ?? esc_html__( 'Deleted', 'surecart' ) ) . '</span>';
240 }
241
242 return \add_submenu_page( $this->slug, $name, $name . $status, 'manage_options', $this->getSubMenuPageSlug( $slug, $page_id ), '' );
243 }
244
245 /**
246 * Get the page menu slug.
247 *
248 * @param string $slug The slug.
249 * @param int $page_id The page id.
250 */
251 public function getSubMenuPageSlug( $slug, $page_id ) {
252 // check if it is not an essential page.
253 if ( ! in_array( $slug, self::ESSENTIAL_PAGES, true ) ) {
254 return 'post.php?post=' . $page_id . '&action=edit';
255 }
256
257 $post_status = get_post_status( $page_id );
258
259 // check if the page is published.
260 if ( 'publish' === $post_status ) {
261 return 'post.php?post=' . $page_id . '&action=edit';
262 }
263
264 return 'admin.php?page=sc-restore&restore=' . $slug;
265 }
266
267 /**
268 * Add a submenu page for a template.
269 *
270 * @param string $slug The slug.
271 * @param string $name The name.
272 * @param string $template_slug The template slug.
273 *
274 * @return null|string|false
275 */
276 public function addTemplateSubMenuPage( $slug, $name, $template_slug ) {
277 // add filter to disable shop page menu item.
278 if ( ! get_option( 'surecart_' . $slug . '_admin_menu', true ) ) {
279 return;
280 }
281
282 return \add_submenu_page(
283 $this->slug,
284 $name,
285 $name,
286 'manage_options',
287 add_query_arg(
288 [
289 'postId' => rawurlencode( $template_slug ),
290 'postType' => 'wp_template_part',
291 'canvas' => 'edit',
292 ],
293 'site-editor.php',
294 ),
295 ''
296 );
297 }
298
299 /**
300 * Select menu item.
301 *
302 * @param string $file The file.
303 *
304 * @return string
305 */
306 public function applyMenuOverrides( $file ) {
307 global $plugin_page;
308
309 foreach ( self::MENU_CURRENT_OVERRIDES as $key => $value ) {
310 if ( $key === $plugin_page ) {
311 $plugin_page = $value; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
312 }
313 }
314
315 return $file;
316 }
317 }
318