-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslideshow-upgrade.php
72 lines (60 loc) · 2.06 KB
/
slideshow-upgrade.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
<?php
add_action('init', 'bu_slideshow_upgrade', 99);
/**
* Run any appropriate upgrade routines for the current version &
* update version number in site options
*/
function bu_slideshow_upgrade() {
$current_version = get_option('bu_slideshow_version');
$upgrade_firstrun = ( FALSE === $current_version && version_compare('2.3', BU_SLIDESHOW_VERSION, '<=') );
$update_to_latest = ( -1 === version_compare($current_version, BU_SLIDESHOW_VERSION ) );
if ( $upgrade_firstrun || $update_to_latest ) {
update_option('bu_slideshow_version', BU_SLIDESHOW_VERSION);
if( $upgrade_firstrun || -1 === version_compare($current_version, '2.3' ) ){
bu_slideshow_migrate_shows();
}
}
}
/**
* Move slideshows to individual site options
*/
function bu_slideshow_migrate_shows() {
$all_slideshows = get_option( 'bu_slideshows' , array() );
$has_errors = FALSE;
$slideshow_id_map_option = 'bu_slideshow_id_map';
$map = get_option( $slideshow_id_map_option );
if( FALSE === $map ){
$map = array();
add_option( $slideshow_id_map_option, $map, '', false );
}
foreach ($all_slideshows as $k => $show) {
if( ! intval( $show->id ) ){
$has_errors = TRUE;
error_log( sprintf( '[%s] Invalid Slideshow ID: %d Object: %s',
__FUNCTION__, $show->id, var_export( $show, TRUE ) ) );
continue;
}
$postID = bu_slideshow_create_post( $show, $map );
if( FALSE !== $postID ){
$map[ $show->id ] = $postID;
}
}
// Wrap-up
if( ! $has_errors ){
// delete_option( 'bu_slideshows' );
update_option( $slideshow_id_map_option, $map );
}
}
function bu_slideshow_create_post( $show, $map ){
$result = $postID = $show->create_post();
if( FALSE === $result ){
error_log( sprintf( '[%s] Error creating post: %s Object: %s',
__FUNCTION__, $result->get_error_message(), var_export( $show, TRUE ) ) );
return FALSE;
} else if( array_key_exists( $postID, $map ) ){
error_log( sprintf( '[%s] Error creating post: Slideshow ID %d exists. Map: %s',
__FUNCTION__, $postID, var_export( $map, TRUE ) ) );
return bu_slideshow_create_post( $show, $map );
}
return $postID;
}