]> git.bts.cx Git - cx.git/commitdiff
post metadata support (not very good yet)
authorBen Sherratt <redacted>
Sun, 23 Jul 2023 12:07:50 +0000 (13:07 +0100)
committerBen Sherratt <redacted>
Sun, 23 Jul 2023 12:07:50 +0000 (13:07 +0100)
cx/lib/posts.php
cx/lib/template.php
cx/templates/public/base.php
cx/templates/public/post.php

index 6aca72e54ce59fa30f70836896b74e7719a0193b..2c2dc821e811bfc0fe94d66823a28f0af7940ce3 100644 (file)
@@ -10,6 +10,16 @@ function mk_markdown($markdown) {
        return $Parsedown->text($markdown);
 }
 
+class PostMetadata {
+       public $hero_image;
+       public $hero_image_alt;
+
+       public function __construct($dict) {
+               $this->hero_image = $dict['post_hero_image'];
+               $this->hero_image_alt = $dict['post_hero_image_alt'];
+       }
+}
+
 class Post {
        public $id;
        public $title;
@@ -36,6 +46,22 @@ class Post {
                        $this->html_excerpt = mk_markdown($segments[0]);
                }
        }
+
+       public function get_metadata() {
+               $data = [];
+
+               $doc = new DOMDocument();
+               $doc->loadHTML($this->html_content);
+
+               $image_tag = $doc->getElementsByTagName('img')[0];
+
+               if ($image_tag != null) {
+                       $data['post_hero_image'] = $image_tag->getAttribute('src');
+                       $data['post_hero_image_alt'] = htmlspecialchars($image_tag->getAttribute('alt'));
+               }
+
+               return new PostMetadata($data);
+       }
 }
 
 function cx_post_make_slug($title) {
index e368a2ead46a74436e8e28e3daa50ce052369797..76e5318eba3477605dd9a069149db6bb10e46ce9 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-function cx_template_render($class, $name, $variables = []) {
+function cx_template_render($class, $name, $variables = null) {
        global $cx_template_base, $cx_template_content;
        
        $base_template = null;
@@ -11,10 +11,12 @@ function cx_template_render($class, $name, $variables = []) {
                $path = join(DIRECTORY_SEPARATOR, $segments);
                
                $base_template = null;
+               $base_template_variables = null;
 
                $cx_template_base_previous = $cx_template_base;
-               $cx_template_base = function($name) use (&$base_template ) {
+               $cx_template_base = function($name, $base_variables) use (&$base_template, &$base_template_variables) {
                        $base_template = $name;
+                       $base_template_variables = $base_variables;
                };
 
                $cx_template_content_previous = $cx_template_content;
@@ -24,7 +26,10 @@ function cx_template_render($class, $name, $variables = []) {
 
                cx_require('lib', 'url.php'); // For templates
 
-               extract($variables);
+               if ($variables != null) {
+                       extract($variables);
+               }
+
                ob_start();
                include($path);
                $output = ob_get_contents();
@@ -34,14 +39,15 @@ function cx_template_render($class, $name, $variables = []) {
                $cx_template_content = $cx_template_content_previous;
 
                $name = $base_template;
+               $variables = $base_template_variables;
        }
 
        return $output;
 }
 
-function cx_template_base($name) {
+function cx_template_base($name, $variables = null) {
        global $cx_template_base;
-       $cx_template_base($name);
+       $cx_template_base($name, $variables);
 }
 
 function cx_template_content() {
index 8c3794d0970448b44905cfb4cdef0a6bd494a5f6..83fef3d3b3f21218afb2a7f852a2f97366c22c98 100644 (file)
@@ -8,6 +8,21 @@
        <title><?= cx_site_name(); ?></title>
        <link rel="stylesheet" href="<?= cx_url('/design/css/style.css') ?>">
        <link rel="alternate" type="application/rss+xml" title="bts.cx" href="<?= cx_url('/feed/') ?>" />
+
+       <?php if (isset($cx_post_meta)): ?>
+       
+       <meta property="og:title" content="<?= cx_site_name(); ?>">
+       <meta property="og:type" content="article" />
+       <meta property="og:image" content="<?= $cx_post_meta->hero_image; ?>">
+       <meta property="og:url" content="<?= cx_url_site($cx_post_permalink); ?>">
+       <meta name="twitter:card" content="summary_large_image">
+       <meta name="twitter:image:alt" content="<?= $cx_post_meta->hero_image_alt; ?>">
+
+       <?php /*<meta property="og:description" content="Offering tour packages for individuals or groups.">
+       <meta property="og:site_name" content="<?= cx_site_name(); ?>">
+       <meta name="twitter:image:alt" content="Alt text for image">*/ ?>
+
+       <?php endif; ?>
 </head>
 
 <body>
index 9fb9fcea5b51674d35357b4bd9a1a448a6aa9d25..f44cb29046f0391c86aa027aabf90f9edbfbfd35 100644 (file)
@@ -1,8 +1,9 @@
-<?php cx_template_base('base'); ?>
-<main>
 <?php $post = cx_posts_find_post($post_id); ?>
-<?php if ($post): ?>
+<?php $post_metadata = $post->get_metadata(); ?>
 <?php $post_permalink = '/' . date('Y', $post->date) . '/' . date('m', $post->date) . '/' . $post->slug; ?>
+<?php cx_template_base('base', ['cx_post_meta' => $post_metadata, 'cx_post_permalink' => $post_permalink]); ?>
+<main>
+<?php if ($post): ?>
        <article>
                <h1><a href="<?= cx_url($post_permalink) ?>"><?= $post->title ?></a></h1>
                <p class="updated"><?= date('l, F jS, Y',$post->date) ?></p>