Skip to content

Instantly share code, notes, and snippets.

@haruair
Created October 4, 2014 15:40
Show Gist options
  • Save haruair/0ef8f46922e845b00f27 to your computer and use it in GitHub Desktop.
Save haruair/0ef8f46922e845b00f27 to your computer and use it in GitHub Desktop.
Scrapbook plugin for Wordpress
<?php
/**
* @package Haruair
* @version 1.0
*/
/*
Plugin Name: Scrapbook
Description: scrapbook custom type plugin
Author: haruair
Version: 1.0
Author URI: http://haruair.com/
*/
function haruair_scrapbook_init() {
haruair_scrapbook_create_post_types();
haruair_scrapbook_create_initial_taxonomies();
}
// Register Custom Post Type
function haruair_scrapbook_create_post_types() {
$labels = array(
'name' => 'Scraps',
'singular_name' => 'Scrap',
'menu_name' => 'Scrap',
'parent_item_colon' => 'Parent Item:',
'all_items' => 'All Items',
'view_item' => 'View Item',
'add_new_item' => 'Add New Item',
'add_new' => 'Add New',
'edit_item' => 'Edit Item',
'update_item' => 'Update Item',
'search_items' => 'Search Item',
'not_found' => 'Not found',
'not_found_in_trash' => 'Not found in Trash',
);
$args = array(
'label' => 'scrap',
'description' => 'Scrap',
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'post-formats', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 10,
'rewrite' => array('with_front' => false),
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type( 'scrap', $args );
}
// Register Custom Taxonomy
function haruair_scrapbook_create_initial_taxonomies() {
$category_args = array(
'labels' => haruair_scrapbook_taxonomy_label('category', 'categories'),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
$taxonomy_args = array(
'labels' => haruair_scrapbook_taxonomy_label('tag', 'tags'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'scrap_category', array( 'scrap' ), $category_args );
register_taxonomy( 'scrap_tag', array( 'scrap' ), $taxonomy_args );
}
function haruair_scrapbook_taxonomy_label($singular, $plural) {
$ucSingular = ucfirst($singular);
$ucPlural = ucfirst($plural);
$lowerPlural = strtolower($plural);
$labels = array(
'name' => $ucPlural,
'singular_name' => $ucSingular,
'menu_name' => 'Scrap ' . $ucSingular,
'all_items' => 'All '. $ucPlural,
'parent_item' => 'Parent ' . $ucSingular,
'parent_item_colon' => 'Parent ' . $ucSingular . ':',
'new_item_name' => 'New ' . $ucSingular . ' Name',
'add_new_item' => 'Add New ' . $ucSingular,
'edit_item' => 'Edit ' . $ucSingular,
'update_item' => 'Update ' . $ucSingular,
'separate_items_with_commas' => 'Separate ' . $lowerPlural . ' with commas',
'search_items' => 'Search '. $ucPlural,
'add_or_remove_items' => 'Add or remove ' . $lowerPlural,
'choose_from_most_used' => 'Choose from the most used ' . $lowerPlural,
'not_found' => 'Not Found',
);
return $labels;
}
class WP_Widget_Scrapbook_Categories extends WP_Widget {
function __construct() {
$widget_ops = array( 'classname' => 'widget_scrapbook_categories', 'description' => __( "A list or dropdown of scrapbook categories." ) );
parent::__construct('scrap_categories', __('Scrap Categories'), $widget_ops);
}
function widget( $args, $instance ) {
extract( $args );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base );
$c = ! empty( $instance['count'] ) ? '1' : '0';
$h = ! empty( $instance['hierarchical'] ) ? '1' : '0';
$d = ! empty( $instance['dropdown'] ) ? '1' : '0';
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
$cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'scrap_category');
if ( $d ) {
$cat_args['show_option_none'] = __('Select Category');
/**
* Filter the arguments for the Categories widget drop-down.
*
* @since 2.8.0
*
* @see wp_dropdown_categories()
*
* @param array $cat_args An array of Categories widget drop-down arguments.
*/
wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args ) );
?>
<script type='text/javascript'>
/* <![CDATA[ */
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "<?php echo home_url(); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
/* ]]> */
</script>
<?php
} else {
?>
<ul>
<?php
$cat_args['title_li'] = '';
/**
* Filter the arguments for the Categories widget.
*
* @since 2.8.0
*
* @param array $cat_args An array of Categories widget options.
*/
wp_list_categories( apply_filters( 'widget_categories_args', $cat_args ) );
?>
</ul>
<?php
}
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['count'] = !empty($new_instance['count']) ? 1 : 0;
$instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
$instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
return $instance;
}
function form( $instance ) {
//Defaults
$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
$title = esc_attr( $instance['title'] );
$count = isset($instance['count']) ? (bool) $instance['count'] :false;
$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
$dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
<label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br />
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
<label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
<?php
}
}
function haruair_scrapbook_widgets_init() {
register_sidebar( array(
'name' => 'Scrap Sidebar',
'id' => 'sidebar-scrap',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h1 class="widget-title">',
'after_title' => '</h1>',
) );
register_widget('WP_Widget_Scrapbook_Categories');
}
function haruair_scrapbook_per_get_posts( $query ) {
$queried_object = $query->get_queried_object();
if ( $query->is_post_type_archive('scrap') || $query->is_tax('scrap_category') || $query->is_tax('scrap_tag') ) {
$query->set( 'posts_per_page', 10 );
}
return $query;
}
// Hook into the 'init' action
add_action( 'init', 'haruair_scrapbook_init', 0 );
// set widgets
add_action( 'widgets_init', 'haruair_scrapbook_widgets_init' );
// set per posts
add_filter( 'pre_get_posts', 'haruair_scrapbook_per_get_posts' );
// public function for theme
function get_the_scrap_category_list( $separator = '', $parents='', $post_id = false ) {
global $wp_rewrite;
if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'scrap_category' ) ) {
/** This filter is documented in wp-includes/category-template.php */
return apply_filters( 'the_category', '', $separator, $parents );
}
$categories = get_the_scrap_category( $post_id );
if ( empty( $categories ) ) {
/** This filter is documented in wp-includes/category-template.php */
return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
}
$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
$thelist = '';
if ( '' == $separator ) {
$thelist .= '<ul class="post-categories">';
foreach ( $categories as $category ) {
$thelist .= "\n\t<li>";
switch ( strtolower( $parents ) ) {
case 'multiple':
if ( $category->parent )
$thelist .= get_scrap_category_parents( $category->parent, true, $separator );
$thelist .= '<a href="' . esc_url( get_scrap_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
break;
case 'single':
$thelist .= '<a href="' . esc_url( get_scrap_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
if ( $category->parent )
$thelist .= get_scrap_category_parents( $category->parent, false, $separator );
$thelist .= $category->name.'</a></li>';
break;
case '':
default:
$thelist .= '<a href="' . esc_url( get_scrap_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
}
}
$thelist .= '</ul>';
} else {
$i = 0;
foreach ( $categories as $category ) {
if ( 0 < $i )
$thelist .= $separator;
switch ( strtolower( $parents ) ) {
case 'multiple':
if ( $category->parent )
$thelist .= get_scrap_category_parents( $category->parent, true, $separator );
$thelist .= '<a href="' . esc_url( get_scrap_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
break;
case 'single':
$thelist .= '<a href="' . esc_url( get_scrap_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
if ( $category->parent )
$thelist .= get_scrap_category_parents( $category->parent, false, $separator );
$thelist .= "$category->name</a>";
break;
case '':
default:
$thelist .= '<a href="' . esc_url( get_scrap_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
}
++$i;
}
}
/**
* Filter the category or list of categories.
*
* @since 1.2.0
*
* @param array $thelist List of categories for the current post.
* @param string $separator Separator used between the categories.
* @param string $parents How to display the category parents. Accepts 'multiple',
* 'single', or empty.
*/
return apply_filters( 'the_category', $thelist, $separator, $parents );
}
/**
* Retrieve category link URL.
*
* @since 1.0.0
* @see get_term_link()
*
* @param int|object $category Category ID or object.
* @return string Link on success, empty string if category does not exist.
*/
function get_scrap_category_link( $category ) {
if ( ! is_object( $category ) )
$category = (int) $category;
$category = get_term_link( $category, 'scrap_category' );
if ( is_wp_error( $category ) )
return '';
return $category;
}
/**
* Retrieve post categories.
*
* @since 0.71
* @uses $post
*
* @param int $id Optional, default to current post ID. The post ID.
* @return array
*/
function get_the_scrap_category( $id = false ) {
$categories = get_the_terms( $id, 'scrap_category' );
if ( ! $categories || is_wp_error( $categories ) )
$categories = array();
$categories = array_values( $categories );
foreach ( array_keys( $categories ) as $key ) {
_make_cat_compat( $categories[$key] );
}
/**
* Filter the array of categories to return for a post.
*
* @since 3.1.0
*
* @param array $categories An array of categories to return for the post.
*/
return apply_filters( 'get_the_categories', $categories );
}
/**
* Retrieve category parents with separator.
*
* @since 1.2.0
*
* @param int $id Category ID.
* @param bool $link Optional, default is false. Whether to format with link.
* @param string $separator Optional, default is '/'. How to separate categories.
* @param bool $nicename Optional, default is false. Whether to use nice name for display.
* @param array $visited Optional. Already linked to categories to prevent duplicates.
* @return string|WP_Error A list of category parents on success, WP_Error on failure.
*/
function get_scrap_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
$chain = '';
$parent = get_term( $id, 'category' );
if ( is_wp_error( $parent ) )
return $parent;
if ( $nicename )
$name = $parent->slug;
else
$name = $parent->name;
if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
$visited[] = $parent->parent;
$chain .= get_scrap_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
}
if ( $link )
$chain .= '<a href="' . esc_url( get_scrap_category_link( $parent->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
else
$chain .= $name.$separator;
return $chain;
}
function the_scrap_category( $separator = '', $parents='', $post_id = false ) {
echo get_the_scrap_category_list( $separator, $parents, $post_id );
}
/**
* Retrieve the tags for a post.
*
* @since 2.3.0
*
* @param int $id Post ID.
* @return array|bool Array of tag objects on success, false on failure.
*/
function get_the_scrap_tags( $id = 0 ) {
/**
* Filter the array of tags for the given post.
*
* @since 2.3.0
*
* @see get_the_terms()
*
* @param array $terms An array of tags for the given post.
*/
return apply_filters( 'get_the_tags', get_the_terms( $id, 'scrap_tag' ) );
}
/**
* Retrieve the tags for a post formatted as a string.
*
* @since 2.3.0
*
* @param string $before Optional. Before tags.
* @param string $sep Optional. Between tags.
* @param string $after Optional. After tags.
* @param int $id Optional. Post ID. Defaults to the current post.
* @return string|bool|WP_Error A list of tags on success, false or WP_Error on failure.
*/
function get_the_scrap_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
/**
* Filter the tags list for a given post.
*
* @since 2.3.0
*
* @param string $tag_list List of tags.
* @param string $before String to use before tags.
* @param string $sep String to use between the tags.
* @param string $after String to use after tags.
* @param int $id Post ID.
*/
return apply_filters( 'the_tags', get_the_term_list( $id, 'scrap_tag', $before, $sep, $after ), $before, $sep, $after, $id );
}
/**
* Retrieve the tags for a post.
*
* @since 2.3.0
*
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
*/
function the_scrap_tags( $before = null, $sep = ', ', $after = '' ) {
if ( null === $before )
$before = __('Tags: ');
echo get_the_scrap_tag_list($before, $sep, $after);
}
function has_scrap_tag( $tag = '', $post = null ) {
return has_term( $tag, 'scrap_tag', $post );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment