| 1 |
<?php |
| 2 |
namespace Elementor\Modules\Gutenberg; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Module as BaseModule; |
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\User; |
| 7 |
use Elementor\Utils; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Module extends BaseModule { |
| 14 |
|
| 15 |
protected $is_gutenberg_editor_active = false; |
| 16 |
|
| 17 |
/** |
| 18 |
* @since 2.1.0 |
| 19 |
* @access public |
| 20 |
*/ |
| 21 |
public function get_name() { |
| 22 |
return 'gutenberg'; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @since 2.1.0 |
| 27 |
* @access public |
| 28 |
* @static |
| 29 |
*/ |
| 30 |
public static function is_active() { |
| 31 |
return function_exists( 'register_block_type' ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @since 2.1.0 |
| 36 |
* @access public |
| 37 |
*/ |
| 38 |
public function register_elementor_rest_field() { |
| 39 |
register_rest_field( get_post_types( '', 'names' ), |
| 40 |
'gutenberg_elementor_mode', [ |
| 41 |
'update_callback' => function( $request_value, $object ) { |
| 42 |
if ( ! User::is_current_user_can_edit( $object->ID ) ) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
$document = Plugin::$instance->documents->get( $object->ID ); |
| 47 |
|
| 48 |
if ( ! $document ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
$document->set_is_built_with_elementor( false ); |
| 53 |
|
| 54 |
return true; |
| 55 |
}, |
| 56 |
] |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @since 2.1.0 |
| 62 |
* @access public |
| 63 |
*/ |
| 64 |
public function enqueue_assets() { |
| 65 |
$document = Plugin::$instance->documents->get( get_the_ID() ); |
| 66 |
|
| 67 |
if ( ! $document || ! $document->is_editable_by_current_user() ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$this->is_gutenberg_editor_active = true; |
| 72 |
|
| 73 |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 74 |
|
| 75 |
wp_enqueue_script( 'elementor-gutenberg', ELEMENTOR_ASSETS_URL . 'js/gutenberg' . $suffix . '.js', [ 'jquery' ], ELEMENTOR_VERSION, true ); |
| 76 |
|
| 77 |
$elementor_settings = [ |
| 78 |
'isElementorMode' => $document->is_built_with_elementor(), |
| 79 |
'editLink' => $document->get_edit_url(), |
| 80 |
]; |
| 81 |
Utils::print_js_config( 'elementor-gutenberg', 'ElementorGutenbergSettings', $elementor_settings ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @since 2.1.0 |
| 86 |
* @access public |
| 87 |
*/ |
| 88 |
public function print_admin_js_template() { |
| 89 |
if ( ! $this->is_gutenberg_editor_active ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
?> |
| 94 |
<script id="elementor-gutenberg-button-switch-mode" type="text/html"> |
| 95 |
<div id="elementor-switch-mode"> |
| 96 |
<button id="elementor-switch-mode-button" type="button" class="button button-primary button-large"> |
| 97 |
<span class="elementor-switch-mode-on"><?php echo esc_html__( '← Back to WordPress Editor', 'elementor' ); ?></span> |
| 98 |
<span class="elementor-switch-mode-off"> |
| 99 |
<i class="eicon-elementor-square" aria-hidden="true"></i> |
| 100 |
<?php echo esc_html__( 'Edit with Elementor', 'elementor' ); ?> |
| 101 |
</span> |
| 102 |
</button> |
| 103 |
</div> |
| 104 |
</script> |
| 105 |
|
| 106 |
<script id="elementor-gutenberg-panel" type="text/html"> |
| 107 |
<div id="elementor-editor"><a id="elementor-go-to-edit-page-link" href="#"> |
| 108 |
<div id="elementor-editor-button" class="button button-primary button-hero"> |
| 109 |
<i class="eicon-elementor-square" aria-hidden="true"></i> |
| 110 |
<?php echo esc_html__( 'Edit with Elementor', 'elementor' ); ?> |
| 111 |
</div> |
| 112 |
<div class="elementor-loader-wrapper"> |
| 113 |
<div class="elementor-loader"> |
| 114 |
<div class="elementor-loader-boxes"> |
| 115 |
<div class="elementor-loader-box"></div> |
| 116 |
<div class="elementor-loader-box"></div> |
| 117 |
<div class="elementor-loader-box"></div> |
| 118 |
<div class="elementor-loader-box"></div> |
| 119 |
</div> |
| 120 |
</div> |
| 121 |
<div class="elementor-loading-title"><?php echo esc_html__( 'Loading', 'elementor' ); ?></div> |
| 122 |
</div> |
| 123 |
</a></div> |
| 124 |
</script> |
| 125 |
<?php |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @since 2.1.0 |
| 130 |
* @access public |
| 131 |
*/ |
| 132 |
public function __construct() { |
| 133 |
add_action( 'rest_api_init', [ $this, 'register_elementor_rest_field' ] ); |
| 134 |
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_assets' ] ); |
| 135 |
add_action( 'admin_footer', [ $this, 'print_admin_js_template' ] ); |
| 136 |
} |
| 137 |
} |
| 138 |
|