← Back
Editing: class-configs.php
<?php namespace WPaaS; if ( ! defined( 'ABSPATH' ) ) { exit; } final class Configs { use Helpers; const INFO_FILE = '/info.json'; /** * Array of config data. * * @var array */ private $data = []; /** * Object of v2 data. * * @var object */ private $v2Data; /** * Class constructor. */ public function __construct() { $path = $this->find_readable_path( self::gd_config_paths() ); if ( $path ) { require_once $path; } $this->load_info_json(); } /** * Return the v2 data. * * @return object */ public function get_v2_data() { return $this->v2Data; } /** * Where this platform delivers the per-site info.json. * * Not filterable: this file carries the site's plan data. A v2 platform that * binds it elsewhere is added here. * * @since 4.302.0 * * @return string */ public static function info_json_path() { return self::INFO_FILE; } /** * Read the per-site info.json. * * @param string|null $path Where to read it from. Defaults to the delivered * location, which is what the constructor passes; the * argument exists so the read and log decisions below can * be exercised against a fixture without a filter on * info_json_path(), which the site must not be able to * repoint. */ public function load_info_json( $path = null ) { $this->v2Data = new \stdClass; if ( ! self::is_wpaas_v2() ) { return; } $path = null === $path ? self::info_json_path() : $path; /* * An absent info.json is a supported state, not an error: a v2 platform may * carry none of the data this file holds. It used to error_log() here, which * on such a platform wrote a line per request forever. Only a file that is * present but unusable is worth a log line, because that one is a real defect * in whatever wrote it. */ if ( ! $path || ! file_exists( $path ) ) { return; } $contents = is_readable( $path ) ? file_get_contents( $path ) : false; if ( false === $contents ) { error_log( "Couldn't read the info.json file for the domain, path: " . $path ); return; } $decoded = \json_decode( $contents ); if ( null === $decoded && JSON_ERROR_NONE !== \json_last_error() ) { error_log( "Couldn't decode the info.json file for the domain, path: " . $path . ", error: " . \json_last_error_msg() ); return; } $this->v2Data = (object) $decoded; } /** * Return the first readable path from an array. * * @param array $paths * * @return string|false */ private function find_readable_path( array $paths ) { foreach ( $paths as $path ) { if ( is_readable( $path ) ) { return $path; } } return false; } }
Save File
Cancel