-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog-categories.php
120 lines (96 loc) · 3.63 KB
/
blog-categories.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Plugin Name: Blog Categories
* Plugin URI: https://github.com/menmo/wp-blog-categories
* Description: A plugin for categorizing blogs.
* Version: 1.0
* Author: Menmo AB
* Author URI: http://www.menmo.se
* License: GPL2
*/
require_once(plugin_dir_path(__FILE__) . 'db/blog-cats.php');
require_once(plugin_dir_path(__FILE__) . 'db/blog-cat-relationships.php');
require_once(plugin_dir_path(__FILE__) . 'blog-cat-list.php');
define("br", "<br />");
class Blog_Categories_Plugin {
function __construct() {
defined('ABSPATH') or die();
register_activation_hook( __FILE__, array($this, 'activate') );
add_action('network_admin_menu', array($this, 'add_menu') );
//add_action('wpmu_new_blog', array($this, 'new_blog') );
add_action('admin_menu', array($this, 'add_options_page') );
}
public function activate(){
$this->die_if_not_superadmin();
Blog_Cats_DB::create_table();
Blog_Cat_Relationships_DB::create_table();
}
public function add_menu() {
$page_title = __('Categories');
$menu_title = __('Categories');
$capability = 'manage_sites';
$menu_slug = 'blog-cats';
$function = array($this, 'blog_cats_page');
add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, 'dashicons-category' );
}
public function blog_cats_page() {
$this->die_if_not_superadmin();
include(plugin_dir_path(__FILE__) . 'pages/manage-blog-cats.php');
}
function new_blog() {
exit( wp_redirect( admin_url( 'network/index.php?page=blog-cats' ) ) );
}
function add_options_page() {
$page_title = __('Blog Categories');
$menu_title = __('Categories');
$capability = 'manage_sites';
$menu_slug = 'blog-cats-select';
$function = array($this, 'manage_blog_cats_page');
add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function);
}
public function manage_blog_cats_page() {
$this->die_if_not_superadmin();
include(plugin_dir_path(__FILE__) . 'pages/manage-blog.php');
}
function die_if_not_superadmin() {
if( !is_super_admin() ) {
wp_die( __('You do not have permission to access this page.') );
}
}
}
new Blog_Categories_Plugin();
// Public methods
function blog_categories_get_categories($args = array()) {
return Blog_Cats_DB::get_list($args);
}
function blog_categories_get_latest_posts($cat_ID, $avatar_size = 48) {
$blogs = Blog_Cat_Relationships_DB::get_blog_list($cat_ID);
$result = array();
foreach($blogs as $blog) {
global $wpdb;
$details = get_blog_details($blog);
if($details && $details->archived == 0 && $details->deleted == 0) {
switch_to_blog($blog);
$posts = get_posts(array(
'posts_per_page' => 1
));
if(!empty($posts)) {
$avatar_id = get_user_meta($posts[0]->post_author, $wpdb->get_blog_prefix().'user_avatar', true);
$result[] = array(
'blog' => $details,
'post' => $posts[0],
'permalink' => get_permalink($posts[0]->ID),
'home_url' => esc_url( home_url( '/' ) ),
'avatar' => wp_get_attachment_image_src( $avatar_id, $avatar_size ),
'sponsored_logo' => of_get_option('sponsor-logo'),
);
}
restore_current_blog();
}
};
usort($result, function($a, $b) {
return strcmp($b['post']->post_date, $a['post']->post_date);
});
return $result;
}
?>