| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
/* Protection! */ |
| 6 |
if (! function_exists('add_action')) |
| 7 |
{ |
| 8 |
echo 'Nothing to See here. Move along now people.'; |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
/** |
| 14 |
* Delta the Directory table (install or update) |
| 15 |
*/ |
| 16 |
function name_directory_db_tables() |
| 17 |
{ |
| 18 |
global $name_directory_db_version; |
| 19 |
global $name_directory_table_directory; |
| 20 |
global $name_directory_table_directory_name; |
| 21 |
|
| 22 |
$name_directory_table_queries = array(" |
| 23 |
CREATE TABLE $name_directory_table_directory ( |
| 24 |
id INT( 11 ) NOT NULL AUTO_INCREMENT, |
| 25 |
name VARCHAR( 255 ) NOT NULL, |
| 26 |
show_title BOOLEAN NULL, |
| 27 |
show_description BOOLEAN NULL DEFAULT 1, |
| 28 |
show_submit_form BOOLEAN NULL, |
| 29 |
show_submitter_name BOOLEAN NULL, |
| 30 |
show_line_between_names BOOLEAN NULL, |
| 31 |
show_character_header BOOLEAN NULL, |
| 32 |
show_search_form BOOLEAN NULL, |
| 33 |
show_all_names_on_index BOOLEAN NULL DEFAULT 1, |
| 34 |
show_all_index_letters BOOLEAN NULL DEFAULT 1, |
| 35 |
show_current_num_names BOOLEAN NULL DEFAULT 1, |
| 36 |
show_index_instructions BOOLEAN NULL DEFAULT 1, |
| 37 |
search_in_description BOOLEAN NULL DEFAULT 0, |
| 38 |
search_highlight BOOLEAN NULL DEFAULT 1, |
| 39 |
jump_to_search_results BOOLEAN NULL DEFAULT 0, |
| 40 |
nr_columns INT( 1 ) NULL, |
| 41 |
nr_most_recent INT(5) NULL DEFAULT 0, |
| 42 |
nr_words_description INT(5) NULL DEFAULT 0, |
| 43 |
description TEXT NULL, |
| 44 |
email_for_submission VARCHAR( 255 ) NULL, |
| 45 |
name_term VARCHAR( 255 ) NULL, |
| 46 |
name_term_singular VARCHAR( 255 ) NULL, |
| 47 |
submitted_by_term VARCHAR( 255 ) NULL, |
| 48 |
check_submitted_names_first BOOLEAN NULL DEFAULT 1, |
| 49 |
UNIQUE KEY id (id), |
| 50 |
PRIMARY KEY (id));", |
| 51 |
"CREATE TABLE $name_directory_table_directory_name ( |
| 52 |
id INT( 11 ) NOT NULL AUTO_INCREMENT, |
| 53 |
directory INT( 11 ) NOT NULL , |
| 54 |
name VARCHAR( 255 ) NOT NULL , |
| 55 |
letter VARCHAR( 1 ) NOT NULL , |
| 56 |
description TEXT NULL , |
| 57 |
published BOOL NOT NULL , |
| 58 |
submitted_by VARCHAR( 255 ) NULL, |
| 59 |
UNIQUE KEY id (id), |
| 60 |
PRIMARY KEY (id));" |
| 61 |
); |
| 62 |
|
| 63 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 64 |
|
| 65 |
dbDelta( $name_directory_table_queries ); |
| 66 |
|
| 67 |
update_option("name_directory_db_version", $name_directory_db_version); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* This function holds all kinds of maintenance that can be invoked after an update |
| 73 |
*/ |
| 74 |
function name_directory_db_post_update_actions() |
| 75 |
{ |
| 76 |
global $wpdb; |
| 77 |
global $name_directory_db_version; |
| 78 |
global $name_directory_table_directory; |
| 79 |
global $name_directory_table_directory_name; |
| 80 |
|
| 81 |
$convert_dirs = "ALTER TABLE $name_directory_table_directory CONVERT TO CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';"; |
| 82 |
$wpdb->query($convert_dirs); |
| 83 |
|
| 84 |
$convert_names = "ALTER TABLE $name_directory_table_directory_name CONVERT TO CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci';"; |
| 85 |
$wpdb->query($convert_names); |
| 86 |
|
| 87 |
$convert_opt = "UPDATE $name_directory_table_directory SET `show_all_names_on_index`=1 WHERE `show_all_names_on_index` IS NULL;"; |
| 88 |
$wpdb->query($convert_opt); |
| 89 |
|
| 90 |
$wpdb->show_errors = false; |
| 91 |
$wpdb->suppress_errors = true; |
| 92 |
$make_index = "ALTER TABLE $name_directory_table_directory_name ADD INDEX `dir_idx` (`directory` DESC);"; |
| 93 |
$wpdb->query($make_index); |
| 94 |
$make_index2 = "ALTER TABLE $name_directory_table_directory_name ADD INDEX `published_idx` (`published` DESC);"; |
| 95 |
$wpdb->query($make_index2); |
| 96 |
$wpdb->suppress_errors = false; |
| 97 |
|
| 98 |
update_option("name_directory_db_version", $name_directory_db_version); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
/** |
| 103 |
* Install some sample data on first install |
| 104 |
*/ |
| 105 |
function name_directory_db_install_demo_data() |
| 106 |
{ |
| 107 |
global $wpdb; |
| 108 |
global $name_directory_table_directory; |
| 109 |
global $name_directory_table_directory_name; |
| 110 |
|
| 111 |
// Only insert sample data when there is no data |
| 112 |
$wpdb->query(sprintf("SELECT * FROM `%s`", $name_directory_table_directory)); |
| 113 |
if($wpdb->num_rows === 0) |
| 114 |
{ |
| 115 |
$wpdb->insert($name_directory_table_directory, array( |
| 116 |
'id' => 1, |
| 117 |
'name' => 'Bird names (demo data)', |
| 118 |
'show_title' => 1, |
| 119 |
'show_description' => 1, |
| 120 |
'show_submit_form' => 1, |
| 121 |
'show_submitter_name' => 0, |
| 122 |
'show_character_header' => 0, |
| 123 |
'show_line_between_names' => 1, |
| 124 |
'show_search_form' => 1, |
| 125 |
'search_in_description' => 1, |
| 126 |
'search_highlight' => 0, |
| 127 |
'show_all_names_on_index' => 1, |
| 128 |
'jump_to_search_results' => 0, |
| 129 |
'nr_most_recent' => 0, |
| 130 |
'nr_words_description' => 0, |
| 131 |
'check_submitted_names_first' => 1, |
| 132 |
'description' => 'Cool budgie names', |
| 133 |
'name_term' => 'budgies', |
| 134 |
'name_term_singular' => 'budgie', |
| 135 |
'submitted_by_term' => 'Birdwatcher' |
| 136 |
)); |
| 137 |
$wpdb->insert($name_directory_table_directory_name, array( |
| 138 |
'directory' => 1, |
| 139 |
'name' => 'Navi', |
| 140 |
'letter' => 'N', |
| 141 |
'description' => 'Navi is a good aviator and navigator. A very strong and big budgie, almost English', |
| 142 |
'published' => 1 |
| 143 |
)); |
| 144 |
$wpdb->insert($name_directory_table_directory_name, array( |
| 145 |
'directory' => 1, |
| 146 |
'name' => 'Mister', |
| 147 |
'letter' => 'M', |
| 148 |
'description' => 'Mister is a name which can only be assigned to a typical English Budgie. Big, strong and stringent.', |
| 149 |
'published' => 1 |
| 150 |
)); |
| 151 |
$wpdb->insert($name_directory_table_directory_name, array( |
| 152 |
'directory' => 1, |
| 153 |
'name' => 'Isa', |
| 154 |
'letter' => 'I', |
| 155 |
'description' => 'Isa is a direct descent of Mister. As a fullblood daughter she is also a typical English Budgie.', |
| 156 |
'published' => 1 |
| 157 |
)); |
| 158 |
|
| 159 |
name_directory_db_post_update_actions(); |
| 160 |
} |
| 161 |
} |
| 162 |
|