function hocwp_insert_trending($args = array()) { if (!is_array($args) && is_numeric($args)) { $args = array('post_id' => $args); } $post_id = hocwp_get_value_by_key($args, 'post_id'); if (hocwp_id_number_valid($post_id)) { global $wpdb; $datetime = hocwp_get_current_datetime_mysql(); $post = get_post($post_id); $action = hocwp_get_value_by_key($args, 'action', 'view'); if (empty($action)) { $action = 'view'; } $table_name = $wpdb->prefix . HOCWP_TRENDING_TABLE; if (!hocwp_is_table_exists($table_name)) { return; } $trending_day = absint(apply_filters('hocwp_trending_interval', 7)); $sql = "DELETE FROM {$table_name} WHERE UNIX_TIMESTAMP(post_date) < UNIX_TIMESTAMP(DATE_SUB('{$datetime}', INTERVAL {$trending_day} DAY))"; $wpdb->query($sql); $sql = "INSERT INTO {$table_name} (post_id, post_date, post_type, action)"; $sql .= " VALUES ('{$post_id}', '{$datetime}', '{$post->post_type}', '{$action}')"; $wpdb->query($sql); } }
function hocwp_statistics_avg() { global $wpdb; $table = $wpdb->prefix . HOCWP_COUNTER_TABLE_STATISTICS; $results = $wpdb->get_results("SELECT visited_timestamp FROM {$table} ORDER BY ID LIMIT 1"); $total_days = 1; if ($wpdb->num_rows > 0) { $timestamp = $results[0]->visited_timestamp; $diff = strtotime(hocwp_get_current_datetime_mysql()) - $timestamp; $total_days = ceil($diff / DAY_IN_SECONDS); } $wpdb->get_results("SELECT ID FROM {$table}"); return ceil($wpdb->num_rows / $total_days); }
function hocwp_human_time_diff_to_now($from) { if (!is_int($from)) { $from = strtotime($from); } return human_time_diff($from, strtotime(hocwp_get_current_datetime_mysql())); }