plugin-update-checker.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. <?php
  2. /**
  3. * Plugin Update Checker Library 2.3.0
  4. * http://w-shadow.com/
  5. *
  6. * Copyright 2015 Janis Elsts
  7. * Released under the MIT license. See license.txt for details.
  8. */
  9. if ( !class_exists('PluginUpdateChecker_2_3', false) ):
  10. /**
  11. * A custom plugin update checker.
  12. *
  13. * @author Janis Elsts
  14. * @copyright 2015
  15. * @version 2.3
  16. * @access public
  17. */
  18. class PluginUpdateChecker_2_3 {
  19. public $metadataUrl = ''; //The URL of the plugin's metadata file.
  20. public $pluginAbsolutePath = ''; //Full path of the main plugin file.
  21. public $pluginFile = ''; //Plugin filename relative to the plugins directory. Many WP APIs use this to identify plugins.
  22. public $slug = ''; //Plugin slug.
  23. public $checkPeriod = 12; //How often to check for updates (in hours).
  24. public $optionName = ''; //Where to store the update info.
  25. public $muPluginFile = ''; //For MU plugins, the plugin filename relative to the mu-plugins directory.
  26. public $debugMode = false; //Set to TRUE to enable error reporting. Errors are raised using trigger_error()
  27. //and should be logged to the standard PHP error log.
  28. public $throttleRedundantChecks = false; //Check less often if we already know that an update is available.
  29. public $throttledCheckPeriod = 72;
  30. private $cronHook = null;
  31. private $cachedInstalledVersion = null;
  32. private $upgradedPluginFile = null; //The plugin that is currently being upgraded by WordPress.
  33. /**
  34. * Class constructor.
  35. *
  36. * @param string $metadataUrl The URL of the plugin's metadata file.
  37. * @param string $pluginFile Fully qualified path to the main plugin file.
  38. * @param string $slug The plugin's 'slug'. If not specified, the filename part of $pluginFile sans '.php' will be used as the slug.
  39. * @param integer $checkPeriod How often to check for updates (in hours). Defaults to checking every 12 hours. Set to 0 to disable automatic update checks.
  40. * @param string $optionName Where to store book-keeping info about update checks. Defaults to 'external_updates-$slug'.
  41. * @param string $muPluginFile Optional. The plugin filename relative to the mu-plugins directory.
  42. */
  43. public function __construct($metadataUrl, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = ''){
  44. $this->metadataUrl = $metadataUrl;
  45. $this->pluginAbsolutePath = $pluginFile;
  46. $this->pluginFile = plugin_basename($this->pluginAbsolutePath);
  47. $this->muPluginFile = $muPluginFile;
  48. $this->checkPeriod = $checkPeriod;
  49. $this->slug = $slug;
  50. $this->optionName = $optionName;
  51. $this->debugMode = defined('WP_DEBUG') && WP_DEBUG;
  52. //If no slug is specified, use the name of the main plugin file as the slug.
  53. //For example, 'my-cool-plugin/cool-plugin.php' becomes 'cool-plugin'.
  54. if ( empty($this->slug) ){
  55. $this->slug = basename($this->pluginFile, '.php');
  56. }
  57. if ( empty($this->optionName) ){
  58. $this->optionName = 'external_updates-' . $this->slug;
  59. }
  60. //Backwards compatibility: If the plugin is a mu-plugin but no $muPluginFile is specified, assume
  61. //it's the same as $pluginFile given that it's not in a subdirectory (WP only looks in the base dir).
  62. if ( empty($this->muPluginFile) && (strpbrk($this->pluginFile, '/\\') === false) && $this->isMuPlugin() ) {
  63. $this->muPluginFile = $this->pluginFile;
  64. }
  65. $this->installHooks();
  66. }
  67. /**
  68. * Install the hooks required to run periodic update checks and inject update info
  69. * into WP data structures.
  70. *
  71. * @return void
  72. */
  73. protected function installHooks(){
  74. //Override requests for plugin information
  75. add_filter('plugins_api', array($this, 'injectInfo'), 20, 3);
  76. //Insert our update info into the update array maintained by WP
  77. add_filter('site_transient_update_plugins', array($this,'injectUpdate')); //WP 3.0+
  78. add_filter('transient_update_plugins', array($this,'injectUpdate')); //WP 2.8+
  79. add_filter('plugin_row_meta', array($this, 'addCheckForUpdatesLink'), 10, 2);
  80. add_action('admin_init', array($this, 'handleManualCheck'));
  81. add_action('all_admin_notices', array($this, 'displayManualCheckResult'));
  82. //Clear the version number cache when something - anything - is upgraded or WP clears the update cache.
  83. add_filter('upgrader_post_install', array($this, 'clearCachedVersion'));
  84. add_action('delete_site_transient_update_plugins', array($this, 'clearCachedVersion'));
  85. //Set up the periodic update checks
  86. $this->cronHook = 'check_plugin_updates-' . $this->slug;
  87. if ( $this->checkPeriod > 0 ){
  88. //Trigger the check via Cron.
  89. //Try to use one of the default schedules if possible as it's less likely to conflict
  90. //with other plugins and their custom schedules.
  91. $defaultSchedules = array(
  92. 1 => 'hourly',
  93. 12 => 'twicedaily',
  94. 24 => 'daily',
  95. );
  96. if ( array_key_exists($this->checkPeriod, $defaultSchedules) ) {
  97. $scheduleName = $defaultSchedules[$this->checkPeriod];
  98. } else {
  99. //Use a custom cron schedule.
  100. $scheduleName = 'every' . $this->checkPeriod . 'hours';
  101. add_filter('cron_schedules', array($this, '_addCustomSchedule'));
  102. }
  103. if ( !wp_next_scheduled($this->cronHook) && !defined('WP_INSTALLING') ) {
  104. wp_schedule_event(time(), $scheduleName, $this->cronHook);
  105. }
  106. add_action($this->cronHook, array($this, 'maybeCheckForUpdates'));
  107. register_deactivation_hook($this->pluginFile, array($this, '_removeUpdaterCron'));
  108. //In case Cron is disabled or unreliable, we also manually trigger
  109. //the periodic checks while the user is browsing the Dashboard.
  110. add_action( 'admin_init', array($this, 'maybeCheckForUpdates') );
  111. //Like WordPress itself, we check more often on certain pages.
  112. /** @see wp_update_plugins */
  113. add_action('load-update-core.php', array($this, 'maybeCheckForUpdates'));
  114. add_action('load-plugins.php', array($this, 'maybeCheckForUpdates'));
  115. add_action('load-update.php', array($this, 'maybeCheckForUpdates'));
  116. //This hook fires after a bulk update is complete.
  117. add_action('upgrader_process_complete', array($this, 'maybeCheckForUpdates'), 11, 0);
  118. } else {
  119. //Periodic checks are disabled.
  120. wp_clear_scheduled_hook($this->cronHook);
  121. }
  122. //Rename the update directory to be the same as the existing directory.
  123. add_filter('upgrader_source_selection', array($this, 'fixDirectoryName'), 10, 3);
  124. //Keep track of which plugin WordPress is currently upgrading.
  125. add_filter('upgrader_pre_install', array($this, 'setUpgradedPlugin'), 10, 2);
  126. add_filter('upgrader_package_options', array($this, 'setUpgradedPluginFromOptions'), 10, 1);
  127. add_filter('upgrader_post_install', array($this, 'clearUpgradedPlugin'), 10, 1);
  128. add_action('upgrader_process_complete', array($this, 'clearUpgradedPlugin'), 10, 1);
  129. }
  130. /**
  131. * Add our custom schedule to the array of Cron schedules used by WP.
  132. *
  133. * @param array $schedules
  134. * @return array
  135. */
  136. public function _addCustomSchedule($schedules){
  137. if ( $this->checkPeriod && ($this->checkPeriod > 0) ){
  138. $scheduleName = 'every' . $this->checkPeriod . 'hours';
  139. $schedules[$scheduleName] = array(
  140. 'interval' => $this->checkPeriod * 3600,
  141. 'display' => sprintf('Every %d hours', $this->checkPeriod),
  142. );
  143. }
  144. return $schedules;
  145. }
  146. /**
  147. * Remove the scheduled cron event that the library uses to check for updates.
  148. *
  149. * @return void
  150. */
  151. public function _removeUpdaterCron(){
  152. wp_clear_scheduled_hook($this->cronHook);
  153. }
  154. /**
  155. * Get the name of the update checker's WP-cron hook. Mostly useful for debugging.
  156. *
  157. * @return string
  158. */
  159. public function getCronHookName() {
  160. return $this->cronHook;
  161. }
  162. /**
  163. * Retrieve plugin info from the configured API endpoint.
  164. *
  165. * @uses wp_remote_get()
  166. *
  167. * @param array $queryArgs Additional query arguments to append to the request. Optional.
  168. * @return PluginInfo
  169. */
  170. public function requestInfo($queryArgs = array()){
  171. //Query args to append to the URL. Plugins can add their own by using a filter callback (see addQueryArgFilter()).
  172. $installedVersion = $this->getInstalledVersion();
  173. $queryArgs['installed_version'] = ($installedVersion !== null) ? $installedVersion : '';
  174. $queryArgs = apply_filters('puc_request_info_query_args-'.$this->slug, $queryArgs);
  175. //Various options for the wp_remote_get() call. Plugins can filter these, too.
  176. $options = array(
  177. 'timeout' => 10, //seconds
  178. 'headers' => array(
  179. 'Accept' => 'application/json'
  180. ),
  181. );
  182. $options = apply_filters('puc_request_info_options-'.$this->slug, $options);
  183. //The plugin info should be at 'http://your-api.com/url/here/$slug/info.json'
  184. $url = $this->metadataUrl;
  185. if ( !empty($queryArgs) ){
  186. $url = add_query_arg($queryArgs, $url);
  187. }
  188. $result = wp_remote_get(
  189. $url,
  190. $options
  191. );
  192. //Try to parse the response
  193. $pluginInfo = null;
  194. if ( !is_wp_error($result) && isset($result['response']['code']) && ($result['response']['code'] == 200) && !empty($result['body']) ){
  195. $pluginInfo = PluginInfo_2_3::fromJson($result['body'], $this->debugMode);
  196. $pluginInfo->filename = $this->pluginFile;
  197. $pluginInfo->slug = $this->slug;
  198. } else if ( $this->debugMode ) {
  199. $message = sprintf("The URL %s does not point to a valid plugin metadata file. ", $url);
  200. if ( is_wp_error($result) ) {
  201. $message .= "WP HTTP error: " . $result->get_error_message();
  202. } else if ( isset($result['response']['code']) ) {
  203. $message .= "HTTP response code is " . $result['response']['code'] . " (expected: 200)";
  204. } else {
  205. $message .= "wp_remote_get() returned an unexpected result.";
  206. }
  207. trigger_error($message, E_USER_WARNING);
  208. }
  209. $pluginInfo = apply_filters('puc_request_info_result-'.$this->slug, $pluginInfo, $result);
  210. return $pluginInfo;
  211. }
  212. /**
  213. * Retrieve the latest update (if any) from the configured API endpoint.
  214. *
  215. * @uses PluginUpdateChecker::requestInfo()
  216. *
  217. * @return PluginUpdate An instance of PluginUpdate, or NULL when no updates are available.
  218. */
  219. public function requestUpdate(){
  220. //For the sake of simplicity, this function just calls requestInfo()
  221. //and transforms the result accordingly.
  222. $pluginInfo = $this->requestInfo(array('checking_for_updates' => '1'));
  223. if ( $pluginInfo == null ){
  224. return null;
  225. }
  226. return PluginUpdate_2_3::fromPluginInfo($pluginInfo);
  227. }
  228. /**
  229. * Get the currently installed version of the plugin.
  230. *
  231. * @return string Version number.
  232. */
  233. public function getInstalledVersion(){
  234. if ( isset($this->cachedInstalledVersion) ) {
  235. return $this->cachedInstalledVersion;
  236. }
  237. $pluginHeader = $this->getPluginHeader();
  238. if ( isset($pluginHeader['Version']) ) {
  239. $this->cachedInstalledVersion = $pluginHeader['Version'];
  240. return $pluginHeader['Version'];
  241. } else {
  242. //This can happen if the filename points to something that is not a plugin.
  243. if ( $this->debugMode ) {
  244. trigger_error(
  245. sprintf(
  246. "Can't to read the Version header for '%s'. The filename is incorrect or is not a plugin.",
  247. $this->pluginFile
  248. ),
  249. E_USER_WARNING
  250. );
  251. }
  252. return null;
  253. }
  254. }
  255. /**
  256. * Get plugin's metadata from its file header.
  257. *
  258. * @return array
  259. */
  260. protected function getPluginHeader() {
  261. if ( !is_file($this->pluginAbsolutePath) ) {
  262. //This can happen if the plugin filename is wrong.
  263. if ( $this->debugMode ) {
  264. trigger_error(
  265. sprintf(
  266. "Can't to read the plugin header for '%s'. The file does not exist.",
  267. $this->pluginFile
  268. ),
  269. E_USER_WARNING
  270. );
  271. }
  272. return array();
  273. }
  274. if ( !function_exists('get_plugin_data') ){
  275. require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  276. }
  277. return get_plugin_data($this->pluginAbsolutePath, false, false);
  278. }
  279. /**
  280. * Check for plugin updates.
  281. * The results are stored in the DB option specified in $optionName.
  282. *
  283. * @return PluginUpdate|null
  284. */
  285. public function checkForUpdates(){
  286. $installedVersion = $this->getInstalledVersion();
  287. //Fail silently if we can't find the plugin or read its header.
  288. if ( $installedVersion === null ) {
  289. if ( $this->debugMode ) {
  290. trigger_error(
  291. sprintf('Skipping update check for %s - installed version unknown.', $this->pluginFile),
  292. E_USER_WARNING
  293. );
  294. }
  295. return null;
  296. }
  297. $state = $this->getUpdateState();
  298. if ( empty($state) ){
  299. $state = new StdClass;
  300. $state->lastCheck = 0;
  301. $state->checkedVersion = '';
  302. $state->update = null;
  303. }
  304. $state->lastCheck = time();
  305. $state->checkedVersion = $installedVersion;
  306. $this->setUpdateState($state); //Save before checking in case something goes wrong
  307. $state->update = $this->requestUpdate();
  308. $this->setUpdateState($state);
  309. return $this->getUpdate();
  310. }
  311. /**
  312. * Check for updates if the configured check interval has already elapsed.
  313. * Will use a shorter check interval on certain admin pages like "Dashboard -> Updates" or when doing cron.
  314. *
  315. * You can override the default behaviour by using the "puc_check_now-$slug" filter.
  316. * The filter callback will be passed three parameters:
  317. * - Current decision. TRUE = check updates now, FALSE = don't check now.
  318. * - Last check time as a Unix timestamp.
  319. * - Configured check period in hours.
  320. * Return TRUE to check for updates immediately, or FALSE to cancel.
  321. *
  322. * This method is declared public because it's a hook callback. Calling it directly is not recommended.
  323. */
  324. public function maybeCheckForUpdates(){
  325. if ( empty($this->checkPeriod) ){
  326. return;
  327. }
  328. $currentFilter = current_filter();
  329. if ( in_array($currentFilter, array('load-update-core.php', 'upgrader_process_complete')) ) {
  330. //Check more often when the user visits "Dashboard -> Updates" or does a bulk update.
  331. $timeout = 60;
  332. } else if ( in_array($currentFilter, array('load-plugins.php', 'load-update.php')) ) {
  333. //Also check more often on the "Plugins" page and /wp-admin/update.php.
  334. $timeout = 3600;
  335. } else if ( $this->throttleRedundantChecks && ($this->getUpdate() !== null) ) {
  336. //Check less frequently if it's already known that an update is available.
  337. $timeout = $this->throttledCheckPeriod * 3600;
  338. } else if ( defined('DOING_CRON') && constant('DOING_CRON') ) {
  339. //WordPress cron schedules are not exact, so lets do an update check even
  340. //if slightly less than $checkPeriod hours have elapsed since the last check.
  341. $cronFuzziness = 20 * 60;
  342. $timeout = $this->checkPeriod * 3600 - $cronFuzziness;
  343. } else {
  344. $timeout = $this->checkPeriod * 3600;
  345. }
  346. $state = $this->getUpdateState();
  347. $shouldCheck =
  348. empty($state) ||
  349. !isset($state->lastCheck) ||
  350. ( (time() - $state->lastCheck) >= $timeout );
  351. //Let plugin authors substitute their own algorithm.
  352. $shouldCheck = apply_filters(
  353. 'puc_check_now-' . $this->slug,
  354. $shouldCheck,
  355. (!empty($state) && isset($state->lastCheck)) ? $state->lastCheck : 0,
  356. $this->checkPeriod
  357. );
  358. if ( $shouldCheck ){
  359. $this->checkForUpdates();
  360. }
  361. }
  362. /**
  363. * Load the update checker state from the DB.
  364. *
  365. * @return StdClass|null
  366. */
  367. public function getUpdateState() {
  368. $state = get_site_option($this->optionName, null);
  369. if ( empty($state) || !is_object($state)) {
  370. $state = null;
  371. }
  372. if ( !empty($state) && isset($state->update) && is_object($state->update) ){
  373. $state->update = PluginUpdate_2_3::fromObject($state->update);
  374. }
  375. return $state;
  376. }
  377. /**
  378. * Persist the update checker state to the DB.
  379. *
  380. * @param StdClass $state
  381. * @return void
  382. */
  383. private function setUpdateState($state) {
  384. if ( isset($state->update) && is_object($state->update) && method_exists($state->update, 'toStdClass') ) {
  385. $update = $state->update; /** @var PluginUpdate $update */
  386. $state->update = $update->toStdClass();
  387. }
  388. update_site_option($this->optionName, $state);
  389. }
  390. /**
  391. * Reset update checker state - i.e. last check time, cached update data and so on.
  392. *
  393. * Call this when your plugin is being uninstalled, or if you want to
  394. * clear the update cache.
  395. */
  396. public function resetUpdateState() {
  397. delete_site_option($this->optionName);
  398. }
  399. /**
  400. * Intercept plugins_api() calls that request information about our plugin and
  401. * use the configured API endpoint to satisfy them.
  402. *
  403. * @see plugins_api()
  404. *
  405. * @param mixed $result
  406. * @param string $action
  407. * @param array|object $args
  408. * @return mixed
  409. */
  410. public function injectInfo($result, $action = null, $args = null){
  411. $relevant = ($action == 'plugin_information') && isset($args->slug) && (
  412. ($args->slug == $this->slug) || ($args->slug == dirname($this->pluginFile))
  413. );
  414. if ( !$relevant ){
  415. return $result;
  416. }
  417. $pluginInfo = $this->requestInfo();
  418. $pluginInfo = apply_filters('puc_pre_inject_info-' . $this->slug, $pluginInfo);
  419. if ($pluginInfo){
  420. return $pluginInfo->toWpFormat();
  421. }
  422. return $result;
  423. }
  424. /**
  425. * Insert the latest update (if any) into the update list maintained by WP.
  426. *
  427. * @param StdClass $updates Update list.
  428. * @return StdClass Modified update list.
  429. */
  430. public function injectUpdate($updates){
  431. //Is there an update to insert?
  432. $update = $this->getUpdate();
  433. //No update notifications for mu-plugins unless explicitly enabled. The MU plugin file
  434. //is usually different from the main plugin file so the update wouldn't show up properly anyway.
  435. if ( !empty($update) && empty($this->muPluginFile) && $this->isMuPlugin() ) {
  436. $update = null;
  437. }
  438. if ( !empty($update) ) {
  439. //Let plugins filter the update info before it's passed on to WordPress.
  440. $update = apply_filters('puc_pre_inject_update-' . $this->slug, $update);
  441. if ( !is_object($updates) ) {
  442. $updates = new StdClass();
  443. $updates->response = array();
  444. }
  445. $wpUpdate = $update->toWpFormat();
  446. $pluginFile = $this->pluginFile;
  447. if ( $this->isMuPlugin() ) {
  448. //WP does not support automatic update installation for mu-plugins, but we can still display a notice.
  449. $wpUpdate->package = null;
  450. $pluginFile = $this->muPluginFile;
  451. }
  452. $updates->response[$pluginFile] = $wpUpdate;
  453. } else if ( isset($updates, $updates->response) ) {
  454. unset($updates->response[$this->pluginFile]);
  455. if ( !empty($this->muPluginFile) ) {
  456. unset($updates->response[$this->muPluginFile]);
  457. }
  458. }
  459. return $updates;
  460. }
  461. /**
  462. * Rename the update directory to match the existing plugin directory.
  463. *
  464. * When WordPress installs a plugin or theme update, it assumes that the ZIP file will contain
  465. * exactly one directory, and that the directory name will be the same as the directory where
  466. * the plugin/theme is currently installed.
  467. *
  468. * GitHub and other repositories provide ZIP downloads, but they often use directory names like
  469. * "project-branch" or "project-tag-hash". We need to change the name to the actual plugin folder.
  470. *
  471. * @param string $source The directory to copy to /wp-content/plugins. Usually a subdirectory of $remoteSource.
  472. * @param string $remoteSource WordPress has extracted the update to this directory.
  473. * @param WP_Upgrader $upgrader
  474. * @return string|WP_Error
  475. */
  476. function fixDirectoryName($source, $remoteSource, $upgrader) {
  477. global $wp_filesystem; /** @var WP_Filesystem_Base $wp_filesystem */
  478. //Basic sanity checks.
  479. if ( !isset($source, $remoteSource, $upgrader, $upgrader->skin, $wp_filesystem) ) {
  480. return $source;
  481. }
  482. //If WordPress is upgrading anything other than our plugin, leave the directory name unchanged.
  483. if ( !$this->isPluginBeingUpgraded($upgrader) ) {
  484. return $source;
  485. }
  486. //Rename the source to match the existing plugin directory.
  487. $pluginDirectoryName = dirname($this->pluginFile);
  488. if ( ($pluginDirectoryName === '.') || ($pluginDirectoryName === '/') ) {
  489. return $source;
  490. }
  491. $correctedSource = trailingslashit($remoteSource) . $pluginDirectoryName . '/';
  492. if ( $source !== $correctedSource ) {
  493. //The update archive should contain a single directory that contains the rest of plugin files. Otherwise,
  494. //WordPress will try to copy the entire working directory ($source == $remoteSource). We can't rename
  495. //$remoteSource because that would break WordPress code that cleans up temporary files after update.
  496. $sourceFiles = $wp_filesystem->dirlist($remoteSource);
  497. if ( is_array($sourceFiles) ) {
  498. $sourceFiles = array_keys($sourceFiles);
  499. $firstFilePath = trailingslashit($remoteSource) . $sourceFiles[0];
  500. if ( (count($sourceFiles) > 1) || (!$wp_filesystem->is_dir($firstFilePath)) ) {
  501. return new WP_Error(
  502. 'puc-incorrect-directory-structure',
  503. sprintf(
  504. 'The directory structure of the update is incorrect. All plugin files should be inside ' .
  505. 'a directory named <span class="code">%s</span>, not at the root of the ZIP file.',
  506. htmlentities($this->slug)
  507. )
  508. );
  509. }
  510. }
  511. $upgrader->skin->feedback(sprintf(
  512. 'Renaming %s to %s&#8230;',
  513. '<span class="code">' . basename($source) . '</span>',
  514. '<span class="code">' . $pluginDirectoryName . '</span>'
  515. ));
  516. if ( $wp_filesystem->move($source, $correctedSource, true) ) {
  517. $upgrader->skin->feedback('Plugin directory successfully renamed.');
  518. return $correctedSource;
  519. } else {
  520. return new WP_Error(
  521. 'puc-rename-failed',
  522. 'Unable to rename the update to match the existing plugin directory.'
  523. );
  524. }
  525. }
  526. return $source;
  527. }
  528. /**
  529. * Is there and update being installed RIGHT NOW, for this specific plugin?
  530. *
  531. * Caution: This method is unreliable. WordPress doesn't make it easy to figure out what it is upgrading,
  532. * and upgrader implementations are liable to change without notice.
  533. *
  534. * @param WP_Upgrader|null $upgrader The upgrader that's performing the current update.
  535. * @return bool
  536. */
  537. public function isPluginBeingUpgraded($upgrader = null) {
  538. if ( isset($upgrader) ) {
  539. $pluginFile = $this->getPluginBeingUpgradedBy($upgrader);
  540. if ( !empty($pluginFile) ) {
  541. $this->upgradedPluginFile = $pluginFile;
  542. }
  543. }
  544. return ( !empty($this->upgradedPluginFile) && ($this->upgradedPluginFile === $this->pluginFile) );
  545. }
  546. /**
  547. * Get the file name of the plugin that's currently being upgraded.
  548. *
  549. * @param Plugin_Upgrader|WP_Upgrader $upgrader
  550. * @return string|null
  551. */
  552. private function getPluginBeingUpgradedBy($upgrader) {
  553. if ( !isset($upgrader, $upgrader->skin) ) {
  554. return null;
  555. }
  556. //Figure out which plugin is being upgraded.
  557. $pluginFile = null;
  558. $skin = $upgrader->skin;
  559. if ( $skin instanceof Plugin_Upgrader_Skin ) {
  560. if ( isset($skin->plugin) && is_string($skin->plugin) && ($skin->plugin !== '') ) {
  561. $pluginFile = $skin->plugin;
  562. }
  563. } elseif ( isset($skin->plugin_info) && is_array($skin->plugin_info) ) {
  564. //This case is tricky because Bulk_Plugin_Upgrader_Skin (etc) doesn't actually store the plugin
  565. //filename anywhere. Instead, it has the plugin headers in $plugin_info. So the best we can
  566. //do is compare those headers to the headers of installed plugins.
  567. if ( !function_exists('get_plugins') ){
  568. require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  569. }
  570. $installedPlugins = get_plugins();
  571. $matches = array();
  572. foreach($installedPlugins as $pluginBasename => $headers) {
  573. $diff1 = array_diff_assoc($headers, $skin->plugin_info);
  574. $diff2 = array_diff_assoc($skin->plugin_info, $headers);
  575. if ( empty($diff1) && empty($diff2) ) {
  576. $matches[] = $pluginBasename;
  577. }
  578. }
  579. //It's possible (though very unlikely) that there could be two plugins with identical
  580. //headers. In that case, we can't unambiguously identify the plugin that's being upgraded.
  581. if ( count($matches) !== 1 ) {
  582. return null;
  583. }
  584. $pluginFile = reset($matches);
  585. }
  586. return $pluginFile;
  587. }
  588. /**
  589. * @access private
  590. *
  591. * @param mixed $input
  592. * @param array $hookExtra
  593. * @return mixed Returns $input unaltered.
  594. */
  595. public function setUpgradedPlugin($input, $hookExtra) {
  596. if (!empty($hookExtra['plugin']) && is_string($hookExtra['plugin'])) {
  597. $this->upgradedPluginFile = $hookExtra['plugin'];
  598. } else {
  599. $this->upgradedPluginFile = null;
  600. }
  601. return $input;
  602. }
  603. /**
  604. * @access private
  605. *
  606. * @param array $options
  607. * @return array
  608. */
  609. public function setUpgradedPluginFromOptions($options) {
  610. if (isset($options['hook_extra']['plugin']) && is_string($options['hook_extra']['plugin'])) {
  611. $this->upgradedPluginFile = $options['hook_extra']['plugin'];
  612. } else {
  613. $this->upgradedPluginFile = null;
  614. }
  615. return $options;
  616. }
  617. /**
  618. * @access private
  619. *
  620. * @param mixed $input
  621. * @return mixed Returns $input unaltered.
  622. */
  623. public function clearUpgradedPlugin($input = null) {
  624. $this->upgradedPluginFile = null;
  625. return $input;
  626. }
  627. /**
  628. * Get the details of the currently available update, if any.
  629. *
  630. * If no updates are available, or if the last known update version is below or equal
  631. * to the currently installed version, this method will return NULL.
  632. *
  633. * Uses cached update data. To retrieve update information straight from
  634. * the metadata URL, call requestUpdate() instead.
  635. *
  636. * @return PluginUpdate|null
  637. */
  638. public function getUpdate() {
  639. $state = $this->getUpdateState(); /** @var StdClass $state */
  640. //Is there an update available insert?
  641. if ( !empty($state) && isset($state->update) && !empty($state->update) ){
  642. $update = $state->update;
  643. //Check if the update is actually newer than the currently installed version.
  644. $installedVersion = $this->getInstalledVersion();
  645. if ( ($installedVersion !== null) && version_compare($update->version, $installedVersion, '>') ){
  646. $update->filename = $this->pluginFile;
  647. return $update;
  648. }
  649. }
  650. return null;
  651. }
  652. /**
  653. * Add a "Check for updates" link to the plugin row in the "Plugins" page. By default,
  654. * the new link will appear after the "Visit plugin site" link.
  655. *
  656. * You can change the link text by using the "puc_manual_check_link-$slug" filter.
  657. * Returning an empty string from the filter will disable the link.
  658. *
  659. * @param array $pluginMeta Array of meta links.
  660. * @param string $pluginFile
  661. * @return array
  662. */
  663. public function addCheckForUpdatesLink($pluginMeta, $pluginFile) {
  664. $isRelevant = ($pluginFile == $this->pluginFile)
  665. || (!empty($this->muPluginFile) && $pluginFile == $this->muPluginFile);
  666. if ( $isRelevant && current_user_can('update_plugins') ) {
  667. $linkUrl = wp_nonce_url(
  668. add_query_arg(
  669. array(
  670. 'puc_check_for_updates' => 1,
  671. 'puc_slug' => $this->slug,
  672. ),
  673. is_network_admin() ? network_admin_url('plugins.php') : admin_url('plugins.php')
  674. ),
  675. 'puc_check_for_updates'
  676. );
  677. $linkText = apply_filters('puc_manual_check_link-' . $this->slug, 'Check for updates');
  678. if ( !empty($linkText) ) {
  679. $pluginMeta[] = sprintf('<a href="%s">%s</a>', esc_attr($linkUrl), $linkText);
  680. }
  681. }
  682. return $pluginMeta;
  683. }
  684. /**
  685. * Check for updates when the user clicks the "Check for updates" link.
  686. * @see self::addCheckForUpdatesLink()
  687. *
  688. * @return void
  689. */
  690. public function handleManualCheck() {
  691. $shouldCheck =
  692. isset($_GET['puc_check_for_updates'], $_GET['puc_slug'])
  693. && $_GET['puc_slug'] == $this->slug
  694. && current_user_can('update_plugins')
  695. && check_admin_referer('puc_check_for_updates');
  696. if ( $shouldCheck ) {
  697. $update = $this->checkForUpdates();
  698. $status = ($update === null) ? 'no_update' : 'update_available';
  699. wp_redirect(add_query_arg(
  700. array(
  701. 'puc_update_check_result' => $status,
  702. 'puc_slug' => $this->slug,
  703. ),
  704. is_network_admin() ? network_admin_url('plugins.php') : admin_url('plugins.php')
  705. ));
  706. }
  707. }
  708. /**
  709. * Display the results of a manual update check.
  710. * @see self::handleManualCheck()
  711. *
  712. * You can change the result message by using the "puc_manual_check_message-$slug" filter.
  713. */
  714. public function displayManualCheckResult() {
  715. if ( isset($_GET['puc_update_check_result'], $_GET['puc_slug']) && ($_GET['puc_slug'] == $this->slug) ) {
  716. $status = strval($_GET['puc_update_check_result']);
  717. if ( $status == 'no_update' ) {
  718. $message = 'This plugin is up to date.';
  719. } else if ( $status == 'update_available' ) {
  720. $message = 'A new version of this plugin is available.';
  721. } else {
  722. $message = sprintf('Unknown update checker status "%s"', htmlentities($status));
  723. }
  724. printf(
  725. '<div class="updated"><p>%s</p></div>',
  726. apply_filters('puc_manual_check_message-' . $this->slug, $message, $status)
  727. );
  728. }
  729. }
  730. /**
  731. * Check if the plugin file is inside the mu-plugins directory.
  732. *
  733. * @return bool
  734. */
  735. protected function isMuPlugin() {
  736. static $cachedResult = null;
  737. if ( $cachedResult === null ) {
  738. //Convert both paths to the canonical form before comparison.
  739. $muPluginDir = realpath(WPMU_PLUGIN_DIR);
  740. $pluginPath = realpath($this->pluginAbsolutePath);
  741. $cachedResult = (strpos($pluginPath, $muPluginDir) === 0);
  742. }
  743. return $cachedResult;
  744. }
  745. /**
  746. * Clear the cached plugin version. This method can be set up as a filter (hook) and will
  747. * return the filter argument unmodified.
  748. *
  749. * @param mixed $filterArgument
  750. * @return mixed
  751. */
  752. public function clearCachedVersion($filterArgument = null) {
  753. $this->cachedInstalledVersion = null;
  754. return $filterArgument;
  755. }
  756. /**
  757. * Register a callback for filtering query arguments.
  758. *
  759. * The callback function should take one argument - an associative array of query arguments.
  760. * It should return a modified array of query arguments.
  761. *
  762. * @uses add_filter() This method is a convenience wrapper for add_filter().
  763. *
  764. * @param callable $callback
  765. * @return void
  766. */
  767. public function addQueryArgFilter($callback){
  768. add_filter('puc_request_info_query_args-'.$this->slug, $callback);
  769. }
  770. /**
  771. * Register a callback for filtering arguments passed to wp_remote_get().
  772. *
  773. * The callback function should take one argument - an associative array of arguments -
  774. * and return a modified array or arguments. See the WP documentation on wp_remote_get()
  775. * for details on what arguments are available and how they work.
  776. *
  777. * @uses add_filter() This method is a convenience wrapper for add_filter().
  778. *
  779. * @param callable $callback
  780. * @return void
  781. */
  782. public function addHttpRequestArgFilter($callback){
  783. add_filter('puc_request_info_options-'.$this->slug, $callback);
  784. }
  785. /**
  786. * Register a callback for filtering the plugin info retrieved from the external API.
  787. *
  788. * The callback function should take two arguments. If the plugin info was retrieved
  789. * successfully, the first argument passed will be an instance of PluginInfo. Otherwise,
  790. * it will be NULL. The second argument will be the corresponding return value of
  791. * wp_remote_get (see WP docs for details).
  792. *
  793. * The callback function should return a new or modified instance of PluginInfo or NULL.
  794. *
  795. * @uses add_filter() This method is a convenience wrapper for add_filter().
  796. *
  797. * @param callable $callback
  798. * @return void
  799. */
  800. public function addResultFilter($callback){
  801. add_filter('puc_request_info_result-'.$this->slug, $callback, 10, 2);
  802. }
  803. /**
  804. * Register a callback for one of the update checker filters.
  805. *
  806. * Identical to add_filter(), except it automatically adds the "puc_" prefix
  807. * and the "-$plugin_slug" suffix to the filter name. For example, "request_info_result"
  808. * becomes "puc_request_info_result-your_plugin_slug".
  809. *
  810. * @param string $tag
  811. * @param callable $callback
  812. * @param int $priority
  813. * @param int $acceptedArgs
  814. */
  815. public function addFilter($tag, $callback, $priority = 10, $acceptedArgs = 1) {
  816. add_filter('puc_' . $tag . '-' . $this->slug, $callback, $priority, $acceptedArgs);
  817. }
  818. }
  819. endif;
  820. if ( !class_exists('PluginInfo_2_3', false) ):
  821. /**
  822. * A container class for holding and transforming various plugin metadata.
  823. *
  824. * @author Janis Elsts
  825. * @copyright 2015
  826. * @version 2.3
  827. * @access public
  828. */
  829. class PluginInfo_2_3 {
  830. //Most fields map directly to the contents of the plugin's info.json file.
  831. //See the relevant docs for a description of their meaning.
  832. public $name;
  833. public $slug;
  834. public $version;
  835. public $homepage;
  836. public $sections;
  837. public $banners;
  838. public $download_url;
  839. public $author;
  840. public $author_homepage;
  841. public $requires;
  842. public $tested;
  843. public $upgrade_notice;
  844. public $rating;
  845. public $num_ratings;
  846. public $downloaded;
  847. public $active_installs;
  848. public $last_updated;
  849. public $id = 0; //The native WP.org API returns numeric plugin IDs, but they're not used for anything.
  850. public $filename; //Plugin filename relative to the plugins directory.
  851. /**
  852. * Create a new instance of PluginInfo from JSON-encoded plugin info
  853. * returned by an external update API.
  854. *
  855. * @param string $json Valid JSON string representing plugin info.
  856. * @param bool $triggerErrors
  857. * @return PluginInfo|null New instance of PluginInfo, or NULL on error.
  858. */
  859. public static function fromJson($json, $triggerErrors = false){
  860. /** @var StdClass $apiResponse */
  861. $apiResponse = json_decode($json);
  862. if ( empty($apiResponse) || !is_object($apiResponse) ){
  863. if ( $triggerErrors ) {
  864. trigger_error(
  865. "Failed to parse plugin metadata. Try validating your .json file with http://jsonlint.com/",
  866. E_USER_NOTICE
  867. );
  868. }
  869. return null;
  870. }
  871. //Very, very basic validation.
  872. $valid = isset($apiResponse->name) && !empty($apiResponse->name) && isset($apiResponse->version) && !empty($apiResponse->version);
  873. if ( !$valid ){
  874. if ( $triggerErrors ) {
  875. trigger_error(
  876. "The plugin metadata file does not contain the required 'name' and/or 'version' keys.",
  877. E_USER_NOTICE
  878. );
  879. }
  880. return null;
  881. }
  882. $info = new self();
  883. foreach(get_object_vars($apiResponse) as $key => $value){
  884. $info->$key = $value;
  885. }
  886. return $info;
  887. }
  888. /**
  889. * Transform plugin info into the format used by the native WordPress.org API
  890. *
  891. * @return object
  892. */
  893. public function toWpFormat(){
  894. $info = new StdClass;
  895. //The custom update API is built so that many fields have the same name and format
  896. //as those returned by the native WordPress.org API. These can be assigned directly.
  897. $sameFormat = array(
  898. 'name', 'slug', 'version', 'requires', 'tested', 'rating', 'upgrade_notice',
  899. 'num_ratings', 'downloaded', 'active_installs', 'homepage', 'last_updated',
  900. );
  901. foreach($sameFormat as $field){
  902. if ( isset($this->$field) ) {
  903. $info->$field = $this->$field;
  904. } else {
  905. $info->$field = null;
  906. }
  907. }
  908. //Other fields need to be renamed and/or transformed.
  909. $info->download_link = $this->download_url;
  910. if ( !empty($this->author_homepage) ){
  911. $info->author = sprintf('<a href="%s">%s</a>', $this->author_homepage, $this->author);
  912. } else {
  913. $info->author = $this->author;
  914. }
  915. if ( is_object($this->sections) ){
  916. $info->sections = get_object_vars($this->sections);
  917. } elseif ( is_array($this->sections) ) {
  918. $info->sections = $this->sections;
  919. } else {
  920. $info->sections = array('description' => '');
  921. }
  922. if ( !empty($this->banners) ) {
  923. //WP expects an array with two keys: "high" and "low". Both are optional.
  924. //Docs: https://wordpress.org/plugins/about/faq/#banners
  925. $info->banners = is_object($this->banners) ? get_object_vars($this->banners) : $this->banners;
  926. $info->banners = array_intersect_key($info->banners, array('high' => true, 'low' => true));
  927. }
  928. return $info;
  929. }
  930. }
  931. endif;
  932. if ( !class_exists('PluginUpdate_2_3', false) ):
  933. /**
  934. * A simple container class for holding information about an available update.
  935. *
  936. * @author Janis Elsts
  937. * @copyright 2015
  938. * @version 2.3
  939. * @access public
  940. */
  941. class PluginUpdate_2_3 {
  942. public $id = 0;
  943. public $slug;
  944. public $version;
  945. public $homepage;
  946. public $download_url;
  947. public $upgrade_notice;
  948. public $filename; //Plugin filename relative to the plugins directory.
  949. private static $fields = array('id', 'slug', 'version', 'homepage', 'download_url', 'upgrade_notice', 'filename');
  950. /**
  951. * Create a new instance of PluginUpdate from its JSON-encoded representation.
  952. *
  953. * @param string $json
  954. * @param bool $triggerErrors
  955. * @return PluginUpdate|null
  956. */
  957. public static function fromJson($json, $triggerErrors = false){
  958. //Since update-related information is simply a subset of the full plugin info,
  959. //we can parse the update JSON as if it was a plugin info string, then copy over
  960. //the parts that we care about.
  961. $pluginInfo = PluginInfo_2_3::fromJson($json, $triggerErrors);
  962. if ( $pluginInfo != null ) {
  963. return self::fromPluginInfo($pluginInfo);
  964. } else {
  965. return null;
  966. }
  967. }
  968. /**
  969. * Create a new instance of PluginUpdate based on an instance of PluginInfo.
  970. * Basically, this just copies a subset of fields from one object to another.
  971. *
  972. * @param PluginInfo $info
  973. * @return PluginUpdate
  974. */
  975. public static function fromPluginInfo($info){
  976. return self::fromObject($info);
  977. }
  978. /**
  979. * Create a new instance of PluginUpdate by copying the necessary fields from
  980. * another object.
  981. *
  982. * @param StdClass|PluginInfo|PluginUpdate $object The source object.
  983. * @return PluginUpdate The new copy.
  984. */
  985. public static function fromObject($object) {
  986. $update = new self();
  987. $fields = self::$fields;
  988. if (!empty($object->slug)) $fields = apply_filters('puc_retain_fields-'.$object->slug, $fields);
  989. foreach($fields as $field){
  990. if (property_exists($object, $field)) {
  991. $update->$field = $object->$field;
  992. }
  993. }
  994. return $update;
  995. }
  996. /**
  997. * Create an instance of StdClass that can later be converted back to
  998. * a PluginUpdate. Useful for serialization and caching, as it avoids
  999. * the "incomplete object" problem if the cached value is loaded before
  1000. * this class.
  1001. *
  1002. * @return StdClass
  1003. */
  1004. public function toStdClass() {
  1005. $object = new StdClass();
  1006. $fields = self::$fields;
  1007. if (!empty($this->slug)) $fields = apply_filters('puc_retain_fields-'.$this->slug, $fields);
  1008. foreach($fields as $field){
  1009. if (property_exists($this, $field)) {
  1010. $object->$field = $this->$field;
  1011. }
  1012. }
  1013. return $object;
  1014. }
  1015. /**
  1016. * Transform the update into the format used by WordPress native plugin API.
  1017. *
  1018. * @return object
  1019. */
  1020. public function toWpFormat(){
  1021. $update = new StdClass;
  1022. $update->id = $this->id;
  1023. $update->slug = $this->slug;
  1024. $update->new_version = $this->version;
  1025. $update->url = $this->homepage;
  1026. $update->package = $this->download_url;
  1027. $update->plugin = $this->filename;
  1028. if ( !empty($this->upgrade_notice) ){
  1029. $update->upgrade_notice = $this->upgrade_notice;
  1030. }
  1031. return $update;
  1032. }
  1033. }
  1034. endif;
  1035. if ( !class_exists('PucFactory', false) ):
  1036. /**
  1037. * A factory that builds instances of other classes from this library.
  1038. *
  1039. * When multiple versions of the same class have been loaded (e.g. PluginUpdateChecker 1.2
  1040. * and 1.3), this factory will always use the latest available version. Register class
  1041. * versions by calling {@link PucFactory::addVersion()}.
  1042. *
  1043. * At the moment it can only build instances of the PluginUpdateChecker class. Other classes
  1044. * are intended mainly for internal use and refer directly to specific implementations. If you
  1045. * want to instantiate one of them anyway, you can use {@link PucFactory::getLatestClassVersion()}
  1046. * to get the class name and then create it with <code>new $class(...)</code>.
  1047. */
  1048. class PucFactory {
  1049. protected static $classVersions = array();
  1050. protected static $sorted = false;
  1051. /**
  1052. * Create a new instance of PluginUpdateChecker.
  1053. *
  1054. * @see PluginUpdateChecker::__construct()
  1055. *
  1056. * @param $metadataUrl
  1057. * @param $pluginFile
  1058. * @param string $slug
  1059. * @param int $checkPeriod
  1060. * @param string $optionName
  1061. * @param string $muPluginFile
  1062. * @return PluginUpdateChecker
  1063. */
  1064. public static function buildUpdateChecker($metadataUrl, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '') {
  1065. $class = self::getLatestClassVersion('PluginUpdateChecker');
  1066. return new $class($metadataUrl, $pluginFile, $slug, $checkPeriod, $optionName, $muPluginFile);
  1067. }
  1068. /**
  1069. * Get the specific class name for the latest available version of a class.
  1070. *
  1071. * @param string $class
  1072. * @return string|null
  1073. */
  1074. public static function getLatestClassVersion($class) {
  1075. if ( !self::$sorted ) {
  1076. self::sortVersions();
  1077. }
  1078. if ( isset(self::$classVersions[$class]) ) {
  1079. return reset(self::$classVersions[$class]);
  1080. } else {
  1081. return null;
  1082. }
  1083. }
  1084. /**
  1085. * Sort available class versions in descending order (i.e. newest first).
  1086. */
  1087. protected static function sortVersions() {
  1088. foreach ( self::$classVersions as $class => $versions ) {
  1089. uksort($versions, array(__CLASS__, 'compareVersions'));
  1090. self::$classVersions[$class] = $versions;
  1091. }
  1092. self::$sorted = true;
  1093. }
  1094. protected static function compareVersions($a, $b) {
  1095. return -version_compare($a, $b);
  1096. }
  1097. /**
  1098. * Register a version of a class.
  1099. *
  1100. * @access private This method is only for internal use by the library.
  1101. *
  1102. * @param string $generalClass Class name without version numbers, e.g. 'PluginUpdateChecker'.
  1103. * @param string $versionedClass Actual class name, e.g. 'PluginUpdateChecker_1_2'.
  1104. * @param string $version Version number, e.g. '1.2'.
  1105. */
  1106. public static function addVersion($generalClass, $versionedClass, $version) {
  1107. if ( !isset(self::$classVersions[$generalClass]) ) {
  1108. self::$classVersions[$generalClass] = array();
  1109. }
  1110. self::$classVersions[$generalClass][$version] = $versionedClass;
  1111. self::$sorted = false;
  1112. }
  1113. }
  1114. endif;
  1115. //Register classes defined in this file with the factory.
  1116. PucFactory::addVersion('PluginUpdateChecker', 'PluginUpdateChecker_2_3', '2.3');
  1117. PucFactory::addVersion('PluginUpdate', 'PluginUpdate_2_3', '2.3');
  1118. PucFactory::addVersion('PluginInfo', 'PluginInfo_2_3', '2.3');
  1119. /**
  1120. * Create non-versioned variants of the update checker classes. This allows for backwards
  1121. * compatibility with versions that did not use a factory, and it simplifies doc-comments.
  1122. */
  1123. if ( !class_exists('PluginUpdateChecker', false) ) {
  1124. class PluginUpdateChecker extends PluginUpdateChecker_2_3 { }
  1125. }
  1126. if ( !class_exists('PluginUpdate', false) ) {
  1127. class PluginUpdate extends PluginUpdate_2_3 {}
  1128. }
  1129. if ( !class_exists('PluginInfo', false) ) {
  1130. class PluginInfo extends PluginInfo_2_3 {}
  1131. }