227 lines
6.6 KiB
PHP
227 lines
6.6 KiB
PHP
<?php
|
|
// Copyright 2026 Umbra Tools (umbratools.com)
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
/**
|
|
* SEO Metadata Manager class
|
|
*
|
|
* @package BD_Bookshelf
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class BDM_SEO {
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
public function __construct() {
|
|
add_action('wp_head', [$this, 'output_meta_tags'], 1);
|
|
add_action('wp_head', [$this, 'output_open_graph'], 2);
|
|
add_action('wp_head', [$this, 'output_twitter_cards'], 3);
|
|
|
|
// Add sitemap entries
|
|
add_filter('wp_sitemaps_posts_entry', [$this, 'filter_sitemap_entry'], 10, 2);
|
|
}
|
|
|
|
/**
|
|
* Get book SEO data
|
|
*/
|
|
public static function get_book_seo($book_id) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('book_seo');
|
|
|
|
$seo = $wpdb->get_row(
|
|
$wpdb->prepare(
|
|
"SELECT * FROM {$table} WHERE book_id = %d",
|
|
$book_id
|
|
)
|
|
);
|
|
|
|
return $seo;
|
|
}
|
|
|
|
/**
|
|
* Update book SEO data
|
|
*/
|
|
public static function update_book_seo($book_id, $data) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('book_seo');
|
|
|
|
$existing = self::get_book_seo($book_id);
|
|
|
|
$update_data = [
|
|
'book_id' => absint($book_id),
|
|
'seo_title' => sanitize_text_field($data['seo_title'] ?? ''),
|
|
'seo_description' => wp_kses_post($data['seo_description'] ?? ''),
|
|
'seo_keywords' => sanitize_text_field($data['seo_keywords'] ?? ''),
|
|
'og_title' => sanitize_text_field($data['og_title'] ?? ''),
|
|
'og_description' => wp_kses_post($data['og_description'] ?? ''),
|
|
'og_image' => esc_url_raw($data['og_image'] ?? ''),
|
|
];
|
|
|
|
if ($existing) {
|
|
$result = $wpdb->update(
|
|
$table,
|
|
$update_data,
|
|
['book_id' => $book_id],
|
|
null,
|
|
['%d']
|
|
);
|
|
} else {
|
|
$result = $wpdb->insert($table, $update_data);
|
|
}
|
|
|
|
return $result !== false;
|
|
}
|
|
|
|
/**
|
|
* Delete book SEO data
|
|
*/
|
|
public static function delete_book_seo($book_id) {
|
|
global $wpdb;
|
|
|
|
$table = BDM_Database::table('book_seo');
|
|
return $wpdb->delete($table, ['book_id' => $book_id], ['%d']) !== false;
|
|
}
|
|
|
|
/**
|
|
* Output meta tags
|
|
*/
|
|
public function output_meta_tags() {
|
|
if (!is_singular()) {
|
|
return;
|
|
}
|
|
|
|
$post_id = get_queried_object_id();
|
|
$book_id = $this->get_book_id_for_post($post_id);
|
|
|
|
if (!$book_id) {
|
|
return;
|
|
}
|
|
|
|
$seo = self::get_book_seo($book_id);
|
|
if (!$seo) {
|
|
return;
|
|
}
|
|
|
|
if (!empty($seo->seo_title)) {
|
|
echo '<meta name="title" content="' . esc_attr($seo->seo_title) . '">' . "\n";
|
|
}
|
|
|
|
if (!empty($seo->seo_description)) {
|
|
echo '<meta name="description" content="' . esc_attr($seo->seo_description) . '">' . "\n";
|
|
}
|
|
|
|
if (!empty($seo->seo_keywords)) {
|
|
echo '<meta name="keywords" content="' . esc_attr($seo->seo_keywords) . '">' . "\n";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Output Open Graph tags
|
|
*/
|
|
public function output_open_graph() {
|
|
if (!is_singular()) {
|
|
return;
|
|
}
|
|
|
|
$post_id = get_queried_object_id();
|
|
$book_id = $this->get_book_id_for_post($post_id);
|
|
|
|
if (!$book_id) {
|
|
return;
|
|
}
|
|
|
|
$seo = self::get_book_seo($book_id);
|
|
$book = BDM_Book_Manager::get_book($book_id);
|
|
|
|
if (!$book) {
|
|
return;
|
|
}
|
|
|
|
$og_title = !empty($seo->og_title) ? $seo->og_title : get_the_title($post_id);
|
|
$og_description = !empty($seo->og_description) ? $seo->og_description : wp_trim_words(get_the_excerpt($post_id), 30);
|
|
$og_image = !empty($seo->og_image) ? $seo->og_image : get_the_post_thumbnail_url($post_id, 'large');
|
|
|
|
echo '<meta property="og:type" content="book">' . "\n";
|
|
echo '<meta property="og:title" content="' . esc_attr($og_title) . '">' . "\n";
|
|
echo '<meta property="og:description" content="' . esc_attr($og_description) . '">' . "\n";
|
|
echo '<meta property="og:url" content="' . esc_url(get_permalink($post_id)) . '">' . "\n";
|
|
|
|
if ($og_image) {
|
|
echo '<meta property="og:image" content="' . esc_url($og_image) . '">' . "\n";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Output Twitter Card tags
|
|
*/
|
|
public function output_twitter_cards() {
|
|
if (!is_singular()) {
|
|
return;
|
|
}
|
|
|
|
$post_id = get_queried_object_id();
|
|
$book_id = $this->get_book_id_for_post($post_id);
|
|
|
|
if (!$book_id) {
|
|
return;
|
|
}
|
|
|
|
$seo = self::get_book_seo($book_id);
|
|
|
|
$twitter_title = !empty($seo->og_title) ? $seo->og_title : get_the_title($post_id);
|
|
$twitter_description = !empty($seo->og_description) ? $seo->og_description : wp_trim_words(get_the_excerpt($post_id), 30);
|
|
$twitter_image = !empty($seo->og_image) ? $seo->og_image : get_the_post_thumbnail_url($post_id, 'large');
|
|
|
|
echo '<meta name="twitter:card" content="summary_large_image">' . "\n";
|
|
echo '<meta name="twitter:title" content="' . esc_attr($twitter_title) . '">' . "\n";
|
|
echo '<meta name="twitter:description" content="' . esc_attr($twitter_description) . '">' . "\n";
|
|
|
|
if ($twitter_image) {
|
|
echo '<meta name="twitter:image" content="' . esc_url($twitter_image) . '">' . "\n";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get book ID for a WordPress post
|
|
*/
|
|
private function get_book_id_for_post($post_id) {
|
|
// Check if there's a book ID stored in post meta
|
|
return get_post_meta($post_id, '_bd_bookshelf_book_id', true);
|
|
}
|
|
|
|
/**
|
|
* Filter sitemap entry for book posts
|
|
*/
|
|
public function filter_sitemap_entry($entry, $post) {
|
|
$book_id = $this->get_book_id_for_post($post->ID);
|
|
|
|
if (!$book_id) {
|
|
return $entry;
|
|
}
|
|
|
|
$book = BDM_Book_Manager::get_book($book_id);
|
|
|
|
if (!$book) {
|
|
return $entry;
|
|
}
|
|
|
|
// Higher rating = higher priority
|
|
$rating = max($book->rating_amazon, $book->rating_goodreads);
|
|
$priority = 0.5 + ($rating / 10); // 0.5 to 1.0 based on rating
|
|
|
|
$entry['loc'] = get_permalink($post->ID);
|
|
$entry['lastmod'] = get_the_modified_date('c', $post);
|
|
$entry['changefreq'] = 'monthly';
|
|
$entry['priority'] = round($priority, 1);
|
|
|
|
return $entry;
|
|
}
|
|
}
|