Skip to content

Commit

Permalink
Merge pull request #770 from helsingborg-stad/fix/custom-meta-key-man…
Browse files Browse the repository at this point in the history
…ual-input

feat: custom meta key manual input
  • Loading branch information
NiclasNorin authored Jan 16, 2025
2 parents da87f65 + 6f33a39 commit 641dc2e
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 20 deletions.
32 changes: 30 additions & 2 deletions source/php/AcfFields/json/mod-manual-input.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"width": "30",
"class": "",
"id": ""
},
Expand All @@ -91,6 +91,34 @@
"ui_off_text": "",
"ui": 1
},
{
"key": "field_678784f60a1a6",
"label": "Save as custom meta key",
"name": "save_as_custom_meta_key",
"aria-label": "",
"type": "text",
"instructions": "Will default to the module ID but can be set to save as a custom meta key.",
"required": 0,
"conditional_logic": [
[
{
"field": "field_67126c170c176",
"operator": "==",
"value": "1"
}
]
],
"wrapper": {
"width": "70",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"placeholder": "unique-key",
"prepend": "",
"append": ""
},
{
"key": "field_67289fa6dfea3",
"label": "Free text filtering",
Expand Down Expand Up @@ -762,4 +790,4 @@
"acfe_meta": "",
"acfe_note": ""
}]


53 changes: 40 additions & 13 deletions source/php/AcfFields/php/mod-manual-input.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

if (function_exists('acf_add_local_field_group')) {

acf_add_local_field_group(array(

if (function_exists('acf_add_local_field_group')) {
acf_add_local_field_group(array(
'key' => 'group_64ff22b117e2c',
'title' => __('Manual Input Data', 'modularity'),
'fields' => array(
Expand Down Expand Up @@ -85,7 +84,7 @@
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'width' => '30',
'class' => '',
'id' => '',
),
Expand All @@ -96,6 +95,34 @@
'ui' => 1,
),
3 => array(
'key' => 'field_678784f60a1a6',
'label' => __('Save as custom meta key', 'modularity'),
'name' => 'save_as_custom_meta_key',
'aria-label' => '',
'type' => 'text',
'instructions' => __('Will default to the module ID but can be set to save as a custom meta key.', 'modularity'),
'required' => 0,
'conditional_logic' => array(
0 => array(
0 => array(
'field' => 'field_67126c170c176',
'operator' => '==',
'value' => '1',
),
),
),
'wrapper' => array(
'width' => '70',
'class' => '',
'id' => '',
),
'default_value' => '',
'maxlength' => '',
'placeholder' => __('unique-key', 'modularity'),
'prepend' => '',
'append' => '',
),
4 => array(
'key' => 'field_67289fa6dfea3',
'label' => __('Free text filtering', 'modularity'),
'name' => 'free_text_filtering',
Expand Down Expand Up @@ -123,7 +150,7 @@
'ui_off_text' => '',
'ui' => 1,
),
4 => array(
5 => array(
'key' => 'field_650067ed6cc3c',
'label' => __('Column marking', 'modularity'),
'name' => 'accordion_column_marking',
Expand Down Expand Up @@ -151,7 +178,7 @@
'prepend' => '',
'append' => '',
),
5 => array(
6 => array(
'key' => 'field_65005968bbc75',
'label' => __('Column titles', 'modularity'),
'name' => 'accordion_column_titles',
Expand Down Expand Up @@ -205,7 +232,7 @@
),
),
),
6 => array(
7 => array(
'key' => 'field_65001d039d4c4',
'label' => __('Columns', 'modularity'),
'name' => 'columns',
Expand Down Expand Up @@ -253,7 +280,7 @@
'allow_custom' => 0,
'search_placeholder' => '',
),
7 => array(
8 => array(
'key' => 'field_663372f4922a5',
'label' => __('Highlight first input', 'modularity'),
'name' => 'highlight_first_input',
Expand Down Expand Up @@ -302,7 +329,7 @@
'ui_on_text' => '',
'ui_off_text' => '',
),
8 => array(
9 => array(
'key' => 'field_6641de045ab9d',
'label' => __('Image position', 'modularity'),
'name' => 'image_position',
Expand Down Expand Up @@ -335,7 +362,7 @@
'layout' => 'horizontal',
'save_other_choice' => 0,
),
9 => array(
10 => array(
'key' => 'field_65016a6f0a085',
'label' => __('Ratio', 'modularity'),
'name' => 'ratio',
Expand Down Expand Up @@ -379,7 +406,7 @@
'allow_custom' => 0,
'search_placeholder' => '',
),
10 => array(
11 => array(
'key' => 'field_64ff22b2d91b7',
'label' => __('Manual inputs', 'modularity'),
'name' => 'manual_inputs',
Expand Down Expand Up @@ -767,4 +794,4 @@
'acfe_meta' => '',
'acfe_note' => '',
));
}
}
Expand Down
80 changes: 80 additions & 0 deletions source/php/Module/ManualInput/Private/PrivateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
class PrivateController
{
public static $index = 0;
private string $userMetaKey = 'manualInputs';
public function __construct(private ManualInput $manualInputInstance)
{
$this->registerMeta();
add_filter('acf/update_value/key=field_6718c31e2862b', array($this, 'assignUniqueIdToRows'), 20, 4);

add_filter('acf/prepare_field/key=field_678784f60a1a6', [$this, 'onlyShowCustomMetaKeyFieldIfAdministrator']);

add_filter('acf/update_value/key=field_678784f60a1a6', [$this, 'checkForChangedMetaKeyValue'], 10, 4);

// Do not cache private manual inputs
if ($this->manualInputInstance->postStatus === 'private') {
$this->manualInputInstance->cacheTtl = 0;
Expand All @@ -37,6 +42,8 @@ public function decorateData(array $data, array $fields): array
$this->manualInputInstance->template = 'private';

$data['user'] = $user->ID;
$data['userMetaKey'] = $this->userMetaKey;
$data['privateModuleMetaKey'] = $this->getPrivateMetaKey($fields);

$data['lang'] = [
'save' => __('Save', 'modularity'),
Expand All @@ -54,6 +61,27 @@ public function decorateData(array $data, array $fields): array
return $data;
}

/**
* Retrieves the private meta key for the module.
*
* This function takes an array of fields and checks if the 'save_as_custom_meta_key' field is not empty.
* If it is not empty, it sanitizes the value and assigns it to the $privateModuleMetaKey variable.
* If it is empty, it assigns the ID of the manualInputInstance to the $privateModuleMetaKey variable.
*
* @param array $fields The array of fields.
* @return string The private meta key for the module.
*/
private function getPrivateMetaKey(array $fields): string
{
$privateModuleMetaKey = null;

if (!empty($fields['save_as_custom_meta_key'])) {
$privateModuleMetaKey = sanitize_title($fields['save_as_custom_meta_key']);
}

return !empty($privateModuleMetaKey) ? $privateModuleMetaKey : $this->manualInputInstance->ID;
}

private function getUserStructuredManualInputs(array $data): array
{
$userManualInputs = get_user_meta($data['user'], 'manualInputs', true);
Expand Down Expand Up @@ -114,4 +142,56 @@ public function assignUniqueIdToRows($value, $postId, $field, $original): string

return $value;
}

/**
* Checks if the meta key value has changed and updates the user meta accordingly.
*
* @param mixed $value The new value of the meta key.
* @param int $postId The ID of the post.
* @param array $field The field array containing the key.
* @param mixed $originalValue The original value of the meta key.
* @return mixed The updated value of the meta key.
*/
public function checkForChangedMetaKeyValue($value, $postId, $field, $originalValue)
{
$oldKey = get_field($field['key'], $postId);
$oldKey = sanitize_title(empty($oldKey) ? $postId : $oldKey);

$newKey = sanitize_title(empty($value) ? $postId : $value);

if ($oldKey === $newKey) {
return $value;
}

$user = wp_get_current_user();

$userMeta = get_user_meta($user->ID, $this->userMetaKey, true);

if (isset($userMeta[$oldKey])) {
$userMeta[$newKey] = $userMeta[$oldKey];
unset($userMeta[$oldKey]);

update_user_meta($user->ID, $this->userMetaKey, $userMeta);
}

return $value;
}

/**
* Determines if the custom meta key field should be shown only for administrators.
*
* @param mixed $field The field to be checked.
*
* @return mixed Returns the field if the current user is an administrator, otherwise returns false.
*/
public function onlyShowCustomMetaKeyFieldIfAdministrator($field)
{
$user = wp_get_current_user();

if (!$user->caps || !in_array('administrator', $user->caps)) {
$field['wrapper']['class'] = 'acf-hidden';
}

return $field;
}
}
10 changes: 5 additions & 5 deletions source/php/Module/ManualInput/views/private.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@element([
'attributeList' => [
'data-js-user-editable' => 'manualInputs',
'data-js-user-editable-user' => $user,
'data-js-user-editable-id' => $ID
'data-js-user-editable-id' => $privateModuleMetaKey,
'data-js-user-editable' => $userMetaKey
],
])
@includeFirst([$template, 'base'],
Expand All @@ -12,14 +12,14 @@
'icon' => 'edit',
'size' => 'md',
'attributeList' => [
'data-open' => 'modal-' . $ID,
'data-open' => 'modal-' . $privateModuleMetaKey,
'style' => 'cursor: pointer;'
]
]
])

@modal([
'id' => 'modal-' . $ID,
'id' => 'modal-' . $privateModuleMetaKey,
'size' => 'sm',
'padding' => 4,
'heading' => $lang['changeContent']
Expand Down Expand Up @@ -121,4 +121,4 @@
@endgroup
@endslot
@endmodal
@endelement
@endelement

0 comments on commit 641dc2e

Please sign in to comment.