; and without
* JavaScript the images are still visible and linked to the full-size file.
*/
defined( 'ABSPATH' ) || exit;
class EquaFrame_Renderer {
/** @var int Instances rendered on this request, used to keep element IDs unique. */
private static $instances = 0;
/** @var array Custom image IDs already printed on this page (IDs must be unique). */
private static $used_ids = array();
/** @var array Gallery IDs whose CSS has already been added to the page. */
private static $styled = array();
public static function init() {
add_shortcode( 'equaframe', array( __CLASS__, 'shortcode' ) );
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_assets' ) );
}
public static function register_assets() {
wp_register_style( 'eqfr-frontend', EQUAFRAME_URL . 'assets/css/frontend.css', array(), EQUAFRAME_VERSION );
wp_register_script( 'eqfr-frontend', EQUAFRAME_URL . 'assets/js/frontend.js', array(), EQUAFRAME_VERSION, array( 'in_footer' => true ) );
wp_add_inline_script(
'eqfr-frontend',
'window.eqfrI18n=' . wp_json_encode(
array(
/* translators: 1: current image number, 2: total number of images. Keep the {current} and {total} placeholders. */
'imageOf' => __( 'Image {current} of {total}', 'equaframe' ),
)
) . ';',
'before'
);
// Galleries that are in the main post content get their stylesheet and settings CSS in
,
// so the layout never jumps. Galleries added any other way (widgets, page builders) enqueue
// the same styles while rendering and print in the footer instead.
$post = is_singular() ? get_post() : null;
if ( $post && has_shortcode( $post->post_content, 'equaframe' ) ) {
wp_enqueue_style( 'eqfr-frontend' );
wp_register_style( 'eqfr-gallery-head', false, array( 'eqfr-frontend' ), EQUAFRAME_VERSION );
wp_enqueue_style( 'eqfr-gallery-head' );
foreach ( self::shortcode_gallery_ids( $post->post_content ) as $gallery_id ) {
$gallery = EquaFrame_Gallery::get( $gallery_id );
if ( $gallery && 'publish' === $gallery->post_status ) {
self::add_gallery_css( $gallery->ID, EquaFrame_Gallery::get_settings( $gallery->ID ), 'eqfr-gallery-head' );
}
}
}
}
/**
* Gallery IDs used by [equaframe] shortcodes in a piece of content.
*
* @param string $content Post content.
* @return int[]
*/
private static function shortcode_gallery_ids( $content ) {
$ids = array();
if ( preg_match_all( '/' . get_shortcode_regex( array( 'equaframe' ) ) . '/', $content, $matches, PREG_SET_ORDER ) ) {
foreach ( $matches as $match ) {
$atts = shortcode_parse_atts( $match[3] );
if ( ! empty( $atts['id'] ) ) {
$ids[] = (int) $atts['id'];
}
}
}
return array_unique( array_filter( $ids ) );
}
/**
* Add one gallery's settings CSS to a style handle, once per page.
* The CSS is built from sanitized values only (see EquaFrame_Settings::sanitize).
*
* @param int $gallery_id Gallery ID.
* @param array $settings Sanitized settings.
* @param string $handle Registered style handle.
*/
private static function add_gallery_css( $gallery_id, $settings, $handle ) {
if ( isset( self::$styled[ $gallery_id ] ) ) {
return;
}
self::$styled[ $gallery_id ] = true;
wp_add_inline_style( $handle, EquaFrame_CSS::build( $gallery_id, $settings ) );
}
public static function shortcode( $atts ) {
$atts = shortcode_atts(
array(
'id' => 0,
'class' => '',
),
$atts,
'equaframe'
);
$post = EquaFrame_Gallery::get( (int) $atts['id'] );
if ( ! $post || 'publish' !== $post->post_status ) {
return current_user_can( 'edit_posts' )
? '
' . esc_html__( 'EquaFrame: that gallery could not be found.', 'equaframe' ) . '
'
: '';
}
return self::render( $post, $atts['class'] );
}
/**
* Build the gallery markup.
*
* @param WP_Post $post Gallery post.
* @param string $extra_class Extra wrapper class(es) from the shortcode.
*/
public static function render( $post, $extra_class = '' ) {
$settings = EquaFrame_Gallery::get_settings( $post->ID );
$images = EquaFrame_Gallery::get_images( $post->ID );
if ( ! $images ) {
return current_user_can( 'edit_posts' )
? '
' . esc_html__( 'EquaFrame: this gallery has no images yet.', 'equaframe' ) . '
'; // viewport.
// Controls are hidden by CSS until JavaScript has made them work.
if ( $total > 1 && $settings['ss_arrows'] ) {
$stage .= '';
$stage .= '';
}
$stage .= '