/
bfox_refs_controller.php
242 lines (205 loc) · 5.67 KB
/
bfox_refs_controller.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
<?php
class BfoxRefsController extends BfoxPluginController {
/**
* @var BfoxCoreController
*/
var $core;
/**
* @var BfoxOptions
*/
var $options;
function init() {
parent::init();
$this->options = new BfoxOptions('bfox_refs');
}
/**
* Returns a string of links for the passed in Bible reference, uses separate links for each individual range of verses
*
* Example: Genesis 1-2,4 would become two separate links [Genesis 1-2]; [Genesis 4] because chapters 1-2 are not contiguous with chapter 4
* For returning [Genesis 1-2,4] use the link() function
*
* @param mixed $ref string or BfoxRef
* @param array $attributes Attributes for the links
* @return string
*/
function links($ref, $attributes = array()) {
if (!is_a($ref, 'BfoxRef')) {
$ref = new BfoxRef($ref);
}
$serializer = new BfoxRefSerializer();
$serializer->setCombineNone();
$elements = $serializer->elementsForRef($ref);
$linksString = '';
foreach ($elements as $index => $element) {
if ($index % 2 == 0) $linksString .= $this->link($element, $attributes);
else $linksString .= $element;
}
return $linksString;
}
/**
* Returns a single link for the passed in Bible reference
*
* Example: Genesis 1-2,4 would become two separate links [Genesis 1-2,4]
* For returning separate links - [Genesis 1-2]; [Genesis 4], use the links() function
*
* @param mixed $refStr string or BfoxRef
* @param array $attributes Attributes for the links
* @return string
*/
function link($refStr, $attributes = array()) {
if (is_a($refStr, 'BfoxRef')) {
$refStr = $refStr->get_string();
}
return $this->linkForRefStr($refStr, $attributes);
}
function url($refStr) {
if (is_a($refStr, 'BfoxRef')) {
$refStr = $refStr->get_string();
}
return $this->urlForRefStr($refStr);
}
function linkForRefStr($refStr, $attributes = array()) {
$linker = $this->currentLinker();
return $linker->linkForRefStr($refStr, $attributes);
}
function urlForRefStr($refStr) {
$url = '';
if (!is_null($this->core->tools)) {
$url = $this->core->tools->urlForRefStr($refStr);
}
else if (!is_null($this->core->index)) {
$url = $this->core->index->urlForRefStr($refStr);
}
return $url;
}
function linkForTextAndRef($text, BfoxRef $ref) {
return $this->linkForRefStr($ref->get_string(), array('text' => $text));
}
function pushLinker(BfoxRefLinker $linker) {
$this->core->stackGroup->push('refLinker', $linker);
}
/**
* @return BfoxRefLinker
*/
function popLinker() {
return $this->core->stackGroup->pop('refLinker');
}
/**
* @return BfoxRefLinker
*/
function currentLinker() {
return $this->core->stackGroup->current('refLinker');
}
private $contexts = array();
var $mainContextName = 'main';
/**
* @param string $name
* @return BfoxRefContext
*/
function contextForName($name) {
if (empty($name)) return null;
if (!isset($this->contexts[$name])) {
$context = new BfoxRefContext($name, $this);
$this->contexts[$name] = $context;
}
return $this->contexts[$name];
}
/**
* @return BfoxRefContext
*/
function mainContext() {
return $this->contextForName($this->mainContextName);
}
function pushContext(BfoxRefContext $context) {
$this->core->stackGroup->push('refContext', $context);
$linker = $this->basicLinkerForContext($context);
$this->pushLinker($linker);
}
/**
* @return BfoxRefContext
*/
function popContext() {
$this->popLinker();
return $this->core->stackGroup->pop('refContext');
}
/**
* @return BfoxRefContext
*/
function currentContext() {
return $this->core->stackGroup->current('refContext');
}
function replaceCallback() {
$linker = $this->currentLinker();
return $linker->replaceCallback();
}
/**
* @return BfoxRefLinker
*/
function basicLinker($useTooltips = true) {
$linker = new BfoxRefLinker();
$linker->attributeCallbacks['href'] = $this->functionWithName('urlForRefStr');
if ($useTooltips && !is_null($this->core->tooltips)) {
$linker->addClass($this->core->tooltips->linkClass);
}
return $linker;
}
/**
* @return BfoxRefLinker
*/
function basicLinkerForContext(BfoxRefContext $context, $useTooltips = false) {
$linker = $this->basicLinker($useTooltips);
$linker->addClass('bfox-ref-context-updater');
$linker->attributeValues['data-selector'] = '.' . $context->dependencyName;
return $linker;
}
/**
* Replaces bible references with bible links in a given html string
* @param string $content
* @return string
*/
function replaceInHTML($html, $callback = '') {
if (empty($callback)) $callback = $this->replaceCallback();
return BfoxRefParser::simple_html($html, null, $callback);
}
/**
* Finds any bible references in an array of tag links and replaces them with the result of a callback
*
* @param array $tag_links
* @return array
*/
function replaceInTagLinks($tag_links, $callback = '') {
if (empty($callback)) $callback = $this->replaceCallback();
if (!empty($tag_links)) foreach ($tag_links as &$tag_link) if (preg_match('/<a.*>(.*)<\/a>/', $tag_link, $matches)) {
$tag = $matches[1];
$ref = $this->refFromTag($tag);
if ($ref->is_valid()) {
$tag_link = call_user_func_array($callback, array($tag, $ref));
}
}
return $tag_links;
}
/**
* Returns a BfoxRef for the given tag string
*
* @param string $tag
* @return BfoxRef
*/
function refFromTag($tag) {
return BfoxRefParser::simple($tag);
}
/**
* Returns a BfoxRef for the given post content string
*
* @param string $content
* @return BfoxRef
*/
function refFromPostContent($content) {
$ref = new BfoxRef;
BfoxRefParser::simple_html($content, $ref);
return $ref;
}
function wpInit() {
$this->pushLinker($this->basicLinker());
}
}
?>