Completed
Push — develop ( df3f0b...88eeb8 )
by David
07:56 queued 02:04
created

Wordlift_Timeline_Shortcode::get_locale()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * The wl_timeline shortcode displays an interactive timeline of events bound to the current post.
5
 *
6
 * @since 3.1.0
7
 */
8
class Wordlift_Timeline_Shortcode extends Wordlift_Shortcode {
9
10
	const SHORTCODE = 'wl_timeline';
11
12
	/**
13
	 * The list of locales supported by TimelineJS (correspond to the list of
14
	 * files in the locale subfolder).
15
	 *
16
	 * @since 3.7.0
17
	 * @var array An array of two-letters language codes.
18
	 */
19
	private static $supported_locales = array(
20
		'ur',
21
		'uk',
22
		'tr',
23
		'tl',
24
		'th',
25
		'te',
26
		'ta',
27
		'sv',
28
		'sr',
29
		'sl',
30
		'sk',
31
		'si',
32
		'ru',
33
		'ro',
34
		'rm',
35
		'pt',
36
		'pl',
37
		'no',
38
		'nl',
39
		'ne',
40
		'ms',
41
		'lv',
42
		'lt',
43
		'lb',
44
		'ko',
45
		'ka',
46
		'ja',
47
		'iw',
48
		'it',
49
		'is',
50
		'id',
51
		'hy',
52
		'hu',
53
		'hr',
54
		'hi',
55
		'he',
56
		'gl',
57
		'ga',
58
		'fy',
59
		'fr',
60
		'fo',
61
		'fi',
62
		'fa',
63
		'eu',
64
		'et',
65
		'es',
66
		'eo',
67
		'en',
68
		'el',
69
		'de',
70
		'da',
71
		'cz',
72
		'ca',
73
		'bg',
74
		'be',
75
		'ar',
76
		'af'
77
	);
78
79
	/**
80
	 * The Log service.
81
	 *
82
	 * @since 3.1.0
83
	 * @access private
84
	 * @var \Wordlift_Log_Service $log_service The Log service.
85
	 */
86
	private $log_service;
87
88
	/**
89
	 * Create a Wordlift_Timeline_Shortcode instance.
90
	 *
91
	 * @since 3.1.0
92
	 */
93
	public function __construct() {
94
		parent::__construct();
95
96
		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_Timeline_Shortcode' );
97
98
	}
99
100
	/**
101
	 * Renders the Timeline.
102
	 *
103
	 * @since 3.1.0
104
	 *
105
	 * @param array $atts An array of shortcode attributes.
106
	 *
107
	 * @return string The rendered HTML.
108
	 */
109
	public function render( $atts ) {
110
111
		//extract attributes and set default values
112
		$settings = shortcode_atts( array(
113
			'debug'                            => defined( 'WP_DEBUG' ) && WP_DEBUG,
114
			'height'                           => NULL,
115
			'width'                            => NULL,
116
			'is_embed'                         => FALSE,
117
			'hash_bookmark'                    => FALSE,
118
			'default_bg_color'                 => 'white',
119
			'scale_factor'                     => 2,
120
			'initial_zoom'                     => NULL,
121
			'zoom_sequence'                    => '[0.5, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]',
122
			'timenav_position'                 => 'bottom',
123
			'optimal_tick_width'               => 100,
124
			'base_class'                       => 'tl-timeline',
125
			'timenav_height'                   => 150,
126
			'timenav_height_percentage'        => NULL,
127
			'timenav_mobile_height_percentage' => 40,
128
			'timenav_height_min'               => 150,
129
			'marker_height_min'                => 30,
130
			'marker_width_min'                 => 100,
131
			'marker_padding'                   => 5,
132
			'start_at_slide'                   => 0,
133
			'start_at_end'                     => FALSE,
134
			'menubar_height'                   => 0,
135
			'use_bc'                           => FALSE,
136
			'duration'                         => 1000,
137
			'ease'                             => 'TL.Ease.easeInOutQuint',
138
			'slide_default_fade'               => '0%',
139
			'language'                         => $this->get_locale(),
140
			'ga_property_id'                   => NULL,
141
			'track_events'                     => "['back_to_start','nav_next','nav_previous','zoom_in','zoom_out']",
142
			'global'                           => FALSE,
143
			// The following settings are unrelated to TimelineJS script.
144
			'display_images_as'                => 'media',
145
			'excerpt_words'                    => 55,
146
		), $atts );
147
148
		// Load the TimelineJS stylesheets and scripts.
149
		wp_enqueue_style( 'timelinejs', dirname( plugin_dir_url( __FILE__ ) ) . '/timelinejs/css/timeline.css' );
150
		wp_enqueue_script( 'timelinejs', dirname( plugin_dir_url( __FILE__ ) ) . '/timelinejs/js/timeline' . ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ? '-min' : '' ) . '.js' );
151
152
		// Enqueue the scripts for the timeline.
153
		$this->enqueue_scripts();
154
155
		// Provide the script with options.
156
		wp_localize_script( 'timelinejs', 'wl_timeline_params', array(
157
			'ajax_url'          => admin_url( 'admin-ajax.php' ),
158
			// TODO: this parameter is already provided by WP
159
			'action'            => 'wl_timeline',
160
			// These settings apply to our wl_timeline AJAX endpoint.
161
			'display_images_as' => $settings['display_images_as'],
162
			'excerpt_words'     => $settings['excerpt_words'],
163
			// These settings apply to the timeline javascript client.
164
			'settings'          => array_filter( $settings, function ( $value, $key = NULL ) {
165
				// Do not set NULL values or settings which are not related to the client TimelineJS script.
166
				return ( NULL != $key && NULL !== $value && 'display_images_as' !== $key && 'excerpt_words' !== $key );
167
			} )
168
		) );
169
170
		// Get the current post id or set null if global is set to true.
171
		$post_id = ( $settings['global'] ? NULL : get_the_ID() );
172
173
		// Escaping atts.
174
		$style        = sprintf( 'style="%s%s"', isset( $settings['width'] ) ? "width:{$settings['width']};" : '', isset( $settings['height'] ) ? "height:{$settings['height']};" : '' );
175
		$data_post_id = ( isset( $post_id ) ? "data-post-id='$post_id'" : '' );
176
177
		// Generate a unique ID for this timeline.
178
		$element_id = uniqid( 'wl-timeline-' );
179
180
		if ( WP_DEBUG ) {
181
			$this->log_service->trace( "Creating a timeline widget [ element id :: $element_id ][ post id :: $post_id ]" );
182
		}
183
184
		// Building template.
185
		return sprintf( '<div class="wl-timeline-container" %s><div class="wl-timeline" id="%s" %s></div></div>', $style, $element_id, $data_post_id );
186
	}
187
188
	/**
189
	 * Return the locale for the TimelineJS according to WP's configured locale and
190
	 * support TimelineJS locales. If WP's locale is not supported, english is used.
191
	 *
192
	 * @since 3.7.0
193
	 * @return string The locale (2 letters code).
194
	 */
195
	private function get_locale() {
196
197
		// Get the first 2 letters.
198
		$locale = substr( get_locale(), 0, 2 );
199
200
		// Check that the specified locale is supported otherwise use English.
201
		return in_array( $locale, self::$supported_locales ) ? $locale : 'en';
202
	}
203
204
}
205