add new adv plugin and suppoer webp
This commit is contained in:
@@ -1180,5 +1180,79 @@ function restrict_author_publish_posts( $data ) {
|
||||
}
|
||||
add_filter( 'wp_insert_post_data', 'restrict_author_publish_posts' );
|
||||
|
||||
function replace_first_figure_in_content($content) {
|
||||
|
||||
global $post;
|
||||
|
||||
if (!isset($post->ID)) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
$startPos = strpos($content, '<figure');
|
||||
if ($startPos === false) {
|
||||
return $content; // Если <figure> не найдено, возвращаем исходный контент
|
||||
}
|
||||
|
||||
$endPos = strpos($content, '</figure>', $startPos);
|
||||
if ($endPos === false) {
|
||||
return $content; // Если </figure> не найдено, возвращаем исходный контент
|
||||
}
|
||||
|
||||
// Рассчитываем длину части, которую нужно заменить
|
||||
$endPos += strlen('</figure>'); // Двигаем указатель на конец тега </figure>
|
||||
$figureHtml = substr($content, $startPos, $endPos - $startPos);
|
||||
|
||||
|
||||
// Генерируем HTML изображения
|
||||
$replacement = render_webp_picture_by_post($post->ID);
|
||||
|
||||
// Ищем <figcaption> внутри исходного фрагмента
|
||||
$caption = '';
|
||||
if (preg_match('/<figcaption.*?>(.*?)<\/figcaption>/is', $content, $caption_match)) {
|
||||
$caption = '<div class="image-caption">'.$caption_match[1].'</div>'; // только содержимое
|
||||
}
|
||||
|
||||
$replacement .= $caption;
|
||||
|
||||
// Заменяем найденный кусок на новый HTML
|
||||
$newContent = substr_replace($content, $replacement, $startPos, $endPos - $startPos);
|
||||
|
||||
|
||||
return $newContent;
|
||||
|
||||
|
||||
// Ищем первое <figure>...</figure>
|
||||
if (preg_match('/<figure.*?>.*?<\/figure>/is', $content, $match)) {
|
||||
$original_figure = $match[0];
|
||||
|
||||
// Извлекаем <figcaption> (если есть)
|
||||
$caption = '';
|
||||
if (preg_match('/<figcaption.*?>(.*?)<\/figcaption>/is', $original_figure, $caption_match)) {
|
||||
$caption = $caption_match[1]; // только содержимое
|
||||
}
|
||||
|
||||
// Генерируем HTML изображения
|
||||
$replacement = render_webp_picture_by_post($post->ID);
|
||||
|
||||
// Добавляем подпись, если есть
|
||||
if (!empty($caption)) {
|
||||
$replacement .= '<div class="image-caption">' . esc_html($caption) . '</div>';
|
||||
}
|
||||
|
||||
// Заменяем первое вхождение <figure>...</figure> на $replacement
|
||||
$content = preg_replace('/<figure.*?>.*?<\/figure>/is', $replacement, $content, 1);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user