Skip to content

Commit 6ac81a9

Browse files
committed
feat: Improve term saving with sanitization and error handling
1 parent c81b2bf commit 6ac81a9

2 files changed

Lines changed: 45 additions & 17 deletions

File tree

rdo.js

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { domReady } = wp;
2+
const { createNotice } = wp.data.dispatch('core/notices');
23
const { PluginSidebar, PluginSidebarMoreMenuItem } = wp.editor;
34
const { TextareaControl, Button, ToggleControl, Icon } = wp.components;
45
const { withSelect, withDispatch, subscribe } = wp.data;
@@ -302,11 +303,18 @@ const ImportantTermsComponent = compose([
302303
})),
303304
withDispatch(dispatch => ({
304305
setMetaFieldValue: value => {
305-
const editor = dispatch('core/editor');
306-
let content = selectData('core/editor').getEditedPostContent();
307-
content = removeHighlightingFromContent(content);
308-
editor.editPost({ content: content });
309-
editor.editPost({ meta: { _important_terms: value } });
306+
return new Promise((resolve, reject) => {
307+
try {
308+
const editor = dispatch('core/editor');
309+
let content = selectData('core/editor').getEditedPostContent();
310+
content = removeHighlightingFromContent(content);
311+
editor.editPost({ content: content });
312+
editor.editPost({ meta: { _important_terms: value } });
313+
resolve();
314+
} catch (error) {
315+
reject(error);
316+
}
317+
});
310318
}
311319
}))
312320
])((props) => {
@@ -396,19 +404,38 @@ const ImportantTermsComponent = compose([
396404
terms = terms.filter(term => !term.includes('=='));
397405
terms = [...new Set(terms)];
398406
const cleanedTerms = terms.join('\n');
399-
props.setMetaFieldValue(cleanedTerms);
400-
setLocalTerms(cleanedTerms);
401407

402-
// Update processed terms array and rehighlight if enabled
403-
if (globalHighlightingState) {
404-
processedTermsArray = terms.sort((a, b) => b.length - a.length);
405-
removeHighlighting();
406-
setTimeout(() => {
407-
if (processedTermsArray.length > 0) {
408-
highlightTerms(processedTermsArray);
408+
props.setMetaFieldValue(cleanedTerms).then(() => {
409+
setLocalTerms(cleanedTerms);
410+
411+
if (globalHighlightingState) {
412+
processedTermsArray = terms.sort((a, b) => b.length - a.length);
413+
removeHighlighting();
414+
setTimeout(() => {
415+
if (processedTermsArray.length > 0) {
416+
highlightTerms(processedTermsArray);
417+
}
418+
}, 50);
419+
}
420+
421+
createNotice(
422+
'success',
423+
'Terms saved successfully.',
424+
{
425+
type: 'snackbar',
426+
isDismissible: true,
409427
}
410-
}, 50);
411-
}
428+
);
429+
}).catch(() => {
430+
createNotice(
431+
'error',
432+
'Failed to save terms. Please try again.',
433+
{
434+
type: 'snackbar',
435+
isDismissible: true,
436+
}
437+
);
438+
});
412439
};
413440

414441
return termsHighlighterEl(

relevant-density-optimizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ function rdo_register_meta() {
5050
'type' => 'string',
5151
'auth_callback' => function() {
5252
return current_user_can('edit_posts');
53-
}
53+
},
54+
'sanitize_callback' => 'sanitize_textarea_field'
5455
));
5556
}
5657

0 commit comments

Comments
 (0)