-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfp-print-hreflang.php
63 lines (52 loc) · 2.53 KB
/
tfp-print-hreflang.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
<?php
/**
* Print hreflang tags in <head> of the page.
*
* @package translations for pressbooks
* @since 1.2.5
* @package translations-for-pressbooks
*
*/
defined ("ABSPATH") or die ("No script assholes!");
//add action to print to theme <head> tag
add_action('wp_head', 'tfp_printHreflangTags');
function tfp_printHreflangTags(){
$blog_id = get_current_blog_id();
global $wpdb;
global $wp;
$toprint = "";
// identify if book is translation or not and get the source book ID, first part of the function is from tfp_checkTrans()
//switch_to_blog($blog_id);
$trans_lang = get_post_meta(tfp_getInfoPost(), 'efp_trans_language') ?: 'not_set';
$source = get_post_meta(tfp_getInfoPost(), 'pb_is_based_on', true) ?: 'original';
if ($source == 'original'){
$origin_id = $blog_id; // origin id is the id for the original book
} else {
$origin = str_replace(['http://', 'https://'], '', $source).'/';
switch_to_blog(1);
$origin_id = $wpdb->get_results("SELECT `blog_id` FROM $wpdb->blogs WHERE CONCAT(`domain`, `path`) = '$origin'", ARRAY_A)[0]['blog_id'];
restore_current_blog();
}
switch_to_blog(1);
$relations = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}trans_rel WHERE `a` = '$origin_id'", ARRAY_A);
restore_current_blog();
$current_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
//go through all translations and print them as hreflang tag in the <head>
if(!empty($relations)){
$languageArrayObject = new ArrayObject($relations);
$toprint .= "\n<!-- Translations for pressbooks -->\n";
foreach ($languageArrayObject as $lang => $id) {
if ($id == 0) {
continue;
} elseif ($lang == "a"){
$toprint .= "<link rel='alternate' hreflang='x-default' href='".str_replace(get_blog_details(get_current_blog_id())->path, get_blog_details($id)->path, $current_link)."'/>\n";
$toprint .= "<link rel='alternate' hreflang='".tfp_getOriginalBookLanguage($blog_id)."' href='".str_replace(get_blog_details(get_current_blog_id())->path, get_blog_details($id)->path, $current_link)."'/>\n";
continue;
} else {
$toprint .= "<link rel='alternate' hreflang='". $lang ."' href='".str_replace(get_blog_details(get_current_blog_id())->path, get_blog_details($id)->path, $current_link)."'/>\n";
}
}
$toprint .= "<!-- Translations for pressbooks END -->\n\n";
}
echo $toprint;
}