Skip to content

Instantly share code, notes, and snippets.

@EdwardBock
Last active November 26, 2021 06:05
Show Gist options
  • Save EdwardBock/62c9a6802e2b5d148fbc9fce81fb09cb to your computer and use it in GitHub Desktop.
Save EdwardBock/62c9a6802e2b5d148fbc9fce81fb09cb to your computer and use it in GitHub Desktop.
ReadingTime Database Class
<?php
namespace PublicFunctionOrg\WordPress\ReadingTime;
class Database {
public function __construct(){
global $wpdb;
$this->wpdb = $wpdb;
$this->table = $this->wpdb->prefix."posts_reading_time";
}
public function set($post_id, $reading_time){
return $this->wpdb->replace(
$this->table,
[
"post_id" => $post_id,
"reading_time" => $reading_time,
],
[
"%d",
"%d",
]
);
}
public function get($post_id){
return intval($this->wpdb->get_var(
$this->wpdb->prepare(
"SELECT reading_time FROM {$this->table} WHERE post_id = %d",
$post_id
)
));
}
public function createTables(){
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
\dbDelta("CREATE TABLE IF NOT EXISTS {$this->table}
(
post_id bigint (20) unsigned NOT NULL,
reading_time int(3) unsigned NOT NULL,
primary key (post_id),
key (reading_time),
key post_reading_time (post_id, reading_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
}
}
// use register_activation_hook(...) to create tables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment