165 lines
4.6 KiB
PHP

<?php
/**
* Plugin Name: Book Page Reformatter with tabs!
* Description: Rewrites the book-wrapper block using output buffering for Supapress pages.
* Version: 2.0
* Author: Kian
*/
function enqueue_theme_assets(){
wp_enqueue_script( 'main', plugins_url( '/js/main.js', __FILE__ ));
}
add_action( 'wp_enqueue_scripts', 'enqueue_theme_assets' );
add_action('template_redirect', function() {
if (is_singular()) {
ob_start('br_replace_book_wrapper');
ob_start('br_replace_book_link');
}
});
function enqueue_shop_links_script() {
if (is_singular()) {
// 1. Enqueue your JS file
wp_enqueue_script(
'search_shop_link',
plugins_url('js/search_shop_link.js', __FILE__),
[],
false,
true
);
// 2. Localize data for that script
wp_localize_script(
'search_shop_link',
'SearchShopLinkData',
[
'imageUrl' => plugins_url('assets/cart_add.png', __FILE__)
]
);
}
}
add_action('wp_enqueue_scripts', 'enqueue_shop_links_script');
function br_replace_book_wrapper($html) {
if (strpos($html, 'product-details') === false) {
return $html;
}
preg_match('/<div class="book-wrapper">(.*?)<\/div>\s*<\/div>/s', $html, $match);
if (!isset($match[0])) return $html;
$book_block = $match[0];
$cover = extract_attr($book_block, '/<img[^>]*src="([^"]+)"/');
$title = extract_text($book_block, '/<div class="title-wrapper">\s*<h1>(.*?)<\/h1>/');
$author = extract_text($book_block, '/<p class="sp__the-author">(.*?)<\/p>/');
$publisher = extract_text($book_block, '/<p class="sp__the-publisher">(.*?)<\/p>/');
$imprint = extract_text($book_block, '/<p class="sp__the-imprint">(.*?)<\/p>/');
$series = extract_text($book_block, '/<p class="sp__the-series">(.*?)<\/p>/');
$isbn13 = extract_text($book_block, '/<p class="sp__the-isbn13">(.*?)<\/p>/');
$price = extract_text($book_block, '/<p class="sp__the-price">(.*?)<\/p>/');
$format = extract_text($book_block, '/<p class="sp__the-format">(.*?)<\/p>/');
$pubdate = extract_text($book_block, '/<p class="sp__the-publication-date">(.*?)<\/p>/');
$pages = extract_text($book_block, '/<p class="sp__the-pages">(.*?)<\/p>/');
$summary = extract_html($book_block, '/<div class="sp__the-summary">(.*?)<\/div>/s');
$description= extract_html($book_block, '/<div class="sp__the-description">(.*?)<\/div>/s');
$reviews = extract_html($book_block, '/<div class="sp__the-reviews">(.*?)<\/div>/s');
$html = preg_replace_callback(
'/(<div role="tabpanel" class="wp-block-ub-tabbed-content-tab-content-wrap.*?"[^>]*>)(.*?)(<\/div>)/s',
function($matches) use ($description, $author, $reviews) {
static $i = 0;
$replacement = '';
if ($i === 0) $replacement = $description;
elseif ($i === 1) $replacement = 'Hello World!';
elseif ($i === 2) $replacement = $author;
elseif ($i === 3) $replacement = $reviews;
$i++;
return $matches[1] . $replacement . $matches[3];
},
$html
);
return $html;
}
function extract_text($html, $pattern) {
preg_match($pattern, $html, $m);
return $m[1] ?? '';
}
function extract_attr($html, $pattern) {
preg_match($pattern, $html, $m);
return $m[1] ?? '';
}
function extract_html($html, $pattern) {
preg_match($pattern, $html, $m);
return $m[1] ?? '';
}
//gets rid of information and image wrapper
add_action('wp_head', function() {
echo '<style>
.information-wrapper {
display: none !important;
}
.sp__the-subtitle{
display: none !important;
}
.sp__the-author{
display: none !important;
}
.sp__the-author-bio{
display: none !important;
}
.sp__the-publisher{
display: none !important;
}
.sp__the-imprint{ display: none !important;
}
.sp__the-isbn13{ display: none !important;
}
.sp__the-price{ display: none !important;
}
.sp__the-format{ display: none !important;
}
.sp__the-publication-date{ display: none !important;
}
.sp__the-sales-date{ display: none !important;
}
.sp__the-series{
display: none !important;
}
</style>';
});