Smilia: children's dental clinic website (WordPress theme and plugin)
This commit is contained in:
commit
4f4b5f2c0a
266 files changed
+16459
No files matched your search
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
namespace Smilia\Core;
|
||||
|
||||
use WP_Post;
|
||||
use WP_Query;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
final class Content {
|
||||
|
||||
private const CACHE_PREFIX = 'smilia_content_';
|
||||
private const CACHE_TTL = DAY_IN_SECONDS;
|
||||
private const VERSION = 'smilia_content_version';
|
||||
|
||||
private const TRACKED = array(
|
||||
Post_Types::SERVICE,
|
||||
Post_Types::DOCTOR,
|
||||
Post_Types::REVIEW,
|
||||
Post_Types::FAQ,
|
||||
);
|
||||
|
||||
public function register(): void {
|
||||
add_action( 'save_post', array( $this, 'flush_for_post' ), 20, 2 );
|
||||
add_action( 'deleted_post', array( $this, 'flush_for_post' ), 20, 2 );
|
||||
add_action( 'set_object_terms', array( self::class, 'flush_all' ) );
|
||||
add_action( 'update_option_' . Settings::OPTION, array( self::class, 'flush_all' ) );
|
||||
}
|
||||
|
||||
public function flush_for_post( int $post_id, $post = null ): void {
|
||||
$type = $post instanceof WP_Post ? $post->post_type : get_post_type( $post_id );
|
||||
if ( in_array( $type, self::TRACKED, true ) ) {
|
||||
self::flush_all();
|
||||
}
|
||||
}
|
||||
|
||||
public static function flush_all(): void {
|
||||
update_option( self::VERSION, (string) microtime( true ), true );
|
||||
}
|
||||
|
||||
private static function language(): ?string {
|
||||
if ( ! function_exists( 'pll_current_language' ) ) {
|
||||
return get_locale();
|
||||
}
|
||||
$language = pll_current_language();
|
||||
if ( ! $language && function_exists( 'pll_get_post_language' ) && get_post() ) {
|
||||
$language = pll_get_post_language( get_post()->ID );
|
||||
}
|
||||
return $language ? $language : null;
|
||||
}
|
||||
|
||||
private static function key( string $name, string $language ): string {
|
||||
return self::CACHE_PREFIX . md5( $name . '|' . get_option( self::VERSION, '0' ) . '|' . $language );
|
||||
}
|
||||
|
||||
private static function service_items(): array {
|
||||
return self::remember(
|
||||
'services',
|
||||
static function (): array {
|
||||
return array_map(
|
||||
static fn( WP_Post $post ): array => array(
|
||||
'id' => $post->ID,
|
||||
'slug' => $post->post_name,
|
||||
'title' => get_the_title( $post ),
|
||||
'url' => get_permalink( $post ),
|
||||
'summary' => get_the_excerpt( $post ),
|
||||
'image_id' => (int) get_post_thumbnail_id( $post ),
|
||||
'tone' => (string) get_post_meta( $post->ID, Meta_Fields::TONE, true ),
|
||||
),
|
||||
self::posts( Post_Types::SERVICE )
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public static function services(): array {
|
||||
return self::with_images( self::service_items() );
|
||||
}
|
||||
|
||||
public static function service_urls(): array {
|
||||
return array_column( self::service_items(), 'url', 'slug' );
|
||||
}
|
||||
|
||||
public static function doctors(): array {
|
||||
$items = self::remember(
|
||||
'doctors',
|
||||
static function (): array {
|
||||
return array_map(
|
||||
static fn( WP_Post $post ): array => array(
|
||||
'id' => $post->ID,
|
||||
'name' => get_the_title( $post ),
|
||||
'url' => get_permalink( $post ),
|
||||
'position' => (string) get_post_meta( $post->ID, Meta_Fields::POSITION, true ),
|
||||
'image_id' => (int) get_post_thumbnail_id( $post ),
|
||||
'tone' => (string) get_post_meta( $post->ID, Meta_Fields::TONE, true ),
|
||||
),
|
||||
self::posts( Post_Types::DOCTOR )
|
||||
);
|
||||
}
|
||||
);
|
||||
return self::with_images( $items );
|
||||
}
|
||||
|
||||
public static function reviews( int $limit = 3 ): array {
|
||||
$all = self::remember(
|
||||
'reviews',
|
||||
static function (): array {
|
||||
return array_map(
|
||||
static fn( WP_Post $post ): array => array(
|
||||
'id' => $post->ID,
|
||||
'author' => get_the_title( $post ),
|
||||
'text' => wp_strip_all_tags( $post->post_content ),
|
||||
'topic' => (string) get_post_meta( $post->ID, Meta_Fields::TOPIC, true ),
|
||||
'source' => (string) get_post_meta( $post->ID, Meta_Fields::SOURCE, true ),
|
||||
),
|
||||
self::posts( Post_Types::REVIEW )
|
||||
);
|
||||
}
|
||||
);
|
||||
return array_slice( $all, 0, $limit );
|
||||
}
|
||||
|
||||
public static function faq( string $group ): array {
|
||||
$group = sanitize_key( $group );
|
||||
return self::remember(
|
||||
'faq_' . $group,
|
||||
static function () use ( $group ): array {
|
||||
return array_map(
|
||||
static fn( WP_Post $post ): array => array(
|
||||
'id' => $post->ID,
|
||||
'question' => get_the_title( $post ),
|
||||
'answer' => apply_filters( 'the_content', $post->post_content ),
|
||||
),
|
||||
self::posts(
|
||||
Post_Types::FAQ,
|
||||
array(
|
||||
'tax_query' => array(
|
||||
array(
|
||||
'taxonomy' => Post_Types::FAQ_GROUP,
|
||||
'field' => 'slug',
|
||||
'terms' => $group,
|
||||
),
|
||||
),
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private static function posts( string $post_type, array $extra = array() ): array {
|
||||
$language = function_exists( 'pll_current_language' ) ? self::language() : null;
|
||||
if ( $language ) {
|
||||
$extra['lang'] = $language;
|
||||
}
|
||||
$query = new WP_Query(
|
||||
array_merge(
|
||||
array(
|
||||
'post_type' => $post_type,
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => 50,
|
||||
'orderby' => array(
|
||||
'menu_order' => 'ASC',
|
||||
'date' => 'DESC',
|
||||
),
|
||||
'no_found_rows' => true,
|
||||
'update_post_term_cache' => false,
|
||||
),
|
||||
$extra
|
||||
)
|
||||
);
|
||||
return $query->posts;
|
||||
}
|
||||
|
||||
private static function with_images( array $items ): array {
|
||||
$ids = array_filter( array_column( $items, 'image_id' ) );
|
||||
if ( $ids ) {
|
||||
_prime_post_caches( $ids, false, true );
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
private static function remember( string $key, callable $build ): array {
|
||||
$language = self::language();
|
||||
if ( null === $language ) {
|
||||
return $build();
|
||||
}
|
||||
$cache_key = self::key( $key, $language );
|
||||
$cached = get_transient( $cache_key );
|
||||
if ( is_array( $cached ) ) {
|
||||
return $cached;
|
||||
}
|
||||
$value = $build();
|
||||
set_transient( $cache_key, $value, self::CACHE_TTL );
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user