first commit

This commit is contained in:
Kian 2026-01-19 21:10:21 -05:00
parent 3e84da3dd1
commit df5072201b

View File

@ -1,89 +1,57 @@
<?php
/**
* Plugin Name: test
* Description: Rewrites the book-wrapper block using output buffering for Supapress pages.
* Plugin Name: the_content
* Description: test to see if the_content works
* Version: 1.0
* Author: Kian
*/
add_filter('the_content', 'test_replace_tabs_on_book_wrapper', 30);
function test_replace_tabs_on_book_wrapper($content) {
add_action('wp_enqueue_scripts','ava_test_init');
function ava_test_init() {
wp_enqueue_script( 'ava-test-js', plugins_url( '/js/ava_test_.js', __FILE__ ));
}
add_action('template_redirect', function() {
if (is_singular()) {
ob_start('br_replace_book_wrapper');
}
});
function br_replace_book_wrapper($html) {
if (strpos($html, 'product-details') === false) {
return $html;
if (strpos($content, 'book-wrapper') === false) {
return $content;
}
preg_match('/<div class="book-wrapper">(.*?)<\/div>\s*<\/div>/s', $html, $match);
libxml_use_internal_errors(true);
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $content);
$xpath = new DOMXPath($dom);
// if (!isset($match[0])) return $html;
$panels = $xpath->query(
"//*[contains(@class,'wp-block-ub-tabbed-content-tab-content-wrap')]"
);
$book_block = $match[0];
if ($panels->length === 0) {
return $content;
}
$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');
if ($panels->item(0)) {
replace_panel_html($dom, $panels->item(0), '<p>TEST AUTHOR CONTENT</p>');
}
if ($panels->item(1)) {
replace_panel_html($dom, $panels->item(1), '<p>TEST SUMMARY CONTENT</p>');
}
if ($panels->item(2)) {
replace_panel_html($dom, $panels->item(2), '<p>TEST REVIEWS CONTENT</p>');
}
return str_replace($book_block, $new_block, $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] ?? '';
return $dom->saveHTML();
}
?>
<script type="text/javascript">
var js_variable = '<?php echo $title;?>';
<script>
function replace_panel_html($dom, $panel, $html) {
while ($panel->firstChild) {
$panel->removeChild($panel->firstChild);
}
$fragment = $dom->createDocumentFragment();
$fragment->appendXML($html);
$panel->appendChild($fragment);
}