-
-
Save lector1982/ed02cb4a1a59551b5252bd368f0b12e8 to your computer and use it in GitHub Desktop.
Плагин altPlus для MODX Evolution. Заполняет отсутствующий/незаполненный атрибут alt в теге img для поля content. На сайте предполагается использование визуального редактора TyniMCE4.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @internal @properties &meatAltBase=предполагаемое содержимое атрибута alt;text;[*pagetitle*] &numImg=добавить нумерацию изображений в alt;list;да,нет;да | |
| * @internal @events OnDocFormSave | |
| * @author Борис Федоров <w-navt@yandex.ru> | |
| */ | |
| if (!isset($meatAltBase)) { | |
| $meatAltBase = '[*pagetitle*]'; // предполагаемое содержимое атрибута alt | |
| } | |
| if (!isset($numImg)) { | |
| $numImg = 'да'; | |
| } | |
| if ($modx->config['tinymce4_element_format'] == 'html') { | |
| $endTag = '>'; // окончание тега img в формате HTML | |
| } else { | |
| $endTag = '/>'; // окончание тега img в формате XHTML | |
| } | |
| include_once(MODX_BASE_PATH.'assets/lib/MODxAPI/modResource.php'); | |
| $doc = new modResource($modx); | |
| $e = $modx->Event; | |
| if (isset($e->params['id']) && $e->params['mode'] == 'upd') { | |
| switch ($e->name) { | |
| case 'OnDocFormSave' : | |
| $doc->edit($e->params['id']); | |
| $content = $doc->get('content'); | |
| if ($content != '') { | |
| $imgMatch = array(); | |
| $images = array(); | |
| // выбираем все картинки | |
| $pattern = '~<img[^>]+>~i'; | |
| preg_match_all($pattern, $content, $imgMatch, PREG_PATTERN_ORDER); | |
| $flag = false; | |
| $i = 1; | |
| $meatAlt = $meatAltBase; | |
| foreach ($imgMatch[0] as $img) { | |
| if ($numImg == 'да') { | |
| $meatAlt = $meatAltBase.' Иллюстрация # '.$i; | |
| $i++; | |
| } | |
| // далее работаем с атрибутом alt | |
| $flag = preg_match('~alt="(.*?)"~u', $img, $m); | |
| // гипотетический случай - пусть будет | |
| if ($flag === false) { | |
| $images[] = $img; | |
| continue; | |
| } | |
| // атрибут alt в теге img нашёлся | |
| if ($flag == 1) { | |
| if ($m[0] == 'alt=""') { | |
| $img = str_replace('alt=""', "alt=\"".$meatAlt."\"", $img); | |
| $images[] = $img; | |
| } else { | |
| $images[] = $img; | |
| } | |
| } | |
| // не нашёлся атрибут alt в теге img | |
| if ($flag == 0) { | |
| $img = str_replace($endTag, " alt=\"".$meatAlt."\" ".$endTag, $img); | |
| $images[] = $img; | |
| } | |
| } // end foreach | |
| // заменяем "старые" теги img на обновлённые | |
| $qImg = count($images); | |
| for ($i=0; $i<$qImg; $i++) { | |
| $content = str_replace($imgMatch[0][$i], $images[$i], $content); | |
| } | |
| $doc->set('content', $content); | |
| $doc->save(false, true); | |
| } | |
| break; | |
| default: | |
| break; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment