forked from netmix/radio-station
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradio-station.php
296 lines (251 loc) · 10.1 KB
/
radio-station.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
* @package Radio Station
* @version 2.2.8
*/
/*
Plugin Name: Radio Station
Plugin URI: https://netmix.com/radio-station
Description: Adds Show pages, DJ role, playlist and on-air programming functionality to your site.
Author: Tony Zeoli <[email protected]>
Version: 2.2.8
Text Domain: radio-station
Domain Path: /languages
Author URI: https://netmix.com/radio-station
GitHub Plugin URI: netmix/radio-station
Copyright 2019 Digital Strategy Works (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// === Setup ===
// - Include Plugin Files
// - Load Plugin Text Domain
// - Flush Rewrite Rules
// - Enqueue Stylesheets
// === Template Filters ===
// - Single Templates Loader
// - Archive Templates Loader
// === User Roles ===
// - Set DJ Role and Capabilities
// - maybe Revoke Edit Show Capability
// 2.2.7: moved all admin functions to radio-station-admin.php
// -------------
// === Setup ===
// -------------
define( 'RADIO_STATION_DIR', dirname( __FILE__ ) );
// --------------------
// Include Plugin Files
// --------------------
require RADIO_STATION_DIR . '/includes/post-types.php';
require RADIO_STATION_DIR . '/includes/master-schedule.php';
require RADIO_STATION_DIR . '/includes/shortcodes.php';
require RADIO_STATION_DIR . '/includes/support-functions.php';
require RADIO_STATION_DIR . '/includes/class-dj-upcoming-widget.php';
require RADIO_STATION_DIR . '/includes/class-dj-widget.php';
require RADIO_STATION_DIR . '/includes/class-playlist-widget.php';
// 2.2.7: added conditional load of admin includes
if ( is_admin() ) {
require RADIO_STATION_DIR . '/radio-station-admin.php';
require RADIO_STATION_DIR . '/includes/post-types-admin.php';
}
// -----------------------
// Load Plugin Text Domain
// -----------------------
function radio_station_init() {
load_plugin_textdomain( 'radio-station', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'plugins_loaded', 'radio_station_init' );
// -------------------
// Flush Rewrite Rules
// -------------------
// (on plugin activation / deactivation)
// 2.2.3: added this for custom post types rewrite flushing
// 2.2.8: fix for mismatched flag function name
register_activation_hook( __FILE__, 'radio_station_flush_rewrite_flag' );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
function radio_station_flush_rewrite_flag() {
add_option( 'radio_station_flush_rewrite_rules', true );
}
// --------------------------
// Enqueue Plugin Stylesheets
// --------------------------
function radio_station_enqueue_styles() {
$program_css = get_stylesheet_directory() . '/program-schedule.css';
if ( file_exists( $program_css ) ) {
$version = filemtime( $program_css );
$url = get_stylesheet_directory_uri() . '/program-schedule.css';
} else {
$version = filemtime( RADIO_STATION_DIR . '/css/program-schedule.css' );
$url = plugins_url( 'css/program-schedule.css', __FILE__ );
}
wp_enqueue_style( 'program-schedule', $url, array(), $version );
// note: widgets.css style enqueueing moved to within widgets
}
add_action( 'wp_enqueue_scripts', 'radio_station_enqueue_styles' );
// ------------------------
// === Template Filters ===
// ------------------------
// -----------------------
// Single Templates Loader
// -----------------------
function radio_station_load_template( $single_template ) {
global $post;
if ( 'playlist' === $post->post_type ) {
// first check to see if there's a template in the active theme's directory
$user_theme = get_stylesheet_directory() . '/single-playlist.php';
if ( ! file_exists( $user_theme ) ) {
$single_template = RADIO_STATION_DIR . '/templates/single-playlist.php';
}
}
if ( 'show' === $post->post_type ) {
// first check to see if there's a template in the active theme's directory
$user_theme = get_stylesheet_directory() . '/single-show.php';
if ( ! file_exists( $user_theme ) ) {
$single_template = RADIO_STATION_DIR . '/templates/single-show.php';
}
}
return $single_template;
}
add_filter( 'single_template', 'radio_station_load_template' );
// ------------------------
// Archive Templates Loader
// ------------------------
function radio_station_load_custom_post_type_template( $archive_template ) {
global $post;
if ( is_post_type_archive( 'playlist' ) ) {
$playlist_archive_theme = get_stylesheet_directory() . '/archive-playlist.php';
if ( ! file_exists( $playlist_archive_theme ) ) {
$archive_template = RADIO_STATION_DIR . '/templates/archive-playlist.php';
}
}
return $archive_template;
}
add_filter( 'archive_template', 'radio_station_load_custom_post_type_template' );
// ----------------------
// DJ Author Template Fix
// ----------------------
// 2.2.8: fix to not 404 author pages for DJs without blog posts
// Ref: https://wordpress.org/plugins/show-authors-without-posts/
function radio_station_fix_djs_without_posts( $template ) {
global $wp_query;
if ( ! is_author() && get_query_var( 'author' ) && ( 0 == $wp_query->posts->post ) ) {
// --- check if author has DJ (or administrator) role ---
$author = get_query_var( 'author_name' ) ? get_user_by( 'slug', get_query_var( 'author_name' ) ) : get_userdata( get_query_var( 'author' ) );
if ( in_array( 'dj', $author->roles ) || in_array( 'administrator', $author->roles ) ) {
return get_author_template();
}
}
return $template;
}
add_filter( '404_template', 'radio_station_fix_djs_without_posts' );
// ------------------
// === User Roles ===
// ------------------
// ----------------------------
// Set DJ Role and Capabilities
// ----------------------------
function radio_station_set_roles() {
global $wp_roles;
// --- set only necessary capabilities for DJs ---
$caps = array(
'edit_shows' => true,
'edit_published_shows' => true,
'edit_others_shows' => true,
'read_shows' => true,
'edit_playlists' => true,
'edit_published_playlists' => true,
// 'edit_others_playlists' => true, // uncomment to allow DJs to edit all playlists
'read_playlists' => true,
'publish_playlists' => true,
'read' => true,
'upload_files' => true,
'edit_posts' => true,
'edit_published_posts' => true,
'publish_posts' => true,
'delete_posts' => true,
);
// $wp_roles->remove_role('dj'); // we need this here in case we ever update the capabilities list
// --- add the role ---
// TODO: maybe translate role name ?
$wp_roles->add_role( 'dj', 'DJ', $caps );
// --- grant all new capabilities to admin users ---
$wp_roles->add_cap( 'administrator', 'edit_shows', true );
$wp_roles->add_cap( 'administrator', 'edit_published_shows', true );
$wp_roles->add_cap( 'administrator', 'edit_others_shows', true );
$wp_roles->add_cap( 'administrator', 'edit_private_shows', true );
$wp_roles->add_cap( 'administrator', 'delete_shows', true );
$wp_roles->add_cap( 'administrator', 'delete_published_shows', true );
$wp_roles->add_cap( 'administrator', 'delete_others_shows', true );
$wp_roles->add_cap( 'administrator', 'delete_private_shows', true );
$wp_roles->add_cap( 'administrator', 'read_shows', true );
$wp_roles->add_cap( 'administrator', 'publish_shows', true );
$wp_roles->add_cap( 'administrator', 'edit_playlists', true );
$wp_roles->add_cap( 'administrator', 'edit_published_playlists', true );
$wp_roles->add_cap( 'administrator', 'edit_others_playlists', true );
$wp_roles->add_cap( 'administrator', 'edit_private_playlists', true );
$wp_roles->add_cap( 'administrator', 'delete_playlists', true );
$wp_roles->add_cap( 'administrator', 'delete_published_playlists', true );
$wp_roles->add_cap( 'administrator', 'delete_others_playlists', true );
$wp_roles->add_cap( 'administrator', 'delete_private_playlists', true );
$wp_roles->add_cap( 'administrator', 'read_playlists', true );
$wp_roles->add_cap( 'administrator', 'publish_playlists', true );
}
if ( is_multisite() ) {
add_action( 'init', 'radio_station_set_roles', 10, 0 );
} else {
add_action( 'admin_init', 'radio_station_set_roles', 10, 0 );
}
// ---------------------------------
// maybe Revoke Edit Show Capability
// ---------------------------------
// (revoke ability to edit show if user is not assigned as a DJ to it)
function radio_station_revoke_show_edit_cap( $allcaps, $cap = 'edit_shows', $args ) {
global $post, $wp_roles;
$user = wp_get_current_user();
// --- get roles with publish shows capability ---
$add_roles = array( 'administrator' );
if ( isset( $wp_roles->roles ) && is_array( $wp_roles->roles ) ) {
foreach ( $wp_roles->roles as $name => $role ) {
foreach ( $role['capabilities'] as $capname => $capstatus ) {
if ( 'publish_shows' === $capname && (bool) $capstatus ) {
$add_roles[] = $name;
}
}
}
}
// --- check if current user has any of these roles ---
$found = false;
foreach ( $add_roles as $role ) {
// 2.2.8: remove strict in_array checking
if ( in_array( $role, $user->roles ) ) {
$found = true;
}
}
if ( ! $found ) {
// --- limit this to published shows ---
if ( isset( $post->post_type ) ) {
if ( is_admin() && ( 'show' === $post->post_type ) && ( 'publish' === $post->post_status ) ) {
$djs = get_post_meta( $post->ID, 'show_user_list', true );
if ( ! isset( $djs ) || empty( $djs ) ) {
$djs = array();
}
// ---- revoke editing capability if not assigned to this show ---
// 2.2.8: remove strict in_array checking
if ( ! in_array( $user->ID, $djs ) ) {
$allcaps['edit_shows'] = false;
$allcaps['edit_published_shows'] = false;
}
}
}
}
return $allcaps;
}
add_filter( 'user_has_cap', 'radio_station_revoke_show_edit_cap', 10, 3 );