jet-admin-bar.php
309 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Bar module |
| 4 | * |
| 5 | * Version: 1.0.0 |
| 6 | */ |
| 7 | |
| 8 | // If this file is called directly, abort. |
| 9 | if ( ! defined( 'WPINC' ) ) { |
| 10 | die; |
| 11 | } |
| 12 | |
| 13 | if ( ! class_exists( 'Jet_Admin_Bar' ) ) { |
| 14 | |
| 15 | /** |
| 16 | * Admin Bar management module. |
| 17 | */ |
| 18 | class Jet_Admin_Bar { |
| 19 | |
| 20 | /** |
| 21 | * A reference to an instance of this class. |
| 22 | * |
| 23 | * @access private |
| 24 | * @var object |
| 25 | */ |
| 26 | private static $instance = null; |
| 27 | |
| 28 | /** |
| 29 | * A reference to an instance of this class. |
| 30 | * |
| 31 | * @access private |
| 32 | * @var object |
| 33 | */ |
| 34 | public $args = array(); |
| 35 | |
| 36 | /** |
| 37 | * Module version. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | protected $version = '1.0.0'; |
| 42 | |
| 43 | private $items = array(); |
| 44 | |
| 45 | private $default_parent_item = 'jet_plugins'; |
| 46 | |
| 47 | /** |
| 48 | * Constructor. |
| 49 | * |
| 50 | * @param array $args |
| 51 | * @access public |
| 52 | * @return void |
| 53 | */ |
| 54 | public function __construct( $args = array() ) { |
| 55 | |
| 56 | if ( is_admin() || ! is_admin_bar_showing() ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | remove_action( 'wp_body_open', 'wp_admin_bar_render', 0 ); |
| 61 | |
| 62 | add_action( 'init', array( $this, 'register_default_items' ) ); |
| 63 | add_action( 'admin_bar_menu', array( $this, 'register_items' ), 99 ); |
| 64 | |
| 65 | add_action( 'wp_enqueue_scripts', array( $this, 'add_inline_style' ) ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Register default items. |
| 70 | */ |
| 71 | public function register_default_items() { |
| 72 | $this->register_parent_item( |
| 73 | $this->default_parent_item, |
| 74 | array( |
| 75 | 'title' => 'Edit with JetPlugins', |
| 76 | ) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Register items. |
| 82 | * |
| 83 | * @param WP_Admin_Bar $wp_admin_bar |
| 84 | */ |
| 85 | public function register_items( $wp_admin_bar ) { |
| 86 | |
| 87 | foreach ( $this->items as $parent_id => $parent_args ) { |
| 88 | |
| 89 | if ( empty( $parent_args['children'] ) ) { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | $_parent_args = $parent_args; |
| 94 | $_parent_args['id'] = $parent_id; |
| 95 | |
| 96 | unset( $_parent_args['children'] ); |
| 97 | |
| 98 | $wp_admin_bar->add_menu( $_parent_args ); |
| 99 | |
| 100 | // Register children items. |
| 101 | $parent_args['children'] = $this->sort_children_items( $parent_args['children'] ); |
| 102 | |
| 103 | foreach ( $parent_args['children'] as $child_id => $child_args ) { |
| 104 | $child_args['id'] = $child_id; |
| 105 | $child_args = $this->prepare_child_item_args( $child_args ); |
| 106 | $wp_admin_bar->add_menu( $child_args ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Sorting children items. |
| 114 | * |
| 115 | * @param array $children |
| 116 | * @return array |
| 117 | */ |
| 118 | public function sort_children_items( $children ) { |
| 119 | $children = wp_list_sort( $children, array( 'priority' => 'ASC', 'sub_title' => 'ASC' ), null, true ); |
| 120 | return $children; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Register parent item. |
| 125 | * |
| 126 | * @param string $id |
| 127 | * @param array $args |
| 128 | */ |
| 129 | public function register_parent_item( $id = null, $args = array() ) { |
| 130 | $args['id'] = $id; |
| 131 | $this->items[ $id ] = $args; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Register child item. |
| 136 | * |
| 137 | * @param string $id |
| 138 | * @param array $args |
| 139 | */ |
| 140 | public function register_item( $id = null, $args = array() ) { |
| 141 | |
| 142 | $default_args = array( |
| 143 | 'title' => '', |
| 144 | 'sub_title' => '', |
| 145 | 'href' => '', |
| 146 | 'parent' => $this->default_parent_item, |
| 147 | |
| 148 | // Additional arguments |
| 149 | 'class' => '', |
| 150 | 'target_blank' => true, |
| 151 | 'priority' => 10, |
| 152 | ); |
| 153 | |
| 154 | $args = array_merge( $default_args, $args ); |
| 155 | |
| 156 | if ( $this->has_item( $id, $args ) ) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | $this->items[ $args['parent'] ]['children'][ $id ] = $args; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Prepare child item arguments. |
| 165 | * |
| 166 | * @param array $args |
| 167 | * @return array |
| 168 | */ |
| 169 | public function prepare_child_item_args( $args ) { |
| 170 | |
| 171 | if ( ! empty( $args['sub_title'] ) ) { |
| 172 | $args['title'] = sprintf( |
| 173 | '<span class="jet-ab-title">%1$s</span><span class="jet-ab-sub-title">%2$s</span>', |
| 174 | $args['title'], |
| 175 | $args['sub_title'] |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | $classes = array( 'jet-ab-item' ); |
| 180 | |
| 181 | if ( ! empty( $args['class'] ) ) { |
| 182 | $classes[] = $args['class']; |
| 183 | } |
| 184 | |
| 185 | $args['meta']['class'] = join( ' ', $classes ); |
| 186 | |
| 187 | if ( $args['target_blank'] ) { |
| 188 | $args['meta']['target'] = '_blank'; |
| 189 | } |
| 190 | |
| 191 | unset( $args['sub_title'] ); |
| 192 | unset( $args['class'] ); |
| 193 | unset( $args['target_blank'] ); |
| 194 | unset( $args['priority'] ); |
| 195 | |
| 196 | return $args; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Register child item by post ID. |
| 201 | * |
| 202 | * @param int $post_id |
| 203 | * @param array $args |
| 204 | */ |
| 205 | public function register_post_item( $post_id = null, $args = array() ) { |
| 206 | |
| 207 | if ( empty( $post_id ) ) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | $id = 'edit_post_' . $post_id; |
| 212 | |
| 213 | if ( $this->has_item( $id, $args ) ) { |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | $post = get_post( $post_id ); |
| 218 | |
| 219 | if ( empty( $post ) ) { |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | $default_args = array( |
| 224 | 'title' => $post->post_title, |
| 225 | 'sub_title' => $this->get_post_type_label( $post->post_type ), |
| 226 | 'href' => $this->get_edit_url( $post_id ), |
| 227 | ); |
| 228 | |
| 229 | $args = array_merge( $default_args, $args ); |
| 230 | |
| 231 | $this->register_item( $id, $args ); |
| 232 | } |
| 233 | |
| 234 | public function has_item( $id, $args ) { |
| 235 | $args['parent'] = ! empty( $args['parent'] ) ? $args['parent'] : $this->default_parent_item; |
| 236 | return isset( $this->items[ $args['parent'] ]['children'][ $id ] ); |
| 237 | } |
| 238 | |
| 239 | public function get_post_type_label( $post_type ) { |
| 240 | $post_type_obj = get_post_type_object( $post_type ); |
| 241 | return $post_type_obj->labels->singular_name; |
| 242 | } |
| 243 | |
| 244 | public function get_edit_url( $id ) { |
| 245 | $is_build_with_elementor = ! ! get_post_meta( $id, '_elementor_edit_mode', true ); |
| 246 | |
| 247 | if ( $is_build_with_elementor && class_exists( 'Elementor\Plugin' ) ) { |
| 248 | $edit_url = Elementor\Plugin::instance()->documents->get( $id )->get_edit_url(); |
| 249 | } else { |
| 250 | $edit_url = get_edit_post_link( $id, '' ); |
| 251 | } |
| 252 | |
| 253 | return $edit_url; |
| 254 | } |
| 255 | |
| 256 | public function add_inline_style() { |
| 257 | $css = ' |
| 258 | #wpadminbar #wp-admin-bar-jet_plugins > .ab-item::before { |
| 259 | content: ""; |
| 260 | width: 20px; |
| 261 | height: 18px; |
| 262 | top: 3px; |
| 263 | background-size: contain; |
| 264 | background-repeat: no-repeat; |
| 265 | background-position: center center; |
| 266 | background-image: url("data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0iI2E3YWFhZCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDA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LjQ2ODhDMTAuOTAzMiAxNi4yNzQ5IDEyLjU5MyAxNi4xMzM4IDEzLjgyNjEgMTUuMjQ3MkwxMy44MTg0IDE1LjIzNzFDMTQuMTAyNiAxNS4wNjMzIDE0LjI5MDQgMTQuNzUxIDE0LjI5MDQgMTQuMzk1OEMxNC4yOTA0IDEzLjg0OTIgMTMuODQzOCAxMy40MDU5IDEzLjI5MzIgMTMuNDA1OUMxMy4wMjY4IDEzLjQwNTkgMTIuNzgzMyAxMy41MDkyIDEyLjYwNTcgMTMuNjgwNUMxMi4wMDY5IDE0LjA4MSAxMS4yMTAyIDE0LjE0MzkgMTAuNTM3OCAxMy43NzYyTDE0LjU2NDQgMTEuOTE5OEMxNC43OTc4IDExLjg0OTMgMTUuMDA1OSAxMS42OTMxIDE1LjEzNTMgMTEuNDY2NEMxNS4yOTI2IDExLjE5NjkgMTUuMzA3OCAxMC44ODcxIDE1LjIwMzggMTAuNjE3NlpNMTIuNDg2NCAxMC4zMTUzQzEyLjYwNTcgMTAuMzgzMyAxMi43MTIyIDEwLjQ2MTQgMTIuODExMiAxMC41NDcxTDkuNDk3NTQgMTIuMDcwOUM5LjQ4OTkzIDExLjcyMDggOS41NzYyIDExLjM2NTcgOS43NjM5NSAxMS4wNDA3QzEwLjMxNDUgMTAuMDkzNyAxMS41MzI0IDkuNzY4NzQgMTIuNDg2NCAxMC4zMTUzWiIgZmlsbD0iI2E3YWFhZCIvPjwvc3ZnPg==")!important; |
| 267 | } |
| 268 | #wpadminbar .jet-ab-item .ab-item { |
| 269 | display: flex; |
| 270 | justify-content: space-between; |
| 271 | align-items: center; |
| 272 | gap: 10px; |
| 273 | } |
| 274 | #wpadminbar .jet-ab-title { |
| 275 | white-space: nowrap; |
| 276 | text-overflow: ellipsis; |
| 277 | overflow: hidden; |
| 278 | width: 100%; |
| 279 | } |
| 280 | #wpadminbar .jet-ab-sub-title { |
| 281 | padding: 4px 8px; |
| 282 | font-size: 11px; |
| 283 | line-height: 9px; |
| 284 | background: #55595c; |
| 285 | border-radius: 3px; |
| 286 | } |
| 287 | '; |
| 288 | |
| 289 | wp_add_inline_style( 'admin-bar', $css ); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Returns the instance. |
| 294 | * |
| 295 | * @param array $args |
| 296 | * @access public |
| 297 | * @return object |
| 298 | */ |
| 299 | public static function get_instance( array $args = array() ) { |
| 300 | // If the single instance hasn't been set, set it now. |
| 301 | if ( null == self::$instance ) { |
| 302 | self::$instance = new self( $args ); |
| 303 | } |
| 304 | |
| 305 | return self::$instance; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 |