PluginProbe
BeyondWords – AI audio for publishers / 7.2.0
BeyondWords – AI audio for publishers v7.2.0
7.2.0 7.1.0 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 All 44 releases
speechkit / src / editor / components / generate-audio / class-generate-audio.php

class-generate-audio.php in BeyondWords – AI audio for publishers 7.2.0, at src/editor/components/generate-audio/class-generate-audio.php

189 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types = 1 );
4
5 /**
6 * BeyondWords Component: Generate audio
7 *
8 * @package BeyondWords\Editor\Components
9 * @author Stuart McAlpine <[email protected]>
10 * @since 3.0.0
11 * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods.
12 */
13
14 namespace BeyondWords\Editor\Components;
15
16 /**
17 * GenerateAudio
18 *
19 * @since 3.0.0
20 * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods.
21 */
22 defined( 'ABSPATH' ) || exit;
23
24 class GenerateAudio {
25
26 /**
27 * Init.
28 *
29 * @since 4.0.0
30 * @since 6.0.0 Make static.
31 * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods.
32 */
33 public static function init() {
34 add_action(
35 'wp_loaded',
36 function (): void {
37 $post_types = \BeyondWords\Settings\Utils::get_compatible_post_types();
38
39 if ( is_array( $post_types ) ) {
40 foreach ( $post_types as $post_type ) {
41 add_action( "save_post_{$post_type}", [ self::class, 'save'], 10 );
42 }
43 }
44 }
45 );
46
47 add_action( 'admin_enqueue_scripts', [ self::class, 'admin_enqueue_scripts' ] );
48 }
49
50 /**
51 * Enqueue the classic-editor Generate audio script.
52 *
53 * Always enqueued (the caption sync needs it); `terms` mode additionally
54 * localizes the term data used to live-update the checkbox.
55 *
56 * @since 7.0.0
57 * @since 7.0.0 Always enqueued (for the caption); preselect data is optional.
58 *
59 * @param string $hook Current admin page hook.
60 */
61 public static function admin_enqueue_scripts( $hook ): void {
62 if ( \BeyondWords\Core\Utils::is_gutenberg_page() ) {
63 return;
64 }
65
66 if ( 'post.php' !== $hook && 'post-new.php' !== $hook ) {
67 return;
68 }
69
70 $post_type = get_post_type();
71
72 if ( ! in_array( $post_type, \BeyondWords\Settings\Utils::get_compatible_post_types(), true ) ) {
73 return;
74 }
75
76 wp_register_script(
77 'beyondwords-metabox--generate-audio',
78 BEYONDWORDS__PLUGIN_URI . 'src/editor/components/generate-audio/classic-metabox.js',
79 [],
80 BEYONDWORDS__PLUGIN_VERSION,
81 true
82 );
83
84 if ( \BeyondWords\Settings\Preselect::MODE_TERMS === \BeyondWords\Settings\Preselect::get_mode( $post_type ) ) {
85 $terms = \BeyondWords\Settings\Preselect::get_selected_terms( $post_type );
86
87 if ( ! empty( $terms ) ) {
88 wp_localize_script(
89 'beyondwords-metabox--generate-audio',
90 'beyondwordsPreselect',
91 [
92 'mode' => \BeyondWords\Settings\Preselect::MODE_TERMS,
93 'terms' => $terms,
94 ]
95 );
96 }
97 }
98
99 wp_enqueue_script( 'beyondwords-metabox--generate-audio' );
100 }
101
102 /**
103 * Check whether the "Generate audio" checkbox should be preselected.
104 *
105 * @since 6.0.0 Make static.
106 * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods.
107 * @since 7.0.0 Delegated to Preselect; term-gating reinstated.
108 *
109 * @param \WP_Post|int $post The post object or ID.
110 */
111 public static function should_preselect_generate_audio( $post ) {
112 return \BeyondWords\Settings\Preselect::should_preselect_for_post( $post );
113 }
114
115 /**
116 * Render the element.
117 *
118 * @since 6.0.0 Make static and refactor generate audio check.
119 * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods.
120 */
121 public static function element( $post ) {
122 wp_nonce_field( 'beyondwords_generate_audio', 'beyondwords_generate_audio_nonce' );
123
124 $generate_audio = \BeyondWords\Post\Meta::has_generate_audio( $post->ID );
125
126 // State-reflecting caption; the data attributes let classic-metabox.js keep
127 // it in step as the checkbox toggles (mirrors the block editor toggle).
128 $label_enabled = __( 'Generation enabled', 'speechkit' );
129 $label_disabled = __( 'Generation disabled', 'speechkit' );
130 ?>
131 <!-- checkbox -->
132 <p id="beyondwords-metabox-generate-audio">
133 <input
134 type="checkbox"
135 id="beyondwords_generate_audio"
136 name="beyondwords_generate_audio"
137 value="1"
138 <?php checked( $generate_audio ); ?>
139 />
140 <label for="beyondwords_generate_audio">
141 <span
142 id="beyondwords-generate-audio-label"
143 data-label-enabled="<?php echo esc_attr( $label_enabled ); ?>"
144 data-label-disabled="<?php echo esc_attr( $label_disabled ); ?>"
145 ><?php echo esc_html( $generate_audio ? $label_enabled : $label_disabled ); ?></span>
146 </label>
147 </p>
148 <?php
149 }
150
151 /**
152 * Save the meta when the post is saved.
153 *
154 * @since 6.0.0 Make static.
155 * @since 7.0.0 Refactored to BeyondWords namespace with snake_case methods.
156 *
157 * @param int $post_id The ID of the post being saved.
158 */
159 public static function save( $post_id ) {
160 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
161 return $post_id;
162 }
163
164 if (
165 ! isset( $_POST['beyondwords_generate_audio_nonce'] ) ||
166 ! wp_verify_nonce(
167 sanitize_key( $_POST['beyondwords_generate_audio_nonce'] ),
168 'beyondwords_generate_audio'
169 )
170 ) {
171 return $post_id;
172 }
173
174 if ( ! current_user_can( 'edit_post', $post_id ) ) {
175 return $post_id;
176 }
177
178 if ( isset( $_POST['beyondwords_generate_audio'] ) ) {
179 update_post_meta( $post_id, 'beyondwords_generate_audio', '1' );
180 } else {
181 delete_post_meta( $post_id, 'speechkit_error_message' );
182 delete_post_meta( $post_id, 'beyondwords_error_message' );
183 update_post_meta( $post_id, 'beyondwords_generate_audio', '0' );
184 }
185
186 return $post_id;
187 }
188 }
189