Completed
Push — dna-wp-replicastore ( 5ba3d9...70bb0d )
by
unknown
322:11 queued 313:37
created

Jetpack_Gallery_Settings::print_media_templates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Renders extra controls in the Gallery Settings section of the new media UI.
5
 */
6
class Jetpack_Gallery_Settings {
7
	function __construct() {
8
		add_action( 'admin_init', array( $this, 'admin_init' ) );
9
	}
10
11
	function admin_init() {
12
		/**
13
		 * Filter the available gallery types.
14
		 *
15
		 * @module shortcodes, tiled-gallery
16
		 *
17
		 * @since 2.5.1
18
		 *
19
		 * @param array $value Array of the default thumbnail grid gallery type. Default array contains one key, ‘default’.
20
		 *
21
		 */
22
		$this->gallery_types = apply_filters( 'jetpack_gallery_types', array( 'default' => __( 'Thumbnail Grid', 'jetpack' ) ) );
0 ignored issues
show
Bug introduced by
The property gallery_types does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
24
		// Enqueue the media UI only if needed.
25
		if ( count( $this->gallery_types ) > 1 ) {
26
			add_action( 'wp_enqueue_media', array( $this, 'wp_enqueue_media' ) );
27
			add_action( 'print_media_templates', array( $this, 'print_media_templates' ) );
28
		}
29
		// Add Slideshow and Galleries functionality to core's media gallery widget.
30
		add_filter( 'widget_media_gallery_instance_schema', array( $this, 'core_media_widget_compat' ) );
31
	}
32
33
	/**
34
	 * Updates the schema of the core gallery widget so we can save the
35
	 * fields that we add to Gallery Widgets, like `type` and `conditions`
36
	 *
37
	 * @param $schema The current schema for the core gallery widget
38
	 *
39
	 * @return array  the updated schema
40
	 */
41
	public function core_media_widget_compat( $schema ) {
42
		$schema['type'] = array(
43
			'type' => 'string',
44
			'enum' => array_keys( $this->gallery_types ),
45
			'description' => __( 'Type of gallery.', 'jetpack' ),
46
			'default' => 'default',
47
		);
48
		return $schema;
49
	}
50
51
	/**
52
	 * Registers/enqueues the gallery settings admin js.
53
	 */
54
	function wp_enqueue_media() {
55 View Code Duplication
		if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) {
56
			/**
57
			 * This only happens if we're not in Jetpack, but on WPCOM instead.
58
			 * This is the correct path for WPCOM.
59
			 */
60
			wp_register_script(
61
				'jetpack-gallery-settings',
62
				Jetpack::get_file_url_for_environment( '_inc/build/gallery-settings.min.js', '_inc/gallery-settings.js' ),
63
				array( 'media-views' ),
64
				'20121225'
65
			);
66
		}
67
68
		wp_enqueue_script( 'jetpack-gallery-settings' );
69
	}
70
71
	/**
72
	 * Outputs a view template which can be used with wp.media.template
73
	 */
74
	function print_media_templates() {
75
		/**
76
		 * Filter the default gallery type.
77
		 *
78
		 * @module tiled-gallery
79
		 *
80
		 * @since 2.5.1
81
		 *
82
		 * @param string $value A string of the gallery type. Default is ‘default’.
83
		 *
84
		 */
85
		$default_gallery_type = apply_filters( 'jetpack_default_gallery_type', 'default' );
86
87
		?>
88
		<script type="text/html" id="tmpl-jetpack-gallery-settings">
89
			<label class="setting">
90
				<span><?php _e( 'Type', 'jetpack' ); ?></span>
91
				<select class="type" name="type" data-setting="type">
92
					<?php foreach ( $this->gallery_types as $value => $caption ) : ?>
93
						<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $default_gallery_type ); ?>><?php echo esc_html( $caption ); ?></option>
94
					<?php endforeach; ?>
95
				</select>
96
			</label>
97
		</script>
98
		<?php
99
	}
100
}
101
new Jetpack_Gallery_Settings;
102