PageRenderTime 53ms CodeModel.GetById 11ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 1ms

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