module.php
144 lines
| 1 | <?php |
| 2 | namespace Elementor\Modules\AdminBar; |
| 3 | |
| 4 | use Elementor\Core\Base\Document; |
| 5 | use Elementor\Core\Base\App as BaseApp; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; // Exit if accessed directly. |
| 9 | } |
| 10 | |
| 11 | class Module extends BaseApp { |
| 12 | /** |
| 13 | * @var Document[] |
| 14 | */ |
| 15 | private $documents = []; |
| 16 | |
| 17 | /** |
| 18 | * @return bool |
| 19 | */ |
| 20 | public static function is_active() { |
| 21 | return is_admin_bar_showing(); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @return string |
| 26 | */ |
| 27 | public function get_name() { |
| 28 | return 'admin-bar'; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Collect the documents that was rendered in the current page. |
| 33 | * |
| 34 | * @param Document $document |
| 35 | * @param $is_excerpt |
| 36 | */ |
| 37 | public function add_document_to_admin_bar( Document $document, $is_excerpt ) { |
| 38 | if ( |
| 39 | $is_excerpt || |
| 40 | ! $document::get_property( 'show_on_admin_bar' ) || |
| 41 | ! $document->is_editable_by_current_user() |
| 42 | ) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | $this->documents[ $document->get_main_id() ] = $document; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Scripts for module. |
| 51 | */ |
| 52 | public function enqueue_scripts() { |
| 53 | if ( empty( $this->documents ) ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | // Should load 'elementor-admin-bar' before 'admin-bar' |
| 58 | wp_dequeue_script( 'admin-bar' ); |
| 59 | |
| 60 | wp_enqueue_script( |
| 61 | 'elementor-admin-bar', |
| 62 | $this->get_js_assets_url( 'elementor-admin-bar' ), |
| 63 | [ 'elementor-frontend-modules' ], |
| 64 | ELEMENTOR_VERSION, |
| 65 | true |
| 66 | ); |
| 67 | |
| 68 | // This is a core script of WordPress, it is not required to pass the 'ver' argument. |
| 69 | wp_enqueue_script( // phpcs:ignore WordPress.WP.EnqueuedResourceParameters |
| 70 | 'admin-bar', |
| 71 | null, |
| 72 | [ 'elementor-admin-bar' ], |
| 73 | false, |
| 74 | true |
| 75 | ); |
| 76 | |
| 77 | $this->print_config( 'elementor-admin-bar' ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Creates admin bar menu items config. |
| 82 | * |
| 83 | * @return array |
| 84 | */ |
| 85 | public function get_init_settings() { |
| 86 | $settings = []; |
| 87 | |
| 88 | if ( ! empty( $this->documents ) ) { |
| 89 | $settings['elementor_edit_page'] = $this->get_edit_button_config(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Admin bar settings in the frontend. |
| 94 | * |
| 95 | * Register admin_bar config to parse later in the frontend and add to the admin bar with JS. |
| 96 | * |
| 97 | * @since 3.0.0 |
| 98 | * |
| 99 | * @param array $settings the admin_bar config |
| 100 | */ |
| 101 | $settings = apply_filters( 'elementor/frontend/admin_bar/settings', $settings ); |
| 102 | |
| 103 | return $settings; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Creates the config for 'Edit with elementor' menu item. |
| 108 | * |
| 109 | * @return array |
| 110 | */ |
| 111 | private function get_edit_button_config() { |
| 112 | $queried_object_id = get_queried_object_id(); |
| 113 | $href = null; |
| 114 | |
| 115 | if ( is_singular() && isset( $this->documents[ $queried_object_id ] ) ) { |
| 116 | $href = $this->documents[ $queried_object_id ]->get_edit_url(); |
| 117 | |
| 118 | unset( $this->documents[ $queried_object_id ] ); |
| 119 | } |
| 120 | |
| 121 | return [ |
| 122 | 'id' => 'elementor_edit_page', |
| 123 | 'title' => esc_html__( 'Edit with Elementor', 'elementor' ), |
| 124 | 'href' => $href, |
| 125 | 'children' => array_map( function ( $document ) { |
| 126 | return [ |
| 127 | 'id' => "elementor_edit_doc_{$document->get_main_id()}", |
| 128 | 'title' => $document->get_post()->post_title, |
| 129 | 'sub_title' => $document::get_title(), |
| 130 | 'href' => $document->get_edit_url(), |
| 131 | ]; |
| 132 | }, $this->documents ), |
| 133 | ]; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Module constructor. |
| 138 | */ |
| 139 | public function __construct() { |
| 140 | add_action( 'elementor/frontend/before_get_builder_content', [ $this, 'add_document_to_admin_bar' ], 10, 2 ); |
| 141 | add_action( 'wp_footer', [ $this, 'enqueue_scripts' ], 11 /* after third party scripts */ ); |
| 142 | } |
| 143 | } |
| 144 |