| 1 |
<?php |
| 2 |
namespace Elementor\Modules\Gutenberg; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Module as BaseModule; |
| 5 |
use Elementor\Core\Experiments\Manager as Experiments_Manager; |
| 6 |
use Elementor\Plugin; |
| 7 |
use Elementor\User; |
| 8 |
use Elementor\Utils; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly. |
| 12 |
} |
| 13 |
|
| 14 |
class Module extends BaseModule { |
| 15 |
|
| 16 |
protected $is_gutenberg_editor_active = false; |
| 17 |
|
| 18 |
/** |
| 19 |
* @since 2.1.0 |
| 20 |
* @access public |
| 21 |
*/ |
| 22 |
public function get_name() { |
| 23 |
return 'gutenberg'; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @since 2.1.0 |
| 28 |
* @access public |
| 29 |
* @static |
| 30 |
*/ |
| 31 |
public static function is_active() { |
| 32 |
return function_exists( 'register_block_type' ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @since 2.1.0 |
| 37 |
* @access public |
| 38 |
*/ |
| 39 |
public function register_elementor_rest_field() { |
| 40 |
register_rest_field( get_post_types( '', 'names' ), |
| 41 |
'gutenberg_elementor_mode', [ |
| 42 |
'update_callback' => function( $request_value, $object ) { |
| 43 |
if ( ! User::is_current_user_can_edit( $object->ID ) ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
$document = Plugin::$instance->documents->get( $object->ID ); |
| 48 |
|
| 49 |
if ( ! $document ) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
$document->set_is_built_with_elementor( false ); |
| 54 |
|
| 55 |
return true; |
| 56 |
}, |
| 57 |
] |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @since 2.1.0 |
| 63 |
* @access public |
| 64 |
*/ |
| 65 |
public function enqueue_assets() { |
| 66 |
$document = Plugin::$instance->documents->get( get_the_ID() ); |
| 67 |
|
| 68 |
if ( ! $document || ! $document->is_editable_by_current_user() ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$this->is_gutenberg_editor_active = true; |
| 73 |
|
| 74 |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 75 |
|
| 76 |
wp_enqueue_script( 'elementor-gutenberg', ELEMENTOR_ASSETS_URL . 'js/gutenberg' . $suffix . '.js', [ 'jquery' ], ELEMENTOR_VERSION, true ); |
| 77 |
|
| 78 |
$elementor_settings = [ |
| 79 |
'isElementorMode' => $document->is_built_with_elementor(), |
| 80 |
'editLink' => $document->get_edit_url(), |
| 81 |
]; |
| 82 |
Utils::print_js_config( 'elementor-gutenberg', 'ElementorGutenbergSettings', $elementor_settings ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @since 2.1.0 |
| 87 |
* @access public |
| 88 |
*/ |
| 89 |
public function print_admin_js_template() { |
| 90 |
if ( ! $this->is_gutenberg_editor_active ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
?> |
| 95 |
<script id="elementor-gutenberg-button-switch-mode" type="text/html"> |
| 96 |
<div id="elementor-switch-mode"> |
| 97 |
<button id="elementor-switch-mode-button" type="button" class="button button-primary button-large"> |
| 98 |
<span class="elementor-switch-mode-on"><?php echo esc_html__( '← Back to WordPress Editor', 'elementor' ); ?></span> |
| 99 |
<span class="elementor-switch-mode-off"> |
| 100 |
<i class="eicon-elementor-square" aria-hidden="true"></i> |
| 101 |
<?php echo esc_html__( 'Edit with Elementor', 'elementor' ); ?> |
| 102 |
</span> |
| 103 |
</button> |
| 104 |
</div> |
| 105 |
</script> |
| 106 |
|
| 107 |
<script id="elementor-gutenberg-panel" type="text/html"> |
| 108 |
<div id="elementor-editor"> |
| 109 |
<div id="elementor-go-to-edit-page-link"> |
| 110 |
<button id="elementor-editor-button" class="button button-primary button-hero"> |
| 111 |
<i class="eicon-elementor-square" aria-hidden="true"></i> |
| 112 |
<?php echo esc_html__( 'Edit with Elementor', 'elementor' ); ?> |
| 113 |
</button> |
| 114 |
<div class="elementor-loader-wrapper"> |
| 115 |
<div class="elementor-loader"> |
| 116 |
<div class="elementor-loader-boxes"> |
| 117 |
<div class="elementor-loader-box"></div> |
| 118 |
<div class="elementor-loader-box"></div> |
| 119 |
<div class="elementor-loader-box"></div> |
| 120 |
<div class="elementor-loader-box"></div> |
| 121 |
</div> |
| 122 |
</div> |
| 123 |
<div class="elementor-loading-title"><?php echo esc_html__( 'Loading', 'elementor' ); ?></div> |
| 124 |
</div> |
| 125 |
</div> |
| 126 |
</div> |
| 127 |
</script> |
| 128 |
<?php |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @since 2.1.0 |
| 133 |
* @access public |
| 134 |
*/ |
| 135 |
public function __construct() { |
| 136 |
$this->register_experiments(); |
| 137 |
|
| 138 |
add_action( 'rest_api_init', [ $this, 'register_elementor_rest_field' ] ); |
| 139 |
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_assets' ] ); |
| 140 |
add_action( 'admin_footer', [ $this, 'print_admin_js_template' ] ); |
| 141 |
|
| 142 |
add_action( 'wp_enqueue_scripts', [ $this, 'dequeue_assets' ], 999 ); |
| 143 |
} |
| 144 |
|
| 145 |
public function register_experiments() { |
| 146 |
Plugin::$instance->experiments->add_feature( [ |
| 147 |
'name' => 'block_editor_assets_optimize', |
| 148 |
'title' => esc_html__( 'Optimized Gutenberg Loading', 'elementor' ), |
| 149 |
'description' => esc_html__( 'Use this experiment to reduce unnecessary render-blocking loads, enhancing site performance by dequeuing unused Gutenberg block editor files (styles and scripts).', 'elementor' ), |
| 150 |
'release_status' => Experiments_Manager::RELEASE_STATUS_STABLE, |
| 151 |
'default' => Experiments_Manager::STATE_ACTIVE, |
| 152 |
'tag' => esc_html__( 'Performance', 'elementor' ), |
| 153 |
'generator_tag' => true, |
| 154 |
] ); |
| 155 |
} |
| 156 |
|
| 157 |
public function dequeue_assets() { |
| 158 |
if ( ! Plugin::$instance->experiments->is_feature_active( 'block_editor_assets_optimize' ) ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
if ( ! static::should_dequeue_gutenberg_assets() ) { |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
wp_dequeue_style( 'wp-block-library' ); |
| 167 |
wp_dequeue_style( 'wp-block-library-theme' ); |
| 168 |
wp_dequeue_style( 'wc-block-style' ); |
| 169 |
wp_dequeue_style( 'wc-blocks-style' ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Check whether the "Optimized Gutenberg Loading" settings is enabled. |
| 174 |
* |
| 175 |
* The 'elementor_optimized_gutenberg_loading' option can be enabled/disabled from the Elementor settings. |
| 176 |
* |
| 177 |
* @since 3.21.0 |
| 178 |
* @access private |
| 179 |
*/ |
| 180 |
private static function is_optimized_gutenberg_loading_enabled() : bool { |
| 181 |
return '1' === get_option( 'elementor_optimized_gutenberg_loading', '1' ); |
| 182 |
} |
| 183 |
|
| 184 |
private static function should_dequeue_gutenberg_assets() : bool { |
| 185 |
$post = get_post(); |
| 186 |
|
| 187 |
if ( empty( $post->ID ) ) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
if ( ! static::is_built_with_elementor( $post ) ) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
if ( static::is_gutenberg_in_post( $post ) ) { |
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
private static function is_built_with_elementor( $post ) : bool { |
| 203 |
$document = Plugin::$instance->documents->get( $post->ID ); |
| 204 |
|
| 205 |
if ( ! $document || ! $document->is_built_with_elementor() ) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
return true; |
| 210 |
} |
| 211 |
|
| 212 |
private static function is_gutenberg_in_post( $post ) : bool { |
| 213 |
if ( has_blocks( $post ) ) { |
| 214 |
return true; |
| 215 |
} |
| 216 |
|
| 217 |
if ( static::current_theme_is_fse_theme() ) { |
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
private static function current_theme_is_fse_theme() : bool { |
| 225 |
if ( function_exists( 'wp_is_block_theme' ) ) { |
| 226 |
return (bool) wp_is_block_theme(); |
| 227 |
} |
| 228 |
if ( function_exists( 'gutenberg_is_fse_theme' ) ) { |
| 229 |
return (bool) gutenberg_is_fse_theme(); |
| 230 |
} |
| 231 |
|
| 232 |
return false; |
| 233 |
} |
| 234 |
} |
| 235 |
|