';
echo '
';
echo __('WP Statistics database requires upgrade.', 'wp-statistics'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '
';
echo '
';
echo '' . esc_html__('Upgrade Database', 'wp-statistics') . '';
echo '
';
echo '
';
echo '
';
});
# Add Script
add_action('admin_footer', function () {
?>
'complete', 'number_process' => 0, 'percentage' => 0);
# Check is Ajax WordPress
if (defined('DOING_AJAX') && DOING_AJAX && User::Access('manage')) {
# Check Status Of Process
if (self::is_require_update_page() === true) {
# Number Process Per Query
$number_per_query = 80;
# Check Number Process
$number_process = self::get_require_number_update();
$i = 0;
if ($number_process > 0) {
# Start Query
$query = $wpdb->get_results(
$wpdb->prepare("SELECT * FROM `" . DB::table('pages') . "` WHERE `type` = '' ORDER BY `page_id` DESC LIMIT 0,%d", $number_per_query),
ARRAY_A);
foreach ($query as $row) {
# Get Page Type
$page_type = self::get_page_type_by_obj($row['id'], $row['uri']);
# Update Table
$wpdb->update(
DB::table('pages'),
array(
'type' => $page_type
),
array('page_id' => $row['page_id'])
);
$i++;
}
# Sanitize the data
$number_all = sanitize_text_field($_GET['number_all']);
if ($number_all > $number_per_query) {
# calculate number process
$return['number_process'] = $number_all - ($number_process - $i);
# Calculate Per
$return['percentage'] = round(($return['number_process'] / $number_all) * 100);
# Set Process
$return['process_status'] = 'incomplete';
} else {
$return['number_process'] = $number_all;
$return['percentage'] = 100;
update_option('wp_statistics_update_page_type', 'yes');
}
}
} else {
# Closed Process
update_option('wp_statistics_update_page_type', 'yes');
}
# Export Data
wp_send_json($return);
exit;
}
});
}
public static function get_require_number_update()
{
global $wpdb;
$pagesTable = DB::table('pages');
if (!DB::ExistTable($pagesTable)) {
return 0;
}
return $wpdb->get_var("SELECT COUNT(*) FROM `{$pagesTable}` WHERE `type` = ''");
}
public static function is_require_update_page()
{
# require update option name
$opt_name = 'wp_statistics_update_page_type';
# Check exist option
$get_opt = get_option($opt_name);
if (!empty($get_opt)) {
return false;
}
# Check number require row
if (self::get_require_number_update() > 0) {
return true;
}
return false;
}
public static function get_page_type_by_obj($obj_ID, $page_url)
{
//Default page type
$page_type = 'unknown';
//check if Home Page
if ($page_url == "/") {
return 'home';
} else {
// Page url
$page_url = ltrim($page_url, "/");
$page_url = trim(get_bloginfo('url'), "/") . "/" . $page_url;
// Check Page Path is exist
$exist_page = url_to_postid($page_url);
//Check Post Exist
if ($exist_page > 0) {
# Get Post Type
$p_type = get_post_type($exist_page);
# Check Post Type
if ($p_type == "product") {
$page_type = 'product';
} elseif ($p_type == "page") {
$page_type = 'page';
} elseif ($p_type == "attachment") {
$page_type = 'attachment';
} else {
$page_type = 'post';
}
} else {
# Check is Term
$term = get_term($obj_ID);
if (is_wp_error(get_term_link($term)) === true) {
//Don't Stuff
} else {
//Which Taxonomy
$taxonomy = $term->taxonomy;
//Check Url is contain
$term_link = get_term_link($term);
$term_link = ltrim(str_ireplace(get_bloginfo('url'), "", $term_link), "/");
if (stripos($page_url, $term_link) === false) {
//Return Unknown
} else {
//Check Type of taxonomy
if ($taxonomy == "category") {
$page_type = 'category';
} elseif ($taxonomy == "post_tag") {
$page_type = 'post_tag';
} else {
$page_type = 'tax_' . $taxonomy;
}
}
}
}
}
return $page_type;
}
}
new Install;