Skip to content

Commit

Permalink
v1.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
misaki-web committed Sep 7, 2024
1 parent 5241a5c commit 0dbd617
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 55 deletions.
4 changes: 2 additions & 2 deletions acf-json-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: A custom ACF field type for manipulating JSON data
* Text Domain: acf-json-field
* Author: Misaki F.
* Version: 1.0.6
* Version: 1.0.7
*/

namespace AcfJsonField;
Expand All @@ -18,7 +18,7 @@
# @title Constants
################################################################################

define('ACF_JSON_FIELD_VERSION', '1.0.6');
define('ACF_JSON_FIELD_VERSION', '1.0.7');

################################################################################
# @title Inclusions
Expand Down
9 changes: 5 additions & 4 deletions assets/css/ajf-admin.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.acf-json-field-data {
.acf-json-field-data:not([data-editor-mode="none"]),
.acf-json-field-editor[data-editor-mode="none"] {
display: none;
}

.acf-json-field-editor {
.acf-json-field-editor:not([data-editor-mode="none"]) {
width: 100%;
height: 400px;
max-width: 1000px;
Expand All @@ -11,10 +12,10 @@
border: 1px solid #ccc;
}

.acf-json-field-editor .jse-tag {
.acf-json-field-editor:not([data-editor-mode="none"]) .jse-tag {
color: #000 !important;
}

.acf-json-field-editor .jse-context-menu-button {
.acf-json-field-editor:not([data-editor-mode="none"]) .jse-context-menu-button {
padding: 5px 10px !important;
}
50 changes: 27 additions & 23 deletions assets/js/ajf-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,35 @@ jQuery(document).ready(function ($) {
const $editor = $('#' + textarea_id + '-editor');

if ($editor.length) {
const textarea_value = ajf_decode_html_entities($textarea.val().trim());
const json_data = ajf_str_to_json_data(textarea_value);
const content = {
text: undefined,
json: json_data.json
};

if (json_data.error) {
const error_msg = `ACF JSON Field: Invalid JSON format in \`textarea#${textarea_id}\`.`;
console.error(error_msg, json_data.error, 'Parsed value: ' + textarea_value);
wp.data.dispatch('core/notices').createNotice('error', error_msg, { isDismissible: true });
}
const editor_mode = $editor.attr('data-editor-mode');

const json_editor = new JSONEditor({
target: $editor[0],
props: {
content,
mode: $editor.attr('data-editor-mode'),
askToFormat: false,
onChange: (updated_content) => {
const updated_content_with_text = toTextContent(updated_content);
$textarea.val(updated_content_with_text.text);
}
if (editor_mode != 'none') {
const textarea_value = ajf_decode_html_entities($textarea.val().trim());
const json_data = ajf_str_to_json_data(textarea_value);
const content = {
text: undefined,
json: json_data.json
};

if (json_data.error) {
const error_msg = `ACF JSON Field: Invalid JSON format in \`textarea#${textarea_id}\`.`;
console.error(error_msg, json_data.error, 'Parsed value: ' + textarea_value);
wp.data.dispatch('core/notices').createNotice('error', error_msg, { isDismissible: true });
}
});

const json_editor = new JSONEditor({
target: $editor[0],
props: {
content,
mode: editor_mode,
askToFormat: false,
onChange: (updated_content) => {
const updated_content_with_text = toTextContent(updated_content);
$textarea.val(updated_content_with_text.text);
}
}
});
}
}
});
});
46 changes: 25 additions & 21 deletions includes/class-acf-field-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,41 +51,43 @@ public function render_field_settings($field) {
acf_render_field_setting(
$field,
[
'name' => 'editor_mode',
'label' => __('Editor Mode', 'acf-json-field'),
'type' => 'select',
'choices' => [
'name' => 'editor_mode',
'label' => __('Editor Mode', 'acf-json-field'),
'type' => 'select',
'choices' => [
'none' => 'none',
'table' => 'table',
'text' => 'text',
'tree' => 'tree',
'text' => 'text',
'tree' => 'tree',
],
'required' => false,
'required' => false,
'default_value' => self::DEFAULT_EDITOR_MODE,
'hint' => sprintf(__('Select the editor mode ("%s" by default)', 'acf-json-field'), self::DEFAULT_EDITOR_MODE),
'instructions' => sprintf(__('Select the editor mode ("%s" by default)', 'acf-json-field'), self::DEFAULT_EDITOR_MODE),
]
);

acf_render_field_setting(
$field,
[
'name' => 'editor_height',
'label' => __('Editor Height', 'acf-json-field'),
'type' => 'number',
'step' => 1,
'min' => 1,
'required' => false,
'name' => 'editor_height',
'label' => __('Editor Height', 'acf-json-field'),
'type' => 'number',
'step' => 1,
'min' => 1,
'required' => false,
'default_value' => '',
'hint' => __('Editor height (px) different from the default height defined in the stylesheet', 'acf-json-field'),
'instructions' => __('Editor height (px) different from the default height defined in the stylesheet. If the editor is disabled, the height will apply to the textarea.', 'acf-json-field'),
]
);

acf_render_field_setting(
$field,
[
'label' => __('Default Value', 'acf-json-field'),
'name' => 'default_value',
'label' => __('Default Value', 'acf-json-field'),
'type' => 'textarea',
'required' => false,
'instructions' => __('Default JSON value if no value is entered by the user', 'acf-json-field'),
'type' => 'textarea',
'name' => 'default_value',
]
);
}
Expand All @@ -101,10 +103,12 @@ public function render_field($field) {
$textarea_name = isset($field['name']) ? esc_attr($field['name']) : '';
$field_value = isset($field['value']) ? $field['value'] : '';

if (!is_string($field_value)) {
$field_value = JsonUtils::encode($field_value);
if (is_string($field_value)) {
# Decode the JSON string to ensure it can be re-encoded with consistent formatting.
$field_value = JsonUtils::decode($field_value);
}

$field_value = JsonUtils::encode($field_value);
$textarea_value = esc_textarea($field_value);

if ($textarea_value === '' && isset($field['default_value'])) {
Expand All @@ -130,7 +134,7 @@ public function render_field($field) {

if ($textarea_id !== '' && $textarea_name !== '') {
echo <<<HTML
<textarea id="$textarea_id" class="acf-json-field-data" name="$textarea_name">$textarea_value</textarea>
<textarea id="$textarea_id" class="acf-json-field-data" name="$textarea_name" data-editor-mode="$editor_mode" {$style_attr}>$textarea_value</textarea>
<div id="$editor_id" class="acf-json-field-editor" data-editor-mode="$editor_mode" {$style_attr}></div>
HTML;
}
Expand Down
26 changes: 21 additions & 5 deletions includes/class-json-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ private static function get_id($id) {
return $id;
}

/**
* Decodes a JSON string into a PHP associative array.
*
* @param string $json_string The JSON string to decode.
* @return array The decoded associative array. Returns an empty array if the input is not a valid JSON string.
*/
public static function decode($json_string) {
$json_decoded = json_decode($json_string, true);

if (json_last_error() !== JSON_ERROR_NONE) {
$json_decoded = [];
}

return $json_decoded;
}

/**
* Encodes PHP data to a JSON string with formatting options.
*
Expand All @@ -61,11 +77,11 @@ private static function get_id($id) {
*/
public static function encode($php_data) {
$json_encoded = json_encode($php_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

if ($json_encoded === false) {
$json_encoded = '{}';
}

$json_encoded = preg_replace_callback('/^( {4,})/m', function ($matches) {
return str_replace(' ', "\t", $matches[1]);
}, $json_encoded);
Expand Down Expand Up @@ -114,12 +130,12 @@ public static function get_json_field($field, $id = null, $format = 'php') {
$id = self::get_id($id);
$data = '';
$raw_data = get_field($field, $id);

if (!is_string($raw_data)) {
$raw_data = '{}';
}
$json_decoded = json_decode($raw_data, true);

$json_decoded = self::decode($raw_data);

if ($format === 'html') {
$data = self::generate_pre($json_decoded);
Expand Down

0 comments on commit 0dbd617

Please sign in to comment.