Skip to content
Commits on Source (2)
......@@ -17,200 +17,197 @@ define('AI_ACTIVITY_TABLE_NAME', 'ai_activity');
/* Database schema initialization */
function ai_activity_install() {
global $wpdb;
global $ai_activity_db_version;
$table_name = AI_ACTIVITY_TABLE_NAME;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
blog_id integer(11) NOT NULL,
post_id integer(11) NOT NULL,
title text,
author text,
content text,
url text,
published_at datetime DEFAULT current_timestamp(),
PRIMARY KEY (id),
KEY (published_at)
) $charset_collate;";
require_once(ABSPATH . "wp-admin/includes/upgrade.php");
dbDelta($sql);
add_option('ai_activity_db_version', $ai_activity_db_version);
global $wpdb;
global $ai_activity_db_version;
$table_name = AI_ACTIVITY_TABLE_NAME;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
blog_id integer(11) NOT NULL,
post_id integer(11) NOT NULL,
title text,
author text,
content text,
url text,
published_at datetime DEFAULT current_timestamp(),
PRIMARY KEY (id),
KEY published_at (published_at)
) $charset_collate;";
require_once(ABSPATH . "wp-admin/includes/upgrade.php");
dbDelta($sql);
add_option('ai_activity_db_version', $ai_activity_db_version);
}
/* Database schema upgrade check */
function ai_activity_update_db_check() {
global $ai_activity_db_version;
if (get_site_option('ai_activity_db_version') != $ai_activity_db_version) {
ai_activity_install();
}
global $ai_activity_db_version;
if (get_site_option('ai_activity_db_version') != $ai_activity_db_version) {
ai_activity_install();
}
}
/* Publish post callback */
function ai_activity_publish_post($post_id) {
global $wpdb;
$blog_id = get_current_blog_id();
// Check that the blog is public.
$blog_archived = get_blog_status($blog_id, 'archived');
$blog_mature = get_blog_status($blog_id, 'mature');
$blog_spam = get_blog_status($blog_id, 'spam');
$blog_deleted = get_blog_status($blog_id, 'deleted');
if ($blog_archived == '1' || $blog_mature == '1' || $blog_spam == '1' || $blog_deleted == '1') {
return;
}
$post = get_post($post_id, OBJECT, 'display');
// Only applies to published posts.
if ($post->post_type != "post" || $post->post_status != "publish" || !empty($post->post_password)) {
return;
}
$post_title = get_the_title($post);
$post_url = get_post_permalink($post);
//$post_content = apply_filters('the_content', $post->post_content);
$post_content = get_the_content('more...', true, $post);
$post_author = apply_filters('the_author', $post->post_author);
$table_name = AI_ACTIVITY_TABLE_NAME;
$wpdb->insert(
$table_name,
global $wpdb;
$blog_id = get_current_blog_id();
// Check that the blog is public.
$blog_archived = get_blog_status($blog_id, 'archived');
$blog_mature = get_blog_status($blog_id, 'mature');
$blog_spam = get_blog_status($blog_id, 'spam');
$blog_deleted = get_blog_status($blog_id, 'deleted');
if ($blog_archived == '1' || $blog_mature == '1' || $blog_spam == '1' || $blog_deleted == '1') {
return;
}
$post = get_post($post_id, OBJECT, 'display');
// Only applies to published posts.
if ($post->post_type != "post" || $post->post_status != "publish" || !empty($post->post_password)) {
return;
}
$post_title = get_the_title($post);
$post_url = get_post_permalink($post);
//$post_content = apply_filters('the_content', $post->post_content);
$post_content = get_the_content('more...', true, $post);
$post_author = apply_filters('the_author', $post->post_author);
$wpdb->insert(
AI_ACTIVITY_TABLE_NAME,
array(
'title' => $post_title,
'blog_id' => $blog_id,
'post_id' => $post_id,
'author' => $post_author,
'content' => $post_content,
'url' => $post_url,
)
);
'title' => $post_title,
'blog_id' => $blog_id,
'post_id' => $post_id,
'author' => $post_author,
'content' => $post_content,
'url' => $post_url,
)
);
}
function ai_activity_get_current_page() {
$p = $_REQUEST['p'];
if ($p) { return $p; }
return 0;
$p = $_REQUEST['p'];
if ($p) { return $p; }
return 0;
}
function ai_activity_get_previous_page_link() {
$p = ai_activity_get_current_page();
if ($p > 0) {
return "?p=" . ($p - 1);
}
return "";
$p = ai_activity_get_current_page();
if ($p > 0) {
return "?p=" . ($p - 1);
}
return "";
}
function ai_activity_get_next_page_link() {
$p = ai_activity_get_current_page();
return "?p=" . ($p + 1);
$p = ai_activity_get_current_page();
return "?p=" . ($p + 1);
}
function ai_activity_get_latest_posts($offset, $limit) {
global $wpdb;
$table_name = AI_ACTIVITY_TABLE_NAME;
$results = $wpdb->get_results($wpdb->prepare(
"
SELECT blog_id, post_id, title, author, content, url, published_at
FROM $table_name
ORDER BY published_at DESC
LIMIT %d OFFSET %d
",
$limit, $offset
));
return $results;
global $wpdb;
$table_name = AI_ACTIVITY_TABLE_NAME;
$results = $wpdb->get_results($wpdb->prepare(
"
SELECT blog_id, post_id, title, author, content, url, published_at
FROM $table_name
ORDER BY published_at DESC
LIMIT %d OFFSET %d
",
$limit, $offset
));
return $results;
}
function ai_activity_display_recent_posts($tmp_number, $tmp_title_characters, $tmp_content_characters, $tmp_title_link = 'no', $tmp_show_avatars = 'yes', $tmp_avatar_size = 16, $posttype = 'post', $read_more = '', $read_more_link = false, $show_blog = false, $output = true) {
$html = '';
$html = '';
$posts = ai_activity_get_latest_posts($tmp_number * ai_activity_get_current_page(), $tmp_number);
$posts = ai_activity_get_latest_posts($tmp_number * ai_activity_get_current_page(), $tmp_number);
if ($posts) {
$default_avatar = get_option('default_avatar');
if ($posts) {
$default_avatar = get_option('default_avatar');
foreach ($posts as &$post) {
$blog_id = $post->blog_id;
$post_url = $post->url;
foreach ($posts as &$post) {
$blog_id = $post->blog_id;
$post_url = $post->url;
$html .= '<div class="global-activity-post">';
$html .= '<div class="global-activity-post">';
if ($tmp_title_characters > 0) {
if ($tmp_title_characters > 0) {
$html .= '<div class="post-title">';
$html .= '<div class="post-title">';
if ($tmp_show_avatars == 'yes') {
$html .= get_avatar($post->author, $tmp_avatar_size, $default_avatar) . ' ';
}
if ($tmp_title_link == 'no') {
$html .= substr($post->title, 0, $tmp_title_characters);
} else {
$html .= '<a href="' . $post_url . '" >' . substr($post->title, 0, $tmp_title_characters) . '</a>';
}
if ($tmp_show_avatars == 'yes') {
$html .= get_avatar($post->author, $tmp_avatar_size, $default_avatar) . ' ';
}
if ($show_blog) {
$blog_details = get_blog_details($blog_id);
$site_url = get_site_url($blog_id);
$class = 'blog-info';
$post_blog = "<span class='{$class}'>
<a href='{$site_url}'>{$blog_details->blogname}</a>
</span>";
$title_short = substr($post->title, 0, $tmp_title_characters);
if ($tmp_title_link == 'no') {
$html .= $title_short;
} else {
$html .= '<a href="{$post_url}" >${title_short}</a>';
}
$post_blog = apply_filters(
'recent_network_posts_post_blog',
$post_blog,
$blog_id
);
if ($show_blog) {
$blog_details = get_blog_details($blog_id);
$site_url = get_site_url($blog_id);
$class = 'blog-info';
$post_blog = '<span class="{$class}">
<a href="{$site_url}">{$blog_details->blogname}</a>
</span>';
$html .= ' (<a href="' . $site_url . '">'. $blog_details->blogname .'</a>)';
}
$html .= '</div>';
}
$post_blog = apply_filters('recent_network_posts_post_blog',
$post_blog,
$blog_id);
$html .= '<div class="post-content">' . $post->content . '</div>';
$html .= '</div>';
$html .= ' (<a href="{$site_url}">{$blog_details->blogname}</a>)';
}
}
$html .= '</div>';
}
$html .= '<div class="global-activity-pagination">';
$prev_link = ai_activity_get_previous_page_link();
if ($prev_link) {
$html .= '<div class="nav-previous align-left"><a href="' . $prev_link . '">Newer</a></div>';
}
$next_link = ai_activity_get_next_page_link();
if ($next_link) {
$html .= '<div class="nav-next align-right"><a href="' . $next_link . '">Older</a></div>';
}
$html .= '</div>';
if ($output) {
echo $html;
} else {
return $html;
$html .= '<div class="post-content">{$post->content}</div>';
$html .= '</div>';
}
}
$html .= '<div class="global-activity-pagination">';
$prev_link = ai_activity_get_previous_page_link();
if ($prev_link) {
$html .= '<div class="nav-previous align-left"><a href="{$prev_link}">Newer</a></div>';
}
$next_link = ai_activity_get_next_page_link();
if ($next_link) {
$html .= '<div class="nav-next align-right"><a href="{$next_link}">Older</a></div>';
}
$html .= '</div>';
if ($output) {
echo $html;
} else {
return $html;
}
}
function ai_activity_display_recent_posts_shortcode($attrs) {
extract(shortcode_atts(array(
'number' => 50,
'title_characters' => 250,
'content_characters' => 0,
'title_link' => 'yes',
'show_avatars' => 'no',
'avatar_size' => 16,
'posttype' => 'post',
'read_more' => '',
'read_more_link' => false,
'show_blog' => false
), $attrs));
return ai_activity_display_recent_posts($number, $title_characters, $content_characters, $title_link, $show_avatars, $avatar_size, $posttype, $read_more, $read_more_link, $show_blog, false);
extract(shortcode_atts(array(
'number' => 50,
'title_characters' => 250,
'content_characters' => 0,
'title_link' => 'yes',
'show_avatars' => 'no',
'avatar_size' => 16,
'posttype' => 'post',
'read_more' => '',
'read_more_link' => false,
'show_blog' => false
), $attrs));
return ai_activity_display_recent_posts($number, $title_characters, $content_characters, $title_link, $show_avatars, $avatar_size, $posttype, $read_more, $read_more_link, $show_blog, false);
}
add_action('publish_post', 'ai_activity_publish_post');
......