db.php
3.5 KB
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
<?php
/**
* Database reference file
*
* @category Utilities
* @package My Calendar
* @author Joe Dolson
* @license GPLv2 or later
* @link https://www.joedolson.com/my-calendar/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* My Calendar main table
*
* @param int|boolean $site Site ID in multisite.
*
* @return string table name
*/
function my_calendar_table( $site = false ) {
return my_calendar_select_table( 'my_calendar', $site );
}
/**
* My Calendar event table
*
* @param int|boolean $site Site ID in multisite.
*
* @return string table name
*/
function my_calendar_event_table( $site = false ) {
return my_calendar_select_table( 'my_calendar_events', $site );
}
/**
* My Calendar category table
*
* @param int|boolean $site Site ID in multisite.
*
* @return string table name
*/
function my_calendar_categories_table( $site = false ) {
return my_calendar_select_table( 'my_calendar_categories', $site );
}
/**
* My Calendar category relationships table
*
* @param int|boolean $site Site ID in multisite.
*
* @return string table name
*/
function my_calendar_category_relationships_table( $site = false ) {
return my_calendar_select_table( 'my_calendar_category_relationships', $site );
}
/**
* My Calendar location relationships table
*
* @param int|boolean $site Site ID in multisite.
*
* @return string table name
*/
function my_calendar_location_relationships_table( $site = false ) {
return my_calendar_select_table( 'my_calendar_location_relationships', $site );
}
/**
* My Calendar locations table
*
* @param int|boolean $site Site ID in multisite.
*
* @return string table name
*/
function my_calendar_locations_table( $site = false ) {
return my_calendar_select_table( 'my_calendar_locations', $site );
}
/**
* Get table to query based on table data required & required site.
*
* @since 2.5.0
*
* @param string $table table name.
* @param int|string|false $site 'global' to get global database; site ID to get that site's database; false for defaults according to settings.
*
* @return string properly prefixed table name
*/
function my_calendar_select_table( $table = 'my_calendar_events', $site = false ) {
global $wpdb;
$local = $wpdb->prefix . $table;
if ( is_multisite() ) {
$option = (int) get_site_option( 'mc_multisite' );
$choice = (int) mc_get_option( 'current_table' );
$show = (int) get_site_option( 'mc_multisite_show' ); // 1 == use global instead of local.
if ( 'global' === $site ) {
return $wpdb->base_prefix . $table;
}
if ( false !== $site && $site ) {
$site = absint( $site );
$wpdb->set_blog_id( $site );
}
$local = ( 1 === $show ) ? $wpdb->base_prefix . $table : $wpdb->prefix . $table;
$global = $wpdb->base_prefix . $table;
switch ( $option ) {
case 0:
$return = $local;
break;
case 1:
$return = $global;
break;
case 2:
$return = ( 1 === $choice ) ? $global : $local;
break;
default:
$return = $local;
}
} else {
$return = $local;
}
return $return;
}
/**
* Determine whether we're accessing a remote database.
*
* @return object WP DB object
*/
function mc_is_remote_db() {
global $wpdb;
global $remotedb;
$mcdb = $wpdb;
if ( 'true' === mc_get_option( 'remote' ) && function_exists( 'mc_remote_db' ) ) {
if ( ! isset( $remotedb ) ) {
$remotedb = mc_remote_db();
}
$mcdb = $remotedb;
}
return $mcdb;
}