Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.29% covered (warning)
89.29%
25 / 28
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Sitemap_Buffer_Image_XMLWriter
96.15% covered (success)
96.15%
25 / 26
66.67% covered (warning)
66.67%
2 / 3
9
0.00% covered (danger)
0.00%
0 / 1
 initialize_buffer
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 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        $this->writer->writePi(
29            'xml-stylesheet',
30            'type="text/xsl" href="' . $this->finder->construct_sitemap_url( 'image-sitemap.xsl' ) . '"'
31        );
32    }
33
34    /**
35     * Start the root element and write its namespaces.
36     */
37    protected function start_root() {
38        $this->writer->startElement( 'urlset' );
39
40        /**
41         * Filter the XML namespaces included in image sitemaps.
42         *
43         * @module sitemaps
44         *
45         * @since 4.8.0
46         *
47         * @param array $namespaces Associative array with namespaces and namespace URIs.
48         */
49        $namespaces = apply_filters(
50            'jetpack_sitemap_image_ns',
51            array(
52                'xmlns:xsi'          => 'http://www.w3.org/2001/XMLSchema-instance',
53                'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
54                'xmlns'              => 'http://www.sitemaps.org/schemas/sitemap/0.9',
55                'xmlns:image'        => 'http://www.google.com/schemas/sitemap-image/1.1',
56            )
57        );
58
59        foreach ( $namespaces as $name => $value ) {
60            $this->writer->writeAttribute( $name, $value );
61        }
62    }
63
64    /**
65     * Append a URL entry with image information to the sitemap.
66     *
67     * @param array $array The URL item to append.
68     */
69    protected function append_item( $array ) {
70        if ( ! isset( $array['url'] ) || ! is_array( $array['url'] ) ) {
71            return;
72        }
73
74        if ( isset( $array['url']['image:image'] ) ) {
75            if ( empty( $array['url']['image:image']['image:title'] ) ) {
76                unset( $array['url']['image:image']['image:title'] );
77            }
78            if ( empty( $array['url']['image:image']['image:caption'] ) ) {
79                unset( $array['url']['image:image']['image:caption'] );
80            }
81        }
82
83        $this->array_to_xml( $array );
84    }
85}