Initial Commit

This commit is contained in:
David Stone
2024-11-30 18:24:12 -07:00
commit e8f7955c1c
5432 changed files with 1397750 additions and 0 deletions

View File

@ -0,0 +1,279 @@
(function(blocks, element, components, wu_blocks, _) {
const el = element.createElement,
registerBlockType = blocks.registerBlockType,
ServerSideRender = wp.serverSideRender,
PanelBody = wp.components.PanelBody,
TextControl = wp.components.TextControl,
NumberControl = wp.components.__experimentalNumberControl,
RangeControl = wp.components.RangeControl,
ToggleControl = wp.components.ToggleControl,
TextareaControl = wp.components.TextareaControl,
Notice = wp.components.Notice,
SelectControl = wp.components.SelectControl,
InspectorControls = wp.blockEditor.InspectorControls;
_.each(wu_blocks, function(block, index) {
registerBlockType(block.id, {
icon: wu_badge(),
category: 'wp-ultimo',
title: block.title,
description: block.description,
keywords: block.keywords,
supports: {
multiple: false,
html: false,
},
edit(props) {
return [
el(ServerSideRender, {
key: block.id + index,
block: block.id,
attributes: props.attributes,
}),
wu_fields_to_block_options(block.fields, props),
];
},
save() {
return null;
},
});
}); // end each;
function wu_fields_to_block_options(fields, props) {
const gt_panels = [];
let gt_fields = [];
let current_panel = null;
let current_panel_slug = null;
let new_panel = false;
let first_panel = true;
_.each(fields, function(field, field_slug) {
if (field.type === 'group') {
const sub_fields = field.fields;
field = _.first(_.values(sub_fields));
field.desc = '';
field_slug = _.first(_.keys(sub_fields));
} // end if;
const component = wu_get_field_component(field.type, field);
if (! _.isObject(field.required)) {
field.required = {};
} // end if;
let should_display = true;
_.each(field.required, function(value, key) {
// eslint-disable-next-line eqeqeq
should_display = props.attributes[key] == value;
});
if (should_display) {
gt_fields.push(el(component, {
key: field_slug,
label: field.title,
help: field.desc,
value: props.attributes[field_slug],
checked: props.attributes[field_slug],
options: wu_format_options(field.options),
min: field.min,
max: field.max,
onChange(value) {
const obj = {};
obj[field_slug] = value;
props.setAttributes(obj);
},
}));
} // end if;
/*
* Handle header types differently
*/
if (field.type === 'header') {
gt_fields.pop();
if (current_panel !== null) {
new_panel = true;
} // end if;
if (new_panel) {
gt_panels.push(
el(PanelBody, {
key: current_panel_slug,
title: current_panel.title,
description: current_panel.desc,
initialOpen: first_panel,
}, gt_fields)
);
first_panel = false;
} // end if;
current_panel = field;
current_panel_slug = field_slug;
gt_fields = [];
} // end if;
});
gt_panels.push(
el(PanelBody, {
key: current_panel_slug,
title: current_panel.title,
help: current_panel.desc,
initialOpen: first_panel,
}, gt_fields)
);
return el(InspectorControls, { key: 'wp-ultimo' }, gt_panels);
} // end wu_fields_to_block_options;
function wu_format_options(options) {
const formatted_options = [];
_.each(options, function(label, value) {
formatted_options.push({
label,
value,
});
});
return formatted_options;
}
function wu_get_field_component(field_type, field) {
let component = TextControl;
switch (field_type) {
case 'toggle':
component = ToggleControl;
break;
case 'number':
if (field.max && field.min) {
component = RangeControl;
} else {
component = NumberControl;
} // end if;
break;
case 'textarea':
component = TextareaControl;
break;
case 'select':
component = SelectControl;
break;
case 'note':
component = Notice;
break;
default:
component = TextControl;
break;
} // end switch;
return component;
}
/* eslint-disable */
function wu_badge() {
return el('svg', {
width: '116px',
height: '116px',
viewBox: '0 0 116 116',
version: '1.1',
xmlnsXlink: 'http://www.w3.org/1999/xlink',
}, el('g', {
transform: 'translate(24.000000, 0.000000)',
fill: '#000',
stroke: 'none',
strokeWidth: 1,
fillRule: 'evenodd',
}, el('g', {
transform: 'translate(7.555556, 0.000000)',
}, el('polygon', {
points: '19.5185185 51.1370873 53.4139809 1.0658141e-14 30.1083134 54.9623572',
}), el('polygon', {
transform: 'translate(16.947731, 88.302800) scale(-1, -1) translate(-16.947731, -88.302800) ',
points: '-1.55687808e-13 111.958709 33.8954624 60.8216216 10.5897949 115.783979',
}), el('polygon', {
points: '19.5185185 51.4162162 23.300226 60.62179 33.9358783 64.4738951 30.0960764 55.2115998',
})), el('path', {
d: 'M15.401 86.662C6.127 80.616 0 70.177 0 58.314c0-18.7 15.222-33.86 34-33.86 2.012 0 3.984.174 5.9.508l-5.802 9.002c-13.569.154-24.52 11.155-24.52 24.704 0 7.837 3.663 14.821 9.378 19.347l-3.555 8.647zm12.932 5.043l5.26-8.343c.263.008.528.012.793.012 13.701 0 24.808-11.061 24.808-24.706 0-8.207-4.018-15.48-10.203-19.973l3.58-8.748C61.861 35.99 68 46.438 68 58.314c0 18.7-15.222 33.859-34 33.859-1.93 0-3.824-.16-5.667-.468z',
})));
}
/* eslint-enable */
}(
window.wp.blocks,
window.wp.element,
window.wp.components,
window.wu_blocks,
window._
));

View File

@ -0,0 +1 @@
!function(e,t,n,o,i){const l=t.createElement,s=e.registerBlockType,r=wp.serverSideRender,c=wp.components.PanelBody,p=wp.components.TextControl,a=wp.components.__experimentalNumberControl,u=wp.components.RangeControl,d=wp.components.ToggleControl,w=wp.components.TextareaControl,m=wp.components.Notice,k=wp.components.SelectControl,h=wp.blockEditor.InspectorControls;function b(e,t){const n=[];let o=[],s=null,r=null,b=!1,g=!0;return i.each(e,(function(e,h){if("group"===e.type){const t=e.fields;(e=i.first(i.values(t))).desc="",h=i.first(i.keys(t))}const y=function(e,t){let n=p;switch(e){case"toggle":n=d;break;case"number":n=t.max&&t.min?u:a;break;case"textarea":n=w;break;case"select":n=k;break;case"note":n=m;break;default:n=p}return n}(e.type,e);i.isObject(e.required)||(e.required={});let x=!0;i.each(e.required,(function(e,n){x=t.attributes[n]==e})),x&&o.push(l(y,{key:h,label:e.title,help:e.desc,value:t.attributes[h],checked:t.attributes[h],options:f(e.options),min:e.min,max:e.max,onChange(e){const n={};n[h]=e,t.setAttributes(n)}})),"header"===e.type&&(o.pop(),null!==s&&(b=!0),b&&(n.push(l(c,{key:r,title:s.title,description:s.desc,initialOpen:g},o)),g=!1),s=e,r=h,o=[])})),n.push(l(c,{key:r,title:s.title,help:s.desc,initialOpen:g},o)),l(h,{key:"wp-ultimo"},n)}function f(e){const t=[];return i.each(e,(function(e,n){t.push({label:e,value:n})})),t}i.each(o,(function(e,t){s(e.id,{icon:l("svg",{width:"116px",height:"116px",viewBox:"0 0 116 116",version:"1.1",xmlnsXlink:"http://www.w3.org/1999/xlink"},l("g",{transform:"translate(24.000000, 0.000000)",fill:"#000",stroke:"none",strokeWidth:1,fillRule:"evenodd"},l("g",{transform:"translate(7.555556, 0.000000)"},l("polygon",{points:"19.5185185 51.1370873 53.4139809 1.0658141e-14 30.1083134 54.9623572"}),l("polygon",{transform:"translate(16.947731, 88.302800) scale(-1, -1) translate(-16.947731, -88.302800) ",points:"-1.55687808e-13 111.958709 33.8954624 60.8216216 10.5897949 115.783979"}),l("polygon",{points:"19.5185185 51.4162162 23.300226 60.62179 33.9358783 64.4738951 30.0960764 55.2115998"})),l("path",{d:"M15.401 86.662C6.127 80.616 0 70.177 0 58.314c0-18.7 15.222-33.86 34-33.86 2.012 0 3.984.174 5.9.508l-5.802 9.002c-13.569.154-24.52 11.155-24.52 24.704 0 7.837 3.663 14.821 9.378 19.347l-3.555 8.647zm12.932 5.043l5.26-8.343c.263.008.528.012.793.012 13.701 0 24.808-11.061 24.808-24.706 0-8.207-4.018-15.48-10.203-19.973l3.58-8.748C61.861 35.99 68 46.438 68 58.314c0 18.7-15.222 33.859-34 33.859-1.93 0-3.824-.16-5.667-.468z"}))),category:"wp-ultimo",title:e.title,description:e.description,keywords:e.keywords,supports:{multiple:!1,html:!1},edit:n=>[l(r,{key:e.id+t,block:e.id,attributes:n.attributes}),b(e.fields,n)],save:()=>null})}))}(window.wp.blocks,window.wp.element,window.wp.components,window.wu_blocks,window._);

View File

@ -0,0 +1,269 @@
<?php
/**
* Handles Block Editor Widget Support.
*
* @package WP_Ultimo\Builders
* @subpackage Block_Editor
* @since 2.0.0
*/
namespace WP_Ultimo\Builders\Block_Editor;
// Exit if accessed directly
defined('ABSPATH') || exit;
use \WP_Ultimo\Database\Sites\Site_Type;
/**
* Handles Block Editor Widget Support.
*
* @since 2.0.0
*/
class Block_Editor_Widget_Manager {
use \WP_Ultimo\Traits\Singleton;
/**
* Runs when Block_Editor element support is first loaded.
*
* @since 2.0.0
* @return void
*/
public function init() {
if (\WP_Ultimo\Compat\Gutenberg_Support::get_instance()->should_load()) {
add_action('wu_element_loaded', array($this, 'handle_element'));
add_action('init', array($this, 'register_scripts'));
add_action('wu_element_is_preview', array($this, 'is_block_preview'));
} // end if;
} // end init;
/**
* Adds the required scripts.
*
* @since 2.0.0
* @return void
*/
public function register_scripts() {
\WP_Ultimo\Scripts::get_instance()->register_script('wu-blocks', wu_get_asset('blocks.js', 'js', 'inc/builders/block-editor/assets'), array('underscore', 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor', 'wu-functions', 'wp-i18n', 'wp-polyfill'));
$blocks = apply_filters('wu_blocks', array());
wp_localize_script('wu-blocks', 'wu_blocks', $blocks);
} // end register_scripts;
/**
* Checks if we are inside a block preview render.
*
* @since 2.0.0
* @param boolean $is_preview The previous preview status from the filter.
* @return boolean
*/
public function is_block_preview($is_preview) {
if (defined('REST_REQUEST') && true === REST_REQUEST && 'edit' === filter_input(INPUT_GET, 'context', FILTER_SANITIZE_STRING)) {
$is_preview = true;
} // end if;
return $is_preview;
} // end is_block_preview;
/**
* Gets called when a new element is registered
*
* @since 2.0.0
*
* @param \WP_Ultimo\UI\Base_Element $element The element being registered.
* @return void
*/
public function handle_element($element) {
if (wu_get_current_site()->get_type() === Site_Type::CUSTOMER_OWNED) {
return;
} // end if;
$this->register_block($element);
add_filter('wu_blocks', fn($blocks) => $this->load_block_settings($blocks, $element));
} // end handle_element;
/**
* Registers block with WordPress.
*
* @since 2.0.0
*
* @param \WP_Ultimo\UI\Base_Element $element The element being registered.
* @return void
*/
public function register_block($element) {
if (\WP_Block_Type_Registry::get_instance()->is_registered($element->get_id())) {
return;
} // end if;
$attributes = $this->get_attributes_from_fields($element);
register_block_type($element->get_id(), array(
'attributes' => $attributes,
'editor_script' => 'wu-blocks',
'render_callback' => \Closure::fromCallable([$element, 'display']),
));
} // end register_block;
/**
* Consolidate field attributes that are callables for blocks.
*
* @since 2.0.9
*
* @param array $fields The list of fields.
* @return array
*/
protected function consolidate_callables($fields) {
$callable_keys = array(
'options',
'value',
);
$fields_to_ignore = array(
'note',
);
foreach ($fields as $field_slug => &$field) {
/*
* Discard fields that are notes and start with _
*/
if (in_array($field['type'], $fields_to_ignore, true) && strncmp($field_slug, '_', strlen('_')) === 0) {
unset($fields[$field_slug]);
} // end if;
/*
* Deal with the group type.
* On those, we need to loop the sub-fields.
*/
if ($field['type'] === 'group') {
foreach ($field['fields'] as &$sub_field) {
foreach ($sub_field as $sub_item => &$sub_value) {
if (in_array($sub_item, $callable_keys, true) && is_callable($sub_value)) {
$sub_value = call_user_func($sub_value);
} // end if;
} // end foreach;
} // end foreach;
} // end if;
/*
* Deal with the regular field types and its
* callables.
*/
foreach ($field as $item => &$value) {
if (in_array($item, $callable_keys, true) && is_callable($value)) {
$value = call_user_func($value);
} // end if;
} // end foreach;
} // end foreach;
return $fields;
} // end consolidate_callables;
/**
* Registers the block so WP Ultimo can add it on the JS side.
*
* @since 2.0.0
*
* @param array $blocks List of blocks registered.
* @param \WP_Ultimo\UI\Base_Element $element The element being registered.
* @return array
*/
public function load_block_settings($blocks, $element) {
$fields = $this->consolidate_callables($element->fields());
$blocks[] = array(
'id' => $element->get_id(),
'title' => $element->get_title(),
'description' => $element->get_description(),
'fields' => $fields,
'keywords' => $element->keywords(),
);
return $blocks;
} // end load_block_settings;
/**
* Generates the list of attributes supported based on the fields.
*
* @since 2.0.0
* @param \WP_Ultimo\UI\Base_Element $element The element being registered.
* @return array
*/
public function get_attributes_from_fields($element) {
$fields = $element->fields();
$defaults = $element->defaults();
$_fields = array();
foreach ($fields as $field_id => $field) {
$type = 'string';
if ($field['type'] === 'toggle') {
$type = 'boolean';
} // end if;
if ($field['type'] === 'number') {
$type = 'integer';
} // end if;
$default_value = wu_get_isset($defaults, $field_id, '');
$_fields[$field_id] = array(
'default' => wu_get_isset($field, 'value', $default_value),
'type' => $type,
);
} // end foreach;
return $_fields;
} // end get_attributes_from_fields;
} // end class Block_Editor_Widget_Manager;