forked from VBog/Bg-Bible-References
/
bg_bibrefs.php
823 lines (744 loc) · 35.1 KB
/
bg_bibrefs.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
<?php
/*
Plugin Name: Bg Bible References
Plugin URI: http://bogaiskov.ru/bg_bibfers/
Description: Плагин подсвечивает ссылки на текст Библии с помощью гиперссылок на сайт <a href="http://azbyka.ru/">Православной энциклопедии "Азбука веры"</a> и толкование Священного Писания на сайте <a href="http://bible.optina.ru/">монастыря "Оптина Пустынь"</a>. / The plugin will highlight references to the Bible text with links to site of <a href="http://azbyka.ru/">Orthodox encyclopedia "The Alphabet of Faith"</a> and interpretation of Scripture on the site of the <a href="http://bible.optina.ru/">monastery "Optina Pustyn"</a>.
Version: 3.10.3
Author: VBog
Author URI: http://bogaiskov.ru
License: GPL2
Text Domain: bg_bibfers
Domain Path: /languages/
*/
/* Copyright 2015 Vadim Bogaiskov (email: vadim.bogaiskov@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*****************************************************************************************
Блок загрузки плагина
******************************************************************************************/
// Запрет прямого запуска скрипта
if ( !defined('ABSPATH') ) {
die( 'Sorry, you are not allowed to access this page directly.' );
}
define('BG_BIBREFS_VERSION', '3.10.3');
// Таблица стилей для плагина
function bg_enqueue_frontend_styles () {
wp_enqueue_style( "bg_bibfers_styles", plugins_url( '/css/styles.css', plugin_basename(__FILE__) ), array() , BG_BIBREFS_VERSION );
}
add_action( 'wp_enqueue_scripts' , 'bg_enqueue_frontend_styles' );
add_action( 'admin_enqueue_scripts' , 'bg_enqueue_frontend_styles' );
// JS скрипт
function bg_enqueue_frontend_scripts () {
$bg_preq_val = get_option( 'bg_bibfers_prereq' );
if ($bg_preq_val == 'on') $preq = 1;
else $preq = 0;
wp_enqueue_script( 'bg_bibrefs_proc', plugins_url( 'js/bg_bibrefs.js?preq='.$preq , __FILE__ ), false, BG_BIBREFS_VERSION, true );
}
function bg_bibrefs_js_options () {
// $content="#content";
$content=get_option( "bg_bibfers_content" );
?>
<script> var bg_bibrefs_content='<?php echo $content; ?>';</script>
<?php
}
if ( !is_admin() ) {
add_action( 'wp_enqueue_scripts' , 'bg_enqueue_frontend_scripts' );
add_action( 'wp_head' , 'bg_bibrefs_js_options' );
}
// Загрузка интернационализации
add_action( 'plugins_loaded', 'bg_bibfers_load_textdomain' );
function bg_bibfers_load_textdomain() {
load_plugin_textdomain( 'bg_bibfers', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
// Подключаем дополнительные модули
include_once('includes/settings.php');
include_once('includes/references.php');
include_once('includes/quotes.php');
include_once('includes/search.php');
if ( defined('ABSPATH') && defined('WPINC') ) {
// Регистрируем крючок для обработки контента при его загрузке
add_filter( 'the_content', 'bg_bibfers' );
// Регистрируем крючок для добавления меню администратора
add_action('admin_menu', 'bg_bibfers_add_pages');
// Регистрируем крючок на удаление плагина
if (function_exists('register_uninstall_hook')) {
register_uninstall_hook(__FILE__, 'bg_bibfers_deinstall');
}
// Регистрируем шорт-код bible
add_shortcode( 'bible', 'bg_bibfers_qoutes' );
// Регистрируем шорт-код bible_epigraph
add_shortcode( 'bible_epigraph', 'bg_bibfers_bible_epigraph' );
// Регистрируем шорт-код references
add_shortcode( 'references', 'bg_bibfers_references' );
// Регистрируем шорт-код norefs
add_shortcode( 'norefs', 'bg_bibfers_norefs' );
// Регистрируем шорт-код bible_search
add_shortcode( 'bible_search', 'bg_bibfers_bible_search' );
// Регистрируем шорт-код bible_omnisearch
add_shortcode( 'bible_omnisearch', 'bg_bibfers_bible_omnisearch' );
// Инициализируем значения параметров настройки плагина по умолчанию
bg_bibrefs_options_ini ();
}
/*****************************************************************************************
Функции установки языка Библии
******************************************************************************************/
function set_bible_lang() {
global $post;
if (function_exists ( 'bg_custom_lang' )) $blog_lang = bg_custom_lang(); // Если определена внешняя функция определения языка, то используем ее
if (!$blog_lang) $blog_lang = get_bloginfo('language'); // Если язык не задан, берем язык блога
$blog_lang = substr($blog_lang, 0, 2);
$bg_verses_lang_val = get_option( 'bg_bibfers_verses_lang' ); // Задан язык Библии в настройках плагина
$bible_lang = ((!$bg_verses_lang_val)?$blog_lang:$bg_verses_lang_val);
$bible_lang_posts_val = ($post)?get_post_meta($post->ID, 'bible_lang', true):""; // Задан язык Библии для поста
if ($bible_lang_posts_val) {
$bible_lang = $bible_lang_posts_val;
}
$file_books = dirname( __FILE__ ).'/bible/'.$blog_lang.'/books.php'; // Если для этого языка отсутствует каталог с Библией
if (!file_exists($file_books)) $blog_lang = 'en'; // По умолчанию английский язык
return $bible_lang;
}
/*****************************************************************************************
Функции подключения языкового файла списка книг Библии
******************************************************************************************/
function include_books($lang) {
global $bg_bibfers_lang_name, $bg_bibfers_chapter, $bg_bibfers_ch;
global $bg_bibfers_url, $bg_bibfers_bookTitle, $bg_bibfers_shortTitle, $bg_bibfers_bookFile;
$file_books = dirname( __FILE__ ).'/bible/'.$lang.'/books.php';
if (!file_exists($file_books)) $lang = set_bible_lang(); // Если язык задан неверно, устанавливаем язык системы
include(dirname(__FILE__ ).'/bible/'.$lang.'/books.php');
return $lang;
}
/*****************************************************************************************
Функции запуска плагина
******************************************************************************************/
// Функция обработки ссылок на Библию
function bg_bibfers($content) {
$content = bg_bibfers_bible_proc($content);
return $content;
}
// Функция действия перед крючком добавления меню
function bg_bibfers_add_pages() {
// Добавим новое подменю в раздел Параметры
add_options_page( __('Bible References', 'bg_bibfers' ), __('Bible References', 'bg_bibfers' ), 'manage_options', __FILE__, 'bg_bibfers_options_page');
}
/*****************************************************************************************
Шорт-коды
Функции обработки шорт-кода
******************************************************************************************/
// [bible]
function bg_bibfers_qoutes( $atts, $content=null ) {
extract( shortcode_atts( array(
'ref' => '',
'book' => '',
'ch' => '1-999',
'type' => 'verses',
'lang' => '',
'prll' => ''
), $atts ) );
// Если $ref задано значение "get", то получаем $book и $ch из ссылки
if ($ref == "get") {
$ref = $_GET["bs"];
$book = $_GET["book"];
$ch = $_GET["ch"];
if ($ch == "") $ch = "1-999";
$l = $_GET["lang"];
if ($l != "") $lang = $l;
$prll = $_GET["prll"];
}
// это и все нововведения для версии 3.7
$book = bg_bibfers_getBook($book);
if (!$lang) $lang = set_bible_lang();
if ($ref == "rnd" || $ref == "days" || is_numeric ($ref)) $ref = bg_bibfers_bible_quote_refs($ref, $lang);
if ($content) $quote = bg_bibfers_bible_proc($content, $type, $lang, $prll);
else if ($ref) $quote = bg_bibfers_bible_proc($ref, $type, $lang, $prll);
else if ($book != '') {
if ($type == 'link') $quote = '('.bg_bibfers_get_url($book, $ch, bg_bibfers_getshortTitle($book).' '.$ch, $lang).')';
else $quote = bg_bibfers_getQuotes($book, $ch, $type, $lang, $prll);
}
else return "";
if ($quote != "") {
$class_val = get_option( 'bg_bibfers_class' );
if ($class_val == "") $class_val = 'bg_bibfers';
$quote = "<span class='".$class_val."'>".$quote."</span>";
}
return "{$quote}";
}
// [bible_epigraph]
function bg_bibfers_bible_epigraph( $atts ) {
extract( shortcode_atts( array(
'ref' => 'rnd',
'lang' => ''
), $atts ) );
$quote = bg_bibfers_get_bible_epigraph( $ref, $lang );
return "{$quote}";
}
function bg_bibfers_get_bible_epigraph( $ref, $lang ) {
if (!$lang) $lang = set_bible_lang();
if ($ref == "rnd" || $ref == "days" || is_numeric ($ref)) $ref = bg_bibfers_bible_quote_refs($ref, $lang);
if ($ref != "") $quote = bg_bibfers_bible_proc($ref, 'quote', $lang);
if ($quote != "") {
$class_val = get_option( 'bg_bibfers_class' );
if ($class_val == "") $class_val = 'bg_bibfers';
$quote = "<span class='".$class_val."'>«".$quote."...» (".$ref.")</span>";
}
return $quote;
}
// [references]
function bg_bibfers_references( $atts ) {
extract( shortcode_atts( array(
'type' => 'list',
'separator' => ', ',
'list' => 'o',
'col' => 1
), $atts ) );
global $bg_bibfers_all_refs;
$references = '<div class="bg_refs_list">';
$j=0;
$cnt = count($bg_bibfers_all_refs);
for ($i = 0; $i < $cnt; $i++) {
$ref = $bg_bibfers_all_refs[$i];
switch ($type) {
case 'string':
if ($i == 0) $references .= '<p>';
$references .= $ref;
if ($i == $cnt-1) $references .= '</p>';
else $references .= $separator;
break;
case 'list':
if ($list == 'u' || $list == 'o') {
if ($i == 0) $references .= '<table><tr valign="top"><td><'.$list.'l>';
$references .= '<li>'.$ref.'</li>';
if (!(($i+1) % ceil($cnt/$col)) && $i+1 < $cnt) $references .= '</'.$list.'l></td><td><'.$list.'l start="'.($i+2).'">';
if ($i == $cnt-1) $references .= '</'.$list.'l></td></tr></table>';
}
break;
case 'table':
if ($i == 0) $references .= '<table>';
if ($j == 0) $references .= '<tr>';
$references .= '<td>'.$ref.'</td>';
if ($j == $col-1) $references .= '</tr>';
$j++;
if ($j == $col) $j = 0;
if ($i == $cnt-1) {
while ($j < $col) {
$references .= '<td> </td>';
$j++;
}
$references .= '</table>';
}
break;
}
}
$references .= '</div>';
return "{$references}";
}
// [norefs]
function bg_bibfers_norefs( $atts, $content = null ) {
return do_shortcode($content);
}
// [bible_search]
function bg_bibfers_bible_search( $atts ) {
extract( shortcode_atts( array(
'context' => 'get',
'book' => '',
'ch' => '1-999',
'type' => 'b_verses',
'lang' => '',
'prll' => ''
), $atts ) );
// Если $context задано значение "get", то получаем $context из ссылки
if ($context == "get") {
$context = $_GET["bs"];
$book = $_GET["book"];
$ch = $_GET["ch"];
if ($ch == "") $ch = "1-999";
$l = $_GET["lang"];
if ($l != "") $lang = $l;
$prll = $_GET["prll"];
if (!isset($_GET["bs"]) && !isset($_GET["book"])) {
$keys = array_keys($_GET); $context = $keys[0];
$context = str_replace ( '_' , ' ' , $context );
$context = trim ($context);
if ($context == "ch" || $context == "type" || $context == "lang" || $context == "prll") $context = '';
}
}
if ($book) {
$book = bg_bibfers_getBook($book);
$context = $book.$ch;
}
$context = trim($context);
if (!$context) return "";
$quote = bg_bibfers_bible_proc($context, $type, $lang, $prll);
if (!$quote || $quote == $context) $quote = bg_bibfers_search_result($context, $type, $lang, $prll);
if ($quote != "") {
$class_val = get_option( 'bg_bibfers_class' );
if ($class_val == "") $class_val = 'bg_bibfers';
$quote = "<span class='".$class_val."'>".$quote."</span>";
}
return "{$quote}";
}
// [bible_omnisearch]
function bg_bibfers_bible_omnisearch( $atts ) {
global $bg_bibfers_url, $bg_bibfers_bookTitle, $bg_bibfers_shortTitle, $bg_bibfers_bookFile;
extract( shortcode_atts( array(
'lang' => '',
'page' => ''
), $atts ) );
$quote = '';
if (!$lang) $lang = set_bible_lang();
$lang = include_books($lang);
if (!$page) $page = get_permalink();
// Искомое слово или фраза
$quote .= '<input class="required" id="bg_omnisearch_ptrnId" type="search" placeholder="'.__('Search', 'bg_bibfers' ).'…" value="" style="width:100%" onblur= "bg_omnisearch_goToPage()" onkeypress="return bg_omnisearch_testKey(event)">';
// Ссылка на страницу в шорт-кодом
$quote .= '<input id="bg_omnisearch_pageId" type="hidden" value="'. $page .'">';
// Язык Библии
$quote .= '<input id="bg_omnisearch_langId" type="hidden" value="'. $lang. '">';
$quote .= '<script>
if (window.localStorage["bg_omnisearch_ptrn"]) {
document.getElementById("bg_omnisearch_ptrnId").value = window.localStorage["bg_omnisearch_ptrn"];
}
function bg_omnisearch_goToPage() {
var bg_omnisearch_page = document.getElementById("bg_omnisearch_pageId").value;
var bg_omnisearch_lang = document.getElementById("bg_omnisearch_langId").value;
var bg_omnisearch_ptrn = document.getElementById("bg_omnisearch_ptrnId").value;
document.location.href = encodeURI(bg_omnisearch_page + "?bs=" + bg_omnisearch_ptrn + "&lang=" + bg_omnisearch_lang);
window.localStorage["bg_omnisearch_ptrn"] = bg_omnisearch_ptrn;
}
function bg_omnisearch_testKey(e) {
// Проверяем доступность event.charCode
var key = (typeof e.charCode == "undefined" ? e.keyCode : e.charCode);
// Нажата клавиша Enter ?
if (key == 13) {bg_omnisearch_goToPage(); return false;}
else return true;
}
</script>';
return "{$quote}";
}
/*****************************************************************************************
Генератор ответа AJAX
******************************************************************************************/
add_action ('wp_ajax_bg_bibrefs', 'bg_bibrefs_callback');
add_action ('wp_ajax_nopriv_bg_bibrefs', 'bg_bibrefs_callback');
function bg_bibrefs_callback() {
$title = $_GET["title"];
$chapter = $_GET["chapter"];
if (!$chapter) $chapter = '1-999';
$type = $_GET["type"];
$lang = $_GET["lang"];
if (!$type) $type = 'verses';
$expand_button = '<img src="'.plugins_url( '/js/expand.png' , __FILE__ ).'" style="cursor:pointer; margin-right: 8px;" align="left" width=16 height=16 title1="'.(__('Expand', 'bg_bibfers' )).'" title2="'.(__('Hide', 'bg_bibfers' )).'" />';
echo $expand_button.bg_bibfers_getQuotes($title, $chapter, $type, $lang);
die();
}
function get_plugin_version() {
$plugin_data = get_plugin_data( __FILE__ );
return $plugin_data['Version'];
}
/*****************************************************************************************
Добавляем блок в основную колонку на страницах постов и пост. страниц
******************************************************************************************/
add_action('admin_init', 'bg_bibfers_extra_fields', 1);
// Создание блока
function bg_bibfers_extra_fields() {
add_meta_box( 'bg_bibfers_extra_fields', __('Bible References', 'bg_bibfers'), 'bg_bibfers_extra_fields_box_func', 'post', 'normal', 'high' );
}
// Добавление полей
function bg_bibfers_extra_fields_box_func( $post ){
?>
<label for="bg_verses_lang"><?php _e('Language of references and tooltips', 'bg_bibfers' ); ?></label>
<select id="bg_verses_lang" name="bg_bibfers_extra[bible_lang]">
<?php $bg_verses_lang_val = get_post_meta($post->ID, 'bible_lang', 1); ?>
<option <?php if($bg_verses_lang_val=="") echo "selected" ?> value=""><?php _e('Default', 'bg_bibfers' ); ?></option>
<?php $path = dirname( __FILE__ ).'/bible/';
if ($handle = opendir($path)) {
while (false !== ($dir = readdir($handle))) {
if (is_dir ( $path.$dir ) && $dir != '.' && $dir != '..') {
include ($path.$dir.'/books.php');
echo "<option ";
if($bg_verses_lang_val==$dir) echo "selected";
echo " value=".$dir.">".$bg_bibfers_lang_name."</option>\n";
}
}
closedir($handle);
} ?>
</select>
<label for="bg_norefs"><?php _e('Ban to highlight references', 'bg_bibfers' ); ?></label>
<select id="bg_norefs" name="bg_bibfers_extra[norefs]">
<?php $bg_norefs_val = get_post_meta($post->ID, 'norefs', 1); ?>
<option <?php if($bg_norefs_val=="") echo "selected" ?> value=""><?php _e('Off', 'bg_bibfers' ); ?></option>
<option <?php if($bg_norefs_val) echo "selected" ?> value="on"><?php _e('On', 'bg_bibfers' ); ?></option>
</select>
<input type="hidden" name="bg_bibfers_extra_fields_nonce" value="<?php echo wp_create_nonce(__FILE__); ?>" />
<?php
}
// Сохранение значений произвольных полей при автосохранении поста
add_action('save_post', 'bg_bibfers_extra_fields_update', 0);
// Сохранение значений произвольных полей при сохранении поста
function bg_bibfers_extra_fields_update( $post_id ){
if (!isset ($_POST['bg_bibfers_extra_fields_nonce']) ) return false;
if ( !wp_verify_nonce($_POST['bg_bibfers_extra_fields_nonce'], __FILE__) ) return false;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return false;
if ( !current_user_can('edit_post', $post_id) ) return false;
if( !isset($_POST['bg_bibfers_extra']) ) return false;
$_POST['bg_bibfers_extra'] = array_map('trim', $_POST['bg_bibfers_extra']);
foreach( $_POST['bg_bibfers_extra'] as $key=>$value ){
if( empty($value) ) {
delete_post_meta($post_id, $key);
continue;
}
update_post_meta($post_id, $key, $value);
}
return $post_id;
}
/*****************************************************************************************
Виджет для ввода ссылки на Библию
******************************************************************************************/
class BibleWidget extends WP_Widget
{
public function __construct() {
parent::__construct("bg_bibfers_bible_widget", __('Bible References Widget', 'bg_bibfers' ),
array("description" => __('Create reference to the Bible', 'bg_bibfers' )));
}
// Создаем форму для ввода данных на странице виджетов
public function form($instance) {
$title = "";
$page = "";
// если instance не пустой, достанем значения
if (!empty($instance)) {
$title = $instance["title"];
$page = $instance["page"];
$dlang = $instance["dlang"];
$storage = $instance["storage"];
}
// Заголовок виджета в сайдбаре
$titleId = $this->get_field_id("title");
$titleName = $this->get_field_name("title");
echo '<p><label for="' . $titleId . '">' . __('Title:', 'bg_bibfers' ) . '</label><br>';
echo '<input id="' . $titleId . '" type="text" name="' . $titleName . '" value="' . $title . '" size="50"></p>';
// Ссылка на предварительно созданную страницу для вывода текста Библии
$pageId = $this->get_field_id("page");
$pageName = $this->get_field_name("page");
echo '<p><label for="' . $pageId . '">' . __('Permalink to page:', 'bg_bibfers' ) . '</label><br>';
echo '<input id="' . $pageId . '" type="text" name="' . $pageName . '" value="' . $page . '" size="50"></p>';
// Язык Библии по-умолчанию
$dlangId = $this->get_field_id("dlang");
$dlangName = $this->get_field_name("dlang");
echo '<p><input id="' . $dlangId . '" type="checkbox" name="' . $dlangName. '"';
if ($dlang) echo " checked";
echo '>' . __('Default language', 'bg_bibfers' ) . '</p>';
// Сохранять параметры выбора (на стороне пользователя)
$storageId = $this->get_field_id("storage");
$storageName = $this->get_field_name("storage");
echo '<p><input id="' . $storageId . '" type="checkbox" name="' . $storageName. '"';
if ($storage) echo " checked";
echo '>' . __('Save selected options (on user side only)', 'bg_bibfers' ) . '</p>';
}
// Сохранение настроек
public function update($newInstance, $oldInstance) {
$values = array();
$values["title"] = htmlentities($newInstance["title"], ENT_COMPAT | ENT_HTML401, "UTF-8");
$values["page"] = htmlentities($newInstance["page"], ENT_COMPAT | ENT_HTML401, "UTF-8");
$values["dlang"] = htmlentities($newInstance["dlang"], ENT_COMPAT | ENT_HTML401, "UTF-8");
$values["storage"] = htmlentities($newInstance["storage"], ENT_COMPAT | ENT_HTML401, "UTF-8");
return $values;
}
// Отображение виджета непосредственно в сайдбаре на сайте
public function widget($args, $instance) {
global $bg_bibfers_url, $bg_bibfers_bookTitle, $bg_bibfers_shortTitle, $bg_bibfers_bookFile;
if (!$lang) $lang = set_bible_lang();
$lang = include_books($lang);
$num_books = count($bg_bibfers_bookTitle);
$books = array_keys ( $bg_bibfers_bookTitle);
$title = $instance["title"];
$page = $instance["page"];
$dlang = $instance["dlang"];
$storage = $instance["storage"];
?>
<aside id="bg-bibrefs-1" class="widget widget_bg-bibrefs">
<h2 class="widget-title"><?php echo $title; ?></h2>
<!-- Ссылка на страницу в шорт-кодом -->
<input id="bg_quote_pageId" type="hidden" value="<?php echo $page; ?>">
<!-- Список книг Библии -->
<p><label class="widget-title" for="bg_quote_bookId"><?php _e('Book', 'bg_bibfers' ); ?></label><br>
<select class="required" id="bg_quote_bookId">
<?php for ($i = 0; $i< $num_books; $i++) {
echo "<option value=".$books[$i].">".$bg_bibfers_bookTitle[$books[$i]]."</option>\n";
} ?>
</select><br>
<!-- Номера глав и стихов -->
<label class="widget-title" for="bg_quote_chId"><?php _e('Chapter', 'bg_bibfers' ); ?></label><br>
<input class="required" id="bg_quote_chId" type="text" value="" onkeypress="return bg_quote_testKey(event)" placeholder="<?php _e('Chapters and verses', 'bg_bibfers' ); ?>…"><br>
<!-- Язык Библии -->
<?php if (!$dlang) { ?>
<label class="widget-title" for="bg_quote_langId"><?php _e('Language', 'bg_bibfers' ); ?></label><br>
<select class="required" id="bg_quote_langId">
<?php $path = dirname( __FILE__ ).'/bible/';
if ($handle = opendir($path)) {
while (false !== ($dir = readdir($handle))) {
if (is_dir ( $path.$dir ) && $dir != '.' && $dir != '..') {
include ($path.$dir.'/books.php');
echo "<option ";
if($lang==$dir) echo "selected";
echo " value=".$dir.">".$bg_bibfers_lang_name."</option>\n";
}
}
closedir($handle);
} ?>
</select>
<?php } else { ?>
<input id="bg_quote_langId" type="hidden" value="">
<?php } ?>
</p>
<p><input type="submit" value="<?php _e('Go', 'bg_bibfers' ); ?>" onclick="bg_quote_goToPage()"></p>
</aside>
<?php if ($storage) { ?>
<script>
if (window.localStorage['bg_quote_book'])
document.getElementById('bg_quote_bookId').value = window.localStorage['bg_quote_book'];
if (window.localStorage['bg_quote_ch'])
document.getElementById('bg_quote_chId').value = window.localStorage['bg_quote_ch'];
if (window.localStorage['bg_quote_lang'])
document.getElementById('bg_quote_langId').value = window.localStorage['bg_quote_lang'];
</script>
<?php } ?>
<script>
function bg_quote_goToPage() {
var bg_quote_page = document.getElementById('bg_quote_pageId').value;
var bg_quote_book = document.getElementById('bg_quote_bookId').value;
var bg_quote_ch = document.getElementById('bg_quote_chId').value;
var bg_quote_lang = document.getElementById('bg_quote_langId').value;
document.location.href = encodeURI(bg_quote_page + "?book=" + bg_quote_book + ((bg_quote_ch!="")?"&ch=":"") + bg_quote_ch + ((bg_quote_lang!="")?"&lang=":"") + bg_quote_lang);
window.localStorage['bg_quote_book'] = bg_quote_book;
window.localStorage['bg_quote_ch'] = bg_quote_ch;
window.localStorage['bg_quote_lang'] = bg_quote_lang;
}
function bg_quote_testKey(e) {
// Проверяем доступность event.charCode
var key = (typeof e.charCode == 'undefined' ? e.keyCode : e.charCode);
// Игнорируем специальные кнопки
if (e.ctrlKey || e.altKey || key < 32) return true;
key = String.fromCharCode(key);
// Допустимы цифры, запятая, двоеточие и тире
return /[\d\,\:\-]/.test(key);
}
</script>
<?php
}
}
/*****************************************************************************************
Виджет для поиска в Библии
******************************************************************************************/
class BibleSearchWidget extends WP_Widget
{
public function __construct() {
parent::__construct("bg_bibfers_bible_search_widget", __('Bible Search Widget', 'bg_bibfers' ),
array("description" => __('Finds words or phrases in the Bible', 'bg_bibfers' )));
}
// Создаем форму для ввода данных на странице виджетов
public function form($instance) {
$title = "";
$page = "";
// если instance не пустой, достанем значения
if (!empty($instance)) {
$title = $instance["title"];
$page = $instance["page"];
$storage = $instance["storage"];
}
// Заголовок виджета в сайдбаре
$titleId = $this->get_field_id("title");
$titleName = $this->get_field_name("title");
echo '<p><label for="' . $titleId . '">' . __('Title:', 'bg_bibfers' ) . '</label><br>';
echo '<input id="' . $titleId . '" type="text" name="' . $titleName . '" value="' . $title . '" size="50"></p>';
// Ссылка на предварительно созданную страницу для вывода результатов поиска
$pageId = $this->get_field_id("page");
$pageName = $this->get_field_name("page");
echo '<p><label for="' . $pageId . '">' . __('Permalink to page:', 'bg_bibfers' ) . '</label><br>';
echo '<input id="' . $pageId . '" type="text" name="' . $pageName . '" value="' . $page . '" size="50"></p>';
// Язык Библии по-умолчанию
$dlangId = $this->get_field_id("dlang");
$dlangName = $this->get_field_name("dlang");
echo '<p><input id="' . $dlangId . '" type="checkbox" name="' . $dlangName. '"';
if ($dlang) echo " checked";
echo '>' . __('Default language', 'bg_bibfers' ) . '</p>';
// Сохранять параметры выбора (на стороне пользователя)
$storageId = $this->get_field_id("storage");
$storageName = $this->get_field_name("storage");
echo '<p><input id="' . $storageId . '" type="checkbox" name="' . $storageName. '"';
if ($storage) echo " checked";
echo '>' . __('Save selected options (on user side only)', 'bg_bibfers' ) . '</p>';
}
// Сохранение настроек
public function update($newInstance, $oldInstance) {
$values = array();
$values["title"] = htmlentities($newInstance["title"], ENT_COMPAT | ENT_HTML401, "UTF-8");
$values["page"] = htmlentities($newInstance["page"], ENT_COMPAT | ENT_HTML401, "UTF-8");
$values["dlang"] = htmlentities($newInstance["dlang"], ENT_COMPAT | ENT_HTML401, "UTF-8");
$values["storage"] = htmlentities($newInstance["storage"], ENT_COMPAT | ENT_HTML401, "UTF-8");
return $values;
}
// Отображение виджета непосредственно в сайдбаре на сайте
public function widget($args, $instance) {
global $bg_bibfers_url, $bg_bibfers_bookTitle, $bg_bibfers_shortTitle, $bg_bibfers_bookFile;
if (!$lang) $lang = set_bible_lang();
$lang = include_books($lang);
$num_books = count($bg_bibfers_bookTitle);
$books = array_keys ( $bg_bibfers_bookTitle);
$title = $instance["title"];
$page = $instance["page"];
$storage = $instance["storage"];
?>
<aside id="bg-bibrefs-2" class="widget widget_bg-bibrefs">
<h2 class="widget-title"><?php echo $title; ?></h2>
<!-- Ссылка на страницу в шорт-кодом -->
<input id="bg_search_pageId" type="hidden" value="<?php echo $page; ?>">
<!-- Искомое слово или фраза -->
<input class="required" id="bg_search_ptrnId" type="text" placeholder="<?php _e('Search', 'bg_bibfers' ); ?>…" value=""><br>
<!-- Язык Библии -->
<?php if (!$dlang) { ?>
<label class="widget-title" for="bg_search_langId"><?php _e('Language', 'bg_bibfers' ); ?></label><br>
<select class="required" id="bg_search_langId">
<?php $path = dirname( __FILE__ ).'/bible/';
if ($handle = opendir($path)) {
while (false !== ($dir = readdir($handle))) {
if (is_dir ( $path.$dir ) && $dir != '.' && $dir != '..') {
include ($path.$dir.'/books.php');
echo "<option ";
if($lang==$dir) echo "selected";
echo " value=".$dir.">".$bg_bibfers_lang_name."</option>\n";
}
}
closedir($handle);
} ?>
</select>
<?php } else { ?>
<input id="bg_quote_langId" type="hidden" value="">
<?php } ?>
</p>
<p><input type="submit" value="<?php _e('Search', 'bg_bibfers' ); ?>" onclick="bg_search_goToPage()"></p>
</aside>
<?php if ($storage) { ?>
<script>
if (window.localStorage['bg_search_ptrn'])
document.getElementById('bg_search_ptrnId').value = window.localStorage['bg_search_ptrn'];
if (window.localStorage['bg_quote_lang'])
document.getElementById('bg_search_langId').value = window.localStorage['bg_search_lang'];
</script>
<?php } ?>
<script>
function bg_search_goToPage()
{
var bg_search_page = document.getElementById('bg_search_pageId').value;
var bg_search_ptrn = document.getElementById('bg_search_ptrnId').value;
var bg_search_lang = document.getElementById('bg_search_langId').value;
document.location.href = encodeURI(bg_search_page + "?bs=" + bg_search_ptrn + "&lang=" + bg_search_lang);
window.localStorage['bg_search_ptrn'] = bg_search_ptrn;
window.localStorage['bg_search_lang'] = bg_search_lang;
}
</script>
<?php
}
}
/*****************************************************************************************
Виджет выводит на экран цитату дня из Библии
******************************************************************************************/
class QuotesWidget extends WP_Widget
{
public function __construct() {
parent::__construct("bg_bibfers_quote_widget", __('Bible Quote Widget', 'bg_bibfers' ),
array("description" => __('Display Random Quote or Day\'s Quote from the Bible', 'bg_bibfers' )));
}
// Создаем форму для ввода данных на странице виджетов
public function form($instance) {
$title = "";
$ref = 'days';
$lang = set_bible_lang();
// если instance не пустой, достанем значения
if (!empty($instance)) {
$title = $instance["title"];
$ref = $instance["ref"];
$lang = $instance["lang"];
}
// Заголовок виджета в сайдбаре
$titleId = $this->get_field_id("title");
$titleName = $this->get_field_name("title");
echo '<p><label for="' . $titleId . '">' . __('Title:', 'bg_bibfers' ) . '</label><br>';
echo '<input id="' . $titleId . '" type="text" name="' . $titleName . '" value="' . $title . '" size="50"></p>';
// Тип цитаты
$reId = $this->get_field_id("ref");
$refName = $this->get_field_name("ref");
echo '<p><label for="' . $refId . '">' . __('Type of quote:', 'bg_bibfers' ) . '</label><br>';
echo '<input id="' . $refId . '" type="radio" name="' . $refName . '" value="days"';
if ($ref=="days") echo " checked";
echo '>' . __('Day\'s Quote', 'bg_bibfers' ) . ' ';
echo '<input id="' . $refId . '" type="radio" name="' . $refName . '" value="rnd"';
if ($ref=="rnd") echo " checked";
echo '>' . __('Random Quote', 'bg_bibfers' ) . '</p>';
// Язык цитаты
$langId = $this->get_field_id("lang");
$langName = $this->get_field_name("lang");
echo '<p><label for="' . $langId . '">' . __('Language of the Bible:', 'bg_bibfers' ) . '</label><br>';
echo '<select class="required" id="' . $langId . '" name="'.$langName.'" type="text">';
echo "<option ";
if(!$lang) echo "selected ";
echo " value=''>".__('Default', 'bg_bibfers' )."</option>\n";
$path = dirname( __FILE__ ).'/bible/';
if ($handle = opendir($path)) {
while (false !== ($dir = readdir($handle))) {
if (is_dir ( $path.$dir ) && $dir != '.' && $dir != '..') {
include ($path.$dir.'/books.php');
echo "<option ";
if($lang==$dir) echo "selected ";
echo " value=".$dir.">".$bg_bibfers_lang_name."</option>\n";
}
}
closedir($handle);
}
echo '</select></p>';
}
// Сохранение настроек
public function update($newInstance, $oldInstance) {
$values = array();
$values["title"] = htmlentities($newInstance["title"], ENT_COMPAT | ENT_HTML401, "UTF-8");
$values["ref"] = htmlentities($newInstance["ref"], ENT_COMPAT | ENT_HTML401, "UTF-8");
$values["lang"] = htmlentities($newInstance["lang"], ENT_COMPAT | ENT_HTML401, "UTF-8");
return $values;
}
// Отображение виджета непосредственно в сайдбаре на сайте
public function widget($args, $instance) {
global $bg_bibfers_url, $bg_bibfers_bookTitle, $bg_bibfers_shortTitle, $bg_bibfers_bookFile;
$title = $instance["title"];
$ref = $instance["ref"];
$lang = $instance["lang"];
if (!$lang) $lang = set_bible_lang();
$lang = include_books($lang);
$num_books = count($bg_bibfers_bookTitle);
$books = array_keys ( $bg_bibfers_bookTitle);
?>
<aside id="bg-bibrefs-3" class="widget widget_bg-bibrefs">
<h2 class="widget-title"><?php echo $title; ?></h2>
<p> <?php $quote = bg_bibfers_get_bible_epigraph( $ref, $lang );
echo $quote; ?>
</p>
</aside>
<?php
}
}
// Регистрируем виджеты
function register_widgets() {
register_widget("BibleWidget");
register_widget("BibleSearchWidget");
register_widget("QuotesWidget");
};
add_action("widgets_init", "register_widgets");