-
Notifications
You must be signed in to change notification settings - Fork 0
/
twig-setup.php
120 lines (109 loc) · 3.94 KB
/
twig-setup.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
<?php
/*
* This file loads Twig and adds custom filters to it that interact with WordPress
*/
// Load Twig
if(!class_exists("Twig_Autoloader")) {
require_once dirname(__FILE__) . '/Twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
}
// Initiate Twig by passing in an empty loader (allowing us to pass strings)
$LoopShortcodeTwigLoader = new Twig_Loader_Array(array());
$LoopShortcodeTwig = new Twig_Environment($LoopShortcodeTwigLoader);
/*******************************************************************************
* Twig WordPress helper extensions
******************************************************************************/
/*
* For word concatination. It uses a very simple algorithm that sees words as
* spaces.
*
* Use:
* {{ excerpt|words(50,'...') }}
*/
if(!function_exists("loop_shortcode_words")):
function loop_shortcode_words($string, $words = 50, $append = '...', $trim_punctuation = true) {
// Simple, quick way
$return = implode(' ',
array_slice(
explode(' ', strip_tags($string)),
0, $words)
);
// Remove trailing small punctuation
if($trim_punctuation) {
if(in_array(substr($return, -1),str_split('.,/@\\&-=+_|#$%^*;:'))) {
$return = substr($return, 0, -1);
}
}
$return .= $append;
return $return;
}
$filter = new Twig_SimpleFilter('words', 'loop_shortcode_words');
$LoopShortcodeTwig->addFilter($filter);
endif;
/*
* For title concatenation. Returns everything before the first colon,
* surrounded by the first and second arguments
*
* Use:
* {{ excerpt|title('<span class="maintitle">','</span>') }}
*/
if(!function_exists("loop_shortcode_title")):
function loop_shortcode_title($string, $prepend = '', $append = '') {
if(strpos($string, ':') === FALSE) return $string;
// Simple, quick way
return $prepend
. trim(strstr($string, ':', true))
. $append;
}
$filter = new Twig_SimpleFilter('title', 'loop_shortcode_title');
$LoopShortcodeTwig->addFilter($filter);
endif;
/*
* For subtitle concatenation. Returns everything after the first colon,
* surrounded by the first and second arguments.
*
* Use:
* {{ excerpt|subtitle('<span class="subtitle">','</span>') }}
*/
if(!function_exists("loop_shortcode_subtitle")):
function loop_shortcode_subtitle($string, $prepend = '', $append = '') {
if(strpos($string, ':') === FALSE) return '';
// Simple, quick way
return $prepend
. trim(substr($string, (-1 * (strlen($string) - strpos($string, ':') - 1))))
. $append;
}
$filter = new Twig_SimpleFilter('subtitle', 'loop_shortcode_subtitle');
$LoopShortcodeTwig->addFilter($filter);
// For outputting text safe for use in title tags
// Use: <a href="{{ link }}" title="View full post of {{ title|titlesafe }}">{{ title }}</a>
function loop_shortcode_titlesafe($string) {
return str_replace(array("\r\n","\r","\n"), ' ', htmlspecialchars(strip_tags($string)));
}
$filter = new Twig_SimpleFilter('titlesafe', 'loop_shortcode_titlesafe');
$LoopShortcodeTwig->addFilter($filter);
endif;
/*
* For getting author meta from an ID. For more information, see the WordPress
* manual on get_the_author_meta.
*
* Use:
* {{ otherauthor.id|wpauthormeta('display_name') }}
*/
if(!function_exists("loop_shortcode_wpauthormeta")):
function loop_shortcode_wpauthormeta($id, $options = array()) {
$id = (int) $id;
$field = (is_array($options)) ? $options[0] : $options;
return get_the_author_meta($field, $id);
}
$filter = new Twig_SimpleFilter('wpauthormeta', 'loop_shortcode_wpauthormeta');
$LoopShortcodeTwig->addFilter($filter);
// For getting the author posts page
// use {{ otherauthor.id|wpgetauthorpage }}
function loop_shortcode_wpgetauthorpage($id) {
$id = (int) $id;
return get_author_posts_url($id);
}
$filter = new Twig_SimpleFilter('wpgetauthorpage', 'loop_shortcode_wpgetauthorpage');
$LoopShortcodeTwig->addFilter($filter);
endif;