/wp-content/plugins/google-sitemap-generator/sitemap-loader.php

https://bitbucket.org/carloskikea/helpet · PHP · 464 lines · 196 code · 64 blank · 204 comment · 37 complexity · 268ddc608a34cf119ecfb51ba2d3f88b MD5 · raw file

  1. <?php
  2. /**
  3. * Loader class for the Google Sitemap Generator
  4. *
  5. * This class takes care of the sitemap plugin and tries to load the different parts as late as possible.
  6. * On normal requests, only this small class is loaded. When the sitemap needs to be rebuild, the generator itself is loaded.
  7. * The last stage is the user interface which is loaded when the administration page is requested.
  8. *
  9. * @author Arne Brachhold
  10. * @package sitemap
  11. */
  12. class GoogleSitemapGeneratorLoader {
  13. /**
  14. * @var string Version of the generator in SVN
  15. */
  16. private static $svnVersion = '$Id: sitemap-loader.php 937300 2014-06-23 18:04:11Z arnee $';
  17. /**
  18. * Enabled the sitemap plugin with registering all required hooks
  19. *
  20. * @uses add_action Adds actions for admin menu, executing pings and handling robots.txt
  21. * @uses add_filter Adds filtes for admin menu icon and contexual help
  22. * @uses GoogleSitemapGeneratorLoader::CallShowPingResult() Shows the ping result on request
  23. */
  24. public static function Enable() {
  25. //Register the sitemap creator to wordpress...
  26. add_action('admin_menu', array(__CLASS__, 'RegisterAdminPage'));
  27. // Add a widget to the dashboard.
  28. add_action( 'wp_dashboard_setup', array(__CLASS__, 'WpDashboardSetup'));
  29. //Nice icon for Admin Menu (requires Ozh Admin Drop Down Plugin)
  30. add_filter('ozh_adminmenu_icon', array(__CLASS__, 'RegisterAdminIcon'));
  31. //Additional links on the plugin page
  32. add_filter('plugin_row_meta', array(__CLASS__, 'RegisterPluginLinks'), 10, 2);
  33. //Listen to ping request
  34. add_action('sm_ping', array(__CLASS__, 'CallSendPing'), 10, 1);
  35. //Listen to daily ping
  36. add_action('sm_ping_daily', array(__CLASS__, 'CallSendPingDaily'), 10, 1);
  37. //Post is somehow changed (also publish to publish (=edit) is fired)
  38. add_action('transition_post_status', array(__CLASS__, 'SchedulePingOnStatusChange'), 9999, 3);
  39. //Robots.txt request
  40. add_action('do_robots', array(__CLASS__, 'CallDoRobots'), 100, 0);
  41. //Help topics for context sensitive help
  42. //add_filter('contextual_help_list', array(__CLASS__, 'CallHtmlShowHelpList'), 9999, 2);
  43. //Check if the result of a ping request should be shown
  44. if(!empty($_GET["sm_ping_service"])) {
  45. self::CallShowPingResult();
  46. }
  47. //Fix rewrite rules if not already done on activation hook. This happens on network activation for example.
  48. if (get_option("sm_rewrite_done", null) != self::$svnVersion) {
  49. add_action('wp_loaded', array(__CLASS__, 'ActivateRewrite'), 9999, 1);
  50. }
  51. //Schedule daily ping
  52. if (!wp_get_schedule('sm_ping_daily')) {
  53. wp_schedule_event(time() + (60 * 60), 'daily', 'sm_ping_daily');
  54. }
  55. }
  56. /**
  57. * Sets up the query vars and template redirect hooks
  58. * @uses GoogleSitemapGeneratorLoader::RegisterQueryVars
  59. * @uses GoogleSitemapGeneratorLoader::DoTemplateRedirect
  60. * @since 4.0
  61. */
  62. public static function SetupQueryVars() {
  63. add_filter('query_vars', array(__CLASS__, 'RegisterQueryVars'), 1, 1);
  64. add_filter('template_redirect', array(__CLASS__, 'DoTemplateRedirect'), 1, 0);
  65. }
  66. /**
  67. * Register the plugin specific "xml_sitemap" query var
  68. *
  69. * @since 4.0
  70. * @param $vars Array Array of existing query_vars
  71. * @return Array An aarray containing the new query vars
  72. */
  73. public static function RegisterQueryVars($vars) {
  74. array_push($vars, 'xml_sitemap');
  75. return $vars;
  76. }
  77. /**
  78. * Registers the plugin specific rewrite rules
  79. *
  80. * Combined: sitemap(-+([a-zA-Z0-9_-]+))?\.(xml|html)(.gz)?$
  81. *
  82. * @since 4.0
  83. * @param $wpRules Array of existing rewrite rules
  84. * @return Array An array containing the new rewrite rules
  85. */
  86. public static function AddRewriteRules($wpRules) {
  87. $smRules = array(
  88. 'sitemap(-+([a-zA-Z0-9_-]+))?\.xml$' => 'index.php?xml_sitemap=params=$matches[2]',
  89. 'sitemap(-+([a-zA-Z0-9_-]+))?\.xml\.gz$' => 'index.php?xml_sitemap=params=$matches[2];zip=true',
  90. 'sitemap(-+([a-zA-Z0-9_-]+))?\.html$' => 'index.php?xml_sitemap=params=$matches[2];html=true',
  91. 'sitemap(-+([a-zA-Z0-9_-]+))?\.html.gz$' => 'index.php?xml_sitemap=params=$matches[2];html=true;zip=true'
  92. );
  93. return array_merge($smRules,$wpRules);
  94. }
  95. /**
  96. * Returns the rules required for Nginx permalinks
  97. *
  98. * @return string[]
  99. */
  100. public static function GetNginXRules() {
  101. return array(
  102. 'rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=$2" last;',
  103. 'rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml\.gz$ "/index.php?xml_sitemap=params=$2;zip=true" last;',
  104. 'rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html$ "/index.php?xml_sitemap=params=$2;html=true" last;',
  105. 'rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html.gz$ "/index.php?xml_sitemap=params=$2;html=true;zip=true" last;'
  106. );
  107. }
  108. /**
  109. * Adds the filters for wp rewrite rule adding
  110. *
  111. * @since 4.0
  112. * @uses add_filter()
  113. */
  114. public static function SetupRewriteHooks() {
  115. add_filter('rewrite_rules_array', array(__CLASS__, 'AddRewriteRules'), 1, 1);
  116. }
  117. /**
  118. * Flushes the rewrite rules
  119. *
  120. * @since 4.0
  121. * @global $wp_rewrite WP_Rewrite
  122. * @uses WP_Rewrite::flush_rules()
  123. */
  124. public static function ActivateRewrite() {
  125. /** @var $wp_rewrite WP_Rewrite */
  126. global $wp_rewrite;
  127. $wp_rewrite->flush_rules(false);
  128. update_option("sm_rewrite_done", self::$svnVersion);
  129. }
  130. /**
  131. * Handled the plugin activation on installation
  132. *
  133. * @uses GoogleSitemapGeneratorLoader::ActivateRewrite
  134. * @since 4.0
  135. */
  136. public static function ActivatePlugin() {
  137. self::SetupRewriteHooks();
  138. self::ActivateRewrite();
  139. if(self::LoadPlugin()) {
  140. $gsg = GoogleSitemapGenerator::GetInstance();
  141. if($gsg->OldFileExists()) {
  142. $gsg->DeleteOldFiles();
  143. }
  144. }
  145. }
  146. /**
  147. * Handled the plugin deactivation
  148. *
  149. * @uses GoogleSitemapGeneratorLoader::ActivateRewrite
  150. * @since 4.0
  151. */
  152. public static function DeactivatePlugin() {
  153. delete_option("sm_rewrite_done");
  154. wp_clear_scheduled_hook('sm_ping_daily');
  155. }
  156. /**
  157. * Handles the plugin output on template redirection if the xml_sitemap query var is present.
  158. *
  159. * @since 4.0
  160. */
  161. public static function DoTemplateRedirect() {
  162. /** @var $wp_query WP_Query */
  163. global $wp_query;
  164. if(!empty($wp_query->query_vars["xml_sitemap"])) {
  165. $wp_query->is_404 = false;
  166. $wp_query->is_feed = true;
  167. self::CallShowSitemap($wp_query->query_vars["xml_sitemap"]);
  168. }
  169. }
  170. /**
  171. * Registers the plugin in the admin menu system
  172. *
  173. * @uses add_options_page()
  174. */
  175. public static function RegisterAdminPage() {
  176. add_options_page(__('XML-Sitemap Generator', 'sitemap'), __('XML-Sitemap', 'sitemap'), 'administrator', self::GetBaseName(), array(__CLASS__, 'CallHtmlShowOptionsPage'));
  177. }
  178. /**
  179. * Add a widget to the dashboard.
  180. */
  181. public static function WpDashboardSetup($a) {
  182. self::LoadPlugin();
  183. $sg = GoogleSitemapGenerator::GetInstance();
  184. if ($sg->ShowSurvey()) {
  185. add_action( 'admin_notices', array(__CLASS__, 'WpDashboardAdminNotices' ) );
  186. }
  187. }
  188. public static function WpDashboardAdminNotices() {
  189. $sg = GoogleSitemapGenerator::GetInstance();
  190. $sg->HtmlSurvey();
  191. }
  192. /**
  193. * Returns a nice icon for the Ozh Admin Menu if the {@param $hook} equals to the sitemap plugin
  194. *
  195. * @param string $hook The hook to compare
  196. * @return string The path to the icon
  197. */
  198. public static function RegisterAdminIcon($hook) {
  199. if($hook == self::GetBaseName() && function_exists('plugins_url')) {
  200. return plugins_url('img/icon-arne.gif', self::GetBaseName());
  201. }
  202. return $hook;
  203. }
  204. /**
  205. * Registers additional links for the sitemap plugin on the WP plugin configuration page
  206. *
  207. * Registers the links if the $file param equals to the sitemap plugin
  208. * @param $links Array An array with the existing links
  209. * @param $file string The file to compare to
  210. * @return string[]
  211. */
  212. public static function RegisterPluginLinks($links, $file) {
  213. $base = self::GetBaseName();
  214. if($file == $base) {
  215. $links[] = '<a href="options-general.php?page=' . self::GetBaseName() . '">' . __('Settings', 'sitemap') . '</a>';
  216. $links[] = '<a href="http://www.arnebrachhold.de/redir/sitemap-plist-faq/">' . __('FAQ', 'sitemap') . '</a>';
  217. $links[] = '<a href="http://www.arnebrachhold.de/redir/sitemap-plist-support/">' . __('Support', 'sitemap') . '</a>';
  218. }
  219. return $links;
  220. }
  221. /**
  222. * @param $new_status string The new post status
  223. * @param $old_status string The old post status
  224. * @param $post WP_Post The post object
  225. */
  226. public static function SchedulePingOnStatusChange($new_status, $old_status, $post ) {
  227. if($new_status == 'publish') {
  228. set_transient('sm_ping_post_id', $post->ID, 120);
  229. wp_schedule_single_event(time() + 5, 'sm_ping');
  230. }
  231. }
  232. /**
  233. * Invokes the HtmlShowOptionsPage method of the generator
  234. * @uses GoogleSitemapGeneratorLoader::LoadPlugin()
  235. * @uses GoogleSitemapGenerator::HtmlShowOptionsPage()
  236. */
  237. public static function CallHtmlShowOptionsPage() {
  238. if(self::LoadPlugin()) {
  239. GoogleSitemapGenerator::GetInstance()->HtmlShowOptionsPage();
  240. }
  241. }
  242. /**
  243. * Invokes the ShowPingResult method of the generator
  244. * @uses GoogleSitemapGeneratorLoader::LoadPlugin()
  245. * @uses GoogleSitemapGenerator::ShowPingResult()
  246. */
  247. public static function CallShowPingResult() {
  248. if(self::LoadPlugin()) {
  249. GoogleSitemapGenerator::GetInstance()->ShowPingResult();
  250. }
  251. }
  252. /**
  253. * Invokes the SendPing method of the generator
  254. * @uses GoogleSitemapGeneratorLoader::LoadPlugin()
  255. * @uses GoogleSitemapGenerator::SendPing()
  256. */
  257. public static function CallSendPing() {
  258. if(self::LoadPlugin()) {
  259. GoogleSitemapGenerator::GetInstance()->SendPing();
  260. }
  261. }
  262. /**
  263. * Invokes the SendPingDaily method of the generator
  264. * @uses GoogleSitemapGeneratorLoader::LoadPlugin()
  265. * @uses GoogleSitemapGenerator::SendPingDaily()
  266. */
  267. public static function CallSendPingDaily()
  268. {
  269. if (self::LoadPlugin()) {
  270. GoogleSitemapGenerator::GetInstance()->SendPingDaily();
  271. }
  272. }
  273. /**
  274. * Invokes the ShowSitemap method of the generator
  275. * @uses GoogleSitemapGeneratorLoader::LoadPlugin()
  276. * @uses GoogleSitemapGenerator::ShowSitemap()
  277. */
  278. public static function CallShowSitemap($options) {
  279. if(self::LoadPlugin()) {
  280. GoogleSitemapGenerator::GetInstance()->ShowSitemap($options);
  281. }
  282. }
  283. /**
  284. * Invokes the DoRobots method of the generator
  285. * @uses GoogleSitemapGeneratorLoader::LoadPlugin()
  286. * @uses GoogleSitemapGenerator::DoRobots()
  287. */
  288. public static function CallDoRobots() {
  289. if(self::LoadPlugin()) {
  290. GoogleSitemapGenerator::GetInstance()->DoRobots();
  291. }
  292. }
  293. /**
  294. * Displays the help links in the upper Help Section of WordPress
  295. *
  296. * @return Array The new links
  297. */
  298. public static function CallHtmlShowHelpList() {
  299. $screen = get_current_screen();
  300. $id = get_plugin_page_hookname(self::GetBaseName(), 'options-general.php');
  301. if(is_object($screen) && $screen->id == $id) {
  302. /*
  303. load_plugin_textdomain('sitemap',false,dirname( plugin_basename( __FILE__ ) ) . '/lang');
  304. $links = array(
  305. __('Plugin Homepage', 'sitemap') => 'http://www.arnebrachhold.de/redir/sitemap-help-home/',
  306. __('My Sitemaps FAQ', 'sitemap') => 'http://www.arnebrachhold.de/redir/sitemap-help-faq/'
  307. );
  308. $filterVal[$id] = '';
  309. $i = 0;
  310. foreach($links AS $text => $url) {
  311. $filterVal[$id] .= '<a href="' . $url . '">' . $text . '</a>' . ($i < (count($links) - 1) ? ' | ' : '');
  312. $i++;
  313. }
  314. $screen->add_help_tab( array(
  315. 'id' => 'sitemap-links',
  316. 'title' => __('My Sitemaps FAQ', 'sitemap'),
  317. 'content' => '<p>' . __('dsf dsf sd f', 'sitemap') . '</p>',
  318. ));
  319. */
  320. }
  321. //return $filterVal;
  322. }
  323. /**
  324. * Loads the actual generator class and tries to raise the memory and time limits if not already done by WP
  325. *
  326. * @uses GoogleSitemapGenerator::Enable()
  327. * @return boolean true if run successfully
  328. */
  329. public static function LoadPlugin() {
  330. if(!class_exists("GoogleSitemapGenerator")) {
  331. $mem = abs(intval(@ini_get('memory_limit')));
  332. if($mem && $mem < 128) {
  333. @ini_set('memory_limit', '128M');
  334. }
  335. $time = abs(intval(@ini_get("max_execution_time")));
  336. if($time != 0 && $time < 120) {
  337. @set_time_limit(120);
  338. }
  339. $path = trailingslashit(dirname(__FILE__));
  340. if(!file_exists($path . 'sitemap-core.php')) return false;
  341. require_once($path . 'sitemap-core.php');
  342. }
  343. GoogleSitemapGenerator::Enable();
  344. return true;
  345. }
  346. /**
  347. * Returns the plugin basename of the plugin (using __FILE__)
  348. *
  349. * @return string The plugin basename, "sitemap" for example
  350. */
  351. public static function GetBaseName() {
  352. return plugin_basename(sm_GetInitFile());
  353. }
  354. /**
  355. * Returns the name of this loader script, using sm_GetInitFile
  356. *
  357. * @return string The sm_GetInitFile value
  358. */
  359. public static function GetPluginFile() {
  360. return sm_GetInitFile();
  361. }
  362. /**
  363. * Returns the plugin version
  364. *
  365. * Uses the WP API to get the meta data from the top of this file (comment)
  366. *
  367. * @return string The version like 3.1.1
  368. */
  369. public static function GetVersion() {
  370. if(!isset($GLOBALS["sm_version"])) {
  371. if(!function_exists('get_plugin_data')) {
  372. if(file_exists(ABSPATH . 'wp-admin/includes/plugin.php')) {
  373. require_once(ABSPATH . 'wp-admin/includes/plugin.php');
  374. }
  375. else return "0.ERROR";
  376. }
  377. $data = get_plugin_data(self::GetPluginFile(), false, false);
  378. $GLOBALS["sm_version"] = $data['Version'];
  379. }
  380. return $GLOBALS["sm_version"];
  381. }
  382. public static function GetSvnVersion() {
  383. return self::$svnVersion;
  384. }
  385. }
  386. //Enable the plugin for the init hook, but only if WP is loaded. Calling this php file directly will do nothing.
  387. if(defined('ABSPATH') && defined('WPINC')) {
  388. add_action("init", array("GoogleSitemapGeneratorLoader", "Enable"), 15, 0);
  389. register_activation_hook(sm_GetInitFile(), array('GoogleSitemapGeneratorLoader', 'ActivatePlugin'));
  390. register_deactivation_hook(sm_GetInitFile(), array('GoogleSitemapGeneratorLoader', 'DeactivatePlugin'));
  391. //Set up hooks for adding permalinks, query vars.
  392. //Don't wait until init with this, since other plugins might flush the rewrite rules in init already...
  393. GoogleSitemapGeneratorLoader::SetupQueryVars();
  394. GoogleSitemapGeneratorLoader::SetupRewriteHooks();
  395. }