forked from gordielachance/wp-soundsystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpsstm-core-artists.php
289 lines (224 loc) · 10.7 KB
/
wpsstm-core-artists.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
<?php
class WP_SoundSystem_Core_Artists{
public $artist_metakey = '_wpsstm_artist';
public $qvar_artist_lookup = 'lookup_artist';
public $artist_mbtype = 'artist'; //musicbrainz type, for lookups
/**
* @var The one true Instance
*/
private static $instance;
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new WP_SoundSystem_Core_Artists;
self::$instance->init();
}
return self::$instance;
}
private function __construct() { /* Do nothing here */ }
function init(){
//add_action( 'wpsstm_loaded',array($this,'setup_globals') );
add_action( 'wpsstm_loaded',array($this,'setup_actions') );
}
function setup_actions(){
add_action( 'init', array($this,'register_post_type_artist' ));
add_filter( 'query_vars', array($this,'add_query_var_artist') );
add_filter( 'pre_get_posts', array($this,'pre_get_posts_by_artist') );
add_action( 'save_post', array($this,'update_title_artist'), 99);
add_action( 'add_meta_boxes', array($this, 'metabox_artist_register'));
add_action( 'save_post', array($this,'metabox_artist_save'), 5);
//add_filter( 'manage_posts_columns', array($this,'column_artist_register'), 10, 2 );
//add_action( 'manage_posts_custom_column' , array($this,'column_artist_content'), 10, 2 );
}
function column_artist_register($defaults) {
$post_types = array(
wpsstm()->post_type_artist,
wpsstm()->post_type_track,
wpsstm()->post_type_album
);
$before = array();
$after = array();
if ( isset($_GET['post_type']) && in_array($_GET['post_type'],$post_types) ){
$after['artist'] = __('Artist','wpsstm');
}
return array_merge($before,$defaults,$after);
}
function column_artist_content($column,$post_id){
global $post;
switch ( $column ) {
case 'artist':
if (!$artist = wpsstm_get_post_artist($post_id) ){
$artist = '—';
}
echo $artist;
break;
}
}
function pre_get_posts_by_artist( $query ) {
if ( $search = $query->get( $this->qvar_artist_lookup ) ){
$query->set( 'meta_query', array(
array(
'key' => $this->artist_metakey,
'value' => $search,
'compare' => '='
)
));
}
return $query;
}
/**
Update the post title to match the artist/album/track, so we still have a nice post permalink
**/
function update_title_artist( $post_id ) {
//only for albums
if (get_post_type($post_id) != wpsstm()->post_type_artist) return;
//check capabilities
$is_autosave = ( ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) || wp_is_post_autosave($post_id) );
$is_autodraft = ( get_post_status( $post_id ) == 'auto-draft' );
$is_revision = wp_is_post_revision( $post_id );
$has_cap = current_user_can('edit_post', $post_id);
if ( $is_autosave || $is_autodraft || $is_revision || !$has_cap ) return;
$artist = $post_title = wpsstm_get_post_artist($post_id);
if ( !$artist ) return;
//no changes - use get_post_field here instead of get_the_title() so title is not filtered
if ( $post_title == get_post_field('post_title',$post_id) ) return;
//log
wpsstm()->debug_log(array('post_id'=>$post_id,'title'=>$post_title),"update_title_artist()");
$args = array(
'ID' => $post_id,
'post_title' => $post_title
);
remove_action( 'save_post',array($this,'update_title_artist'), 99 ); //avoid infinite loop - ! hook priorities
wp_update_post( $args );
add_action( 'save_post',array($this,'update_title_artist'), 99 );
}
function register_post_type_artist() {
$labels = array(
'name' => _x( 'Artists', 'Artists General Name', 'wpsstm' ),
'singular_name' => _x( 'Artist', 'Artist Singular Name', 'wpsstm' ),
'menu_name' => __( 'Artists', 'wpsstm' ),
'name_admin_bar' => __( 'Artist', 'wpsstm' ),
'archives' => __( 'Artist Archives', 'wpsstm' ),
'attributes' => __( 'Artist Attributes', 'wpsstm' ),
'parent_item_colon' => __( 'Parent Artist:', 'wpsstm' ),
'all_items' => __( 'All Artists', 'wpsstm' ),
'add_new_item' => __( 'Add New Artist', 'wpsstm' ),
//'add_new' => __( 'Add New', 'wpsstm' ),
'new_item' => __( 'New Artist', 'wpsstm' ),
'edit_item' => __( 'Edit Artist', 'wpsstm' ),
'update_item' => __( 'Update Artist', 'wpsstm' ),
'view_item' => __( 'View Artist', 'wpsstm' ),
'view_items' => __( 'View Artists', 'wpsstm' ),
'search_items' => __( 'Search Artists', 'wpsstm' ),
//'not_found' => __( 'Not found', 'wpsstm' ),
//'not_found_in_trash' => __( 'Not found in Trash', 'wpsstm' ),
//'featured_image' => __( 'Featured Image', 'wpsstm' ),
//'set_featured_image' => __( 'Set featured image', 'wpsstm' ),
//'remove_featured_image' => __( 'Remove featured image', 'wpsstm' ),
//'use_featured_image' => __( 'Use as featured image', 'wpsstm' ),
'insert_into_item' => __( 'Insert into artist', 'wpsstm' ),
'uploaded_to_this_item' => __( 'Uploaded to this artist', 'wpsstm' ),
'items_list' => __( 'Artists list', 'wpsstm' ),
'items_list_navigation' => __( 'Artists list navigation', 'wpsstm' ),
'filter_items_list' => __( 'Filter artists list', 'wpsstm' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'supports' => array( 'author','title','thumbnail', 'comments' ),
'taxonomies' => array( 'post_tag' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
//http://justintadlock.com/archives/2013/09/13/register-post-type-cheat-sheet
'capability_type' => 'post', //artist
//'map_meta_cap' => true,
/*
'capabilities' => array(
// meta caps (don't assign these to roles)
'edit_post' => 'edit_artist',
'read_post' => 'read_artist',
'delete_post' => 'delete_artist',
// primitive/meta caps
'create_posts' => 'create_artists',
// primitive caps used outside of map_meta_cap()
'edit_posts' => 'edit_artists',
'edit_others_posts' => 'manage_artists',
'publish_posts' => 'manage_artists',
'read_private_posts' => 'read',
// primitive caps used inside of map_meta_cap()
'read' => 'read',
'delete_posts' => 'manage_artists',
'delete_private_posts' => 'manage_artists',
'delete_published_posts' => 'manage_artists',
'delete_others_posts' => 'manage_artists',
'edit_private_posts' => 'edit_artists',
'edit_published_posts' => 'edit_artists'
),
*/
);
register_post_type( wpsstm()->post_type_artist, $args );
}
function add_query_var_artist( $qvars ) {
$qvars[] = $this->qvar_artist_lookup;
return $qvars;
}
function metabox_artist_register(){
$metabox_post_types = array(
wpsstm()->post_type_artist,
wpsstm()->post_type_track,
wpsstm()->post_type_album
);
add_meta_box(
'wpsstm-artist',
__('Artist','wpsstm'),
array($this,'metabox_artist_content'),
$metabox_post_types,
'after_title',
'high'
);
}
function metabox_artist_content( $post ){
$artist_name = get_post_meta( $post->ID, $this->artist_metakey, true );
?>
<input type="text" name="wpsstm_artist" class="wpsstm-fullwidth wpsstm-lookup-artist" value="<?php echo $artist_name;?>" placeholder="<?php printf("Enter artist here",'wpsstm');?>"/>
<?php
wp_nonce_field( 'wpsstm_artist_meta_box', 'wpsstm_artist_meta_box_nonce' );
}
/**
Save artist field for this post
**/
function metabox_artist_save( $post_id ) {
//check save status
$is_autosave = ( ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) || wp_is_post_autosave($post_id) );
$is_autodraft = ( get_post_status( $post_id ) == 'auto-draft' );
$is_revision = wp_is_post_revision( $post_id );
$is_metabox = isset($_POST['wpsstm_artist_meta_box_nonce']);
if ( !$is_metabox || $is_autodraft || $is_autosave || $is_revision ) return;
//check post type
$post_type = get_post_type($post_id);
$allowed_post_types = array(wpsstm()->post_type_artist,wpsstm()->post_type_album,wpsstm()->post_type_track);
if ( !in_array($post_type,$allowed_post_types) ) return;
$is_valid_nonce = ( wp_verify_nonce( $_POST['wpsstm_artist_meta_box_nonce'], 'wpsstm_artist_meta_box' ) );
if ( !$is_valid_nonce ) return;
//this should run only once (for the main post); so unset meta box nonce.
//without this the function would be called for every subtrack if there was some.
unset($_POST['wpsstm_artist_meta_box_nonce']);
$artist = ( isset($_POST[ 'wpsstm_artist' ]) ) ? $_POST[ 'wpsstm_artist' ] : null;
if (!$artist){
delete_post_meta( $post_id, $this->artist_metakey );
}else{
update_post_meta( $post_id, $this->artist_metakey, $artist );
}
}
}
function wpsstm_artists() {
return WP_SoundSystem_Core_Artists::instance();
}
wpsstm_artists();