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

fileupload_link_atts()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

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