Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 43ddbddd11 | |||
| 7b9f26e966 |
@@ -13,6 +13,7 @@
|
||||
const resultField = document.getElementById('groq-ai-output');
|
||||
const jsonCopyButton = modal.querySelector('.groq-ai-copy-json');
|
||||
const contextToggles = modal.querySelectorAll('.groq-ai-context-toggle');
|
||||
const attributeToggles = modal.querySelectorAll('.groq-ai-attribute-toggle');
|
||||
const resultFields = {};
|
||||
modal.querySelectorAll('.groq-ai-result-field').forEach((field) => {
|
||||
const key = field.getAttribute('data-field');
|
||||
@@ -60,6 +61,7 @@
|
||||
promptField.value = GroqAIGenerator.defaultPrompt;
|
||||
}
|
||||
resetContextToggles();
|
||||
resetAttributeToggles();
|
||||
setTimeout(() => promptField.focus(), 50);
|
||||
}
|
||||
|
||||
@@ -115,6 +117,7 @@
|
||||
payload.append('prompt', prompt);
|
||||
payload.append('post_id', GroqAIGenerator.postId || 0);
|
||||
payload.append('context_fields', JSON.stringify(collectContextSelection()));
|
||||
payload.append('attribute_includes', JSON.stringify(collectAttributeSelection()));
|
||||
|
||||
toggleLoading(true);
|
||||
resultWrapper.hidden = true;
|
||||
@@ -458,6 +461,20 @@
|
||||
});
|
||||
}
|
||||
|
||||
function resetAttributeToggles() {
|
||||
const defaults = Array.isArray(GroqAIGenerator.attributeIncludesDefaults)
|
||||
? GroqAIGenerator.attributeIncludesDefaults
|
||||
: [];
|
||||
|
||||
attributeToggles.forEach((toggle) => {
|
||||
const key = toggle.getAttribute('data-attribute');
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
toggle.checked = defaults.includes(key);
|
||||
});
|
||||
}
|
||||
|
||||
function collectContextSelection() {
|
||||
const selected = [];
|
||||
contextToggles.forEach((toggle) => {
|
||||
@@ -467,4 +484,18 @@
|
||||
});
|
||||
return selected;
|
||||
}
|
||||
|
||||
function collectAttributeSelection() {
|
||||
const selected = [];
|
||||
attributeToggles.forEach((toggle) => {
|
||||
if (!toggle.checked) {
|
||||
return;
|
||||
}
|
||||
const key = toggle.getAttribute('data-attribute');
|
||||
if (key) {
|
||||
selected.push(key);
|
||||
}
|
||||
});
|
||||
return selected;
|
||||
}
|
||||
})(jQuery);
|
||||
|
||||
@@ -9,7 +9,11 @@
|
||||
}
|
||||
|
||||
const promptField = document.getElementById('groq-ai-term-prompt');
|
||||
const outputField = document.getElementById('groq-ai-term-generated');
|
||||
const outputTopField = document.getElementById('groq-ai-term-generated-top');
|
||||
const outputBottomField = document.getElementById('groq-ai-term-generated-bottom');
|
||||
const outputMetaTitleField = document.getElementById('groq-ai-term-generated-meta-title');
|
||||
const outputMetaDescriptionField = document.getElementById('groq-ai-term-generated-meta-description');
|
||||
const outputFocusKeywordsField = document.getElementById('groq-ai-term-generated-focus-keywords');
|
||||
const rawField = document.getElementById('groq-ai-term-raw');
|
||||
const statusField = document.getElementById('groq-ai-term-status');
|
||||
const applyButton = document.getElementById('groq-ai-term-apply');
|
||||
@@ -48,14 +52,27 @@
|
||||
applyButton.addEventListener('click', () => {
|
||||
const descriptionField = document.getElementById('description');
|
||||
const bottomDescriptionField = document.getElementById('groq-ai-term-bottom-description');
|
||||
if (!outputField) {
|
||||
const rankmathTitleField = document.getElementById('groq-ai-rankmath-title');
|
||||
const rankmathDescriptionField = document.getElementById('groq-ai-rankmath-description');
|
||||
const rankmathKeywordsField = document.getElementById('groq-ai-rankmath-keywords');
|
||||
if (!outputTopField) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (bottomDescriptionField) {
|
||||
bottomDescriptionField.value = outputField.value || '';
|
||||
} else if (descriptionField) {
|
||||
descriptionField.value = outputField.value || '';
|
||||
if (descriptionField) {
|
||||
descriptionField.value = outputTopField.value || '';
|
||||
}
|
||||
if (bottomDescriptionField && outputBottomField) {
|
||||
bottomDescriptionField.value = outputBottomField.value || '';
|
||||
}
|
||||
if (rankmathTitleField && outputMetaTitleField) {
|
||||
rankmathTitleField.value = outputMetaTitleField.value || '';
|
||||
}
|
||||
if (rankmathDescriptionField && outputMetaDescriptionField) {
|
||||
rankmathDescriptionField.value = outputMetaDescriptionField.value || '';
|
||||
}
|
||||
if (rankmathKeywordsField && outputFocusKeywordsField) {
|
||||
rankmathKeywordsField.value = outputFocusKeywordsField.value || '';
|
||||
}
|
||||
|
||||
setStatus('Tekst ingevuld. Vergeet niet op "Opslaan" te klikken.', 'success');
|
||||
@@ -76,9 +93,11 @@
|
||||
rawField.textContent = '';
|
||||
}
|
||||
|
||||
if (outputField) {
|
||||
outputField.value = '';
|
||||
}
|
||||
if (outputTopField) outputTopField.value = '';
|
||||
if (outputBottomField) outputBottomField.value = '';
|
||||
if (outputMetaTitleField) outputMetaTitleField.value = '';
|
||||
if (outputMetaDescriptionField) outputMetaDescriptionField.value = '';
|
||||
if (outputFocusKeywordsField) outputFocusKeywordsField.value = '';
|
||||
|
||||
fetch(GroqAITermGenerator.ajaxUrl, {
|
||||
method: 'POST',
|
||||
@@ -94,9 +113,25 @@
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
if (outputField) {
|
||||
const text = json.data && json.data.description ? json.data.description : '';
|
||||
outputField.value = String(text).trim();
|
||||
if (outputTopField) {
|
||||
const top = json.data && (json.data.top_description || json.data.description) ? (json.data.top_description || json.data.description) : '';
|
||||
outputTopField.value = String(top).trim();
|
||||
}
|
||||
if (outputBottomField) {
|
||||
const bottom = json.data && json.data.bottom_description ? json.data.bottom_description : '';
|
||||
outputBottomField.value = String(bottom).trim();
|
||||
}
|
||||
if (outputMetaTitleField) {
|
||||
const metaTitle = json.data && json.data.meta_title ? json.data.meta_title : '';
|
||||
outputMetaTitleField.value = String(metaTitle).trim();
|
||||
}
|
||||
if (outputMetaDescriptionField) {
|
||||
const metaDescription = json.data && json.data.meta_description ? json.data.meta_description : '';
|
||||
outputMetaDescriptionField.value = String(metaDescription).trim();
|
||||
}
|
||||
if (outputFocusKeywordsField) {
|
||||
const keywords = json.data && json.data.focus_keywords ? json.data.focus_keywords : '';
|
||||
outputFocusKeywordsField.value = String(keywords).trim();
|
||||
}
|
||||
if (rawField) {
|
||||
rawField.textContent = (json.data && json.data.raw ? String(json.data.raw) : '').trim();
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
/**
|
||||
* Plugin Name: SitiAI Product Teksten
|
||||
* Description: Genereer productteksten met diverse AI-aanbieders rechtstreeks vanuit WooCommerce.
|
||||
* Version: 1.4.3
|
||||
* Version: 1.4.5
|
||||
* Author: SitiAI
|
||||
* Text Domain: siti-ai-product-content-generator
|
||||
* Domain Path: /languages
|
||||
|
||||
@@ -59,6 +59,9 @@ class Groq_AI_Product_Text_Product_UI {
|
||||
$post_id = ( $post && isset( $post->ID ) ) ? (int) $post->ID : 0;
|
||||
|
||||
$settings = $this->plugin->get_settings();
|
||||
$attribute_defaults = isset( $settings['product_attribute_includes'] ) && is_array( $settings['product_attribute_includes'] )
|
||||
? array_values( array_unique( array_map( 'sanitize_key', $settings['product_attribute_includes'] ) ) )
|
||||
: [];
|
||||
|
||||
wp_localize_script(
|
||||
'groq-ai-admin',
|
||||
@@ -69,6 +72,7 @@ class Groq_AI_Product_Text_Product_UI {
|
||||
'defaultPrompt' => $settings['default_prompt'],
|
||||
'postId' => $post_id,
|
||||
'contextDefaults' => isset( $settings['context_fields'] ) ? $settings['context_fields'] : $this->plugin->get_default_context_fields(),
|
||||
'attributeIncludesDefaults' => $attribute_defaults,
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -82,6 +86,7 @@ class Groq_AI_Product_Text_Product_UI {
|
||||
|
||||
$settings = $this->plugin->get_settings();
|
||||
$rankmath_enabled = $this->plugin->is_rankmath_active() && $this->plugin->is_module_enabled( 'rankmath', $settings );
|
||||
$attribute_options = $this->get_product_attribute_include_options();
|
||||
?>
|
||||
<div id="groq-ai-modal" class="groq-ai-modal" aria-hidden="true">
|
||||
<div class="groq-ai-modal__dialog" role="dialog" aria-modal="true" aria-labelledby="groq-ai-modal-title">
|
||||
@@ -109,6 +114,9 @@ class Groq_AI_Product_Text_Product_UI {
|
||||
$context_definitions = $this->plugin->get_context_field_definitions();
|
||||
$context_defaults = isset( $settings['context_fields'] ) ? $settings['context_fields'] : $this->plugin->get_default_context_fields();
|
||||
foreach ( $context_definitions as $context_key => $context_info ) :
|
||||
if ( 'attributes' === $context_key ) {
|
||||
continue;
|
||||
}
|
||||
$checked = ! empty( $context_defaults[ $context_key ] );
|
||||
?>
|
||||
<label class="groq-ai-context-option">
|
||||
@@ -122,6 +130,23 @@ class Groq_AI_Product_Text_Product_UI {
|
||||
</label>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
|
||||
<h3 style="margin-top:16px;"><?php esc_html_e( 'Attributen meesturen', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></h3>
|
||||
<p class="description"><?php esc_html_e( 'Selecteer welke productattributen je mee wilt geven aan de AI. Dit vervangt de oude alles-of-niets optie.', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></p>
|
||||
<?php if ( empty( $attribute_options ) ) : ?>
|
||||
<p class="description"><?php esc_html_e( 'Geen WooCommerce-attributen gevonden.', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></p>
|
||||
<?php else : ?>
|
||||
<div class="groq-ai-context-options__grid">
|
||||
<?php foreach ( $attribute_options as $attr_key => $attr_label ) : ?>
|
||||
<label class="groq-ai-context-option">
|
||||
<input type="checkbox" class="groq-ai-attribute-toggle" data-attribute="<?php echo esc_attr( $attr_key ); ?>" />
|
||||
<div>
|
||||
<strong><?php echo esc_html( $attr_label ); ?></strong>
|
||||
</div>
|
||||
</label>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
@@ -225,4 +250,39 @@ class Groq_AI_Product_Text_Product_UI {
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
private function get_product_attribute_include_options() {
|
||||
$options = [
|
||||
'__custom__' => __( 'Custom attributen (niet-taxonomie)', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
||||
];
|
||||
|
||||
if ( function_exists( 'wc_get_attribute_taxonomies' ) ) {
|
||||
$taxonomies = wc_get_attribute_taxonomies();
|
||||
if ( is_array( $taxonomies ) ) {
|
||||
foreach ( $taxonomies as $attr ) {
|
||||
$name = isset( $attr->attribute_name ) ? sanitize_key( (string) $attr->attribute_name ) : '';
|
||||
$label = isset( $attr->attribute_label ) ? sanitize_text_field( (string) $attr->attribute_label ) : '';
|
||||
if ( '' === $name ) {
|
||||
continue;
|
||||
}
|
||||
$taxonomy = 'pa_' . $name;
|
||||
if ( '' === $label ) {
|
||||
$label = function_exists( 'wc_attribute_label' ) ? wc_attribute_label( $taxonomy ) : $taxonomy;
|
||||
}
|
||||
$options[ $taxonomy ] = $label;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( count( $options ) > 1 ) {
|
||||
$fixed = [
|
||||
'__custom__' => $options['__custom__'],
|
||||
];
|
||||
unset( $options['__custom__'] );
|
||||
asort( $options, SORT_NATURAL | SORT_FLAG_CASE );
|
||||
$options = $fixed + $options;
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,9 +321,18 @@ class Groq_AI_Product_Text_Settings_Page {
|
||||
$meta_prompt = get_term_meta( $term_id, 'groq_ai_term_custom_prompt', true );
|
||||
$settings = $this->plugin->get_settings();
|
||||
$bottom_meta_key = $this->resolve_term_bottom_description_meta_key( $term, $settings );
|
||||
$bottom_description = '';
|
||||
if ( '' !== $bottom_meta_key ) {
|
||||
$bottom_description = (string) get_term_meta( $term_id, $bottom_meta_key, true );
|
||||
$effective_bottom_meta_key = '' !== $bottom_meta_key ? $bottom_meta_key : 'groq_ai_term_bottom_description';
|
||||
$bottom_description = (string) get_term_meta( $term_id, $effective_bottom_meta_key, true );
|
||||
$rankmath_module_enabled = $this->plugin->is_module_enabled( 'rankmath', $settings );
|
||||
$rankmath_active = $this->plugin->is_rankmath_active();
|
||||
$rankmath_title = '';
|
||||
$rankmath_description = '';
|
||||
$rankmath_focus_keywords = '';
|
||||
if ( $rankmath_module_enabled ) {
|
||||
$rankmath_keys = $this->resolve_rankmath_term_meta_keys( $term, $settings );
|
||||
$rankmath_title = (string) get_term_meta( $term_id, $rankmath_keys['title'], true );
|
||||
$rankmath_description = (string) get_term_meta( $term_id, $rankmath_keys['description'], true );
|
||||
$rankmath_focus_keywords = (string) get_term_meta( $term_id, $rankmath_keys['focus_keyword'], true );
|
||||
}
|
||||
$default_prompt = (string) $meta_prompt;
|
||||
if ( '' === trim( $default_prompt ) ) {
|
||||
@@ -360,23 +369,24 @@ class Groq_AI_Product_Text_Settings_Page {
|
||||
<p class="description"><?php esc_html_e( 'Dit is de standaard WordPress term-omschrijving (wordt o.a. gebruikt op categorie/merk pagina’s).', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></p>
|
||||
</td>
|
||||
</tr>
|
||||
<?php if ( '' !== $bottom_meta_key ) : ?>
|
||||
<tr>
|
||||
<th scope="row"><label for="groq-ai-term-bottom-description"><?php esc_html_e( 'Omschrijving (onderaan)', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></label></th>
|
||||
<td>
|
||||
<textarea name="groq_ai_term_bottom_description" id="groq-ai-term-bottom-description" rows="8" class="large-text"><?php echo esc_textarea( (string) $bottom_description ); ?></textarea>
|
||||
<p class="description">
|
||||
<?php
|
||||
printf(
|
||||
/* translators: %s: meta key */
|
||||
esc_html__( 'Dit veld wordt opgeslagen in term meta (%s) en wordt onderaan de pagina getoond via LiveBetter customfields.', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
||||
esc_html( $bottom_meta_key )
|
||||
);
|
||||
?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endif; ?>
|
||||
<tr>
|
||||
<th scope="row"><label for="groq-ai-term-bottom-description"><?php esc_html_e( 'Omschrijving (onderaan)', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></label></th>
|
||||
<td>
|
||||
<textarea name="groq_ai_term_bottom_description" id="groq-ai-term-bottom-description" rows="8" class="large-text"><?php echo esc_textarea( (string) $bottom_description ); ?></textarea>
|
||||
<p class="description">
|
||||
<?php
|
||||
printf(
|
||||
/* translators: %s: meta key */
|
||||
esc_html__( 'Deze tekst wordt opgeslagen in term meta (%s) en is bedoeld voor helemaal onderaan (LiveBetter customfields).', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
||||
esc_html( $effective_bottom_meta_key )
|
||||
);
|
||||
if ( '' === $bottom_meta_key ) {
|
||||
echo ' ' . esc_html__( 'Let op: stel de juiste LiveBetter meta key in via de plugin-instelling of via de filter groq_ai_term_bottom_description_meta_key.', GROQ_AI_PRODUCT_TEXT_DOMAIN );
|
||||
}
|
||||
?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="groq-ai-term-custom-prompt"><?php esc_html_e( 'Prompt (optioneel, per term)', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></label></th>
|
||||
<td>
|
||||
@@ -384,6 +394,34 @@ class Groq_AI_Product_Text_Settings_Page {
|
||||
<p class="description"><?php esc_html_e( 'Laat leeg om de standaard prompt te gebruiken. Deze prompt wordt gebruikt wanneer je op de knop "Genereer" klikt.', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></p>
|
||||
</td>
|
||||
</tr>
|
||||
<?php if ( $rankmath_module_enabled ) : ?>
|
||||
<?php if ( ! $rankmath_active ) : ?>
|
||||
<tr>
|
||||
<th scope="row"><?php esc_html_e( 'Rank Math', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></th>
|
||||
<td>
|
||||
<p class="description"><?php esc_html_e( 'Rank Math plugin lijkt niet actief. Velden zijn wel invulbaar en worden opgeslagen in term meta, maar Rank Math gebruikt ze pas na activatie.', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></p>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endif; ?>
|
||||
<tr>
|
||||
<th scope="row"><label for="groq-ai-rankmath-title"><?php esc_html_e( 'Rank Math meta title', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></label></th>
|
||||
<td>
|
||||
<textarea name="groq_ai_rankmath_meta_title" id="groq-ai-rankmath-title" rows="2" class="large-text"><?php echo esc_textarea( (string) $rankmath_title ); ?></textarea>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="groq-ai-rankmath-description"><?php esc_html_e( 'Rank Math meta description', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></label></th>
|
||||
<td>
|
||||
<textarea name="groq_ai_rankmath_meta_description" id="groq-ai-rankmath-description" rows="3" class="large-text"><?php echo esc_textarea( (string) $rankmath_description ); ?></textarea>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row"><label for="groq-ai-rankmath-keywords"><?php esc_html_e( 'Rank Math focus keywords', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></label></th>
|
||||
<td>
|
||||
<textarea name="groq_ai_rankmath_focus_keywords" id="groq-ai-rankmath-keywords" rows="2" class="large-text" placeholder="<?php esc_attr_e( 'bijv. luxe massage apparaat, wellness cadeau', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?>"><?php echo esc_textarea( (string) $rankmath_focus_keywords ); ?></textarea>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endif; ?>
|
||||
</table>
|
||||
<?php submit_button( __( 'Opslaan', GROQ_AI_PRODUCT_TEXT_DOMAIN ) ); ?>
|
||||
</form>
|
||||
@@ -407,19 +445,21 @@ class Groq_AI_Product_Text_Settings_Page {
|
||||
<textarea id="groq-ai-term-prompt" class="large-text" rows="5"><?php echo esc_textarea( $default_prompt ); ?></textarea>
|
||||
<p>
|
||||
<button type="submit" class="button button-primary"><?php esc_html_e( 'Genereer', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></button>
|
||||
<button type="button" class="button" id="groq-ai-term-apply">
|
||||
<?php
|
||||
if ( '' !== $bottom_meta_key ) {
|
||||
esc_html_e( 'Zet in onderaan-veld', GROQ_AI_PRODUCT_TEXT_DOMAIN );
|
||||
} else {
|
||||
esc_html_e( 'Zet in omschrijving', GROQ_AI_PRODUCT_TEXT_DOMAIN );
|
||||
}
|
||||
?>
|
||||
</button>
|
||||
<button type="button" class="button" id="groq-ai-term-apply"><?php esc_html_e( 'Zet in velden', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></button>
|
||||
</p>
|
||||
<div id="groq-ai-term-status" class="description" aria-live="polite"></div>
|
||||
<h3><?php esc_html_e( 'Gegenereerde tekst (omschrijving, 1 alinea)', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></h3>
|
||||
<textarea id="groq-ai-term-generated-top" class="large-text" rows="6"></textarea>
|
||||
<h3><?php esc_html_e( 'Gegenereerde tekst (onderaan)', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></h3>
|
||||
<textarea id="groq-ai-term-generated" class="large-text" rows="10"></textarea>
|
||||
<textarea id="groq-ai-term-generated-bottom" class="large-text" rows="10"></textarea>
|
||||
<?php if ( $rankmath_module_enabled ) : ?>
|
||||
<h3><?php esc_html_e( 'Gegenereerde Rank Math meta title', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></h3>
|
||||
<textarea id="groq-ai-term-generated-meta-title" class="large-text" rows="2"></textarea>
|
||||
<h3><?php esc_html_e( 'Gegenereerde Rank Math meta description', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></h3>
|
||||
<textarea id="groq-ai-term-generated-meta-description" class="large-text" rows="3"></textarea>
|
||||
<h3><?php esc_html_e( 'Gegenereerde Rank Math focus keywords', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></h3>
|
||||
<textarea id="groq-ai-term-generated-focus-keywords" class="large-text" rows="2"></textarea>
|
||||
<?php endif; ?>
|
||||
<h3><?php esc_html_e( 'Ruwe JSON-output', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?></h3>
|
||||
<pre id="groq-ai-term-raw" style="background:#fff;border:1px solid #ddd;padding:12px;max-height:240px;overflow:auto;"></pre>
|
||||
</form>
|
||||
@@ -438,6 +478,24 @@ class Groq_AI_Product_Text_Settings_Page {
|
||||
return $key;
|
||||
}
|
||||
|
||||
private function resolve_rankmath_term_meta_keys( $term, $settings ) {
|
||||
$keys = [
|
||||
'title' => 'rank_math_title',
|
||||
'description' => 'rank_math_description',
|
||||
'focus_keyword' => 'rank_math_focus_keyword',
|
||||
];
|
||||
$keys = apply_filters( 'groq_ai_rankmath_term_meta_keys', $keys, $term, $settings );
|
||||
if ( ! is_array( $keys ) ) {
|
||||
$keys = [];
|
||||
}
|
||||
|
||||
return [
|
||||
'title' => isset( $keys['title'] ) ? sanitize_key( (string) $keys['title'] ) : 'rank_math_title',
|
||||
'description' => isset( $keys['description'] ) ? sanitize_key( (string) $keys['description'] ) : 'rank_math_description',
|
||||
'focus_keyword' => isset( $keys['focus_keyword'] ) ? sanitize_key( (string) $keys['focus_keyword'] ) : 'rank_math_focus_keyword',
|
||||
];
|
||||
}
|
||||
|
||||
public function handle_save_term_content() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( esc_html__( 'Geen toestemming.', GROQ_AI_PRODUCT_TEXT_DOMAIN ) );
|
||||
@@ -450,6 +508,9 @@ class Groq_AI_Product_Text_Settings_Page {
|
||||
$description = isset( $_POST['description'] ) ? wp_kses_post( wp_unslash( $_POST['description'] ) ) : '';
|
||||
$bottom_description = isset( $_POST['groq_ai_term_bottom_description'] ) ? wp_kses_post( wp_unslash( $_POST['groq_ai_term_bottom_description'] ) ) : '';
|
||||
$custom_prompt = isset( $_POST['groq_ai_term_custom_prompt'] ) ? sanitize_textarea_field( wp_unslash( $_POST['groq_ai_term_custom_prompt'] ) ) : '';
|
||||
$rankmath_meta_title = isset( $_POST['groq_ai_rankmath_meta_title'] ) ? sanitize_text_field( wp_unslash( $_POST['groq_ai_rankmath_meta_title'] ) ) : '';
|
||||
$rankmath_meta_description = isset( $_POST['groq_ai_rankmath_meta_description'] ) ? sanitize_text_field( wp_unslash( $_POST['groq_ai_rankmath_meta_description'] ) ) : '';
|
||||
$rankmath_focus_keywords = isset( $_POST['groq_ai_rankmath_focus_keywords'] ) ? sanitize_text_field( wp_unslash( $_POST['groq_ai_rankmath_focus_keywords'] ) ) : '';
|
||||
|
||||
if ( '' === $taxonomy || ! taxonomy_exists( $taxonomy ) || ! $term_id ) {
|
||||
wp_safe_redirect( $this->get_settings_page_url() );
|
||||
@@ -470,8 +531,15 @@ class Groq_AI_Product_Text_Settings_Page {
|
||||
$term = get_term( $term_id, $taxonomy );
|
||||
if ( $term && ! is_wp_error( $term ) ) {
|
||||
$bottom_meta_key = $this->resolve_term_bottom_description_meta_key( $term, $settings );
|
||||
if ( '' !== $bottom_meta_key ) {
|
||||
update_term_meta( $term_id, $bottom_meta_key, $bottom_description );
|
||||
$effective_bottom_meta_key = '' !== $bottom_meta_key ? $bottom_meta_key : 'groq_ai_term_bottom_description';
|
||||
update_term_meta( $term_id, $effective_bottom_meta_key, $bottom_description );
|
||||
|
||||
$rankmath_module_enabled = $this->plugin->is_module_enabled( 'rankmath', $settings );
|
||||
if ( $rankmath_module_enabled ) {
|
||||
$rankmath_keys = $this->resolve_rankmath_term_meta_keys( $term, $settings );
|
||||
update_term_meta( $term_id, $rankmath_keys['title'], $rankmath_meta_title );
|
||||
update_term_meta( $term_id, $rankmath_keys['description'], $rankmath_meta_description );
|
||||
update_term_meta( $term_id, $rankmath_keys['focus_keyword'], $rankmath_focus_keywords );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -621,6 +689,14 @@ class Groq_AI_Product_Text_Settings_Page {
|
||||
'groq_ai_product_text_prompts'
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
'groq_ai_product_attribute_includes',
|
||||
__( 'Productattributen meesturen', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
||||
[ $this, 'render_product_attribute_includes_field' ],
|
||||
'groq-ai-product-text-prompts',
|
||||
'groq_ai_product_text_prompts'
|
||||
);
|
||||
|
||||
add_settings_field(
|
||||
'groq_ai_response_format_compat',
|
||||
__( 'Response-format compatibiliteit', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
||||
@@ -1568,6 +1644,9 @@ class Groq_AI_Product_Text_Settings_Page {
|
||||
?>
|
||||
<div class="groq-ai-context-defaults">
|
||||
<?php foreach ( $definitions as $key => $definition ) :
|
||||
if ( 'attributes' === $key ) {
|
||||
continue;
|
||||
}
|
||||
$checked = ! empty( $values[ $key ] );
|
||||
?>
|
||||
<label>
|
||||
@@ -1584,6 +1663,74 @@ class Groq_AI_Product_Text_Settings_Page {
|
||||
<?php
|
||||
}
|
||||
|
||||
public function render_product_attribute_includes_field() {
|
||||
$settings = $this->plugin->get_settings();
|
||||
$values = isset( $settings['product_attribute_includes'] ) && is_array( $settings['product_attribute_includes'] )
|
||||
? $settings['product_attribute_includes']
|
||||
: [];
|
||||
$values = array_values( array_unique( array_map( 'sanitize_key', $values ) ) );
|
||||
|
||||
$options = $this->get_product_attribute_include_options();
|
||||
?>
|
||||
<div class="groq-ai-attribute-includes">
|
||||
<p class="description" style="margin-top:0;">
|
||||
<?php esc_html_e( 'Selecteer welke productattributen je als context mee wilt sturen naar de AI. Als je niets selecteert, worden attributen niet meegestuurd (tenzij je dit eerder al had ingeschakeld via de oude instelling).', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?>
|
||||
</p>
|
||||
<?php if ( empty( $options ) ) : ?>
|
||||
<p class="description">
|
||||
<?php esc_html_e( 'Geen WooCommerce-attributen gevonden.', GROQ_AI_PRODUCT_TEXT_DOMAIN ); ?>
|
||||
</p>
|
||||
<?php else : ?>
|
||||
<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(260px,1fr));gap:8px;">
|
||||
<?php foreach ( $options as $key => $label ) :
|
||||
$checked = in_array( $key, $values, true );
|
||||
?>
|
||||
<label style="display:flex;gap:8px;align-items:flex-start;">
|
||||
<input type="checkbox" name="<?php echo esc_attr( $this->plugin->get_option_key() ); ?>[product_attribute_includes][]" value="<?php echo esc_attr( $key ); ?>" <?php checked( $checked ); ?> />
|
||||
<span><?php echo esc_html( $label ); ?></span>
|
||||
</label>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
private function get_product_attribute_include_options() {
|
||||
$options = [
|
||||
'__custom__' => __( 'Custom attributen (niet-taxonomie)', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
||||
];
|
||||
|
||||
if ( function_exists( 'wc_get_attribute_taxonomies' ) ) {
|
||||
$taxonomies = wc_get_attribute_taxonomies();
|
||||
if ( is_array( $taxonomies ) ) {
|
||||
foreach ( $taxonomies as $attr ) {
|
||||
$name = isset( $attr->attribute_name ) ? sanitize_key( (string) $attr->attribute_name ) : '';
|
||||
$label = isset( $attr->attribute_label ) ? sanitize_text_field( (string) $attr->attribute_label ) : '';
|
||||
if ( '' === $name ) {
|
||||
continue;
|
||||
}
|
||||
$taxonomy = 'pa_' . $name;
|
||||
if ( '' === $label ) {
|
||||
$label = function_exists( 'wc_attribute_label' ) ? wc_attribute_label( $taxonomy ) : $taxonomy;
|
||||
}
|
||||
$options[ $taxonomy ] = $label;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( count( $options ) > 1 ) {
|
||||
$fixed = [
|
||||
'__custom__' => $options['__custom__'],
|
||||
];
|
||||
unset( $options['__custom__'] );
|
||||
asort( $options, SORT_NATURAL | SORT_FLAG_CASE );
|
||||
$options = $fixed + $options;
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
public function render_response_format_compat_field() {
|
||||
$settings = $this->plugin->get_settings();
|
||||
$is_enabled = ! empty( $settings['response_format_compat'] );
|
||||
|
||||
@@ -105,20 +105,14 @@ class Groq_AI_Ajax_Controller {
|
||||
];
|
||||
}
|
||||
|
||||
$default_bottom_key = isset( $settings['term_bottom_description_meta_key'] ) ? sanitize_key( (string) $settings['term_bottom_description_meta_key'] ) : '';
|
||||
$bottom_meta_key = apply_filters( 'groq_ai_term_bottom_description_meta_key', $default_bottom_key, $term, $settings );
|
||||
$bottom_meta_key = sanitize_key( (string) $bottom_meta_key );
|
||||
$has_bottom_field = ( '' !== $bottom_meta_key );
|
||||
|
||||
$top_description = isset( $parsed['description'] ) ? (string) $parsed['description'] : '';
|
||||
$bottom_description = isset( $parsed['bottom_description'] ) ? (string) $parsed['bottom_description'] : '';
|
||||
$apply_text = $has_bottom_field ? ( '' !== $bottom_description ? $bottom_description : $top_description ) : $top_description;
|
||||
|
||||
wp_send_json_success(
|
||||
[
|
||||
'top_description' => $top_description,
|
||||
'bottom_description' => $has_bottom_field ? $apply_text : $bottom_description,
|
||||
'description' => $apply_text,
|
||||
'top_description' => isset( $parsed['top_description'] ) ? $parsed['top_description'] : ( isset( $parsed['description'] ) ? $parsed['description'] : '' ),
|
||||
'bottom_description' => isset( $parsed['bottom_description'] ) ? $parsed['bottom_description'] : '',
|
||||
'meta_title' => isset( $parsed['meta_title'] ) ? $parsed['meta_title'] : '',
|
||||
'meta_description' => isset( $parsed['meta_description'] ) ? $parsed['meta_description'] : '',
|
||||
'focus_keywords' => isset( $parsed['focus_keywords'] ) ? $parsed['focus_keywords'] : '',
|
||||
'description' => isset( $parsed['description'] ) ? $parsed['description'] : ( isset( $parsed['top_description'] ) ? $parsed['top_description'] : '' ),
|
||||
'raw' => $response_text,
|
||||
]
|
||||
);
|
||||
@@ -149,6 +143,23 @@ class Groq_AI_Ajax_Controller {
|
||||
$system_prompt = $prompt_builder->build_system_prompt( $settings, $conversation_id );
|
||||
$model = $this->plugin->get_selected_model( $provider, $settings );
|
||||
$context_fields = $prompt_builder->parse_context_fields_from_request( isset( $_POST['context_fields'] ) ? $_POST['context_fields'] : '', $settings );
|
||||
if ( array_key_exists( 'attribute_includes', $_POST ) ) {
|
||||
$attribute_includes = [];
|
||||
$attribute_raw = (string) wp_unslash( $_POST['attribute_includes'] );
|
||||
$decoded = json_decode( $attribute_raw, true );
|
||||
if ( is_array( $decoded ) ) {
|
||||
foreach ( $decoded as $value ) {
|
||||
$key = sanitize_key( (string) $value );
|
||||
if ( '' === $key ) {
|
||||
continue;
|
||||
}
|
||||
if ( in_array( $key, [ '__custom__', '__all__' ], true ) || 0 === strpos( $key, 'pa_' ) ) {
|
||||
$attribute_includes[] = $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
$settings['product_attribute_includes'] = array_values( array_unique( $attribute_includes ) );
|
||||
}
|
||||
$image_context_mode = $this->plugin->get_image_context_mode( $settings );
|
||||
$image_context_limit = $this->plugin->get_image_context_limit( $settings );
|
||||
|
||||
@@ -174,7 +185,7 @@ class Groq_AI_Ajax_Controller {
|
||||
}
|
||||
}
|
||||
|
||||
$product_context_text = $prompt_builder->build_product_context_block( $post_id, $context_fields, $prompt_image_mode, $image_context_limit );
|
||||
$product_context_text = $prompt_builder->build_product_context_block( $post_id, $context_fields, $prompt_image_mode, $image_context_limit, $settings );
|
||||
$image_context_payloads = [];
|
||||
if ( $use_base64_payloads ) {
|
||||
$image_context_payloads = $prompt_builder->get_product_image_payloads( $post_id, $image_context_limit );
|
||||
|
||||
@@ -338,7 +338,7 @@ class Groq_AI_Prompt_Builder {
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
public function build_product_context_block( $post_id, $fields, $image_mode = 'url', $image_limit = 3 ) {
|
||||
public function build_product_context_block( $post_id, $fields, $image_mode = 'url', $image_limit = 3, $settings = null ) {
|
||||
$post_id = absint( $post_id );
|
||||
|
||||
if ( ! $post_id ) {
|
||||
@@ -368,8 +368,14 @@ class Groq_AI_Prompt_Builder {
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $fields['attributes'] ) ) {
|
||||
$attributes = $this->get_product_attributes_text( $post_id );
|
||||
$attribute_includes = [];
|
||||
if ( is_array( $settings ) && isset( $settings['product_attribute_includes'] ) && is_array( $settings['product_attribute_includes'] ) ) {
|
||||
$attribute_includes = array_values( array_unique( array_map( 'sanitize_key', $settings['product_attribute_includes'] ) ) );
|
||||
}
|
||||
|
||||
$include_attributes = ! empty( $attribute_includes ) || ! empty( $fields['attributes'] );
|
||||
if ( $include_attributes ) {
|
||||
$attributes = $this->get_product_attributes_text( $post_id, $attribute_includes );
|
||||
if ( $attributes ) {
|
||||
$parts[] = sprintf( __( 'Attributen: %s', GROQ_AI_PRODUCT_TEXT_DOMAIN ), $attributes );
|
||||
}
|
||||
@@ -425,6 +431,7 @@ class Groq_AI_Prompt_Builder {
|
||||
}
|
||||
|
||||
$bottom_meta_key = $this->resolve_term_bottom_description_meta_key( $term, $settings );
|
||||
$bottom_meta_key = '' !== $bottom_meta_key ? $bottom_meta_key : 'groq_ai_term_bottom_description';
|
||||
if ( '' !== $bottom_meta_key && $term_id ) {
|
||||
$bottom = (string) get_term_meta( $term_id, $bottom_meta_key, true );
|
||||
$bottom = trim( wp_strip_all_tags( $bottom ) );
|
||||
@@ -474,27 +481,18 @@ class Groq_AI_Prompt_Builder {
|
||||
$title_pixels = $this->settings_manager->get_rankmath_meta_title_pixel_limit( $settings );
|
||||
$desc_pixels = $this->settings_manager->get_rankmath_meta_description_pixel_limit( $settings );
|
||||
|
||||
$bottom_meta_key = $this->resolve_term_bottom_description_meta_key( null, $settings );
|
||||
$use_bottom_field = ( '' !== $bottom_meta_key );
|
||||
|
||||
$properties = [];
|
||||
$required = [];
|
||||
|
||||
if ( $use_bottom_field ) {
|
||||
$properties['bottom_description'] = [
|
||||
$properties = [
|
||||
'top_description' => [
|
||||
'type' => 'string',
|
||||
'description' => __( 'Uitgebreide HTML-omschrijving (helemaal onderaan) met paragrafen en eventueel lijstjes.', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
||||
'description' => __( 'Korte HTML-omschrijving (1 alinea) voor de standaard WordPress term description. Exact één alinea in <p>-tags.', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
||||
'minLength' => 20,
|
||||
];
|
||||
$required[] = 'bottom_description';
|
||||
} else {
|
||||
$properties['description'] = [
|
||||
],
|
||||
'bottom_description' => [
|
||||
'type' => 'string',
|
||||
'description' => __( 'HTML-omschrijving (WordPress term description) met paragrafen en eventueel lijstjes.', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
||||
'description' => __( 'Uitgebreide HTML-omschrijving (helemaal onderaan), 2–4 alinea’s, met paragrafen en eventueel lijstjes.', GROQ_AI_PRODUCT_TEXT_DOMAIN ),
|
||||
'minLength' => 20,
|
||||
];
|
||||
$required[] = 'description';
|
||||
}
|
||||
],
|
||||
];
|
||||
|
||||
if ( $rankmath_enabled ) {
|
||||
$properties['meta_title'] = [
|
||||
@@ -529,7 +527,7 @@ class Groq_AI_Prompt_Builder {
|
||||
$schema = [
|
||||
'type' => 'object',
|
||||
'properties' => $properties,
|
||||
'required' => $required,
|
||||
'required' => [ 'top_description', 'bottom_description' ],
|
||||
'additionalProperties' => false,
|
||||
];
|
||||
|
||||
@@ -572,36 +570,24 @@ class Groq_AI_Prompt_Builder {
|
||||
];
|
||||
}
|
||||
|
||||
$bottom_meta_key = $this->resolve_term_bottom_description_meta_key( null, $settings );
|
||||
$use_bottom_field = ( '' !== $bottom_meta_key );
|
||||
|
||||
$description = isset( $decoded['description'] ) ? trim( (string) $decoded['description'] ) : '';
|
||||
$top = isset( $decoded['top_description'] ) ? trim( (string) $decoded['top_description'] ) : '';
|
||||
$bottom = isset( $decoded['bottom_description'] ) ? trim( (string) $decoded['bottom_description'] ) : '';
|
||||
|
||||
if ( $use_bottom_field ) {
|
||||
if ( '' === $bottom ) {
|
||||
$bottom = '' !== $description ? $description : $top;
|
||||
}
|
||||
if ( '' === $bottom ) {
|
||||
return new WP_Error( 'groq_ai_parse_error', __( 'De AI-respons bevatte geen bottom_description veld.', GROQ_AI_PRODUCT_TEXT_DOMAIN ) );
|
||||
}
|
||||
} else {
|
||||
if ( '' === $description ) {
|
||||
$description = '' !== $top ? $top : $bottom;
|
||||
}
|
||||
if ( '' === $description ) {
|
||||
return new WP_Error( 'groq_ai_parse_error', __( 'De AI-respons bevatte geen description veld.', GROQ_AI_PRODUCT_TEXT_DOMAIN ) );
|
||||
}
|
||||
// Backward compatibility: older prompts only returned `description`.
|
||||
if ( '' === $top && isset( $decoded['description'] ) ) {
|
||||
$top = trim( (string) $decoded['description'] );
|
||||
}
|
||||
if ( '' === $top && '' === $bottom ) {
|
||||
return new WP_Error( 'groq_ai_parse_error', __( 'De AI-respons bevatte geen top_description/bottom_description velden.', GROQ_AI_PRODUCT_TEXT_DOMAIN ) );
|
||||
}
|
||||
|
||||
$result = [];
|
||||
if ( $use_bottom_field ) {
|
||||
$result['bottom_description'] = $bottom;
|
||||
if ( '' !== $top ) {
|
||||
$result['top_description'] = $top;
|
||||
// For backwards compatibility with existing UI, keep `description` alias.
|
||||
$result['description'] = $bottom;
|
||||
} else {
|
||||
$result['description'] = $description;
|
||||
$result['description'] = $top;
|
||||
}
|
||||
if ( '' !== $bottom ) {
|
||||
$result['bottom_description'] = $bottom;
|
||||
}
|
||||
|
||||
if ( isset( $decoded['meta_title'] ) ) {
|
||||
@@ -628,15 +614,10 @@ class Groq_AI_Prompt_Builder {
|
||||
}
|
||||
|
||||
private function get_term_structured_response_instructions( $settings = null ) {
|
||||
$bottom_meta_key = $this->resolve_term_bottom_description_meta_key( null, $settings );
|
||||
$use_bottom_field = ( '' !== $bottom_meta_key );
|
||||
|
||||
$schema_parts = [];
|
||||
if ( $use_bottom_field ) {
|
||||
$schema_parts[] = '"bottom_description":"..."';
|
||||
} else {
|
||||
$schema_parts[] = '"description":"..."';
|
||||
}
|
||||
$schema_parts = [
|
||||
'"top_description":"..."',
|
||||
'"bottom_description":"..."',
|
||||
];
|
||||
|
||||
$rankmath_enabled = $this->settings_manager->is_module_enabled( 'rankmath', $settings );
|
||||
if ( $rankmath_enabled ) {
|
||||
@@ -652,13 +633,9 @@ class Groq_AI_Prompt_Builder {
|
||||
$json_structure
|
||||
);
|
||||
|
||||
if ( $use_bottom_field ) {
|
||||
$instruction .= ' ' . __( 'Zorg dat bottom_description geldige HTML bevat. Dit is de tekst die helemaal onderaan de pagina komt.', GROQ_AI_PRODUCT_TEXT_DOMAIN );
|
||||
} else {
|
||||
$instruction .= ' ' . __( 'Zorg dat description geldige HTML bevat.', GROQ_AI_PRODUCT_TEXT_DOMAIN );
|
||||
}
|
||||
$instruction .= ' ' . __( 'Zorg dat top_description en bottom_description geldige HTML bevatten. top_description moet exact één alinea zijn in <p>-tags. bottom_description moet 2–4 alinea’s bevatten.', GROQ_AI_PRODUCT_TEXT_DOMAIN );
|
||||
$instruction .= ' ' . __( 'Voeg geen extra tekst buiten het JSON-object toe.', GROQ_AI_PRODUCT_TEXT_DOMAIN );
|
||||
$instruction .= ' ' . __( 'Als in de context een sectie "Interne links" staat, verwerk dan 2–5 van deze links natuurlijk in de hoofdtekst als HTML-links (<a href="URL">Anker</a>).', GROQ_AI_PRODUCT_TEXT_DOMAIN );
|
||||
$instruction .= ' ' . __( 'Als in de context een sectie "Interne links" staat, verwerk dan 2–5 van deze links natuurlijk in bottom_description als HTML-links (<a href="URL">Anker</a>).', GROQ_AI_PRODUCT_TEXT_DOMAIN );
|
||||
return $instruction;
|
||||
}
|
||||
|
||||
@@ -862,7 +839,7 @@ class Groq_AI_Prompt_Builder {
|
||||
return substr( $text, 0, $limit );
|
||||
}
|
||||
|
||||
private function get_product_attributes_text( $post_id ) {
|
||||
private function get_product_attributes_text( $post_id, $attribute_includes = [] ) {
|
||||
if ( ! function_exists( 'wc_get_product' ) ) {
|
||||
return '';
|
||||
}
|
||||
@@ -879,14 +856,27 @@ class Groq_AI_Prompt_Builder {
|
||||
return '';
|
||||
}
|
||||
|
||||
$attribute_includes = is_array( $attribute_includes ) ? array_values( array_unique( array_map( 'sanitize_key', $attribute_includes ) ) ) : [];
|
||||
$include_all = empty( $attribute_includes ) || in_array( '__all__', $attribute_includes, true );
|
||||
$include_custom = $include_all || in_array( '__custom__', $attribute_includes, true );
|
||||
|
||||
$lines = [];
|
||||
|
||||
foreach ( $attributes as $attribute ) {
|
||||
if ( $attribute->is_taxonomy() ) {
|
||||
$terms = wc_get_product_terms( $post_id, $attribute->get_name(), [ 'fields' => 'names' ] );
|
||||
$taxonomy_name = sanitize_key( (string) $attribute->get_name() );
|
||||
if ( ! $include_all && ! in_array( $taxonomy_name, $attribute_includes, true ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$terms = wc_get_product_terms( $post_id, $taxonomy_name, [ 'fields' => 'names' ] );
|
||||
$value = implode( ', ', array_map( 'sanitize_text_field', (array) $terms ) );
|
||||
$label = wc_attribute_label( $attribute->get_name() );
|
||||
$label = wc_attribute_label( $taxonomy_name );
|
||||
} else {
|
||||
if ( ! $include_custom ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$options = $attribute->get_options();
|
||||
$value = implode( ', ', array_map( 'sanitize_text_field', (array) $options ) );
|
||||
$label = sanitize_text_field( $attribute->get_name() );
|
||||
|
||||
@@ -33,6 +33,7 @@ class Groq_AI_Settings_Manager {
|
||||
'store_context' => '',
|
||||
'default_prompt' => '',
|
||||
'max_output_tokens' => 2048,
|
||||
'product_attribute_includes' => [],
|
||||
'term_bottom_description_meta_key' => '',
|
||||
'groq_api_key' => '',
|
||||
'openai_api_key' => '',
|
||||
@@ -74,6 +75,10 @@ class Groq_AI_Settings_Manager {
|
||||
$limit = isset( $settings['image_context_limit'] ) ? $this->sanitize_image_context_limit_value( $settings['image_context_limit'] ) : 3;
|
||||
$settings['image_context_limit'] = $limit;
|
||||
|
||||
$settings['product_attribute_includes'] = $this->sanitize_product_attribute_includes(
|
||||
isset( $settings['product_attribute_includes'] ) ? $settings['product_attribute_includes'] : []
|
||||
);
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
@@ -90,6 +95,7 @@ class Groq_AI_Settings_Manager {
|
||||
'store_context' => '',
|
||||
'default_prompt' => '',
|
||||
'max_output_tokens' => 2048,
|
||||
'product_attribute_includes' => [],
|
||||
'term_bottom_description_meta_key' => '',
|
||||
'groq_api_key' => '',
|
||||
'openai_api_key' => '',
|
||||
@@ -151,6 +157,7 @@ class Groq_AI_Settings_Manager {
|
||||
'store_context' => sanitize_textarea_field( $input['store_context'] ),
|
||||
'default_prompt' => sanitize_textarea_field( $input['default_prompt'] ),
|
||||
'max_output_tokens' => $max_output_tokens,
|
||||
'product_attribute_includes' => $this->sanitize_product_attribute_includes( isset( $raw_input['product_attribute_includes'] ) ? $raw_input['product_attribute_includes'] : [] ),
|
||||
'term_bottom_description_meta_key' => sanitize_key( (string) $input['term_bottom_description_meta_key'] ),
|
||||
'groq_api_key' => sanitize_text_field( $input['groq_api_key'] ),
|
||||
'openai_api_key' => sanitize_text_field( $input['openai_api_key'] ),
|
||||
@@ -177,6 +184,33 @@ class Groq_AI_Settings_Manager {
|
||||
];
|
||||
}
|
||||
|
||||
private function sanitize_product_attribute_includes( $value ) {
|
||||
if ( ! is_array( $value ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$clean = [];
|
||||
foreach ( $value as $item ) {
|
||||
$item = sanitize_key( (string) $item );
|
||||
if ( '' === $item ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Allow special tokens and attribute taxonomies.
|
||||
if ( in_array( $item, [ '__all__', '__custom__' ], true ) || 0 === strpos( $item, 'pa_' ) ) {
|
||||
$clean[] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
$clean = array_values( array_unique( $clean ) );
|
||||
// Hard cap to avoid overly large option payloads.
|
||||
if ( count( $clean ) > 200 ) {
|
||||
$clean = array_slice( $clean, 0, 200 );
|
||||
}
|
||||
|
||||
return $clean;
|
||||
}
|
||||
|
||||
public function get_context_field_definitions() {
|
||||
if ( null === $this->context_field_definitions ) {
|
||||
$this->context_field_definitions = [
|
||||
|
||||
Reference in New Issue
Block a user