init
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
.block-editor #uxbuilder-edit-button {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
margin-right: 12px;
|
||||
height: 36px;
|
||||
}
|
||||
|
||||
.block-editor #uxbuilder-edit-button svg {
|
||||
margin-right: 5px;
|
||||
}
|
||||
|
||||
.block-editor #uxbuilder-edit-button.is-busy {
|
||||
cursor: default;
|
||||
pointer-events: none;
|
||||
}
|
@@ -0,0 +1,94 @@
|
||||
/* global wp */
|
||||
(function () {
|
||||
var el = wp.element.createElement
|
||||
var useDispatch = wp.data.useDispatch
|
||||
var InnerBlocks = wp.blockEditor.InnerBlocks
|
||||
var registerBlockType = wp.blocks.registerBlockType
|
||||
|
||||
function FlatsomeIcon (props) {
|
||||
return el(
|
||||
'svg',
|
||||
{
|
||||
width: props.width || 24,
|
||||
height: props.height || 24,
|
||||
viewBox: '0 0 33 32',
|
||||
fill: 'none',
|
||||
xmlns: 'http://www.w3.org/2000/svg'
|
||||
},
|
||||
[
|
||||
el('path', { key: 0, fill: 'currentColor', d: 'M15.9433 4.47626L18.474 7.00169L7.01624 18.4357L4.48556 15.9103L15.9433 4.47626ZM15.9433 0L0 15.9103L7.01624 22.912L22.9596 7.00169L15.9433 0Z' }),
|
||||
el('path', { key: 1, fill: 'currentColor', d: 'M16.128 22.83L18.4798 25.1769L16.128 27.5239L13.7761 25.1769L16.128 22.83ZM16.128 18.3537L9.29039 25.1766L16.128 32L22.9655 25.1766L16.128 18.3532V18.3537Z' }),
|
||||
el('path', { key: 2, fill: 'currentColor', fillOpacity: '0.6', d: 'M25.229 13.7475L27.5808 16.0945L25.229 18.4414L22.8775 16.0946L25.2293 13.7477L25.229 13.7475ZM25.2293 9.27141L18.3914 16.0946L25.229 22.918L32.0666 16.0946L25.229 9.27124L25.2293 9.27141Z' })
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
function gotoUxBuilder () {
|
||||
if (window.flatsome_gutenberg) {
|
||||
window.top.location.href = window.flatsome_gutenberg.edit_button.url
|
||||
}
|
||||
}
|
||||
|
||||
function DefaultBlockEdit () {
|
||||
var editor = useDispatch('core/editor')
|
||||
|
||||
return el(
|
||||
wp.components.Placeholder,
|
||||
{
|
||||
icon: el(FlatsomeIcon, { width: 21, height: 21 }),
|
||||
label: 'UX Builder content',
|
||||
instructions: 'This content can only be edited in UX Builder.'
|
||||
},
|
||||
el(
|
||||
wp.components.Button,
|
||||
{
|
||||
isDefault: true,
|
||||
onFocus: function (e) {
|
||||
e.stopPropagation()
|
||||
},
|
||||
onClick: function () {
|
||||
editor.savePost().then(gotoUxBuilder)
|
||||
}
|
||||
},
|
||||
'Edit with UX Builder'
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
function DefaultBlockSaveInnerBlocks () {
|
||||
return el(InnerBlocks.Content)
|
||||
}
|
||||
|
||||
function DefaultBlockSaveRawHTML (props) {
|
||||
return el(wp.element.RawHTML, {}, props.attributes.content)
|
||||
}
|
||||
|
||||
for (var name in window.flatsomeBlockSettings) {
|
||||
if (window.flatsomeBlockSettings.hasOwnProperty(name)) {
|
||||
var data = window.flatsomeBlockSettings[name]
|
||||
|
||||
registerBlockType(name, {
|
||||
title: 'UX Builder content',
|
||||
description: 'This block contains content created with UX Builder.',
|
||||
category: 'common',
|
||||
attributes: data.attributes,
|
||||
supports: Object.assign({}, {
|
||||
html: false,
|
||||
align: false,
|
||||
anchor: false,
|
||||
reusable: false,
|
||||
inserter: false,
|
||||
alignWide: false,
|
||||
className: false,
|
||||
customClassName: false,
|
||||
defaultStylePicker: false
|
||||
}, data.supports),
|
||||
icon: FlatsomeIcon,
|
||||
edit: DefaultBlockEdit,
|
||||
save: name === 'flatsome/uxbuilder'
|
||||
? DefaultBlockSaveRawHTML
|
||||
: DefaultBlockSaveInnerBlocks
|
||||
})
|
||||
}
|
||||
}
|
||||
}())
|
@@ -0,0 +1,69 @@
|
||||
/* global flatsome_gutenberg, wp */
|
||||
(function () {
|
||||
'use strict'
|
||||
|
||||
var FlatsomeGutenberg = {
|
||||
headerToolbar: null,
|
||||
editButton: null,
|
||||
init: function () {
|
||||
if (!flatsome_gutenberg.edit_button.enabled) {
|
||||
return
|
||||
}
|
||||
|
||||
this.buttonText = flatsome_gutenberg.edit_button.text
|
||||
this.editUrl = flatsome_gutenberg.edit_button.url
|
||||
this.buttonIcon = '<svg style="width:16px; height:16px;" width="16" height="16" viewBox="0 0 33 32" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M15.9433 4.47626L18.474 7.00169L7.01624 18.4357L4.48556 15.9103L15.9433 4.47626ZM15.9433 0L0 15.9103L7.01624 22.912L22.9596 7.00169L15.9433 0Z" fill="white"/> <path d="M16.128 22.83L18.4798 25.1769L16.128 27.5239L13.7761 25.1769L16.128 22.83ZM16.128 18.3537L9.29039 25.1766L16.128 32L22.9655 25.1766L16.128 18.3532V18.3537Z" fill="white"/> <path d="M25.229 13.7475L27.5808 16.0945L25.229 18.4414L22.8775 16.0946L25.2293 13.7477L25.229 13.7475ZM25.2293 9.27141L18.3914 16.0946L25.229 22.918L32.0666 16.0946L25.229 9.27124L25.2293 9.27141Z" fill="white" fill-opacity="0.6"/> </svg>'
|
||||
|
||||
this.addEditButton()
|
||||
this.bindEvents()
|
||||
},
|
||||
addEditButton: function () {
|
||||
this.headerToolbar = document.querySelector('.block-editor .edit-post-header__toolbar')
|
||||
|
||||
if (!this.headerToolbar) return
|
||||
this.headerToolbar.insertAdjacentHTML('beforeend',
|
||||
'<button id="uxbuilder-edit-button" class="components-button is-button is-primary is-large">' + this.buttonIcon + this.buttonText + '</button>')
|
||||
|
||||
this.editButton = this.headerToolbar.querySelector('#uxbuilder-edit-button')
|
||||
},
|
||||
bindEvents: function () {
|
||||
var self = this
|
||||
|
||||
if (!this.editButton) return
|
||||
this.editButton.addEventListener('click', function (e) {
|
||||
e.preventDefault()
|
||||
|
||||
this.classList.add('is-busy')
|
||||
this.blur()
|
||||
|
||||
var title = wp.data.select('core/editor').getEditedPostAttribute('title')
|
||||
if (!title) wp.data.dispatch('core/editor').editPost({title: 'Auto Draft'})
|
||||
|
||||
wp.data.dispatch('core/editor').savePost()
|
||||
self.redirectToBuilder()
|
||||
}, false)
|
||||
},
|
||||
redirectToBuilder: function () {
|
||||
var self = this
|
||||
|
||||
setTimeout(function () {
|
||||
if (wp.data.select('core/editor').isSavingPost()) {
|
||||
return self.redirectToBuilder()
|
||||
}
|
||||
if (wp.data.select('core/editor').didPostSaveRequestSucceed()) {
|
||||
window.top.location.href = self.editUrl
|
||||
self.editButton.innerHTML += '...'
|
||||
} else {
|
||||
self.editButton.classList.remove('is-busy')
|
||||
}
|
||||
}, 500)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
setTimeout(function () {
|
||||
FlatsomeGutenberg.init()
|
||||
}, 10)
|
||||
})
|
||||
}())
|
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* Block editor (Gutenberg)
|
||||
*
|
||||
* @author UX Themes
|
||||
* @category Gutenberg
|
||||
* @package Flatsome\Admin
|
||||
* @since 3.7.0
|
||||
*/
|
||||
|
||||
namespace Flatsome\Admin;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Class Gutenberg
|
||||
*
|
||||
* @package Flatsome\Admin
|
||||
*/
|
||||
class Gutenberg {
|
||||
|
||||
/**
|
||||
* Current version
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $version;
|
||||
|
||||
/**
|
||||
* Holds assets directory.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $assets;
|
||||
|
||||
/**
|
||||
* Gutenberg constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$theme = wp_get_theme( get_template() );
|
||||
|
||||
$this->version = $theme->get( 'Version' );
|
||||
$this->assets = get_template_directory_uri() . '/inc/admin/gutenberg/assets';
|
||||
|
||||
$this->init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise
|
||||
*/
|
||||
private function init() {
|
||||
if ( $this->is_block_editor_available() ) {
|
||||
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles' ] );
|
||||
add_action( 'enqueue_block_editor_assets', [ $this, 'add_edit_button' ], 11 );
|
||||
add_action( 'enqueue_block_editor_assets', [ $this, 'add_block_settings' ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if block editor is available.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_block_editor_available() {
|
||||
return function_exists( 'register_block_type' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a post type should display the Gutenberg/block editor.
|
||||
*
|
||||
* @param string $post_type The name of the post type.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_post_type_gutenberg( $post_type ) {
|
||||
if ( function_exists( 'use_block_editor_for_post_type' ) ) {
|
||||
// WP 5.0 or greater.
|
||||
return use_block_editor_for_post_type( $post_type );
|
||||
} elseif ( function_exists( 'gutenberg_can_edit_post_type' ) ) {
|
||||
// Lower then WP 5.0 and Gutenberg plugin is installed.
|
||||
return gutenberg_can_edit_post_type( $post_type );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register and enqueue main styles.
|
||||
*/
|
||||
public function enqueue_styles() {
|
||||
wp_register_style( 'flatsome-gutenberg', $this->assets . '/css/style.css', [], $this->version );
|
||||
wp_enqueue_style( 'flatsome-gutenberg' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add 'Edit with UX Builder' inside gutenberg editor header.
|
||||
*/
|
||||
public function add_edit_button() {
|
||||
global $typenow;
|
||||
if ( ! $this->is_post_type_gutenberg( $typenow ) ) {
|
||||
return;
|
||||
}
|
||||
wp_enqueue_script( 'flatsome-gutenberg-edit-button', $this->assets . '/js/edit-button.js', array( 'wp-edit-post' ), $this->version, true );
|
||||
|
||||
$page_id = get_the_ID();
|
||||
|
||||
$params = [
|
||||
'edit_button' => [
|
||||
'enabled' => $this->is_edit_button_visible( $page_id ),
|
||||
'text' => __( 'Edit with UX Builder', 'flatsome' ),
|
||||
'url' => ux_builder_edit_url( $page_id ),
|
||||
],
|
||||
];
|
||||
|
||||
wp_localize_script( 'flatsome-gutenberg-edit-button', 'flatsome_gutenberg', $params );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the `window.flatsomeBlockSettings` object.
|
||||
*/
|
||||
public function add_block_settings() {
|
||||
$blocks_js = $this->assets . '/js/blocks.js';
|
||||
$blocks_deps = array(
|
||||
'wp-data',
|
||||
'wp-blocks',
|
||||
'wp-element',
|
||||
'wp-edit-post',
|
||||
'wp-components',
|
||||
'wp-block-editor',
|
||||
);
|
||||
$block_settings = array_filter(
|
||||
get_block_editor_server_block_settings(),
|
||||
function( $block, $name ) {
|
||||
return strpos( $name, 'flatsome/' ) === 0;
|
||||
},
|
||||
ARRAY_FILTER_USE_BOTH
|
||||
);
|
||||
|
||||
wp_enqueue_script( 'flatsome-blocks', $blocks_js, $blocks_deps, $this->version, true );
|
||||
wp_localize_script( 'flatsome-blocks', 'flatsomeBlockSettings', $block_settings );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines when the edit button should be visible or not.
|
||||
*
|
||||
* @param int $page_id The ID of the current post.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_edit_button_visible( $page_id ) {
|
||||
// Do not show UX Builder link on Shop page.
|
||||
if ( function_exists( 'is_woocommerce' ) && $page_id == wc_get_page_id( 'shop' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Do not show UX Builder link on Posts Page.
|
||||
$page_for_posts = get_option( 'page_for_posts' );
|
||||
if ( $page_id == $page_for_posts ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
new Gutenberg();
|
Reference in New Issue
Block a user