Add allowed post statuses setting for Text to Speech feature - #1160
Add allowed post statuses setting for Text to Speech feature#1160Moferanoluwa wants to merge 2 commits into
Conversation
Text to Speech currently generates audio for any post status (including Draft and Pending), which causes stale/incorrect audio to be attached to public post previews while a post is still being actively edited through several rounds of changes. Add a "post_statuses" setting to the feature, mirroring the existing pattern already used by the post_types setting and by the Classification feature's post_statuses field: - New settings field (checkbox group) to choose allowed post statuses, defaulting to Publish only. - Enforce the setting in both audio-generation entry points: save_post_metadata() (classic editor / meta box save) and rest_handle_audio() (block editor / REST save). Fixes 10up#1000
| add_settings_field( | ||
| 'post_statuses', | ||
| esc_html__( 'Allowed post statuses', 'classifai' ), | ||
| array( $this, 'render_checkbox_group' ), | ||
| $this->get_option_name(), | ||
| $this->get_option_name() . '_section', | ||
| array( | ||
| 'label_for' => 'post_statuses', | ||
| 'options' => \Classifai\get_post_statuses_for_language_settings(), | ||
| 'default_values' => $settings['post_statuses'], | ||
| 'description' => __( 'Choose which post statuses are allowed to generate audio, e.g. disable this for Draft to avoid generating audio for content that is still being edited.', 'classifai' ), | ||
| ) | ||
| ); |
There was a problem hiding this comment.
This only adds the setting for the legacy settings experience in ClassifAI. We are moving away from supporting this (it hasn't been the default v3.2.0) so there's two things needed here:
- I'd suggest removing this block as we'll be removing all of these legacy settings in an upcoming release anyway
- You'll need to add these settings via javascript. See here
There was a problem hiding this comment.
Good catch, thanks — done in 683e07b. Removed the add_settings_field() block from the legacy PHP settings screen and added the equivalent control to src/js/settings/components/feature-additional-settings/text-to-speech.js, mirroring the existing post_statuses block the Classification feature's React settings already use (same window.classifAISettings.postStatuses data source — it's already localized in Admin/Settings.php, so no new PHP localization was needed). Ran this through wp-scripts lint-js and tsc --noEmit locally, both clean.
Per review feedback: the legacy PHP settings screen (add_settings_field) is being phased out and hasn't been the default since v3.2.0, so new settings should be added to the React settings UI instead. - Remove the post_statuses add_settings_field() block from TextToSpeech::add_custom_settings_fields(). - Add the equivalent control to src/js/settings/components/feature-additional-settings/text-to-speech.js, mirroring the existing post_statuses block already used by the Classification feature's React settings (same window.classifAISettings.postStatuses data source, already localized in Admin/Settings.php - no new PHP localization needed). The default settings, sanitization, and enforcement gates added in the previous commit are unchanged - those are backend/data-layer concerns independent of which UI renders the control.
Description
Fixes #1000. As described in the issue, a client's "Sponsored Posts" workflow makes numerous text changes on Drafts, and Public Post Preview was showing the wrong/stale audio because Text to Speech currently generates audio for any post status, including Draft and Pending.
@dkotter noted:
This adds exactly that:
Feature::get_supported_post_statuses()already exists as a shared base-class method (reads$this->get_settings()['post_statuses']) —Classificationalready uses it,TextToSpeechjust wasn't populating or checking that setting yet.Classification's own default), so out of the box audio will no longer regenerate on every Draft save — which is the behavior the issue asks for. Anyone who wants the old behavior back can just check "Draft" in the new setting.@dkotter flagged in review that my first pass added the setting via the legacy PHP settings screen (
add_custom_settings_fields()), which isn't the default UI since v3.2.0 and is being phased out. Moved the actual settings control to React instead, matching howClassification's own React settings already handlepost_statuses(src/js/settings/components/feature-additional-settings/classification.js) — samewindow.classifAISettings.postStatusesdata source, already localized inAdmin/Settings.php, so no new PHP localization was needed.Changes
In
includes/Classifai/Features/TextToSpeech.php:get_feature_default_settings()— addpost_statusesdefault (publishonly).sanitize_default_feature_settings()— sanitize the new setting the same waypost_typesalready is.save_post_metadata()— gate audio generation (classic editor / meta box save path) onget_supported_post_statuses(), same pattern as the existingpost_typescheck right next to it.rest_handle_audio()— same gate for the block editor / REST save path, since that's the path Gutenberg actually uses and is likely what the reporter's workflow hits.In
src/js/settings/components/feature-additional-settings/text-to-speech.js:Classification's existing React settings component for the same setting.Testing/Review Recommendations
I don't have a full WordPress + PHP environment set up locally, so I wasn't able to run PHPCS/PHPUnit myself for the PHP side — verified that by careful manual review instead (matched the existing
post_typespattern exactly, confirmed the helper functions are already generic/shared, re-read the diff for syntax correctness).For the JS side, I do have a working toolchain: ran
wp-scripts lint-jsandtsc --noEmiton the changed file, both clean.Happy to adjust the default (e.g. include Pending too) or extend the meta-box visibility logic if you'd like the box itself hidden for disallowed statuses — I kept this PR to the functional gate to start.