Plugin Directory

Changeset 2951458


Ignore:
Timestamp:
08/10/2023 11:36:38 AM (3 years ago)
Author:
easywpstuff
Message:

update

Location:
accessibility-plus
Files:
9 added
3 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • accessibility-plus/trunk/accessibility.php

    r2949281 r2951458  
    44Plugin URI:
    55Description: Improve pagespeed insight or lighthouse accessibility scores by fixing recommended issues.
    6 Version: 1.2
     6Version: 1.2.1
    77Author: easywpstuff
    88Author URI: https://easywpstuff.com
     
    1010*/
    1111
    12 
    13 require_once('vendor/autoload.php');
     12// Load the SimpleHTMLDOM library
     13require_once('inc/simple_html_dom.php');
    1414
    1515// include option
     
    2424  }
    2525
    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  }
    2730  $inputs = $dom->find('input');
    28 
     31   
    2932  foreach ($inputs as $input) {
    3033    $parent = $input->parent();
    3134    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')) {
    3336        $inputType = $input->getAttribute('type');
    3437        if ($inputType && !in_array($inputType, ['hidden', 'button', 'submit'])) {
     
    3841    }
    3942  }
    40 
    41   return $dom->html();
     43  return $dom->save();
    4244}
    4345
    4446// Links do not have a discernible name
    4547function 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();
    107105}
    108106
     
    111109
    112110function 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  }
    114116  $buttons = $dom->find('button');
    115117
    116118  foreach ($buttons as $button) {
    117     if (!trim($button->text()) && !$button->hasAttribute('aria-label')) {
     119    if (!trim($button->plaintext) && !$button->getattribute('aria-label')) {
    118120      $child_elements = $button->find('*');
    119121      if (empty($child_elements)) {
    120         $button->setAttribute('aria-label', 'button');
     122        $button->setattribute('aria-label', 'button');
    121123      } 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();
    142142}
    143143
    144144//[user-scalable="no"] is used in the <meta name="viewport"> element or the [maximum-scale] attribute is less than 5.
    145145function 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);
    149152  if ($viewport_meta_tag) {
    150153    $content = $viewport_meta_tag->getAttribute('content');
    151    
     154     
    152155    $content = preg_replace('/,\s*user-scalable=(?:no|0)/', '', $content);
    153156
    154157    if (preg_match('/maximum-scale=([^,\s]*)/', $content, $matches)) {
     158
    155159      $maximum_scale = (float) $matches[1];
    156160      if ($maximum_scale < 2) {
     
    163167    $viewport_meta_tag->setAttribute('content', $content);
    164168  }
    165 
    166   return $dom->html();
    167 }
     169  return $dom->save();
     170}
     171
    168172
    169173function 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')) {
    174181            $div->setAttribute('aria-label', 'link');
    175182        }
    176         if ($role === 'button' && !$div->hasAttribute('aria-label')) {
     183        if ($div->role == 'button' && !$div->hasAttribute('aria-label')) {
    177184            $div->setAttribute('aria-label', 'button');
    178185        }
    179         if ($role === 'menuitem' && !$div->hasAttribute('aria-label')) {
     186        if ($div->role == 'menuitem' && !$div->hasAttribute('aria-label')) {
    180187            $div->setAttribute('aria-label', 'menuitem');
    181188        }
    182189    }
    183190
    184     return $dom->html();
     191    $html = $dom->save(); // Save the modified HTML
     192    return $html; // Return the modified HTML
    185193}
    186194
    187195// <frame> or <iframe> elements do not have a title
    188196function 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) {
    191203        if (!$iframe->hasAttribute('title')) {
    192204            $iframe->setAttribute('title', 'iframe');
     
    194206    }
    195207
    196     return $dom->html();
     208    $html = $dom->save();
     209    return $html;
    197210}
    198211
    199212//Links are not crawlable
    200213function 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    }
    202218    $blankLinks = $dom->find('a');
    203219    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;
    214232}
    215233
    216234//ARIA progressbar elements do not have accessible names.
    217235function 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
    220243        $has_label = $element->hasAttribute('aria-label') ||
    221244                     $element->hasAttribute('aria-labelledby') ||
    222245                     $element->hasAttribute('title');
    223 
     246        // If the element doesn't have a label, add aria-label="progressbar"
    224247        if (!$has_label || (empty($element->getAttribute('aria-label')) && empty($element->getAttribute('aria-labelledby')) && empty($element->getAttribute('title')))) {
    225248            $element->setAttribute('aria-label', 'progressbar');
    226249        }
    227250    }
    228 
    229     return $dom->html();
     251    // Return the modified HTML
     252    $html = $dom->save();
     253    return $html;
    230254}
    231255//Image elements do not have [alt] attributes
    232256function 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     }
    234261    $imgs = $dom->find('img');
    235262    foreach ($imgs as $img) {
    236         if (!$img->getAttribute('alt')) {
     263        if (!$img->alt) {
    237264            $alt = '';
    238             if ($img->hasAttribute('title')) {
    239                 $alt = $img->getAttribute('title');
     265            if ($img->title) {
     266                $alt = $img->title;
    240267            } else {
    241                 $src = $img->getAttribute('src');
     268                $src = $img->src;
    242269                $alt = basename($src);
    243270                $alt = preg_replace('/\d+/', '', $alt);
    244271                $alt = preg_replace('/\.[^.]+$/', '', $alt);
    245272                $alt = str_replace('-', ' ', $alt);
    246                 $alt = str_replace('_', ' ', $alt);
     273                $alt = str_replace('_', ' ', $alt);
    247274            }
    248             $img->setAttribute('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}
    254281
    255282// buffer if option is enabled
     
    296323    return $buffer;
    297324}
    298 
    299325//add_action( 'template_redirect', 'easywpstuff_assesplus_bstr_template_redirect', 99 );
    300326
  • accessibility-plus/trunk/readme.txt

    r2949281 r2951458  
    33Tags: pagespeed, lighthouse, accessibility, core web vitals
    44Requires at least: 5.0
    5 Tested up to: 6.2.2
     5Tested up to: 6.3
    66Requires PHP: 5.6
    7 Stable tag: 1.2
     7Stable tag: 1.2.1
    88License: GNU General Public License v2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    5252
    5353== Changelog ==
     54= 1.2.1 =
     55* Minor Fixes
    5456= 1.2 =
    5557* Speedup DOM and Fixed Fatal error
Note: See TracChangeset for help on using the changeset viewer.