Completed
Pull Request — develop (#1577)
by Zack
18:22
created

fileupload_link_atts()   B

Complexity

Conditions 11
Paths 9

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 9
nop 4
dl 0
loc 43
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Integrate with the FancyBox lightbox and gallery scripts
4
 * @see https://fancyapps.com/fancybox/3/docs/#options
5
 * @since 2.10
6
 */
7
8
/**
9
 * Register the FancyBox lightbox
10
 *
11
 * @internal
12
 */
13
class GravityView_Lightbox_Provider_FancyBox extends GravityView_Lightbox_Provider {
14
15
	public static $slug = 'fancybox';
16
17
	public static $script_slug = 'gravityview-fancybox';
18
19
	public static $style_slug = 'gravityview-fancybox';
20
21
	/**
22
	 * @inheritDoc
23
	 */
24
	public function print_scripts( $gravityview ) {
25
26
		parent::print_scripts( $gravityview );
27
28
		if ( ! self::is_active( $gravityview ) ) {
29
			return;
30
		}
31
32
		$settings = self::get_settings();
33
34
		$settings = json_encode( $settings );
35
		?>
36
		<style>
37
			.fancybox-container {
38
				z-index: 100000; /** Divi is 99999 */
39
			}
40
41
			.admin-bar .fancybox-container {
42
				margin-top: 32px;
43
			}
44
		</style>
45
		<script>
46
			if ( window.jQuery ) {
47
				jQuery( '.gravityview-fancybox' ).fancybox(<?php echo $settings; ?>);
48
			}
49
		</script>
50
		<?php
51
52
	}
53
54
	/**
55
	 * Options to pass to Fancybox
56
	 *
57
	 * @see https://fancyapps.com/fancybox/3/docs/#options
58
	 *
59
	 * @return array
60
	 */
61
	protected function default_settings() {
62
63
		$defaults = array(
64
				'animationEffect' => 'fade',
65
				'toolbar'         => true,
66
				'closeExisting'   => true,
67
				'arrows'          => true,
68
				'buttons'         => array(
69
					'thumbs',
70
					'close',
71
				),
72
				'i18n'            => array(
73
					'en' => array(
74
						'CLOSE'       => __( 'Close', 'gravityview' ),
75
						'NEXT'        => __( 'Next', 'gravityview' ),
76
						'PREV'        => __( 'Previous', 'gravityview' ),
77
						'ERROR'       => __( 'The requested content cannot be loaded. Please try again later.', 'gravityview' ),
78
						'PLAY_START'  => __( 'Start slideshow', 'gravityview' ),
79
						'PLAY_STOP'   => __( 'Pause slideshow', 'gravityview' ),
80
						'FULL_SCREEN' => __( 'Full screen', 'gravityview' ),
81
						'THUMBS'      => __( 'Thumbnails', 'gravityview' ),
82
						'DOWNLOAD'    => __( 'Download', 'gravityview' ),
83
						'SHARE'       => __( 'Share', 'gravityview' ),
84
						'ZOOM'        => __( 'Zoom', 'gravityview' ),
85
					),
86
				)
87
		);
88
89
		return $defaults;
90
	}
91
92
	/**
93
	 * @inheritDoc
94
	 */
95
	public function enqueue_scripts() {
96
		$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
97
		wp_register_script( self::$script_slug, plugins_url( 'assets/lib/fancybox/dist/jquery.fancybox' . $min . '.js', GRAVITYVIEW_FILE ), array( 'jquery' ), GV_PLUGIN_VERSION );
98
	}
99
100
	/**
101
	 * @inheritDoc
102
	 */
103
	public function enqueue_styles() {
104
		$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
105
		wp_register_style( self::$style_slug, plugins_url( 'assets/lib/fancybox/dist/jquery.fancybox' . $min . '.css', GRAVITYVIEW_FILE ), array(), GV_PLUGIN_VERSION );
106
	}
107
108
	/**
109
	 * @inheritDoc
110
	 */
111
	public function allowed_atts( $atts = array() ) {
112
113
		$atts['data-fancybox']         = null;
114
		$atts['data-fancybox-trigger'] = null;
115
		$atts['data-fancybox-index']   = null;
116
		$atts['data-src']              = null;
117
		$atts['data-type']             = null;
118
		$atts['data-width']            = null;
119
		$atts['data-height']           = null;
120
		$atts['data-srcset']           = null;
121
		$atts['data-caption']          = null;
122
		$atts['data-options']          = null;
123
		$atts['data-filter']           = null;
124
		$atts['data-type']             = null;
125
126
		return $atts;
127
	}
128
129
	/**
130
	 * @inheritDoc
131
	 */
132
	public function fileupload_link_atts( $link_atts, $field_compat = array(), $context = null, $additional_details = null ) {
133
134
		if ( $context && ! $context->view->settings->get( 'lightbox', false ) ) {
135
			return $link_atts;
136
		}
137
138
		// Prevent empty content from getting added to the lightbox gallery
139
		if ( is_array( $additional_details ) && empty( $additional_details['file_path'] ) ) {
140
			return $link_atts;
141
		}
142
143
		// Prevent empty content from getting added to the lightbox gallery
144
		if ( is_array( $additional_details ) && ! empty( $additional_details['disable_lightbox'] ) ) {
145
			return $link_atts;
146
		}
147
148
		$link_atts['class'] = \GV\Utils::get( $link_atts, 'class' ) . ' gravityview-fancybox';
149
150
		$link_atts['class'] = gravityview_sanitize_html_class( $link_atts['class'] );
151
152
		if ( $context && ! empty( $context->field->field ) ) {
153
			if ( $context->field->field->multipleFiles ) {
154
				$entry = $context->entry->as_entry();
155
				$link_atts['data-fancybox'] = 'gallery-' . sprintf( "%s-%s-%s", $entry['form_id'], $context->field->ID, $context->entry->get_slug() );
156
			}
157
		}
158
159
		$file_path = \GV\Utils::get( $additional_details, 'file_path' );
160
161
		if ( false !== strpos( $file_path, 'gv-iframe' ) ) {
162
163
			$fancybox_settings = array(
164
				'type' => 'iframe',
165
				'iframe' => array(
166
					'preload' => false,
167
				),
168
			);
169
170
			$link_atts['data-options'] = json_encode( $fancybox_settings );
171
		}
172
173
		return $link_atts;
174
	}
175
176
}
177
178
GravityView_Lightbox::register( 'GravityView_Lightbox_Provider_FancyBox' );
179