/**
* Sitemaps: WP_Sitemaps_Stylesheet class
*
* This class provides the XSL stylesheets to style all sitemaps.
*
* @package WordPress
* @subpackage Sitemaps
* @since 5.5.0
*/
/**
* Stylesheet provider class.
*
* @since 5.5.0
*/
#[AllowDynamicProperties]
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 = << => ]`
*/
public function get_registered_shortcodes() {
$shortcodes = [];
/**
* Allow the registering of shortcodes into the our Tribe plugins.
*
* @since 4.12.0
*
* @var array An associative array of shortcodes in the shape `[ => ]`.
*/
$shortcodes = apply_filters( 'tribe_shortcodes', $shortcodes );
return $shortcodes;
}
/**
* Verifies if a given shortcode slug is registered for handling.
*
* @since 4.12.0
*
* @param string $slug Which slug we are checking if is registered.
*
* @return bool Whether a shortcode is registered or not.
*/
public function is_shortcode_registered( $slug ) {
$registered_shortcodes = $this->get_registered_shortcodes();
return isset( $registered_shortcodes[ $slug ] );
}
/**
* Verifies if a given shortcode class name is registered for handling.
*
* @since 4.12.0
*
* @param string $class_name Which class name we are checking if is registered.
*
* @return bool Whether a shortcode is registered, by class.
*/
public function is_shortcode_registered_by_class( $class_name ) {
$registered_shortcodes = $this->get_registered_shortcodes();
return in_array( $class_name, $registered_shortcodes );
}
/**
* Add new shortcodes handler to catch the correct strings.
*
* @since 4.12.0
*/
public function add_shortcodes() {
$registered_shortcodes = $this->get_registered_shortcodes();
// Add to WordPress all of the registered Shortcodes
foreach ( $registered_shortcodes as $shortcode => $class_name ) {
add_shortcode( $shortcode, [ $this, 'render_shortcode' ] );
}
}
/**
* Makes sure we are correctly handling the Shortcodes we manage.
*
* @since 4.12.0
*
* @param array $arguments Set of arguments passed to the Shortcode at hand.
* @param string $content Contents passed to the shortcode, inside of the open and close brackets.
* @param string $shortcode Which shortcode tag are we handling here.
*
* @return string The rendered shortcode HTML.
*/
public function render_shortcode( $arguments, $content, $shortcode ) {
$registered_shortcodes = $this->get_registered_shortcodes();
// Bail when we try to handle an unregistered shortcode (shouldn't happen)
if ( ! $this->is_shortcode_registered( $shortcode ) ) {
return false;
}
/** @var Shortcode_Interface $instance */
$instance = new $registered_shortcodes[ $shortcode ];
$instance->setup( $arguments, $content );
return $instance->get_html();
}
/**
* Filter `pre_do_shortcode_tag` to add the current shortcode.
*
* @since 4.12.9
*
* @param bool|string $return Short-circuit return value. Either false or the value to replace the shortcode with.
* @param string $tag Shortcode name.
* @param array $attr Shortcode attributes array,
* @param array $m Regular expression match array.
*
* @return bool|string Short-circuit return value.
*/
public function filter_pre_do_shortcode_tag( $return, $tag, $attr, $m ) {
if ( ! $this->is_shortcode_registered( $tag ) ) {
return $return;
}
// Add to the doing shortcode.
$this->current_shortcode[] = $tag;
return $return;
}
/**
* Filter `do_shortcode_tag` to remove the shortcode from the `$tribe_current_shortcode` list.
*
* @since 4.12.9
*
* @param string $output Shortcode output.
* @param string $tag Shortcode name.
* @param array|string $attr Shortcode attributes array or empty string.
* @param array $m Regular expression match array.
*
* @return string Shortcode output.
*/
public function filter_do_shortcode_tag( $output, $tag, $attr, $m ) {
if ( ! $this->is_shortcode_registered( $tag ) ) {
return $output;
}
if ( isset( $this->current_shortcode[ $tag ] ) ) {
unset( $this->current_shortcode[ $tag ] );
}
return $output;
}
/**
* Check if a shortcode is being done.
*
* @since 4.12.9
*
* @param null|string $tag The shortcode tag name, or null to check if doing any shortcode.
*
* @return bool If the shortcode is being done or not.
*/
public function is_doing_shortcode( $tag = null ) {
if ( null === $tag ) {
return ! empty( $this->current_shortcode );
}
return in_array( $tag, $this->current_shortcode, true );
}
}