C_Photocrati_Installer

The NextGEN Gallery C Photocrati Installer class.

Defined (1)

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

/non_pope/class.photocrati_installer.php  
  1. class C_Photocrati_Installer 
  2. static $_instance = NULL; 
  3. static function get_instance() 
  4. if (is_null(self::$_instance)) { 
  5. $klass = get_class(); 
  6. self::$_instance = new $klass(); 
  7. return self::$_instance; 
  8.  
  9.  
  10. /** 
  11. * Each product and module will register it's own handler (a class, with an install() and uninstall() method) 
  12. * to be used for install/uninstall routines 
  13. * @param $name 
  14. * @param $handler 
  15. */ 
  16. static function add_handler($name, $handler) 
  17. self::get_instance()->_installers[$name] = $handler; 
  18.  
  19. /** 
  20. * Gets an instance of an installation handler 
  21. * @param $name 
  22. * @return mixed 
  23. */ 
  24. static function get_handler_instance($name) 
  25. $installers = $handler = self::get_instance()->_installers; 
  26. if (isset($installers[$name])) { 
  27. $klass = $installers[$name]; 
  28. return new $klass; 
  29. else return NULL; 
  30.  
  31.  
  32. /** 
  33. * Uninstalls a product 
  34. * @param $product 
  35. * @param bool $hard 
  36. * @return mixed 
  37. */ 
  38. static function uninstall($product, $hard=FALSE) 
  39. $handler = self::get_handler_instance($product); 
  40. if (method_exists($handler, 'uninstall')) return $handler->uninstall($hard); 
  41.  
  42. if ($hard) { 
  43. C_NextGen_Settings::get_instance()->destroy(); 
  44. C_NextGen_Global_Settings::get_instance()->destroy(); 
  45.  
  46. static function can_do_upgrade() 
  47. $proceed = FALSE; 
  48.  
  49. // Proceed if no other process has started the installer routines 
  50. if (!($doing_upgrade = get_option('ngg_doing_upgrade', FALSE))) { 
  51. update_option('ngg_doing_upgrade', time()); 
  52. $proceed = TRUE; 
  53.  
  54. // Or, force proceeding if we have a stale ngg_doing_upgrade record 
  55. elseif ($doing_upgrade === TRUE OR time() - $doing_upgrade > 120) { 
  56. update_option('ngg_doing_upgrade', time()); 
  57. $proceed = TRUE; 
  58. return $proceed; 
  59.  
  60. static function done_upgrade() 
  61. delete_option('ngg_doing_upgrade'); 
  62.  
  63. static function update($reset=FALSE) 
  64. $local_settings = C_NextGen_Settings::get_instance(); 
  65. $global_settings = C_NextGen_Global_Settings::get_instance(); 
  66.  
  67. // Somehow some installations are missing several default settings 
  68. // Because gallerystorage_driver is essential to know we do a 'soft' reset here 
  69. // by filling in any missing options from the default settings 
  70. if (is_null($local_settings->gallerystorage_driver)) { 
  71. $settings_installer = new C_NextGen_Settings_Installer(); 
  72.  
  73. $local_settings->reset(); 
  74. $settings_installer->install_local_settings(); 
  75. $local_settings->save(); 
  76.  
  77. $global_settings->reset(); 
  78. $settings_installer->install_global_settings(); 
  79. $global_settings->save(); 
  80.  
  81. // This is a specific hack/work-around/fix and can probably be removed sometime after 2.0.20's release 
  82. // 
  83. // NextGen 2x was not multisite compatible until 2.0.18. Users that upgraded before this 
  84. // will have nearly all of their settings stored globally (network wide) in wp_sitemeta. If 
  85. // pope_module_list (which should always be a local setting) exists site-wide we wipe the current 
  86. // global ngg_options and restore from defaults. This should only ever run once. 
  87. if (is_multisite() && isset($global_settings->pope_module_list)) 
  88. // Setting this to TRUE will wipe current settings for display types, but also 
  89. // allows the display type installer to run correctly 
  90. $reset = TRUE; 
  91.  
  92. $settings_installer = new C_NextGen_Settings_Installer(); 
  93. $global_defaults = $settings_installer->get_global_defaults(); 
  94.  
  95. // Preserve the network options we honor by restoring them after calling $global_settings->reset() 
  96. $global_settings_to_keep = array(); 
  97. foreach ($global_defaults as $key => $val) { 
  98. $global_settings_to_keep[$key] = $global_settings->$key; 
  99.  
  100. // Resets internal options to an empty array 
  101. $global_settings->reset(); 
  102.  
  103. // Restore the defaults, then our saved values. This must be done again later because 
  104. // we've set $reset to TRUE. 
  105. $settings_installer->install_global_settings(); 
  106. foreach ($global_settings_to_keep as $key => $val) { 
  107. $global_settings->$key = $val; 
  108.  
  109. // Get last module list and current module list. Compare... 
  110. $last_module_list = self::_get_last_module_list($reset); 
  111. $current_module_list = self::_generate_module_info(); 
  112. $diff = array_diff($current_module_list, $last_module_list); 
  113. $do_upgrade = (count($diff)>0 || count($last_module_list) != count($current_module_list)); 
  114. $can_upgrade = $do_upgrade ? self::can_do_upgrade() : FALSE; 
  115. if ($can_upgrade && !$diff) $diff = $current_module_list; 
  116.  
  117. if ($can_upgrade && $do_upgrade) { 
  118.  
  119. // Clear APC cache 
  120. if (function_exists('apc_clear_cache')) { 
  121. @apc_clear_cache('opcode'); 
  122. apc_clear_cache(); 
  123.  
  124. // We flush ALL transients 
  125. wp_cache_flush(); 
  126. global $wpdb; 
  127. $wpdb->query("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient%'"); 
  128.  
  129. // Remove all NGG created cron jobs 
  130. self::refresh_cron(); 
  131.  
  132. // Delete auto-update cache 
  133. update_option('photocrati_auto_update_admin_update_list', null); 
  134. update_option('photocrati_auto_update_admin_check_date', ''); 
  135.  
  136. // Other Pope applications might be loaded, and therefore 
  137. // all singletons should be destroyed, so that they can be 
  138. // adapted as necessary. For now, we'll just assume that the factory 
  139. // is the only singleton that will be used by other Pope applications 
  140. C_Component_Factory::$_instances = array(); 
  141.  
  142. foreach ($diff as $module_name) { 
  143. $parts = explode('|', $module_name); 
  144. if (($handler = self::get_handler_instance(array_shift($parts)))) { 
  145. if (method_exists($handler, 'install')) 
  146. $handler->install($reset); 
  147.  
  148. // NOTE & TODO: if the above section that declares $global_settings_to_keep is removed this should also 
  149. // Since a hard-reset of the settings was forced we must again re-apply our previously saved values 
  150. if (isset($global_settings_to_keep)) { 
  151. foreach ($global_settings_to_keep as $key => $val) { 
  152. $global_settings->$key = $val; 
  153.  
  154. // Save any changes settings 
  155. $global_settings->save(); 
  156. $local_settings->save(); 
  157.  
  158. // Another workaround to an issue caused by NextGen's lack of multisite compatibility. It's possible 
  159. // the string substitation wasn't performed, so if a '%' symbol exists in gallerypath we reset it. It's 
  160. // another db call, but again this should only ever run once. 
  161. // 
  162. // Remove this when removing the above reset-global-settings code 
  163. if (strpos($local_settings->gallerypath, '%')) 
  164. $settings_installer = new C_NextGen_Settings_Installer(); 
  165. $local_settings->gallerypath = $settings_installer->gallerypath_replace($global_settings->gallerypath); 
  166. $local_settings->save(); 
  167.  
  168. // Update the module list, and remove the update flag 
  169. if ($can_upgrade) { 
  170. update_option('pope_module_list', $current_module_list); 
  171. self::done_upgrade(); 
  172.  
  173. static function _get_last_module_list($reset=FALSE) 
  174. // Return empty array to reset 
  175. if ($reset) return array(); 
  176.  
  177. // First try getting the list from a single WP option, "pope_module_list" 
  178. $retval = get_option('pope_module_list', array()); 
  179. if (!$retval) { 
  180. $local_settings = C_NextGen_Settings::get_instance(); 
  181. $retval = $local_settings->get('pope_module_list', array()); 
  182. $local_settings->delete('pope_module_list'); 
  183.  
  184. return $retval; 
  185.  
  186. static function _generate_module_info() 
  187. $retval = array(); 
  188. $registry = C_Component_Registry::get_instance(); 
  189. $products = array('photocrati-nextgen'); 
  190. foreach ($registry->get_product_list() as $product_id) { 
  191. if ($product_id != 'photocrati-nextgen') $products[] = $product_id; 
  192.  
  193. foreach ($products as $product_id) { 
  194. foreach ($registry->get_module_list($product_id) as $module_id) { 
  195. if (($module = $registry->get_module($module_id))) { 
  196. $module_version = $module->module_version; 
  197. $module_string = "{$module_id}|{$module_version}"; 
  198. if (!in_array($module_string, $retval)) $retval[] = $module_string; 
  199.  
  200. return $retval; 
  201.  
  202. static function refresh_cron() 
  203. if (!extension_loaded('suhosin')) @ini_set('memory_limit', -1); 
  204.  
  205. // Remove all cron jobs created by NextGEN Gallery 
  206. $cron = _get_cron_array(); 
  207. if (is_array($cron)) { 
  208. foreach ($cron as $timestamp => $job) { 
  209. if (is_array($job)) { 
  210. unset($cron[$timestamp]['ngg_delete_expired_transients']); 
  211. if (empty($cron[$timestamp])) { 
  212. unset($cron[$timestamp]); 
  213. _set_cron_array($cron);