111 lines
5.9 KiB
PHP
111 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace EventLabs\LoopBuilder;
|
|
|
|
use BurnEvents\Events;
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
final class Library {
|
|
private const PAGE = 'eventlabs-loop-presets';
|
|
|
|
public static function register(): void {
|
|
add_action( 'admin_menu', [ self::class, 'menu' ] );
|
|
add_action( 'manage_posts_extra_tablenav', [ self::class, 'list_link' ] );
|
|
}
|
|
|
|
private static function url( array $filters = [] ): string {
|
|
return add_query_arg( [ 'post_type' => Events::EVENT, 'page' => self::PAGE ] + $filters, admin_url( 'edit.php' ) );
|
|
}
|
|
|
|
public static function menu(): void {
|
|
$label = __( 'Loop Presets', 'eventlabs-loop-builder' );
|
|
$page = add_submenu_page( 'edit.php?post_type=' . Events::EVENT, $label, $label, Templates::CAPABILITY, self::PAGE, [ self::class, 'render' ] );
|
|
if ( $page ) {
|
|
add_action( 'admin_enqueue_scripts', static function ( string $screen ) use ( $page ): void {
|
|
if ( $page === $screen ) {
|
|
wp_enqueue_style( ASSET . '-library', plugins_url( 'assets/library.css', FILE ), [ ASSET . '-presets' ], VERSION );
|
|
}
|
|
} );
|
|
}
|
|
}
|
|
|
|
public static function list_link( string $position ): void {
|
|
$screen = get_current_screen();
|
|
if ( 'top' === $position && $screen && Templates::POST_TYPE === $screen->post_type && current_user_can( Templates::CAPABILITY ) ) {
|
|
echo '<a class="button" href="' . esc_url( self::url() ) . '">' . esc_html__( 'Browse loop presets', 'eventlabs-loop-builder' ) . '</a>';
|
|
}
|
|
}
|
|
|
|
private static function selected( string $key, array $options ): string {
|
|
$value = isset( $_GET[ $key ] ) && is_string( $_GET[ $key ] ) ? sanitize_key( wp_unslash( $_GET[ $key ] ) ) : '';
|
|
return isset( $options[ $value ] ) ? $value : '';
|
|
}
|
|
|
|
public static function render(): void {
|
|
if ( ! current_user_can( Templates::CAPABILITY ) ) {
|
|
wp_die( esc_html__( 'You cannot view loop presets.', 'eventlabs-loop-builder' ), '', [ 'response' => 403 ] );
|
|
}
|
|
$layouts = Presets::layouts();
|
|
$families = Presets::families();
|
|
$layout = self::selected( 'ell_layout', $layouts );
|
|
$family = self::selected( 'ell_family', $families );
|
|
$presets = array_filter( Presets::catalog(), static fn( array $style ): bool => ( ! $layout || $layout === $style['layout'] ) && ( ! $family || $family === $style['family'] ) );
|
|
?>
|
|
<div class="wrap ell-library">
|
|
<h1><?php esc_html_e( 'Loop Presets', 'eventlabs-loop-builder' ); ?></h1>
|
|
<p><?php esc_html_e( 'Choose a style to create an editable copy in Elementor. You can also select any preset directly in the Event Loop widget.', 'eventlabs-loop-builder' ); ?></p>
|
|
<div class="ell-library-toolbar">
|
|
<form class="ell-library-filters" method="get" action="<?php echo esc_url( admin_url( 'edit.php' ) ); ?>">
|
|
<input type="hidden" name="post_type" value="<?php echo esc_attr( Events::EVENT ); ?>">
|
|
<input type="hidden" name="page" value="<?php echo esc_attr( self::PAGE ); ?>">
|
|
<?php self::select( 'ell_layout', __( 'Layout', 'eventlabs-loop-builder' ), [ '' => __( 'All layouts', 'eventlabs-loop-builder' ) ] + $layouts, $layout ); ?>
|
|
<?php self::select( 'ell_family', __( 'Style', 'eventlabs-loop-builder' ), [ '' => __( 'All styles', 'eventlabs-loop-builder' ) ] + $families, $family ); ?>
|
|
<button class="button" type="submit"><?php esc_html_e( 'Filter presets', 'eventlabs-loop-builder' ); ?></button>
|
|
<?php if ( $layout || $family ) : ?>
|
|
<a href="<?php echo esc_url( self::url() ); ?>"><?php esc_html_e( 'Clear filters', 'eventlabs-loop-builder' ); ?></a>
|
|
<?php endif; ?>
|
|
</form>
|
|
<a class="button" href="<?php echo esc_url( admin_url( 'edit.php?post_type=' . Templates::POST_TYPE ) ); ?>"><?php esc_html_e( 'My loop templates', 'eventlabs-loop-builder' ); ?></a>
|
|
</div>
|
|
<p class="ell-library-count"><?php
|
|
/* translators: %s: number of presets. */
|
|
printf( esc_html( _n( '%s preset', '%s presets', count( $presets ), 'eventlabs-loop-builder' ) ), esc_html( number_format_i18n( count( $presets ) ) ) );
|
|
?></p>
|
|
<div class="ell-library-grid">
|
|
<?php foreach ( $presets as $slug => $style ) : ?>
|
|
<article class="ell-library-item">
|
|
<div class="ell-preset-preview" aria-hidden="true"><?php echo Presets::sample( $slug ); // Preset renderer escapes its content. ?></div>
|
|
<div class="ell-library-details">
|
|
<p class="ell-library-type"><?php echo esc_html( $families[ $style['family'] ] . ' · ' . $layouts[ $style['layout'] ] ); ?></p>
|
|
<h2><?php echo esc_html( $style['title'] ); ?></h2>
|
|
<p class="ell-library-description"><?php echo esc_html( $style['description'] ); ?></p>
|
|
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post">
|
|
<input type="hidden" name="action" value="<?php echo esc_attr( Templates::STARTER_ACTION ); ?>">
|
|
<input type="hidden" name="preset" value="<?php echo esc_attr( $slug ); ?>">
|
|
<input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( Templates::STARTER_ACTION ) ); ?>">
|
|
<button class="button button-primary" type="submit" aria-label="<?php
|
|
/* translators: 1: style family, 2: layout, 3: preset name. */
|
|
printf( esc_attr__( 'Use %1$s %2$s %3$s', 'eventlabs-loop-builder' ), esc_attr( $families[ $style['family'] ] ), esc_attr( $layouts[ $style['layout'] ] ), esc_attr( $style['title'] ) );
|
|
?>"><?php esc_html_e( 'Use preset', 'eventlabs-loop-builder' ); ?></button>
|
|
</form>
|
|
</div>
|
|
</article>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
private static function select( string $name, string $label, array $options, string $current ): void {
|
|
?>
|
|
<label for="<?php echo esc_attr( $name ); ?>"><?php echo esc_html( $label ); ?></label>
|
|
<select id="<?php echo esc_attr( $name ); ?>" name="<?php echo esc_attr( $name ); ?>">
|
|
<?php foreach ( $options as $value => $title ) : ?>
|
|
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $current, $value ); ?>><?php echo esc_html( $title ); ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<?php
|
|
}
|
|
}
|