AdminMenuPageService.php
2 years ago
AdminMenuPageServiceProvider.php
2 years ago
ProductCollectionsMenuService.php
2 years ago
AdminMenuPageService.php
212 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 = []; |
| 24 | |
| 25 | /** |
| 26 | * Add menu items. |
| 27 | */ |
| 28 | public function bootstrap() { |
| 29 | add_action( 'admin_menu', [ $this, 'registerAdminPages' ] ); |
| 30 | add_action( 'admin_head', [ $this, 'adminMenuCSS' ] ); |
| 31 | add_filter( 'parent_file', [ $this, 'forceSelect' ] ); |
| 32 | |
| 33 | // Admin bar menus. |
| 34 | if ( apply_filters( 'surecart_show_admin_bar_visit_store', true ) ) { |
| 35 | add_action( 'admin_bar_menu', array( $this, 'adminBarMenu' ), 31 ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Add the "Visit Store" link in admin bar main menu. |
| 41 | * |
| 42 | * @since 2.4.0 |
| 43 | * @param WP_Admin_Bar $wp_admin_bar Admin bar instance. |
| 44 | */ |
| 45 | public function adminBarMenu( $wp_admin_bar ) { |
| 46 | if ( ! is_admin() || ! is_admin_bar_showing() ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // Show only when the user is a member of this site, or they're a super admin. |
| 51 | if ( ! is_user_member_of_blog() && ! is_super_admin() ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // Don't display when shop page is the same of the page on front. |
| 56 | if ( intval( get_option( 'page_on_front' ) ) === \SureCart::pages()->getId( 'shop' ) ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | // Add an option to visit the store. |
| 61 | $wp_admin_bar->add_node( |
| 62 | array( |
| 63 | 'parent' => 'site-name', |
| 64 | 'id' => 'view-sc-store', |
| 65 | 'title' => class_exists( 'WooCommerce' ) ? __( 'Visit SureCart Store', 'surecart' ) : __( 'Visit Store', 'surecart' ), |
| 66 | 'href' => \SureCart::pages()->url( 'shop' ), |
| 67 | ) |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Make sure these menu items get selected. |
| 73 | * |
| 74 | * @param string $file The file string. |
| 75 | * |
| 76 | * @return string |
| 77 | */ |
| 78 | public function forceSelect( $file ) { |
| 79 | global $submenu_file; |
| 80 | global $post; |
| 81 | |
| 82 | if ( ! empty( $post->ID ) && in_array( |
| 83 | $post->ID, |
| 84 | [ |
| 85 | \SureCart::pages()->getId( 'cart', 'sc_cart' ), |
| 86 | \SureCart::pages()->getId( 'checkout' ), |
| 87 | \SureCart::pages()->getId( 'shop' ), |
| 88 | \SureCart::pages()->getId( 'dashboard' ), |
| 89 | ] |
| 90 | ) ) { |
| 91 | $file = 'sc-dashboard'; |
| 92 | // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 93 | $submenu_file = 'post.php?post=' . (int) $post->ID . '&action=edit'; |
| 94 | } |
| 95 | |
| 96 | return $file; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Add some divider css. |
| 101 | * |
| 102 | * @return void |
| 103 | */ |
| 104 | public function adminMenuCSS(): void { |
| 105 | echo '<style> |
| 106 | #toplevel_page_sc-dashboard li { |
| 107 | clear: both; |
| 108 | } |
| 109 | #toplevel_page_sc-dashboard li:not(:last-child) a[href^="admin.php?page=sc-customers"]:after { |
| 110 | border-bottom: 1px solid hsla(0,0%,100%,.2); |
| 111 | display: block; |
| 112 | float: left; |
| 113 | margin: 13px -15px 8px; |
| 114 | content: ""; |
| 115 | width: calc(100% + 26px); |
| 116 | } |
| 117 | #toplevel_page_sc-dashboard li:not(:last-child) a[href^="admin.php?page=sc-dashboard"]:after { |
| 118 | border-bottom: 1px solid hsla(0,0%,100%,.2); |
| 119 | display: block; |
| 120 | float: left; |
| 121 | margin: 13px -15px 8px; |
| 122 | content: ""; |
| 123 | width: calc(100% + 26px); |
| 124 | } |
| 125 | #toplevel_page_sc-dashboard li:not(:last-child) a[href^="edit.php?post_type=sc_form"]:after { |
| 126 | border-bottom: 1px solid hsla(0,0%,100%,.2); |
| 127 | display: block; |
| 128 | float: left; |
| 129 | margin: 13px -15px 8px; |
| 130 | content: ""; |
| 131 | width: calc(100% + 26px); |
| 132 | } |
| 133 | </style>'; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Register admin pages. |
| 138 | * |
| 139 | * @return void |
| 140 | */ |
| 141 | public function registerAdminPages() { |
| 142 | $entitlements = \SureCart::account()->entitlements; |
| 143 | if ( ! ApiToken::get() ) { |
| 144 | $this->slug = 'sc-getting-started'; |
| 145 | } |
| 146 | |
| 147 | $logo = file_get_contents( plugin_dir_path( SURECART_PLUGIN_FILE ) . 'images/icon.svg' ); |
| 148 | \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 ); |
| 149 | |
| 150 | // not yet installed. |
| 151 | if ( ! ApiToken::get() ) { |
| 152 | $this->pages = [ |
| 153 | 'get-started' => \add_submenu_page( $this->slug, __( 'Get Started', 'surecart' ), __( 'Get Started', 'surecart' ), 'manage_options', $this->slug, '__return_false' ), |
| 154 | 'complete-signup' => \add_submenu_page( '', __( 'Complete Signup', 'surecart' ), __( 'Complete Signup', 'surecart' ), 'manage_options', 'sc-complete-signup', '__return_false' ), |
| 155 | 'settings' => \add_submenu_page( $this->slug, __( 'Settings', 'surecart' ), __( 'Settings', 'surecart' ), 'manage_options', 'sc-settings', '__return_false' ), |
| 156 | ]; |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | $this->pages = [ |
| 161 | 'get-started' => \add_submenu_page( $this->slug, __( 'Dashboard', 'surecart' ), __( 'Dashboard', 'surecart' ), 'manage_sc_shop_settings', $this->slug, '__return_false' ), |
| 162 | 'complete-signup' => \add_submenu_page( '', __( 'Complete Signup', 'surecart' ), __( 'Complete Signup', 'surecart' ), 'manage_options', 'sc-complete-signup', '__return_false' ), |
| 163 | 'claim-account' => \add_submenu_page( '', __( 'Claim Account', 'surecart' ), __( 'Claim Account', 'surecart' ), 'manage_options', 'sc-claim-account', '__return_false' ), |
| 164 | 'orders' => \add_submenu_page( $this->slug, __( 'Orders', 'surecart' ), __( 'Orders', 'surecart' ), 'edit_sc_orders', 'sc-orders', '__return_false' ), |
| 165 | 'checkouts' => in_array( $_GET['page'] ?? '', [ 'sc-checkouts' ] ) ? \add_submenu_page( $this->slug, __( 'Checkouts', 'surecart' ), '↳ ' . __( 'Add New', 'surecart' ), 'edit_sc_orders', 'sc-checkouts', '__return_false' ) : null, |
| 166 | 'abandoned' => in_array( $_GET['page'] ?? '', [ '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, |
| 167 | 'products' => \add_submenu_page( $this->slug, __( 'Products', 'surecart' ), __( 'Products', 'surecart' ), 'edit_sc_products', 'sc-products', '__return_false' ), |
| 168 | 'product-collections' => in_array( $_GET['page'] ?? '', [ 'sc-products', 'sc-product-groups', 'sc-bumps', 'sc-product-collections' ], true ) ? \add_submenu_page( $this->slug, __( 'Collections', 'surecart' ), '↳ ' . __( 'Collections', 'surecart' ), 'edit_sc_products', 'sc-product-collections', '__return_false' ) : null, |
| 169 | 'bumps' => ! empty( $entitlements->bumps ) && in_array( $_GET['page'] ?? '', [ 'sc-products', 'sc-product-groups', 'sc-bumps', 'sc-product-collections' ] ) ? \add_submenu_page( $this->slug, __( 'Order Bumps', 'surecart' ), '↳ ' . __( 'Order Bumps', 'surecart' ), 'edit_sc_products', 'sc-bumps', '__return_false' ) : null, |
| 170 | 'product-groups' => ! empty( $entitlements->product_groups ) && in_array( $_GET['page'] ?? '', [ 'sc-products', 'sc-product-groups', 'sc-bumps', 'sc-product-collections' ] ) ? \add_submenu_page( $this->slug, __( 'Upgrade Groups', 'surecart' ), '↳ ' . __( 'Upgrade Groups', 'surecart' ), 'edit_sc_products', 'sc-product-groups', '__return_false' ) : null, |
| 171 | 'coupons' => \add_submenu_page( $this->slug, __( 'Coupons', 'surecart' ), __( 'Coupons', 'surecart' ), 'edit_sc_coupons', 'sc-coupons', '__return_false' ), |
| 172 | 'licenses' => ! empty( $entitlements->licensing ) ? \add_submenu_page( $this->slug, __( 'Licenses', 'surecart' ), __( 'Licenses', 'surecart' ), 'edit_sc_products', 'sc-licenses', '__return_false' ) : null, |
| 173 | 'subscriptions' => \add_submenu_page( $this->slug, __( 'Subscriptions', 'surecart' ), __( 'Subscriptions', 'surecart' ), 'edit_sc_subscriptions', 'sc-subscriptions', '__return_false' ), |
| 174 | 'cancellations' => in_array( $_GET['page'] ?? '', [ 'sc-subscriptions', 'sc-cancellation-insights' ] ) ? \add_submenu_page( $this->slug, __( 'Cancellation Insights', 'surecart' ), '↳ ' . __( 'Cancellations', 'surecart' ), 'edit_sc_subscriptions', 'sc-cancellation-insights', '__return_false' ) : null, |
| 175 | 'customers' => \add_submenu_page( $this->slug, __( 'Customers', 'surecart' ), __( 'Customers', 'surecart' ), 'edit_sc_customers', 'sc-customers', '__return_false' ), |
| 176 | 'shop' => $this->getPage( 'shop', __( 'Shop', 'surecart' ) ), |
| 177 | 'cart' => $this->getPage( 'cart', __( 'Cart', 'surecart' ), 'sc_cart' ), |
| 178 | 'checkout' => $this->getPage( 'checkout', __( 'Checkout', 'surecart' ) ), |
| 179 | 'dashboard' => $this->getPage( 'dashboard', __( 'Customer Area', 'surecart' ) ), |
| 180 | 'forms' => \add_submenu_page( $this->slug, __( 'Forms', 'surecart' ), __( 'Custom Forms', 'surecart' ), 'edit_posts', 'edit.php?post_type=sc_form', '' ), |
| 181 | 'settings' => \add_submenu_page( $this->slug, __( 'Settings', 'surecart' ), __( 'Settings', 'surecart' ), 'manage_options', 'sc-settings', '__return_false' ), |
| 182 | ]; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Get the page link. |
| 187 | * |
| 188 | * @param string $slug The slug. |
| 189 | * @param string $name The name. |
| 190 | * @param string $post_type The post type. |
| 191 | * |
| 192 | * @return void |
| 193 | */ |
| 194 | public function getPage( $slug, $name, $post_type = 'page' ) { |
| 195 | // add filter to disable shop page menu item. |
| 196 | if ( ! get_option( 'surecart_' . $slug . '_admin_menu', true ) ) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | $page_id = \SureCart::pages()->getId( $slug, $post_type ); |
| 201 | |
| 202 | $status = ''; |
| 203 | |
| 204 | $post_status = get_post_status( $page_id ); |
| 205 | if ( 'publish' !== $post_status ) { |
| 206 | $status = '<span class="awaiting-mod">' . ( get_post_status_object( $post_status )->label ?? esc_html__( 'Deleted', 'surecart' ) ) . '</span>'; |
| 207 | } |
| 208 | |
| 209 | return \add_submenu_page( $this->slug, $name, $name . $status, 'manage_options', 'post.php?post=' . (int) $page_id . '&action=edit', '' ); |
| 210 | } |
| 211 | } |
| 212 |