array( 'class' => true, 'id' => true, 'style' => true, 'role' => true, 'aria-label' => true, 'data-*' => true, ), 'p' => array( 'class' => true, ), ); } /** * Render the widget container and enqueue its script. * * @param string $token Token, embed code or empty for the default widget. * @param array $args Optional. `class` (extra CSS classes), `attributes` (extra container attributes). * @return string HTML. */ public static function render( $token = '', $args = array() ) { $token = self::normalize_token( $token ); if ( '' === $token ) { $token = self::get_default_token(); } if ( '' === $token ) { if ( current_user_can( 'edit_posts' ) ) { return '

' . esc_html__( 'SterneWidget: No widget token set. Add a token to the block or shortcode, or set a default widget under Settings → SterneWidget.', 'sternewidget' ) . '

'; } return ''; } self::enqueue_script( $token ); $args = wp_parse_args( $args, array( 'class' => '', 'attributes' => array(), ) ); $classes = array( 'sternewidget' ); foreach ( preg_split( '/\s+/', (string) $args['class'], -1, PREG_SPLIT_NO_EMPTY ) as $class ) { $class = sanitize_html_class( $class ); if ( '' !== $class ) { $classes[] = $class; } } $attributes = array_merge( array( 'class' => implode( ' ', array_unique( $classes ) ), 'data-bui' => $token, ), is_array( $args['attributes'] ) ? $args['attributes'] : array() ); /** * Filters the container attributes. * * @param array $attributes Attribute name => value. * @param string $token Widget token. */ $attributes = apply_filters( 'sternewidget_container_attributes', $attributes, $token ); $html_attributes = ''; foreach ( $attributes as $name => $value ) { $html_attributes .= ' ' . esc_attr( $name ) . '="' . esc_attr( $value ) . '"'; } $html = ''; /** * Filters the final widget HTML. * * @param string $html Container HTML. * @param string $token Widget token. */ return (string) apply_filters( 'sternewidget_render_output', $html, $token ); } /** * Register and enqueue the widget script for a token (idempotent). * * @param string $token Normalized token. * @return void */ public static function enqueue_script( $token ) { $handle = self::HANDLE_PREFIX . $token; if ( ! wp_script_is( $handle, 'registered' ) ) { wp_register_script( $handle, add_query_arg( 'bui', rawurlencode( $token ), self::script_base_url() ), array(), null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- No ?ver= on a third-party URL. array( 'strategy' => 'async', 'in_footer' => true, ) ); } wp_enqueue_script( $handle ); } /** * Add crossorigin and optimizer-exclusion attributes to our own script tags. * * @param string $tag Script tag. * @param string $handle Script handle. * @return string */ public static function filter_script_tag( $tag, $handle ) { if ( 0 !== strpos( (string) $handle, self::HANDLE_PREFIX ) ) { return $tag; } return str_replace( '