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