ComingSoonAdminBarBadge.php
5 days ago
ComingSoonCacheInvalidator.php
1 year ago
ComingSoonHelper.php
1 year ago
ComingSoonRequestHandler.php
9 months ago
ComingSoonAdminBarBadge.php
140 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\ComingSoon; |
| 6 | |
| 7 | /** |
| 8 | * Adds hooks to add a badge to the WordPress admin bar showing site visibility. |
| 9 | */ |
| 10 | class ComingSoonAdminBarBadge { |
| 11 | |
| 12 | /** |
| 13 | * Check if the site visibility badge is enabled. |
| 14 | * |
| 15 | * @return bool |
| 16 | */ |
| 17 | private function is_badge_enabled(): bool { |
| 18 | return 'yes' === get_option( 'woocommerce_feature_site_visibility_badge_enabled', 'yes' ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Sets up the hooks. |
| 23 | * |
| 24 | * @internal |
| 25 | */ |
| 26 | final public function init() { |
| 27 | add_action( 'init', array( $this, 'init_hooks' ) ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Sets up the hooks if user has required capabilities. |
| 32 | * |
| 33 | * @internal |
| 34 | */ |
| 35 | public function init_hooks() { |
| 36 | // Early exit if the user is not logged in as administrator / shop manager. |
| 37 | if ( ! is_user_logged_in() || ! current_user_can( 'manage_woocommerce' ) ) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | add_action( 'admin_bar_menu', array( $this, 'site_visibility_badge' ), 31 ); |
| 42 | add_action( 'wp_head', array( $this, 'output_css' ) ); |
| 43 | add_action( 'admin_head', array( $this, 'output_css' ) ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Add site visibility cache badge to WP admin bar. |
| 48 | * |
| 49 | * @internal |
| 50 | * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance. |
| 51 | */ |
| 52 | public function site_visibility_badge( $wp_admin_bar ) { |
| 53 | if ( ! $this->is_badge_enabled() ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $labels = array( |
| 58 | 'coming-soon' => __( 'Coming soon', 'woocommerce' ), |
| 59 | 'store-coming-soon' => __( 'Store coming soon', 'woocommerce' ), |
| 60 | 'live' => __( 'Live', 'woocommerce' ), |
| 61 | ); |
| 62 | |
| 63 | if ( get_option( 'woocommerce_coming_soon' ) === 'yes' ) { |
| 64 | if ( get_option( 'woocommerce_store_pages_only' ) === 'yes' ) { |
| 65 | $key = 'store-coming-soon'; |
| 66 | } else { |
| 67 | $key = 'coming-soon'; |
| 68 | } |
| 69 | } else { |
| 70 | $key = 'live'; |
| 71 | } |
| 72 | |
| 73 | $args = array( |
| 74 | 'id' => 'woocommerce-site-visibility-badge', |
| 75 | 'title' => $labels[ $key ], |
| 76 | 'href' => admin_url( 'admin.php?page=wc-settings&tab=site-visibility' ), |
| 77 | 'meta' => array( |
| 78 | 'class' => 'woocommerce-site-status-badge-' . $key, |
| 79 | ), |
| 80 | ); |
| 81 | $wp_admin_bar->add_node( $args ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Output CSS for site visibility badge. |
| 86 | * |
| 87 | * @internal |
| 88 | */ |
| 89 | public function output_css() { |
| 90 | if ( ! $this->is_badge_enabled() ) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if ( is_admin_bar_showing() ) { |
| 95 | echo '<style> |
| 96 | #wpadminbar .quicklinks #wp-admin-bar-woocommerce-site-visibility-badge { |
| 97 | padding: 7px 0; |
| 98 | } |
| 99 | |
| 100 | #wpadminbar .quicklinks #wp-admin-bar-woocommerce-site-visibility-badge a.ab-item { |
| 101 | /* Layout */ |
| 102 | background-color: #F6F7F7; |
| 103 | border-radius: 2px; |
| 104 | display: flex; |
| 105 | height: 18px; |
| 106 | padding: 0px 6px; |
| 107 | align-items: center; |
| 108 | gap: 8px; |
| 109 | |
| 110 | /* Typography */ |
| 111 | color: #3C434A; |
| 112 | font-size: 12px; |
| 113 | font-style: normal; |
| 114 | font-weight: 500; |
| 115 | line-height: 16px; |
| 116 | } |
| 117 | |
| 118 | #wpadminbar .quicklinks #wp-admin-bar-woocommerce-site-visibility-badge a.ab-item:hover, |
| 119 | #wpadminbar .quicklinks #wp-admin-bar-woocommerce-site-visibility-badge a.ab-item:focus { |
| 120 | background-color: #DCDCDE; |
| 121 | } |
| 122 | |
| 123 | #wpadminbar .quicklinks #wp-admin-bar-woocommerce-site-visibility-badge a.ab-item:focus { |
| 124 | outline: var(--wp-admin-border-width-focus) solid var(--wp-admin-theme-color-darker-20); |
| 125 | } |
| 126 | |
| 127 | #wpadminbar .quicklinks #wp-admin-bar-woocommerce-site-visibility-badge.woocommerce-site-status-badge-live a.ab-item { |
| 128 | background-color: #E6F2E8; |
| 129 | color: #00450C; |
| 130 | } |
| 131 | |
| 132 | #wpadminbar .quicklinks #wp-admin-bar-woocommerce-site-visibility-badge.woocommerce-site-status-badge-live a.ab-item:hover, |
| 133 | #wpadminbar .quicklinks #wp-admin-bar-woocommerce-site-visibility-badge.woocommerce-site-status-badge-live a.ab-item:focus { |
| 134 | background-color: #B8E6BF; |
| 135 | } |
| 136 | </style>'; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 |