PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.5.1
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.5.1
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 / AdminToolbarService.php
surecart / app / src / WordPress / Admin / Menus Last commit date
AdminMenuPageService.php 3 months ago AdminMenuPageServiceProvider.php 9 months ago AdminToolbarService.php 9 months ago ProductCollectionsMenuService.php 4 months ago
AdminToolbarService.php
412 lines
1 <?php
2
3 namespace SureCart\WordPress\Admin\Menus;
4
5 use SureCart\Models\ApiToken;
6
7 /**
8 * Handles the admin toolbar menus and items.
9 */
10 class AdminToolbarService {
11 /**
12 * Application instance.
13 *
14 * @var \SureCartCore\Application\Application
15 */
16 protected $app = null;
17
18 /**
19 * Constructor.
20 *
21 * @param \SureCartCore\Application\Application $app Application instance.
22 */
23 public function __construct( $app ) {
24 $this->app = $app;
25 }
26
27 /**
28 * Bootstrap the service and hook into WordPress.
29 *
30 * @return void
31 */
32 public function bootstrap() {
33 // Admin bar site menu.
34 if ( apply_filters( 'surecart_show_admin_bar_visit_store', true ) ) {
35 add_action( 'admin_bar_menu', array( $this, 'adminBarSiteMenu' ), 31 );
36 }
37
38 // Admin toolbar new content menu.
39 if ( apply_filters( 'surecart_show_admin_bar_new_content', true ) ) {
40 add_action( 'admin_bar_menu', array( $this, 'adminBarNewContent' ), 71 );
41 }
42
43 // Admin toolbar SureCart menu.
44 if ( $this->isEnabled() ) {
45 add_action( 'admin_bar_menu', array( $this, 'adminBarMenu' ), 99 );
46 }
47 }
48
49 /**
50 * Check if the admin toolbar is enabled.
51 *
52 * @return bool
53 */
54 public function isEnabled(): bool {
55 return ! get_option( 'surecart_admin_toolbar_disabled', false );
56 }
57
58 /**
59 * Check if the current user is in the admin area.
60 *
61 * @return bool
62 */
63 public function isAdmin(): bool {
64 return is_admin();
65 }
66
67 /**
68 * Check if the admin bar is showing.
69 *
70 * @return bool
71 */
72 public function isAdminBarShowing(): bool {
73 return is_admin_bar_showing();
74 }
75
76 /**
77 * Check if the current page is the admin area.
78 *
79 * @return bool
80 */
81 public function isShopPageOnFront(): bool {
82 return intval( get_option( 'page_on_front' ) ) === \SureCart::pages()->getId( 'shop' );
83 }
84
85 /**
86 * Check if the admin menu should be shown.
87 *
88 * @return bool
89 */
90 public function showAdminMenu(): bool {
91 if ( ! $this->isAdmin() || ! $this->isAdminBarShowing() ) {
92 return false;
93 }
94
95 // Show only when the user is a member of this site, or they're a super admin.
96 if ( ! $this->isBlogMember() ) {
97 return false;
98 }
99
100 // Don't display when shop page is the same of the page on front.
101 if ( $this->isShopPageOnFront() ) {
102 return false;
103 }
104
105 return true;
106 }
107
108 /**
109 * Check if the user is a member of the site.
110 *
111 * @return bool
112 */
113 public function isBlogMember(): bool {
114 return is_user_member_of_blog() || is_super_admin();
115 }
116
117 /**
118 * Add the "Visit Store" link in admin bar main menu.
119 *
120 * @since 2.4.0
121 * @param \WP_Admin_Bar $wp_admin_bar Admin bar instance.
122 */
123 public function adminBarSiteMenu( $wp_admin_bar ) {
124 if ( ! $this->showAdminMenu() ) {
125 return;
126 }
127
128 // Add an option to visit the store.
129 $wp_admin_bar->add_node(
130 array(
131 'parent' => 'site-name',
132 'id' => 'view-sc-store',
133 'title' => class_exists( 'WooCommerce' ) ? __( 'Visit SureCart Store', 'surecart' ) : __( 'Visit Store', 'surecart' ),
134 'href' => \SureCart::pages()->url( 'shop' ),
135 )
136 );
137 }
138
139 /**
140 * Add SureCart admin page links to the admin bar "+ New" menu.
141 *
142 * @param \WP_Admin_Bar $wp_admin_bar Admin bar instance.
143 */
144 public function adminBarNewContent( $wp_admin_bar ) {
145 // Show only when the user is a member of this site, or they're a super admin.
146 if ( ! $this->isBlogMember() ) {
147 return;
148 }
149
150 // Only show if user has API token connected.
151 if ( ! \SureCart::account()->isConnected() ) {
152 return;
153 }
154
155 $woocommerce_exists = class_exists( 'WooCommerce' );
156
157 if ( current_user_can( 'edit_sc_products' ) ) {
158 // Add Product link.
159 $wp_admin_bar->add_node(
160 array(
161 'parent' => 'new-content',
162 'id' => 'new-sc-product',
163 'title' => $woocommerce_exists ? __( 'SureCart Product', 'surecart' ) : __( 'Product', 'surecart' ),
164 'href' => esc_url( admin_url( 'admin.php?page=sc-products&action=edit' ) ),
165 )
166 );
167
168 // Add Product Collection link.
169 $wp_admin_bar->add_node(
170 array(
171 'parent' => 'new-sc-product',
172 'id' => 'new-sc-collection',
173 'title' => $woocommerce_exists ? __( 'SureCart Collection', 'surecart' ) : __( 'Collection', 'surecart' ),
174 'href' => esc_url( admin_url( 'admin.php?page=sc-product-collections&action=edit' ) ),
175 )
176 );
177
178 // Add Order Bump link.
179 $wp_admin_bar->add_node(
180 array(
181 'parent' => 'new-sc-product',
182 'id' => 'new-sc-bump',
183 'title' => $woocommerce_exists ? __( 'SureCart Order Bump', 'surecart' ) : __( 'Order Bump', 'surecart' ),
184 'href' => esc_url( admin_url( 'admin.php?page=sc-bumps&action=edit' ) ),
185 )
186 );
187
188 // Add Upsell link.
189 $wp_admin_bar->add_node(
190 array(
191 'parent' => 'new-sc-product',
192 'id' => 'new-sc-upsell',
193 'title' => $woocommerce_exists ? __( 'SureCart Upsell', 'surecart' ) : __( 'Upsell', 'surecart' ),
194 'href' => esc_url( admin_url( 'admin.php?page=sc-upsell-funnels&action=edit' ) ),
195 )
196 );
197 }
198
199 // Add Coupon link.
200 if ( current_user_can( 'edit_sc_coupons' ) ) {
201 $wp_admin_bar->add_node(
202 array(
203 'parent' => 'new-content',
204 'id' => 'new-sc-coupon',
205 'title' => $woocommerce_exists ? __( 'SureCart Coupon', 'surecart' ) : __( 'Coupon', 'surecart' ),
206 'href' => esc_url( admin_url( 'admin.php?page=sc-coupons&action=edit' ) ),
207 )
208 );
209 }
210
211 // Add Invoice link.
212 if ( current_user_can( 'edit_sc_invoices' ) ) {
213 $wp_admin_bar->add_node(
214 array(
215 'parent' => 'new-content',
216 'id' => 'new-sc-invoice',
217 'title' => $woocommerce_exists ? __( 'SureCart Invoice', 'surecart' ) : __( 'Invoice', 'surecart' ),
218 'href' => esc_url( \SureCart::getUrl()->create( 'invoices' ) ),
219 )
220 );
221 }
222 }
223
224 /**
225 * Add SureCart main menu to the admin bar.
226 *
227 * @param \WP_Admin_Bar $wp_admin_bar Admin bar instance.
228 *
229 * @return void
230 */
231 public function adminBarMenu( $wp_admin_bar ) {
232 // Its for frontend only.
233 if ( ! is_admin_bar_showing() || is_admin() ) {
234 return;
235 }
236
237 // Only show on single product edit screen and user can edit products.
238 if ( ! is_singular( 'sc_product' ) || ! current_user_can( 'edit_sc_products' ) ) {
239 return;
240 }
241
242 // Only show if user has API token and product is not empty.
243 if ( ! ApiToken::get() || empty( sc_get_product() ) ) {
244 return;
245 }
246
247 $surecart_logo = esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'images/icon.svg' );
248 ?>
249
250 <style>
251 #wp-admin-bar-surecart-toolbar > .ab-item {
252 display: flex !important;
253 align-items: center;
254 }
255
256 #wp-admin-bar-surecart-toolbar > .ab-item::before {
257 content: "";
258 display: inline-block;
259 width: 18px;
260 height: 18px;
261 background-color: #a7aaad !important; /* WordPress toolbar like icon color */
262 -webkit-mask: url('<?php echo esc_url( $surecart_logo ); ?>') no-repeat center;
263 mask: url('<?php echo esc_url( $surecart_logo ); ?>') no-repeat center;
264 -webkit-mask-size: contain;
265 mask-size: contain;
266 -webkit-mask-size: 18px;
267 mask-size: 18px;
268 }
269 </style>
270
271 <?php
272 $product = sc_get_product();
273 $wp_admin_bar->add_node(
274 [
275 'id' => 'surecart-toolbar',
276 'title' => __( 'Edit Product', 'surecart' ),
277 'href' => '#',
278 ]
279 );
280
281 // Add product edit link.
282 $wp_admin_bar->add_node(
283 [
284 'id' => 'surecart-edit-product',
285 'parent' => 'surecart-toolbar',
286 'title' => 'Product Details',
287 'href' => \SureCart::getUrl()->edit( 'product', $product->id ),
288 ]
289 );
290
291 // Add secondary group
292 $wp_admin_bar->add_group(
293 array(
294 'id' => 'surecart-design',
295 'parent' => 'surecart-toolbar',
296 'meta' => array( 'class' => 'ab-sub-secondary' ),
297 )
298 );
299
300 $this->renderProductContent( $wp_admin_bar );
301 $this->renderProductTemplate( $wp_admin_bar );
302 $this->maybeAddStickyPurchaseTemplate( $wp_admin_bar );
303 }
304
305 /**
306 * Check if the current page is rendered with blocks.
307 *
308 * @return bool
309 */
310 public function isRenderedWithBlocks(): bool {
311 return ! $this->isRenderedWithBricks() && ! $this->isRenderedWithElementor();
312 }
313
314 /**
315 * Add product content and template items to admin toolbar.
316 *
317 * @param \WP_Admin_Bar $wp_admin_bar The admin bar instance.
318 *
319 * @return void
320 */
321 public function renderProductContent( $wp_admin_bar ): void {
322 if ( ! $this->isRenderedWithBlocks() ) {
323 return;
324 }
325
326 // Add product content link.
327 $wp_admin_bar->add_node(
328 [
329 'id' => 'surecart-edit-product-content',
330 'parent' => 'surecart-design',
331 'title' => __( 'Content Designer', 'surecart' ),
332 'href' => admin_url( '/post.php?post=' . get_the_ID() . '&action=edit' ),
333 ]
334 );
335 }
336
337 /**
338 * Add product content item to admin toolbar.
339 *
340 * @param \WP_Admin_Bar $wp_admin_bar The admin bar instance.
341 *
342 * @return void
343 */
344 public function renderProductTemplate( $wp_admin_bar ): void {
345 if ( ! $this->isRenderedWithBlocks() ) {
346 return;
347 }
348
349 $product = sc_get_product();
350 $template_type = wp_is_block_theme() ? 'wp_template' : 'wp_template_part';
351 $template_id = wp_is_block_theme() ? $product->template_id : $product->template_part_id;
352 if ( empty( $template_id ) ) {
353 $template_id = wp_is_block_theme() ? 'surecart/surecart//single-sc_product' : 'surecart/surecart//product-info';
354 }
355
356 if ( empty( get_block_template( $template_id, $template_type ) ) ) {
357 return;
358 }
359
360 // Add product template link.
361 $wp_admin_bar->add_node(
362 [
363 'id' => 'surecart-edit-product-template',
364 'parent' => 'surecart-design',
365 'title' => __( 'Product Template', 'surecart' ),
366 'href' => admin_url( '/site-editor.php?postType=' . rawurlencode( $template_type ) . '&postId=' . rawurlencode( $template_id ) . '&canvas=edit' ),
367 ]
368 );
369 }
370
371 /**
372 * Check if bricks is rendering the current page.
373 *
374 * @return bool
375 */
376 public function isRenderedWithBricks(): bool {
377 return class_exists( '\Bricks\Elements' ) && $this->app->resolve( 'surecart.bricks.template' )->isRenderedWithBricks();
378 }
379
380 /**
381 * Check if elementor is rendering the current page.
382 *
383 * @return bool
384 */
385 public function isRenderedWithElementor(): bool {
386 return class_exists( '\Elementor\Plugin' ) && $this->app->resolve( 'elementor.templates.service' )->isRenderedWithElementor();
387 }
388
389 /**
390 * Maybe add sticky purchase template to admin toolbar.
391 *
392 * @param \WP_Admin_Bar $wp_admin_bar The admin bar instance.
393 *
394 * @return void
395 */
396 public function maybeAddStickyPurchaseTemplate( $wp_admin_bar ): void {
397 $sticky_purchase_template_id = 'surecart/surecart//sticky-purchase';
398 if ( empty( get_block_template( $sticky_purchase_template_id, 'wp_template_part' ) ) ) {
399 return;
400 }
401
402 $wp_admin_bar->add_node(
403 [
404 'id' => 'surecart-edit-sticky-template',
405 'parent' => 'surecart-design',
406 'title' => __( 'Sticky Purchase Template', 'surecart' ),
407 'href' => admin_url( '/site-editor.php?postType=wp_template_part&postId=' . rawurlencode( $sticky_purchase_template_id ) . '&canvas=edit' ),
408 ]
409 );
410 }
411 }
412