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