
$upload_dir = wp_upload_dir(); define('CACHED_TEMPLATE', trailingslashit($upload_dir['basedir']) . 'cached_template/'); /** * Кеширующая обертка для get_template_part() * * @param string $template Путь к шаблону * @param string $name Имя шаблона (необязательно) * @param array $data Данные для передачи в шаблон * @param int $expire Время жизни кеша в секундах * @return void */ function cached_get_template_part( $template, $name = null, $data = [] ) { $fcache = CACHED_TEMPLATE.$template.'.html'; if ( file_exists($fcache) ){ echo file_get_contents($fcache); return true; } $file_directory = dirname($fcache); ob_start(); get_template_part($template, $name, $data ); $content = ob_get_clean(); echo $content; if (!file_exists($file_directory)) { if (!wp_mkdir_p($file_directory)) { error_log('Не удалось создать директорию: ' . $file_directory); return false; } } if (false === file_put_contents($fcache, $content)) { error_log('Не удалось сохранить файл: ' . $fcache); return false; } return true; } add_action('save_post', function($post_id, $post, $update) { // Проверки безопасности if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; if (wp_is_post_revision($post_id)) return; if (!current_user_can('edit_post', $post_id)) return; if ( $post->post_type = 'profile_article' ){ unlink( CACHED_TEMPLATE.'template-parts/home/list-items.html' ); } }, 10, 3); ?>