| 1 |
<?php |
| 2 |
|
| 3 |
namespace Gianism; |
| 4 |
|
| 5 |
use Gianism\Api\ShortCodes; |
| 6 |
use Gianism\Controller\Admin; |
| 7 |
use Gianism\Controller\Login; |
| 8 |
use Gianism\Controller\Network; |
| 9 |
use Gianism\Controller\Profile; |
| 10 |
use Gianism\Controller\ProfileChecker; |
| 11 |
use Gianism\Controller\Rewrite; |
| 12 |
use Gianism\Controller\UserList; |
| 13 |
use Gianism\Helper\ServiceManager; |
| 14 |
use Gianism\Pattern\AppBase; |
| 15 |
use Gianism\Pattern\Singleton; |
| 16 |
|
| 17 |
/** |
| 18 |
* Main controller of Gianism |
| 19 |
* |
| 20 |
* This controller initialize Gianism. |
| 21 |
* |
| 22 |
* @package Gianism |
| 23 |
* @author Takahashi Fumiki |
| 24 |
* @since 2.0.0 |
| 25 |
*/ |
| 26 |
class Bootstrap extends Singleton { |
| 27 |
|
| 28 |
use AppBase; |
| 29 |
|
| 30 |
/** |
| 31 |
* Initialize Gianism |
| 32 |
*/ |
| 33 |
public static function init() { |
| 34 |
$instance = self::get_instance(); |
| 35 |
/** |
| 36 |
* Fires after gianism is set up. |
| 37 |
* |
| 38 |
* @action gianism_init |
| 39 |
*/ |
| 40 |
do_action( 'gianism_after_setup' ); |
| 41 |
// Register Gianism command |
| 42 |
if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 43 |
\WP_CLI::add_command( 'gianism', 'Gianism\\Commands\\TestCommand' ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Constructor |
| 49 |
* |
| 50 |
* @param array $argument |
| 51 |
*/ |
| 52 |
protected function __construct( array $argument = [] ) { |
| 53 |
parent::__construct( $argument ); |
| 54 |
// Register assets and blocks |
| 55 |
add_action( 'init', array( $this, 'register_assets' ) ); |
| 56 |
add_action( 'init', array( $this, 'register_blocks' ) ); |
| 57 |
// Admin page |
| 58 |
add_action( |
| 59 |
'admin_menu', |
| 60 |
function () { |
| 61 |
Admin::get_instance(); |
| 62 |
} |
| 63 |
); |
| 64 |
// Register notices |
| 65 |
$notices = []; |
| 66 |
foreach ( scandir( __DIR__ . '/Notices' ) as $file ) { |
| 67 |
if ( preg_match( '#^([^_.]+)\.php$#u', $file, $matches ) ) { |
| 68 |
$class_name = "Gianism\\Notices\\{$matches[1]}"; |
| 69 |
if ( class_exists( $class_name ) && ! $class_name::DEPRECATED ) { |
| 70 |
$notices[] = $class_name; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
/** |
| 75 |
* gianism_admin_notices_class |
| 76 |
* |
| 77 |
* @package Gianism |
| 78 |
* @since 3.0.4 |
| 79 |
* @param array $notices Class name array. |
| 80 |
*/ |
| 81 |
$notices = apply_filters( 'gianism_admin_notices_class', $notices ); |
| 82 |
foreach ( $notices as $notice ) { |
| 83 |
if ( is_subclass_of( $notice, 'Gianism\\Pattern\\AbstractNotice' ) ) { |
| 84 |
$instance = new $notice(); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
// Remove WP Multi-byte Patch's CSS |
| 89 |
// Because it breaks icon font |
| 90 |
add_action( |
| 91 |
'admin_enqueue_scripts', |
| 92 |
function () { |
| 93 |
wp_dequeue_style( 'wpmp-admin-custom' ); |
| 94 |
}, |
| 95 |
1000 |
| 96 |
); |
| 97 |
|
| 98 |
/** |
| 99 |
* Fires before gianism start. |
| 100 |
* |
| 101 |
* @action gianism_before_setup |
| 102 |
* @since 3.0.0 |
| 103 |
*/ |
| 104 |
do_action( 'gianism_before_setup' ); |
| 105 |
// Initialize service manager. |
| 106 |
$service = ServiceManager::get_instance(); |
| 107 |
$service->init(); |
| 108 |
|
| 109 |
// Initialize Rewrite rules. |
| 110 |
Rewrite::get_instance(); |
| 111 |
// Network controller. |
| 112 |
Network::get_instance(); |
| 113 |
// User list |
| 114 |
UserList::get_instance(); |
| 115 |
|
| 116 |
// If enabled, create interface and rewrite rules. |
| 117 |
if ( $this->option->is_enabled() ) { |
| 118 |
// Init profile manager |
| 119 |
Profile::get_instance(); |
| 120 |
ProfileChecker::get_instance(); |
| 121 |
// Init login manager |
| 122 |
Login::get_instance(); |
| 123 |
// Enqueue scripts |
| 124 |
add_action( 'login_enqueue_scripts', [ $this, 'enqueue_global_assets' ] ); |
| 125 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_global_assets' ] ); |
| 126 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] ); |
| 127 |
// Add short codes. |
| 128 |
ShortCodes::get_instance(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Register blocks |
| 134 |
*/ |
| 135 |
public function register_blocks() { |
| 136 |
$blocks_dir = $this->dir . '/assets/blocks'; |
| 137 |
if ( ! is_dir( $blocks_dir ) ) { |
| 138 |
return; |
| 139 |
} |
| 140 |
foreach ( scandir( $blocks_dir ) as $block ) { |
| 141 |
if ( '.' === $block || '..' === $block ) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
$block_json = $blocks_dir . '/' . $block . '/block.json'; |
| 145 |
if ( file_exists( $block_json ) ) { |
| 146 |
register_block_type( $block_json ); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Register assets |
| 153 |
* |
| 154 |
*/ |
| 155 |
public function register_assets() { |
| 156 |
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 157 |
// JS Cookie |
| 158 |
wp_register_script( 'js-cookie', $this->url . 'assets/vendor/js.cookie' . $min . '.js', [], '3.0.5', true ); |
| 159 |
// Register assets in dependencies.json |
| 160 |
$json = $this->dir . '/wp-dependencies.json'; |
| 161 |
if ( file_exists( $json ) ) { |
| 162 |
$deps = json_decode( file_get_contents( $json ), true ); |
| 163 |
if ( $deps ) { |
| 164 |
foreach ( $deps as $dep ) { |
| 165 |
if ( empty( $dep['path'] ) ) { |
| 166 |
continue; |
| 167 |
} |
| 168 |
$url = $this->url . $dep['path']; |
| 169 |
switch ( $dep['ext'] ) { |
| 170 |
case 'js': |
| 171 |
$footer = [ 'in_footer' => $dep['footer'] ]; |
| 172 |
if ( in_array( $dep['strategy'], [ 'defer', 'async' ], true ) ) { |
| 173 |
$footer['strategy'] = $dep['strategy']; |
| 174 |
} |
| 175 |
wp_register_script( $dep['handle'], $url, $dep['deps'], $dep['hash'], $footer ); |
| 176 |
break; |
| 177 |
case 'css': |
| 178 |
wp_register_style( $dep['handle'], $url, $dep['deps'], $dep['hash'], 'screen' ); |
| 179 |
break; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Enqueue assets for public screen |
| 188 |
*/ |
| 189 |
public function enqueue_global_assets() { |
| 190 |
wp_enqueue_style( $this->name ); |
| 191 |
wp_enqueue_script( $this->name . '-notice-helper' ); |
| 192 |
wp_localize_script( |
| 193 |
$this->name . '-notice-helper', |
| 194 |
'GianismHelper', |
| 195 |
[ |
| 196 |
'confirmLabel' => __( 'Consent Required', 'wp-gianism' ), |
| 197 |
'btnConfirm' => __( 'Confirm', 'wp-gianism' ), |
| 198 |
'btnCancel' => __( 'Cancel', 'wp-gianism' ), |
| 199 |
] |
| 200 |
); |
| 201 |
wp_localize_script( |
| 202 |
$this->name . '-notice-helper', |
| 203 |
'Gianism', |
| 204 |
array( |
| 205 |
'admin' => false, |
| 206 |
) |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Enqueue admin scripts |
| 212 |
*/ |
| 213 |
public function enqueue_admin_assets() { |
| 214 |
wp_enqueue_script( $this->name . '-notice-helper' ); |
| 215 |
wp_localize_script( |
| 216 |
$this->name . '-notice-helper', |
| 217 |
'Gianism', |
| 218 |
array( |
| 219 |
'admin' => true, |
| 220 |
) |
| 221 |
); |
| 222 |
} |
| 223 |
} |
| 224 |
|