Skip to content

Instantly share code, notes, and snippets.

@zacscott
Last active March 11, 2017 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacscott/b4cbacf5a0825a270a28 to your computer and use it in GitHub Desktop.
Save zacscott/b4cbacf5a0825a270a28 to your computer and use it in GitHub Desktop.
Notes - WordPress Plugin
<?php
/**
* Plugin Name: Notes
* Description: Keep private notes within WordPress
* Version: 1.0
* Author: Zachary Scott
* Author URI: https://www.zacscott.net
* Plugin URI: https://gist.github.com/zacscott/b4cbacf5a0825a270a28
* License: GPLv3+
*/
/**
* Notes plugin driver class.
*
* Hooks/filters:
* - `notes_cpt_slug` - filter the notes CPT slug
* - `notes_cpt_args` - filter the notes CPT arguments, passed to
* register_post_type()
* - `notes_pre_get_posts` - `pre_get_posts` action for the notes CPT
* - `notes_tags_slug` - `pre_get_posts` action for the notes tags taxonomy
* - `notes_tags_args` - filter the notes tags taxonomy, passed to
* register_taxonomy()
*
* @author Zachary Scott <zscott.dev@gmail.com>
*/
class NotesPlugin {
/* TODO
* restrict by user (only author can view own notes)
* custom columns
* - title, author modified date
* - auto sort by modified date
*/
// The post type & tags tax slugs
private static $cpt_slug = 'notes';
private static $tags_slug = 'notes_tags';
function __construct() {
// Get the notes CPT & taxonomy slug
self::$cpt_slug = apply_filters( 'notes_cpt_slug', self::$cpt_slug );
self::$tags_slug = apply_filters( 'notes_tags_slug', self::$tags_slug );
// Setup notes CPT
add_action( 'init', array( $this, 'register_cpt' ) );
add_action( 'init', array( $this, 'register_tags_tax' ) );
add_action( 'pre_get_posts', array( $this, 'hide_other_notes' ) );
}
// Registers the Notes custom post type
function register_cpt() {
$labels = array(
'name' => _x( 'Notes', 'Post Type General Name', 'notes' ),
'singular_name' => _x( 'Note', 'Post Type Singular Name', 'notes' ),
'menu_name' => __( 'Notes', 'notes' ),
'parent_item_colon' => __( 'Parent Note:', 'notes' ),
'all_items' => __( 'All Notes', 'notes' ),
'view_item' => __( 'View Note', 'notes' ),
'add_new_item' => __( 'Add New Note', 'notes' ),
'add_new' => __( 'Add New', 'notes' ),
'edit_item' => __( 'Edit Note', 'notes' ),
'update_item' => __( 'Update Note', 'notes' ),
'search_items' => __( 'Search Note', 'notes' ),
'not_found' => __( 'Not found', 'notes' ),
'not_found_in_trash' => __( 'Not found in Trash', 'notes' ),
);
$caps = array(
'edit_post' => 'read',
'read_post' => 'read',
'delete_post' => 'read',
'edit_posts' => 'read',
'edit_others_posts' => 'edit_others_posts',
'publish_posts' => 'read',
'read_private_posts' => 'read_private_posts',
);
$args = array(
'label' => __( 'Notes', 'notes' ),
'description' => __( 'Notes', 'notes' ),
'labels' => $labels,
// 'taxonomies' => array( 'post_tag', ),
'supports' => array( 'title', 'editor', ),
'hierarchical' => false,
'public' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => false,
'menu_position' => 21, // just after Pages
'can_export' => false,
'has_archive' => false,
'exclude_from_search' => true,
'publicly_queryable' => false,
'menu_icon' => 'dashicons-welcome-write-blog',
// 'capability_type' => 'post',
'capabilities' => $caps,
);
$args = apply_filters( 'notes_cpt_args', $args );
register_post_type( self::$cpt_slug, $args );
}
// Registers the Notes tags taxonomy
function register_tags_tax() {
$labels = array(
'name' => _x( 'Tags', 'Taxonomy General Name', 'notes' ),
'singular_name' => _x( 'Tags', 'Taxonomy Singular Name', 'notes' ),
'menu_name' => __( 'Tags', 'notes' ),
'all_items' => __( 'All Tags', 'notes' ),
'parent_item' => __( 'Parent Tag', 'notes' ),
'parent_item_colon' => __( 'Parent Tag:', 'notes' ),
'new_item_name' => __( 'New Tag Name', 'notes' ),
'add_new_item' => __( 'Add New Tag', 'notes' ),
'edit_item' => __( 'Edit Tag', 'notes' ),
'update_item' => __( 'Update Tag', 'notes' ),
'view_item' => __( 'View Tag', 'notes' ),
'separate_items_with_commas' => __( 'Separate tags with commas', 'notes' ),
'add_or_remove_items' => __( 'Add or Remove Tags', 'notes' ),
'choose_from_most_used' => __( 'Choose from the most used', 'notes' ),
'popular_items' => __( 'Popular Tags', 'notes' ),
'search_items' => __( 'Search Tags', 'notes' ),
'not_found' => __( 'Not Found', 'notes' ),
);
$caps= array(
'manage_terms' => 'manage_categories',
'edit_terms' => 'read',
'delete_terms' => 'manage_categories',
'assign_terms' => 'read',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
'capabilities' => $caps,
);
$args = apply_filters( 'notes_tags_args', $args );
register_taxonomy( self::$tags_slug, array( self::$cpt_slug ), $args );
}
// Only show the current users notes
function hide_other_notes( &$query ) {
if ( self::$cpt_slug == $query->post_type ) {
// Only show the current users notes
$query->set( 'author', get_current_user_id() );
do_action( 'notes_pre_get_posts', $query );
}
}
}
new NotesPlugin(); // boot
/**
* Copyright (c) 2014, 2015 <zscott.dev@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment