Files
wordpress-theme-aksrt/tests/widget-system/post-thumbnail-render-harness.php
T

110 lines
5.9 KiB
PHP

<?php
/** Render thumbnail settings and image markup without a WordPress installation. */
declare(strict_types=1);
namespace AKSRT\Settings {
final class Settings {
public static array $values = [];
public static function get_all(): array { return self::$values; }
public static function get_post_list_style_settings(): array { return PostDisplay::get_post_list_style_settings(); }
public static function logo_url(): string { return ''; }
public static function logo_dark_url(): string { return ''; }
public static function favicon_url(): string { return ''; }
public static function theme_color(): string { return '#f04494'; }
public static function dark_theme_color(): string { return '#abcdef'; }
public static function main_radius(): int { return 8; }
public static function container_max_width(): int { return 1200; }
public static function font_family(): string { return 'system'; }
public static function font_family_custom(): string { return ''; }
public static function logo_width(): int { return 150; }
public static function get_sidebar_config(): array { return ['width' => 311]; }
public static function get_header_theme_config(): array { return []; }
public static function background_css(string $key): string { return ''; }
}
}
namespace AKSRT\Core {
final class PostMetaBox {
public static array $meta = [];
public static function get_meta_value(int $id, string $key, $default = '') { return self::$meta[$key] ?? $default; }
}
}
namespace {
define('ABSPATH', dirname(__DIR__, 2) . DIRECTORY_SEPARATOR);
set_error_handler(static function (int $severity, string $message, string $file, int $line): bool {
if (!(error_reporting() & $severity)) return false;
throw new \ErrorException($message, 0, $severity, $file, $line);
});
function __($text, $domain = ''): string { return (string) $text; }
function esc_html($text): string { return htmlspecialchars((string) $text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); }
function esc_attr($text): string { return esc_html($text); }
function esc_url($url): string { return esc_attr($url); }
function esc_url_raw($url): string { return (string) $url; }
function sanitize_key($value): string { return preg_replace('/[^a-z0-9_-]/', '', strtolower((string) $value)); }
function wp_kses_post($html): string { return (string) $html; }
function strip_shortcodes($content): string { return (string) $content; }
function wp_strip_all_tags($content): string { return strip_tags((string) $content); }
function get_post_thumbnail_id($id): int { return $GLOBALS['featured_id']; }
function attachment_url_to_postid($url): int { return 0; }
function wp_get_attachment_image_url($id, $size) {
return $id === 42 ? 'https://example.test/featured-' . $size . '.jpg' : false;
}
function get_post_field($field, $id, $context = ''): string { return $GLOBALS['post_content']; }
function get_the_category($id): array { return []; }
function get_post_format($id = null) { return false; }
function aksrt_get_lazy_thumb($size = ''): string { return 'https://example.test/placeholder.svg'; }
function aksrt_is_lazy($key): bool { return $key === 'lazy_sider'; }
function aksrt_get_lazy_attr($key, $src, $class = ''): string {
if (aksrt_is_lazy($key)) {
return ' class="lazyload ' . esc_attr($class) . '" src="' . aksrt_get_lazy_thumb() . '" data-src="' . esc_url($src) . '"';
}
return ' class="' . esc_attr($class) . '" src="' . esc_url($src) . '"';
}
require ABSPATH . 'src/Settings/PostDisplay.php';
require ABSPATH . 'src/Frontend/InlineStyleBuilder.php';
require ABSPATH . 'inc/helpers/posts-renderer/config.php';
require ABSPATH . 'inc/helpers/posts-renderer/post-data.php';
$styles = [];
foreach (['defaults' => [], 'custom' => ['list_list_option' => ['scale' => 100], 'list_card_option' => ['scale' => 56]], 'bounds' => ['list_list_option' => ['scale' => 1], 'list_card_option' => ['scale' => 900]]] as $name => $values) {
\AKSRT\Settings\Settings::$values = $values;
ob_start();
(new \AKSRT\Frontend\InlineStyleBuilder())->output();
$styles[$name] = (string) ob_get_clean();
}
$GLOBALS['featured_id'] = 42;
$GLOBALS['post_content'] = '<p><img src="https://example.test/content.jpg"></p>';
\AKSRT\Core\PostMetaBox::$meta = ['thumbnail_url' => 'https://example.test/external.jpg'];
$cfg = ['thumb' => ['thumb_postfirstimg_size' => 'large', 'thumb_postfirstimg_s' => true, 'spare_thumbnail' => ['https://example.test/spare.jpg']]];
$featured = aksrt_renderer_resolve_images(1, $cfg);
$GLOBALS['featured_id'] = 999;
$external = aksrt_renderer_resolve_images(1, $cfg);
\AKSRT\Core\PostMetaBox::$meta = [];
$content = aksrt_renderer_resolve_images(1, $cfg);
$GLOBALS['post_content'] = '';
$spare = aksrt_renderer_resolve_images(1, $cfg);
\AKSRT\Core\PostMetaBox::$meta = ['featured_slide' => '999,42'];
$slides = aksrt_renderer_curated_images(1, 'medium');
$data = [
'post_id' => 1, 'primary' => $featured['primary'], 'has_real' => true,
'permalink' => 'https://example.test/post/', 'title' => 'Featured image',
'video_url' => '', 'slides' => [], 'gallery_count' => 0,
];
$cover = aksrt_renderer_thumb($data, ['thumb' => ['list_thumb_fit' => '']]);
$contain = aksrt_renderer_thumb($data, ['thumb' => ['list_thumb_fit' => 'contain']]);
$data['is_slide'] = true;
$data['slides'] = ['https://example.test/slide.jpg'];
$single = aksrt_renderer_thumb($data, ['thumb' => []]);
\AKSRT\Core\PostMetaBox::$meta = ['featured_slide' => implode(',', array_fill(0, 12, '42'))];
$data['slides'] = aksrt_renderer_curated_images(1, 'large');
$multiple = aksrt_renderer_thumb($data, ['thumb' => ['list_thumb_fit' => 'contain']]);
$data['video_url'] = 'https://example.test/movie.mp4';
$video = aksrt_renderer_thumb($data, ['thumb' => ['list_thumb_video_s' => true, 'list_thumb_video_mute_s' => true]]);
echo json_encode(compact('styles', 'featured', 'external', 'content', 'spare', 'slides', 'cover', 'contain', 'single', 'multiple', 'video'), JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
}