-
Notifications
You must be signed in to change notification settings - Fork 11
/
basic-scaffold.php
122 lines (105 loc) · 4.2 KB
/
basic-scaffold.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* MWPD Basic Plugin Scaffold.
*
* @package MWPD\BasicScaffold
* @author Alain Schlesser <[email protected]>
* @license MIT
* @link https://www.mwpd.io/
* @copyright 2019 Alain Schlesser
*
*------------------------------------------------------------------------------
*-- 1. Provide the plugin meta information that WordPress needs. --
*------------------------------------------------------------------------------
*
* @wordpress-plugin
* Plugin Name: MWPD Basic Plugin Scaffold
* Plugin URI: https://www.mwpd.io/
* Description: Basic plugin scaffold for the "Modern WordPress Plugin Development" book,
* Version: 0.1.0
* Requires PHP: 7.2
* Author: Alain Schlesser <[email protected]>
* Author URI: https://www.alainschlesser.com/
* Text Domain: mwpd-basic
* Domain Path: /languages
* License: MIT
* License URI: https://opensource.org/licenses/MIT
*/
namespace MWPD\BasicScaffold;
/*
* This is the plugin's bootstrap file. It serves three main purposes:
* 1. Provide the plugin meta information that WordPress needs;
* 2. Prepare the environment so that it is ready to execute our OOP code;
* 3. Instantiate and kick off our "composition root" (our 'Plugin' class).
*
* The bootstrap file should not do anything else, so that we have a clean
* separation between a.) code that needs to be run sequentially and produces
* side-effects and b.) declarations that can be taken out of contexts for
* testing and reuse and have no side-effects.
*
* Anything past this bootstrap file should be autoloadable classes, interfaces
* or traits without any side-effects.
*/
/*
* As this is the only PHP file having side effects, we need to provide a
* safeguard, so we want to make sure this file is only run from within
* WordPress and cannot be directly called through a web request.
*/
if ( ! defined( 'ABSPATH' ) ) {
die();
}
/*------------------------------------------------------------------------------
*-- 2. Prepare the environment so that it is ready to execute our OOP code. --
*----------------------------------------------------------------------------*/
/*
* We try to load the Composer if it exists.
* If it doesn't exist, we fall back to a basic bundled autoloader
* implementation. This allows us to just use the plugin as-is without requirin
* the 'composer install' step.
* Note: If you use Composer not only for autoloading, but also including
* dependencies needed in production, the 'composer install' becomes mandatory
* and the fallback autoloader should probably be removed.
*/
$composer_autoloader = __DIR__ . '/vendor/autoload.php';
if ( is_readable ( $composer_autoloader ) ) {
require $composer_autoloader;
}
if ( ! class_exists( __NAMESPACE__ . '\\PluginFactory' ) ) {
// Composer autoloader apparently was not found, so fall back to our bundled
// autoloader.
require_once __DIR__ . '/src/Infrastructure/Autoloader.php';
( new Infrastructure\Autoloader() )
->add_namespace( __NAMESPACE__, __DIR__ . '/src' )
->register();
}
/*------------------------------------------------------------------------------
*-- 3. Instantiate and kick off our "composition root" (our "Plugin" class). --
*----------------------------------------------------------------------------*/
/*
* We use a factory to instantiate the actual plugin.
*
* The factory keeps the object as a shared instance, so that you can also
* get outside access to that same plugin instance through the factory.
*
* This is similar to a Singleton, but without all the drawbacks the Singleton
* anti-pattern brings along.
*
* For more information on why to avoid a Singleton,
* @see https://www.alainschlesser.com/singletons-shared-instances/
*/
$plugin = BasicScaffoldPluginFactory::create();
/*
* We register activation and deactivation hooks by using closures, as these
* need static access to work correctly.
*/
\register_activation_hook( __FILE__, function () use ( $plugin ) {
$plugin->activate();
} );
\register_deactivation_hook( __FILE__, function () use ( $plugin ) {
$plugin->deactivate();
} );
/*
* Finally, we run the plugin's register method to Hook the plugin into the
* WordPress request lifecycle.
*/
$plugin->register();