PluginProbe
Vimeography: Vimeo Video Gallery WordPress Plugin / 2.2
Vimeography: Vimeo Video Gallery WordPress Plugin v2.2
2.4.9 2.4.8 trunk 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 0.6 0.6.1 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.6.7 0.6.8 0.6.8.1 0.6.9 0.6.9.1 0.6.9.2 0.7 0.8 All 103 releases
vimeography / lib / database.php

database.php in Vimeography: Vimeo Video Gallery WordPress Plugin 2.2, at lib/database.php

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