CF7DBInstallIndicator

The methods in this class are used to track whether or not the plugin has been installed.

Defined (1)

The class is defined in the following location(s).

/CF7DBInstallIndicator.php  
  1. abstract class CF7DBInstallIndicator extends CF7DBOptionsManager { 
  2.  
  3. const optionInstalled = '_installed'; 
  4. const optionVersion = '_version'; 
  5.  
  6. /** 
  7. * @return bool indicating if the plugin is installed already 
  8. */ 
  9. public function isInstalled() { 
  10. return $this->getOption(self::optionInstalled) == true; 
  11.  
  12. /** 
  13. * Note in DB that the plugin is installed 
  14. * @return null 
  15. */ 
  16. protected function markAsInstalled() { 
  17. return $this->updateOption(self::optionInstalled, true); 
  18.  
  19. /** 
  20. * Note in DB that the plugin is uninstalled 
  21. * @return bool returned form delete_option. 
  22. * true implies the plugin was installed at the time of this call,  
  23. * false implies it was not. 
  24. */ 
  25. protected function markAsUnInstalled() { 
  26. return $this->deleteOption(self::optionInstalled); 
  27.  
  28. /** 
  29. * Set a version string in the options. This is useful if you install upgrade and 
  30. * need to check if an older version was installed to see if you need to do certain 
  31. * upgrade housekeeping (e.g. changes to DB schema). 
  32. * @return string|null value of the version 
  33. */ 
  34. protected function getVersionSaved() { 
  35. return $this->getOption(self::optionVersion); 
  36.  
  37. /** 
  38. * Set a version string in the options. 
  39. * need to check if 
  40. * @param $version string best practice: use a dot-delimited string like '1.2.3' so version strings can be easily 
  41. * compared using version_compare (http://php.net/manual/en/function.version-compare.php) 
  42. * @return null 
  43. */ 
  44. protected function setVersionSaved($version) { 
  45. return $this->updateOption(self::optionVersion, $version); 
  46.  
  47. /** 
  48. * @abstract 
  49. * @return string name of the main plugin file that has the header section with 
  50. * "Plugin Name", "Version", "Description", "Text Domain", etc. 
  51. */ 
  52. protected abstract function getMainPluginFileName(); 
  53.  
  54. /** 
  55. * Get a value for input key in the header section of main plugin file. 
  56. * E.g. "Plugin Name", "Version", "Description", "Text Domain", etc. 
  57. * @param $key string header value name 
  58. * @return string if found, otherwise null 
  59. */ 
  60. public function getPluginHeaderValue($key) { 
  61. // Read the string from the comment header of the main plugin file 
  62. $data = file_get_contents($this->getPluginDir() . DIRECTORY_SEPARATOR . $this->getMainPluginFileName()); 
  63. $match = array(); 
  64. preg_match('/' . $key . ':\s*(\S+)/', $data, $match); 
  65. if (count($match) >= 1) { 
  66. return $match[1]; 
  67. return null; 
  68.  
  69. /** 
  70. * If your subclass of this class lives in a different directory,  
  71. * override this method with the exact same code. Since __FILE__ will 
  72. * be different, you will then get the right dir returned.  
  73. * @return string 
  74. */ 
  75. protected function getPluginDir() { 
  76. return dirname(__FILE__); 
  77.  
  78. /** 
  79. * Version of this code. 
  80. * Best practice: define version strings to be easily compared using version_compare() 
  81. * (http://php.net/manual/en/function.version-compare.php) 
  82. * NOTE: You should manually make this match the SVN tag for your main plugin file 'Version' release and 'Stable tag' in readme.txt 
  83. * @return string 
  84. */ 
  85. public function getVersion() { 
  86. return $this->getPluginHeaderValue('Version'); 
  87.  
  88.  
  89. /** 
  90. * Useful when checking for upgrades, can tell if the currently installed version is earlier than the 
  91. * newly installed code. This case indicates that an upgrade has been installed and this is the first time it 
  92. * has been activated, so any upgrade actions should be taken. 
  93. * @return bool true if the version saved in the options is earlier than the version declared in getVersion(). 
  94. * true indicates that new code is installed and this is the first time it is activated, so upgrade actions 
  95. * should be taken. Assumes that version string comparable by version_compare, examples: '1', '1.1', '1.1.1', '2.0', etc. 
  96. */ 
  97. public function isInstalledCodeAnUpgrade() { 
  98. return $this->isSavedVersionLessThan($this->getVersion()); 
  99.  
  100. /** 
  101. * Used to see if the installed code is an earlier version than the input version 
  102. * @param $aVersion string 
  103. * @return bool true if the saved version is earlier (by natural order) than the input version 
  104. */ 
  105. public function isSavedVersionLessThan($aVersion) { 
  106. return $this->isVersionLessThan($this->getVersionSaved(), $aVersion); 
  107.  
  108. /** 
  109. * Used to see if the installed code is the same or earlier than the input version. 
  110. * Useful when checking for an upgrade. If you haven't specified the number of the newer version yet,  
  111. * but the last version (installed) was 2.3 (for example) you could check if 
  112. * For example, $this->isSavedVersionLessThanEqual('2.3') == true indicates that the saved version is not upgraded 
  113. * past 2.3 yet and therefore you would perform some appropriate upgrade action. 
  114. * @param $aVersion string 
  115. * @return bool true if the saved version is earlier (by natural order) than the input version 
  116. */ 
  117. public function isSavedVersionLessThanEqual($aVersion) { 
  118. return $this->isVersionLessThanEqual($this->getVersionSaved(), $aVersion); 
  119.  
  120. /** 
  121. * @param $version1 string a version string such as '1', '1.1', '1.1.1', '2.0', etc. 
  122. * @param $version2 string a version string such as '1', '1.1', '1.1.1', '2.0', etc. 
  123. * @return bool true if version_compare of $versions1 and $version2 shows $version1 as the same or earlier 
  124. */ 
  125. public function isVersionLessThanEqual($version1, $version2) { 
  126. return (version_compare($version1, $version2) <= 0); 
  127.  
  128. /** 
  129. * @param $version1 string a version string such as '1', '1.1', '1.1.1', '2.0', etc. 
  130. * @param $version2 string a version string such as '1', '1.1', '1.1.1', '2.0', etc. 
  131. * @return bool true if version_compare of $versions1 and $version2 shows $version1 as earlier 
  132. */ 
  133. public function isVersionLessThan($version1, $version2) { 
  134. return (version_compare($version1, $version2) < 0); 
  135.  
  136. /** 
  137. * Record the installed version to options. 
  138. * This helps track was version is installed so when an upgrade is installed, it should call this when finished 
  139. * upgrading to record the new current version 
  140. * @param $version string optional version string. If not set, uses $this->getVersion() 
  141. * @return void 
  142. */ 
  143. protected function saveInstalledVersion($version = null) { 
  144. if (!$version) { 
  145. $version = $this->getVersion(); 
  146. if ($version) { 
  147. $this->setVersionSaved($version); 
  148.