This repository was archived by the owner on Jan 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-dictate-button.php
More file actions
214 lines (181 loc) · 6.07 KB
/
class-dictate-button.php
File metadata and controls
214 lines (181 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* Plugin Name: Dictate Button
* Description: Adds speech-to-text dictation functionality to WordPress forms via dictate-button.io.
* Tags: voice input, speech-to-text, transcription, dictation, dictate-button
* Version: 1.3.0
* Author: Dictate Button
* Author URI: https://dictate-button.io/
* Text Domain: dictate-button
* License: Apache-2.0
*
* @package dictate-button
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'DICTATE_BUTTON_JS_FALLBACK_VERSION', '1.4.1' );
// Include admin settings.
require_once plugin_dir_path( __FILE__ ) . 'admin/class-dictate-button-settings.php';
/**
* Dictate Button class.
*/
class Dictate_Button {
/**
* Constructor.
*/
public function __construct() {
// Enqueue scripts and styles.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// End-of-life notice.
add_action( 'admin_notices', array( $this, 'eol_notice' ) );
// Add dictate button to comment forms if enabled.
if ( Dictate_Button_Settings::get_option( 'comments' ) === 'on' ) {
add_filter( 'comment_form_defaults', array( $this, 'add_dictate_button_to_comment_form' ) );
}
// Add dictate button to search forms if enabled.
if ( Dictate_Button_Settings::get_option( 'search' ) === 'on' ) {
add_filter( 'get_search_form', array( $this, 'add_dictate_button_to_search_form' ) );
add_filter( 'render_block_core/search', array( $this, 'add_dictate_button_to_search_block' ), 10, 2 );
}
// Add dictate button to Contact Form 7 if enabled.
if ( Dictate_Button_Settings::get_option( 'contact_form_7' ) === 'on' ) {
add_filter( 'wpcf7_form_elements', array( $this, 'add_dictate_button_to_cf7' ), 10, 1 );
}
}
public function eol_notice() {
if ( ! is_admin() ) {
return;
}
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
echo '<div class="notice notice-error">';
echo '<p><strong>The Dictate Button plugin is no longer supported.</strong><br>';
echo 'The external API it depends on is being changed or discontinued and the plugin will stop working. ';
echo 'Please remove this plugin from the <a href="' . esc_url( admin_url( 'plugins.php' ) ) . '">plugins page</a>. Sorry for the inconvenience :(</p>';
echo '</div>';
}
/**
* Enqueue scripts and styles for frontend.
*/
public function enqueue_scripts() {
$should_load = false;
// Check if we should load the scripts based on settings.
if ( ( is_singular() && comments_open() && 'on' === Dictate_Button_Settings::get_option( 'comments' ) ) ||
( 'on' === Dictate_Button_Settings::get_option( 'search' ) ) ||
( 'on' === Dictate_Button_Settings::get_option( 'contact_form_7' ) && $this->has_contact_form_7() )
) {
$should_load = true;
}
if ( $should_load ) {
$this->register_dictate_button_scripts();
}
}
/**
* Register dictate button scripts.
*/
private function register_dictate_button_scripts() {
wp_register_script(
'dictate-button-bundle',
plugin_dir_url( __FILE__ ) . 'assets/js/dictate-button-bundle.js',
array(),
$this->get_dictate_button_version(),
true
);
wp_enqueue_script( 'dictate-button-bundle' );
}
/**
* Add dictate button to comment form.
*
* @param array $defaults The comment form defaults.
* @return array The comment form defaults with dictate button data attribute.
*/
public function add_dictate_button_to_comment_form( $defaults ) {
// Add data attribute to comment textarea.
$defaults['comment_field'] = str_replace(
'<textarea',
'<textarea data-dictate-button-on',
$defaults['comment_field']
);
return $defaults;
}
/**
* Add dictate button to search form.
*
* @param string $form The search form HTML.
* @return string The search form HTML with dictate button data attribute.
*/
public function add_dictate_button_to_search_form( $form ) {
// Add data attribute to search input.
$form = str_replace(
'type="search"',
'type="search" data-dictate-button-on',
$form
);
return $form;
}
/**
* Add dictate button to search block.
*
* @param string $block_content The search block content.
* @return string The search block content with dictate button data attribute.
*/
public function add_dictate_button_to_search_block( $block_content ) {
// Add data attribute to search input in block content.
$block_content = str_replace(
'type="search"',
'type="search" data-dictate-button-on',
$block_content
);
return $block_content;
}
/**
* Check if Contact Form 7 is active and has forms on current page.
*/
private function has_contact_form_7() {
return class_exists( 'WPCF7' ) && ( has_shortcode( get_post()->post_content ?? '', 'contact-form-7' ) || is_admin() );
}
/**
* Add dictate button to Contact Form 7 textarea fields.
*
* @param string $html The Contact Form 7 HTML.
* @return string The Contact Form 7 HTML with dictate button data attribute.
*/
public function add_dictate_button_to_cf7( $html ) {
// Add data attribute to textarea elements.
$html = str_replace(
'<textarea',
'<textarea data-dictate-button-on',
$html
);
return $html;
}
/**
* Get dictate-button version from package.json.
*/
private function get_dictate_button_version() {
$package_json_path = plugin_dir_path( __FILE__ ) . 'package.json';
if ( ! file_exists( $package_json_path ) ) {
return DICTATE_BUTTON_JS_FALLBACK_VERSION;
}
// Initialize WordPress filesystem.
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();
global $wp_filesystem;
$package_json_content = $wp_filesystem->get_contents( $package_json_path );
if ( false === $package_json_content ) {
return DICTATE_BUTTON_JS_FALLBACK_VERSION;
}
$package_json = json_decode( $package_json_content, true );
if ( ! $package_json || ! isset( $package_json['dependencies']['dictate-button'] ) ) {
return DICTATE_BUTTON_JS_FALLBACK_VERSION;
}
// Clean version string using regex to remove prefixes and suffixes.
$version = $package_json['dependencies']['dictate-button'];
return preg_replace( '/^[~^>=<]+|[-+].*$/', '', $version );
}
}
// Initialize the plugin.
new Dictate_Button();