. * * @param string $slug Raw slug. * @return string Clean slug or ''. */ function layers_doc_embed_clean_slug( $slug ) { $slug = strtolower( trim( (string) $slug ) ); return preg_match( '/^[a-z0-9][a-z0-9-]{0,119}$/', $slug ) ? $slug : ''; } /** * The attribution mark Layers counts visits by: embed-. * * @param string $from Optional override from the shortcode. * @return string */ function layers_doc_embed_from_mark( $from = '' ) { $from = strtolower( trim( (string) $from ) ); if ( '' === $from ) { $host = wp_parse_url( home_url(), PHP_URL_HOST ); $host = preg_replace( '/^www\./', '', strtolower( (string) $host ) ); $from = 'embed-' . $host; } $from = preg_replace( '/[^a-z0-9_-]+/', '-', $from ); $from = trim( $from, '-_' ); if ( 0 !== strpos( $from, 'embed-' ) ) { $from = 'embed-' . $from; } return substr( $from, 0, 64 ); } /** * Builds the iframe for a template. * * @param array $atts slug, height, from, title, view. * @param string $wrapper Optional attributes for the outer div, already escaped * (the block passes get_block_wrapper_attributes() so that * alignwide / alignfull and other block supports reach the page). * @return string HTML. */ function layers_doc_embed_render( $atts, $wrapper = '' ) { $slug = layers_doc_embed_clean_slug( isset( $atts['slug'] ) ? $atts['slug'] : '' ); if ( '' === $slug ) { if ( current_user_can( 'edit_posts' ) ) { return '

' . esc_html__( 'Fillable Documents: set a template slug, for example [layers-doc slug="affidavit-of-loss"].', 'layers-doc-embed' ) . '

'; } return ''; } $options = layers_doc_embed_options(); $height = isset( $atts['height'] ) ? absint( $atts['height'] ) : 0; if ( $height < 300 || $height > 4000 ) { $height = $options['height']; } $from = layers_doc_embed_from_mark( isset( $atts['from'] ) ? $atts['from'] : '' ); $title = isset( $atts['title'] ) && '' !== trim( $atts['title'] ) ? $atts['title'] : $slug; $args = array( 'embed' => '1', 'from' => $from, ); if ( '' !== $options['affiliate_code'] ) { $args['ref'] = $options['affiliate_code']; } $view = isset( $atts['view'] ) && in_array( $atts['view'], array( 'page', 'file' ), true ) ? $atts['view'] : $options['view']; if ( 'page' === $view ) { $args['as'] = 'page'; } $src = add_query_arg( $args, LAYERS_DOC_EMBED_APP . '/t/' . rawurlencode( $slug ) ); if ( $options['autoheight'] ) { wp_enqueue_script( 'layers-doc-embed-height' ); } if ( '' === $wrapper ) { $wrapper = 'class="layers-doc-embed"'; } $html = '
'; $html .= sprintf( '', esc_url( $src ), esc_attr( $title ), $height ); if ( $options['credit'] ) { $html .= sprintf( '

%2$s

', esc_url( 'https://layers.md/embed?from=' . rawurlencode( $from ) ), esc_html__( 'Powered by Layers', 'layers-doc-embed' ) ); } $html .= '
'; return $html; }