| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
class CJT_Framework_Extensions_Package_State_Extension { |
| 10 |
|
| 11 |
/** |
| 12 |
* |
| 13 |
*/ |
| 14 |
const INSTALLED = 3; |
| 15 |
|
| 16 |
/** |
| 17 |
* |
| 18 |
*/ |
| 19 |
const NOT_INSTALLED = 1; |
| 20 |
|
| 21 |
/** |
| 22 |
* |
| 23 |
*/ |
| 24 |
const UPGRADE = 2; |
| 25 |
|
| 26 |
/** |
| 27 |
* put your comment there... |
| 28 |
* |
| 29 |
* @var mixed |
| 30 |
*/ |
| 31 |
protected $data; |
| 32 |
|
| 33 |
/** |
| 34 |
* put your comment there... |
| 35 |
* |
| 36 |
* @var mixed |
| 37 |
*/ |
| 38 |
protected $dbOptionName; |
| 39 |
|
| 40 |
/** |
| 41 |
* put your comment there... |
| 42 |
* |
| 43 |
* @var mixed |
| 44 |
*/ |
| 45 |
protected $extDefDoc; |
| 46 |
|
| 47 |
/** |
| 48 |
* put your comment there... |
| 49 |
* |
| 50 |
* @var mixed |
| 51 |
*/ |
| 52 |
protected $name; |
| 53 |
|
| 54 |
/** |
| 55 |
* put your comment there... |
| 56 |
* |
| 57 |
* @var mixed |
| 58 |
*/ |
| 59 |
protected $newVersion; |
| 60 |
|
| 61 |
/** |
| 62 |
* put your comment there... |
| 63 |
* |
| 64 |
* @var mixed |
| 65 |
*/ |
| 66 |
protected $state; |
| 67 |
|
| 68 |
/** |
| 69 |
* put your comment there... |
| 70 |
* |
| 71 |
* @param mixed $name |
| 72 |
* @return CJT_Framework_Extensions_Package_InfoStructure |
| 73 |
*/ |
| 74 |
public function __construct(SimpleXMLElement & $extDeDoc) { |
| 75 |
# Initialize |
| 76 |
$this->extDefDoc =& $extDeDoc; |
| 77 |
$this->name = (string) $extDeDoc->attributes()->class; |
| 78 |
$this->newVersion = (string) $extDeDoc->packages->attributes()->version; |
| 79 |
# Getting DB Option name |
| 80 |
$this->dbOptionName = self::getDbOptionName($this->name); |
| 81 |
# Read from database |
| 82 |
$this->data = get_option($this->dbOptionName, array()); |
| 83 |
# Cache state |
| 84 |
$this->getState(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* put your comment there... |
| 89 |
* |
| 90 |
* @param mixed $extensionClass |
| 91 |
*/ |
| 92 |
public static function create($extensionClass) { |
| 93 |
# Option name |
| 94 |
$dbOptionName = self::getDbOptionName($extensionClass); |
| 95 |
# Reading cached state |
| 96 |
$state = get_option($dbOptionName); |
| 97 |
# Getting extension defFile |
| 98 |
$defFile = ABSPATH . PLUGINDIR . DIRECTORY_SEPARATOR . $state['defFile']; |
| 99 |
$deDoc = new SimpleXMLElement(file_get_contents($defFile)); |
| 100 |
# Returns new instance |
| 101 |
return new CJT_Framework_Extensions_Package_State_Extension($deDoc); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* put your comment there... |
| 106 |
* |
| 107 |
*/ |
| 108 |
public function clearInstallInfo() { |
| 109 |
return delete_option($this->dbOptionName); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* put your comment there... |
| 114 |
* |
| 115 |
* @param mixed $name |
| 116 |
*/ |
| 117 |
protected function getDBVar($name) { |
| 118 |
return isset($this->data[$name]) ? $this->data[$name] : null; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* put your comment there... |
| 123 |
* |
| 124 |
* @param mixed $extensionClass |
| 125 |
*/ |
| 126 |
protected static function getDbOptionName($extensionClass) { |
| 127 |
return "{$extensionClass}.state"; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* put your comment there... |
| 132 |
* |
| 133 |
*/ |
| 134 |
public function & getExtensionDeDoc() { |
| 135 |
return $this->extDefDoc; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* put your comment there... |
| 140 |
* |
| 141 |
*/ |
| 142 |
protected function getInstalledVersion() { |
| 143 |
return $this->getDBVar('version'); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* put your comment there... |
| 148 |
* |
| 149 |
*/ |
| 150 |
public function getState() { |
| 151 |
# Initialize |
| 152 |
$deDoc =& $this->getExtensionDeDoc(); |
| 153 |
$installedVersion = $this->getInstalledVersion(); |
| 154 |
$newVersion = $this->newVersion; |
| 155 |
# Check extension installation state |
| 156 |
if (!$installedVersion) { |
| 157 |
$this->state = self::NOT_INSTALLED; |
| 158 |
} |
| 159 |
elseif ($installedVersion != $newVersion) { |
| 160 |
$this->state = self::UPGRADE; |
| 161 |
} |
| 162 |
else { |
| 163 |
$this->state = self::INSTALLED; |
| 164 |
} |
| 165 |
# Retrun state |
| 166 |
return $this->state; |
| 167 |
} |
| 168 |
|
| 169 |
public static function isInstalled($extensionClass) { |
| 170 |
# Getting DB Option name |
| 171 |
$dbOptionName = self::getDbOptionName($extensionClass); |
| 172 |
# Get data |
| 173 |
$state = get_option($dbOptionName, array()); |
| 174 |
# Returns |
| 175 |
return !empty($state); |
| 176 |
} |
| 177 |
/** |
| 178 |
* put your comment there... |
| 179 |
* |
| 180 |
* @param mixed $pluginFile |
| 181 |
* @return bool |
| 182 |
*/ |
| 183 |
public function upgrade($defFile) { |
| 184 |
# Set version |
| 185 |
$this->data['version'] = $this->newVersion; |
| 186 |
# Hold path to XML file to be used when uninstalling extension data |
| 187 |
$this->data['defFile'] = $defFile; |
| 188 |
# Save |
| 189 |
return update_option($this->dbOptionName, $this->data); |
| 190 |
} |
| 191 |
|
| 192 |
} # End class |