| 1 |
<?php |
| 2 |
namespace CatFolders; |
| 3 |
|
| 4 |
class Install { |
| 5 |
public function __construct() { |
| 6 |
add_action( 'init', array( __CLASS__, 'checkVersion' ), 5 ); |
| 7 |
} |
| 8 |
|
| 9 |
public static function install() { |
| 10 |
self::createTables(); |
| 11 |
self::updatePluginVersion(); |
| 12 |
} |
| 13 |
|
| 14 |
public static function createTables() { |
| 15 |
global $wpdb; |
| 16 |
|
| 17 |
$wpdb->hide_errors(); |
| 18 |
|
| 19 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 20 |
|
| 21 |
dbDelta( self::getSchema() ); |
| 22 |
} |
| 23 |
|
| 24 |
public static function getSchema() { |
| 25 |
global $wpdb; |
| 26 |
|
| 27 |
$collate = ''; |
| 28 |
|
| 29 |
if ( $wpdb->has_cap( 'collation' ) ) { |
| 30 |
$collate = $wpdb->get_charset_collate(); |
| 31 |
} |
| 32 |
|
| 33 |
$tables = " |
| 34 |
CREATE TABLE {$wpdb->prefix}catfolders ( |
| 35 |
id int(11) unsigned NOT NULL AUTO_INCREMENT, |
| 36 |
title varchar(250) NOT NULL, |
| 37 |
parent int(11) unsigned NOT NULL DEFAULT 0, |
| 38 |
type varchar(20) NOT NULL DEFAULT 'attachment', |
| 39 |
ord int(11) unsigned NULL DEFAULT 0, |
| 40 |
created_by bigint(20) NOT NULL DEFAULT 0, |
| 41 |
PRIMARY KEY (id) |
| 42 |
) $collate; |
| 43 |
CREATE TABLE {$wpdb->prefix}catfolders_posts ( |
| 44 |
folder_id int(11) unsigned NOT NULL, |
| 45 |
post_id bigint(20) unsigned NOT NULL, |
| 46 |
UNIQUE KEY `folder_post` (`folder_id`,`post_id`) |
| 47 |
) $collate; |
| 48 |
"; |
| 49 |
|
| 50 |
return $tables; |
| 51 |
} |
| 52 |
|
| 53 |
public static function updatePluginVersion() { |
| 54 |
update_option( 'catf_version', CATF_VERSION ); |
| 55 |
} |
| 56 |
|
| 57 |
public static function checkVersion() { |
| 58 |
if ( version_compare( get_option( 'catf_version' ), CATF_VERSION, '<' ) ) { |
| 59 |
self::install(); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|