PluginProbe ʕ •ᴥ•ʔ
XML Sitemap & Google News / trunk
XML Sitemap & Google News vtrunk
5.7.7 5.7.5 5.7.6 5.7.4 trunk 1.0 2.0 2.1 3.0 3.2 3.3 3.4 3.5 3.6.1 3.7.4 3.8.8 3.9.3 4.0.1 4.1.4 4.2.3 4.3.2 4.4.1 4.5.1 4.6.3 4.7.5 4.8.3 4.9.4 5.0.7 5.1.2 5.2.7 5.3.6 5.4.6 5.4.9 5.5.1 5.5.2 5.5.3 5.5.4 5.5.5 5.5.6 5.5.7 5.5.8 5.5.9 5.6 5.6.1 5.6.2 5.6.3 5.7 5.7.1 5.7.2 5.7.3
xml-sitemap-feed / inc / functions.php
xml-sitemap-feed / inc Last commit date
admin 1 month ago compat 1 month ago class-bwt-api-handler.php 2 months ago class-bwt-connect.php 5 months ago class-gsc-api-handler.php 2 months ago class-gsc-connect.php 2 months ago class-gsc-oauth-handler.php 5 months ago class-secret.php 5 months ago class-sitemap-core.php 6 months ago class-sitemap-news.php 1 month ago class-sitemap-plugin.php 1 month ago class-sitemap.php 1 month ago class-sitemaps-provider-custom.php 6 months ago class-sitemaps-provider-external.php 6 months ago class-sitemaps-provider-news.php 6 months ago class-xmlsitemapfeed.php 1 month ago functions-debugging.php 11 months ago functions-pluggable.php 11 months ago functions.php 1 month ago translations.php 2 months ago
functions.php
206 lines
1 <?php
2 /**
3 * Global Functions
4 *
5 * @package XML Sitemap & Google News
6 */
7
8 namespace XMLSF;
9
10 /**
11 * Sitemap loaded
12 *
13 * Common actions to prepare sitemap loading.
14 * Hooked into xmlsf_sitemap_loaded action.
15 */
16 function sitemap_loaded() {
17 // Prepare headers.
18 \add_filter( 'wp_headers', __NAMESPACE__ . '\headers' );
19
20 // Set the sitemap conditional flag.
21 \xmlsf()->is_sitemap = true;
22
23 // Make sure we have the proper locale setting for calculations.
24 \setlocale( LC_NUMERIC, 'C' );
25
26 // Save a few db queries.
27 \add_filter( 'split_the_query', '__return_false' );
28
29 // Don't go redirecting anything from now on...
30 \remove_action( 'template_redirect', 'redirect_canonical' );
31
32 /** GENERAL MISC. PREPARATIONS */
33
34 // Prevent public errors breaking xml.
35 @\ini_set( 'display_errors', 0 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.PHP.IniSet.display_errors_Disallowed
36
37 // Remove filters to prevent stuff like cdn urls for xml stylesheet and images.
38 \remove_all_filters( 'plugins_url' );
39 \remove_all_filters( 'wp_get_attachment_url' );
40 \remove_all_filters( 'image_downsize' );
41
42 // Remove actions that we do not need.
43 \remove_all_actions( 'widgets_init' );
44 \remove_all_actions( 'wp_footer' );
45 }
46
47 /**
48 * Get XML Stylesheet URL
49 *
50 * @since 5.4
51 *
52 * @param string|false $sitemap Optional sitemap name.
53 *
54 * @return string|false
55 */
56 function get_stylesheet_url( $sitemap = false ) {
57 /**
58 * GET STYLESHEET URL
59 *
60 * DEVELOPERS: a custom stylesheet file in the active (parent or child) theme /assets subdirectory, will be used when found there
61 *
62 * Must start with 'sitemap', optionally followed by another designator, separated by a hyphen.
63 * It should always end with the xsl extension.
64 *
65 * Examples:
66 * assets/sitemap.xsl
67 * assets/sitemap-root.xsl
68 * assets/sitemap-posttype.xsl
69 * assets/sitemap-taxonomy.xsl
70 * assets/sitemap-author.xsl
71 * assets/sitemap-custom.xsl
72 * assets/sitemap-news.xsl
73 * assets/sitemap-[custom_sitemap_name].xsl
74 */
75
76 $file = $sitemap ? 'assets/sitemap-' . $sitemap . '.xsl' : 'assets/sitemap.xsl';
77
78 // Find theme stylesheet file.
79 if ( \file_exists( \get_stylesheet_directory() . '/' . $file ) ) {
80 $url = \get_stylesheet_directory_uri() . '/' . $file;
81 } elseif ( \file_exists( \get_template_directory() . '/' . $file ) ) {
82 $url = \get_template_directory_uri() . '/' . $file;
83 } elseif ( \file_exists( XMLSF_DIR . '/' . $file ) ) {
84 $url = \plugins_url( $file, XMLSF_BASENAME );
85 } else {
86 $url = false;
87 }
88
89 return \apply_filters( 'xmlsf_stylesheet_url', $url );
90 }
91
92 /**
93 * Are any sitemaps enabled?
94 *
95 * @since 5.4
96 *
97 * @param string $which Which sitemap to check for. Default any sitemap.
98 *
99 * @return bool
100 */
101 function sitemaps_enabled( $which = 'any' ) {
102 $sitemaps = (array) \get_option( 'xmlsf_sitemaps', array() );
103
104 if ( 1 !== (int) \get_option( 'blog_public' ) || empty( $sitemaps ) ) {
105 $return = false;
106 } elseif ( 'any' === $which ) {
107 $return = true;
108 } else {
109 $key = 'news' === $which ? 'sitemap-news' : $which;
110 $return = \array_key_exists( $key, $sitemaps );
111 }
112
113 return \apply_filters( 'xmlsf_sitemaps_enabled', $return, $which );
114 }
115
116 /**
117 * Response headers filter
118 * Does not check if we are really in a sitemap feed.
119 *
120 * @param array $headers Response headers.
121 *
122 * @return array
123 */
124 function headers( $headers ) {
125 // Force status 200.
126 $headers['Status'] = '200';
127
128 // Set noindex.
129 $headers['X-Robots-Tag'] = 'noindex, follow';
130
131 // Force content type.
132 $headers['Content-Type'] = 'application/xml; charset=' . \get_bloginfo( 'charset' );
133
134 // And merge with nocache headers if... TODO option to avoid cache for regular sitemaps.
135 if ( \xmlsf()->is_news ) {
136 $headers = \array_merge( $headers, \wp_get_nocache_headers() );
137 }
138
139 return $headers;
140 }
141
142 /**
143 * Santize number value
144 * Expects proper locale setting for calculations: setlocale( LC_NUMERIC, 'C' );
145 *
146 * Returns a float or integer within the set limits.
147 *
148 * @since 5.2
149 *
150 * @param float|int|string $number Number value.
151 * @param float|int $min Minimum value.
152 * @param float|int $max Maximum value.
153 * @param int $decimals Formating, can be float or integer.
154 *
155 * @return float|int
156 */
157 function sanitize_number( $number, $min = .1, $max = 1, $decimals = 1 ) {
158 if ( ! is_numeric( $number ) ) {
159 return $number;
160 }
161
162 \setlocale( LC_NUMERIC, 'C' );
163
164 $number = $decimals ? \str_replace( ',', '.', $number ) : \str_replace( ',', '', $number );
165 $number = $decimals ? \floatval( $number ) : \intval( $number );
166
167 $number = \min( \max( $min, $number ), $max );
168
169 return \number_format( $number, $decimals, '.', '' );
170 }
171
172 /**
173 * Filter robots.txt rules
174 *
175 * @param string $output Default robots.txt content.
176 *
177 * @return string
178 */
179 function robots_txt( $output ) {
180 $robots = \trim( \get_option( 'xmlsf_robots', '' ) );
181 if ( $robots ) {
182 $output .= PHP_EOL . $robots . PHP_EOL;
183 }
184
185 return $output;
186 }
187
188 /**
189 * Default options
190 *
191 * @param string $key Which key to get.
192 *
193 * @return array|string|bool|null
194 */
195 function get_default_settings( $key = false ) {
196 $defaults = \xmlsf()->defaults();
197
198 if ( $key ) {
199 $return = ( isset( $defaults[ $key ] ) ) ? $defaults[ $key ] : null;
200 } else {
201 $return = $defaults;
202 }
203
204 return \apply_filters( 'xmlsf_defaults', $return, $key, $defaults );
205 }
206