class WP_Sitemaps_Stylesheet {
/**
* Renders the XSL stylesheet depending on whether it's the sitemap index or not.
*
* @param string $type Stylesheet type. Either 'sitemap' or 'index'.
*/
public function render_stylesheet( $type ) {
header( 'Content-Type: application/xml; charset=UTF-8' );
if ( 'sitemap' === $type ) {
// All content is escaped below.
echo $this->get_sitemap_stylesheet();
}
if ( 'index' === $type ) {
// All content is escaped below.
echo $this->get_sitemap_index_stylesheet();
}
exit;
}
/**
* Returns the escaped XSL for all sitemaps, except index.
*
* @since 5.5.0
*/
public function get_sitemap_stylesheet() {
$css = $this->get_stylesheet_css();
$title = esc_xml( __( 'XML Sitemap' ) );
$description = esc_xml( __( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.' ) );
$learn_more = sprintf(
'%s',
esc_url( __( 'https://www.sitemaps.org/' ) ),
esc_xml( __( 'Learn more about XML sitemaps.' ) )
);
$text = sprintf(
/* translators: %s: Number of URLs. */
esc_xml( __( 'Number of URLs in this XML Sitemap: %s.' ) ),
''
);
$lang = get_language_attributes( 'html' );
$url = esc_xml( __( 'URL' ) );
$lastmod = esc_xml( __( 'Last Modified' ) );
$changefreq = esc_xml( __( 'Change Frequency' ) );
$priority = esc_xml( __( 'Priority' ) );
$xsl_content = <<
{$title}
{$text}
| {$url} |
{$lastmod} |
{$changefreq} |
{$priority} |
|
|
|
|
XSL;
/**
* Filters the content of the sitemap stylesheet.
*
* @since 5.5.0
*
* @param string $xsl_content Full content for the XML stylesheet.
*/
return apply_filters( 'wp_sitemaps_stylesheet_content', $xsl_content );
}
/**
* Returns the escaped XSL for the index sitemaps.
*
* @since 5.5.0
*/
public function get_sitemap_index_stylesheet() {
$css = $this->get_stylesheet_css();
$title = esc_xml( __( 'XML Sitemap' ) );
$description = esc_xml( __( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.' ) );
$learn_more = sprintf(
'%s',
esc_url( __( 'https://www.sitemaps.org/' ) ),
esc_xml( __( 'Learn more about XML sitemaps.' ) )
);
$text = sprintf(
/* translators: %s: Number of URLs. */
esc_xml( __( 'Number of URLs in this XML Sitemap: %s.' ) ),
''
);
$lang = get_language_attributes( 'html' );
$url = esc_xml( __( 'URL' ) );
$lastmod = esc_xml( __( 'Last Modified' ) );
$xsl_content = <<
{$title}
XSL;
/**
* Filters the content of the sitemap index stylesheet.
*
* @since 5.5.0
*
* @param string $xsl_content Full content for the XML stylesheet.
*/
return apply_filters( 'wp_sitemaps_stylesheet_index_content', $xsl_content );
}
/**
* Gets the CSS to be included in sitemap XSL stylesheets.
*
* @since 5.5.0
*
* @return string The CSS.
*/
public function get_stylesheet_css() {
$text_align = is_rtl() ? 'right' : 'left';
$css = <<locations = array(
'everywhere' => array(),
'frontend_only' => array(),
'admin_only' => array(),
'frontend_cl' => array(),
'on_demand' => array(),
);
}
/**
* Load the label.
*
* @return void
*/
public function load_label() {
$this->label = __( 'PHP Snippets Only', 'insert-headers-and-footers' );
}
/**
* Load the available locations.
*
* @return void
*/
public function load_locations() {
$this->locations = array(
'everywhere' => array(
'label' => esc_html__( 'Run Everywhere', 'insert-headers-and-footers' ),
'description' => esc_html__( 'Snippet gets executed everywhere on your website.', 'insert-headers-and-footers' ),
),
'frontend_only' => array(
'label' => esc_html__( 'Frontend Only', 'insert-headers-and-footers' ),
'description' => esc_html__( 'Snippet gets executed only in the frontend of the website.', 'insert-headers-and-footers' ),
),
'admin_only' => array(
'label' => esc_html__( 'Admin Only', 'insert-headers-and-footers' ),
'description' => esc_html__( 'The snippet only gets executed in the wp-admin area.', 'insert-headers-and-footers' ),
),
'frontend_cl' => array(
'label' => esc_html__( 'Frontend Conditional Logic', 'insert-headers-and-footers' ),
'description' => esc_html__( 'Ideal for running the snippet later with conditional logic rules in the frontend.', 'insert-headers-and-footers' ),
),
'on_demand' => array(
'label' => esc_html__( 'On Demand', 'insert-headers-and-footers' ),
'description' => esc_html__( 'Execute this snippet on demand or programmatically just when you need it.', 'insert-headers-and-footers' ),
),
);
}
/**
* Execute snippets.
*
* @return void
*/
public function run_snippets() {
$snippets = $this->get_snippets_for_location( 'everywhere' );
$line_reference = array();
$code = array();
$last_line = 0;
$last_snippet = null;
if ( is_admin() ) {
$snippets = array_merge( $snippets, $this->get_snippets_for_location( 'admin_only' ) );
}
// Merge all the code into 1, so we can track on which line the error occurs, if any.
foreach ( $snippets as $snippet ) {
// Use the WPCode_Snippet_Execute_Type filters here for compatibility with class even though we're skipping it for these particular locations.
$snippet_code = apply_filters( 'wpcode_snippet_output_php', $snippet->get_code(), $snippet );
$snippet_code = apply_filters( 'wpcode_snippet_output', $snippet_code, $snippet );
// Let's see how many lines the code has.
$lines = substr_count( $snippet_code, PHP_EOL );
// Let's keep a record of the start and end line of each snippet.
$last_line ++;
$line_reference[ $snippet->get_id() ] = array(
'start' => $last_line,
'end' => $last_line + $lines,
);
$last_line = $last_line + $lines;
$code[] = $snippet_code;
$last_snippet = $snippet;
}
if ( ! empty( $code ) ) {
// Implode all the code and execute it.
$code = implode( PHP_EOL, $code );
// Execute the code.
wpcode()->execute->safe_execute_php( $code, $last_snippet, $line_reference );
}
if ( ! is_admin() ) {
$snippets = $this->get_snippets_for_location( 'frontend_only' );
foreach ( $snippets as $snippet ) {
wpcode()->execute->get_snippet_output( $snippet );
}
}
}
/**
* Execute snippets on the init hook to allow using more Conditional Logic options.
*
* @return void
*/
public function run_init_snippets() {
$snippets = $this->get_snippets_for_location( 'frontend_cl' );
foreach ( $snippets as $snippet ) {
wpcode()->execute->get_snippet_output( $snippet );
}
}
/**
* Override the default hook and short-circuit any other conditions
* checks as these snippets will run everywhere.
*
* @return void
*/
protected function add_start_hook() {
add_action( 'plugins_loaded', array( $this, 'run_snippets' ), 5 );
add_action( 'wp', array( $this, 'run_init_snippets' ) );
}
}
new WPCode_Auto_Insert_Everywhere();