' . esc_html( $game['title'] ) . '' : ''; } /** * Game iframe inside its sized frame, with the full screen button under it (shown by play.js where the browser * allows full screen). * * With a poster the game waits for the visitor: the poster covers the frame and play.js loads the game on a click. * Without JavaScript the copy inside noscript loads right away. * * @param string $src Iframe address, '' for an empty frame. * @param string $title Accessible title. * @param string $style Frame style from dicegem_frame_style(). * @param string $poster Poster markup from dicegem_poster(), '' to load the game at once. * @return string HTML. */ function dicegem_frame( $src, $title, $style, $poster = '' ) { $attrs = '="' . esc_url( $src ) . '" title="' . esc_attr( $title ) . '" loading="lazy" allow="fullscreen; autoplay" sandbox="allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox" allowfullscreen'; $inner = '' === $poster ? '' : $poster . ''; return '
' . $inner . '
' . '
'; } /** * Poster shown in place of a game until the visitor presses Play. * * @param array $game Clean game. * @return string HTML. */ function dicegem_poster( array $game ) { return sprintf( '', /* translators: %s: game title. */ esc_attr( sprintf( __( 'Play %s', 'dicegem-games' ), $game['title'] ) ), dicegem_cover( $game ), esc_html__( 'Play', 'dicegem-games' ) ); } /** * Iframe address for a game, with the publisher ID when one is set. * * @param array $game Clean game. * @return string URL. */ function dicegem_play_src( array $game ) { $pub_id = dicegem_settings()['pub_id']; return '' === $pub_id ? $game['play_url'] : add_query_arg( 'pub', $pub_id, $game['play_url'] ); } /** * Optional credit line; empty unless the site owner turned it on. * * @param string $href Link target on dicegem.com. * @return string HTML. */ function dicegem_credit( $href ) { if ( ! dicegem_settings()['credit'] ) { return ''; } return '

' . sprintf( /* translators: %s: link to dicegem.com with the text "DiceGem". */ esc_html__( 'Play more free games on %s', 'dicegem-games' ), 'DiceGem' ) . '

'; } /** * Notice under casino-style games. * * @param bool $hidden Whether the note starts hidden (grid player). * @return string HTML. */ function dicegem_casino_note( $hidden = false ) { return '

' . esc_html__( 'Virtual credits only, no real money. 18+', 'dicegem-games' ) . '

'; } /** * A problem shown only to people who can edit posts; visitors see nothing. * * @param string $message Plain-text message. * @return string HTML. */ function dicegem_editor_notice( $message ) { return current_user_can( 'edit_posts' ) ? '

' . esc_html( $message ) . '

' : ''; } /** * Renders [dicegem game="slug"] and the dicegem/game block. * * @param array|string $atts Shortcode or block attributes: game, width, ratio. * @param string $wrapper Attributes of the outer element. * @return string HTML. */ function dicegem_render_game( $atts, $wrapper = 'class="dicegem-game"' ) { $atts = shortcode_atts( array( 'game' => '', 'width' => '', 'ratio' => '', ), $atts, 'dicegem' ); $slug = (string) $atts['game']; $games = dicegem_games(); if ( ! isset( $games[ $slug ] ) ) { $catalog = dicegem_catalog(); return dicegem_editor_notice( isset( $catalog[ $slug ] ) ? __( 'DiceGem: casino-style games are turned off in DiceGem Games > Settings.', 'dicegem-games' ) /* translators: %s: game slug. */ : sprintf( __( 'DiceGem: unknown game "%s".', 'dicegem-games' ), $slug ) ); } $game = $games[ $slug ]; wp_enqueue_style( 'dicegem-games' ); wp_enqueue_script( 'dicegem-games-play' ); return '
' . dicegem_frame( dicegem_play_src( $game ), $game['title'], dicegem_frame_style( $game, $atts['ratio'], $atts['width'] ), dicegem_settings()['click_to_play'] ? dicegem_poster( $game ) : '' ) . ( dicegem_is_casino( $game ) ? dicegem_casino_note() : '' ) . dicegem_credit( $game['url'] ) . '
'; } /** * Renders [dicegem_grid category="board-games" count="6"] and the dicegem/grid block. * * Cards link to the game's page on this site when one exists, otherwise to dicegem.com. * * @param array|string $atts Shortcode or block attributes: category (name or slug, '' for all), count, exclude (slug). * @param string $wrapper Attributes of the outer element. * @return string HTML. */ function dicegem_render_grid( $atts, $wrapper = 'class="dicegem-grid"' ) { $atts = shortcode_atts( array( 'category' => '', 'count' => 12, 'exclude' => '', // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- A shortcode attribute, not a query argument. ), $atts, 'dicegem_grid' ); $category = sanitize_title( $atts['category'] ); $games = dicegem_games(); unset( $games[ (string) $atts['exclude'] ] ); if ( '' !== $category ) { $games = array_filter( $games, function ( $game ) use ( $category ) { return $game['category_slug'] === $category; } ); } $games = array_slice( $games, 0, min( 100, max( 1, absint( $atts['count'] ) ) ) ); if ( ! $games ) { return dicegem_editor_notice( __( 'DiceGem: no games match this grid.', 'dicegem-games' ) ); } wp_enqueue_style( 'dicegem-games' ); wp_enqueue_script( 'dicegem-games-grid' ); wp_enqueue_script( 'dicegem-games-play' ); $links = dicegem_page_links( array_keys( $games ) ); $cards = ''; foreach ( $games as $slug => $game ) { $cards .= sprintf( '
  • %6$s%4$s%7$s
  • ', esc_url( isset( $links[ $slug ] ) ? $links[ $slug ] : $game['url'] ), esc_url( dicegem_play_src( $game ) ), esc_attr( dicegem_frame_style( $game ) ), esc_html( $game['title'] ), dicegem_is_casino( $game ) ? ' data-casino' : '', dicegem_cover( $game ), esc_html( $game['category'] ) ); } return '
    ' . '' . '' . dicegem_credit( 'https://dicegem.com/' ) . '
    '; }