Skip to content

Commit

Permalink
v1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
misaki-web committed Jul 15, 2024
1 parent 3e95e90 commit e2fe2dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 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.2
* Version: 1.0.3
*/

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

define('ACF_JSON_FIELD_VERSION', '1.0.2');
define('ACF_JSON_FIELD_VERSION', '1.0.3');

################################################################################
# @title Inclusions
Expand Down
23 changes: 13 additions & 10 deletions includes/class-acf-field-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* acf_field_json class.
*/
class acf_field_json extends \acf_field {
private const DEFAULT_EDITOR_MODE = 'tree';

/**
* Constructor.
Expand Down Expand Up @@ -59,7 +60,7 @@ public function render_field_settings($field) {
'tree' => 'tree',
],
'required' => false,
'default_value' => 'tree',
'default_value' => self::DEFAULT_EDITOR_MODE,
'hint' => __('Select the editor mode ("tree" by default)', 'acf-json-field'),
]
);
Expand All @@ -82,20 +83,22 @@ public function render_field_settings($field) {
* @return void
*/
public function render_field($field) {
$textarea_id = esc_attr($field['id']);
$textarea_name = esc_attr($field['name']);
$textarea_value = esc_textarea($field['value']);
$textarea_id = isset($field['id']) ? esc_attr($field['id']) : '';
$textarea_name = isset($field['name']) ? esc_attr($field['name']) : '';
$textarea_value = isset($field['value']) ? esc_textarea($field['value']) : '';

if ($textarea_value === '') {
if ($textarea_value === '' && isset($field['default_value'])) {
$textarea_value = esc_textarea($field['default_value']);
}

$editor_id = $textarea_id . '-editor';
$editor_mode = esc_attr($field['editor_mode']);
$editor_mode = isset($field['editor_mode']) ? esc_attr($field['editor_mode']) : self::DEFAULT_EDITOR_MODE;

echo <<<HTML
<textarea id="$textarea_id" class="acf-json-field-data" name="$textarea_name">$textarea_value</textarea>
<div id="$editor_id" class="acf-json-field-editor" data-editor-mode="$editor_mode"></div>
HTML;
if ($textarea_id !== '' && $textarea_name !== '') {
echo <<<HTML
<textarea id="$textarea_id" class="acf-json-field-data" name="$textarea_name">$textarea_value</textarea>
<div id="$editor_id" class="acf-json-field-editor" data-editor-mode="$editor_mode"></div>
HTML;
}
}
}

0 comments on commit e2fe2dc

Please sign in to comment.