-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathupgrade.php
155 lines (117 loc) · 4.83 KB
/
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
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
<?php
if(!class_exists('WpPluginUpgradeBase')) :
abstract class WpPluginUpgradeBase {
function __construct() {
add_action( 'admin_init', array(&$this, 'load_plugin') );
}
/**
* Namespace the given key
* @param string $key the key to namespace
* @return the namespaced key
*/
public function N($key = false) {
// nothing provided, return namespace
if( ! $key || empty($key) ) { return get_class($this); }
return sprintf('%s_%s', get_class($this), $key);
}
const HOOK_ACTIVATED = 'activated';
const HOOK_LOADED = 'loaded';
public function loaded_hook_name() { return $this->N(self::HOOK_LOADED); }
public function activated_hook_name() { return $this->N(self::HOOK_ACTIVATED); }
public function register($original_file) {
register_activation_hook( $original_file, array( &$this, 'activate' ) );
}
public function activate() {
// delay doing anything until plugins are actually ready
// see http://codex.wordpress.org/Function_Reference/register_activation_hook#Process_Flow
add_option( $this->N(), $this->N() );
/* other non-plugin-dependent activation code here */
do_action( $this->activated_hook_name(), 'activated' );
}
public function load_plugin() {
if ( is_admin() && get_option( $this->N() ) == $this->N() ) {
// clear the 'run once' flag
delete_option( $this->N() );
/* do stuff once right after activation */
do_action( $this->loaded_hook_name(), 'loaded' );
}
}
}//--- class WpPluginUpgradeBase
endif; // class_exists
class Forms3rdPartyIntegrationUpgrade extends WpPluginUpgradeBase {
function __construct() {
parent::__construct();
add_action($this->loaded_hook_name(), array(&$this, 'loaded'));
## test
### add_action($this->loaded_hook_name(), array(&$this, 'test'));
### add_action($this->activated_hook_name(), array(&$this, 'test'));
}
public function test($action) {
// just prove it was called
error_log(print_r(array(__FILE__, __CLASS__, __FUNCTION__, $action), true));
}
/**
* List of important upgrade steps
*/
private $upgrades = array(self::VERSION_FPLUGINBASE);
const VERSION_FPLUGINBASE = '1.6.0';
public function loaded($action) {
// check current plugin version
$current = Forms3rdPartyIntegration::pluginVersion;
// compare against prev version and do stuff
$prev = get_option( Forms3rdPartyIntegration::$instance->N('version') );
### error_log('prev version ' . $prev . ', current ' . $current);
// special case: we've never set the version before; not all plugins will need to upgrade in that case
if(empty($prev) || version_compare($prev, $current) < 0) {
// are there upgrade steps depending on how out-of-date?
foreach($this->upgrades as $next_version) {
if(version_compare($prev, $next_version) < 0) $this->do_upgrade($prev, $next_version);
$prev = $next_version;
}
}
// update stored plugin version for next time
update_option(Forms3rdPartyIntegration::$instance->N('version'), $current);
}
function do_upgrade($prev, $next) {
## error_log('upgrade from ' . $prev . ' to ' . $next);
switch($next) {
case self::VERSION_FPLUGINBASE:
## error_log('doing fpluginbase upgrade...');
// check the attached forms, and guess what prefixes should be added based on the
// currently activated form plugins.
// should be okay to "overdo it" and add multiple prefixes,
// because they'll get selected and then corrected on next admin save
$services = Forms3rdPartyIntegration::$instance->get_services();
// only add prefix if corresponding plugin is active
$has_cf7 = is_plugin_active('contact-form-7/wp-contact-form-7.php');
$has_gf = is_plugin_active('gravityforms/gravityforms.php');
// $has_ninja = is_plugin_active('ninja-forms/ninja-forms.php'); // don't need this for < 1.6.0
$prefixes = array();
if($has_cf7) $prefixes []= Forms3rdpartyIntegration_CF7::FORM_ID_PREFIX;
if($has_gf) $prefixes []= Forms3rdpartyIntegration_Gf::FORM_ID_PREFIX;
// nothing to do? quit
if(empty($prefixes)) break; // return?
foreach($services as &$service) {
if( !isset($service['forms']) || empty($service['forms']) ) continue; // nothing attached
$new_forms_list = array();
foreach($service['forms'] as &$form_id) {
// old style, no prefix?
if( ! is_numeric($form_id) ) {
// don't really need to preserve this, since this shouldn't ever happen
// unless someone migrated and messed with settings on purpose
// any any invalid values will disappear the next time the plugin settings are saved
$new_forms_list []= $form_id;
continue;
}
foreach($prefixes as $prefix) {
$new_forms_list []= $prefix . $form_id;
}
}
$service['forms'] = $new_forms_list;
} // foreach service
// now save the service changes
Forms3rdPartyIntegration::$instance->save_services($services);
break;
}
}
}