From fe6922ec178647a19d742cbb944aa0310e1cd8b4 Mon Sep 17 00:00:00 2001 From: Amirreza Nazemi Date: Sat, 22 Nov 2025 17:42:21 +0330 Subject: [PATCH] feat: add embed shortcode --- includes/modules/shortcodes/types/embed.php | 98 +++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 includes/modules/shortcodes/types/embed.php diff --git a/includes/modules/shortcodes/types/embed.php b/includes/modules/shortcodes/types/embed.php new file mode 100644 index 0000000..8926a9c --- /dev/null +++ b/includes/modules/shortcodes/types/embed.php @@ -0,0 +1,98 @@ + '', + 'before' => '', + 'after' => '', + 'fallback' => '', + 'format' => '', + ]; + } + + /** + * Renders the shortcode output. + * + * @since NEXT + * + * @param array $attributes Shortcode attributes. + * @param string $content Enclosed content (optional). + * + * @return string + */ + public function render( array $attributes, string $content ): string { + // Retrieves merged shortcode attributes. + $attributes = $this->get_attributes( $attributes ); + + // Resolves dynamic placeholder attributes. + $attributes = anys_parse_dynamic_attributes( $attributes ); + + $url = isset( $attributes['url'] ) ? trim( (string) $attributes['url'] ) : ''; + + // Returns early if no URL is provided. + if ( '' === $url ) { + return ''; + } + + // Attempts to fetch an oEmbed preview for the URL. + $embed_html = wp_oembed_get( $url ); + + // Applies fallback rendering if oEmbed fails. + if ( ! $embed_html ) { + $fallback = isset( $attributes['fallback'] ) ? (string) $attributes['fallback'] : ''; + + if ( '' !== $fallback ) { + $embed_html = $fallback; + } else { + // Uses a basic anchor link as the final fallback. + $embed_html = sprintf( + '%s', + esc_url( $url ), + esc_html( $url ) + ); + } + } + + $value = $embed_html; + + // Wraps the value with before/after markup and fallback handling. + $output = anys_wrap_output( $value, $attributes ); + + // Returns the rendered content and processes nested shortcodes. + return $output . do_shortcode( $content ); + } +}