Skip to content
9 changes: 8 additions & 1 deletion inc/admin-pages/class-shortcodes-admin-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace WP_Ultimo\Admin_Pages;

use WP_Ultimo\UI\Base_Element;
use WP_Ultimo\UI\Field;

// Exit if accessed directly
defined('ABSPATH') || exit;
Expand Down Expand Up @@ -151,7 +152,13 @@ public function get_data() {
$params[ $key ]['options'] = '0 | 1';
break;
case 'select':
$params[ $key ]['options'] = implode(' | ', array_keys(wu_get_isset($value, 'options', [])));
$options = wu_get_isset($value, 'options', []);

if (is_callable($options)) {
$options = call_user_func($options, new Field($key, $value));
}

$params[ $key ]['options'] = implode(' | ', array_keys($options));
break;
case 'int':
$params[ $key ]['options'] = __('integer', 'ultimate-multisite');
Expand Down
65 changes: 52 additions & 13 deletions inc/builders/block-editor/class-block-editor-widget-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,39 +198,78 @@ public function load_block_settings($blocks, $element) {
}

/**
* Generates the list of attributes supported based on the fields.
* Generates the list of attributes supported based on element field types and defaults.
*
* @since 2.0.0
* Field definitions are read as metadata only; option providers are not evaluated.
*
* @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 = $element->fields();

$_fields = [];
$attribute_fields = [];

foreach ($fields as $field_id => $field) {
$type = 'string';
if ( ! is_array($field)) {
continue;
}

$field_type = $field['type'] ?? 'text';

if ('group' === $field_type && ! empty($field['fields']) && is_array($field['fields'])) {
foreach ($field['fields'] as $sub_field_id => $sub_field) {
if (is_array($sub_field)) {
$attribute_fields[ $sub_field_id ] = $sub_field;
}
}

if ('toggle' === $field['type']) {
$type = 'boolean';
continue;
}

if ('number' === $field['type']) {
$type = 'integer';
if (in_array($field_type, ['header', 'note'], true)) {
continue;
}

$default_value = wu_get_isset($defaults, $field_id, '');
$attribute_fields[ $field_id ] = $field;
}

$attributes = [];

foreach ($attribute_fields as $field_id => $field) {
$has_default = array_key_exists($field_id, $defaults);
$default_value = $has_default ? $defaults[ $field_id ] : ($field['value'] ?? '');

if ( ! $has_default && ! is_string($default_value) && is_callable($default_value)) {
$default_value = '';
}

$field_type = $field['type'] ?? 'text';
$type = 'string';

if ('toggle' === $field_type) {
$type = 'boolean';
$default_value = wu_string_to_bool($default_value);
}

if (in_array($field_type, ['int', 'number'], true)) {
$type = is_float($default_value) ? 'number' : 'integer';
$default_value = 'number' === $type ? (float) $default_value : (int) $default_value;
}

if ('string' === $type && is_scalar($default_value)) {
$default_value = (string) $default_value;
}

$_fields[ $field_id ] = [
'default' => wu_get_isset($field, 'value', $default_value),
$attributes[ $field_id ] = [
'default' => $default_value,
'type' => $type,
];
}

return $_fields;
return $attributes;
}
}
6 changes: 6 additions & 0 deletions inc/compat/class-gutenberg-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public function init(): void {
*/
public function add_scripts(): void {

$screen = get_current_screen();

if ( ! $screen || ! $screen->is_block_editor()) {
return;
}

wp_register_script('wu-gutenberg-support', wu_get_asset('gutenberg-support.js', 'js'), ['jquery'], wu_get_version(), true);

// translators: the placeholder is replaced with the network name.
Expand Down
35 changes: 35 additions & 0 deletions inc/functions/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,41 @@ function wu_is_new_site_page() {
return absint(wu_get_setting('default_new_site_page', 0)) === $post->ID;
}

/**
* Returns a request-memoized list of pages for select field options.
*
* @since 2.0.0
* @param string $default_label The label for the current page option.
* @return array
*/
function wu_get_pages_as_options($default_label) {

static $pages_by_context = [];

$current_page_id = get_the_ID();

if ( ! $current_page_id && is_admin()) {
$requested_page_id = wu_request('post', 0);
$current_page_id = is_scalar($requested_page_id) ? absint($requested_page_id) : 0;
}

$context_key = implode(':', [get_current_blog_id(), $current_page_id, get_current_user_id(), determine_locale()]);

if ( ! array_key_exists($context_key, $pages_by_context)) {
$page_query_args = $current_page_id ? ['exclude' => [$current_page_id]] : []; // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude

$pages_by_context[ $context_key ] = get_pages($page_query_args) ?: [];
}

$pages_list = [0 => $default_label];

foreach ($pages_by_context[ $context_key ] as $page) {
$pages_list[ $page->ID ] = $page->post_title;
}

return $pages_list;
}

/**
* Checks if the current page is a login page.
*
Expand Down
16 changes: 1 addition & 15 deletions inc/ui/class-current-site-element.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,12 @@ public function fields() {
'value' => 1,
];

$pages = get_pages(
[
'exclude' => [get_the_ID()], // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
]
);

$pages = $pages ?: [];

$pages_list = [0 => __('Current Page', 'ultimate-multisite')];

foreach ($pages as $page) {
$pages_list[ $page->ID ] = $page->post_title;
}

$fields['breadcrumbs_my_sites_page'] = [
'type' => 'select',
'title' => __('My Sites Page', 'ultimate-multisite'),
'value' => 0,
'desc' => __('The page with the customer sites list.', 'ultimate-multisite'),
'options' => $pages_list,
'options' => fn() => wu_get_pages_as_options(__('Current Page', 'ultimate-multisite')),
];

$fields['display_description'] = [
Expand Down
16 changes: 1 addition & 15 deletions inc/ui/class-my-sites-element.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,6 @@ public function fields() {
],
];

$pages = get_pages(
[
'exclude' => [get_the_ID()], // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
]
);

$pages = $pages ?: [];

$pages_list = [0 => __('Current Page', 'ultimate-multisite')];

foreach ($pages as $page) {
$pages_list[ $page->ID ] = $page->post_title;
}

$fields['custom_manage_page'] = [
'type' => 'select',
'title' => __('Manage Redirect Page', 'ultimate-multisite'),
Expand All @@ -185,7 +171,7 @@ public function fields() {
'required' => [
'site_manage_type' => 'custom_page',
],
'options' => $pages_list,
'options' => fn() => wu_get_pages_as_options(__('Current Page', 'ultimate-multisite')),
];

$fields['columns'] = [
Expand Down
16 changes: 1 addition & 15 deletions inc/ui/class-site-actions-element.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,27 +182,13 @@ public function fields() {
'value' => 1,
];

$pages = get_pages(
[
'exclude' => [get_the_ID()], // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
]
);

$pages = $pages ?: [];

$pages_list = [0 => __('Default', 'ultimate-multisite')];

foreach ($pages as $page) {
$pages_list[ $page->ID ] = $page->post_title;
}

$fields['redirect_after_delete'] = [
'type' => 'select',
'title' => __('Redirect After Delete', 'ultimate-multisite'),
'value' => 0,
'desc' => __('The page to redirect user after delete current site.', 'ultimate-multisite'),
'tooltip' => '',
'options' => $pages_list,
'options' => fn() => wu_get_pages_as_options(__('Default', 'ultimate-multisite')),
];

return $fields;
Expand Down
Loading
Loading