-
Notifications
You must be signed in to change notification settings - Fork 0
/
BadgeosShowSub_InstallIndicator.php
194 lines (166 loc) · 8.37 KB
/
BadgeosShowSub_InstallIndicator.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/*
"WordPress Plugin Template" Copyright (C) 2015 Michael Simpson (email : [email protected])
This file is part of WordPress Plugin Template for WordPress.
Copyright (c) 2015, Michael Simpson
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of Michael Simpson nor the names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
include_once('BadgeosShowSub_OptionsManager.php');
class BadgeosShowSub_InstallIndicator extends BadgeosShowSub_OptionsManager {
const optionInstalled = '_installed';
const optionVersion = '_version';
/**
* @return bool indicating if the plugin is installed already
*/
public function isInstalled() {
return $this->getOption(self::optionInstalled) == true;
}
/**
* Note in DB that the plugin is installed
* @return null
*/
protected function markAsInstalled() {
return $this->updateOption(self::optionInstalled, true);
}
/**
* Note in DB that the plugin is uninstalled
* @return bool returned form delete_option.
* true implies the plugin was installed at the time of this call,
* false implies it was not.
*/
protected function markAsUnInstalled() {
return $this->deleteOption(self::optionInstalled);
}
/**
* Set a version string in the options. This is useful if you install upgrade and
* need to check if an older version was installed to see if you need to do certain
* upgrade housekeeping (e.g. changes to DB schema).
* @return null
*/
protected function getVersionSaved() {
return $this->getOption(self::optionVersion);
}
/**
* Set a version string in the options.
* need to check if
* @param $version string best practice: use a dot-delimited string like '1.2.3' so version strings can be easily
* compared using version_compare (http://php.net/manual/en/function.version-compare.php)
* @return null
*/
protected function setVersionSaved($version) {
return $this->updateOption(self::optionVersion, $version);
}
/**
* @return string name of the main plugin file that has the header section with
* "Plugin Name", "Version", "Description", "Text Domain", etc.
*/
protected function getMainPluginFileName() {
return basename(dirname(__FILE__)) . 'php';
}
/**
* Get a value for input key in the header section of main plugin file.
* E.g. "Plugin Name", "Version", "Description", "Text Domain", etc.
* @param $key string plugin header key
* @return string if found, otherwise null
*/
public function getPluginHeaderValue($key) {
// Read the string from the comment header of the main plugin file
$data = file_get_contents($this->getPluginDir() . DIRECTORY_SEPARATOR . $this->getMainPluginFileName());
$match = array();
preg_match('/' . $key . ':\s*(\S+)/', $data, $match);
if (count($match) >= 1) {
return $match[1];
}
return null;
}
/**
* If your subclass of this class lives in a different directory,
* override this method with the exact same code. Since __FILE__ will
* be different, you will then get the right dir returned.
* @return string
*/
protected function getPluginDir() {
return dirname(__FILE__);
}
/**
* Version of this code.
* Best practice: define version strings to be easily compared using version_compare()
* (http://php.net/manual/en/function.version-compare.php)
* NOTE: You should manually make this match the SVN tag for your main plugin file 'Version' release and 'Stable tag' in readme.txt
* @return string
*/
public function getVersion() {
return $this->getPluginHeaderValue('Version');
}
/**
* Useful when checking for upgrades, can tell if the currently installed version is earlier than the
* newly installed code. This case indicates that an upgrade has been installed and this is the first time it
* has been activated, so any upgrade actions should be taken.
* @return bool true if the version saved in the options is earlier than the version declared in getVersion().
* true indicates that new code is installed and this is the first time it is activated, so upgrade actions
* should be taken. Assumes that version string comparable by version_compare, examples: '1', '1.1', '1.1.1', '2.0', etc.
*/
public function isInstalledCodeAnUpgrade() {
return $this->isSavedVersionLessThan($this->getVersion());
}
/**
* Used to see if the installed code is an earlier version than the input version
* @param $aVersion string
* @return bool true if the saved version is earlier (by natural order) than the input version
*/
public function isSavedVersionLessThan($aVersion) {
return $this->isVersionLessThan($this->getVersionSaved(), $aVersion);
}
/**
* Used to see if the installed code is the same or earlier than the input version.
* Useful when checking for an upgrade. If you haven't specified the number of the newer version yet,
* but the last version (installed) was 2.3 (for example) you could check if
* For example, $this->isSavedVersionLessThanEqual('2.3') == true indicates that the saved version is not upgraded
* past 2.3 yet and therefore you would perform some appropriate upgrade action.
* @param $aVersion string
* @return bool true if the saved version is earlier (by natural order) than the input version
*/
public function isSavedVersionLessThanEqual($aVersion) {
return $this->isVersionLessThanEqual($this->getVersionSaved(), $aVersion);
}
/**
* @param $version1 string a version string such as '1', '1.1', '1.1.1', '2.0', etc.
* @param $version2 string a version string such as '1', '1.1', '1.1.1', '2.0', etc.
* @return bool true if version_compare of $versions1 and $version2 shows $version1 as the same or earlier
*/
public function isVersionLessThanEqual($version1, $version2) {
return (version_compare($version1, $version2) <= 0);
}
/**
* @param $version1 string a version string such as '1', '1.1', '1.1.1', '2.0', etc.
* @param $version2 string a version string such as '1', '1.1', '1.1.1', '2.0', etc.
* @return bool true if version_compare of $versions1 and $version2 shows $version1 as earlier
*/
public function isVersionLessThan($version1, $version2) {
return (version_compare($version1, $version2) < 0);
}
/**
* Record the installed version to options.
* This helps track was version is installed so when an upgrade is installed, it should call this when finished
* upgrading to record the new current version
* @return void
*/
protected function saveInstalledVersion() {
$this->setVersionSaved($this->getVersion());
}
}