Skip to content

Commit 206cfc6

Browse files
authored
Merge pull request #72 from n49/slider_v1.1.0
extracting schema from html and putting in site's head
2 parents 1ad4fdd + 0b0f3c9 commit 206cfc6

3 files changed

Lines changed: 63 additions & 3 deletions

File tree

includes/class-feed-shortcode.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
class Feed_Shortcode {
88

9+
private static $schema_scripts = array();
10+
911
public function __construct(Feed_Deserializer $feed_deserializer) {
1012
$this->feed_deserializer = $feed_deserializer;
13+
// Hook into wp_head to output schemas
14+
add_action('wp_head', array($this, 'output_schemas_to_head'), 1);
1115
}
1216

1317
function custom_esc($str) {
@@ -18,6 +22,53 @@ public function register() {
1822
add_shortcode('opio_feed', array($this, 'init'));
1923
}
2024

25+
/**
26+
* Extract JSON-LD schema scripts from HTML content
27+
*/
28+
private function extract_schema_from_html($html) {
29+
$schemas = array();
30+
31+
// Pattern to match script tags with type="application/ld+json" and id="jsonldSchema"
32+
// Handles attributes in any order and with single or double quotes
33+
$pattern = '/<script[^>]*(?:id=["\']jsonldSchema["\'][^>]*type=["\']application\/ld\+json["\']|type=["\']application\/ld\+json["\'][^>]*id=["\']jsonldSchema["\'])[^>]*>(.*?)<\/script>/is';
34+
35+
if (preg_match_all($pattern, $html, $matches, PREG_SET_ORDER)) {
36+
foreach ($matches as $match) {
37+
$schemas[] = $match[0]; // Full script tag
38+
}
39+
}
40+
41+
return $schemas;
42+
}
43+
44+
/**
45+
* Remove schema scripts from HTML content
46+
*/
47+
private function remove_schema_from_html($html) {
48+
// Remove script tags with id="jsonldSchema" and type="application/ld+json"
49+
// Handles attributes in any order and with single or double quotes
50+
$pattern = '/<script[^>]*(?:id=["\']jsonldSchema["\'][^>]*type=["\']application\/ld\+json["\']|type=["\']application\/ld\+json["\'][^>]*id=["\']jsonldSchema["\'])[^>]*>.*?<\/script>/is';
51+
$html = preg_replace($pattern, '', $html);
52+
53+
// Also remove any stray head tags that might be in the content
54+
// Remove opening <head> tags
55+
$html = preg_replace('/<head[^>]*>/i', '', $html);
56+
// Remove closing </head> tags
57+
$html = preg_replace('/<\/head>/i', '', $html);
58+
59+
return $html;
60+
}
61+
62+
/**
63+
* Output all collected schemas to the head
64+
*/
65+
public function output_schemas_to_head() {
66+
if (!empty(self::$schema_scripts)) {
67+
foreach (self::$schema_scripts as $schema) {
68+
echo $schema . "\n";
69+
}
70+
}
71+
}
2172

2273
public function init($atts) {
2374
if (get_option('opio_active') === '0') {
@@ -58,6 +109,15 @@ public function init($atts) {
58109
$opio_handler = new Opio_Handler($biz_id, $option, $review_type, $org_id);
59110
$reviews = $opio_handler->get_business();
60111

112+
// Extract schema scripts from the feed HTML
113+
$schemas = $this->extract_schema_from_html($reviews);
114+
if (!empty($schemas)) {
115+
// Store schemas to be output in head
116+
self::$schema_scripts = array_merge(self::$schema_scripts, $schemas);
117+
// Remove schemas from body content
118+
$reviews = $this->remove_schema_from_html($reviews);
119+
}
120+
61121
// Wrap entire feed content with Nitropack exclusion wrapper
62122
echo '<div data-nitro-exclude="all" data-nitro-ignore="true" data-nitro-no-optimize="true" data-nitro-preserve-ws="true">';
63123

opio.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: Widget for OPIO Reviews
44
Plugin URI:
55
Description: Instantly add OPIO Reviews on your website to increase user confidence and SEO.
6-
Version: 1.0.99
6+
Version: 1.1.0
77
Author: Dhiraj Timalsina <dhiraj@n49.com>
88
Text Domain: widget-for-opio-reviews
99
Domain Path: /languages
@@ -22,7 +22,7 @@
2222

2323
require(ABSPATH . 'wp-includes/version.php');
2424

25-
define('OPIO_PLUGIN_VERSION' , '1.0.99');
25+
define('OPIO_PLUGIN_VERSION' , '1.1.0');
2626
define('OPIO_PLUGIN_FILE' , __FILE__);
2727
define('OPIO_PLUGIN_URL' , plugins_url(basename(plugin_dir_path(__FILE__ )), basename(__FILE__)));
2828
define('OPIO_ASSETS_URL' , OPIO_PLUGIN_URL . '/assets/');

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: Dhiraj Timalsina
33
Tags: Widget for OPIO Reviews, opio, reviews, rating, widget, google business, testimonials
44
Tested up to: 6.4
5-
Stable tag: 1.0.99
5+
Stable tag: 1.1.0
66
License: GPLv2 or later
77
License URI: http://www.gnu.org/licenses/gpl-2.0.html
88

0 commit comments

Comments
 (0)