Completed
Push — try/jetpack-stories-block-mobi... ( a0d35f...3f9c63 )
by
unknown
10:04
created

Jetpack_Gallery_Settings   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 12.63 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 12
loc 95
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A admin_init() 0 21 2
A core_media_widget_compat() 0 9 1
A wp_enqueue_media() 12 16 2
A print_media_templates() 0 26 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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