Files
smilia/wp-content/plugins/smilia-core/includes/class-markup.php
T

115 lines
3.7 KiB
PHP

<?php
namespace Smilia\Core;
use WP_Post;
defined( 'ABSPATH' ) || exit;
final class Markup {
public static function text( $value ): string {
return esc_html( wp_strip_all_tags( (string) $value ) );
}
public static function link( $value ): string {
return esc_url( self::url( (string) $value ) );
}
public static function url( string $value ): string {
$value = trim( $value );
if ( ! str_starts_with( $value, '/' ) ) {
return $value;
}
$home = function_exists( 'pll_home_url' ) ? pll_home_url() : home_url( '/' );
return rtrim( $home, '/' ) . $value;
}
private static function can_edit( int $post_id, array $attributes ): bool {
return 'all' !== ( $attributes['part'] ?? 'all' ) && current_user_can( 'edit_post', $post_id );
}
public static function edit_link( int $post_id, array $attributes ): string {
if ( ! self::can_edit( $post_id, $attributes ) ) {
return '';
}
return sprintf( ' data-edit-url="%s"', esc_url( (string) get_edit_post_link( $post_id, 'raw' ) ) );
}
public static function edit_button( int $post_id, array $attributes ): void {
if ( 'all' === ( $attributes['part'] ?? 'all' ) ) {
edit_post_link( __( 'Изменить', 'smilia-core' ), '', '', $post_id, 'edit-badge' );
return;
}
if ( ! self::can_edit( $post_id, $attributes ) ) {
return;
}
printf(
'<span class="edit-badge" role="button" title="%s">%s</span>',
esc_attr( sprintf( __( 'Изменить: %s', 'smilia-core' ), get_the_title( $post_id ) ) ),
esc_html__( 'Изменить', 'smilia-core' )
);
}
public static function trail( ?WP_Post $post = null ): array {
$post = $post ?? get_post();
if ( ! $post instanceof WP_Post ) {
return array();
}
$home = function_exists( 'pll_home_url' ) ? pll_home_url() : home_url( '/' );
$trail = array(
array(
'title' => __( 'Home' ),
'url' => $home,
),
);
if ( (int) get_option( 'page_on_front' ) === $post->ID || ( function_exists( 'pll_get_post' ) && (int) pll_get_post( (int) get_option( 'page_on_front' ) ) === $post->ID ) ) {
return $trail;
}
foreach ( array_reverse( get_post_ancestors( $post ) ) as $ancestor ) {
$trail[] = array(
'title' => get_the_title( $ancestor ),
'url' => get_permalink( $ancestor ),
);
}
$trail[] = array(
'title' => get_the_title( $post ),
'url' => get_permalink( $post ),
);
return $trail;
}
public static function wave(): void {
echo '<svg class="wave" viewBox="0 0 1440 130" preserveAspectRatio="none" aria-hidden="true" focusable="false"><path class="wave__back" d="M0,78 C240,18 480,14 740,52 C1000,90 1220,96 1440,48 L1440,130 L0,130 Z"/><path class="wave__middle" d="M0,96 C260,42 520,40 770,72 C1020,104 1240,108 1440,70 L1440,130 L0,130 Z"/><path class="wave__front" d="M0,114 C280,70 560,68 800,92 C1040,116 1260,118 1440,94 L1440,130 L0,130 Z"/></svg>';
}
public static function asset( string $path ): string {
$url = plugins_url( 'assets/img/' . $path, SMILIA_CORE_FILE );
$file = SMILIA_CORE_DIR . '/assets/img/' . $path;
return is_readable( $file ) ? add_query_arg( 'ver', (string) filemtime( $file ), $url ) : $url;
}
public static function asset_svg( string $path, string $class_name = '', string $alt = '', string $loading = 'lazy' ): void {
Picture::svg( SMILIA_CORE_DIR . '/assets/img/' . $path, self::asset( $path ), $class_name, $alt, $loading );
}
public static function image( int $attachment_id, string $size, string $sizes, array $attr = array() ): void {
if ( $attachment_id <= 0 ) {
return;
}
echo wp_get_attachment_image(
$attachment_id,
$size,
false,
array_merge(
array(
'sizes' => $sizes,
'decoding' => 'async',
'loading' => 'lazy',
),
$attr
)
);
}
}