Created
August 13, 2019 19:46
-
-
Save marktenney/c28b9910277215edb23d77a3b85fb5ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Blackline Client Portal | |
Plugin URI: https://blackline.limited/ | |
Description: This is the client portal we use when we are actively designing and developing projects for clients. | |
Version: 1.0 | |
Author: Blackline Branding Limited | |
Author URI: https://blackline.limited/ | |
License: GPL2 | |
*/ | |
// Register Custom Post Type for Projects | |
if (!function_exists('custom_post_type_bline_clients')) { | |
// Update Permalinks Page with Custom Field | |
add_action( 'load-options-permalink.php', 'bline_clients_load_permalinks' ); | |
function bline_clients_load_permalinks() | |
{ | |
if( isset( $_POST['bline-clients'] ) ) | |
{ | |
update_option( 'bline-clients', sanitize_title_with_dashes( $_POST['bline-clients'] ) ); | |
} | |
// Add a settings field to the permalink page | |
add_settings_field( 'bline-clients', __( 'Project base' ), 'bline_clients_field_callback', 'permalink', 'optional' ); | |
} | |
function bline_clients_field_callback() | |
{ | |
$value = get_option( 'bline-clients' ); // This is the get_option that carries the data | |
echo '<input type="text" value="' . esc_attr( $value ) . '" name="bline-clients" id="bline-clients" class="regular-text" />'; | |
} | |
function custom_post_type_bline_clients() { | |
$slug = get_option( 'bline-clients' ); // This is where we get the slug data from the permalinks page | |
if( ! $slug ) $slug = 'client'; // If it isn't set, it defaults to client | |
$labels = array( | |
'name' => _x( 'Clients', 'Post Type General Name', 'text_domain' ), | |
'singular_name' => _x( 'Client', 'Post Type Singular Name', 'text_domain' ), | |
'menu_name' => __( 'Clients', 'text_domain' ), | |
'name_admin_bar' => __( 'Client', 'text_domain' ), | |
'archives' => __( 'Client Archives', 'text_domain' ), | |
'attributes' => __( 'Client Attributes', 'text_domain' ), | |
'parent_item_colon' => __( 'Parent Client:', 'text_domain' ), | |
'all_items' => __( 'All Clients', 'text_domain' ), | |
'add_new_item' => __( 'Add New Client', 'text_domain' ), | |
'add_new' => __( 'Add New', 'text_domain' ), | |
'new_item' => __( 'New Client', 'text_domain' ), | |
'edit_item' => __( 'Edit Client', 'text_domain' ), | |
'update_item' => __( 'Update Client', 'text_domain' ), | |
'view_item' => __( 'View Client', 'text_domain' ), | |
'view_items' => __( 'View Client', 'text_domain' ), | |
'search_items' => __( 'Search Client', 'text_domain' ), | |
'not_found' => __( 'Not found', 'text_domain' ), | |
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ), | |
'featured_image' => __( 'Featured Image', 'text_domain' ), | |
'set_featured_image' => __( 'Set featured image', 'text_domain' ), | |
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ), | |
'use_featured_image' => __( 'Use as featured image', 'text_domain' ), | |
'insert_into_item' => __( 'Insert into Client', 'text_domain' ), | |
'uploaded_to_this_item' => __( 'Uploaded to this Client', 'text_domain' ), | |
'items_list' => __( 'Client list', 'text_domain' ), | |
'items_list_navigation' => __( 'Client list navigation', 'text_domain' ), | |
'filter_items_list' => __( 'Filter Client list', 'text_domain' ), | |
); | |
$args = array( | |
'label' => __( 'Client', 'text_domain' ), | |
'description' => __( 'Client Description', 'text_domain' ), | |
'labels' => $labels, | |
'supports' => array( 'title', 'editor', 'page-attributes' ), | |
'taxonomies' => array( 'category', 'post_tag' ), | |
'hierarchical' => true, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'menu_position' => 5, | |
'show_in_admin_bar' => true, | |
'show_in_nav_menus' => true, | |
'can_export' => true, | |
'has_archive' => true, | |
'exclude_from_search' => false, | |
'publicly_queryable' => true, | |
'capability_type' => 'page', | |
'rewrite'=> array( | |
'slug' => $slug, // This should set the default slug to clients | |
'with_front' => false, | |
'feed' => true, | |
'pages' => true, | |
), | |
); | |
register_post_type( 'bline-clients', $args ); | |
} | |
add_action( 'init', 'custom_post_type_bline_clients', 0 ); | |
add_post_type_support( 'bline-clients', 'thumbnail' ); post_type_supports( 'bline-clients', 'thumbnail' ); | |
} | |
/*** Setup Projects Post Type *****/ | |
if (!function_exists('custom_post_type_projects')) { | |
function custom_post_type_projects() { | |
$slug = get_option( 'bline-projects' ); // This is where we get the slug data from the permalinks page | |
if( ! $slug ) $slug = 'project'; // If it isn't set, it defaults to sermons | |
$labels = array( | |
'name' => _x( 'Projects', 'Post Type General Name', 'text_domain' ), | |
'singular_name' => _x( 'Project', 'Post Type Singular Name', 'text_domain' ), | |
'menu_name' => __( 'Projects', 'text_domain' ), | |
'name_admin_bar' => __( 'Project', 'text_domain' ), | |
'archives' => __( 'Project Archives', 'text_domain' ), | |
'attributes' => __( 'Project Attributes', 'text_domain' ), | |
'parent_item_colon' => __( 'Parent Project:', 'text_domain' ), | |
'all_items' => __( 'All Projects', 'text_domain' ), | |
'add_new_item' => __( 'Add New Project', 'text_domain' ), | |
'add_new' => __( 'Add New', 'text_domain' ), | |
'new_item' => __( 'New Project', 'text_domain' ), | |
'edit_item' => __( 'Edit Project', 'text_domain' ), | |
'update_item' => __( 'Update Project', 'text_domain' ), | |
'view_item' => __( 'View Project', 'text_domain' ), | |
'view_items' => __( 'View Project', 'text_domain' ), | |
'search_items' => __( 'Search Project', 'text_domain' ), | |
'not_found' => __( 'Not found', 'text_domain' ), | |
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ), | |
'featured_image' => __( 'Featured Image', 'text_domain' ), | |
'set_featured_image' => __( 'Set featured image', 'text_domain' ), | |
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ), | |
'use_featured_image' => __( 'Use as featured image', 'text_domain' ), | |
'insert_into_item' => __( 'Insert into Project', 'text_domain' ), | |
'uploaded_to_this_item' => __( 'Uploaded to this Project', 'text_domain' ), | |
'items_list' => __( 'Project list', 'text_domain' ), | |
'items_list_navigation' => __( 'Project list navigation', 'text_domain' ), | |
'filter_items_list' => __( 'Filter Project list', 'text_domain' ), | |
); | |
$args = array( | |
'label' => __( 'Project', 'text_domain' ), | |
'description' => __( 'Project Description', 'text_domain' ), | |
'labels' => $labels, | |
'supports' => array( 'title', 'editor', 'page-attributes' ), | |
'taxonomies' => array( 'category', 'post_tag' ), | |
'hierarchical' => false, | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'menu_position' => 5, | |
'show_in_admin_bar' => true, | |
'show_in_nav_menus' => true, | |
'can_export' => true, | |
'has_archive' => true, | |
'exclude_from_search' => true, | |
'publicly_queryable' => true, | |
'capability_type' => 'page', | |
'rewrite'=> array( | |
'slug' => $slug, // This should set the default slug to sermons | |
'with_front' => false, | |
'feed' => true, | |
'pages' => true, | |
), | |
); | |
register_post_type( 'bline-projects', $args ); | |
} | |
add_action( 'init', 'custom_post_type_projects', 0 ); | |
add_post_type_support( 'bline-projects', 'thumbnail' ); post_type_supports( 'bline-projects', 'thumbnail' ); | |
} | |
/**** Make Projects a Child CPT of the Client. Reference https://1fix.io/blog/2016/02/05/parent-from-another-cpt/ ****/ | |
function my_add_meta_boxes() { | |
add_meta_box( 'project-parent', 'Parent Client', 'project_attributes_meta_box', 'bline-projects', 'side', 'high' ); | |
} | |
add_action( 'add_meta_boxes', 'my_add_meta_boxes' ); | |
function project_attributes_meta_box( $post ) { | |
$post_type_object = get_post_type_object( $post->post_type ); | |
$pages = wp_dropdown_pages( array( 'post_type' => 'bline-clients', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __( '(no parent)' ), 'sort_column'=> 'menu_order, post_title', 'echo' => 0 ) ); | |
if ( ! empty( $pages ) ) { | |
echo $pages; | |
} | |
} | |
// Fix URL Structure | |
function my_add_rewrite_rules() { | |
add_rewrite_tag('%bline-projects%', '([^/]+)', 'bline-projects='); | |
add_permastruct('bline-projects', '/client/%bline-clients%/%bline-projects%', false); | |
add_rewrite_rule('^client/([^/]+)/([^/]+)/?','index.php?bline-projects=$matches[2]','top'); | |
} | |
add_action( 'init', 'my_add_rewrite_rules' ); | |
//Update Permalinks | |
function my_permalinks($permalink, $post, $leavename) { | |
$post_id = $post->ID; | |
if($post->post_type != 'bline-projects' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) | |
return $permalink; | |
$parent = $post->post_parent; | |
$parent_post = get_post( $parent ); | |
$permalink = str_replace('%bline-clients%', $parent_post->post_name, $permalink); | |
return $permalink; | |
} | |
add_filter('post_type_link', 'my_permalinks', 10, 3); | |
// Add a Client Taxonomy | |
function taxonomy_client() { | |
$labels = array( | |
'name' => _x( 'Clients', 'Taxonomy General Name', 'text_domain' ), | |
'singular_name' => _x( 'Client', 'Taxonomy Singular Name', 'text_domain' ), | |
'menu_name' => __( 'Client Taxonomy', 'text_domain' ), | |
'all_items' => __( 'All Client Taxonomies', 'text_domain' ), | |
'parent_item' => __( 'Parent Client Taxonomy', 'text_domain' ), | |
'parent_item_colon' => __( 'Parent Client Taxonomy:', 'text_domain' ), | |
'new_item_name' => __( 'New Client Taxonomy Name', 'text_domain' ), | |
'add_new_item' => __( 'Add New Client Taxonomy', 'text_domain' ), | |
'edit_item' => __( 'Edit Client Taxonomy', 'text_domain' ), | |
'update_item' => __( 'Update Client Taxonomy', 'text_domain' ), | |
'view_item' => __( 'View Client Taxonomy', 'text_domain' ), | |
'separate_items_with_commas' => __( 'Separate clients with commas', 'text_domain' ), | |
'add_or_remove_items' => __( 'Add or remove clients', 'text_domain' ), | |
'choose_from_most_used' => __( 'Choose from the most used clients', 'text_domain' ), | |
'popular_items' => __( 'Popular Clients', 'text_domain' ), | |
'search_items' => __( 'Search Clients', 'text_domain' ), | |
'not_found' => __( 'Not Found', 'text_domain' ), | |
'no_terms' => __( 'No client', 'text_domain' ), | |
'items_list' => __( 'Client list', 'text_domain' ), | |
'items_list_navigation' => __( 'Client list navigation', 'text_domain' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'hierarchical' => true, | |
'public' => true, | |
'show_ui' => true, // must remain true so Page Builder can use it. | |
'show_in_menu' => true, | |
'show_in_quick_edit' => true, | |
'meta_box_cb' => true, // this shows the default metabox | |
'show_admin_column' => true, | |
'show_in_nav_menus' => true, | |
'show_tagcloud' => true, | |
'show_in_rest' => true, | |
); | |
register_taxonomy( 'client', array( 'bline-clients', 'bline-projects', 'ph-mockups' ), $args ); | |
} | |
add_action( 'init', 'taxonomy_client', 0 ); | |
// Automatically add each client post title as a new client taxonomy term | |
function add_client_taxonomy_automatically($post_ID) { | |
global $wpdb; | |
if(!has_term('','client',$post_ID)){ | |
$cat = get_the_title($post_ID); | |
wp_set_object_terms($post_ID, $cat, 'client'); | |
} | |
} | |
add_action('publish_bline-clients', 'add_client_taxonomy_automatically'); | |
/****** Add Metabox Fields ******/ | |
function bline_clients_get_meta_box( $meta_boxes ) { | |
$prefix = 'bline-clients-'; | |
$meta_boxes[] = array( | |
'id' => 'bline-clients-settings', | |
'title' => esc_html__( 'Client Restriction', 'metabox-online-generator' ), | |
'post_types' => array('bline-clients', 'bline-projects' ), | |
'context' => 'advanced', | |
'priority' => 'default', | |
'autosave' => 'false', | |
'fields' => array( | |
array( | |
'id' => $prefix . 'client_taxonomy', | |
'type' => 'taxonomy', | |
'name' => esc_html__( 'Client', 'metabox-online-generator' ), | |
'taxonomy' => 'client', | |
'field_type' => 'select_advanced', | |
), | |
), | |
); | |
return $meta_boxes; | |
} | |
add_filter( 'rwmb_meta_boxes', 'bline_clients_get_meta_box' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment