base_prefix . 'top_ten';
if ( $daily ) {
$table_name .= '_daily';
}
$wpdb->query( 'CREATE TEMPORARY TABLE ' . $table_name . '_temp AS SELECT * FROM ' . $table_name . ' GROUP BY postnumber' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( "TRUNCATE TABLE $table_name" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( 'INSERT INTO ' . $table_name . ' SELECT * FROM ' . $table_name . '_temp' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
}
/**
* Function to merge counts with post numbers of blog ID 0 and 1 respectively.
*
* @since 2.0.4
*
* @param bool $daily Daily flag.
*/
function tptn_merge_blogids( $daily = false ) {
global $wpdb;
$table_name = $wpdb->base_prefix . 'top_ten';
if ( $daily ) {
$table_name .= '_daily';
}
if ( $daily ) {
$sql = "
INSERT INTO `$table_name` (postnumber, cntaccess, dp_date, blog_id) (
SELECT
postnumber,
SUM(cntaccess) as sumCount,
dp_date,
1
FROM `$table_name`
WHERE blog_ID IN (0,1)
GROUP BY postnumber, dp_date
) ON DUPLICATE KEY UPDATE cntaccess = VALUES(cntaccess);
";
$wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
} else {
$sql = "
INSERT INTO `$table_name` (postnumber, cntaccess, blog_id) (
SELECT
postnumber,
SUM(cntaccess) as sumCount,
1
FROM `$table_name`
WHERE blog_ID IN (0,1)
GROUP BY postnumber
) ON DUPLICATE KEY UPDATE cntaccess = VALUES(cntaccess);
";
$wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
$wpdb->query( "DELETE FROM $table_name WHERE blog_id = 0" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
/**
* Function to delete and create the primary keys in the database table.
*
* @since 2.5.6
*/
function tptn_recreate_primary_key() {
global $wpdb;
$table_name = $wpdb->base_prefix . 'top_ten';
$table_name_daily = $wpdb->base_prefix . 'top_ten_daily';
$wpdb->hide_errors();
if ( $wpdb->query( $wpdb->prepare( "SHOW INDEXES FROM {$table_name} WHERE Key_name = %s", 'PRIMARY' ) ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( 'ALTER TABLE ' . $table_name . ' DROP PRIMARY KEY ' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange
}
if ( $wpdb->query( $wpdb->prepare( "SHOW INDEXES FROM {$table_name_daily} WHERE Key_name = %s", 'PRIMARY' ) ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( 'ALTER TABLE ' . $table_name_daily . ' DROP PRIMARY KEY ' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange
}
$wpdb->query( 'ALTER TABLE ' . $table_name . ' ADD PRIMARY KEY(postnumber, blog_id) ' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( 'ALTER TABLE ' . $table_name_daily . ' ADD PRIMARY KEY(postnumber, dp_date, blog_id) ' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->show_errors();
}
/**
* Retrieves the SQL code to recreate the PRIMARY KEY.
*
* @since 2.5.7
*/
function tptn_recreate_primary_key_html() {
global $wpdb;
$table_name = $wpdb->base_prefix . 'top_ten';
$table_name_daily = $wpdb->base_prefix . 'top_ten_daily';
$sql = 'ALTER TABLE ' . $table_name . ' DROP PRIMARY KEY; ';
$sql .= '
';
$sql .= 'ALTER TABLE ' . $table_name_daily . ' DROP PRIMARY KEY; ';
$sql .= '
';
$sql .= 'ALTER TABLE ' . $table_name . ' ADD PRIMARY KEY(postnumber, blog_id); ';
$sql .= '
';
$sql .= 'ALTER TABLE ' . $table_name_daily . ' ADD PRIMARY KEY(postnumber, dp_date, blog_id); ';
/**
* Filters the SQL code to recreate the PRIMARY KEY.
*
* @since 2.5.7
* @param string $sql SQL code to recreate PRIMARY KEY.
*/
return apply_filters( 'tptn_recreate_primary_key_html', $sql );
}
/**
* Retrieves the SQL code to recreate the PRIMARY KEY.
*
* @since 2.7.0
*/
function tptn_recreate_tables() {
global $wpdb;
$table_name = $wpdb->base_prefix . 'top_ten';
$table_name_daily = $wpdb->base_prefix . 'top_ten_daily';
$table_name_temp = $table_name . '_temp';
$table_name_daily_temp = $table_name_daily . '_temp';
$wpdb->hide_errors();
// 1. create temporary tables with the data.
$wpdb->query( "CREATE TEMPORARY TABLE {$table_name_temp} SELECT * FROM $table_name;" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query( "CREATE TEMPORARY TABLE {$table_name_daily_temp} SELECT * FROM $table_name_daily;" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// 2. Drop the tables.
$wpdb->query( "DROP TABLE $table_name" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
$wpdb->query( "DROP TABLE $table_name_daily" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
// 3. Run the activation function which will recreate the tables.
tptn_single_activate();
// 4. Reinsert the data from the temporary table.
$sql = "
INSERT INTO `$table_name` (postnumber, cntaccess, blog_id) (
SELECT
postnumber,
cntaccess,
blog_id
FROM `$table_name_temp`
);
";
$wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$sql = "
INSERT INTO `$table_name_daily` (postnumber, cntaccess, dp_date, blog_id) (
SELECT
postnumber,
cntaccess,
dp_date,
blog_id
FROM `$table_name_daily_temp`
);
";
$wpdb->query( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// 5. Drop the temporary tables.
$wpdb->query( "DROP TABLE $table_name_temp" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
$wpdb->query( "DROP TABLE $table_name_daily_temp" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery
$wpdb->show_errors();
}