PHP 8.0.30
Preview: class-json-manager.php Size: 2.78 KB
/home/certprox/zang.certproxywizard.com/wp/wp-content/plugins/seo-by-rank-math/includes/class-json-manager.php

<?php
/**
 * The JSON manager handles json output to admin and frontend.
 *
 * @since      1.0.0
 * @package    RankMath
 * @subpackage RankMath
 * @author     RankMath <support@rankmath.com>
 */

namespace RankMath;

/**
 * Json_Manager class.
 */
class Json_Manager {

	/**
	 * Data.
	 *
	 * @var array
	 */
	private $data = [];

	/**
	 * Construct
	 */
	public function __construct() {
		$hook = is_admin() ? 'admin_footer' : 'wp_footer';
		add_action( $hook, [ $this, 'output' ], 0 );
	}

	/**
	 * Add something to JSON object.
	 *
	 * @param string $key         Unique identifier.
	 * @param mixed  $value       The data itself can be either a single or an array.
	 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
	 */
	public function add( $key, $value, $object_name ) {

		if ( empty( $key ) ) {
			return;
		}

		// If key doesn't exists.
		if ( ! isset( $this->data[ $object_name ][ $key ] ) ) {
			$this->data[ $object_name ][ $key ] = $value;
			return;
		}

		// If key already exists.
		$old_value = $this->data[ $object_name ][ $key ];

		// If both array merge them.
		if ( is_array( $old_value ) && is_array( $value ) ) {
			$this->data[ $object_name ][ $key ] = array_merge( $old_value, $value );
			return;
		}

		$this->data[ $object_name ][ $key ] = $value;
	}

	/**
	 * Remove something from JSON object.
	 *
	 * @param string $key         Unique identifier.
	 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
	 */
	public function remove( $key, $object_name ) {
		if ( isset( $this->data[ $object_name ][ $key ] ) ) {
			unset( $this->data[ $object_name ][ $key ] );
		}
	}

	/**
	 * Print data.
	 */
	public function output() {
		$script = $this->encode();
		if ( ! $script ) {
			return;
		}

		wp_print_inline_script_tag( $script, [ 'id' => 'rank-math-json' ] );
	}

	/**
	 * Get encoded string.
	 *
	 * @return string
	 */
	private function encode() {
		$script = '';
		foreach ( $this->data as $object_name => $object_data ) {
			$script .= $this->single_object( $object_name, $object_data );
		}

		return $script;
	}

	/**
	 * Encode single object.
	 *
	 * @param  string $object_name Object name to use as JS variable.
	 * @param  array  $object_data Object data to json encode.
	 * @return array
	 */
	private function single_object( $object_name, $object_data ) {
		if ( empty( $object_data ) ) {
			return '';
		}

		$object_data = apply_filters( 'rank_math/json_data', $object_data );
		foreach ( (array) $object_data as $key => $value ) {
			if ( ! is_string( $value ) ) {
				continue;
			}

			$object_data[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
		}

		return "var $object_name = " . wp_json_encode( $object_data ) . ';' . PHP_EOL;
	}
}

Directory Contents

Dirs: 14 × Files: 24

Name Size Perms Modified Actions
3rdparty DIR
- drwxr-xr-x 2026-04-02 06:11:37
Edit Download
admin DIR
- drwxr-xr-x 2026-04-02 06:11:41
Edit Download
cli DIR
- drwxr-xr-x 2026-04-02 06:11:41
Edit Download
frontend DIR
- drwxr-xr-x 2026-04-02 06:11:41
Edit Download
helpers DIR
- drwxr-xr-x 2026-04-02 06:11:41
Edit Download
metaboxes DIR
- drwxr-xr-x 2026-04-02 06:11:41
Edit Download
module DIR
- drwxr-xr-x 2026-04-02 06:11:41
Edit Download
modules DIR
- drwxr-xr-x 2026-04-02 06:11:44
Edit Download
opengraph DIR
- drwxr-xr-x 2026-04-02 06:11:46
Edit Download
- drwxr-xr-x 2026-04-02 06:11:46
Edit Download
rest DIR
- drwxr-xr-x 2026-04-02 06:11:46
Edit Download
settings DIR
- drwxr-xr-x 2026-04-02 06:11:46
Edit Download
traits DIR
- drwxr-xr-x 2026-04-02 06:11:46
Edit Download
updates DIR
- drwxr-xr-x 2026-04-02 06:11:46
Edit Download
2.23 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
11.38 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
7.02 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
2.52 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
4.07 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
1.13 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
9.76 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
11.30 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
23.34 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
2.78 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
12.35 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
3.49 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
4.68 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
9.57 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
3.41 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
3.04 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
7.89 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
11.02 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
5.20 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
2.58 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
1.55 KB lrw-r--r-- 2026-04-02 06:11:41
Edit Download
28 B lrw-r--r-- 2026-04-02 06:11:41
Edit Download
380 B lrw-r--r-- 2026-04-02 06:11:41
Edit Download
2.03 KB lrw-r--r-- 2026-04-02 06:11:46
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).