Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Gallery_Settings
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 5
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 admin_init
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 core_media_widget_compat
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 wp_enqueue_media
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 print_media_templates
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Adding extra functions for the gallery.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Assets;
9
10if ( ! defined( 'ABSPATH' ) ) {
11    exit( 0 );
12}
13
14/**
15 * Renders extra controls in the Gallery Settings section of the new media UI.
16 *
17 * @phan-constructor-used-for-side-effects
18 */
19class Jetpack_Gallery_Settings {
20    /**
21     * Available gallery types.
22     *
23     * @var array
24     */
25    public $gallery_types;
26
27    /**
28     * The constructor.
29     */
30    public function __construct() {
31        add_action( 'admin_init', array( $this, 'admin_init' ) );
32    }
33
34    /**
35     * Initialize the admin resources.
36     */
37    public function admin_init() {
38        /**
39         * Filter the available gallery types.
40         *
41         * @module shortcodes, tiled-gallery
42         *
43         * @since 2.5.1
44         *
45         * @param array $value Array of the default thumbnail grid gallery type. Default array contains one key, ‘default’.
46         */
47        $this->gallery_types = apply_filters( 'jetpack_gallery_types', array( 'default' => __( 'Thumbnail Grid', 'jetpack' ) ) );
48
49        // Enqueue the media UI only if needed.
50        if ( is_countable( $this->gallery_types ) && count( $this->gallery_types ) > 1 ) {
51            add_action( 'wp_enqueue_media', array( $this, 'wp_enqueue_media' ) );
52            add_action( 'print_media_templates', array( $this, 'print_media_templates' ) );
53        }
54        // Add Slideshow and Galleries functionality to core's media gallery widget.
55        add_filter( 'widget_media_gallery_instance_schema', array( $this, 'core_media_widget_compat' ) );
56    }
57
58    /**
59     * Updates the schema of the core gallery widget so we can save the
60     * fields that we add to Gallery Widgets, like `type` and `conditions`
61     *
62     * @param array $schema The current schema for the core gallery widget.
63     * @return array  the updated schema
64     */
65    public function core_media_widget_compat( $schema ) {
66        $schema['type'] = array(
67            'type'        => 'string',
68            'enum'        => array_keys( $this->gallery_types ),
69            'description' => __( 'Type of gallery.', 'jetpack' ),
70            'default'     => 'default',
71        );
72        return $schema;
73    }
74
75    /**
76     * Registers/enqueues the gallery settings admin js.
77     */
78    public function wp_enqueue_media() {
79        if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) {
80            /**
81             * This only happens if we're not in Jetpack, but on WPCOM instead.
82             * This is the correct path for WPCOM.
83             */
84            wp_register_script(
85                'jetpack-gallery-settings',
86                Assets::get_file_url_for_environment( '_inc/build/gallery-settings.min.js', '_inc/gallery-settings.js' ),
87                array( 'jquery', 'media-views' ),
88                '20121225',
89                false
90            );
91        }
92
93        wp_enqueue_script( 'jetpack-gallery-settings' );
94    }
95
96    /**
97     * Outputs a view template which can be used with wp.media.template
98     */
99    public function print_media_templates() {
100        /**
101         * Filter the default gallery type.
102         *
103         * @module tiled-gallery
104         *
105         * @since 2.5.1
106         *
107         * @param string $value A string of the gallery type. Default is ‘default’.
108         */
109        $default_gallery_type = apply_filters( 'jetpack_default_gallery_type', 'default' );
110
111        ?>
112        <script type="text/html" id="tmpl-jetpack-gallery-settings">
113            <label class="setting">
114                <span><?php esc_html_e( 'Type', 'jetpack' ); ?></span>
115                <select class="type" name="type" data-setting="type">
116                    <?php foreach ( $this->gallery_types as $value => $caption ) : ?>
117                        <option value="<?php echo esc_attr( $value ); ?><?php selected( $value, $default_gallery_type ); ?>><?php echo esc_html( $caption ); ?></option>
118                    <?php endforeach; ?>
119                </select>
120            </label>
121        </script>
122        <?php
123    }
124}
125new Jetpack_Gallery_Settings();