| 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 |
// We should add dependencies to make sure that 'elementor-admin-bar' is loaded before 'admin-bar'. |
| 70 |
wp_enqueue_script( |
| 71 |
'admin-bar', |
| 72 |
null, |
| 73 |
[ 'elementor-admin-bar' ], |
| 74 |
false, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters |
| 75 |
true |
| 76 |
); |
| 77 |
|
| 78 |
$this->print_config( 'elementor-admin-bar' ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Creates admin bar menu items config. |
| 83 |
* |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
public function get_init_settings() { |
| 87 |
$settings = []; |
| 88 |
|
| 89 |
if ( ! empty( $this->documents ) ) { |
| 90 |
$settings['elementor_edit_page'] = $this->get_edit_button_config(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Admin bar settings in the frontend. |
| 95 |
* |
| 96 |
* Register admin_bar config to parse later in the frontend and add to the admin bar with JS. |
| 97 |
* |
| 98 |
* @since 3.0.0 |
| 99 |
* |
| 100 |
* @param array $settings the admin_bar config |
| 101 |
*/ |
| 102 |
$settings = apply_filters( 'elementor/frontend/admin_bar/settings', $settings ); |
| 103 |
|
| 104 |
return $settings; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Creates the config for 'Edit with elementor' menu item. |
| 109 |
* |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
private function get_edit_button_config() { |
| 113 |
$queried_object_id = get_queried_object_id(); |
| 114 |
$href = null; |
| 115 |
|
| 116 |
if ( is_singular() && isset( $this->documents[ $queried_object_id ] ) ) { |
| 117 |
$href = $this->documents[ $queried_object_id ]->get_edit_url(); |
| 118 |
|
| 119 |
unset( $this->documents[ $queried_object_id ] ); |
| 120 |
} |
| 121 |
|
| 122 |
return [ |
| 123 |
'id' => 'elementor_edit_page', |
| 124 |
'title' => esc_html__( 'Edit with Elementor', 'elementor' ), |
| 125 |
'href' => $href, |
| 126 |
'children' => array_map( function ( $document ) { |
| 127 |
return [ |
| 128 |
'id' => "elementor_edit_doc_{$document->get_main_id()}", |
| 129 |
'title' => $document->get_post()->post_title, |
| 130 |
'sub_title' => $document::get_title(), |
| 131 |
'href' => $document->get_edit_url(), |
| 132 |
]; |
| 133 |
}, $this->documents ), |
| 134 |
]; |
| 135 |
} |
| 136 |
|
| 137 |
public function add_clear_cache_in_admin_bar( $admin_bar_config ): array { |
| 138 |
if ( current_user_can( 'manage_options' ) ) { |
| 139 |
$clear_cache_url = add_query_arg( |
| 140 |
[ |
| 141 |
'_wpnonce' => wp_create_nonce( 'elementor_site_clear_cache' ), |
| 142 |
], |
| 143 |
admin_url( 'admin-post.php?action=elementor_site_clear_cache' ), |
| 144 |
); |
| 145 |
|
| 146 |
$admin_bar_config['elementor_edit_page']['children'][] = [ |
| 147 |
'id' => 'elementor_site_clear_cache', |
| 148 |
'title' => esc_html__( 'Clear Files & Data', 'elementor' ), |
| 149 |
'sub_title' => esc_html__( 'Site', 'elementor' ), |
| 150 |
'href' => $clear_cache_url, |
| 151 |
]; |
| 152 |
} |
| 153 |
|
| 154 |
return $admin_bar_config; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Module constructor. |
| 159 |
*/ |
| 160 |
public function __construct() { |
| 161 |
add_action( 'elementor/frontend/before_get_builder_content', [ $this, 'add_document_to_admin_bar' ], 10, 2 ); |
| 162 |
add_action( 'wp_footer', [ $this, 'enqueue_scripts' ], 11 /* after third party scripts */ ); |
| 163 |
|
| 164 |
add_filter( 'elementor/frontend/admin_bar/settings', [ $this, 'add_clear_cache_in_admin_bar' ], 500 ); |
| 165 |
} |
| 166 |
} |
| 167 |
|