-
Notifications
You must be signed in to change notification settings - Fork 0
/
reescribe-atributo-alt.php
202 lines (167 loc) · 6.25 KB
/
reescribe-atributo-alt.php
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
<?php
/*
Plugin Name: Reescribe atributo ALT
Description: Reescribe los atributos ALT y TITLE de las imágenes para mejorar la accesibilidad.
Version: 1.3
Author: A. Cambronero Blogpocket.com
Text Domain: reescribe-atributo-alt
*/
if (!defined('ABSPATH')) {
exit; // Salir si se accede directamente
}
// Añadir menú al panel de administración
add_action('admin_menu', 'raa_add_admin_menu');
add_action('admin_init', 'raa_settings_init');
function raa_add_admin_menu() {
add_options_page(
'Reescribe atributo ALT',
'Reescribe atributo ALT',
'manage_options',
'reescribe-atributo-alt',
'raa_options_page'
);
}
function raa_settings_init() {
register_setting('raa_settings_group', 'raa_settings', 'raa_sanitize_settings');
add_settings_section(
'raa_settings_section',
__('Configuración del plugin', 'reescribe-atributo-alt'),
'raa_settings_section_callback',
'raa_settings_group'
);
add_settings_field(
'include_title',
__('Incluir título del post o página', 'reescribe-atributo-alt'),
'raa_include_title_render',
'raa_settings_group',
'raa_settings_section'
);
add_settings_field(
'include_filename',
__('Incluir nombre del archivo de imagen', 'reescribe-atributo-alt'),
'raa_include_filename_render',
'raa_settings_group',
'raa_settings_section'
);
add_settings_field(
'default_text',
__('Texto predeterminado', 'reescribe-atributo-alt'),
'raa_default_text_render',
'raa_settings_group',
'raa_settings_section'
);
}
// Función de sanitización de ajustes
function raa_sanitize_settings($input) {
$sanitized_input = array();
// Saneamiento del checkbox 'include_title'
$sanitized_input['include_title'] = isset($input['include_title']) && $input['include_title'] == '1' ? '1' : '';
// Saneamiento del checkbox 'include_filename'
$sanitized_input['include_filename'] = isset($input['include_filename']) && $input['include_filename'] == '1' ? '1' : '';
// Saneamiento del texto 'default_text'
if (isset($input['default_text'])) {
$sanitized_input['default_text'] = sanitize_text_field($input['default_text']);
} else {
$sanitized_input['default_text'] = '';
}
return $sanitized_input;
}
function raa_include_title_render() {
$options = get_option('raa_settings');
$include_title = isset($options['include_title']) ? esc_attr($options['include_title']) : '';
?>
<input type='checkbox' name='raa_settings[include_title]' <?php checked($include_title, '1'); ?> value='1'>
<?php
}
function raa_include_filename_render() {
$options = get_option('raa_settings');
$include_filename = isset($options['include_filename']) ? esc_attr($options['include_filename']) : '';
?>
<input type='checkbox' name='raa_settings[include_filename]' <?php checked($include_filename, '1'); ?> value='1'>
<?php
}
function raa_default_text_render() {
$options = get_option('raa_settings');
$default_text = isset($options['default_text']) ? esc_attr($options['default_text']) : '';
?>
<input type='text' name='raa_settings[default_text]' value='<?php echo $default_text; ?>' style='width: 300px;'>
<?php
}
function raa_settings_section_callback() {
echo __('Configura cómo se reescribirán los atributos ALT y TITLE de las imágenes.', 'reescribe-atributo-alt');
}
function raa_options_page() {
?>
<div class="wrap">
<h1>Reescribe atributo ALT</h1>
<form action='options.php' method='post'>
<?php
settings_fields('raa_settings_group');
do_settings_sections('raa_settings_group');
submit_button();
?>
</form>
</div>
<?php
}
// Reescribir los atributos ALT y TITLE de las imágenes
add_filter('the_content', 'raa_modify_image_alt_tags');
function raa_modify_image_alt_tags($content) {
$options = get_option('raa_settings');
// Saneamiento de opciones
$include_title = isset($options['include_title']) && $options['include_title'] == '1';
$include_filename = isset($options['include_filename']) && $options['include_filename'] == '1';
$default_text = isset($options['default_text']) ? sanitize_text_field($options['default_text']) : '';
// Obtener el título de la página actual
$title = '';
if ($include_title) {
if (is_singular()) {
$title = get_the_title();
} elseif (is_archive()) {
$title = get_the_archive_title();
} elseif (is_home()) {
$title = get_bloginfo('name');
}
$title = sanitize_text_field($title);
}
// Cargar el contenido en DOMDocument
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));
libxml_clear_errors();
$images = $dom->getElementsByTagName('img');
foreach ($images as $img) {
$alt = $img->getAttribute('alt');
$src = $img->getAttribute('src');
$new_alt_parts = array();
if ($include_title && !empty($title)) {
$new_alt_parts[] = $title;
}
if ($include_filename && $src) {
// Obtener el nombre de archivo sin extensión
$filename = pathinfo(parse_url($src, PHP_URL_PATH), PATHINFO_FILENAME);
$filename = sanitize_text_field($filename);
$new_alt_parts[] = $filename;
}
if (!empty($alt)) {
$alt = sanitize_text_field($alt);
$new_alt_parts[] = $alt;
} else {
$new_alt_parts[] = $default_text;
}
// Filtrar partes vacías y unir con guiones
$new_alt = implode(' - ', array_filter($new_alt_parts));
// Saneamiento del atributo ALT
$new_alt = esc_attr($new_alt);
// Asignar el nuevo valor a los atributos 'alt' y 'title'
$img->setAttribute('alt', $new_alt);
$img->setAttribute('title', $new_alt);
}
// Guardar el contenido modificado
$body = $dom->getElementsByTagName('body')->item(0);
$new_content = '';
foreach ($body->childNodes as $childNode) {
$new_content .= $dom->saveHTML($childNode);
}
return $new_content;
}