-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathinstall.php
240 lines (198 loc) · 6.98 KB
/
install.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
<?php
class WP_Stream_Install {
public static $table_prefix;
/**
* Check db version, create/update table schema accordingly
*
* @return void
*/
public static function check() {
global $wpdb;
$current = WP_Stream::VERSION;
$db_version = get_option( plugin_basename( WP_STREAM_DIR ) . '_db' );
/**
* Allows devs to alter the tables prefix, default to base_prefix
*
* @param string database prefix
* @return string udpated database prefix
*/
self::$table_prefix = apply_filters( 'wp_stream_db_tables_prefix', $wpdb->prefix );
if ( empty( $db_version ) ) {
self::install();
} elseif ( $db_version !== $current ) {
self::update( $db_version, $current );
} else {
return;
}
update_option( plugin_basename( WP_STREAM_DIR ) . '_db', $current );
}
public static function install() {
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$prefix = self::$table_prefix;
$sql = "CREATE TABLE {$prefix}stream (
ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
site_id bigint(20) unsigned NOT NULL DEFAULT '1',
object_id bigint(20) unsigned NULL,
author bigint(20) unsigned NOT NULL DEFAULT '0',
summary longtext NOT NULL,
visibility varchar(20) NOT NULL DEFAULT 'publish',
parent bigint(20) unsigned NOT NULL DEFAULT '0',
type varchar(20) NOT NULL DEFAULT 'stream',
created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
ip varchar(39) NULL,
PRIMARY KEY (ID),
KEY site_id (site_id),
KEY parent (parent),
KEY author (author),
KEY created (created)
)";
if ( ! empty( $wpdb->charset ) ) {
$sql .= " CHARACTER SET $wpdb->charset";
}
if ( ! empty( $wpdb->collate ) ) {
$sql .= " COLLATE $wpdb->collate";
}
$sql .= ';';
dbDelta( $sql );
$sql = "CREATE TABLE {$prefix}stream_context (
meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
record_id bigint(20) unsigned NOT NULL,
context varchar(100) NOT NULL,
action varchar(100) NOT NULL,
connector varchar(100) NOT NULL,
PRIMARY KEY (meta_id),
KEY context (context),
KEY action (action),
KEY connector (connector)
)";
if ( ! empty( $wpdb->charset ) ) {
$sql .= " CHARACTER SET $wpdb->charset";
}
if ( ! empty( $wpdb->collate ) ) {
$sql .= " COLLATE $wpdb->collate";
}
$sql .= ';';
dbDelta( $sql );
$sql = "CREATE TABLE {$prefix}stream_meta (
meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
record_id bigint(20) unsigned NOT NULL,
meta_key varchar(200) NOT NULL,
meta_value varchar(200) NOT NULL,
PRIMARY KEY (meta_id),
KEY record_id (record_id),
KEY meta_key (meta_key),
KEY meta_value (meta_value)
)";
if ( ! empty( $wpdb->charset ) ) {
$sql .= " CHARACTER SET $wpdb->charset";
}
if ( ! empty( $wpdb->collate ) ) {
$sql .= " COLLATE $wpdb->collate";
}
$sql .= ';';
dbDelta( $sql );
}
public static function update( $db_version, $current ) {
global $wpdb;
$prefix = self::$table_prefix;
// If version is lower than 1.1.4, do the update routine
if ( version_compare( $db_version, '1.1.4', '<' ) && ! empty( $wpdb->charset ) ) {
$tables = array( 'stream', 'stream_context', 'stream_meta' );
$collate = ( $wpdb->collate ) ? " COLLATE {$wpdb->collate}" : null;
foreach ( $tables as $table ) {
$wpdb->query( "ALTER TABLE {$prefix}{$table} CONVERT TO CHARACTER SET {$wpdb->charset}{$collate};" );
}
}
// If version is lower than 1.1.7, do the update routine
if ( version_compare( $db_version, '1.1.7', '<' ) ) {
$wpdb->query( "ALTER TABLE {$prefix}stream MODIFY ip varchar(39) NULL AFTER created" );
}
// If version is lower than 1.2.5, do the update routine
// Taxonomy records switch from term_id to term_taxonomy_id
if ( version_compare( $db_version, '1.2.5', '<' ) ) {
$sql = "SELECT r.ID id, tt.term_taxonomy_id tt
FROM $wpdb->stream r
JOIN $wpdb->streamcontext c
ON r.ID = c.record_id AND c.connector = 'taxonomies'
JOIN $wpdb->streammeta m
ON r.ID = m.record_id AND m.meta_key = 'term_id'
JOIN $wpdb->streammeta m2
ON r.ID = m2.record_id AND m2.meta_key = 'taxonomy'
JOIN $wpdb->term_taxonomy tt
ON tt.term_id = m.meta_value
AND tt.taxonomy = m2.meta_value
";
$tax_records = $wpdb->get_results( $sql ); // db call ok
foreach ( $tax_records as $record ) {
if ( ! empty( $record->tt ) ) {
$wpdb->update(
$wpdb->stream,
array( 'object_id' => $record->tt ),
array( 'ID' => $record->id ),
array( '%d' ),
array( '%d' )
);
}
}
}
// If version is lower than 1.2.8, do the update routine
// Change the context for Media connectors to the attachment type
if ( version_compare( $db_version, '1.2.8', '<' ) ) {
$sql = "SELECT r.ID id, r.object_id pid, c.meta_id mid
FROM $wpdb->stream r
JOIN $wpdb->streamcontext c
ON r.ID = c.record_id AND c.connector = 'media' AND c.context = 'media'
";
$media_records = $wpdb->get_results( $sql ); // db call ok
require_once WP_STREAM_INC_DIR . 'query.php';
require_once WP_STREAM_CLASS_DIR . 'connector.php';
require_once WP_STREAM_DIR . 'connectors/media.php';
foreach ( $media_records as $record ) {
$post = get_post( $record->pid );
$guid = isset( $post->guid ) ? $post->guid : null;
$url = $guid ? $guid : get_stream_meta( $record->id, 'url', true );
if ( ! empty( $url ) ) {
$wpdb->update(
$wpdb->streamcontext,
array( 'context' => WP_Stream_Connector_Media::get_attachment_type( $url ) ),
array( 'record_id' => $record->id ),
array( '%s' ),
array( '%d' )
);
}
}
}
// If version is lower than 1.3.0, do the update routine for site options
// Backward settings compatibility for old version plugins
if ( version_compare( $db_version, '1.3.0', '<' ) ) {
add_filter( 'wp_stream_after_connectors_registration', 'WP_Stream_Install::migrate_old_options_to_exclude_tab' );
}
}
/**
* Function will migrate old options from the General and Connectors tabs into the new Exclude tab
*
* @param $labels array connectors terms labels
* @used wp_stream_after_connector_term_labels_loaded
*/
public static function migrate_old_options_to_exclude_tab( $labels ) {
$old_options = get_option( WP_Stream_Settings::KEY, array() );
// Stream > Settings > General > Log Activity for
if ( isset( $old_options['general_log_activity_for'] ) ) {
WP_Stream_Settings::$options['exclude_authors_and_roles'] = array_diff(
array_keys( WP_Stream_Settings::get_roles() ),
$old_options['general_log_activity_for']
);
unset( WP_Stream_Settings::$options['general_log_activity_for'] );
}
// Stream > Settings > Connectors > Active Connectors
if ( isset( $old_options['connectors_active_connectors'] ) ) {
WP_Stream_Settings::$options['exclude_connectors'] = array_diff(
array_keys( $labels ),
$old_options['connectors_active_connectors']
);
unset( WP_Stream_Settings::$options['connectors_active_connectors'] );
}
update_option( WP_Stream_Settings::KEY, WP_Stream_Settings::$options );
}
}