* @date May 12th 2010 * */ class AnimatedBanners { var $meta_fields = array("banner-duration"); var $path; /** * @desc Constructor; initialise post type, register taxonomy, and setup filters * */ function AnimatedBanners() { $this->path = dirname(__FILE__).'/'; // Register custom post types register_post_type('animated-banner', array( 'label' => __('Banners'), 'singular_label' => __('Banner'), 'public' => true, 'show_ui' => true, // UI in admin panel '_builtin' => false, // It's a custom post type, not built in '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => array("slug" => "animated-banner"), // Permalinks 'query_var' => "animated-banner", // This goes to the WP_Query schema 'supports' => array('title','editor') )); add_filter("manage_edit-animated-banner_columns", array(&$this, "edit_columns")); add_action("manage_posts_custom_column", array(&$this, "custom_columns")); // Register custom taxonomy register_taxonomy("caption", array("animated-banner"), array( "hierarchical" => true, "label" => "Captions", "singular_label" => "Caption", "rewrite" => true )); // Admin interface init add_action("admin_init", array(&$this, "admin_init")); add_action("template_redirect", array(&$this, 'template_redirect')); // Insert post hook add_action("wp_insert_post", array(&$this, "wp_insert_post"), 10, 2); } /** * @desc Provide columns on the Edit Banners admin panel */ function edit_columns(){ $columns = array( "cb" => "", "title" => "Animated Banner Title", "banner_description" => "Description", "banner_duration" => "Duration", "banner_captions" => "Captions", ); return $columns; } /** * @desc Provide data for a given column of the Edit Banners Admin Panel * @param @column string Column to provide data for */ function custom_columns($column) { global $post; switch ($column) { case "banner_description": the_excerpt(); break; case "banner_duration": $custom = get_post_custom(); echo $custom["banner-duration"][0]; break; case "banner_captions": $captions = get_the_terms(0, "caption"); $captions_html = array(); if($captions) foreach ($captions as $caption) array_push($captions_html, '' . $caption->name . ''); echo implode($captions_html, ", "); break; } } /** * @desc Display this banner directly - not supported */ function template_redirect(){ global $wp; if ($wp->query_vars["post_type"] == "animated-banner"){ print "This plugin does not support previewing banners yet."; } } /** * @desc Insert post action * @param $post_id int Post ID that's being saved * @param $post object Post Object that's being saved */ function wp_insert_post($post_id, $post = null){ if ($post->post_type == "animated-banner"){ // Loop through the POST data foreach ($this->meta_fields as $key){ $value = @$_POST[$key]; if (empty($value)){ delete_post_meta($post_id, $key); continue; } // If value is a string it should be unique if (!is_array($value)){ if (!update_post_meta($post_id, $key, $value)){ add_post_meta($post_id, $key, $value); } } else { // If passed along is an array, we should remove all previous data delete_post_meta($post_id, $key); // Loop through the array adding new values to the post meta as different entries with the same name foreach ($value as $entry) add_post_meta($post_id, $key, $entry); } } } } /** * @desc Initalise meta area on the admin loader */ function admin_init(){ // Custom meta boxes for the edit animated-banner screen add_meta_box( "banner-meta", "Information", array(&$this, "meta_options"), "animated-banner", "advanced", //side "high" //"low" ); } /** * @desc Prepare contents of the meta area in the Banner Admin (loads HTML template) */ function meta_options(){ global $post; $custom = get_post_custom($post->ID); $duration = $custom["banner-duration"][0]; include $path.'AdminFormMeta.html'; } } ?>