AdminMenuPageService.php
1 year ago
AdminMenuPageServiceProvider.php
2 years ago
ProductCollectionsMenuService.php
2 years ago
AdminMenuPageService.php
346 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 | // Check if we're editing a taxonomy that applies to sc_product post types. |
| 118 | $screen = get_current_screen(); |
| 119 | $taxonomy = get_taxonomy( $screen->taxonomy ); |
| 120 | if ( $screen && 'edit-tags' === $screen->base && in_array( 'sc_product', (array) $taxonomy->object_type, true ) ) { |
| 121 | $file = 'sc-dashboard'; |
| 122 | $submenu_file = \SureCart::taxonomies()->editLink( $screen->taxonomy, 'sc_product' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 123 | } |
| 124 | |
| 125 | return $file; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Add some divider css. |
| 130 | * |
| 131 | * @return void |
| 132 | */ |
| 133 | public function adminMenuCSS(): void { |
| 134 | echo '<style> |
| 135 | #toplevel_page_sc-dashboard li { |
| 136 | clear: both; |
| 137 | } |
| 138 | #toplevel_page_sc-dashboard li:not(:last-child) a[href^="admin.php?page=sc-customers"]: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^="admin.php?page=sc-dashboard"]: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 | #toplevel_page_sc-dashboard li:not(:last-child) a[href^="edit.php?post_type=sc_form"]:after { |
| 155 | border-bottom: 1px solid hsla(0,0%,100%,.2); |
| 156 | display: block; |
| 157 | float: left; |
| 158 | margin: 13px -15px 8px; |
| 159 | content: ""; |
| 160 | width: calc(100% + 26px); |
| 161 | } |
| 162 | </style>'; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Register admin pages. |
| 167 | * |
| 168 | * @return void |
| 169 | */ |
| 170 | public function registerAdminPages() { |
| 171 | $entitlements = \SureCart::account()->entitlements; |
| 172 | if ( ! ApiToken::get() ) { |
| 173 | $this->slug = 'sc-getting-started'; |
| 174 | } |
| 175 | |
| 176 | $logo = file_get_contents( plugin_dir_path( SURECART_PLUGIN_FILE ) . 'images/icon.svg' ); |
| 177 | // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 178 | \add_menu_page( __( 'Dashboard', 'surecart' ), __( 'SureCart', 'surecart' ), 'manage_sc_shop_settings', $this->slug, '__return_false', 'data:image/svg+xml;base64,' . base64_encode( $logo ), apply_filters( 'surecart_menu_priority', 2.0001 ) ); |
| 179 | |
| 180 | // not yet installed. |
| 181 | if ( ! ApiToken::get() ) { |
| 182 | $this->pages = array( |
| 183 | 'get-started' => \add_submenu_page( $this->slug, __( 'Get Started', 'surecart' ), __( 'Get Started', 'surecart' ), 'manage_options', $this->slug, '__return_false' ), |
| 184 | 'complete-signup' => \add_submenu_page( '', __( 'Complete Signup', 'surecart' ), __( 'Complete Signup', 'surecart' ), 'manage_options', 'sc-complete-signup', '__return_false' ), |
| 185 | 'settings' => \add_submenu_page( $this->slug, __( 'Settings', 'surecart' ), __( 'Settings', 'surecart' ), 'manage_options', 'sc-settings', '__return_false' ), |
| 186 | ); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | $affiliate_pages = array( 'sc-affiliates', 'sc-affiliate-requests', 'sc-affiliate-clicks', 'sc-affiliate-referrals', 'sc-affiliate-payouts', 'sc-affiliate-payout-groups' ); |
| 191 | |
| 192 | $taxonomies = array_diff( get_object_taxonomies( 'sc_product' ), array( 'sc_account', 'sc_collection' ) ); |
| 193 | sort( $taxonomies, SORT_STRING ); // Sort the taxonomies alphabetically. |
| 194 | $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_merge( $taxonomies, array( 'sc_collection' ) ), true ); |
| 195 | |
| 196 | $this->pages = array( |
| 197 | 'get-started' => \add_submenu_page( $this->slug, __( 'Dashboard', 'surecart' ), __( 'Dashboard', 'surecart' ), 'manage_sc_shop_settings', $this->slug, '__return_false' ), |
| 198 | 'complete-signup' => \add_submenu_page( '', __( 'Complete Signup', 'surecart' ), __( 'Complete Signup', 'surecart' ), 'manage_options', 'sc-complete-signup', '__return_false' ), |
| 199 | 'claim-account' => \add_submenu_page( '', __( 'Claim Account', 'surecart' ), __( 'Claim Account', 'surecart' ), 'manage_options', 'sc-claim-account', '__return_false' ), |
| 200 | 'orders' => \add_submenu_page( $this->slug, __( 'Orders', 'surecart' ), __( 'Orders', 'surecart' ), 'edit_sc_orders', 'sc-orders', '__return_false' ), |
| 201 | 'abandoned' => in_array( $_GET['page'] ?? '', [ 'sc-orders', 'sc-abandoned-checkouts', 'sc-invoices' ], true ) ? \add_submenu_page( $this->slug, __( 'Abandoned', 'surecart' ), '↳ ' . __( 'Abandoned', 'surecart' ), 'edit_sc_orders', 'sc-abandoned-checkouts', '__return_false' ) : null, |
| 202 | 'invoices' => in_array( $_GET['page'] ?? '', [ 'sc-orders', 'sc-abandoned-checkouts', 'sc-invoices' ], true ) ? \add_submenu_page( $this->slug, __( 'Invoices', 'surecart' ), '↳ ' . __( 'Invoices', 'surecart' ), 'edit_sc_invoices', 'sc-invoices', '__return_false' ) : null, |
| 203 | 'products' => \add_submenu_page( $this->slug, __( 'Products', 'surecart' ), __( 'Products', 'surecart' ), 'edit_sc_products', 'sc-products', '__return_false' ), |
| 204 | '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, |
| 205 | ); |
| 206 | |
| 207 | if ( ! empty( $taxonomies ) && is_array( $taxonomies ) && $is_product_menu_opened ) { |
| 208 | $this->pages += array_map( |
| 209 | function ( $taxonomy ) { |
| 210 | if ( ! taxonomy_exists( $taxonomy ) ) { |
| 211 | return null; |
| 212 | } |
| 213 | $taxonomy_obj = get_taxonomy( $taxonomy ); |
| 214 | 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' ), '' ); |
| 215 | }, |
| 216 | $taxonomies, |
| 217 | [ $is_product_menu_opened, $this->slug ] |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | $this->pages += array( |
| 222 | 'bumps' => $is_product_menu_opened ? \add_submenu_page( $this->slug, __( 'Order Bumps', 'surecart' ), '↳ ' . __( 'Order Bumps', 'surecart' ), 'edit_sc_products', 'sc-bumps', '__return_false' ) : null, |
| 223 | 'upsells' => $is_product_menu_opened ? \add_submenu_page( $this->slug, __( 'Upsells', 'surecart' ), '↳ ' . __( 'Upsells', 'surecart' ), 'edit_sc_products', 'sc-upsell-funnels', '__return_false' ) : null, |
| 224 | '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, |
| 225 | 'coupons' => \add_submenu_page( $this->slug, __( 'Coupons', 'surecart' ), __( 'Coupons', 'surecart' ), 'edit_sc_coupons', 'sc-coupons', '__return_false' ), |
| 226 | 'licenses' => ! empty( $entitlements->licensing ) ? \add_submenu_page( $this->slug, __( 'Licenses', 'surecart' ), __( 'Licenses', 'surecart' ), 'edit_sc_products', 'sc-licenses', '__return_false' ) : null, |
| 227 | 'subscriptions' => \add_submenu_page( $this->slug, __( 'Subscriptions', 'surecart' ), __( 'Subscriptions', 'surecart' ), 'edit_sc_subscriptions', 'sc-subscriptions', '__return_false' ), |
| 228 | '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, |
| 229 | 'affiliates' => \add_submenu_page( $this->slug, __( 'Affiliates', 'surecart' ), __( 'Affiliates', 'surecart' ), 'edit_sc_affiliates', 'sc-affiliates', '__return_false' ), |
| 230 | '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, |
| 231 | '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, |
| 232 | '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, |
| 233 | '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, |
| 234 | '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, |
| 235 | 'customers' => \add_submenu_page( $this->slug, __( 'Customers', 'surecart' ), __( 'Customers', 'surecart' ), 'edit_sc_customers', 'sc-customers', '__return_false' ), |
| 236 | 'restore' => 'sc-restore' === ( $_GET['page'] ?? '' ) ? \add_submenu_page( null, __( 'Restore', 'surecart' ), __( 'Restore', 'surecart' ), 'manage_options', 'sc-restore', '__return_false' ) : null, |
| 237 | 'shop' => $this->getPage( 'shop', __( 'Shop', 'surecart' ) ), |
| 238 | 'checkout' => $this->getPage( 'checkout', __( 'Checkout', 'surecart' ) ), |
| 239 | 'cart' => $this->addTemplateSubMenuPage( 'cart', __( 'Cart', 'surecart' ), 'surecart/surecart//cart' ), |
| 240 | 'dashboard' => $this->getPage( 'dashboard', __( 'Customer Area', 'surecart' ) ), |
| 241 | 'forms' => \add_submenu_page( $this->slug, __( 'Forms', 'surecart' ), __( 'Custom Forms', 'surecart' ), 'edit_posts', 'edit.php?post_type=sc_form', '' ), |
| 242 | 'settings' => \add_submenu_page( $this->slug, __( 'Settings', 'surecart' ), __( 'Settings', 'surecart' ), 'manage_options', 'sc-settings', '__return_false' ), |
| 243 | ); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Get the page link. |
| 248 | * |
| 249 | * @param string $slug The slug. |
| 250 | * @param string $name The name. |
| 251 | * @param string $post_type The post type. |
| 252 | * |
| 253 | * @return void |
| 254 | */ |
| 255 | public function getPage( $slug, $name, $post_type = 'page' ) { |
| 256 | // add filter to disable shop page menu item. |
| 257 | if ( ! get_option( 'surecart_' . $slug . '_admin_menu', true ) ) { |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | $page_id = \SureCart::pages()->getId( $slug, $post_type ); |
| 262 | |
| 263 | $status = ''; |
| 264 | |
| 265 | $post_status = get_post_status( $page_id ); |
| 266 | if ( 'publish' !== $post_status ) { |
| 267 | $status = '<span class="awaiting-mod">' . ( get_post_status_object( $post_status )->label ?? esc_html__( 'Deleted', 'surecart' ) ) . '</span>'; |
| 268 | } |
| 269 | |
| 270 | return \add_submenu_page( $this->slug, $name, $name . $status, 'manage_options', $this->getSubMenuPageSlug( $slug, $page_id ), '' ); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Get the page menu slug. |
| 275 | * |
| 276 | * @param string $slug The slug. |
| 277 | * @param int $page_id The page id. |
| 278 | */ |
| 279 | public function getSubMenuPageSlug( $slug, $page_id ) { |
| 280 | // check if it is not an essential page. |
| 281 | if ( ! in_array( $slug, self::ESSENTIAL_PAGES, true ) ) { |
| 282 | return 'post.php?post=' . $page_id . '&action=edit'; |
| 283 | } |
| 284 | |
| 285 | $post_status = get_post_status( $page_id ); |
| 286 | |
| 287 | // check if the page is published. |
| 288 | if ( 'publish' === $post_status ) { |
| 289 | return 'post.php?post=' . $page_id . '&action=edit'; |
| 290 | } |
| 291 | |
| 292 | return 'admin.php?page=sc-restore&restore=' . $slug; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Add a submenu page for a template. |
| 297 | * |
| 298 | * @param string $slug The slug. |
| 299 | * @param string $name The name. |
| 300 | * @param string $template_slug The template slug. |
| 301 | * |
| 302 | * @return null|string|false |
| 303 | */ |
| 304 | public function addTemplateSubMenuPage( $slug, $name, $template_slug ) { |
| 305 | // add filter to disable shop page menu item. |
| 306 | if ( ! get_option( 'surecart_' . $slug . '_admin_menu', true ) ) { |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | return \add_submenu_page( |
| 311 | $this->slug, |
| 312 | $name, |
| 313 | $name, |
| 314 | 'manage_options', |
| 315 | add_query_arg( |
| 316 | [ |
| 317 | 'postId' => rawurlencode( $template_slug ), |
| 318 | 'postType' => 'wp_template_part', |
| 319 | 'canvas' => 'edit', |
| 320 | ], |
| 321 | 'site-editor.php', |
| 322 | ), |
| 323 | '' |
| 324 | ); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Select menu item. |
| 329 | * |
| 330 | * @param string $file The file. |
| 331 | * |
| 332 | * @return string |
| 333 | */ |
| 334 | public function applyMenuOverrides( $file ) { |
| 335 | global $plugin_page; |
| 336 | |
| 337 | foreach ( self::MENU_CURRENT_OVERRIDES as $key => $value ) { |
| 338 | if ( $key === $plugin_page ) { |
| 339 | $plugin_page = $value; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return $file; |
| 344 | } |
| 345 | } |
| 346 |