Repository navigation
Expand file tree
/
Copy pathbasic-scaffold.php
More file actions
157 lines (139 loc) · 5.58 KB
/
Copy pathbasic-scaffold.php
File metadata and controls
157 lines (139 loc) · 5.58 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* MWPD Basic Plugin Scaffold.
*
* @package MWPD\BasicScaffold
* @author Alain Schlesser <alain.schlesser@gmail.com>
* @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.4
* Author: Alain Schlesser <alain.schlesser@gmail.com>
* Author URI: https://www.alainschlesser.com/
* Text Domain: mwpd-basic
* Domain Path: /languages
* License: MIT
* License URI: https://opensource.org/licenses/MIT
*/
declare( strict_types=1 );
namespace MWPD\BasicScaffold;
use MWPD\BasicScaffold\Infrastructure\Lifecycle\LifecycleScope;
/*
* 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.
*/
if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
require __DIR__ . '/vendor/autoload.php';
}
if ( ! class_exists( BasicScaffoldPluginFactory::class ) ) {
// 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 the activation
* and deactivation hooks below reach that same plugin instance through it.
*
* 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/
*
* Because the factory always returns that same shared instance, we ask it for
* the plugin wherever we need it, instead of storing it in a variable.
* WordPress includes this file in the global scope, so any variable defined
* here would become a global and could clash with WordPress core or with other
* plugins. This file therefore defines no variables at all.
*/
/*
* We register activation and deactivation hooks by using closures, as these
* need static access to work correctly.
*
* On multisite, WordPress tells them whether the plugin is activated or
* deactivated for the whole network. The flag is turned into a LifecycleScope
* right here, at the boundary, so the plugin never handles the raw value.
*
* The uninstall hook is not registered here: WordPress stores it in the
* database, so the plugin registers it once, when it is activated. The
* callback it stores is the static BasicScaffoldUninstallHook::uninstall().
*/
\register_activation_hook(
__FILE__,
/**
* Activate the plugin, for the whole network if WordPress says so.
*
* @param mixed $network_wide Whether the plugin is network-activated.
*/
static function ( $network_wide = false ): void {
BasicScaffoldPluginFactory::create()->activate_in( LifecycleScope::from_wordpress_flag( $network_wide ) );
}
);
\register_deactivation_hook(
__FILE__,
/**
* Deactivate the plugin, for the whole network if WordPress says so.
*
* @param mixed $network_wide Whether the plugin is network-deactivated.
*/
static function ( $network_wide = false ): void {
BasicScaffoldPluginFactory::create()->deactivate_in( LifecycleScope::from_wordpress_flag( $network_wide ) );
}
);
/*
* Finally, we run the plugin's register method to Hook the plugin into the
* WordPress request lifecycle.
*/
BasicScaffoldPluginFactory::create()->register();