Uname: Linux p3plzcpnl499967.prod.phx3.secureserver.net 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64
Software: Apache
PHP version: 8.2.30 [ PHP INFO ] PHP os: Linux
Server Ip: 208.109.40.231
Your Ip: 216.73.216.26
User: nayff91c5tsx (10005085) | Group: nayff91c5tsx (10005085)
Safe Mode: OFF
Disable Function:
NONE

name : json-ld-schema.php
<?php
/**
 * Class responsible for handling FAQ structured data schema (JSON-LD)
 */
namespace Jet_Tabs;

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if ( ! class_exists( 'Jet_Tabs\JSON_LD_Schema' ) ) {

	/**
	 * Define JSON_LD_Schema class
	 */
	class JSON_LD_Schema {

		/**
		 * Holds the single instance of the class.
		 *
		 * @var JSON_LD_Schema|null
		 */
		private static $instance = null;

		/**
		 * Array to store FAQ schema items.
		 *
		 * @var array
		 */
		private $schema = [];

		/**
		 * Indicates whether the schema rendering has been hooked.
		 *
		 * @var bool
		 */
		private $hooked = false;

		/**
		 * Get instance of the class
		 *
		 * @return JSON_LD_Schema
		 */
		public static function instance() {
			if ( is_null( self::$instance ) ) {
				self::$instance = new self();
			}
			return self::$instance;
		}

		/**
		 * Add item (Question-Answer) to schema
		 *
		 * @param string $question Question text
		 * @param string $answer   Answer text
		 *
		 * @return void
		 */
		public function add_item( $question, $answer ) {

			if ( ! $this->hooked ) {
				add_action( 'wp_footer', [ $this, 'print_schema' ], 999 );
				$this->hooked = true;
			}

			$this->schema[] = array(
				'@type' => 'Question',
				'name'  => wp_strip_all_tags( $question ),
				'acceptedAnswer' => array(
					'@type' => 'Answer',
					'text'  => wp_strip_all_tags( $answer ),
				),
			);
		}

		/**
		 * Print schema in JSON-LD format
		 *
		 * @return void
		 */
		public function print_schema() {
			if ( ! empty( $this->schema ) ) {
				$json_ld = array(
					'@context'   => 'https://schema.org',
					'@type'      => 'FAQPage',
					'mainEntity' => $this->schema,
				);
				echo '<script type="application/ld+json">' . wp_json_encode( $json_ld ) . '</script>';
			}
		}
	}
}
© 2026 GrazzMean