my-maxi-skirt /wp-content/plugins/wp-lister-for-ebay/classes/WPL_Install_Uninstall.php

Language PHP Lines 340
MD5 Hash 54d3e95b44e350bed2998a4fc812ac02 Estimated Cost $5,906 (why?)
Repository https://bitbucket.org/sanders_nick/my-maxi-skirt.git View Raw File
  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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php

class WPLister_Install {
	
	public function __construct( $file ) {
		register_activation_hook( $file, array( &$this, 'onWpActivatePlugin' ) );
		add_action( 'wpmu_new_blog', array( &$this, 'onWpmuNewBlog' ), 10, 6 );
	}
 
	public function onWpActivatePlugin( $networkwide ) {
		global $wpdb;
        // global $wpl_logger;
        // $wpl_logger = new WPL_Logger();
		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

	    // check for multisite installation
	    if (function_exists('is_multisite') && is_multisite()) {

	        // check if it is a network activation - if so, run the activation function for each blog id
	        if ($networkwide) {
                // $old_blog = $wpdb->blogid;

	            // Get all blog ids
	            $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs"));
	            foreach ($blogids as $blog_id) {

	                switch_to_blog($blog_id);

					$this->createOptions( $networkwide );
					$this->createFolders( $networkwide );
					$this->createTables( $networkwide );

					restore_current_blog();
	            }

	            // switch_to_blog($old_blog);
	            // return;
	        }   

	    } else {
	    	// no multisite
			$this->createOptions( $networkwide );
			$this->createFolders( $networkwide );
			$this->createTables( $networkwide );
	    }

        // debug:
        // echo "<br> blogid: ".$wpdb->blogid;
        // echo "<br> networkwide: ".print_r($networkwide,1);
        // echo "<br> is_main_site(): ".print_r(is_main_site(),1);
        // echo "<br> get_current_site(): ".print_r(get_current_site(),1);
        // die();

	}
	
	public function onWpmuNewBlog( $blog_id, $user_id, $domain, $path, $site_id, $meta ) {
	    if (is_plugin_active_for_network('wp-lister/wp-lister.php')) {
	        switch_to_blog($blog_id);
			$this->createOptions( $networkwide );
			$this->createFolders( $networkwide );
			$this->createTables( $networkwide );
			restore_current_blog();
	    }
	}

	public function createOptions( $networkwide ) {

        if ( $networkwide ) {
			WPL_WPLister::addOption( 'is_network_activated','1' );
        } else {
			WPL_WPLister::addOption( 'is_network_activated','0' );
        }

		WPL_WPLister::addOption( 'ebay_token',			'' );
		WPL_WPLister::addOption( 'ebay_site_id',		'0' );
		WPL_WPLister::addOption( 'sandbox_enabled',		'0' );
		
		WPL_WPLister::addOption( 'paypal_email',		'' );
		WPL_WPLister::addOption( 'cron_auctions',		'Y' );
		WPL_WPLister::addOption( 'cron_transactions',	'Y' );
		WPL_WPLister::addOption( 'log_level',			'' );
		WPL_WPLister::addOption( 'log_to_db',			'0' );
		WPL_WPLister::addOption( 'uninstall',			'0' );
		WPL_WPLister::addOption( 'db_version',			'4' );

		WPL_WPLister::addOption( 'setup_next_step',		'1' );

	}
	
	public function createFolders( $networkwide ) {
		global $wpl_logger;
		$wpl_logger->info('creating wp-content/uploads/wp-lister/templates etc.');		

		// make subdirectories in wp-content/uploads
		$uploads = wp_upload_dir();
		$uploaddir = $uploads['basedir'];

		$wpldir = $uploaddir . '/wp-lister';
		if ( !is_dir($wpldir) ) mkdir($wpldir);

		$tpldir = $wpldir . '/templates';
		if ( !is_dir($tpldir) ) mkdir($tpldir);

		$wpl_logger->info('template folder: '.$tpldir);		
	
	}
	
	public function createTables( $networkwide ) {
		global $wpdb;
        global $wpl_logger;
        // $wpl_logger = new WPL_Logger();
		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');


		$wpl_logger->info("creating table {$wpdb->prefix}ebay_auctions");		

		// create table: ebay_auctions
		$sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}ebay_auctions` (
		  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
		  `ebay_id` bigint(255) DEFAULT NULL,
		  `auction_title` varchar(255) DEFAULT NULL,
		  `auction_type` varchar(255) DEFAULT NULL,
		  `listing_duration` varchar(255) DEFAULT NULL,
		  `date_created` datetime DEFAULT NULL,
		  `date_published` datetime DEFAULT NULL,
		  `date_finished` datetime DEFAULT NULL,
		  `end_date` datetime DEFAULT NULL,
		  `price` float DEFAULT NULL,
		  `quantity` int(11) DEFAULT NULL,
		  `quantity_sold` int(11) DEFAULT NULL,
		  `status` varchar(50) DEFAULT NULL,
		  `details` text,
		  `ViewItemURL` varchar(255) DEFAULT NULL,
		  `GalleryURL` varchar(255) DEFAULT NULL,
		  `post_content` text,
		  `post_id` int(11) DEFAULT NULL,
		  `profile_id` int(11) DEFAULT NULL,
		  `profile_data` text,
		  `template` varchar(255) DEFAULT '',
		  `fees` float DEFAULT NULL,
		  PRIMARY KEY  (`id`)
		);";
		#dbDelta($sql);
		$result = $wpdb->query($sql);

		$wpl_logger->info($sql);		
		$wpl_logger->info(mysql_error());		
		
		
		// create table: ebay_categories
		$sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}ebay_categories` (
		  `cat_id` bigint(16) DEFAULT NULL,
		  `parent_cat_id` bigint(11) DEFAULT NULL,
		  `level` int(11) DEFAULT NULL,
		  `leaf` tinyint(4) DEFAULT NULL,
		  `version` int(11) DEFAULT NULL,
		  `cat_name` varchar(255) DEFAULT NULL,
		  `wp_term_id` int(11) DEFAULT NULL,
		  KEY `cat_id` (`cat_id`),
		  KEY `parent_cat_id` (`parent_cat_id`)		
		);";
		$wpdb->query($sql);
		
		
		// create table: ebay_store_categories
		$sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}ebay_store_categories` (
		  `cat_id` bigint(20) DEFAULT NULL,
		  `parent_cat_id` bigint(20) DEFAULT NULL,
		  `level` int(11) DEFAULT NULL,
		  `leaf` tinyint(4) DEFAULT NULL,
		  `version` int(11) DEFAULT NULL,
		  `cat_name` varchar(255) DEFAULT NULL,
		  `order` int(11) DEFAULT NULL,
		  `wp_term_id` int(11) DEFAULT NULL,
		  KEY `cat_id` (`cat_id`),
		  KEY `parent_cat_id` (`parent_cat_id`)		
		);";
		$wpdb->query($sql);
		
		
		// create table: ebay_payment
		$sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}ebay_payment` (
		  `payment_name` varchar(255) DEFAULT NULL,
		  `payment_description` varchar(255) DEFAULT NULL,
		  `version` int(11) DEFAULT NULL	
		);";
		$wpdb->query($sql);
		
		
		// create table: ebay_profiles
		$sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}ebay_profiles` (
		  `profile_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
		  `profile_name` varchar(255) DEFAULT NULL,
		  `profile_description` varchar(255) DEFAULT NULL,
		  `listing_duration` varchar(255) DEFAULT NULL,
		  `type` varchar(255) DEFAULT NULL,
		  `details` text,
		  `conditions` text,
		  PRIMARY KEY  (`profile_id`)	
		);";
		$wpdb->query($sql);
		
		
		
		// create table: ebay_shipping
		$sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}ebay_shipping` (
		  `service_id` int(11) DEFAULT NULL,
		  `service_name` varchar(255) DEFAULT NULL,
		  `service_description` varchar(255) DEFAULT NULL,
		  `carrier` varchar(255) DEFAULT NULL,
		  `international` tinyint(4) DEFAULT NULL,
		  `version` int(11) DEFAULT NULL	
		);";
		$wpdb->query($sql);
		
		// create table: ebay_transactions
		$sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}ebay_transactions` (
		  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
		  `item_id` bigint(255) DEFAULT NULL,
		  `transaction_id` bigint(255) DEFAULT NULL,
		  `date_created` datetime DEFAULT NULL,
		  `item_title` varchar(255) DEFAULT NULL,
		  `price` float DEFAULT NULL,
		  `quantity` int(11) DEFAULT NULL,
		  `status` varchar(50) DEFAULT NULL,
		  `details` text,
		  `post_id` int(11) DEFAULT NULL,
		  `buyer_userid` varchar(255) DEFAULT NULL,
		  `buyer_name` varchar(255) DEFAULT NULL,
		  `buyer_email` varchar(255) DEFAULT NULL,
		  `eBayPaymentStatus` varchar(50) DEFAULT NULL,
		  `CheckoutStatus` varchar(50) DEFAULT NULL,
		  `ShippingService` varchar(50) DEFAULT NULL,
		  `PaymentMethod` varchar(50) DEFAULT NULL,
		  `ShippingAddress_City` varchar(50) DEFAULT NULL,
		  `CompleteStatus` varchar(50) DEFAULT NULL,
		  `LastTimeModified` datetime DEFAULT NULL,
		  PRIMARY KEY (`id`)
  		);";
		$wpdb->query($sql);
		
		// create table: ebay_log
		$sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}ebay_log` (
		  `id` int(11) NOT NULL AUTO_INCREMENT,
		  `timestamp` datetime DEFAULT NULL,
		  `request_url` text DEFAULT NULL,
		  `request` text DEFAULT NULL,
		  `response` text DEFAULT NULL,
		  `callname` varchar(64) DEFAULT NULL,
		  `success` varchar(16) DEFAULT NULL,
		  `ebay_id` bigint(255) DEFAULT NULL,
		  `user_id` int(11) DEFAULT NULL,	
		  PRIMARY KEY (`id`)	
		);";
		$wpdb->query($sql);

		// mysql updates - insert new columns
		$this->add_column_if_not_exist( $wpdb->prefix.'ebay_profiles', 'conditions', 'TEXT' );

	}

	// mysql update helper method
	// from http://www.edmondscommerce.co.uk/mysql/mysql-add-column-if-not-exists-php-function/
	function add_column_if_not_exist( $table, $column, $column_attr = "VARCHAR( 255 ) NULL" ){
	    $exists = false;
	    $columns = mysql_query("show columns from $table");
	    while($col = mysql_fetch_assoc($columns)){
	        if($col['Field'] == $column){
	            $exists = true;
	            break;
	        }
	    }      
	    if(!$exists){
	        mysql_query("ALTER TABLE `$table` ADD `$column` $column_attr ");
	    }
	}

}

class WPLister_Uninstall {
	
	// TODO: when uninstalling, maybe have an option to backup and restore settings
	
	public function __construct( $file ) {
		register_deactivation_hook( $file, array( &$this, 'onWpDeactivatePlugin' ) );
	}
	
	public function onWpDeactivatePlugin( $networkwide ) {
		global $wpdb;

	    // check for multisite installation
	    if (function_exists('is_multisite') && is_multisite()) {

	        // check if it is a network (de)activation - if so, run the (de)activation function for each blog id
	        if ($networkwide) {

	            // Get all blog ids
	            $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs"));
	            foreach ($blogids as $blog_id) {

	                switch_to_blog($blog_id);
					$this->deactivatePlugin();
					restore_current_blog();
	            }

	        }   

	    } else {
	    	// no multisite
			$this->deactivatePlugin();
	    }

	}
	
	public function deactivatePlugin() {
		global $wpdb;

		// always uninstall on multisite networks
		if ( ( is_multisite() ) || ( WPL_WPLister::getOption('uninstall') == 1 ) ) {

			// remove tables
			$wpdb->query( 'DROP TABLE '.$wpdb->prefix.'ebay_auctions' );
			$wpdb->query( 'DROP TABLE '.$wpdb->prefix.'ebay_categories' );
			$wpdb->query( 'DROP TABLE '.$wpdb->prefix.'ebay_store_categories' );
			$wpdb->query( 'DROP TABLE '.$wpdb->prefix.'ebay_payment' );
			$wpdb->query( 'DROP TABLE '.$wpdb->prefix.'ebay_profiles' );
			$wpdb->query( 'DROP TABLE '.$wpdb->prefix.'ebay_shipping' );
			$wpdb->query( 'DROP TABLE '.$wpdb->prefix.'ebay_transactions' );
			$wpdb->query( 'DROP TABLE '.$wpdb->prefix.'ebay_log' );			
			$wpdb->query( 'DROP TABLE '.$wpdb->prefix.'ebay_jobs' );			

			// remove options
			$wpdb->query( 'DELETE FROM '.$wpdb->prefix."options WHERE option_name LIKE 'wplister_%' " );

		}

	}
}
Back to Top