class-core.php
<?php
namespace HivePress;
use HivePress\Helpers as hp;
defined( 'ABSPATH' ) || exit;
final class Core {
protected static $instance;
protected $extensions = [];
protected $configs = [];
protected $objects = [];
protected $classes = [];
protected function __clone() {}
public function __wakeup() {
throw new \BadMethodCallException();
}
protected function __construct() {
spl_autoload_register( [ $this, 'autoload' ] );
register_activation_hook( HP_FILE, [ __CLASS__, 'activate' ] );
register_deactivation_hook( HP_FILE, [ __CLASS__, 'deactivate' ] );
add_action( 'init', [ $this, 'install' ], 10000 );
add_action( 'plugins_loaded', [ $this, 'setup' ], -10 );
}
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
public function autoload( $class ) {
$parts = explode( '\\', str_replace( '_', '-', strtolower( $class ) ) );
if ( count( $parts ) > 1 && reset( $parts ) === 'hivepress' ) {
$filename = 'class-' . end( $parts ) . '.php';
array_shift( $parts );
array_pop( $parts );
foreach ( $this->get_paths() as $dir ) {
$filepath = rtrim( $dir . '/includes/' . implode( '/', $parts ), '/' ) . '/' . $filename;
if ( file_exists( $filepath ) ) {
require_once $filepath;
if ( ! ( new \ReflectionClass( $class ) )->isAbstract() && method_exists( $class, 'init' ) && ( new \ReflectionMethod( $class, 'init' ) )->isStatic() ) {
call_user_func( [ $class, 'init' ] );
}
break;
}
}
}
}
public static function activate() {
update_option( 'hp_core_activated', '1' );
}
public static function deactivate() {
do_action( 'hivepress/v1/deactivate' );
}
public function install() {
if ( get_option( 'hp_core_activated' ) || count( $this->extensions ) !== absint( get_option( 'hp_extensions_number' ) ) ) {
do_action( 'hivepress/v1/activate' );
if ( get_option( 'hp_core_activated' ) ) {
update_option( 'hp_core_activated', '' );
}
if ( count( $this->extensions ) !== absint( get_option( 'hp_extensions_number' ) ) ) {
update_option( 'hp_extensions_number', count( $this->extensions ) );
}
if ( ! get_option( 'hp_installed_time' ) ) {
update_option( 'hp_installed_time', time() );
}
}
if ( ! get_option( 'hp_core_version' ) || version_compare( get_option( 'hp_core_version' ), $this->get_version(), '<' ) ) {
if ( get_option( 'hp_core_version' ) ) {
do_action( 'hivepress/v1/update', get_option( 'hp_core_version' ) );
}
update_option( 'hp_core_version', $this->get_version() );
}
}
public function setup() {
$this->setup_extensions();
require_once $this->get_path() . '/includes/helpers.php';
$this->load_textdomains();
$this->load_packages();
$this->get_components();
do_action( 'hivepress/v1/setup' );
}
protected function setup_extensions() {
$extensions = apply_filters( 'hivepress/v1/extensions', [ dirname( HP_FILE ) ] );
if ( ! isset( $extensions['updates'] ) ) {
$path = '/vendor/hivepress/hivepress-updates';
foreach ( $extensions as $dir ) {
if ( file_exists( $dir . $path . '/hivepress-updates.php' ) ) {
$extensions['updates'] = $dir . $path;
break;
}
}
}
foreach ( $extensions as $name => $dir ) {
if ( is_array( $dir ) ) {
$this->extensions[ $name ] = $dir;
} else {
$dirname = basename( $dir );
$filepath = $dir . '/' . $dirname . '.php';
$name = str_replace( '-', '_', preg_replace( '/^hivepress-/', '', $dirname ) );
if ( 'hivepress' === $dirname ) {
$name = 'core';
}
if ( file_exists( $filepath ) ) {
$filedata = get_file_data(
$filepath,
[
'name' => 'Plugin Name',
'version' => 'Version',
]
);
$this->extensions[ $name ] = [
'name' => $filedata['name'],
'version' => $filedata['version'],
'path' => $dir,
'url' => rtrim( plugin_dir_url( $filepath ), '/' ),
];
}
}
}
}
protected function load_textdomains() {
foreach ( $this->get_paths() as $dir ) {
$dirname = basename( $dir );
$textdomain = hp\sanitize_slug( $dirname );
load_plugin_textdomain( $textdomain, false, $dirname . '/languages' );
}
}
protected function load_packages() {
foreach ( $this->get_paths() as $dir ) {
$filepath = $dir . '/vendor/autoload.php';
if ( file_exists( $filepath ) ) {
require_once $filepath;
}
}
}
public function __call( $name, $args ) {
if ( strpos( $name, 'get_' ) === 0 ) {
$property = substr( $name, strlen( 'get_' ) );
if ( in_array( $property, [ 'name', 'version', 'path', 'url' ], true ) ) {
$extension = 'core';
if ( $args ) {
$extension = hp\get_first_array_value( $args );
}
$value = null;
if ( isset( $this->extensions[ $extension ][ $property ] ) ) {
$value = $this->extensions[ $extension ][ $property ];
}
return $value;
} else {
$object_type = $property;
if ( ! isset( $this->objects[ $object_type ] ) ) {
$this->objects[ $object_type ] = [];
foreach ( $this->get_paths() as $dir ) {
foreach ( glob( $dir . '/includes/' . $object_type . '/*.php' ) as $filepath ) {
$object_name = str_replace( '-', '_', preg_replace( '/^class-/', '', basename( $filepath, '.php' ) ) );
$object = hp\create_class_instance( '\HivePress\\' . $object_type . '\\' . $object_name );
if ( $object ) {
$this->objects[ $object_type ][ $object_name ] = $object;
}
}
}
}
return $this->objects[ $object_type ];
}
}
throw new \BadMethodCallException();
}
public function __get( $name ) {
return hp\get_array_value( $this->get_components(), $name );
}
public function get_paths() {
return array_column( $this->extensions, 'path' );
}
public function get_config( $name ) {
if ( ! isset( $this->configs[ $name ] ) ) {
$this->configs[ $name ] = [];
foreach ( $this->get_paths() as $dir ) {
$filepath = $dir . '/includes/configs/' . hp\sanitize_slug( $name ) . '.php';
if ( file_exists( $filepath ) ) {
$this->configs[ $name ] = hp\merge_arrays( $this->configs[ $name ], include $filepath );
}
}
$this->configs[ $name ] = apply_filters( 'hivepress/v1/' . $name, $this->configs[ $name ] );
}
return $this->configs[ $name ];
}
public function get_classes( $namespace ) {
if ( ! isset( $this->classes[ $namespace ] ) ) {
$this->classes[ $namespace ] = [];
foreach ( $this->get_paths() as $dir ) {
foreach ( glob( $dir . '/includes/' . $namespace . '/*.php' ) as $filepath ) {
$name = str_replace( '-', '_', preg_replace( '/^class-/', '', basename( $filepath, '.php' ) ) );
$class = '\HivePress\\' . $namespace . '\\' . $name;
if ( class_exists( $class ) && ! ( new \ReflectionClass( $class ) )->isAbstract() ) {
$this->classes[ $namespace ][ $name ] = $class;
}
}
}
}
return $this->classes[ $namespace ];
}
}