Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DCMerch {
- function DCMerch() {
- add_action("init", array(&$this, "merch_init"));
- add_action("admin_init", array(&$this, "merch_admin_init"));
- }
- function merch_init()
- {
- register_post_type('Merch', array(
- 'label' => 'Merch',
- 'public' => true,
- 'show_ui' => true,
- '_builtin' => false,
- 'capability_type' => 'post',
- 'hierarchical' => false,
- 'menu_position' => 5,
- 'rewrite' => array('slug' => 'merch'),
- 'supports' => array('title')
- ));
- register_taxonomy('client', 'Merch',
- array(
- 'hierarchical' => false,
- 'label' => 'Clients',
- 'singular_label' => 'Client',
- 'query_var' => 'client',
- 'show_ui' => false,
- 'rewrite' => array('slug' => 'client' )
- )
- );
- add_action("template_redirect", 'merch_template_redirect');
- }
- function merch_admin_init()
- {
- add_filter("manage_edit-Merch_columns", array(&$this, "edit_merch_columns"));
- add_action("manage_posts_custom_column", array(&$this, "custom_columns"));
- // Custom meta boxes for the edit merch screen
- add_meta_box("merch-meta", "Merch Options", array(&$this, "merch_meta_options"), "Merch", "advanced", "high");
- add_action('save_post','save_merch_meta');
- }
- //Defines the columns for the merch post type
- function edit_merch_columns($columns)
- {
- $columns = array(
- "cb" => "<input type=\"checkbox\" />",
- "title" => "Title",
- "client" => "Client",
- "thumb" => "Thumbnail",
- "date" => "Date"
- );
- return $columns;
- }
- //Outputs custom columns depending on the type
- function custom_columns($column)
- {
- global $post;
- $meta = get_post_meta($post->ID,'_merch',TRUE);
- $client = wp_get_post_terms($post->ID, 'client');
- if ($client == null || count($client) != 1)
- $client = '';
- else
- $client = $client[0]->name;
- switch ($column)
- {
- case "thumb":
- echo $meta['imageurl'];
- break;
- case "client":
- echo $client;
- break;
- }
- }
- // Admin post meta contents
- function merch_meta_options()
- {
- global $post;
- $meta = get_post_meta($post->ID,'_merch',TRUE);
- $client = wp_get_post_terms($post->ID, 'client');
- if ($client == null || count($client) != 1)
- $client = '';
- else
- $client = $client[0]->name;
- include('merch_meta.php');
- echo '<input type="hidden" name="_merch_noncename" value="' . wp_create_nonce(plugin_basename(__FILE__)) . '" />';
- }
- }
- function merch_template_redirect()
- {
- global $wp;
- if (array_key_exists('merch', $wp->query_vars)) {
- include(TEMPLATEPATH . "/merch.php");
- die();
- }
- }
- function save_merch_meta($post_id)
- {
- // authentication checks
- // make sure data came from our meta box
- if (!wp_verify_nonce($_POST['_merch_noncename'],plugin_basename(__FILE__))) return $post_id;
- // check user permissions
- if ($_POST['post_type'] == 'page')
- {
- if (!current_user_can('edit_page', $post_id)) return $post_id;
- }
- else
- {
- if (!current_user_can('edit_post', $post_id)) return $post_id;
- }
- // authentication passed, save data
- $data = get_post_meta($post_id, '_merch', TRUE);
- $new_data = $_POST['_merch'];
- foreach ($new_data as $n => $v)
- {
- if ($n == 'client') {
- if (trim($v) != '') {
- wp_set_post_terms($post_id, trim($v), 'client');
- }
- unset($new_data[$n]);
- }
- else if ($n == 'imagepath') {
- if ($new_data[$n] != $data[$n])
- {
- $attachment = array(
- 'post_title' => 'file'. time(),
- 'post_content' => '',
- 'post_type' => 'attachment',
- 'post_parent' => $post_id,
- 'post_mime_type' => 'image/jpeg'
- );
- $attach_id = wp_insert_attachment( $attachment, $new_data[$n], $post_id );
- $attach_data = wp_generate_attachment_metadata( $attach_id, $new_data[$n] );
- wp_update_attachment_metadata( $attach_id, $attach_data );
- }
- }
- else
- {
- if (is_array($v))
- {
- foreach ($new_data[$n] as $nn => $vv)
- {
- if (trim($vv) === '') unset($new_data[$n][$nn]);
- }
- }
- else
- {
- if (trim($v) === '') unset($new_data[$n]);
- }
- }
- }
- if ($data) update_post_meta($post_id, '_merch', $new_data);
- else add_post_meta($post_id, '_merch', $new_data, TRUE);
- }
- new DCMerch();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement