diff --git a/ruady-og-head.php b/ruady-og-head.php index f4c89b4..1e9ad13 100644 --- a/ruady-og-head.php +++ b/ruady-og-head.php @@ -16,7 +16,26 @@ if ( ! defined( 'ABSPATH' ) ) { final class Ruady_OG_Head { public static function init(): void { + add_action( 'init', [ __CLASS__, 'register_endpoint' ] ); + add_action( 'template_redirect', [ __CLASS__, 'maybe_render_image' ] ); add_action( 'wp_head', [ __CLASS__, 'render_head_markup' ], 20 ); + + register_activation_hook( __FILE__, [ __CLASS__, 'activate' ] ); + register_deactivation_hook( __FILE__, [ __CLASS__, 'deactivate' ] ); + } + + public static function register_endpoint(): void { + // Adds /og-image/ to singular permalinks. + add_rewrite_endpoint( 'og-image', EP_PERMALINK | EP_PAGES ); + } + + public static function activate(): void { + self::register_endpoint(); + flush_rewrite_rules(); + } + + public static function deactivate(): void { + flush_rewrite_rules(); } /** @@ -84,7 +103,8 @@ final class Ruady_OG_Head { $site_url_p = parse_url($site_url); $site_host = $site_url_p['host']; - $description = self::make_description(wp_strip_all_tags( $excerpt )); + $og_image_url = user_trailingslashit( trailingslashit( $post_url ) . 'og-image' ); + $description = self::make_description(wp_strip_all_tags( $excerpt )); // Example business logic: // - add a meta tag for every post @@ -100,7 +120,7 @@ final class Ruady_OG_Head { - + @@ -108,16 +128,89 @@ final class Ruady_OG_Head { - + - - - - + ID, $accent ); + + status_header( 200 ); + header( 'Content-Type: image/png' ); + header( 'Cache-Control: public, max-age=3600' ); + + imagepng( $image ); + imagedestroy( $image ); + exit; + } + + private static function render_text_error( int $status, string $message ): void { + status_header( $status ); + header( 'Content-Type: text/plain; charset=utf-8' ); + echo $message; + exit; + } + + private static function truncate( string $text, int $max_len ): string { + if ( mb_strlen( $text ) <= $max_len ) { + return $text; + } + + return mb_substr( $text, 0, $max_len - 1 ) . '…'; + } } Ruady_OG_Head::init();