gasap-posttypes.php 20 KB
Newer Older
agnez's avatar
agnez committed
1 2 3 4 5 6 7 8
<?php
/**
 * @package gasap_posttypes
 * @version 1.0
 */
/*
Plugin Name: GASAP post types
Description: Plugin for the GASAP network, custom post types
agnez's avatar
agnez committed
9
Author: Lore Mipsum
agnez's avatar
agnez committed
10 11 12 13 14 15 16 17 18
Text Domain: gasap-posttypes
Domain Path: /languages
Version: 1.0
Author URI: http://mipsum.be/
*/ 
defined( 'ABSPATH' ) or die();

add_action( 'plugins_loaded', 'i18n' );
function i18n() {
agnez's avatar
agnez committed
19
	load_plugin_textdomain( 'gasap-posttypes', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
agnez's avatar
agnez committed
20 21 22 23
}

add_action( 'init', 'create_post_types' );
function create_post_types() {
24
	register_post_type('gasap', // GASAP
agnez's avatar
agnez committed
25
		array(
agnez's avatar
agnez committed
26 27 28 29 30 31 32 33 34 35 36 37 38
			'labels' => array(
					'name'				 	=> _x( 'GASAP', 'post type general name', 'gasap-posttypes' ),
					'singular_name'		 	=> _x( 'GASAP', 'post type singular name', 'gasap-posttypes' ),
					'add_new'				=> _x( 'Add New', 'GASAP', 'gasap-posttypes' ),
					'add_new_item' 			=> __( 'Add New GASAP', 'gasap-posttypes' ),
					'edit_item' 			=> __( 'Edit GASAP', 'gasap-posttypes' ),
					'new_item' 				=> __( 'New GASAP', 'gasap-posttypes' ),
					'view' 					=> __( 'View GASAP', 'gasap-posttypes' ),
					'view_item' 			=> __( 'View GASAP', 'gasap-posttypes' ),
					'search_items' 			=> __( 'Search GASAP', 'gasap-posttypes' ),
					'not_found' 			=> __( 'No GASAP found', 'gasap-posttypes' ),
					'not_found_in_trash' 	=> __( 'No GASAP found in Trash', 'gasap-posttypes' ),
			 ),
39 40
			'public' 					=> true,
			'has_archive' 				=> false,		
agnez's avatar
agnez committed
41
			'show_ui' 					=> true,
42
			'exclude_from_search' 		=> true,
agnez's avatar
agnez committed
43
			'hierarchical' 				=> false,
agnez's avatar
agnez committed
44
			'supports' 					=> array( 'title', 'editor', 'excerpt', 'author' ),
45
			'capability_type' 			=> 'post',
agnez's avatar
agnez committed
46
			'query_var' 				=> true, 
47
			'menu_position' 			=> 7,
agnez's avatar
agnez committed
48
		)
49 50
	);	
	register_post_type('producer', // Producers
agnez's avatar
agnez committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
		array(
		 'labels' => array(
				'name'				 	=> _x( 'Producers', 'post type general name', 'gasap-posttypes' ),
				'singular_name'		 	=> _x( 'Producer', 'post type singular name', 'gasap-posttypes' ),
				'add_new'				=> _x( 'Add New', 'Producer', 'gasap-posttypes' ),
				'add_new_item' 			=> __( 'Add New Producer', 'gasap-posttypes' ),
				'edit_item' 			=> __( 'Edit Producer', 'gasap-posttypes' ),
				'new_item' 				=> __( 'New Producer', 'gasap-posttypes' ),
				'search_items' 			=> __( 'Search Producers', 'gasap-posttypes' ),
				'not_found' 			=> __( 'No Producers found', 'gasap-posttypes' ),
				'not_found_in_trash' 	=> __( 'No Producers found in Trash', 'gasap-posttypes' ),
		 ),
			'public' 					=> false,
			'has_archive' 				=> false,		
			'show_ui' 					=> true,
			'exclude_from_search' 		=> true,
			'hierarchical' 				=> false,
			'supports' 					=> array( 'title', 'editor', 'thumbnail', 'excerpt' ),
agnez's avatar
agnez committed
69
			'capability_type' 			=> 'page',
agnez's avatar
agnez committed
70 71 72 73
			'query_var' 				=> true, 
			'menu_position' 			=> 8,
		)
	);
74
	register_post_type('worker', // employés
agnez's avatar
agnez committed
75 76
		array(
		 'labels' => array(
77 78 79 80 81 82 83 84 85
				'name'				 	=> _x( 'Workers', 'post type general name', 'gasap-posttypes' ),
				'singular_name'		 	=> _x( 'Worker', 'post type singular name', 'gasap-posttypes' ),
				'add_new'				=> _x( 'Add New', 'Worker', 'gasap-posttypes' ),
				'add_new_item' 			=> __( 'Add New Worker', 'gasap-posttypes' ),
				'edit_item' 			=> __( 'Edit Worker', 'gasap-posttypes' ),
				'new_item' 				=> __( 'New Worker', 'gasap-posttypes' ),
				'search_items' 			=> __( 'Search Worker', 'gasap-posttypes' ),
				'not_found' 			=> __( 'No Worker found', 'gasap-posttypes' ),
				'not_found_in_trash' 	=> __( 'No Worker found in Trash', 'gasap-posttypes' ),
agnez's avatar
agnez committed
86 87 88 89 90 91 92
		 ),
			'public' 					=> false,
			'has_archive' 				=> false,		
			'show_ui' 					=> true,
			'exclude_from_search' 		=> true,
			'hierarchical' 				=> false,
			'supports' 					=> array( 'title', 'editor', 'thumbnail', 'excerpt' ),
agnez's avatar
agnez committed
93
			'capability_type' 			=> 'page',
agnez's avatar
agnez committed
94
			'query_var' 				=> true, 
95
			'menu_position' 			=> 11,
agnez's avatar
agnez committed
96
		)
97
	); 
agnez's avatar
agnez committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	register_post_type('partner', // partenaires
		array(
		 'labels' => array(
				'name'				 	=> _x( 'Partners', 'post type general name', 'gasap-posttypes' ),
				'singular_name'		 	=> _x( 'Partner', 'post type singular name', 'gasap-posttypes' ),
				'add_new'				=> _x( 'Add New', 'Partner', 'gasap-posttypes' ),
				'add_new_item' 			=> __( 'Add New Partner', 'gasap-posttypes' ),
				'edit_item' 			=> __( 'Edit Partner', 'gasap-posttypes' ),
				'new_item' 				=> __( 'New Partner', 'gasap-posttypes' ),
				'search_items' 			=> __( 'Search Partner', 'gasap-posttypes' ),
				'not_found' 			=> __( 'No Partner found', 'gasap-posttypes' ),
				'not_found_in_trash' 	=> __( 'No Partner found in Trash', 'gasap-posttypes' ),
		 ),
			'public' 					=> false,
			'has_archive' 				=> false,		
			'show_ui' 					=> true,
			'exclude_from_search' 		=> true,
			'hierarchical' 				=> false,
			'supports' 					=> array( 'title', 'thumbnail', ),
agnez's avatar
agnez committed
117
			'capability_type' 			=> 'page',
118
			'query_var' 				=> true, 
agnez's avatar
agnez committed
119 120 121
			'menu_position' 			=> 12,
		)
	);
122
 	register_post_type('contentpiece', //Content pieces
agnez's avatar
agnez committed
123 124
		array(
		 'labels' => array(
125 126 127 128 129 130 131 132 133
				'name'				 	=> _x( 'Content Pieces', 'post type general name', 'gasap-posttypes' ),
				'singular_name'		 	=> _x( 'Content Piece', 'post type singular name', 'gasap-posttypes' ),
				'add_new'				=> _x( 'Add New', 'Content Piece', 'gasap-posttypes' ),
				'add_new_item' 			=> __( 'Add New Content Piece', 'gasap-posttypes' ),
				'edit_item' 			=> __( 'Edit Content Piece', 'gasap-posttypes' ),
				'new_item' 				=> __( 'New Content Piece', 'gasap-posttypes' ),
				'search_items' 			=> __( 'Search Content Pieces', 'gasap-posttypes' ),
				'not_found' 			=> __( 'No Content Pieces found', 'gasap-posttypes' ),
				'not_found_in_trash' 	=> __( 'No Content Pieces found in Trash', 'gasap-posttypes' ),
agnez's avatar
agnez committed
134
		 ),
135
			'public' 					=> false,	
agnez's avatar
agnez committed
136 137
			'show_ui' 					=> true,
			'exclude_from_search' 		=> true,
138
			'has_archive' 				=> false,
agnez's avatar
agnez committed
139
			'hierarchical' 				=> false,
140 141 142 143
			'supports' 					=> array( 'title', 'editor', 'thumbnail' ),
			'capability_type' 			=> 'page',
			'query_var' 				=> true, 
			'menu_position' 			=> 13,
agnez's avatar
agnez committed
144
		)
145
	);			
agnez's avatar
agnez committed
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
}

/** 
	* TAXONOMY
	*/
// Functional tags for content-pieces
add_action( 'init', 'create_contentpiece_taxonomies',0 );
function create_contentpiece_taxonomies() {
		 $labels = array(
				'name'				 	=> _x( 'Contentpiece Types', 'taxonomy general name', 'gasap-posttypes' ),
				'singular_name'		 	=> _x( 'Contentpiece Type', 'taxonomy singular name', 'gasap-posttypes' ),
				'all_items'				=> __( 'All Types', 'gasap-posttypes' ),
				'parent_item'			=> null,
				'parent_item_colon'		=> null,					
				'search_items'			=> __( 'Search Types', 'gasap-posttypes' ),
				'edit_item'				=> __( 'Edit Type', 'gasap-posttypes' ),
				'update_item'			 => __( 'Update Type', 'gasap-posttypes' ),
				'add_new_item'			=> __( 'Add New Type', 'gasap-posttypes' ),
				'new_item_name'		 	=> __( 'New Type Name', 'gasap-posttypes' ),
				'menu_name'				=> __( 'Content-piece Types', 'gasap-posttypes' ),
			);
			$capabilities = array (
				'manage_terms' 		=> 'manage_options', //by default only admin
				'edit_terms' 		=> 'manage_options',
				'delete_terms' 		=> 'manage_options',
				'assign_terms' 		=> 'manage_options',
			);
			$args = array(
				'hierarchical'			=> false,
				'labels'			 	=> $labels,
				'show_ui'				=> true,
				'show_admin_column' 	=> true,
				'query_var'				=> true,
				'public' 				=> true, //needs to be public for the radio-button plugin to work
				'rewrite'				=> false,
				'capabilities'			=> $capabilities,
			);
	register_taxonomy( 'contentpiecetype', array( 'contentpiece' ), $args );
}

// Produceer type tags
add_action('init','add_type_taxonomy_to_producer');	
function add_type_taxonomy_to_producer(){
	$taxonomy = 'producertype';
	$object_type = 'producer';
	$labels = array(
		'name'						=> 'Producer Types',
		'singular_name'	 			=> 'Producer Type',
		'search_items'				=> 'Search Producer Types',
		'all_items'		 			=> 'All Producer Types',
		'parent_item'				=> 'Parent Type',
		'parent_item_colon'			=> 'Parent Type:',
		'update_item'				=> 'Update Type',
		'edit_item'		 			=> 'Edit Type',
		'add_new_item'				=> 'Add New Producer Type', 
		'new_item_name'	 			=> 'New Type Name',
		'menu_name'		 			=> 'Type',
	);
	$args = array(
		'labels'				=> $labels,
		'hierarchical'	 		=> false,
		'show_ui'				=> true,
		'show_in_nav_menus'		=> false,
		'public'				=> false,
		'show_admin_column' 	=> true,
		'query_var'				=> true,
		'rewrite'				=> true,
	);
	register_taxonomy($taxonomy, $object_type, $args); 
}

// GASAP status tags
add_action('init','add_status_taxonomy_to_gasap');	
function add_status_taxonomy_to_gasap(){
	$taxonomy = 'gasapstatus';
	$object_type = 'gasap';
	$labels = array(
		'name'					=> 'GASAP Statuses',
		'singular_name'	 		=> 'GASAP Status',
		'search_items'			=> 'Search GASAP Status',
		'all_items'		 		=> 'All GASAP Statuses',
		'parent_item'			=> null,
		'parent_item_colon'		=> null,
		'update_item'			=> 'Update Status',
		'edit_item'		 		=> 'Edit Status',
		'add_new_item'			=> 'Add New GASAP Status', 
		'new_item_name'	 		=> 'New GASAP Status',
		'menu_name'		 		=> 'GASAP Status',
	);
	$args = array(
		'labels'				=> $labels,
		'hierarchical'	 		=> false,
		'show_ui'				=> true,
		'show_in_nav_menus'		=> false,
		'public'				=> true, //needs to be public for the radio-button plugin to work
		'show_admin_column' 	=> true,
		'query_var'				=> true,
		'rewrite'				=> true,
	);
	register_taxonomy($taxonomy, $object_type, $args); 
}
// GASAP type tags
add_action('init','add_type_taxonomy_to_gasap');	
function add_type_taxonomy_to_gasap(){
	$taxonomy = 'gasaptype';
	$object_type = 'gasap';
	$labels = array(
		'name'					=> 'GASAP Types',
		'singular_name'	 		=> 'GASAP Type',
		'search_items'	 		=> 'Search GASAP Types',
		'all_items'		 		=> 'All GASAP Types',
		'parent_item'			=> null,
		'parent_item_colon'		=> null,		
		'update_item'			=> 'Update Type',
		'edit_item'		 		=> 'Edit Type',
		'add_new_item'			=> 'Add New GASAP Type', 
		'new_item_name'	 		=> 'New GASAP Type',
		'menu_name'		 		=> 'GASAP Type',
	);
	$args = array(
		'labels'				=> $labels,
		'hierarchical'	 		=> false,
		'show_ui'		 		=> true,
		'show_in_nav_menus'		=> false,
		'public'				=> true, //needs to be public for the radio-button plugin to work
		'show_admin_column' 	=> true,
		'query_var'				=> true,
		'rewrite'				=> true,
	);
	register_taxonomy($taxonomy, $object_type, $args); 
}
// default type for new posts http://stackoverflow.com/questions/29049543/set-default-taxonomy-term-for-custom-post-type
add_action( 'save_post_gasap', 'gasap_set_default_type_term' );
function gasap_set_default_type_term( $post_id ) {
		$current_post = get_post( $post_id );
		if ( $current_post->post_date == $current_post->post_modified ) {
		wp_set_object_terms( $post_id, 'gasap', 'gasaptype', true );
		}
}
// GASAP day tags
add_action('init','add_day_taxonomy_to_gasap');	
function add_day_taxonomy_to_gasap(){
	$taxonomy = 'gasapday';
	$object_type = 'gasap';
	$labels = array(
		'name'					=> 'GASAP days',
		'singular_name'	 		=> 'GASAP day',
		'search_items'	 		=> 'Search GASAP days',
		'all_items'		 		=> 'All GASAP days',
		'parent_item'			=> null,
		'parent_item_colon'		=> null,		
		'update_item'			=> 'Update day',
		'edit_item'		 		=> 'Edit day',
		'add_new_item'			=> 'Add New GASAP day', 
		'new_item_name'	 		=> 'New GASAP day',
		'menu_name'		 		=> 'GASAP day',
	);
	$args = array(
		'labels'				=> $labels,
		'hierarchical'	 		=> false,
		'show_ui'		 		=> true,
		'show_in_nav_menus'		=> false,
		'public'				=> true, //needs to be public for the radio-button plugin to work
		'show_admin_column' 	=> true,
		'query_var'				=> true,
		'rewrite'				=> true,
	);
	register_taxonomy($taxonomy, $object_type, $args); 
}
//hide "no term" https://wordpress.org/plugins/radio-buttons-for-taxonomies/faq/
add_filter( 'radio-buttons-for-taxonomies-no-term-gasapstatus', '__return_FALSE' );
add_filter( 'radio-buttons-for-taxonomies-no-term-gasaptype', '__return_FALSE' );
add_filter( 'radio-buttons-for-taxonomies-no-term-gasapday', '__return_FALSE' );

// ---rename Post to News http://revelationconcept.com/wordpress-rename-default-posts-news-something-else/
agnez's avatar
agnez committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
function gasap_change_post_label() {
	global $menu;
	global $submenu;
	$menu[5][0] = __( 'News', 'gasap-posttypes' );
	$submenu['edit.php'][5][0] = __( 'News', 'gasap-posttypes' );
	$submenu['edit.php'][10][0] = __( 'Add News', 'gasap-posttypes' );
	//$submenu['edit.php'][16][0] = __( 'News Tags', 'gasap-posttypes' );
}
function gasap_change_post_object() {
	global $wp_post_types;
	$labels = &$wp_post_types['post']->labels;
	$labels->name						= _x( 'News', 'post type general name', 'gasap-posttypes' );
	$labels->singular_name				= _x( 'News', 'post type singular name', 'gasap-posttypes' );
	$labels->add_new					= _x( 'Add New', 'News', 'gasap-posttypes' );
	$labels->add_new_item				= __( 'Add News', 'gasap-posttypes' );
	$labels->edit_item		 			= __( 'Edit News', 'gasap-posttypes' );
	$labels->new_item					= __( 'News', 'gasap-posttypes' );
	$labels->view_item		 			= __( 'View News', 'gasap-posttypes' );
	$labels->search_items				= __( 'Search News', 'gasap-posttypes' );
	$labels->not_found		 			= __( 'No News found', 'gasap-posttypes' );
	$labels->not_found_in_trash 		= __( 'No News found in Trash', 'gasap-posttypes' );
	$labels->all_items		 			= __( 'All', 'gasap-posttypes' );
	$labels->menu_name		 			= __( 'News', 'gasap-posttypes' );
	$labels->name_admin_bar				= __( 'News', 'gasap-posttypes' );
}
agnez's avatar
agnez committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
add_action( 'admin_menu', 'gasap_change_post_label' );
add_action( 'init', 'gasap_change_post_object' );

// taxonomy for attachments https://code.tutsplus.com/articles/applying-categories-tags-and-custom-taxonomies-to-media-attachments--wp-32319
add_action( 'init', 'gasap_add_usage_taxonomy' );
function gasap_add_usage_taxonomy() {
		$labels = array(
			'name'					=> _x( 'Usage Tags', 'taxonomy general name', 'gasap-posttypes' ),
			'singular_name'			=> _x( 'Usage Tag', 'taxonomy singular name', 'gasap-posttypes' ),		
			'search_items'			=> __( 'Search Usage Tags', 'gasap-posttypes' ),
			'all_items'				=> __( 'All Usage Tags', 'gasap-posttypes' ),
			'parent_item'			=> null,
			'parent_item_colon'		=> null,			
			'edit_item'				=> __( 'Edit Usage Tag', 'gasap-posttypes' ),
			'update_item'			=> __( 'Update Usage Tag', 'gasap-posttypes' ),
			'add_new_item'			=> __( 'Add New Usage Tag', 'gasap-posttypes' ),
			'new_item_name'			=> __( 'New Usage Tag Name', 'gasap-posttypes' ),
			'menu_name'				=> __( 'Usage Tag', 'gasap-posttypes' ),
		);
 
		$args = array(
			'labels'				=> $labels,
			'hierarchical' 			=> false,
			'query_var' 			=> true,
			'rewrite' 				=> true,
			'show_admin_column' 	=> true,
			'show_ui'		 		=> true,
			'show_in_nav_menus'		=> false,
			'public'				=> false,
		);
		register_taxonomy( 'media_usage', 'attachment', $args );
}

/* *** */

// ---reorder admin menu. default 10 = Media, 20 = Pages, 4 = Separator
	add_action('admin_menu', 'gasap_change_admin_menu');
	function gasap_change_admin_menu() {
		global $menu;
		$menu[6] = $menu[20];
		$menu[30] = $menu[10];
		unset($menu[4]);
		unset($menu[10]);
		unset($menu[20]);
agnez's avatar
agnez committed
390
		add_menu_page( __( 'Event', 'gasap-posttypes' ),'Event','page','edit.php?post_type=event','','',5 );
agnez's avatar
agnez committed
391 392 393 394 395 396 397 398 399 400
		if ( !current_user_can( 'manage_options' ) ){
			remove_menu_page( 'tools.php' );
			remove_submenu_page( 'edit.php?post_type=gasap', 'edit-tags.php?taxonomy=gasaptype&post_type=gasap' );
			remove_submenu_page( 'edit.php?post_type=gasap', 'edit-tags.php?taxonomy=gasapstatus&post_type=gasap' );
		}
	}

/** 
	* METABOXES
	*/
agnez's avatar
agnez committed
401 402 403 404

/** Add metabox URL-partenaire. */
add_action( 'add_meta_boxes', 'partenaire_url_add_metabox' );
function partenaire_url_add_metabox() {
agnez's avatar
agnez committed
405 406 407 408 409 410 411
	 add_meta_box(
		'url_partenaire',
		__( 'URL site', 'gasap-posttypes' ),
		'partenaire_url_metabox_callback',
		'partner',
		'normal',
		'high'
agnez's avatar
agnez committed
412
	);
agnez's avatar
agnez committed
413
}
agnez's avatar
agnez committed
414 415
/** Print the metabox content. */
function partenaire_url_metabox_callback( $post ) {
agnez's avatar
agnez committed
416
	 // Create a nonce field.
agnez's avatar
agnez committed
417 418 419
	wp_nonce_field( 'partenaire_url_metabox', 'partenaire_url_metabox_nonce' );
	// Retrieve a previously saved value, if available.
	$url = get_post_meta( $post->ID, '_partenaire_url', true );
agnez's avatar
agnez committed
420 421 422 423 424 425
	 // Create the metabox field mark-up.
	 ?>
		<p>
		 <label>URL site partenaire</label><input style="width: 20em;" type="text" name="partenaire_url" value="<?php echo esc_url( $url ); ?>" size="30" class="regular-text" />
		</p>
	 <?php
agnez's avatar
agnez committed
426
}
agnez's avatar
agnez committed
427 428 429
/** Save the metabox. */
add_action( 'save_post', 'partenaire_url_save_metabox' );
function partenaire_url_save_metabox( $post_id ) {
agnez's avatar
agnez committed
430 431 432 433 434 435 436 437 438
	 // Check if our nonce is set.
	 if ( ! isset( $_POST['partenaire_url_metabox_nonce'] ) ) {
		return;
	 }
	 $nonce = $_POST['partenaire_url_metabox_nonce'];
	 // Verify that the nonce is valid.
	 if ( ! wp_verify_nonce( $nonce, 'partenaire_url_metabox' ) ) {
		return;
	 }
agnez's avatar
agnez committed
439

agnez's avatar
agnez committed
440 441 442 443
	 // If this is an autosave, our form has not been submitted, so we don't want to do anything.
	 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	 }
agnez's avatar
agnez committed
444

agnez's avatar
agnez committed
445 446 447 448
	 // Check the user's permissions.
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		return;
	 }
agnez's avatar
agnez committed
449

agnez's avatar
agnez committed
450 451 452 453
	 // Check for and sanitize user input.
	 if ( ! isset( $_POST['partenaire_url'] ) ) {
		return;
	 }
agnez's avatar
agnez committed
454

agnez's avatar
agnez committed
455
	 $url = esc_url_raw( $_POST['partenaire_url'] );
agnez's avatar
agnez committed
456

agnez's avatar
agnez committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
	 // Update the meta fields in the database, or clean up after ourselves.
	 if ( empty( $url ) ) {
		delete_post_meta( $post_id, '_partenaire_url' );
	 } else {
		update_post_meta( $post_id, '_partenaire_url', $url );
	 }
}
//-------------end metabox URL-partenaire

// Producteurs du GASAP meta box
add_action( 'add_meta_boxes', 'gasap_prods' );
function gasap_prods( $post ) {
	add_meta_box( 
		'gasap_producers',
		__( 'Producers', 'gasap-posttypes' ),
		'render_producers_content',
		'gasap',
		'normal',
		'high'
	);
agnez's avatar
agnez committed
477
}
agnez's avatar
agnez committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
function render_producers_content( $post ) {
	wp_nonce_field( plugin_basename( __FILE__ ), 'render_producers_content_nonce' );
	$selected_producers = get_post_meta( $post->ID, '_producer' );
	$producers = get_posts( 
		array(
		'post_type' => 'producer',
		'numberposts' => -1,
		'orderby' => 'post_title',
		'order' => 'ASC'
		) 
	);
	?>
	<input type="hidden" name="render_producers_content_nonce" value="<?php echo wp_create_nonce( basename( __FILE__ ) ); ?>" />
	<table class="form-table">
		<tr valign="top">
		<td>
		<?php foreach ( $producers as $producer ) : ?>
		<span style="white-space:nowrap;"><input type="checkbox" name="producer[]" value="<?php echo $producer->ID; ?>" <?php echo (in_array($producer->ID, $selected_producers)) ? 'checked="checked"' : ''; ?> /><?php echo apply_filters('the_title',$producer->post_title); ?>&nbsp;&nbsp;&nbsp;</span>	 
		<?php endforeach; ?>
		</select></td></tr>
	</table> <?php
}
add_action( 'save_post', 'save_producer_field' );
function save_producer_field($post_id) {
	global $post;
	// only run this for gasap
	if ( 'gasap' != get_post_type( $post_id ) )
		return $post_id;		

	// verify nonce
	if ( empty( $_POST['render_producers_content_nonce'] ) || !wp_verify_nonce( $_POST['render_producers_content_nonce'], basename( __FILE__ ) ) )
		return $post_id;

	// check autosave
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
		return $post_id;

	// check permissions
	if ( !current_user_can( 'edit_post', $post_id ) )
		return $post_id;

	// save
	if( isset( $_POST[ 'producer' ] ) ) {
		$custom = $_POST['producer'];
				//$custom = array(
				//		'prodid' => '$_POST['producer']'
				//);		
		$old_meta = get_post_meta($post->ID, '_producer', true);
		// Update post meta
		if(!empty($old_meta)){
			update_post_meta($post->ID, '_producer', $custom);
		} else {
			add_post_meta($post->ID, '_producer', $custom, true);
		}
	}
}
//-------------end gasap-producer meta box

agnez's avatar
agnez committed
536 537 538 539 540 541 542 543 544 545

/**
 * Hide meta boxes for specific capabilities
 */
add_action( 'add_meta_boxes', 'gasap_remove_post_meta_boxes' );
function gasap_remove_post_meta_boxes() {
	if ( !current_user_can( 'manage_options' ) ){
		remove_meta_box( 'contentpiecetypediv', 'contentpiece', 'normal' );		
	}

agnez's avatar
agnez committed
546 547
}
/**
agnez's avatar
agnez committed
548
 * Rename author meta boxes in GASAP poste types
agnez's avatar
agnez committed
549 550 551 552 553 554 555 556 557 558 559 560 561 562
 */
add_action( 'add_meta_boxes_gasap',  'gasap_rename_metabox' );
function gasap_rename_metabox( $post ) {
    if( 'gasap' == get_post_type($post) ){
         remove_meta_box( 'authordiv', 'gasap', 'core' );
         add_meta_box(
         	'authordiv',
         	__('Author = GASAP representative','gasap-posttypes'),
         	'post_author_meta_box',
         	'gasap',
         	'advanced',
         	'high'
         );
     }
agnez's avatar
agnez committed
563
}