Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.46% covered (warning)
88.46%
23 / 26
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Sitemap_Buffer_Image_XMLWriter
95.83% covered (success)
95.83%
23 / 24
66.67% covered (warning)
66.67%
2 / 3
10
0.00% covered (danger)
0.00%
0 / 1
 initialize_buffer
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 start_root
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 append_item
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
6.07
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * XMLWriter implementation of the image sitemap buffer.
4 *
5 * @since 14.6
6 * @package automattic/jetpack
7 */
8
9if ( ! defined( 'ABSPATH' ) ) {
10    exit( 0 );
11}
12
13/**
14 * A buffer for constructing sitemap image xml files using XMLWriter.
15 *
16 * @since 14.6
17 */
18class Jetpack_Sitemap_Buffer_Image_XMLWriter extends Jetpack_Sitemap_Buffer_XMLWriter {
19    /**
20     * Initialize the buffer with required headers (no root element here).
21     */
22    protected function initialize_buffer() {
23        // Add generator comment
24        $this->writer->writeComment( "generator='jetpack-" . JETPACK__VERSION . "'" );
25        $this->writer->writeComment( 'Jetpack_Sitemap_Buffer_Image_XMLWriter' );
26
27        // Add stylesheet
28        foreach ( Jetpack_Sitemap_Stylist::sitemap_stylesheet_urls() as $url ) {
29            $this->writer->writePi( 'xml-stylesheet', 'type="text/css" href="' . esc_xml( $url ) . '"' );
30        }
31    }
32
33    /**
34     * Start the root element and write its namespaces.
35     */
36    protected function start_root() {
37        $this->writer->startElement( 'urlset' );
38
39        /**
40         * Filter the XML namespaces included in image sitemaps.
41         *
42         * @module sitemaps
43         *
44         * @since 4.8.0
45         *
46         * @param array $namespaces Associative array with namespaces and namespace URIs.
47         */
48        $namespaces = apply_filters(
49            'jetpack_sitemap_image_ns',
50            array(
51                'xmlns:xsi'          => 'http://www.w3.org/2001/XMLSchema-instance',
52                'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
53                'xmlns'              => 'http://www.sitemaps.org/schemas/sitemap/0.9',
54                'xmlns:image'        => 'http://www.google.com/schemas/sitemap-image/1.1',
55            )
56        );
57
58        foreach ( $namespaces as $name => $value ) {
59            $this->writer->writeAttribute( $name, $value );
60        }
61    }
62
63    /**
64     * Append a URL entry with image information to the sitemap.
65     *
66     * @param array $array The URL item to append.
67     */
68    protected function append_item( $array ) {
69        if ( ! isset( $array['url'] ) || ! is_array( $array['url'] ) ) {
70            return;
71        }
72
73        if ( isset( $array['url']['image:image'] ) ) {
74            if ( empty( $array['url']['image:image']['image:title'] ) ) {
75                unset( $array['url']['image:image']['image:title'] );
76            }
77            if ( empty( $array['url']['image:image']['image:caption'] ) ) {
78                unset( $array['url']['image:image']['image:caption'] );
79            }
80        }
81
82        $this->array_to_xml( $array );
83    }
84}