AdminMenuPageService.php
5 days ago
AdminMenuPageServiceProvider.php
9 months ago
AdminToolbarService.php
5 days ago
ProductCollectionsMenuService.php
4 months ago
AdminToolbarService.php
425 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 Bundle link. |
| 169 | $wp_admin_bar->add_node( |
| 170 | array( |
| 171 | 'parent' => 'new-sc-product', |
| 172 | 'id' => 'new-sc-bundle', |
| 173 | 'title' => __( 'Bundle', 'surecart' ), |
| 174 | 'href' => esc_url( \SureCart::getUrl()->edit( 'bundle' ) ), |
| 175 | ) |
| 176 | ); |
| 177 | |
| 178 | // Add Product Collection link. |
| 179 | $wp_admin_bar->add_node( |
| 180 | array( |
| 181 | 'parent' => 'new-sc-product', |
| 182 | 'id' => 'new-sc-collection', |
| 183 | 'title' => $woocommerce_exists ? __( 'SureCart Collection', 'surecart' ) : __( 'Collection', 'surecart' ), |
| 184 | 'href' => esc_url( admin_url( 'admin.php?page=sc-product-collections&action=edit' ) ), |
| 185 | ) |
| 186 | ); |
| 187 | |
| 188 | // Add Order Bump link. |
| 189 | $wp_admin_bar->add_node( |
| 190 | array( |
| 191 | 'parent' => 'new-sc-product', |
| 192 | 'id' => 'new-sc-bump', |
| 193 | 'title' => $woocommerce_exists ? __( 'SureCart Order Bump', 'surecart' ) : __( 'Order Bump', 'surecart' ), |
| 194 | 'href' => esc_url( admin_url( 'admin.php?page=sc-bumps&action=edit' ) ), |
| 195 | ) |
| 196 | ); |
| 197 | |
| 198 | // Add Upsell link. |
| 199 | $wp_admin_bar->add_node( |
| 200 | array( |
| 201 | 'parent' => 'new-sc-product', |
| 202 | 'id' => 'new-sc-upsell', |
| 203 | 'title' => $woocommerce_exists ? __( 'SureCart Upsell', 'surecart' ) : __( 'Upsell', 'surecart' ), |
| 204 | 'href' => esc_url( admin_url( 'admin.php?page=sc-upsell-funnels&action=edit' ) ), |
| 205 | ) |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | // Add Coupon link. |
| 210 | if ( current_user_can( 'edit_sc_coupons' ) ) { |
| 211 | $wp_admin_bar->add_node( |
| 212 | array( |
| 213 | 'parent' => 'new-content', |
| 214 | 'id' => 'new-sc-coupon', |
| 215 | 'title' => $woocommerce_exists ? __( 'SureCart Coupon', 'surecart' ) : __( 'Coupon', 'surecart' ), |
| 216 | 'href' => esc_url( admin_url( 'admin.php?page=sc-coupons&action=edit' ) ), |
| 217 | ) |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | // Add Invoice link. |
| 222 | if ( current_user_can( 'edit_sc_invoices' ) ) { |
| 223 | $wp_admin_bar->add_node( |
| 224 | array( |
| 225 | 'parent' => 'new-content', |
| 226 | 'id' => 'new-sc-invoice', |
| 227 | 'title' => $woocommerce_exists ? __( 'SureCart Invoice', 'surecart' ) : __( 'Invoice', 'surecart' ), |
| 228 | 'href' => esc_url( \SureCart::getUrl()->create( 'invoices' ) ), |
| 229 | ) |
| 230 | ); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Add SureCart main menu to the admin bar. |
| 236 | * |
| 237 | * @param \WP_Admin_Bar $wp_admin_bar Admin bar instance. |
| 238 | * |
| 239 | * @return void |
| 240 | */ |
| 241 | public function adminBarMenu( $wp_admin_bar ) { |
| 242 | // Its for frontend only. |
| 243 | if ( ! is_admin_bar_showing() || is_admin() ) { |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | // Only show on single product edit screen and user can edit products. |
| 248 | if ( ! is_singular( 'sc_product' ) || ! current_user_can( 'edit_sc_products' ) ) { |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | // Only show if user has API token and product is not empty. |
| 253 | if ( ! ApiToken::get() || empty( sc_get_product() ) ) { |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | $surecart_logo = esc_url_raw( plugin_dir_url( SURECART_PLUGIN_FILE ) . 'images/icon.svg' ); |
| 258 | ?> |
| 259 | |
| 260 | <style> |
| 261 | #wp-admin-bar-surecart-toolbar > .ab-item { |
| 262 | display: flex !important; |
| 263 | align-items: center; |
| 264 | } |
| 265 | |
| 266 | #wp-admin-bar-surecart-toolbar > .ab-item::before { |
| 267 | content: ""; |
| 268 | display: inline-block; |
| 269 | width: 18px; |
| 270 | height: 18px; |
| 271 | background-color: #a7aaad !important; /* WordPress toolbar like icon color */ |
| 272 | -webkit-mask: url('<?php echo esc_url( $surecart_logo ); ?>') no-repeat center; |
| 273 | mask: url('<?php echo esc_url( $surecart_logo ); ?>') no-repeat center; |
| 274 | -webkit-mask-size: contain; |
| 275 | mask-size: contain; |
| 276 | -webkit-mask-size: 18px; |
| 277 | mask-size: 18px; |
| 278 | } |
| 279 | </style> |
| 280 | |
| 281 | <?php |
| 282 | $product = sc_get_product(); |
| 283 | $is_bundle = ! empty( $product->bundle ); |
| 284 | $url_key = $is_bundle ? 'bundle' : 'product'; |
| 285 | |
| 286 | $wp_admin_bar->add_node( |
| 287 | [ |
| 288 | 'id' => 'surecart-toolbar', |
| 289 | 'title' => $is_bundle ? __( 'Edit Bundle', 'surecart' ) : __( 'Edit Product', 'surecart' ), |
| 290 | 'href' => '#', |
| 291 | ] |
| 292 | ); |
| 293 | |
| 294 | // Add product/bundle edit link. |
| 295 | $wp_admin_bar->add_node( |
| 296 | [ |
| 297 | 'id' => 'surecart-edit-product', |
| 298 | 'parent' => 'surecart-toolbar', |
| 299 | 'title' => $is_bundle ? __( 'Bundle Details', 'surecart' ) : __( 'Product Details', 'surecart' ), |
| 300 | 'href' => \SureCart::getUrl()->edit( $url_key, $product->id ), |
| 301 | ] |
| 302 | ); |
| 303 | |
| 304 | // Add secondary group |
| 305 | $wp_admin_bar->add_group( |
| 306 | array( |
| 307 | 'id' => 'surecart-design', |
| 308 | 'parent' => 'surecart-toolbar', |
| 309 | 'meta' => array( 'class' => 'ab-sub-secondary' ), |
| 310 | ) |
| 311 | ); |
| 312 | |
| 313 | $this->renderProductContent( $wp_admin_bar ); |
| 314 | $this->renderProductTemplate( $wp_admin_bar ); |
| 315 | $this->maybeAddStickyPurchaseTemplate( $wp_admin_bar ); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Check if the current page is rendered with blocks. |
| 320 | * |
| 321 | * @return bool |
| 322 | */ |
| 323 | public function isRenderedWithBlocks(): bool { |
| 324 | return ! $this->isRenderedWithBricks() && ! $this->isRenderedWithElementor(); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Add product content and template items to admin toolbar. |
| 329 | * |
| 330 | * @param \WP_Admin_Bar $wp_admin_bar The admin bar instance. |
| 331 | * |
| 332 | * @return void |
| 333 | */ |
| 334 | public function renderProductContent( $wp_admin_bar ): void { |
| 335 | if ( ! $this->isRenderedWithBlocks() ) { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | // Add product content link. |
| 340 | $wp_admin_bar->add_node( |
| 341 | [ |
| 342 | 'id' => 'surecart-edit-product-content', |
| 343 | 'parent' => 'surecart-design', |
| 344 | 'title' => __( 'Content Designer', 'surecart' ), |
| 345 | 'href' => admin_url( '/post.php?post=' . get_the_ID() . '&action=edit' ), |
| 346 | ] |
| 347 | ); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Add product content item to admin toolbar. |
| 352 | * |
| 353 | * @param \WP_Admin_Bar $wp_admin_bar The admin bar instance. |
| 354 | * |
| 355 | * @return void |
| 356 | */ |
| 357 | public function renderProductTemplate( $wp_admin_bar ): void { |
| 358 | if ( ! $this->isRenderedWithBlocks() ) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | $product = sc_get_product(); |
| 363 | $template_type = wp_is_block_theme() ? 'wp_template' : 'wp_template_part'; |
| 364 | $template_id = wp_is_block_theme() ? $product->template_id : $product->template_part_id; |
| 365 | if ( empty( $template_id ) ) { |
| 366 | $template_id = wp_is_block_theme() ? 'surecart/surecart//single-sc_product' : 'surecart/surecart//product-info'; |
| 367 | } |
| 368 | |
| 369 | if ( empty( get_block_template( $template_id, $template_type ) ) ) { |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | // Add product template link. |
| 374 | $wp_admin_bar->add_node( |
| 375 | [ |
| 376 | 'id' => 'surecart-edit-product-template', |
| 377 | 'parent' => 'surecart-design', |
| 378 | 'title' => __( 'Product Template', 'surecart' ), |
| 379 | 'href' => admin_url( '/site-editor.php?postType=' . rawurlencode( $template_type ) . '&postId=' . rawurlencode( $template_id ) . '&canvas=edit' ), |
| 380 | ] |
| 381 | ); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Check if bricks is rendering the current page. |
| 386 | * |
| 387 | * @return bool |
| 388 | */ |
| 389 | public function isRenderedWithBricks(): bool { |
| 390 | return class_exists( '\Bricks\Elements' ) && $this->app->resolve( 'surecart.bricks.template' )->isRenderedWithBricks(); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Check if elementor is rendering the current page. |
| 395 | * |
| 396 | * @return bool |
| 397 | */ |
| 398 | public function isRenderedWithElementor(): bool { |
| 399 | return class_exists( '\Elementor\Plugin' ) && $this->app->resolve( 'elementor.templates.service' )->isRenderedWithElementor(); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Maybe add sticky purchase template to admin toolbar. |
| 404 | * |
| 405 | * @param \WP_Admin_Bar $wp_admin_bar The admin bar instance. |
| 406 | * |
| 407 | * @return void |
| 408 | */ |
| 409 | public function maybeAddStickyPurchaseTemplate( $wp_admin_bar ): void { |
| 410 | $sticky_purchase_template_id = 'surecart/surecart//sticky-purchase'; |
| 411 | if ( empty( get_block_template( $sticky_purchase_template_id, 'wp_template_part' ) ) ) { |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | $wp_admin_bar->add_node( |
| 416 | [ |
| 417 | 'id' => 'surecart-edit-sticky-template', |
| 418 | 'parent' => 'surecart-design', |
| 419 | 'title' => __( 'Sticky Purchase Template', 'surecart' ), |
| 420 | 'href' => admin_url( '/site-editor.php?postType=wp_template_part&postId=' . rawurlencode( $sticky_purchase_template_id ) . '&canvas=edit' ), |
| 421 | ] |
| 422 | ); |
| 423 | } |
| 424 | } |
| 425 |