| 1 |
<?php |
| 2 |
global $ays_chart_db_version; |
| 3 |
$ays_chart_db_version = '1.0.3'; |
| 4 |
|
| 5 |
/** |
| 6 |
* Fired during plugin activation |
| 7 |
* |
| 8 |
* @link https://ays-pro.com/ |
| 9 |
* @since 1.0.0 |
| 10 |
* |
| 11 |
* @package Chart_Builder |
| 12 |
* @subpackage Chart_Builder/includes |
| 13 |
*/ |
| 14 |
|
| 15 |
/** |
| 16 |
* Fired during plugin activation. |
| 17 |
* |
| 18 |
* This class defines all code necessary to run during the plugin's activation. |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
* @package Chart_Builder |
| 22 |
* @subpackage Chart_Builder/includes |
| 23 |
* @author Chart Builder Team <info@ays-pro.com> |
| 24 |
*/ |
| 25 |
class Chart_Builder_Activator { |
| 26 |
|
| 27 |
/** |
| 28 |
* Short Description. (use period) |
| 29 |
* |
| 30 |
* Long Description. |
| 31 |
* |
| 32 |
* @since 1.0.0 |
| 33 |
*/ |
| 34 |
public static function activate() { |
| 35 |
|
| 36 |
global $wpdb; |
| 37 |
global $ays_chart_db_version; |
| 38 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 39 |
|
| 40 |
$installed_ver = get_option( "ays_chart_db_version" ); |
| 41 |
$charts_table = $wpdb->prefix . CHART_BUILDER_DB_PREFIX . 'charts'; |
| 42 |
$charts_meta_table = $wpdb->prefix . CHART_BUILDER_DB_PREFIX . 'charts_meta'; |
| 43 |
$settings_table = $wpdb->prefix . CHART_BUILDER_DB_PREFIX . 'settings'; |
| 44 |
$charset_collate = $wpdb->get_charset_collate(); |
| 45 |
|
| 46 |
if( $installed_ver != $ays_chart_db_version ) { |
| 47 |
|
| 48 |
$sql = "CREATE TABLE `".$charts_table."` ( |
| 49 |
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 50 |
`author_id` INT(16) UNSIGNED NOT NULL DEFAULT '0', |
| 51 |
`title` TEXT NOT NULL, |
| 52 |
`description` TEXT NOT NULL DEFAULT '', |
| 53 |
`type` VARCHAR(256) NOT NULL DEFAULT '', |
| 54 |
`source_chart_type` VARCHAR(256) NOT NULL DEFAULT '', |
| 55 |
`source_type` VARCHAR(256) NOT NULL DEFAULT '', |
| 56 |
`source` LONGTEXT NOT NULL DEFAULT '', |
| 57 |
`date_created` DATETIME NOT NULL DEFAULT '1000-01-01 00:00:00', |
| 58 |
`date_modified` DATETIME NOT NULL DEFAULT '1000-01-01 00:00:00', |
| 59 |
`status` VARCHAR(256) NOT NULL DEFAULT 'published', |
| 60 |
`trash_status` VARCHAR(256) NOT NULL DEFAULT '', |
| 61 |
`custom_post_id` INT(16) UNSIGNED DEFAULT NULL, |
| 62 |
`ordering` INT(16) NOT NULL, |
| 63 |
`quiz_query` VARCHAR(256) DEFAULT '', |
| 64 |
`quiz_id` INT(16) UNSIGNED DEFAULT '0', |
| 65 |
`options` TEXT NOT NULL DEFAULT '', |
| 66 |
PRIMARY KEY (`id`) |
| 67 |
)$charset_collate;"; |
| 68 |
|
| 69 |
$results = $wpdb->get_results( // phpcs:ignore |
| 70 |
$wpdb->prepare( |
| 71 |
"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = %s AND table_name = %s", |
| 72 |
DB_NAME, |
| 73 |
$charts_table |
| 74 |
) |
| 75 |
); |
| 76 |
if( empty( $results ) ){ |
| 77 |
// phpcs:ignore |
| 78 |
$wpdb->query( $sql ); |
| 79 |
}else{ |
| 80 |
dbDelta( $sql ); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
$sql = "CREATE TABLE `".$charts_meta_table."` ( |
| 85 |
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 86 |
`chart_id` INT(11) UNSIGNED NOT NULL, |
| 87 |
`meta_key` TEXT NOT NULL DEFAULT '', |
| 88 |
`meta_value` TEXT NOT NULL DEFAULT '', |
| 89 |
`note` TEXT NOT NULL DEFAULT '', |
| 90 |
`options` TEXT NOT NULL DEFAULT '', |
| 91 |
PRIMARY KEY (`id`) |
| 92 |
)$charset_collate;"; |
| 93 |
$results = $wpdb->get_results(// phpcs:ignore |
| 94 |
$wpdb->prepare( |
| 95 |
"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = %s AND table_name = %s", |
| 96 |
DB_NAME, |
| 97 |
$charts_meta_table |
| 98 |
) |
| 99 |
); |
| 100 |
|
| 101 |
|
| 102 |
if(empty($results)){ |
| 103 |
// phpcs:ignore |
| 104 |
$wpdb->query( $sql ); |
| 105 |
}else{ |
| 106 |
dbDelta( $sql ); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
$sql = "CREATE TABLE `".$settings_table."` ( |
| 111 |
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 112 |
`meta_key` TEXT NOT NULL DEFAULT '', |
| 113 |
`meta_value` TEXT NOT NULL DEFAULT '', |
| 114 |
`note` TEXT NOT NULL DEFAULT '', |
| 115 |
`options` TEXT NOT NULL DEFAULT '', |
| 116 |
PRIMARY KEY (`id`) |
| 117 |
)$charset_collate;"; |
| 118 |
|
| 119 |
$results = $wpdb->get_results(// phpcs:ignore |
| 120 |
$wpdb->prepare( |
| 121 |
"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = %s AND table_name = %s", |
| 122 |
DB_NAME, |
| 123 |
$settings_table |
| 124 |
) |
| 125 |
); |
| 126 |
|
| 127 |
if(empty($results)){ |
| 128 |
// phpcs:ignore |
| 129 |
$wpdb->query( $sql ); |
| 130 |
}else{ |
| 131 |
dbDelta( $sql ); |
| 132 |
} |
| 133 |
|
| 134 |
update_option( 'ays_chart_db_version', $ays_chart_db_version ); |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
$metas = array( |
| 139 |
"user_roles", |
| 140 |
"google", |
| 141 |
"options" |
| 142 |
); |
| 143 |
|
| 144 |
foreach($metas as $meta_key){ |
| 145 |
$meta_val = ""; |
| 146 |
if($meta_key == "user_roles"){ |
| 147 |
$meta_val = json_encode(array('administrator')); |
| 148 |
} |
| 149 |
|
| 150 |
$result = $wpdb->get_var(// phpcs:ignore |
| 151 |
$wpdb->prepare( |
| 152 |
"SELECT COUNT(*) |
| 153 |
FROM {$settings_table} |
| 154 |
WHERE meta_key = %s", |
| 155 |
$meta_key |
| 156 |
) |
| 157 |
); |
| 158 |
if(intval($result) == 0){ |
| 159 |
$result = $wpdb->insert(// phpcs:ignore |
| 160 |
$settings_table, |
| 161 |
array( |
| 162 |
'meta_key' => $meta_key,// phpcs:ignore |
| 163 |
'meta_value' => $meta_val,// phpcs:ignore |
| 164 |
'note' => "", |
| 165 |
'options' => "" |
| 166 |
), |
| 167 |
array( '%s', '%s', '%s', '%s' ) |
| 168 |
); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
self::create_default_chart(); |
| 173 |
} |
| 174 |
|
| 175 |
public static function db_update_check() { |
| 176 |
global $ays_chart_db_version; |
| 177 |
if ( get_site_option( 'ays_chart_db_version' ) != $ays_chart_db_version ) { |
| 178 |
self::activate(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
public static function create_default_chart() { |
| 183 |
global $wpdb; |
| 184 |
global $ays_chart_db_version; |
| 185 |
|
| 186 |
$charts_table = $wpdb->prefix . CHART_BUILDER_DB_PREFIX . 'charts'; |
| 187 |
$charts_meta_table = $wpdb->prefix . CHART_BUILDER_DB_PREFIX . 'charts_meta'; |
| 188 |
$settings_table = $wpdb->prefix . CHART_BUILDER_DB_PREFIX . 'settings'; |
| 189 |
|
| 190 |
$chart_count = $wpdb->get_var("SELECT COUNT(*) FROM " . $charts_table); |
| 191 |
$chart_meta_count = $wpdb->get_var("SELECT COUNT(*) FROM " . $charts_meta_table); |
| 192 |
if ($chart_count == 0 && $chart_meta_count == 0) { |
| 193 |
$default_chart_source = array( |
| 194 |
"0" => ["User roles", "Visits"], |
| 195 |
"1" => ["Administrator","11"], |
| 196 |
"2" => ["Author","2"], |
| 197 |
"3" => ["Editor","5"], |
| 198 |
"4" => ["Guest","2"], |
| 199 |
"5" => ["Subscriber","7"] |
| 200 |
); |
| 201 |
|
| 202 |
$default_chart_options = array( |
| 203 |
// |
| 204 |
); |
| 205 |
|
| 206 |
$default_chart = array( |
| 207 |
'title' => 'Default chart', |
| 208 |
'description' => '', |
| 209 |
'type' => 'google-charts', |
| 210 |
'source_chart_type' => 'pie_chart', |
| 211 |
'source_type' => 'manual', |
| 212 |
'source' => json_encode($default_chart_source), |
| 213 |
'date_created' => current_time('mysql'), |
| 214 |
'date_modified' => current_time('mysql'), |
| 215 |
'status' => 'published', |
| 216 |
'trash_status' => '', |
| 217 |
'ordering' => '0', |
| 218 |
'options' => json_encode($default_chart_options), |
| 219 |
'author_id' => get_current_user_id() |
| 220 |
); |
| 221 |
|
| 222 |
$wpdb->insert($charts_table, $default_chart); |
| 223 |
|
| 224 |
$chart_id = $wpdb->insert_id; |
| 225 |
|
| 226 |
$post_type_args = array( |
| 227 |
'chart_id' => $chart_id, |
| 228 |
'author_id' => get_current_user_id(), |
| 229 |
'chart_title' => 'Default chart', |
| 230 |
); |
| 231 |
|
| 232 |
$custom_post_id = Chart_Builder_Custom_Post_Type::ays_chart_add_custom_post($post_type_args); |
| 233 |
|
| 234 |
$default_chart_meta = array( |
| 235 |
array( |
| 236 |
'chart_id' => $chart_id, |
| 237 |
'meta_key' => 'width',// phpcs:ignore |
| 238 |
'meta_value' => '100',// phpcs:ignore |
| 239 |
'note' => '', |
| 240 |
'options' => '' |
| 241 |
), |
| 242 |
array( |
| 243 |
'chart_id' => $chart_id, |
| 244 |
'meta_key' => 'height',// phpcs:ignore |
| 245 |
'meta_value' => '400',// phpcs:ignore |
| 246 |
'note' => '', |
| 247 |
'options' => '' |
| 248 |
), |
| 249 |
array( |
| 250 |
'chart_id' => $chart_id, |
| 251 |
'meta_key' => 'title_color',// phpcs:ignore |
| 252 |
'meta_value' => '#000000',// phpcs:ignore |
| 253 |
'note' => '', |
| 254 |
'options' => '' |
| 255 |
), |
| 256 |
array( |
| 257 |
'chart_id' => $chart_id, |
| 258 |
'meta_key' => 'font_size',// phpcs:ignore |
| 259 |
'meta_value' => '14',// phpcs:ignore |
| 260 |
'note' => '', |
| 261 |
'options' => '' |
| 262 |
), |
| 263 |
array( |
| 264 |
'chart_id' => $chart_id, |
| 265 |
'meta_key' => 'tooltip_trigger',// phpcs:ignore |
| 266 |
'meta_value' => 'hover',// phpcs:ignore |
| 267 |
'note' => '', |
| 268 |
'options' => '' |
| 269 |
), |
| 270 |
array( |
| 271 |
'chart_id' => $chart_id, |
| 272 |
'meta_key' => 'show_color_code',// phpcs:ignore |
| 273 |
'meta_value' => 'off',// phpcs:ignore |
| 274 |
'note' => '', |
| 275 |
'options' => '' |
| 276 |
) |
| 277 |
); |
| 278 |
|
| 279 |
foreach ($default_chart_meta as $key => $value) { |
| 280 |
$wpdb->insert($charts_meta_table, $value); |
| 281 |
} |
| 282 |
|
| 283 |
$default_chart['type'] = 'chart-js'; |
| 284 |
$wpdb->insert($charts_table, $default_chart); |
| 285 |
|
| 286 |
$chart_js_id = $wpdb->insert_id; |
| 287 |
|
| 288 |
$post_type_args = array( |
| 289 |
'chart_id' => $chart_js_id, |
| 290 |
'author_id' => get_current_user_id(), |
| 291 |
'chart_title' => 'Default chart', |
| 292 |
); |
| 293 |
|
| 294 |
$custom_post_id = Chart_Builder_Custom_Post_Type::ays_chart_add_custom_post($post_type_args); |
| 295 |
} else { |
| 296 |
$wpdb->update(// phpcs:ignore |
| 297 |
$charts_table, |
| 298 |
array( |
| 299 |
'type' => 'google-charts', |
| 300 |
), |
| 301 |
array( |
| 302 |
'type' => 'google_charts', |
| 303 |
) |
| 304 |
); |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|