| 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 |
* Register admin_bar config to parse later in the frontend and add to the admin bar with JS |
| 94 |
* |
| 95 |
* @param array $settings the admin_bar config |
| 96 |
* |
| 97 |
* @since 3.0.0 |
| 98 |
*/ |
| 99 |
return apply_filters( 'elementor/frontend/admin_bar/settings', $settings ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Creates the config for 'Edit with elementor' menu item. |
| 104 |
* |
| 105 |
* @return array |
| 106 |
*/ |
| 107 |
private function get_edit_button_config() { |
| 108 |
$queried_object_id = get_queried_object_id(); |
| 109 |
$href = null; |
| 110 |
|
| 111 |
if ( is_singular() && isset( $this->documents[ $queried_object_id ] ) ) { |
| 112 |
$href = $this->documents[ $queried_object_id ]->get_edit_url(); |
| 113 |
|
| 114 |
unset( $this->documents[ $queried_object_id ] ); |
| 115 |
} |
| 116 |
|
| 117 |
return [ |
| 118 |
'id' => 'elementor_edit_page', |
| 119 |
'title' => __( 'Edit with Elementor', 'elementor' ), |
| 120 |
'href' => $href, |
| 121 |
'children' => array_map( function ( $document ) { |
| 122 |
return [ |
| 123 |
'id' => "elementor_edit_doc_{$document->get_main_id()}", |
| 124 |
'title' => $document->get_post()->post_title, |
| 125 |
'sub_title' => $document::get_title(), |
| 126 |
'href' => $document->get_edit_url(), |
| 127 |
]; |
| 128 |
}, $this->documents ), |
| 129 |
]; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Module constructor. |
| 134 |
*/ |
| 135 |
public function __construct() { |
| 136 |
add_action( 'elementor/frontend/before_get_builder_content', [ $this, 'add_document_to_admin_bar' ], 10, 2 ); |
| 137 |
add_action( 'wp_footer', [ $this, 'enqueue_scripts' ], 11 /* after third party scripts */ ); |
| 138 |
} |
| 139 |
} |
| 140 |
|