source: scripturizer/trunk/scripturizer.php @ 327080

Revision 327080, 31.6 KB checked in by chipbennett, 17 months ago (diff)
Line 
1<?php
2/*
3 * Plugin Name: Scripturizer
4 * Version: 2.1
5 * Plugin URI: http://dev.wp-plugins.org/wiki/Scripturizer
6 * Description: Changes Bible references to hyperlinks.
7 * Author: Dean Peters, ported by Glen Davis, updates by LaurenceO.com
8 * Author URI: http://www.healyourchurchwebsite.com/
9 * License:       GNU General Public License, v2 (or newer)
10 * License URI:  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11 *
12 * Original PERL MovableType Plugin Copyright 2002-2004 Dean Peters
13 * Port to PHP WordPress Plugin Copyright Glen Davis
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * 
24 * Permission is hereby granted, free of charge, to any person obtaining a
25 * copy of this software and associated documentation files (the "Software"),
26 * to deal in the Software without restriction, including without limitation
27 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
28 * and/or sell copies of the Software, and to permit persons to whom the
29 * Software is furnished to do so, subject to the following conditions:
30 *
31 * The above copyright notice and this permission notice shall be included in
32 * all copies or substantial portions of the Software.
33 *
34*/
35
36// you can pick any translation supported by the Bible Gateway, as well as the NRSV, the NET, and the ESV
37// should we add Blue Letter Bible and http://www.zhubert.com/greek as original language options somehow? ....
38// http://www.blueletterbible.org/cgi-bin/tools/printer-friendly.pl?book=Gen&chapter=1&version=heb
39// http://www.blueletterbible.org/cgi-bin/tools/printer-friendly.pl?book=Mat&chapter=1&version=grk
40// the interface on zhubert.com is weird - the NT books are numbered instead of named
41// we should also take a look at the New American Bible - http://www.nccbuscc.com/nab/bible/index.htm
42
43global $scripturizer_translations;
44$scripturizer_translations = array(
45        'KJ21'=>'21st Century King James Version', // BibleGateway
46        'ASV'=>'American Standard Version', // BibleGateway
47        'AMP'=>'Amplified Bible', // BibleGateway
48        'CEB'=>'Common English Bible', // BibleGateway
49        'CEV'=>'Contemporary English Version', // BibleGateway
50        'Darby'=>'Darby Translation', // BibleGateway
51        'DRA'=>'Douay-Rheims 1899 American Edition', // BibleGateway
52        'ESV'=>'English Standard Version', // BibleGateway
53        'GW'=>"GOD'S WORD Bible", // BibleGateway
54        'GNT' => 'Good News Translation', // BibleGateway
55        'HCSB'=>'Holman Christian Standard Bible', // BibleGateway
56        'KJV'=>'King James Version', // BibleGateway
57        'NASB'=>'New American Standard Bible', // BibleGateway
58        'NCV' => 'New Century Version', // BibleGateway
59        'NIRV'=>"New International Readers' Version", // BibleGateway
60        'NIV'=>'New International Version', // BibleGateway
61        'NIV1984'=>'New International Version 1984', // BibleGateway
62        'NIV2010'=>'New International Version 2010', // BibleGateway
63        'NIVUK'=>'New International Version (British Edition)', // BibleGateway
64        'NKJV'=>'New King James Version', // BibleGateway
65        'NLV'=>'New Life Version', // BibleGateway
66        'NLT'=>'New Living Translation', // BibleGateway
67        'MSG'=>'The Message', // BibleGateway
68        'TNIV'=>"Today's New International Version", // BibleGateway
69        'WE'=>'Worldwide English New Testament', // BibleGateway
70        'WYC'=>'Wycliffe New Testament', // BibleGateway
71        'YLT'=>"Young's Literal Translation", // BibleGateway
72        'NET'=>'New English Translation',
73        'NRSV'=>'New Revised Standard Version'
74);
75global $scripturizer_translations_original;
76$scripturizer_translations_original = array(
77        'NA26'=>'Nestle-Aland Greek Text 26th edition',
78        'LXX'=>'Septaugint'
79);
80global $scripturizer_translations_non_english;
81$scripturizer_translations_non_english = array(
82        'AMU'=>'[AMU] Amuzgo de Guerrero', // BibleGateway, Amuzgo
83        'ALAB'=>'[AR] Arab Life Application Bible', // BibleGateway, Arabic
84        'BULG'=>'[BG] Bulgarian Bible', // BibleGateway, Bulgarian
85        'BG1940'=>'[BG] Bulgarian Bible 1940', // BibleGateway, Bulgarian
86        'CCO'=>'[CCO] Chinanteco de Comaltepec', // BibleGateway, Chinanteco
87        'CKW'=>'[CKW] Cakchiquel Occidental', // BibleGateway, Cakchiquel
88        'HCV'=>'[CPF] Haitian Creole Version', // BibleGateway, Creole
89        'SNC'=>'[CS] Slovo na cestu', // BibleGateway
90        'DN1933'=>'[DA] Dette er Biblen pÃ¥ dansk', // BibleGateway
91        'HOF'=>'[DE] Hoffnung für Alle', // BibleGateway, German
92        'LUTH1545'=>'[DE] Luther Bibel 1545', // BibleGateway, German
93        'NVI'=>'[ES] Nueva Versión Internacional', // BibleGateway, Spanish
94        'RVA'=>'[ES] Reina-Valera Antigua', // BibleGateway, Spanish
95        'RVR1960'=>'[ES] Reina-Valera 1960', // BibleGateway, Spanish
96        'RVR1995'=>'[ES] Reina-Valera 1995', // BibleGateway, Spanish
97        'CST'=>'[ES] Castilian', // BibleGateway, Spanish
98        'TLA'=>'[ES] Traducción en lenguaje actual', // BibleGateway, Spanish
99        'LBLA'=>'[ES] La Biblia de las Américas', // BibleGateway, Spanish
100        'LSG'=>'[FR] Louis Segond', // BibleGateway, French
101        'BDS'=>'[FR] La Bible du Semeur', // BibleGateway, French
102        'WHNU'=>'[GRC] Westcott-Hort New Testament 1881', // BibleGateway, Greek (Ancient)
103        'TR1550'=>'[GRC] Stephanus New Testament 1550', // BibleGateway, Greek (Ancient)
104        'TR1894'=>'[GRC] Scrivener New Testament 1894', // BibleGateway, Greek (Ancient)
105        'WLC'=>'[HE]) The Westminster Leningrad Codex', // BibleGateway
106        'HLGN'=>'[HIL] Hiligaynon Bible', // BibleGateway
107        'CRO'=>'[HR] Croatian Bible', // BibleGateway
108        'KAR'=>'[HU] Hungarian Károli', // BibleGateway
109        'ICELAND'=>'[IS] Icelandic Bible', // BibleGateway
110        'LND'=>'[IT] La Nuova Diodati', // BibleGateway
111        'LM'=>'[IT] La Parola è Vita', // BibleGateway
112        'JAC'=>'[JAC] Jacalteco, Oriental', // BibleGateway
113        'KEK'=>'[KEK] Kekchi', // BibleGateway
114        'MAORI'=>'[MI] Maori Bible', // BibleGateway
115        'MNT'=>'[MK] Macedonian New Testament', // BibleGateway
116        'MVC'=>'[MVC] Mam, Central', // BibleGateway
117        'MVJ'=>'[MVJ] Mam de Todos Santos Chuchumatán', // BibleGateway
118        'REIMER'=>'[NDS] Reimer 2001', // BibleGateway
119        'NGU'=>'[NGU] Náhuatl de Guerrero', // BibleGateway
120        'HTB'=>'[NL] Het Boek', // BibleGateway
121        'DNB1930'=>'[NO] Det Norsk Bibelselskap 1930', // BibleGateway
122        'LB'=>'[NO] Levande Bibeln', // BibleGateway
123        'OL'=>'[PT] O Livro', // BibleGateway, Portuguese
124        'AA'=>'[PT] João Ferreira de Almeida Atualizada', // BibleGateway, Portuguese
125        'QUT'=>'[QUT] Quiché, Centro Occidental', // BibleGateway
126        'RMNN'=>'[RO] Romanian', // BibleGateway, Romanian
127        'TLCR'=>'[RO] Romanian', // BibleGateway, Romanian
128        'RUSV'=>'[RU] Russian Synodal Version', // BibleGateway, Russian
129        'SZ'=>'[RU] Slovo Zhizny', // BibleGateway, Russian
130        'NPK'=>'[SK] Nádej pre kazdého', // BibleGateway
131        'ALB'=>'[SQ] Albanian Bible', // BibleGateway, Albanian
132        'SVL'=>'[SV] Levande Bibeln', // BibleGateway
133        'SV1917'=>'[SV] Svenska 1917', // BibleGateway
134        'SNT'=>'[SW] Swahili New Testament', // BibleGateway, Swahili
135        'SND'=>'[TL] Ang Salita ng Diyos', // BibleGateway, Tagalog
136        'UKR'=>'[UK] Ukrainian Bible', // BibleGateway, Ukranian
137        'USP'=>'[USP] Uspanteco', // BibleGateway, Uspanteco
138        'VIET'=>'[VI] Vietnamese Bible 1934', // BibleGateway, Vietnamese
139        'CUV'=>'[ZH] Chinese Union Version (Traditional)', // BibleGateway, Chinese
140        'CUVS'=>'[ZH] Chinese Union Version (Simplified)' // BibleGateway, Chinese
141);
142
143global $scripturizer_options_default;
144$scripturizer_options_default = array(
145                'default_translation' => 'ESV',
146                'dynamic_substitution' => true,
147                'xml_show_hide' => false,
148                'esv_key' => 'IP',
149                'xml_css' => 'white-space: pre; display: none; padding: 10px; border: dotted blue 1px; border-left: solid blue 5px; color: black;',
150                'esv_query_options' => 'action=doPassageQuery&include-passage-references=true&include-short-copyright=true&include-audio-link=false&output-format=plain-text&include-passage-horizontal-lines=false&include-heading-horizontal-lines=false&line-length=60&include-headings=false&include-subheadings=false&include-footnotes=false',
151                'libronix' => false,
152                'link_css_class' => 'scripturizer',
153                'link_target_blank' => false
154);
155
156global $scripturizer_options;
157global $scripturizer_admin_options_hook;
158
159/**
160 * Plugin initialization function
161 * Defines default options as an array
162 * If option settings from earlier versions of the Plugin exist,
163 * copies the setting into the options array, and deletes the old option
164 *
165 * @Since 2.0
166 *
167 */     
168function scripturizer_init() {
169
170        // set options equal to defaults
171        global $scripturizer_options_default;
172        global $scripturizer_options;
173        $scripturizer_options = get_option( 'plugin_scripturizer_options' );
174       
175        $scripturizer_options_initial = ( ! $scripturizer_options ? $scripturizer_options_default : $scripturizer_options );
176       
177        // if options exist from previous Plugin version, update options array with old option settings
178        // and delete old database options
179        foreach( $scripturizer_options_initial as $key => $value ) {
180                if( $existing = get_option( 'scripturizer_' . $key ) ) {
181                        $scripturizer_options_initial[$key] = $existing;
182                        delete_option( 'scripturizer_' . $key );
183                }
184        }
185       
186        // Add/update the database options array
187        update_option( 'plugin_scripturizer_options', $scripturizer_options_initial );
188}
189
190/**
191 * Plugin admin options page
192 *
193 * @Since 1.55
194 *
195 */     
196// Function to add admin options page
197function scripturizer_menu() {
198        global $scripturizer_admin_options_hook;
199        $scripturizer_admin_options_hook = add_options_page('Options', 'Scripturizer', 'manage_options', 'scripturizer', 'scripturizer_admin_options_page');
200}
201// Admin options page markup
202// Moved to separate file for ease of management
203function scripturizer_admin_options_page() {
204        include_once( 'scripturizer_admin_options_page.php' );
205}
206
207// Codex Reference: http://codex.wordpress.org/Settings_API
208// Codex Reference: http://codex.wordpress.org/Data_Validation
209// Reference: http://ottopress.com/2009/wordpress-settings-api-tutorial/
210// Reference: http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
211function scripturizer_admin_init(){
212        include_once( 'scripturizer_admin_options_init.php' );
213}
214
215// Admin options page contextual help markup
216// Separate file for ease of management
217function scripturizer_contextual_help( $contextual_help, $screen_id, $screen ) {               
218        global $scripturizer_admin_options_hook;
219        include_once( 'scripturizer_admin_options_help.php' );
220        if ( $screen_id == $scripturizer_admin_options_hook ) {
221                $contextual_help = $text;
222        }
223       
224return $contextual_help;
225}
226
227/**
228 * Link to admin options page in Plugin Action links on Manage Plugins page
229 *
230 * @Since 2.0
231 *
232 */     
233function scripturizer_actlinks( $links ) {
234        $scripturizer_settings_link = '<a href="options-general.php?page=scripturizer">Settings</a>';
235        $links[] = $scripturizer_settings_link;
236        return $links;
237}
238
239
240/**
241 * Add Plugin options to variable array
242 *
243 * @Since 2.0
244 *
245 */     
246$scripturizer_options = get_option( 'plugin_scripturizer_options' );
247
248/**
249 * function scripturize()
250 *
251 * Split the_content accordingly, and only attempt to add scripture references to text that
252 * is inside of anchor tags, pre tags, code tags, [bible] shortcodes, or that is part of a tag attribute.
253 *
254 * @Since 1.2
255 *
256 */     
257function scripturize( $text = '', $bible = NULL ) {
258       
259        global $scripturizer_options;
260       
261        if ( ! isset( $bible ) ) {
262                $bible = $scripturizer_options['default_translation'];
263        }
264    // skip everything within a hyperlink, a <pre> block, a <code> block, or a tag
265    // we skip inside tags because something like <img src="nicodemus.jpg" alt="John 3:16"> should not be messed with
266        $anchor_regex = '<a\s+href.*?<\/a>';
267        $pre_regex = '<pre>.*<\/pre>';
268        $code_regex = '<code>.*<\/code>';
269        $other_plugin_regex= '\[bible\].*\[\/bible\]'; // for the ESV Wordpress plugin (out of courtesy)
270        $other_plugin_block_regex='\[bibleblock\].*\[\/bibleblock\]'; // ditto
271        $tag_regex = '<(?:[^<>\s]*)(?:\s[^<>]*){0,1}>'; // $tag_regex='<[^>]+>';
272        $split_regex = "/((?:$anchor_regex)|(?:$pre_regex)|(?:$code_regex)|(?:$other_plugin_regex)|(?:$other_plugin_block_regex)|(?:$tag_regex))/i";
273        $parsed_text = preg_split( $split_regex, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
274        $linked_text = '';
275
276        while ( list( $key, $value ) = each( $parsed_text ) ) {
277      if ( preg_match( $split_regex, $value ) ) {
278         $linked_text .= $value; // if it is an HTML element or within a link, just leave it as is
279      } else {
280        $linked_text .= scripturizeAddLinks( $value, $bible ); // if it's text, parse it for Bible references
281      }
282  }
283  return $linked_text;
284}
285
286/**
287 * function scripturizeAddLinks()
288 *
289 * Search filtered text from the_content for Scripture references, and if found replace with hyperlink
290 *
291 * @Since 1.2
292 *
293 */     
294function scripturizeAddLinks( $text = '', $bible = NULL ) {
295
296        global $scripturizer_translations;
297        global $scripturizer_options;
298
299        if ( ! isset( $bible ) ) {
300                $bible = $scripturizer_options['default_translation'];
301        }
302        // Regular Expression for Book Volume strings
303    $volume_regex = '1|2|3|I|II|III|1st|2nd|3rd|First|Second|Third';
304        // Regular Expression for OT Book full name strings
305    $book_ot_regex  = 'Genesis|Exodus|Leviticus|Numbers|Deuteronomy|Joshua|Judges|Ruth|Samuel|Kings|Chronicles|Ezra|Nehemiah|Esther';
306    $book_ot_regex .= '|Job|Psalms?|Proverbs?|Ecclesiastes|Songs? of Solomon|Song of Songs|Isaiah|Jeremiah|Lamentations|Ezekiel|Daniel|Hosea|Joel|Amos|Obadiah|Jonah|Micah|Nahum|Habakkuk|Zephaniah|Haggai|Zechariah|Malachi';
307        // Regular Expression for NT Book full name strings
308    $book_nt_regex = '|Mat+hew|Mark|Luke|John|Acts?|Acts of the Apostles|Romans|Corinthians|Galatians|Ephesians|Phil+ippians|Colossians|Thessalonians|Timothy|Titus|Philemon|Hebrews|James|Peter|Jude|Revelations?';
309        // Separate abbreviations from full names in order to find abbreviated book names followed by a period
310        // Regular Expression for OT Book abbreviation strings
311    $abbrev_ot_regex  = 'Gen|Ex|Exo|Lev|Num|Nmb|Deut?|Josh?|Judg?|Jdg|Rut|Sam|Ki?n|Chr(?:on?)?|Ezr|Neh|Est';
312    $abbrev_ot_regex .= '|Jb|Psa?|Pr(?:ov?)?|Eccl?|Song?|Isa|Jer|Lam|Eze|Dan|Hos|Joe|Amo|Oba|Jon|Mic|Nah|Hab|Zeph?|Hag|Zech?|Mal';
313        // Regular Expression for NT Book abbreviation strings
314    $abbrev_nt_regex = '|Mat+|Mr?k|Lu?k|Jh?n|Jo|Act|Rom|Cor|Gal|Eph|Col|Phil?|The?|Thess?|Tim|Tit|Phile|Heb|Ja?m|Pe?t|Ju?d|Rev';
315       
316        // Combine Regular Expressions for OT/NT Book full name strings
317        $book_regex = $book_ot_regex . $book_nt_regex; 
318        // Combine Regular Expressions for OT/NT Book abbreviation strings
319        $abbrev_regex = $abbrev_ot_regex . $abbrev_nt_regex;
320        // Combine Regular Expressions for OT/NT Book full-name and abbreviation strings
321    $book_regex='(?:'.$book_regex.')|(?:'.$abbrev_regex.')\.?';
322        // Regular Expression for Chapter/Verse references
323    $verse_regex="\d{1,3}(?::\d{1,3})?(?:\s?(?:[-&,]\s?\d+))*";
324
325        // non Bible Gateway translations are all together at the end to make it easier to maintain the list
326        $translation_regex = implode('|',array_keys($scripturizer_translations)); // makes it look like 'NIV|KJV|ESV' etc
327
328        // note that this will be executed as PHP code after substitution thanks to the /e at the end!
329    /*
330     * Comment this out for now. I'll figure it out later.
331     *
332    $passage_ot_regex = '/(?:('.$volume_regex.')\s)?('.$book_ot_regex.')\s('.$verse_regex.')(?:\s?[,-]?\s?((?:'.$translation_regex.')|\s?\((?:'.$translation_regex.')\)))?/e';
333    $passage_regex = '/(?:('.$volume_regex.')\s)?('.$book_nt_regex.')\s('.$verse_regex.')(?:\s?[,-]?\s?((?:'.$translation_regex.')|\s?\((?:'.$translation_nt_regex.')\)))?/e';
334    */
335    $passage_regex = '/(?:('.$volume_regex.')\s)?('.$book_regex.')\s('.$verse_regex.')(?:\s?[,-]?\s?((?:'.$translation_regex.')|\s?\((?:'.$translation_regex.')\)))?/e';
336
337    $replacement_regex = "scripturizeLinkReference('\\0','\\1','\\2','\\3','\\4','$bible')";
338
339    $text = preg_replace( $passage_regex, $replacement_regex, $text );
340
341    return $text; // TODO: make this an array, to return text, plus OT/NT (for original languages)
342}
343
344function scripturizeLinkReference( $reference='', $volume='', $book='', $verse='', $translation='', $user_translation='' ) {
345        global $scripturizer_options;
346       
347        $link_target = ( $scripturizer_options['link_target_blank'] ? ' target="_blank"' : '' );
348       
349    if ( $volume ) {
350       $volume = str_replace('III','3',$volume);
351           $volume = str_replace('Third','3',$volume);   
352       $volume = str_replace('II','2',$volume);
353           $volume = str_replace('Second','2',$volume);     
354       $volume = str_replace('I','1',$volume);
355           $volume = str_replace('First','1',$volume);     
356       $volume = $volume{0}; // will remove st,nd,and rd (presupposes regex is correct)
357    }
358       
359        //catch an obscure bug where a sentence like "The 3 of us went downtown" triggers a link to 1 Thess 3
360        if ( ! strcmp( strtolower( $book ), "the" ) && $volume=='' ) {
361                return $reference;
362        }
363
364   if( ! $translation ) {
365         if ( ! $user_translation ) {
366             $translation = $scripturizer_options['default_translation'];
367         } else {
368             $translation = $user_translation;
369         }
370   } else {
371       $translation = trim( $translation, ' ()' ); // strip out any parentheses that might have made it this far
372   }
373
374   // if necessary, just choose part of the verse reference to pass to the web interfaces
375   // they wouldn't know what to do with John 5:1-2, 5, 10-13 so I just give them John 5:1-2
376   // this doesn't work quite right with something like 1:5,6 - it gets chopped to 1:5 instead of converted to 1:5-6
377   if ( $verse ) {
378       $verse = strtok( $verse, ',& ' );
379   }
380
381        if ( $scripturizer_options['libronix'] ) {
382                $libronix=sprintf( '<a class="%s" %s title="View %s in Logos Bible Software Series X" href="libronixdls:keylink|ref=[en]bible:%s"><img border="0" src="%s/wp-content/LibronixLink.gif"></a>', $scripturizer_options['link_css_class'],  $link_target, trim( "$volume $book $verse" ), htmlentities( trim( "$volume $book $verse" ) ), get_settings( 'siteurl' ) );
383        }
384
385   switch ($translation) {
386        case 'ESV':
387        // note: the ESV could actually support a mouseover reference
388        // we could pull it directly from their site and include it in the $title text
389        // http://www.gnpcb.org/esv/share/services/api/ for more info
390             $link = 'http://www.gnpcb.org/esv/search/?go=Go&amp;q=';
391             $title = 'English Standard Version Bible';
392             $link = sprintf( '<a class="%s" %s href="%s%s" title="%s">%s</a>', $scripturizer_options['link_css_class'],  $link_target, $link, htmlentities( urlencode( trim( "$volume $book $verse" ) ) ), $title, trim($reference ) );
393        # Insert Show/Hide link and include ESV verse text
394        if ( $scripturizer_options['xml_show_hide'] ) {
395            $link .= getEsvText($volume, $book, $verse);
396        }
397             break;
398        case 'NET':
399                // example URL http://www.bible.org/netbible2/index.php?book=gen&chapter=1&verse=1&submit=Lookup+Verse
400             $link = 'http://www.bible.org/netbible2/index.php';
401             $title = 'New English Translation';
402             $chapter = trim(strtok($verse,':'));
403             $verses = trim(strtok('-,'));
404             $book = scripturizeNETBook($volume.' '.$book);
405             $link = sprintf( '<a class="%s" %s href="%s?book=%s&amp;chapter=%s&amp;verse=%s&amp;submit=Lookup+Verse" title="%s">%s</a>', $scripturizer_options['link_css_class'], $link_target, $link, htmlentities( urlencode( $book ) ), $chapter, $verses, $title, trim( $reference ) );
406             break;
407        case 'NRSV':
408        // example URL http://bible.oremus.org/?passage=John+1%3A1&vnum=yes&version=nrsv
409        // there is a new interface being developed at http://bible.oremus.org/bible.cgi
410             $link = 'http://bible.oremus.org/';
411             $title = 'New Revised Standard Version';
412                         $options ='&amp;vnum=yes&amp;version=nrsv';
413             $link = sprintf( '<a class="%s" %s href="%s?passage=%s%s" title="%s">%s</a>', $scripturizer_options['link_css_class'], $link_target, $link, htmlentities( urlencode( trim( "$volume $book $verse" ) ) ), $options, $title, trim( $reference ) );
414             break;
415        case 'NA26':
416        case 'LXX':
417        // example URL http://www.zhubert.com/bible?book=Matthew&chapter=2&verse=3
418        // there's also an XML interface to this content - could do a trick like I propose with the ESV
419             $link = 'http://www.zhubert.com/bible';
420             $title = 'original language at zhubert.com';
421                        $chapter=zhubertize_chapter($verse);
422                        $verse=zhubertize_verse($verse);
423                        $book=zhubertize_book($volume.' '.$book);
424             $link = sprintf( '<a class="%s" %s href="%s?book=%s&amp;chapter=%d&amp;verse=%d" title="%s">%s</a>', $scripturizer_options['link_css_class'], $link_target, $link, htmlentities( urlencode( trim( $book ) ) ), $chapter, $verse, $title, trim( $reference ) );
425             break;
426        default:
427                // Bible Gateway has a ton of translations, so just make it the default instead of checking for each one
428                // $translation_regex takes care of ensuring that only valid translations make it this far, anyway
429                // api at http://biblegateway.com/usage/linking/
430             $link = "http://biblegateway.com/bible?version=$translation&amp;passage=";
431             $title = 'Bible Gateway';
432             $link = sprintf( '<a class="%s" %s href="%s%s" title="%s">%s</a>', $scripturizer_options['link_css_class'],  $link_target, $link, htmlentities( urlencode( trim( "$volume $book $verse" ) ) ), $title, trim( $reference ) );
433             break;
434    }
435       
436        if ( $scripturizer_options['libronix'] ) {
437                $link .= $libronix;
438        }
439       
440return $link;
441}
442
443
444function getEsvText($volume, $book, $verse) {
445    //Get passage text from ESV web site
446    $esvPassage = htmlentities(urlencode(trim("$volume $book $verse")));
447    $esvUrl = "http://www.gnpcb.org/esv/share/get/?key=". $scripturizer_options['esv_key'] ."&passage=$esvPassage&". $scripturizer_options['esv_query_options'];
448    $esvCh = curl_init($esvUrl);
449    curl_setopt($esvCh, CURLOPT_RETURNTRANSFER, 1);
450    $esvResponse = curl_exec($esvCh);
451    curl_close($esvCh);
452
453// Get rid of triple and double line breaks since WP turns them into <p>'s and thereby kills our <span>
454//    $esvResponse = str_replace("\n\n\n", "\n", $esvResponse);
455    $esvResponse = str_replace("\n\n", "\n", $esvResponse);
456
457    // Build the show/hide link
458    $esvSpanId = 'scripturizer' .mt_rand(); //prefix the rand number with "id" to pass XHTML validation
459    $output_dynamic = " <a href=\"javascript://\" onclick=\"showhide('"
460        . $esvSpanId
461        . "');\">[+/-]</a><span id=\""
462        . $esvSpanId
463        . "\" style=\""
464        . $scripturizer_options['xml_css']
465        . "\">"
466        . $esvResponse
467        . "<br /><a href=\"http://www.esv.org/\"><img src=\"http://www.esv.org/assets/buttons/small.7.png\" alt=\"This text is from the ESV Bible. Visit www.esv.org to learn about the ESV.\" title=\"Visit www.esv.org to learn about the ESV Bible\" width=\"80\" height=\"21\" /></a>"
468        . "</span>";
469       
470
471    // I don't know why, but I ran into bugs when switching between dynamic and static modes based on how WP parsed the '' in the onclick action.
472    // So, for now, I decided to have two different outputs. The static mode escapes the ' by using a double ''.
473    // The dynamic output does not need to escape the ' -- (go figure!)
474    $output_static = " <a href=\"javascript://\" onclick=\"showhide(''"
475        . $esvSpanId
476        . "'');\">[+/-]</a><span id=\""
477        . $esvSpanId
478        . "\" style=\""
479        . $scripturizer_options['xml_css']
480        . "\">"
481        . $esvResponse
482        . "<br /><a href=\"http://www.esv.org/\"><img src=\"http://www.esv.org/assets/buttons/small.7.png\" alt=\"This text is from the ESV Bible. Visit www.esv.org to learn about the ESV.\" title=\"Visit www.esv.org to learn about the ESV Bible\" width=\"80\" height=\"21\" /></a>"
483        . "</span>";
484
485    if ( $scripturizer_options['dynamic_substitution'] ) {
486        return $output_dynamic;
487    } else {
488        return $output_static;
489    }
490}
491
492function scripturizeNETBook($book='') {
493// need this function because NET Bible needs rigid input
494// it's not perfect, so someone who intends to link to the NET Bible must be cautious with their syntax
495// Jn 5:1 won't work, for example (must be 'joh' or 'john').
496    $book = strtolower(trim($book));
497    if (!$book) return '';
498
499    $book = preg_replace('/\s+/', '', $book); //strip whitespace
500
501    switch ($book) {
502           case 'judges':
503                $book = 'jdg';
504                break;
505           case 'songofsongs':
506           case 'songofsolomon':
507           case 'song':
508                $book = 'sos';
509                break;
510           case 'philemon':
511                 $book = 'phm';
512                 break;
513           default:
514                   $book = substr($book,0,3);
515    }
516    return $book;
517}
518
519function zhubertize_chapter($reference="") {
520        $chapter=strtok($reference,':');
521        return $chapter;
522}
523
524function zhubertize_verse($reference="") {
525        $chapter=strtok($reference,':');
526        $verse=strtok(' ,-;');
527        if (!$verse) {
528                $verse=1;
529        }
530        return $verse;
531}
532
533function zhubertize_book($rawbook) {
534        // ultimately I need to restore all abbreviations to the full book.
535        // perhaps take the first three letters and expand?
536        $book = strtolower(trim($rawbook));
537    $book = preg_replace('/\s+/', '', $book); //strip whitespace
538        $book= substr($book,0,3);
539        switch ($book) {
540                case 'gen':
541                        $book='Genesis';
542                        break;
543                case 'exo':
544                case 'ex':
545                        $book='Exodus';
546                        break;
547                case 'lev':
548                case 'lv':
549                        $book='Leviticus';
550                        break;
551                case 'num':
552                        $book='Numbers';
553                        break;
554                case 'deu':
555                case 'dt':
556                        $book='Deuteronomy';
557                        break;
558                case 'jos':
559                        $book='Joshua';
560                        break;
561                case 'jud':
562                case 'jd':
563                        // could be either Judges or Jude
564                        // abbreviations for Judges should always have a g in them
565                        $judges=strpos($rawbook,'g');
566                        if ($judges===FALSE) {
567                                $book='Jude';
568                        } else {
569                                $book='Judges';
570                        }
571                        break;
572                case 'rut':
573                case 'rth':
574                        $book='Ruth';
575                        break;
576                case '1sa':
577                        $book='1 Samuel';
578                        break;
579                case '2sa':
580                        $book='2 Samuel';
581                        break;
582                case '1ki':
583                        $book='1 Kings';
584                        break;
585                case '2ki':
586                        $book='2 Kings';
587                        break;
588                case '1ch':
589                        $book='1 Chronicles';
590                        break;
591                case '2ch':
592                        $book='2 Chronicles';
593                        break;
594                case 'ezr':
595                case 'ez':
596                        $book='Ezra';
597                        break;
598                case 'neh':
599                case 'nh':
600                        $book='Nehemiah';
601                        break;
602                case 'est':
603                        $book='Esther';
604                        break;
605                case 'job':
606                case 'jb':
607                        $book='Job';
608                        break;
609                case 'psa':
610                case 'ps':
611                        $book='Psalms';
612                        break;
613                case 'pro':
614                case 'pr':
615                        $book='Proverbs';
616                        break;
617                case 'ecc':
618                        $book='Qoheleth';
619                        break;
620                case 'son':
621                case 'sos':
622                        $book='Canticle of Canticles';
623                        break;
624                case 'isa':
625                case 'is':
626                        $book='Isaiah';
627                        break;
628                case 'jer':
629                        $book='Jeremiah';
630                        break;
631                case 'eze':
632                case 'ez':
633                        $book='Ezekiel';
634                        break;
635                case 'dan':
636                case 'dn':
637                        $book='Daniel';
638                        break;
639                case 'hos':
640                        $book='Hosea';
641                        break;
642                case 'joe':
643                        $book='Joel';
644                        break;
645                case 'amo':
646                case 'am':
647                        $book='Amos';
648                        break;
649                case 'oba':
650                case 'ob':
651                        $book='Obadiah';
652                        break;
653                case 'jon':
654                        $book='Jonah';
655                        break;
656                case 'mic':
657                        $book='Micah';
658                        break;
659                case 'nah':
660                        $book='Nahum';
661                        break;
662                case 'hab':
663                        $book='Habakkuk';
664                        break;
665                case 'zep':
666                        $book='Zephaniah';
667                        break;
668                case 'hag':
669                        $book='Haggai';
670                        break;
671                case 'zec':
672                        $book='Zechariah';
673                        break;
674                case 'mal':
675                        $book='Malachi';
676                        break;
677                case 'mat':
678                case 'mt':
679                        $book='Matthew';
680                        break;
681                case 'mar':
682                case 'mk':
683                        $book='Mark';
684                        break;
685                case 'luk':
686                case 'lk':
687                        $book='Luke';
688                        break;
689                case 'joh':
690                case 'jn':
691                        $book='John';
692                        break;
693                case 'act':
694                        $book='Acts';
695                        break;
696                case 'rom':
697                case 'rm':
698                        $book='Romans';
699                        break;
700                case '1co':
701                        $book='1 Corinthians';
702                        break;
703                case '2co':
704                        $book='2 Corinthians';
705                        break;
706                case 'gal':
707                        $book='Galatians';
708                        break;
709                case 'eph':
710                        $book='Ephesians';
711                        break;
712                case 'phi':
713                        $book='Philippians';
714                        break;
715                case 'col':
716                        $book='Colossians';
717                        break;
718                case '1th':
719                        $book='1 Thessalonians';
720                        break;
721                case '2th':
722                        $book='2 Thessalonians';
723                        break;
724                case '1ti':
725                        $book='1 Timothy';
726                        break;
727                case '2ti':
728                        $book='2 Timothy';
729                        break;
730                case 'tit':
731                case 'ti':
732                        $book='Titus';
733                        break;
734                case 'phi':
735                        $book='Philemon';
736                        break;
737                case 'heb':
738                        $book='Hebrews';
739                        break;
740                case 'jam':
741                        $book='James';
742                        break;
743                case '1pe':
744                        $book='1 Peter';
745                        break;
746                case '2pe':
747                        $book='2 Peter';
748                        break;
749                case '1jo':
750                        $book='1 John';
751                        break;
752                case '2jo':
753                        $book='2 John';
754                        break;
755                case '3jo':
756                        $book='3 John';
757                        break;
758                // jude is handled up by judges
759                case 'rev':
760                        $book='Revelation';
761                        break;
762                default:
763                        $book=$rawbook;
764        }
765        return $book;
766}
767
768function scripturizePost($post_ID) {
769    global $wpdb;
770       
771        $tableposts=$wpdb->posts;
772
773    $postdata=$wpdb->get_row("SELECT * FROM $tableposts WHERE ID = '$post_ID'");
774
775    $content = scripturize($postdata->post_content);
776
777    $wpdb->query("UPDATE $tableposts SET post_content = '$content' WHERE ID = '$post_ID'");
778   
779    return $post_ID;
780}
781
782function scripturizeComment($comment_ID) {
783    global $wpdb;
784   
785        $tablecomments=$wpdb->comments;
786
787    $postdata=$wpdb->get_row("SELECT * FROM $tablecomments WHERE ID = '$comment_ID'");
788
789    $content = scripturize($postdata->comment_content);
790
791    $wpdb->query("UPDATE $tablecomments SET comment_content = '$content' WHERE ID = '$comment_ID'");
792   
793    return $comment_ID;
794}
795
796if ( ! function_exists( 'esvShowHideHeader' ) ) {
797
798    function esvShowHideHeader() {
799
800        $content = "
801            <script language=\"javascript\" type=\"text/javascript\">
802            <!-- I modified this script: http://lists.evolt.org/archive/Week-of-Mon-20020624/116151.html to get the following
803            var state = 'none';
804
805            function showhide(layer_ref) {
806
807            if (state == 'block') {
808            state = 'none';
809            }
810            else {
811            state = 'block';
812            }
813            if (document.all) { //IS IE 4 or 5 (or 6 beta)
814            eval( \"document.all.\" + layer_ref + \".style.display = state\");
815            }
816            if (document.layers) { //IS NETSCAPE 4 or below
817            document.layers[layer_ref].display = state;
818            }
819            if (document.getElementById && !document.all) {
820            maxwell_smart = document.getElementById(layer_ref);
821            maxwell_smart.style.display = state;
822            }
823            }
824            //-->
825            </script>";
826    echo $content;
827    }
828}
829
830##### ADD ACTIONS AND FILTERS
831// Initialize Plugin options
832add_action('activate_scripturizer/scripturizer.php', 'scripturizer_init' );
833// add the admin settings and such
834add_action('admin_init', 'scripturizer_admin_init');
835// Load the Admin Options page
836add_action('admin_menu', 'scripturizer_menu');
837// Add contextual help to Admin Options page
838add_action('contextual_help', 'scripturizer_contextual_help', 10, 3);
839// Add a Settings link to Plugin Action Links on Manage Plugins page
840add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'scripturizer_actlinks', 10, 1 );
841// Load the javascript if the xml show/hide option is turned on
842if ( isset( $scripturizer_options['xml_show_hide'] ) && $scripturizer_options['xml_show_hide'] ) {
843    add_action('wp_head', 'esvShowHideHeader', 10);
844    add_action('admin_head', 'esvShowHideHeader', 5);
845}
846// Update content per Dynamic/Static mode setting
847if ( isset( $scripturizer_options['dynamic_substitution'] ) && $scripturizer_options['dynamic_substitution'] ) {
848        add_filter('the_content','scripturize');
849        add_filter('comment_text','scripturize');
850} else {
851        add_action('publish_post','scripturizePost');
852        add_action('comment_post','scripturizeComment');
853        add_action('edit_post','scripturizePost');
854        add_action('edit_comment','scripturizeComment');
855        // note, adding the edit_post action guarantees that if you add or change a scripture reference the link will be inserted
856        // HOWEVER, it will prevent you from removing a link you don't want!
857}
858?>
Note: See TracBrowser for help on using the repository browser.