-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
472 lines (393 loc) · 15 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php
/**
* Timber Lite functions and definitions
*
* @package Timber Lite
*/
if ( ! function_exists( 'timber_lite_setup' ) ) :
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which
* runs before the init hook. The init hook is too late for some features, such
* as indicating support for post thumbnails.
*/
function timber_lite_setup() {
/*
* Make theme available for translation.
* Translations can be filed in the /languages/ directory.
* If you're building a theme based on Timber, use a find and replace
* to change 'timber-lite' to the name of your theme in all the template files
*/
load_theme_textdomain( 'timber-lite', get_template_directory() . '/languages' );
// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );
/*
* Let WordPress manage the document title.
* By adding theme support, we declare that this theme does not use a
* hard-coded <title> tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support( 'title-tag' );
add_theme_support( 'custom-logo' );
/*
* Enable support for Post Thumbnails on posts and pages.
*
* @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
*/
add_theme_support( 'post-thumbnails' );
//used for the small images of projects
add_image_size( 'timber-small-image', 400, 9999, false );
//used for the large images of projects
add_image_size( 'timber-large-image', 1920, 9999, false );
// Used for single project full view
add_image_size( 'timber-fullview-image', 2560, 2560, false );
//used for blog archive
add_image_size( 'timber-square-image', 350, 350, true );
// This theme uses wp_nav_menu() in two locations.
register_nav_menus( array(
'primary' => esc_html__( 'Primary Menu', 'timber-lite' ),
'social' => esc_html__( 'Social Menu', 'timber-lite' )
) );
/*
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
*/
add_theme_support( 'html5', array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
) );
/**
* Enable support for Post Formats.
* See http://codex.wordpress.org/Post_Formats
*/
add_theme_support( 'post-formats', array(
'aside',
'image',
'gallery',
'video',
'audio',
'quote',
'link',
) );
add_filter( 'attachment_link', 'timber_lite_filter_attachment_links_on_singles', 2, 2 );
}
endif; // timber_lite_setup
add_action( 'after_setup_theme', 'timber_lite_setup' );
/**
* We need to force ajax off attachment links on posts and pages since it is a chance to trigger a modal opening
* and an ajax call
* @param $link
* @param $id
*
* @return string
*/
function timber_lite_filter_attachment_links_on_singles( $link, $id ){
if ( wp_attachment_is_image( $id ) && ( is_singular( 'post' ) || is_page()) ) {
return $link . '#';
}
return $link;
}
/**
* Set the content width in pixels, based on the theme's design and stylesheet.
*
* Priority 0 to make it available to lower priority callbacks.
*x
* @global int $content_width
*/
function timber_lite_content_width() {
$GLOBALS['content_width'] = apply_filters( 'timber_content_width', 2570 );
}
add_action( 'after_setup_theme', 'timber_lite_content_width', 0 );
/*
* Disable comments for the Portfolio CPT
*/
function timber_lite_remove_custom_post_comment() {
remove_post_type_support( 'jetpack-portfolio', 'comments' );
}
add_action( 'init', 'timber_lite_remove_custom_post_comment' );
/**
* Enqueue scripts and styles.
*/
function timber_lite_scripts_styles() {
$theme = wp_get_theme( get_template() );
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
// Main Style - we use this path instead of get_stylesheet_uri() so a child theme can extend this not override it.
wp_enqueue_style( 'timber-style', get_template_directory_uri() . '/style.css', array( 'wp-mediaelement' ), $theme->get( 'Version' ) );
$script_dependencies = array( 'jquery', 'wp-mediaelement' );
wp_enqueue_script( 'modernizr', get_template_directory_uri() . '/assets/js/plugins/modernizr.min.js', array( 'jquery' ), '3.3.1' );
$script_dependencies[] = 'modernizr';
wp_enqueue_script( 'tween-max', get_template_directory_uri() . '/assets/js/plugins/TweenMax.min.js', array( 'jquery' ) );
$script_dependencies[] = 'tween-max';
wp_enqueue_script( 'scroll-to-plugin', get_template_directory_uri() . '/assets/js/plugins/ScrollToPlugin.min.js', array( 'jquery' ) );
$script_dependencies[] = 'scroll-to-plugin';
wp_enqueue_script( 'timber-rs', get_template_directory_uri() . '/assets/js/plugins/rs.js', array( 'jquery' ) );
$script_dependencies[] = 'timber-rs';
wp_enqueue_script( 'timber-mix', get_template_directory_uri() . '/assets/js/plugins/mixitup.js', array( 'jquery' ) );
$script_dependencies[] = 'timber-mix';
wp_register_script( 'timber-scripts', get_template_directory_uri() . '/assets/js/main' . $suffix . '.js', $script_dependencies, $theme->get( 'Version' ), true );
// Localize the script with new data
$translation_array = array(
'tPrev' => esc_html__('Previous (Left arrow key)', 'timber-lite'),
'tNext' => esc_html__('Next (Right arrow key)', 'timber-lite'),
'tCounter' => esc_html__('of', 'timber-lite'),
'infscrLoadingText' => wp_kses_post( __("<em>Loading more...</em>", 'timber-lite') ),
'infscrReachedEnd' => wp_kses_post( __("<em>Nothing left to load.</em>", 'timber-lite') ),
);
wp_localize_script( 'timber-scripts', 'objectl10n', $translation_array );
wp_localize_script( 'timber-scripts', 'timber_ajax', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce( 'timber_ajax' ),
'posts_number' => get_option( 'posts_per_page' ),
) );
// Enqueued script with localized data.
wp_enqueue_script( 'timber-scripts' );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
wp_enqueue_script('mousewheel' , get_template_directory_uri() . '/assets/js/plugins/jquery.mousewheel.min.js', array('jquery'), '3.1.13', true );
// For back to top link
global $timber_show_footer;
$timber_show_footer = false;
if ( ! (get_post_type() == 'proof_gallery') && ! timber_lite_post_is_project() && ( is_page() || is_single() ) ) {
$timber_show_footer = true;
$this_template = basename( get_page_template() );
if ( is_page() && $this_template === 'custom-portfolio-page.php' ) {
$custom_portfolio_page_type = get_post_meta( timber_lite_get_post_id(), 'custom_portfolio_page_type', true);
if ( $custom_portfolio_page_type === 'project_slider' ) {
$timber_show_footer = false;
}
}
}
// if jetpack's carousel is enabled, enqueue the scripts in root
// this way the assets will get registered with the proper root url
if ( class_exists( 'Jetpack_Carousel' ) ) {
// Create new carousel so we can use the enqueue_assets() method. Not ideal, but there is a decent amount
// of logic in that method that shouldn't be duplicated.
$carousel = new Jetpack_Carousel();
// First parameter is $output, which comes from filters, and causes bypass of the asset enqueuing. Passing null is correct.
$carousel->enqueue_assets();
}
add_filter( 'jp_carousel_force_enable', true );
}
add_action( 'wp_enqueue_scripts', 'timber_lite_scripts_styles' );
/*
* Enqueue some custom JS in the admin area for various small tasks
*/
function timber_lite_add_admin_page_scripts( $hook ){
wp_enqueue_script(
'timber_admin_custom_js', //unique handle
get_template_directory_uri().'/assets/js/admin/admin.js', //location
array('jquery') //dependencies
);
}
add_action('admin_enqueue_scripts', 'timber_lite_add_admin_page_scripts' );
/*
* Add custom css to the new-project admin page
*/
function timber_lite_add_new_project_admin_style( $hook ){
global $typenow;
if ( 'jetpack-portfolio' === $typenow ) {
$output = '
<style>
.collection-settings {
display: none;
}
</style>';
echo $output;
}
}
add_action('admin_head', 'timber_lite_add_new_project_admin_style' );
function timber_lite_add_new_project_admin_editor_style() {
global $typenow;
if ( 'jetpack-portfolio' === $typenow ) {
add_editor_style( 'project-editor-style.css' );
}
}
add_action( 'admin_init', 'timber_lite_add_new_project_admin_editor_style' );
/**
* Get an array with all queued scripts
*
* @return array
*/
function timber_lite_get_queued_scripts() {
global $wp_scripts;
$loading_scripts = array();
foreach ( $wp_scripts->queue as $key => $handle ) {
if ( strpos( $wp_scripts->registered[ $handle ]->src, 'http' ) === false && strpos( $wp_scripts->registered[ $handle ]->src, '//' ) !== 0 ) {
$loading_scripts[ $handle ]['src'] = site_url() . $wp_scripts->registered[ $handle ]->src;
} else {
$loading_scripts[ $handle ]['src'] = $wp_scripts->registered[ $handle ]->src;
}
if ( isset( $wp_scripts->registered[ $handle ]->extra ) && isset( $wp_scripts->registered[ $handle ]->extra['data'] )){
$loading_scripts[ $handle ]['data'] = $wp_scripts->registered[ $handle ]->extra['data'];
}
}
return $loading_scripts;
}
/**
* Get an array with all queued styles
*
* @return array
*/
function timber_lite_get_queued_styles() {
global $wp_styles;
$loading_styles = array();
foreach ( $wp_styles->queue as $key => $handle ) {
if ( strpos( $wp_styles->registered[ $handle ]->src, 'http' ) === false && strpos( $wp_styles->registered[ $handle ]->src, '//' ) !== 0 ) {
$loading_styles[ $handle ] = site_url() . $wp_styles->registered[ $handle ]->src;
} else {
$loading_styles[ $handle ] = $wp_styles->registered[ $handle ]->src;
}
}
return $loading_styles;
}
/**
* Localize a static list with resources already loaded on the first page load this lists will be filled on
* each d-jax request which has new resources
*
* Note: make this dependent to timber-scripts because we know for sure it is there
*/
function timber_lite_localize_scripts_and_styles() {
wp_localize_script( 'timber-scripts', 'timber_static_resources', array(
'scripts' => timber_lite_get_queued_scripts(),
'styles' => timber_lite_get_queued_styles()
) );
}
add_action( 'wp_enqueue_scripts', 'timber_lite_localize_scripts_and_styles', 999999 );
function timber_lite_last_function(){
/**
* Display dynamic generated data while runing d-jax requests :
*
* a script which will load others scripts on the run
*/
$dynamic_scripts = timber_lite_get_queued_scripts();
$dynamic_styles = timber_lite_get_queued_styles();?>
<div id="djax_list_scripts_and_styles">
<script id="timber_list_scripts_and_styles" class="djax-updatable">
(function ($) {
// wait for all dom elements
$(document).ready(function () {
var globalDebug = false;
// run this only if we have resources
if (!window.hasOwnProperty('timber_static_resources')) return;
window.timber_dynamic_loaded_scripts = <?php echo json_encode( $dynamic_scripts ); ?>;
window.timber_dynamic_loaded_styles = <?php echo json_encode( $dynamic_styles ); ?>;
// timber_dynamic_loaded_scripts is generated in footer when all the scripts should be already enqueued
$.each( window.timber_dynamic_loaded_scripts, function (key, data) {
if (key in timber_static_resources.scripts) return;
var url = data['src'];
// add this script to our global stack so we don't enqueue it again
timber_static_resources.scripts[key] = url;
// if this script has localized content, we need to ensure that it's presence in page
if ( typeof data['data'] !== "undefined" && data['data'].indexOf('=') > 0 ) {
var object_name_start = data['data'].indexOf(' ' ) + 1,
object_name_end = data['data'].indexOf('=') - 1,
object_name = data['data'].substring( object_name_start, object_name_end );
if ( typeof window[object_name] === "undefined") {
// try to parse the data, but please, no errors here
try {
window[object_name] = JSON.parse( data['data'].substring( data['data'].indexOf('=') + 2, data['data'].length - 1 ) );
} catch (e) {
console.log( e );
}
}
}
$.getScript(url)
.done(function (script, textStatus) {
//console.log(textStatus);
$(document).trigger('timber-lite' + key + ':script:loaded');
//console.log( 'timber:' + key + ':script:loaded' );
})
.fail(function (jqxhr, settings, exception) {
if (globalDebug) {
console.log('I failed');
console.log( exception );
}
});
if (globalDebug) {console.groupEnd();}
$(document).trigger('timber:page_scripts:loaded');
});
$.each( window.timber_dynamic_loaded_styles, function (key, url) {
if (key in timber_static_resources.styles) return;
if (globalDebug) {console.dir("Styles loaded dynamic");}
if (globalDebug) {console.dir(key);}
if (globalDebug) {console.log(url);}
// add this style to our global stack so we don't enqueue it again
timber_static_resources.styles[key] = url;
// sorry no cache this time
$.ajax({
url: url,
dataType: 'html',
success: function (data) {
$('<style type="text/css">\n' + data + '</style>').appendTo("head");
}
});
if (globalDebug) {console.groupEnd();}
$(document).trigger('timber:page_styles:loaded');
});
});
})(jQuery);
</script>
</div>
<?php
}
add_action('wp_footer', 'timber_lite_last_function', 999999);
/**
* Registers/enqueues the scripts related to media JS APIs
*
*/
function timber_lite_wp_enqueue_media() {
// this will ensure all the resources needed for the media modal
wp_enqueue_media();
wp_enqueue_script( 'timber-image-widget-admin', get_template_directory_uri() . '/inc/widgets/assets/image.js', array(
'media-upload',
'media-views',
) );
wp_localize_script(
'timber-image-widget-admin',
'TimberImageWidget',
array(
'l10n' => array(
'frameTitle' => esc_html__( 'Choose an Image', 'timber-lite' ),
'frameUpdateText' => esc_html__( 'Update Image', 'timber-lite' ),
),
)
);
}
add_action( 'admin_enqueue_scripts', 'timber_lite_wp_enqueue_media' );
/**
* And all the activation hooks.
*/
require_once get_template_directory() . '/inc/activation.php';
/**
* Load various plugin integrations
*/
require_once get_template_directory() . '/inc/integrations.php';
/**
* MB string functions for when the MB library is not available
*/
require_once get_template_directory() . '/inc/mb_compat.php';
/**
* Custom template tags for this theme.
*/
require_once get_template_directory() . '/inc/template-tags.php';
/**
* Custom functions that act independently of the theme templates.
*/
require_once get_template_directory() . '/inc/extras.php';
/**
* Load the Hybrid Media Grabber class
*/
require_once get_template_directory() . '/inc/hybrid-media-grabber.php';
/**
* Customizer additions.
*/
require_once get_template_directory() . '/inc/customizer.php';
/**
* Admin dashboard related logic.
*/
require_once trailingslashit( get_template_directory() ) . 'inc/admin.php';