| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
|
| 6 |
class Vimeography_Database extends Vimeography { |
| 7 |
/** |
| 8 |
* Vimeography DB Version |
| 9 |
* |
| 10 |
* @var [type] |
| 11 |
*/ |
| 12 |
protected static $_version; |
| 13 |
|
| 14 |
/** |
| 15 |
* [__construct description] |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
add_action( 'plugins_loaded', array($this, 'vimeography_update_db_version_if_not_exists'), 1 ); |
| 19 |
add_action( 'plugins_loaded', array($this, 'vimeography_update_database'), 11 ); |
| 20 |
add_action( 'wpmu_new_blog', array($this, 'install_vimeography_on_new_blog'), 10, 6); |
| 21 |
|
| 22 |
register_activation_hook( VIMEOGRAPHY_BASENAME, array($this, 'vimeography_activation') ); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Helper function to handle DB creation depending on single or multisite installation |
| 28 |
* |
| 29 |
* @param [type] $network_wide [description] |
| 30 |
* @return [type] [description] |
| 31 |
*/ |
| 32 |
public function vimeography_activation( $network_wide ) { |
| 33 |
if ( is_multisite() && $network_wide ) { // See if being activated on the entire network or one blog |
| 34 |
global $wpdb; |
| 35 |
|
| 36 |
// Get this so we can switch back to it later |
| 37 |
$original_blog_id = get_current_blog_id(); |
| 38 |
|
| 39 |
// Get all blogs in the network and activate plugin on each one |
| 40 |
$blogs = $wpdb->get_results( |
| 41 |
$wpdb->prepare( |
| 42 |
" |
| 43 |
SELECT blog_id |
| 44 |
FROM {$wpdb->blogs} |
| 45 |
WHERE site_id = %s |
| 46 |
AND spam = '0' |
| 47 |
AND deleted = '0' |
| 48 |
AND archived = '0' |
| 49 |
", |
| 50 |
$wpdb->siteid |
| 51 |
) |
| 52 |
); |
| 53 |
|
| 54 |
foreach ( $blogs as $blog ) { |
| 55 |
switch_to_blog( $blog->blog_id ); |
| 56 |
self::vimeography_update_tables(); |
| 57 |
self::vimeography_update_db_version(); |
| 58 |
} |
| 59 |
|
| 60 |
// Switch back to the current blog |
| 61 |
// @link http://wordpress.stackexchange.com/a/89114 |
| 62 |
switch_to_blog( $original_blog_id ); |
| 63 |
$GLOBALS['_wp_switched_stack'] = array(); |
| 64 |
$GLOBALS['switched'] = FALSE; |
| 65 |
|
| 66 |
} else { |
| 67 |
// Running on a single blog |
| 68 |
self::vimeography_update_tables(); |
| 69 |
self::vimeography_update_db_version(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
/** |
| 75 |
* [install_vimeography_on_new_blog description] |
| 76 |
* @param [type] $blog_id [description] |
| 77 |
* @param [type] $user_id [description] |
| 78 |
* @param [type] $domain [description] |
| 79 |
* @param [type] $path [description] |
| 80 |
* @param [type] $site_id [description] |
| 81 |
* @param [type] $meta [description] |
| 82 |
* @return [type] [description] |
| 83 |
*/ |
| 84 |
public function install_vimeography_on_new_blog($blog_id, $user_id, $domain, $path, $site_id, $meta ) { |
| 85 |
global $wpdb; |
| 86 |
|
| 87 |
if ( is_plugin_active_for_network( VIMEOGRAPHY_BASENAME ) ) { |
| 88 |
|
| 89 |
$original_blog_id = get_current_blog_id(); |
| 90 |
|
| 91 |
switch_to_blog( $blog_id ); |
| 92 |
self::vimeography_update_tables(); |
| 93 |
self::vimeography_update_db_version(); |
| 94 |
switch_to_blog( $original_blog_id ); |
| 95 |
|
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Create tables and define defaults when plugin is activated. |
| 102 |
* |
| 103 |
* @access public |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public static function vimeography_update_tables() { |
| 107 |
global $wpdb; |
| 108 |
|
| 109 |
delete_site_option('vimeography_default_settings'); |
| 110 |
|
| 111 |
add_option('vimeography_default_settings', array( |
| 112 |
'source_url' => 'https://vimeo.com/channels/staffpicks/', |
| 113 |
'resource_uri' => '/channels/staffpicks', |
| 114 |
'featured_video' => '', |
| 115 |
'video_limit' => 25, |
| 116 |
'cache_timeout' => 3600, |
| 117 |
'theme_name' => 'harvestone', |
| 118 |
)); |
| 119 |
|
| 120 |
$sql = 'CREATE TABLE '.$wpdb->prefix.'vimeography_gallery ( |
| 121 |
id mediumint(8) unsigned NOT NULL AUTO_INCREMENT, |
| 122 |
title varchar(150) NOT NULL, |
| 123 |
date_created datetime NOT NULL, |
| 124 |
is_active tinyint(1) NOT NULL, |
| 125 |
PRIMARY KEY (id) |
| 126 |
); |
| 127 |
CREATE TABLE '.$wpdb->prefix.'vimeography_gallery_meta ( |
| 128 |
id mediumint(8) unsigned NOT NULL AUTO_INCREMENT, |
| 129 |
gallery_id mediumint(8) unsigned NOT NULL, |
| 130 |
source_url varchar(100) NOT NULL, |
| 131 |
resource_uri varchar(100) NOT NULL, |
| 132 |
featured_video varchar(100) DEFAULT NULL, |
| 133 |
video_limit mediumint(7) NOT NULL, |
| 134 |
gallery_width varchar(10) DEFAULT NULL, |
| 135 |
cache_timeout mediumint(7) NOT NULL, |
| 136 |
theme_name varchar(50) NOT NULL, |
| 137 |
PRIMARY KEY (id) |
| 138 |
); |
| 139 |
'; |
| 140 |
|
| 141 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 142 |
dbDelta($sql); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Gets Vimeography version number if it exists in the database. |
| 147 |
* |
| 148 |
* @since 1.2 |
| 149 |
* @return bool |
| 150 |
*/ |
| 151 |
public static function vimeography_get_db_version() { |
| 152 |
return get_site_option('vimeography_db_version'); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Updates the Vimeography version number stored in the database. |
| 157 |
* |
| 158 |
* @access public |
| 159 |
* @static |
| 160 |
* @return bool |
| 161 |
*/ |
| 162 |
public static function vimeography_update_db_version() { |
| 163 |
return update_site_option('vimeography_db_version', VIMEOGRAPHY_VERSION); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* [vimeography_update_db_version_if_not_exists description] |
| 168 |
* @return [type] [description] |
| 169 |
*/ |
| 170 |
public static function vimeography_update_db_version_if_not_exists() { |
| 171 |
if (self::vimeography_get_db_version() === FALSE) { |
| 172 |
self::vimeography_update_db_version(); |
| 173 |
self::$_version = self::vimeography_get_db_version(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* [vimeography_update_database description] |
| 179 |
* @return [type] [description] |
| 180 |
*/ |
| 181 |
public function vimeography_update_database() { |
| 182 |
self::$_version = self::vimeography_get_db_version(); |
| 183 |
|
| 184 |
self::vimeography_update_db_to_0_6(); |
| 185 |
self::vimeography_update_db_to_0_7(); |
| 186 |
self::vimeography_update_db_to_0_8(); |
| 187 |
$this->vimeography_update_db_to_1_0(); |
| 188 |
self::vimeography_update_db_to_1_0_7(); |
| 189 |
self::vimeography_update_db_to_1_1_4(); |
| 190 |
self::vimeography_update_db_to_1_1_6(); |
| 191 |
self::vimeography_update_db_to_1_2(); |
| 192 |
self::vimeography_update_db_to_1_2_8(); |
| 193 |
self::vimeography_update_db_version(); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
/** |
| 198 |
* Check if the Vimeography database structure needs updated to version 0.6 based on the stored db version. |
| 199 |
* |
| 200 |
* @access public |
| 201 |
* @return void |
| 202 |
*/ |
| 203 |
public static function vimeography_update_db_to_0_6() { |
| 204 |
if ( version_compare(self::$_version, '0.6', '<') ) { |
| 205 |
global $wpdb; |
| 206 |
$old_galleries = $wpdb->get_results('SELECT * FROM '.$wpdb->vimeography_gallery_meta.' AS meta JOIN '.$wpdb->vimeography_gallery.' AS gallery ON meta.gallery_id = gallery.id;'); |
| 207 |
$new_galleries = array(); |
| 208 |
|
| 209 |
if ( is_array($old_galleries) ) |
| 210 |
{ |
| 211 |
foreach ($old_galleries as $old_gallery) |
| 212 |
{ |
| 213 |
$new_gallery = array(); |
| 214 |
|
| 215 |
$new_gallery['gallery_id'] = $old_gallery->gallery_id; |
| 216 |
$new_gallery['video_limit'] = $old_gallery->video_count; |
| 217 |
$new_gallery['featured_video'] = $old_gallery->featured_video; |
| 218 |
$new_gallery['cache_timeout'] = $old_gallery->cache_timeout; |
| 219 |
$new_gallery['theme_name'] = $old_gallery->theme_name; |
| 220 |
switch ($old_gallery->source_type) |
| 221 |
{ |
| 222 |
case 'user': |
| 223 |
$new_gallery['source_url'] = 'https://vimeo.com/'.$old_gallery->source_name; |
| 224 |
break; |
| 225 |
case 'album': |
| 226 |
$new_gallery['source_url'] = 'https://vimeo.com/album/'.$old_gallery->source_name; |
| 227 |
break; |
| 228 |
case 'group': |
| 229 |
$new_gallery['source_url'] = 'https://vimeo.com/groups/'.$old_gallery->source_name; |
| 230 |
break; |
| 231 |
case 'channel': |
| 232 |
$new_gallery['source_url'] = 'https://vimeo.com/channels/'.$old_gallery->source_name; |
| 233 |
break; |
| 234 |
} |
| 235 |
$new_galleries[] = $new_gallery; |
| 236 |
} |
| 237 |
} |
| 238 |
$wpdb->query('DROP TABLE '.$wpdb->vimeography_gallery_meta.';'); |
| 239 |
|
| 240 |
self::vimeography_update_tables(); |
| 241 |
|
| 242 |
foreach ($new_galleries as $new_gallery) |
| 243 |
{ |
| 244 |
$wpdb->insert( |
| 245 |
$wpdb->vimeography_gallery_meta, |
| 246 |
$new_gallery |
| 247 |
); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Check if the Vimeography database structure needs updated to version 0.7 based on the stored db version. |
| 254 |
* |
| 255 |
* @access public |
| 256 |
* @return void |
| 257 |
*/ |
| 258 |
public static function vimeography_update_db_to_0_7() |
| 259 |
{ |
| 260 |
if ( version_compare(self::$_version, '0.7', '<') ) |
| 261 |
{ |
| 262 |
self::vimeography_update_tables(); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Check if the Vimeography database structure needs updated to version 0.8 based on the stored db version. |
| 268 |
* In this update, we're converting the featured video field to contain an entire URL, not just the video ID. |
| 269 |
* |
| 270 |
* @access public |
| 271 |
* @return void |
| 272 |
*/ |
| 273 |
public static function vimeography_update_db_to_0_8() |
| 274 |
{ |
| 275 |
if ( version_compare(self::$_version, '0.8', '<') ) |
| 276 |
{ |
| 277 |
global $wpdb; |
| 278 |
$old_galleries = $wpdb->get_results('SELECT * FROM '.$wpdb->vimeography_gallery_meta.' AS meta JOIN '.$wpdb->vimeography_gallery.' AS gallery ON meta.gallery_id = gallery.id;'); |
| 279 |
$new_galleries = array(); |
| 280 |
|
| 281 |
if (is_array($old_galleries)) |
| 282 |
{ |
| 283 |
foreach ($old_galleries as $old_gallery) |
| 284 |
{ |
| 285 |
$new_gallery = array(); |
| 286 |
|
| 287 |
$new_gallery['gallery_id'] = $old_gallery->gallery_id; |
| 288 |
$new_gallery['source_url'] = $old_gallery->source_url; |
| 289 |
$new_gallery['video_limit'] = $old_gallery->video_limit; |
| 290 |
$new_gallery['featured_video'] = empty($old_gallery->featured_video) ? '' : 'https://vimeo.com/'.$old_gallery->featured_video; |
| 291 |
$new_gallery['cache_timeout'] = $old_gallery->cache_timeout; |
| 292 |
$new_gallery['theme_name'] = $old_gallery->theme_name; |
| 293 |
$new_gallery['gallery_width'] = $old_gallery->gallery_width; |
| 294 |
$new_galleries[] = $new_gallery; |
| 295 |
} |
| 296 |
} |
| 297 |
$wpdb->query('DROP TABLE '.$wpdb->vimeography_gallery_meta.';'); |
| 298 |
|
| 299 |
self::vimeography_update_tables(); |
| 300 |
|
| 301 |
foreach ($new_galleries as $new_gallery) |
| 302 |
{ |
| 303 |
$wpdb->insert( |
| 304 |
$wpdb->vimeography_gallery_meta, |
| 305 |
$new_gallery |
| 306 |
); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Database changes for version 1.0 |
| 313 |
* |
| 314 |
* - Adds a resource_uri column, which contains the resource |
| 315 |
* being fetched by the new API |
| 316 |
* |
| 317 |
* - Converts the existing source URL to the resource |
| 318 |
* |
| 319 |
* - Drops the video limit |
| 320 |
* |
| 321 |
* @return [type] [description] |
| 322 |
*/ |
| 323 |
public function vimeography_update_db_to_1_0() |
| 324 |
{ |
| 325 |
|
| 326 |
if ( version_compare(self::$_version, '1.0', '<') ) |
| 327 |
{ |
| 328 |
global $wpdb; |
| 329 |
$wpdb->hide_errors(); |
| 330 |
|
| 331 |
$wpdb->query('ALTER TABLE '.$wpdb->vimeography_gallery_meta.' ADD resource_uri VARCHAR(50) NOT NULL AFTER source_url;'); |
| 332 |
|
| 333 |
$rows = $wpdb->get_results('SELECT gallery_id, source_url FROM '.$wpdb->vimeography_gallery_meta.' WHERE 1'); |
| 334 |
|
| 335 |
if (!empty($rows)) |
| 336 |
{ |
| 337 |
foreach ($rows as $row) |
| 338 |
{ |
| 339 |
try |
| 340 |
{ |
| 341 |
// Convert source_url to resource, then update with value |
| 342 |
$resource_uri = $this->validate_vimeo_source($row->source_url); |
| 343 |
$wpdb->update( $wpdb->vimeography_gallery_meta, array('resource_uri' => $resource_uri), array('gallery_id' => $row->gallery_id) ); |
| 344 |
|
| 345 |
} |
| 346 |
catch (Vimeography_Exception $e) |
| 347 |
{ |
| 348 |
// source_url was not valid, delete row from database |
| 349 |
$wpdb->query( |
| 350 |
$wpdb->prepare( |
| 351 |
' |
| 352 |
DELETE gallery, meta |
| 353 |
FROM '.$wpdb->vimeography_gallery.' gallery, '.$wpdb->vimeography_gallery_meta.' meta |
| 354 |
WHERE gallery.id = %d |
| 355 |
AND meta.gallery_id = %d |
| 356 |
', |
| 357 |
$row->gallery_id, $row->gallery_id |
| 358 |
) |
| 359 |
); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
} // end row manipulation |
| 364 |
|
| 365 |
// Drop the video limit. Edit: 7/28/13 - decided to keep this. The next function will add it if it doesn't exist. |
| 366 |
// $result = $wpdb->query('ALTER TABLE '. $wpdb->vimeography_gallery_meta .' DROP COLUMN video_limit'); |
| 367 |
|
| 368 |
self::vimeography_update_tables(); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
public static function vimeography_update_db_to_1_0_7() |
| 373 |
{ |
| 374 |
if ( version_compare(self::$_version, '1.0.7', '<') ) |
| 375 |
{ |
| 376 |
global $wpdb; |
| 377 |
$wpdb->hide_errors(); |
| 378 |
|
| 379 |
$wpdb->query('ALTER TABLE '.$wpdb->vimeography_gallery_meta.' ADD video_limit MEDIUMINT(7) NOT NULL AFTER featured_video;'); |
| 380 |
self::vimeography_update_tables(); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
public static function vimeography_update_db_to_1_1_4() |
| 385 |
{ |
| 386 |
if ( version_compare(self::$_version, '1.1.4', '<') ) |
| 387 |
{ |
| 388 |
global $wpdb; |
| 389 |
$wpdb->hide_errors(); |
| 390 |
|
| 391 |
$wpdb->query('ALTER TABLE '.$wpdb->vimeography_gallery_meta.' MODIFY resource_uri VARCHAR(100) NOT NULL;'); |
| 392 |
self::vimeography_update_tables(); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
public static function vimeography_update_db_to_1_1_6() |
| 397 |
{ |
| 398 |
if ( version_compare(self::$_version, '1.1.6', '<') ) |
| 399 |
{ |
| 400 |
$activation_keys = get_option('vimeography_activation_keys'); |
| 401 |
|
| 402 |
if ($activation_keys) |
| 403 |
{ |
| 404 |
foreach ($activation_keys as $entry) |
| 405 |
{ |
| 406 |
$activation_key_plugin_path = str_replace('vimeography/', trailingslashit($entry->activation_key), VIMEOGRAPHY_PATH); |
| 407 |
$corrected_plugin_path = str_replace('vimeography/', trailingslashit($entry->plugin_name), VIMEOGRAPHY_PATH); |
| 408 |
|
| 409 |
if (file_exists($activation_key_plugin_path)) |
| 410 |
{ |
| 411 |
// Temporarily deactivate plugin |
| 412 |
$old_basename = trailingslashit($entry->activation_key) . $entry->plugin_name . '.php'; |
| 413 |
$new_basename = trailingslashit($entry->plugin_name) . $entry->plugin_name . '.php'; |
| 414 |
|
| 415 |
// Rename folder to the correct plugin name |
| 416 |
rename($activation_key_plugin_path, $corrected_plugin_path); |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Loop through all of the keys and remove the lot if there are |
| 425 |
* false positives that have been saved. We're also moving |
| 426 |
* to site_option instead of just option in this release. |
| 427 |
* |
| 428 |
* @return void |
| 429 |
*/ |
| 430 |
public static function vimeography_update_db_to_1_2() { |
| 431 |
if ( version_compare(self::$_version, '1.2', '<') ) { |
| 432 |
|
| 433 |
$keys = get_option('vimeography_activation_keys'); |
| 434 |
|
| 435 |
if ($keys) { |
| 436 |
delete_option('vimeography_activation_keys'); |
| 437 |
update_site_option('vimeography_activation_keys', $keys); |
| 438 |
|
| 439 |
foreach ($keys as $key) { |
| 440 |
if ($key === FALSE OR $key === NULL) { |
| 441 |
delete_site_option('vimeography_activation_keys'); |
| 442 |
update_site_option('vimeography_corrupt_keys_found', TRUE); |
| 443 |
break; |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* Retrieve and store more information about any saved license keys. |
| 452 |
* |
| 453 |
* @return void |
| 454 |
*/ |
| 455 |
public static function vimeography_update_db_to_1_2_8() { |
| 456 |
if ( version_compare( self::$_version, '1.2.8', '<' ) ) { |
| 457 |
$licenses = get_site_option('vimeography_activation_keys'); |
| 458 |
|
| 459 |
if ( ! class_exists('Vimeography_Update') ) { |
| 460 |
require_once VIMEOGRAPHY_PATH . 'lib/update.php'; |
| 461 |
$updater = new Vimeography_Update; |
| 462 |
} else { |
| 463 |
$updater = Vimeography::get_instance()->updater; |
| 464 |
} |
| 465 |
|
| 466 |
if ($licenses) { |
| 467 |
foreach ($licenses as $index => $license) { |
| 468 |
|
| 469 |
// Retrieve more information about the license |
| 470 |
$result = $updater->check_license( $license ); |
| 471 |
$license->status = $result->license; |
| 472 |
$license->expires = $result->expires; |
| 473 |
$license->limit = $result->license_limit; |
| 474 |
$license->activations_left = $result->activations_left; |
| 475 |
|
| 476 |
$licenses[$index] = $license; |
| 477 |
} |
| 478 |
update_site_option('vimeography_activation_keys', $licenses); |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
} |
| 484 |
|