Skip to content

Commit a2579f4

Browse files
committed
Adding installing and usage instructions to the readme file
1 parent 2590794 commit a2579f4

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

README.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,129 @@
33
This is a library for protecting WordPress plugins to run twice instances at the same time.
44

55
## Installation
6+
7+
This library should be added as a composer requirement using the command:
8+
9+
```shell
10+
composer require publishpress/publishpress-instance-protection
11+
```
12+
13+
## How to use it
14+
15+
### Making sure the plugin do not break with more running instances
16+
17+
The first step, before even requiring this library, is making sure your plugin do not break if it has two or more instances
18+
running at the same time on the site.
19+
20+
This is easy to accomplish using constants defined after the plugin is loaded, and checking it before loading the plugin.
21+
If the constant is defined, we do not load the plugin again (and not even require its libraries).
22+
23+
This verification has to be out of any hook.
24+
25+
Make sure to rename the variables and constant according to your plugin. This example copy the PublishPress Authors code:
26+
27+
#### Free plugin example
28+
29+
```php
30+
if (! defined('PP_AUTHORS_LOADED')) {
31+
// include libraries, and dependencies
32+
// instantiate the plugin, hooks, etc
33+
34+
define('PP_AUTHORS_LOADED', true);
35+
}
36+
```
37+
38+
#### Pro plugin example
39+
40+
```php
41+
if (! defined('PP_AUTHORS_PRO_LOADED') && ! defined('PP_AUTHORS_LOADED')) {
42+
// include libraries, and dependencies
43+
// instantiate the plugin, hooks, etc
44+
// initialize the free plugin
45+
46+
define('PP_AUTHORS_PRO_LOADED', true);
47+
}
48+
```
49+
50+
Please note that the Pro plugin checks two constants: its own constant, and the one defined in the free plugin. This
51+
way the Pro won't run if the free is already running as a stand alone plugin.
52+
53+
### Adding the admin notices library
54+
55+
This library do not use composer's autoloader because it needs to be loaded before everything else in the plugins. But
56+
it has its own autoloader inside it, following the PSR-4 pattern.
57+
58+
You should always check if the vendor folder is on the expected path before trying to include anything from inside it.
59+
If not on the standard folder, make sure to give an option to the use to define a constant that gives the custom path of
60+
the vendor directory.
61+
62+
*Check the following example on how to include and instantiate the library, just make sure to add this code as the
63+
first thing that will be executed in your plugin, on the root context, out of any hook.*
64+
65+
#### Free Plugin example
66+
67+
```php
68+
<?php
69+
$includeFilebRelativePath = '/publishpress/publishpress-instance-protection/include.php';
70+
if (file_exists(__DIR__ . '/vendor' . $includeFilebRelativePath)) {
71+
require_once __DIR__ . '/vendor' . $includeFilebRelativePath;
72+
} else if (defined('PP_AUTHORS_VENDOR_PATH') && file_exists(PP_AUTHORS_VENDOR_PATH . $includeFilebRelativePath)) {
73+
require_once PP_AUTHORS_VENDOR_PATH . $includeFilebRelativePath;
74+
}
75+
76+
if (class_exists('PublishPressInstanceProtection\\Config')) {
77+
$pluginCheckerConfig = new PublishPressInstanceProtection\Config();
78+
$pluginCheckerConfig->pluginSlug = 'publishpress-authors';
79+
$pluginCheckerConfig->pluginName = 'PublishPress Authors';
80+
81+
$pluginChecker = new PublishPressInstanceProtection\InstanceChecker($pluginCheckerConfig);
82+
}
83+
```
84+
85+
### Pro Plugin example
86+
87+
```php
88+
<?php
89+
$includeFilebRelativePath = '/publishpress/publishpress-instance-protection/include.php';
90+
if (file_exists(__DIR__ . '/vendor' . $includeFilebRelativePath)) {
91+
require_once __DIR__ . '/vendor' . $includeFilebRelativePath;
92+
} else if (defined('PP_AUTHORS_VENDOR_PATH') && file_exists(PP_AUTHORS_VENDOR_PATH . $includeFilebRelativePath)) {
93+
require_once PP_AUTHORS_VENDOR_PATH . $includeFilebRelativePath;
94+
}
95+
96+
if (class_exists('PublishPressInstanceProtection\\Config')) {
97+
$pluginCheckerConfig = new PublishPressInstanceProtection\Config();
98+
$pluginCheckerConfig->pluginSlug = 'publishpress-authors-pro';
99+
$pluginCheckerConfig->pluginName = 'PublishPress Authors Pro';
100+
$pluginCheckerConfig->isProPlugin = true;
101+
$pluginCheckerConfig->freePluginName = 'PublishPress Authors';
102+
103+
$pluginChecker = new PublishPressInstanceProtection\InstanceChecker($pluginCheckerConfig);
104+
}
105+
```
106+
107+
The only required change is that you need to include two more configurations: `isProPlugin` and `freePluginName`.
108+
109+
### Final example
110+
111+
The final code should looks something like:
112+
113+
```php
114+
$includeFilebRelativePath = '/publishpress/publishpress-instance-protection/include.php';
115+
if (file_exists(__DIR__ . '/vendor' . $includeFilebRelativePath)) {
116+
require_once __DIR__ . '/vendor' . $includeFilebRelativePath;
117+
} else if (defined('PP_AUTHORS_VENDOR_PATH') && file_exists(PP_AUTHORS_VENDOR_PATH . $includeFilebRelativePath)) {
118+
require_once PP_AUTHORS_VENDOR_PATH . $includeFilebRelativePath;
119+
}
120+
121+
if (class_exists('PublishPressInstanceProtection\\Config')) {
122+
// ....
123+
}
124+
125+
if (! defined('PP_AUTHORS_LOADED')) {
126+
// include libraries, and dependencies
127+
// instantiate the plugin, hooks, etc
128+
129+
define('PP_AUTHORS_LOADED', true);
130+
}
131+
```

0 commit comments

Comments
 (0)