Changeset 2951458
- Timestamp:
- 08/10/2023 11:36:38 AM (3 years ago)
- Location:
- accessibility-plus
- Files:
-
- 9 added
- 3 deleted
- 2 edited
-
tags/1.2.1 (added)
-
tags/1.2.1/accessibility.php (added)
-
tags/1.2.1/assets (added)
-
tags/1.2.1/assets/style.css (added)
-
tags/1.2.1/inc (added)
-
tags/1.2.1/inc/options.php (added)
-
tags/1.2.1/inc/simple_html_dom.php (added)
-
tags/1.2.1/readme.txt (added)
-
trunk/accessibility.php (modified) (8 diffs)
-
trunk/composer.json (deleted)
-
trunk/composer.lock (deleted)
-
trunk/inc/simple_html_dom.php (added)
-
trunk/readme.txt (modified) (2 diffs)
-
trunk/vendor (deleted)
Legend:
- Unmodified
- Added
- Removed
-
accessibility-plus/trunk/accessibility.php
r2949281 r2951458 4 4 Plugin URI: 5 5 Description: Improve pagespeed insight or lighthouse accessibility scores by fixing recommended issues. 6 Version: 1.2 6 Version: 1.2.1 7 7 Author: easywpstuff 8 8 Author URI: https://easywpstuff.com … … 10 10 */ 11 11 12 13 require_once(' vendor/autoload.php');12 // Load the SimpleHTMLDOM library 13 require_once('inc/simple_html_dom.php'); 14 14 15 15 // include option … … 24 24 } 25 25 26 $dom = new \DiDom\Document($html); 26 $dom = lhb_str_get_html($html, false, true, 'UTF-8', false, PHP_EOL, ' '); 27 if (empty($dom)) { 28 return $dom; 29 } 27 30 $inputs = $dom->find('input'); 28 31 29 32 foreach ($inputs as $input) { 30 33 $parent = $input->parent(); 31 34 if ($parent && $parent->tag !== 'label') { 32 if (!$input-> hasAttribute('id') && !$input->hasAttribute('placeholder') && !$input->hasAttribute('aria-label') && !$input->hasAttribute('aria-labelledby')) {35 if (!$input->getAttribute('id') && !$input->getAttribute('placeholder') && !$input->getAttribute('aria-label') && !$input->getAttribute('aria-labelledby')) { 33 36 $inputType = $input->getAttribute('type'); 34 37 if ($inputType && !in_array($inputType, ['hidden', 'button', 'submit'])) { … … 38 41 } 39 42 } 40 41 return $dom->html(); 43 return $dom->save(); 42 44 } 43 45 44 46 // Links do not have a discernible name 45 47 function easywpstuff_assesplus_add_aria_labels_to_links($html) { 46 // Remove closing tags for img and source elements to avoid parsing issues 47 $html = str_replace('</img>', '', $html); 48 $html = str_replace('</source>', '', $html); 49 50 $dom = new \DiDom\Document($html); 51 if (empty($dom)) { 52 return $dom; 53 } 54 55 $links = $dom->find('a'); 56 $filteredHeaderLinks = array_filter($dom->find('header a'), function ($link) { 57 return $link->has('i') || ($link->has('span') && trim($link->text())); 58 }); 59 60 $links = array_merge($links, $filteredHeaderLinks); 61 62 foreach ($links as $link) { 63 $span = $link->first('span'); 64 $div = $link->first('div'); 65 $spnimg = $link->first('img'); 66 67 if ($span && !$div && !$spnimg && !$link->hasAttribute('aria-label')) { 68 $span_text = trim($span->text()); 69 if ($span_text) { 70 $link->setAttribute('aria-label', $span_text); 71 } 72 } 73 74 $spanimg = $link->first('span img'); 75 if ($spanimg && !$link->hasAttribute('aria-label') && (!$spanimg->hasAttribute('alt') || !$spanimg->hasAttribute('title'))) { 76 $link->setAttribute('aria-label', 'image'); 77 } 78 79 if (!trim($link->text()) && !$link->hasAttribute('aria-label')) { 80 $child_elements = $link->find('*'); 81 if (empty($child_elements)) { 82 $link->setAttribute('aria-label', 'link'); 83 } else { 84 $icon = $link->first('i'); 85 if ($icon) { 86 $link->setAttribute('aria-label', 'icon'); 87 } else { 88 $img = $link->first('img'); 89 if ($img && !$img->hasAttribute('alt')) { 90 $link->setAttribute('aria-label', 'image'); 91 } else if ($img && (!$img->hasAttribute('alt') || !$img->hasAttribute('title'))) { 92 $link->setAttribute('aria-label', 'image'); 93 } 94 $svg = $link->first('svg'); 95 if ($svg) { 96 $link->setAttribute('aria-label', 'svg'); 97 } 98 if (!$svg && !$img) { 99 $link->setAttribute('aria-label', 'link'); 100 } 101 } 102 } 103 } 104 } 105 106 return $dom->html(); 48 $html = str_replace('</img>', '', $html); 49 $html = str_replace('</source>', '', $html); 50 51 $dom = lhb_str_get_html($html, false, true, 'UTF-8', false, PHP_EOL, ' '); 52 if (empty($dom)) { 53 return $dom; 54 } 55 $links = $dom->find('a'); 56 $headerLinks = $dom->find('header a'); 57 $filteredHeaderLinks = array_filter($headerLinks, function($link) { 58 return $link->find('i', 0) || ($link->find('span', 0) && trim($link->plaintext)); 59 }); 60 $links = array_merge($links, $filteredHeaderLinks); 61 62 foreach ($links as $link) { 63 $span = $link->find('span',0); 64 $div = $link->find('div',0); 65 $spnimg = $link->find('img',0); 66 if($span && !$div && !$spnimg && !$link->getattribute('aria-label')){ 67 $span_text = trim($span->plaintext); 68 if($span_text){ 69 $link->setattribute('aria-label', $span_text); 70 } 71 } 72 $spanimg = $link->find('span img',0); 73 if ($spanimg && !$link->getattribute('aria-label') && ($img->getattribute('alt')=='' || $img->getattribute('title')=='') ) { 74 $link->setattribute('aria-label', 'image'); 75 } 76 if (!trim($link->plaintext) && !$link->getattribute('aria-label')) { 77 $child_elements = $link->find('*'); 78 if (empty($child_elements)) { 79 $link->setattribute('aria-label', 'link'); 80 } else { 81 $icon = $link->find('i', 0); 82 if ($icon) { 83 $link->setattribute('aria-label', 'icon'); 84 } else { 85 $img = $link->find('img', 0); 86 if ($img && !$img->getattribute('alt')) { 87 $link->setattribute('aria-label', 'image'); 88 } 89 else if ($img && ($img->getattribute('alt')=='' || $img->getattribute('title')=='')) { 90 $link->setattribute('aria-label', 'image'); 91 } 92 $svg = $link->find('svg', 0); 93 if ($svg) { 94 $link->setattribute('aria-label', 'svg'); 95 } 96 if (!$svg && !$img) { 97 $link->setattribute('aria-label', 'link'); 98 } 99 } 100 } 101 } 102 } 103 104 return $dom->save(); 107 105 } 108 106 … … 111 109 112 110 function easywpstuff_assesplus_add_aria_labels_to_buttons($html) { 113 $dom = new \DiDom\Document($html); 111 112 $dom = lhb_str_get_html($html, false, true, 'UTF-8', false, PHP_EOL, ' '); 113 if (empty($dom)) { 114 return $dom; 115 } 114 116 $buttons = $dom->find('button'); 115 117 116 118 foreach ($buttons as $button) { 117 if (!trim($button-> text()) && !$button->hasAttribute('aria-label')) {119 if (!trim($button->plaintext) && !$button->getattribute('aria-label')) { 118 120 $child_elements = $button->find('*'); 119 121 if (empty($child_elements)) { 120 $button->set Attribute('aria-label', 'button');122 $button->setattribute('aria-label', 'button'); 121 123 } else { 122 $button->setAttribute('aria-label', 'button'); 123 } 124 } 125 if (!$button->hasAttribute('aria-label')) { 126 $svg = $button->first('svg'); 127 $path = $button->first('path'); 128 $span = $button->first('span'); 129 130 if ($svg && $path) { 131 $button->setAttribute('aria-label', 'button'); 132 } elseif ($span) { 133 $span_text = trim($span->text()); 134 if ($span_text) { 135 $button->setAttribute('aria-label', $span_text); 136 } 137 } 138 } 139 } 140 141 return $dom->html(); 124 $button->setattribute('aria-label', 'button'); 125 } 126 } 127 if (!$button->getattribute('aria-label')) { 128 $svg = $button->find('svg',0); 129 $path = $button->find('path',0); 130 $span = $button->find('span',0); 131 if($svg && $path){ 132 $button->setattribute('aria-label', 'button'); 133 }elseif($span){ 134 $span_text = trim($span->plaintext); 135 if($span_text){ 136 $button->setattribute('aria-label', $span_text); 137 } 138 } 139 } 140 } 141 return $dom->save(); 142 142 } 143 143 144 144 //[user-scalable="no"] is used in the <meta name="viewport"> element or the [maximum-scale] attribute is less than 5. 145 145 function easywpstuff_assesplus_modify_viewport_meta_tag($html) { 146 $dom = new \DiDom\Document($html); 147 $viewport_meta_tag = $dom->first('meta[name=viewport]'); 148 146 147 $dom = lhb_str_get_html($html, false, true, 'UTF-8', false, PHP_EOL, ' '); 148 if (empty($dom)) { 149 return $dom; 150 } 151 $viewport_meta_tag = $dom->find('meta[name=viewport]', 0); 149 152 if ($viewport_meta_tag) { 150 153 $content = $viewport_meta_tag->getAttribute('content'); 151 154 152 155 $content = preg_replace('/,\s*user-scalable=(?:no|0)/', '', $content); 153 156 154 157 if (preg_match('/maximum-scale=([^,\s]*)/', $content, $matches)) { 158 155 159 $maximum_scale = (float) $matches[1]; 156 160 if ($maximum_scale < 2) { … … 163 167 $viewport_meta_tag->setAttribute('content', $content); 164 168 } 165 166 return $dom->html(); 167 } 169 return $dom->save(); 170 } 171 168 172 169 173 function easywpstuff_assesplus_add_aria_labels_to_headers($html) { 170 $dom = new \DiDom\Document($html); 171 foreach ($dom->find('header div') as $div) { 172 $role = $div->getAttribute('role'); 173 if ($role === 'link' && !$div->hasAttribute('aria-label')) { 174 $dom = lhb_str_get_html($html, false, true, 'UTF-8', false, PHP_EOL, ' '); 175 if (empty($dom)) { 176 return $dom; 177 } 178 179 foreach($dom->find('header div') as $div) { 180 if ($div->role == 'link' && !$div->hasAttribute('aria-label')) { 174 181 $div->setAttribute('aria-label', 'link'); 175 182 } 176 if ($ role === 'button' && !$div->hasAttribute('aria-label')) {183 if ($div->role == 'button' && !$div->hasAttribute('aria-label')) { 177 184 $div->setAttribute('aria-label', 'button'); 178 185 } 179 if ($ role === 'menuitem' && !$div->hasAttribute('aria-label')) {186 if ($div->role == 'menuitem' && !$div->hasAttribute('aria-label')) { 180 187 $div->setAttribute('aria-label', 'menuitem'); 181 188 } 182 189 } 183 190 184 return $dom->html(); 191 $html = $dom->save(); // Save the modified HTML 192 return $html; // Return the modified HTML 185 193 } 186 194 187 195 // <frame> or <iframe> elements do not have a title 188 196 function easywpstuff_assesplus_booster_add_to_iframes($html) { 189 $dom = new \DiDom\Document($html); 190 foreach ($dom->find('iframe') as $iframe) { 197 $dom = lhb_str_get_html($html, false, true, 'UTF-8', false, PHP_EOL, ' '); 198 if (empty($dom)) { 199 return $dom; 200 } 201 202 foreach($dom->find('iframe') as $iframe) { 191 203 if (!$iframe->hasAttribute('title')) { 192 204 $iframe->setAttribute('title', 'iframe'); … … 194 206 } 195 207 196 return $dom->html(); 208 $html = $dom->save(); 209 return $html; 197 210 } 198 211 199 212 //Links are not crawlable 200 213 function easywpstuff_assesplus_addHashToBlankLinks($html) { 201 $dom = new \DiDom\Document($html); 214 $dom = lhb_str_get_html($html, false, true, 'UTF-8', false, PHP_EOL, ' '); 215 if (empty($dom)) { 216 return $dom; 217 } 202 218 $blankLinks = $dom->find('a'); 203 219 foreach ($blankLinks as $link) { 204 $href = $link->getAttribute('href'); 205 if (!$href || $href === '') { 206 $link->setAttribute('href', '#'); 207 } 208 if ($href === 'javascript: void(0);' || $href === 'javascript:void(0);') { 209 $link->setAttribute('href', '#'); 210 } 211 } 212 213 return $dom->html(); 220 if (!$link->href || $link->href === '') { 221 $link->setattribute('href', '#'); 222 } 223 if (!$link->href || $link->href === 'javascript: void(0);') { 224 $link->setattribute('href', '#'); 225 } 226 if (!$link->href || $link->href === 'javascript:void(0);') { 227 $link->setattribute('href', '#'); 228 } 229 } 230 $html = $dom->save(); 231 return $html; 214 232 } 215 233 216 234 //ARIA progressbar elements do not have accessible names. 217 235 function easywpstuff_assesplus_add_aria_label_to_progressbar($html) { 218 $dom = new \DiDom\Document($html); 219 foreach ($dom->find('[role=progressbar]') as $element) { 236 // Use Simple HTML DOM to parse the HTML 237 $dom = lhb_str_get_html($html, false, true, 'UTF-8', false, PHP_EOL, ' '); 238 if (empty($dom)) { 239 return $dom; 240 } 241 foreach($dom->find('[role=progressbar]') as $element) { 242 // Check if the element has aria-label, aria-labelledby or title attribute 220 243 $has_label = $element->hasAttribute('aria-label') || 221 244 $element->hasAttribute('aria-labelledby') || 222 245 $element->hasAttribute('title'); 223 246 // If the element doesn't have a label, add aria-label="progressbar" 224 247 if (!$has_label || (empty($element->getAttribute('aria-label')) && empty($element->getAttribute('aria-labelledby')) && empty($element->getAttribute('title')))) { 225 248 $element->setAttribute('aria-label', 'progressbar'); 226 249 } 227 250 } 228 229 return $dom->html(); 251 // Return the modified HTML 252 $html = $dom->save(); 253 return $html; 230 254 } 231 255 //Image elements do not have [alt] attributes 232 256 function easywpstuff_assesplus_addAltToImg($html) { 233 $dom = new \DiDom\Document($html); 257 $dom = lhb_str_get_html($html, false, true, 'UTF-8', false, PHP_EOL, ' '); 258 if (empty($dom)) { 259 return $dom; 260 } 234 261 $imgs = $dom->find('img'); 235 262 foreach ($imgs as $img) { 236 if (!$img-> getAttribute('alt')) {263 if (!$img->alt) { 237 264 $alt = ''; 238 if ($img-> hasAttribute('title')) {239 $alt = $img-> getAttribute('title');265 if ($img->title) { 266 $alt = $img->title; 240 267 } else { 241 $src = $img-> getAttribute('src');268 $src = $img->src; 242 269 $alt = basename($src); 243 270 $alt = preg_replace('/\d+/', '', $alt); 244 271 $alt = preg_replace('/\.[^.]+$/', '', $alt); 245 272 $alt = str_replace('-', ' ', $alt); 246 $alt = str_replace('_', ' ', $alt);273 $alt = str_replace('_', ' ', $alt); 247 274 } 248 $img->set Attribute('alt', $alt);249 } 250 } 251 return $dom->html();252 } 253 275 $img->setattribute('alt', $alt); 276 } 277 } 278 $html = $dom->save(); 279 return $html; 280 } 254 281 255 282 // buffer if option is enabled … … 296 323 return $buffer; 297 324 } 298 299 325 //add_action( 'template_redirect', 'easywpstuff_assesplus_bstr_template_redirect', 99 ); 300 326 -
accessibility-plus/trunk/readme.txt
r2949281 r2951458 3 3 Tags: pagespeed, lighthouse, accessibility, core web vitals 4 4 Requires at least: 5.0 5 Tested up to: 6. 2.25 Tested up to: 6.3 6 6 Requires PHP: 5.6 7 Stable tag: 1.2 7 Stable tag: 1.2.1 8 8 License: GNU General Public License v2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 52 52 53 53 == Changelog == 54 = 1.2.1 = 55 * Minor Fixes 54 56 = 1.2 = 55 57 * Speedup DOM and Fixed Fatal error
Note: See TracChangeset
for help on using the changeset viewer.