| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstrap Class |
| 4 |
* |
| 5 |
* @package Authorsy |
| 6 |
*/ |
| 7 |
namespace Authorsy; |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Bootstrap class. |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
final class Bootstrap { |
| 17 |
|
| 18 |
/** |
| 19 |
* @var Bootstrap The Actual Authorsy instance |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
private static $instance; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var string $file The plugin file path. |
| 26 |
* @since 1.0.0 |
| 27 |
*/ |
| 28 |
private $file; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var string $version The plugin version. |
| 32 |
* @since 1.0.0 |
| 33 |
*/ |
| 34 |
public $version; |
| 35 |
/** |
| 36 |
* Throw Error While Trying To Clone Object |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function __clone() { |
| 42 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cloning is forbidden.', 'authorsy' ), '1.0.0' ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Disabling Un-serialization Of This Class |
| 47 |
*/ |
| 48 |
public function __wakeup() { |
| 49 |
_doing_it_wrong( __FUNCTION__, esc_html__( 'Unserializing instances of this class is forbidden.', 'authorsy' ), '1.0.0' ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* The actual authorsy instance |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* @param string $file |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public static function instantiate( $file = '' ) { |
| 60 |
|
| 61 |
// Return if already instantiated |
| 62 |
if ( self::instantiated() ) { |
| 63 |
return self::$instance; |
| 64 |
} |
| 65 |
|
| 66 |
self::prepare_instance( $file ); |
| 67 |
|
| 68 |
self::$instance->initialize_constants(); |
| 69 |
self::$instance->define_tables(); |
| 70 |
self::$instance->include_files(); |
| 71 |
self::$instance->initialize_components(); |
| 72 |
|
| 73 |
return self::$instance; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Return If The Main Class has Already Been Instantiated Or Not |
| 78 |
* |
| 79 |
* @since 1.0.0 |
| 80 |
* @return boolean |
| 81 |
*/ |
| 82 |
private static function instantiated() { |
| 83 |
if ( ( null !== self::$instance ) && ( self::$instance instanceof Bootstrap ) ) { |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Prepare Singleton Instance |
| 92 |
* |
| 93 |
* @since 1.0.0 |
| 94 |
* @param string $file |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
private static function prepare_instance( $file = '' ) { |
| 98 |
self::$instance = new self(); |
| 99 |
self::$instance->file = $file; |
| 100 |
self::$instance->version = \Authorsy::get_version(); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Assets Directory URL |
| 105 |
* |
| 106 |
* @since 1.0.0 |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public function get_assets_url() { |
| 110 |
return trailingslashit( $this->get_plugin_url() . 'assets' ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Assets Directory Path |
| 115 |
* |
| 116 |
* @since 1.0.0 |
| 117 |
* @return void |
| 118 |
*/ |
| 119 |
public function get_assets_dir() { |
| 120 |
return trailingslashit( $this->get_plugin_dir() . 'assets' ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Plugin Directory URL |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
public function get_plugin_url() { |
| 129 |
return trailingslashit( plugin_dir_url( $this->file ) ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Plugin Directory Path |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
public function get_plugin_dir() { |
| 138 |
return \Authorsy::get_plugin_dir(); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Plugin Basename |
| 143 |
* |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
public function get_plugin_basename() { |
| 147 |
return plugin_basename( $this->file ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Setup Plugin Constants |
| 152 |
* |
| 153 |
* @since 1.0.0 |
| 154 |
* @return void |
| 155 |
*/ |
| 156 |
private function initialize_constants() { |
| 157 |
// Plugin Version |
| 158 |
define( 'AUTHORSY_VERSION', \Authorsy::get_version() ); |
| 159 |
|
| 160 |
// Plugin Main File |
| 161 |
define( 'AUTHORSY_PLUGIN_FILE', $this->file ); |
| 162 |
|
| 163 |
// Plugin File Basename |
| 164 |
define( 'AUTHORSY_PLUGIN_BASE', $this->get_plugin_basename() ); |
| 165 |
|
| 166 |
// Plugin Main Directory Path |
| 167 |
define( 'AUTHORSY_PLUGIN_DIR', $this->get_plugin_dir() ); |
| 168 |
|
| 169 |
// Plugin Main Directory URL |
| 170 |
define( 'AUTHORSY_PLUGIN_URL', $this->get_plugin_url() ); |
| 171 |
|
| 172 |
// Plugin Assets Directory URL |
| 173 |
define( 'AUTHORSY_ASSETS_URL', $this->get_assets_url() ); |
| 174 |
|
| 175 |
// Plugin Assets Directory Path |
| 176 |
define( 'AUTHORSY_ASSETS_DIR', $this->get_assets_dir() ); |
| 177 |
|
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
/** |
| 184 |
* Define DB Tables Required For This Plugin |
| 185 |
* |
| 186 |
* @since 1.0.0 |
| 187 |
* @return void |
| 188 |
*/ |
| 189 |
private function define_tables() { |
| 190 |
// To Be Implemented |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Include All Required Files |
| 195 |
* |
| 196 |
* @since 1.0.0 |
| 197 |
* @return void |
| 198 |
*/ |
| 199 |
private function include_files() { |
| 200 |
|
| 201 |
/** |
| 202 |
* Core helpers. |
| 203 |
*/ |
| 204 |
require_once $this->get_plugin_dir() . 'utils/global-helper.php'; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Initialize All Components |
| 209 |
* |
| 210 |
* @since 1.0.0 |
| 211 |
* @return void |
| 212 |
*/ |
| 213 |
private function initialize_components() { |
| 214 |
|
| 215 |
// Register scripts and styles first |
| 216 |
if ( $this->is_request( 'admin' ) ) { |
| 217 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] ); |
| 218 |
} |
| 219 |
|
| 220 |
if ( $this->is_request( 'frontend' ) ) { |
| 221 |
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ) ); |
| 222 |
} |
| 223 |
|
| 224 |
// Register admin menu |
| 225 |
Core\Admin\Menu::instance()->init(); |
| 226 |
Core\Base::instance()->init(); |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
/** |
| 232 |
* Register scripts and styles for admin |
| 233 |
* |
| 234 |
* @return void |
| 235 |
*/ |
| 236 |
public function admin_scripts( ) { |
| 237 |
|
| 238 |
// get screen id. |
| 239 |
$screen = get_current_screen(); |
| 240 |
$screen_id = $screen->id; |
| 241 |
$settings = authorsy_get_settings(); |
| 242 |
$allowed_screen_ids = !empty($settings['visible_author_on']) ? $settings['visible_author_on'] : [] ; |
| 243 |
$enable_multi_author = !empty($settings['enable_multi_author']) ? $settings['enable_multi_author'] : "" ; |
| 244 |
|
| 245 |
|
| 246 |
if ( in_array( $screen_id, $allowed_screen_ids ) && $enable_multi_author) { |
| 247 |
wp_enqueue_style( 'ea-select2', plugin_dir_url( __FILE__ ) . 'assets/css/select2.min.css', [], \Authorsy::get_version(), 'all' ); |
| 248 |
wp_enqueue_script( 'ea-select2', plugin_dir_url( __FILE__ ) . 'assets/js/vendors/select2.min.js', [ 'jquery' ], \Authorsy::get_version(), true ); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
if ( 'toplevel_page_authorsy' == $screen_id ) { |
| 253 |
|
| 254 |
wp_enqueue_media(); |
| 255 |
wp_enqueue_style( 'wp-color-picker' ); |
| 256 |
wp_enqueue_style( 'authorsy-vendor', plugin_dir_url( __FILE__ ). 'assets/css/vendor.css', [], \Authorsy::get_version(), 'all' ); |
| 257 |
wp_enqueue_style( 'authorsy-admin-style', plugin_dir_url( __FILE__ ) . 'assets/css/admin.css', [], \Authorsy::get_version(), 'all' ); |
| 258 |
wp_enqueue_style( 'ea-icon-fonts', plugin_dir_url( __FILE__ ) . 'assets/fonts/icon.css', [], \Authorsy::get_version(), 'all' ); |
| 259 |
wp_enqueue_script( 'authorsy-antd-scripts', plugin_dir_url( __FILE__ ) . 'assets/js/vendors/antd.js', [ 'wp-i18n','wp-element'], \Authorsy::get_version(), true ); |
| 260 |
|
| 261 |
wp_enqueue_script( 'authorsy-dashboard-scripts', plugin_dir_url( __FILE__ ) . 'assets/js/dashboard.js', ['authorsy-antd-scripts', 'wp-color-picker' ], \Authorsy::get_version(), true ); |
| 262 |
$localize_obj = array( |
| 263 |
'site_url' => site_url(), |
| 264 |
'admin_url' => admin_url(), |
| 265 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 266 |
'isPro' => class_exists('AuthorsyPro') ? true : false |
| 267 |
); |
| 268 |
wp_localize_script( 'authorsy-dashboard-scripts', 'authorsy', $localize_obj ); |
| 269 |
wp_set_script_translations( 'authorsy-dashboard-scripts', 'authorsy', \Authorsy::get_plugin_dir() . 'languages/' ); |
| 270 |
|
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Register scripts and styles for frontend |
| 276 |
* |
| 277 |
* @return void |
| 278 |
*/ |
| 279 |
public function frontend_scripts() { |
| 280 |
wp_register_style( 'ea-icon-fonts', plugin_dir_url( __FILE__ ) . 'assets/fonts/icon.css', [], \Authorsy::get_version(), 'all' ); |
| 281 |
wp_register_style( 'ea-frontend', plugin_dir_url( __FILE__ ) . 'assets/css/frontend.css', [], \Authorsy::get_version(), 'all' ); |
| 282 |
wp_register_script( 'ea-frontend-scripts', plugin_dir_url( __FILE__ ) . 'assets/js/frontend.js', [ 'jquery', 'wp-plugins' ], \Authorsy::get_version(), true ); |
| 283 |
$localize_obj = array( |
| 284 |
'site_url' => site_url(), |
| 285 |
'admin_url' => admin_url(), |
| 286 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 287 |
'isPro' => class_exists('AuthorsyPro') ? true : false, |
| 288 |
'login' => is_user_logged_in() ? true : false, |
| 289 |
); |
| 290 |
wp_localize_script( 'ea-frontend-scripts', 'authorsy', $localize_obj ); |
| 291 |
wp_set_script_translations( 'ea-frontend-scripts', 'authorsy', \Authorsy::get_plugin_dir() . 'languages/' ); |
| 292 |
|
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* What type of request |
| 297 |
* |
| 298 |
* @param string $type admin,frontend, ajax, cron |
| 299 |
* @return boolean |
| 300 |
*/ |
| 301 |
private function is_request( $type ) { |
| 302 |
switch ( $type ) { |
| 303 |
case 'admin': |
| 304 |
return is_admin(); |
| 305 |
case 'ajax': |
| 306 |
return defined( 'DOING_AJAX' ); |
| 307 |
case 'cron': |
| 308 |
return defined( 'DOING_CRON' ); |
| 309 |
case 'frontend': |
| 310 |
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ) && ! $this->is_rest_api_request(); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Returns if the request is non-legacy REST API request. |
| 316 |
* |
| 317 |
* @return boolean |
| 318 |
*/ |
| 319 |
private function is_rest_api_request() { |
| 320 |
$server_request = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : false; |
| 321 |
|
| 322 |
if ( ! $server_request ) { |
| 323 |
return false; |
| 324 |
} |
| 325 |
|
| 326 |
$rest_prefix = trailingslashit( rest_get_url_prefix() ); |
| 327 |
$is_rest_request = ( false !== strpos( $server_request, $rest_prefix ) ); |
| 328 |
|
| 329 |
return apply_filters( 'authorsy_is_rest_api_request', $is_rest_request ); |
| 330 |
} |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Returns The Instance Of Authorsy. |
| 336 |
* The main function that is responsible for returning Authorsy instance. |
| 337 |
* |
| 338 |
* @since 1.0.0 |
| 339 |
* @return Authorsy |
| 340 |
*/ |
| 341 |
function authorsy() { |
| 342 |
return Bootstrap::instantiate(); |
| 343 |
} |
| 344 |
|