| 1 |
<?php |
| 2 |
/** |
| 3 |
* Includes the composer Autoloader used for packages and classes in the classes/ directory. |
| 4 |
* |
| 5 |
* @package ContentControl\Plugin |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ContentControl\Plugin; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Autoloader class. |
| 14 |
*/ |
| 15 |
class Autoloader { |
| 16 |
|
| 17 |
/** |
| 18 |
* Static-only class. |
| 19 |
*/ |
| 20 |
private function __construct() { |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Require the autoloader and return the result. |
| 25 |
* |
| 26 |
* If the autoloader is not present, let's log the failure and display a nice admin notice. |
| 27 |
* |
| 28 |
* @param string $name Plugin name for error messaging. |
| 29 |
* @param string $path Path to the plugin. |
| 30 |
* |
| 31 |
* @return boolean |
| 32 |
*/ |
| 33 |
public static function init( $name = '', $path = '' ) { |
| 34 |
$autoloader = $path . '/vendor/autoload.php'; |
| 35 |
|
| 36 |
if ( ! \is_readable( $autoloader ) ) { |
| 37 |
self::missing_autoloader( $name ); |
| 38 |
|
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
require_once $autoloader; |
| 43 |
|
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* If the autoloader is missing, add an admin notice. |
| 49 |
* |
| 50 |
* @param string $plugin_name Plugin name for error messaging. |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
protected static function missing_autoloader( $plugin_name = '' ) { |
| 55 |
/* translators: 1. Plugin name */ |
| 56 |
$text = __( 'Your installation of %1$s is incomplete. If you installed %1$s from GitHub, please refer to this document to set up your development environment.', 'content-control' ); |
| 57 |
|
| 58 |
$message = sprintf( $text, $plugin_name ); |
| 59 |
|
| 60 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 61 |
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 62 |
error_log( |
| 63 |
esc_html( $message ) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
add_action( |
| 68 |
'admin_notices', |
| 69 |
function () use ( $message ) { |
| 70 |
?> |
| 71 |
<div class="notice notice-error"> |
| 72 |
<p><?php echo esc_html( $message ); ?></p> |
| 73 |
</div> |
| 74 |
<?php |
| 75 |
} |
| 76 |
); |
| 77 |
} |
| 78 |
} |
| 79 |
|