-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclass-plugin.php
More file actions
156 lines (135 loc) · 4.39 KB
/
class-plugin.php
File metadata and controls
156 lines (135 loc) · 4.39 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
class MPT_Plugin {
/**
* Callback when plugin is actived
* TODO: Create first member ?
*
* @access public
* @static
*
* @return void.
*/
public static function activate() {
// Add role "Members Manager"
add_role( 'members-manager', __('Members Manager', 'mpt') );
// Add capabilities for administrator and editor default roles
foreach( array('administrator', 'editor', 'members-manager') as $role_name ) {
$role = get_role( $role_name );
if ( $role != NULL ) {
foreach( self::get_capabilities() as $capability ) {
$role->add_cap( $capability );
}
}
unset($role);
}
}
/**
* Callback when plugin is deactived
*
* @access public
* @static
*
* @return void.
*/
public static function deactivate() {
remove_role( 'members-manager' );
}
/**
* Get array with plugin capabilities for User/Role WP API, for post type, taxonomy or both.
*
* @param string $target allow to choose type of capabilities.
*
* @access public
* @static
*
* @return array capabilities.
*/
public static function get_capabilities( $target = 'both ') {
$capabilities = array();
// Custom post type
$capabilities['post_type'] = array(
// Meta capabilities
'edit_post' => 'edit_' . 'member',
'read_post' => 'read_' . 'member',
'delete_post' => 'delete_' . 'member',
// Primitive capabilities used outside of map_meta_cap():
'edit_posts' => 'edit_' . 'members',
'edit_others_posts' => 'edit_others_' . 'members',
'publish_posts' => 'publish_' . 'members',
'read_private_posts' => 'read_private_' . 'members',
// Primitive capabilities used within map_meta_cap():
'read' => 'read',
'delete_posts' => 'delete_' . 'members',
'delete_private_posts' => 'delete_private_' . 'members',
'delete_published_posts' => 'delete_published_' . 'members',
'delete_others_posts' => 'delete_others_' . 'members',
'edit_private_posts' => 'edit_private_' . 'members',
'edit_published_posts' => 'edit_published_' . 'members',
);
// Taxonomy
$capabilities['taxonomy'] = array(
'manage_terms' => 'manage_' . 'members_roles',
'edit_terms' => 'edit_' . 'members_roles',
'delete_terms' => 'delete_' . 'members_roles',
'assign_terms' => 'assign_' . 'members_roles',
);
if ( in_array($target, array('post_type', 'taxonomy') ) ) {
return $capabilities[$target];
}
return array_merge($capabilities['post_type'], $capabilities['taxonomy']);
}
public static function _get_roles( ) {
$roles = array( );
// Add no default role
$roles['none'] = __( 'No default role', 'mpt' );
// Add registered roles
$terms = MPT_Roles::get_roles( );
if(empty($terms)){
return $roles;
}
foreach( $terms as $term ) {
$roles[$term->slug] = $term->name;
}
return $roles;
}
/**
* Get all the pages
*
* @return array page names with key value pairs
*/
public static function _get_pages( ) {
global $wpdb;
$found = false;
$pages_options = wp_cache_get('_get_pages', 'members-post-type', false, $found);
if( $found == false ) {
// Fix performances issues, use directly SQL
$pages = $wpdb->get_results( "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' ORDER BY post_title ASC" );
$pages_options = array( 0 => __( 'Select a page', 'mpt' ) );
if( !empty($pages) ) {
foreach( $pages as $page ) {
$pages_options[$page->ID] = $page->post_title;
}
}
wp_cache_set('_get_pages', $pages_options, 'members-post-type');
}
return $pages_options;
}
/**
* Returns all the settings section
*
* @return array
*/
public static function get_default_settings_sections( ) {
return apply_filters( 'mpt_default_options_sections', (array) include( MPT_DIR . 'classes/helpers/default-sections.php' ) );
}
/**
* Returns all the settings fields
*
* @return array settings fields
*/
public static function get_default_settings_fields( ) {
// Get pages for build settings with list page
$all_pages = MPT_Plugin::_get_pages( );
return apply_filters( 'mpt_default_options_settings' , (array) include( MPT_DIR . 'classes/helpers/default-settings.php' ) );
}
}