Files
profile/inc/replace_old_copyright.php

75 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2026-01-30 12:55:59 +03:00
<?php
/**
* Фильтр для удаления заглавных изображений с влиянием на OpenGraph
*/
// Глобальный массив для хранения ID постов где удалили изображение
$GLOBALS['removed_featured_images'] = array();
/**
* Основной фильтр - удаление изображения
*/
add_filter('post_thumbnail_id', 'filter_featured_image_if_old_and_copyright', 999, 2);
function filter_featured_image_if_old_and_copyright($thumb_id, $post) {
// Быстрый выход если нет ID
if (!$thumb_id) {
return $thumb_id;
}
// Определяем ID поста
$post_id = is_object($post) ? $post->ID : (is_numeric($post) ? $post : 0);
if (!$post_id) return $thumb_id;
// Проверяем тип записи
$post_type = get_post_type($post_id);
if (!in_array($post_type, array('profile_article', 'anew', 'yellow'))) {
return $thumb_id;
}
// Получаем дату загрузки изображения
$attachment = get_post($thumb_id);
if (!$attachment) return $thumb_id;
// Проверяем дату (изображения до 2024 года)
if (strtotime($attachment->post_date) >= strtotime('2024-01-01')) {
return $thumb_id;
}
// Проверяем на наличие копирайтов
$text = strtolower($attachment->post_excerpt . ' ' .
$attachment->post_content . ' ' .
$attachment->post_title);
if (stripos($text, 'shutterstock') !== false ||
stripos($text, 'fotodom') !== false) {
// Запоминаем что для этого поста удалили изображение
$GLOBALS['removed_featured_images'][$post_id] = true;
// Возвращаем false - изображение удалено
return false;
}
return $thumb_id;
}
/**
* 4. Фильтр для Rank Math OpenGraph
*/
add_filter('rank_math/opengraph/facebook/image', 'filter_rankmath_opengraph', 10, 1);
add_filter('rank_math/opengraph/twitter/image', 'filter_rankmath_opengraph', 10, 1);
function filter_rankmath_opengraph($image_url) {
global $post;
if (!is_object($post) || !isset($post->ID)) {
return $image_url;
}
if (isset($GLOBALS['removed_featured_images'][$post->ID])) {
return ''; // Удаляем изображение
}
return $image_url;
}