1 <?php if ( ! defined( 'OT_VERSION' ) ) exit( 'No direct script access allowed' ); |
1 <?php |
2 /** |
2 /** |
3 * OptionTree functions |
3 * OptionTree Function. |
4 * |
4 * |
5 * @package OptionTree |
5 * @package OptionTree |
6 * @author Derek Herman <derek@valendesigns.com> |
|
7 * @copyright Copyright (c) 2013, Derek Herman |
|
8 * @since 2.0 |
|
9 */ |
6 */ |
10 |
7 |
11 /** |
8 if ( ! defined( 'OT_VERSION' ) ) { |
12 * Theme Options ID |
9 exit( 'No direct script access allowed' ); |
13 * |
10 } |
14 * @return string |
11 |
15 * |
|
16 * @access public |
|
17 * @since 2.3.0 |
|
18 */ |
|
19 if ( ! function_exists( 'ot_options_id' ) ) { |
12 if ( ! function_exists( 'ot_options_id' ) ) { |
20 |
13 |
21 function ot_options_id() { |
14 /** |
22 |
15 * Theme Options ID |
23 return apply_filters( 'ot_options_id', 'option_tree' ); |
16 * |
24 |
17 * @return string |
25 } |
18 * |
26 |
19 * @access public |
27 } |
20 * @since 2.3.0 |
28 |
21 */ |
29 /** |
22 function ot_options_id() { |
30 * Theme Settings ID |
23 |
31 * |
24 return apply_filters( 'ot_options_id', 'option_tree' ); |
32 * @return string |
25 |
33 * |
26 } |
34 * @access public |
27 } |
35 * @since 2.3.0 |
28 |
36 */ |
|
37 if ( ! function_exists( 'ot_settings_id' ) ) { |
29 if ( ! function_exists( 'ot_settings_id' ) ) { |
38 |
30 |
39 function ot_settings_id() { |
31 /** |
40 |
32 * Theme Settings ID |
41 return apply_filters( 'ot_settings_id', 'option_tree_settings' ); |
33 * |
42 |
34 * @return string |
43 } |
35 * |
44 |
36 * @access public |
45 } |
37 * @since 2.3.0 |
46 |
38 */ |
47 /** |
39 function ot_settings_id() { |
48 * Theme Layouts ID |
40 |
49 * |
41 return apply_filters( 'ot_settings_id', 'option_tree_settings' ); |
50 * @return string |
42 |
51 * |
43 } |
52 * @access public |
44 } |
53 * @since 2.3.0 |
45 |
54 */ |
|
55 if ( ! function_exists( 'ot_layouts_id' ) ) { |
46 if ( ! function_exists( 'ot_layouts_id' ) ) { |
56 |
47 |
57 function ot_layouts_id() { |
48 /** |
58 |
49 * Theme Layouts ID |
59 return apply_filters( 'ot_layouts_id', 'option_tree_layouts' ); |
50 * |
60 |
51 * @return string |
61 } |
52 * |
62 |
53 * @access public |
63 } |
54 * @since 2.3.0 |
64 |
55 */ |
65 /** |
56 function ot_layouts_id() { |
66 * Get Option. |
57 |
67 * |
58 return apply_filters( 'ot_layouts_id', 'option_tree_layouts' ); |
68 * Helper function to return the option value. |
59 |
69 * If no value has been saved, it returns $default. |
60 } |
70 * |
61 } |
71 * @param string The option ID. |
62 |
72 * @param string The default option value. |
|
73 * @return mixed |
|
74 * |
|
75 * @access public |
|
76 * @since 2.0 |
|
77 */ |
|
78 if ( ! function_exists( 'ot_get_option' ) ) { |
63 if ( ! function_exists( 'ot_get_option' ) ) { |
79 |
64 |
80 function ot_get_option( $option_id, $default = '' ) { |
65 /** |
81 |
66 * Get Option. |
82 /* get the saved options */ |
67 * |
83 $options = get_option( ot_options_id() ); |
68 * Helper function to return the option value. |
84 |
69 * If no value has been saved, it returns $default. |
85 /* look for the saved value */ |
70 * |
86 if ( isset( $options[$option_id] ) && '' != $options[$option_id] ) { |
71 * @param string $option_id The option ID. |
87 |
72 * @param string $default The default option value. |
88 return ot_wpml_filter( $options, $option_id ); |
73 * @return mixed |
89 |
74 * |
90 } |
75 * @access public |
91 |
76 * @since 2.0 |
92 return $default; |
77 */ |
93 |
78 function ot_get_option( $option_id, $default = '' ) { |
94 } |
79 |
95 |
80 // Get the saved options. |
96 } |
81 $options = get_option( ot_options_id() ); |
97 |
82 |
98 /** |
83 // Look for the saved value. |
99 * Echo Option. |
84 if ( isset( $options[ $option_id ] ) && '' !== $options[ $option_id ] ) { |
100 * |
85 |
101 * Helper function to echo the option value. |
86 return ot_wpml_filter( $options, $option_id ); |
102 * If no value has been saved, it echos $default. |
87 |
103 * |
88 } |
104 * @param string The option ID. |
89 |
105 * @param string The default option value. |
90 return $default; |
106 * @return mixed |
91 |
107 * |
92 } |
108 * @access public |
93 } |
109 * @since 2.2.0 |
94 |
110 */ |
|
111 if ( ! function_exists( 'ot_echo_option' ) ) { |
95 if ( ! function_exists( 'ot_echo_option' ) ) { |
112 |
96 |
113 function ot_echo_option( $option_id, $default = '' ) { |
97 /** |
114 |
98 * Echo Option. |
115 echo ot_get_option( $option_id, $default ); |
99 * |
116 |
100 * Helper function to echo the option value. |
117 } |
101 * If no value has been saved, it echos $default. |
118 |
102 * |
119 } |
103 * @param string $option_id The option ID. |
120 |
104 * @param string $default The default option value. |
121 /** |
105 * @return mixed |
122 * Filter the return values through WPML |
106 * |
123 * |
107 * @access public |
124 * @param array $options The current options |
108 * @since 2.2.0 |
125 * @param string $option_id The option ID |
109 */ |
126 * @return mixed |
110 function ot_echo_option( $option_id, $default = '' ) { |
127 * |
111 |
128 * @access public |
112 echo ot_get_option( $option_id, $default ); // phpcs:ignore |
129 * @since 2.1 |
113 |
130 */ |
114 } |
|
115 } |
|
116 |
131 if ( ! function_exists( 'ot_wpml_filter' ) ) { |
117 if ( ! function_exists( 'ot_wpml_filter' ) ) { |
132 |
118 |
133 function ot_wpml_filter( $options, $option_id ) { |
119 /** |
134 |
120 * Filter the return values through WPML |
135 // Return translated strings using WMPL |
121 * |
136 if ( function_exists('icl_t') ) { |
122 * @param array $options The current options. |
137 |
123 * @param string $option_id The option ID. |
138 $settings = get_option( ot_settings_id() ); |
124 * @return mixed |
139 |
125 * |
140 if ( isset( $settings['settings'] ) ) { |
126 * @access public |
141 |
127 * @since 2.1 |
142 foreach( $settings['settings'] as $setting ) { |
128 */ |
143 |
129 function ot_wpml_filter( $options, $option_id ) { |
144 // List Item & Slider |
130 |
145 if ( $option_id == $setting['id'] && in_array( $setting['type'], array( 'list-item', 'slider' ) ) ) { |
131 // Return translated strings using WMPL. |
146 |
132 if ( function_exists( 'icl_t' ) ) { |
147 foreach( $options[$option_id] as $key => $value ) { |
133 |
148 |
134 $settings = get_option( ot_settings_id() ); |
149 foreach( $value as $ckey => $cvalue ) { |
135 |
150 |
136 if ( isset( $settings['settings'] ) ) { |
151 $id = $option_id . '_' . $ckey . '_' . $key; |
137 |
152 $_string = icl_t( 'Theme Options', $id, $cvalue ); |
138 foreach ( $settings['settings'] as $setting ) { |
153 |
139 |
154 if ( ! empty( $_string ) ) { |
140 // List Item & Slider. |
155 |
141 if ( $option_id === $setting['id'] && in_array( $setting['type'], array( 'list-item', 'slider' ), true ) ) { |
156 $options[$option_id][$key][$ckey] = $_string; |
142 |
157 |
143 foreach ( $options[ $option_id ] as $key => $value ) { |
158 } |
144 |
159 |
145 foreach ( $value as $ckey => $cvalue ) { |
160 } |
146 |
161 |
147 $id = $option_id . '_' . $ckey . '_' . $key; |
162 } |
148 $_string = icl_t( 'Theme Options', $id, $cvalue ); |
163 |
149 |
164 // List Item & Slider |
150 if ( ! empty( $_string ) ) { |
165 } else if ( $option_id == $setting['id'] && $setting['type'] == 'social-links' ) { |
151 |
166 |
152 $options[ $option_id ][ $key ][ $ckey ] = $_string; |
167 foreach( $options[$option_id] as $key => $value ) { |
153 } |
168 |
154 } |
169 foreach( $value as $ckey => $cvalue ) { |
155 } |
170 |
156 |
171 $id = $option_id . '_' . $ckey . '_' . $key; |
157 // List Item & Slider. |
172 $_string = icl_t( 'Theme Options', $id, $cvalue ); |
158 } elseif ( $option_id === $setting['id'] && 'social-links' === $setting['type'] ) { |
173 |
159 |
174 if ( ! empty( $_string ) ) { |
160 foreach ( $options[ $option_id ] as $key => $value ) { |
175 |
161 |
176 $options[$option_id][$key][$ckey] = $_string; |
162 foreach ( $value as $ckey => $cvalue ) { |
177 |
163 |
178 } |
164 $id = $option_id . '_' . $ckey . '_' . $key; |
179 |
165 $_string = icl_t( 'Theme Options', $id, $cvalue ); |
180 } |
166 |
181 |
167 if ( ! empty( $_string ) ) { |
182 } |
168 |
183 |
169 $options[ $option_id ][ $key ][ $ckey ] = $_string; |
184 // All other acceptable option types |
170 } |
185 } else if ( $option_id == $setting['id'] && in_array( $setting['type'], apply_filters( 'ot_wpml_option_types', array( 'text', 'textarea', 'textarea-simple' ) ) ) ) { |
171 } |
186 |
172 } |
187 $_string = icl_t( 'Theme Options', $option_id, $options[$option_id] ); |
173 |
188 |
174 // All other acceptable option types. |
189 if ( ! empty( $_string ) ) { |
175 } elseif ( $option_id === $setting['id'] && in_array( $setting['type'], apply_filters( 'ot_wpml_option_types', array( 'text', 'textarea', 'textarea-simple' ) ), true ) ) { |
190 |
176 |
191 $options[$option_id] = $_string; |
177 $_string = icl_t( 'Theme Options', $option_id, $options[ $option_id ] ); |
192 |
178 |
193 } |
179 if ( ! empty( $_string ) ) { |
194 |
180 |
195 } |
181 $options[ $option_id ] = $_string; |
196 |
182 } |
197 } |
183 } |
198 |
184 } |
199 } |
185 } |
200 |
186 } |
201 } |
187 |
202 |
188 return $options[ $option_id ]; |
203 return $options[$option_id]; |
189 } |
204 |
190 } |
205 } |
191 |
206 |
|
207 } |
|
208 |
|
209 /** |
|
210 * Enqueue the dynamic CSS. |
|
211 * |
|
212 * @return void |
|
213 * |
|
214 * @access public |
|
215 * @since 2.0 |
|
216 */ |
|
217 if ( ! function_exists( 'ot_load_dynamic_css' ) ) { |
192 if ( ! function_exists( 'ot_load_dynamic_css' ) ) { |
218 |
193 |
219 function ot_load_dynamic_css() { |
194 /** |
220 |
195 * Enqueue the dynamic CSS. |
221 /* don't load in the admin */ |
196 * |
222 if ( is_admin() ) { |
197 * @access public |
223 return; |
198 * @since 2.0 |
224 } |
199 */ |
225 |
200 function ot_load_dynamic_css() { |
226 /** |
201 |
227 * Filter whether or not to enqueue a `dynamic.css` file at the theme level. |
202 // Don't load in the admin. |
228 * |
203 if ( is_admin() ) { |
229 * By filtering this to `false` OptionTree will not attempt to enqueue any CSS files. |
204 return; |
230 * |
205 } |
231 * Example: add_filter( 'ot_load_dynamic_css', '__return_false' ); |
206 |
232 * |
207 /** |
233 * @since 2.5.5 |
208 * Filter whether or not to enqueue a `dynamic.css` file at the theme level. |
234 * |
209 * |
235 * @param bool $load_dynamic_css Default is `true`. |
210 * By filtering this to `false` OptionTree will not attempt to enqueue any CSS files. |
236 * @return bool |
211 * |
237 */ |
212 * Example: add_filter( 'ot_load_dynamic_css', '__return_false' ); |
238 if ( false === (bool) apply_filters( 'ot_load_dynamic_css', true ) ) { |
213 * |
239 return; |
214 * @since 2.5.5 |
240 } |
215 * |
241 |
216 * @param bool $load_dynamic_css Default is `true`. |
242 /* grab a copy of the paths */ |
217 * @return bool |
243 $ot_css_file_paths = get_option( 'ot_css_file_paths', array() ); |
218 */ |
244 if ( is_multisite() ) { |
219 if ( false === (bool) apply_filters( 'ot_load_dynamic_css', true ) ) { |
245 $ot_css_file_paths = get_blog_option( get_current_blog_id(), 'ot_css_file_paths', $ot_css_file_paths ); |
220 return; |
246 } |
221 } |
247 |
222 |
248 if ( ! empty( $ot_css_file_paths ) ) { |
223 // Grab a copy of the paths. |
249 |
224 $ot_css_file_paths = get_option( 'ot_css_file_paths', array() ); |
250 $last_css = ''; |
225 if ( is_multisite() ) { |
251 |
226 $ot_css_file_paths = get_blog_option( get_current_blog_id(), 'ot_css_file_paths', $ot_css_file_paths ); |
252 /* loop through paths */ |
227 } |
253 foreach( $ot_css_file_paths as $key => $path ) { |
228 |
254 |
229 if ( ! empty( $ot_css_file_paths ) ) { |
255 if ( '' != $path && file_exists( $path ) ) { |
230 |
256 |
231 $last_css = ''; |
257 $parts = explode( '/wp-content', $path ); |
232 |
258 |
233 // Loop through paths. |
259 if ( isset( $parts[1] ) ) { |
234 foreach ( $ot_css_file_paths as $key => $path ) { |
260 |
235 |
261 $sub_parts = explode( '/', $parts[1] ); |
236 if ( '' !== $path && file_exists( $path ) ) { |
262 |
237 |
263 if ( isset( $sub_parts[1] ) && isset( $sub_parts[2] ) ) { |
238 $parts = explode( '/wp-content', $path ); |
264 if ( $sub_parts[1] == 'themes' && $sub_parts[2] != get_stylesheet() ) { |
239 |
265 continue; |
240 if ( isset( $parts[1] ) ) { |
266 } |
241 |
267 } |
242 $sub_parts = explode( '/', $parts[1] ); |
268 |
243 |
269 $css = set_url_scheme( WP_CONTENT_URL ) . $parts[1]; |
244 if ( isset( $sub_parts[1] ) && isset( $sub_parts[2] ) ) { |
270 |
245 if ( 'themes' !== $sub_parts[1] && get_stylesheet() !== $sub_parts[2] ) { |
271 if ( $last_css !== $css ) { |
246 continue; |
272 |
247 } |
273 /* enqueue filtered file */ |
248 } |
274 wp_enqueue_style( 'ot-dynamic-' . $key, $css, false, OT_VERSION ); |
249 |
275 |
250 $css = set_url_scheme( WP_CONTENT_URL ) . $parts[1]; |
276 $last_css = $css; |
251 |
277 |
252 if ( $last_css !== $css ) { |
278 } |
253 |
279 |
254 // Enqueue filtered file. |
280 } |
255 wp_enqueue_style( 'ot-dynamic-' . $key, $css, false, OT_VERSION ); |
281 |
256 |
282 } |
257 $last_css = $css; |
283 |
258 } |
284 } |
259 } |
285 |
260 } |
286 } |
261 } |
287 |
262 } |
288 } |
263 |
289 |
264 } |
290 } |
265 } |
291 |
266 |
292 /** |
|
293 * Enqueue the Google Fonts CSS. |
|
294 * |
|
295 * @return void |
|
296 * |
|
297 * @access public |
|
298 * @since 2.5.0 |
|
299 */ |
|
300 if ( ! function_exists( 'ot_load_google_fonts_css' ) ) { |
267 if ( ! function_exists( 'ot_load_google_fonts_css' ) ) { |
301 |
268 |
302 function ot_load_google_fonts_css() { |
269 /** |
303 |
270 * Enqueue the Google Fonts CSS. |
304 /* don't load in the admin */ |
271 * |
305 if ( is_admin() ) |
272 * @access public |
306 return; |
273 * @since 2.5.0 |
307 |
274 */ |
308 $ot_google_fonts = get_theme_mod( 'ot_google_fonts', array() ); |
275 function ot_load_google_fonts_css() { |
309 $ot_set_google_fonts = get_theme_mod( 'ot_set_google_fonts', array() ); |
276 |
310 $families = array(); |
277 /* don't load in the admin */ |
311 $subsets = array(); |
278 if ( is_admin() ) { |
312 $append = ''; |
279 return; |
313 |
280 } |
314 if ( ! empty( $ot_set_google_fonts ) ) { |
281 |
315 |
282 $ot_google_fonts = get_theme_mod( 'ot_google_fonts', array() ); |
316 foreach( $ot_set_google_fonts as $id => $fonts ) { |
283 $ot_set_google_fonts = get_theme_mod( 'ot_set_google_fonts', array() ); |
317 |
284 $families = array(); |
318 foreach( $fonts as $font ) { |
285 $subsets = array(); |
319 |
286 $append = ''; |
320 // Can't find the font, bail! |
287 |
321 if ( ! isset( $ot_google_fonts[$font['family']]['family'] ) ) { |
288 if ( ! empty( $ot_set_google_fonts ) ) { |
322 continue; |
289 |
323 } |
290 foreach ( $ot_set_google_fonts as $id => $fonts ) { |
324 |
291 |
325 // Set variants & subsets |
292 foreach ( $fonts as $font ) { |
326 if ( ! empty( $font['variants'] ) && is_array( $font['variants'] ) ) { |
293 |
327 |
294 // Can't find the font, bail! |
328 // Variants string |
295 if ( ! isset( $ot_google_fonts[ $font['family'] ]['family'] ) ) { |
329 $variants = ':' . implode( ',', $font['variants'] ); |
296 continue; |
330 |
297 } |
331 // Add subsets to array |
298 |
332 if ( ! empty( $font['subsets'] ) && is_array( $font['subsets'] ) ) { |
299 // Set variants & subsets. |
333 foreach( $font['subsets'] as $subset ) { |
300 if ( ! empty( $font['variants'] ) && is_array( $font['variants'] ) ) { |
334 $subsets[] = $subset; |
301 |
335 } |
302 // Variants string. |
336 } |
303 $variants = ':' . implode( ',', $font['variants'] ); |
337 |
304 |
338 } |
305 // Add subsets to array. |
339 |
306 if ( ! empty( $font['subsets'] ) && is_array( $font['subsets'] ) ) { |
340 // Add family & variants to array |
307 foreach ( $font['subsets'] as $subset ) { |
341 if ( isset( $variants ) ) { |
308 $subsets[] = $subset; |
342 $families[] = str_replace( ' ', '+', $ot_google_fonts[$font['family']]['family'] ) . $variants; |
309 } |
343 } |
310 } |
344 |
311 } |
345 } |
312 |
346 |
313 // Add family & variants to array. |
347 } |
314 if ( isset( $variants ) ) { |
348 |
315 $families[] = str_replace( ' ', '+', $ot_google_fonts[ $font['family'] ]['family'] ) . $variants; |
349 } |
316 } |
350 |
317 } |
351 if ( ! empty( $families ) ) { |
318 } |
352 |
319 } |
353 $families = array_unique( $families ); |
320 |
354 |
321 if ( ! empty( $families ) ) { |
355 // Append all subsets to the path, unless the only subset is latin. |
322 |
356 if ( ! empty( $subsets ) ) { |
323 $families = array_unique( $families ); |
357 $subsets = implode( ',', array_unique( $subsets ) ); |
324 |
358 if ( $subsets != 'latin' ) { |
325 // Append all subsets to the path, unless the only subset is latin. |
359 $append = '&subset=' . $subsets; |
326 if ( ! empty( $subsets ) ) { |
360 } |
327 $subsets = implode( ',', array_unique( $subsets ) ); |
361 } |
328 if ( 'latin' !== $subsets ) { |
362 |
329 $append = '&subset=' . $subsets; |
363 wp_enqueue_style( 'ot-google-fonts', esc_url( '//fonts.googleapis.com/css?family=' . implode( '%7C', $families ) ) . $append, false, null ); |
330 } |
364 } |
331 } |
365 |
332 |
366 } |
333 wp_enqueue_style( 'ot-google-fonts', esc_url( '//fonts.googleapis.com/css?family=' . implode( '%7C', $families ) ) . $append, false, null ); // phpcs:ignore |
367 |
334 } |
368 } |
335 } |
369 |
336 } |
370 /** |
337 |
371 * Registers the Theme Option page link for the admin bar. |
|
372 * |
|
373 * @return void |
|
374 * |
|
375 * @access public |
|
376 * @since 2.1 |
|
377 */ |
|
378 if ( ! function_exists( 'ot_register_theme_options_admin_bar_menu' ) ) { |
338 if ( ! function_exists( 'ot_register_theme_options_admin_bar_menu' ) ) { |
379 |
339 |
380 function ot_register_theme_options_admin_bar_menu( $wp_admin_bar ) { |
340 /** |
381 |
341 * Registers the Theme Option page link for the admin bar. |
382 if ( ! current_user_can( apply_filters( 'ot_theme_options_capability', 'edit_theme_options' ) ) || ! is_admin_bar_showing() ) |
342 * |
383 return; |
343 * @access public |
384 |
344 * @since 2.1 |
385 $wp_admin_bar->add_node( array( |
345 * |
386 'parent' => 'appearance', |
346 * @param object $wp_admin_bar The WP_Admin_Bar object. |
387 'id' => apply_filters( 'ot_theme_options_menu_slug', 'ot-theme-options' ), |
347 */ |
388 'title' => apply_filters( 'ot_theme_options_page_title', __( 'Theme Options', 'option-tree' ) ), |
348 function ot_register_theme_options_admin_bar_menu( $wp_admin_bar ) { |
389 'href' => admin_url( apply_filters( 'ot_theme_options_parent_slug', 'themes.php' ) . '?page=' . apply_filters( 'ot_theme_options_menu_slug', 'ot-theme-options' ) ) |
349 |
390 ) ); |
350 if ( ! current_user_can( apply_filters( 'ot_theme_options_capability', 'edit_theme_options' ) ) || ! is_admin_bar_showing() ) { |
391 |
351 return; |
392 } |
352 } |
393 |
353 |
394 } |
354 $wp_admin_bar->add_node( |
395 |
355 array( |
396 /* End of file ot-functions.php */ |
356 'parent' => 'appearance', |
397 /* Location: ./includes/ot-functions.php */ |
357 'id' => apply_filters( 'ot_theme_options_menu_slug', 'ot-theme-options' ), |
|
358 'title' => apply_filters( 'ot_theme_options_page_title', __( 'Theme Options', 'option-tree' ) ), |
|
359 'href' => admin_url( apply_filters( 'ot_theme_options_parent_slug', 'themes.php' ) . '?page=' . apply_filters( 'ot_theme_options_menu_slug', 'ot-theme-options' ) ), |
|
360 ) |
|
361 ); |
|
362 } |
|
363 } |