Created
November 21, 2019 22:50
-
-
Save booskills/87fb6ecd87286b367b02336c998a850d to your computer and use it in GitHub Desktop.
sorting columns
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 | |
// If this file is called directly, abort. | |
if ( ! defined( 'ABSPATH' ) ) { | |
die; | |
} | |
/** | |
* Functionality for our Custom Post types | |
* | |
* | |
* @package Rocket_Books | |
* @subpackage Rocket_Books/public | |
* @author Rao <rao@booskills.com> | |
*/ | |
if ( ! class_exists( 'Rocket_Books_Post_Types' ) ): | |
class Rocket_Books_Post_Types { | |
/** | |
* The ID of this plugin. | |
* | |
* @since 1.0.0 | |
* @access private | |
* @var string $plugin_name The ID of this plugin. | |
*/ | |
private $plugin_name; | |
/** | |
* The version of this plugin. | |
* | |
* @since 1.0.0 | |
* @access private | |
* @var string $version The current version of this plugin. | |
*/ | |
private $version; | |
private $template_loader; | |
/** | |
* Initialize the class and set its properties. | |
* | |
* @param string $plugin_name The name of the plugin. | |
* @param string $version The version of this plugin. | |
* | |
* @since 1.0.0 | |
* | |
*/ | |
public function __construct( $plugin_name, $version ) { | |
$this->plugin_name = $plugin_name; | |
$this->version = $version; | |
$this->template_loader = rbr_get_template_loader(); | |
} | |
/** | |
* hooked into 'init' action hook | |
*/ | |
public function init() { | |
$this->register_cpt_book(); | |
$this->register_taxonomy_genre(); | |
} | |
/** | |
* Registering Custom Post Type: Book | |
*/ | |
public function register_cpt_book() { | |
register_post_type( 'book', | |
apply_filters( | |
'rbr/cpt/register/book/args', | |
array( | |
'description' => __( 'Books', 'rocket-books' ), | |
'labels' => array( | |
'name' => _x( 'h2-Events', 'post type general name', 'rocket-books' ), | |
'singular_name' => _x( 'h2-Event', 'post type singular name', 'rocket-books' ), | |
'menu_name' => _x( 'h2-Events', 'admin menu', 'rocket-books' ), | |
'name_admin_bar' => _x( 'h2-Event', 'add new book on admin bar', 'rocket-books' ), | |
'add_new' => _x( 'Add New', 'post_type', 'rocket-books' ), | |
'add_new_item' => __( 'Add New Event', 'rocket-books' ), | |
'edit_item' => __( 'Edit Event', 'rocket-books' ), | |
'new_item' => __( 'New Event', 'rocket-books' ), | |
'view_item' => __( 'View Event', 'rocket-books' ), | |
'search_items' => __( 'Search Events', 'rocket-books' ), | |
'not_found' => __( 'No Events found.', 'rocket-books' ), | |
'not_found_in_trash' => __( 'No Events found in Trash.', 'rocket-books' ), | |
'parent_item_colon' => __( 'Parent Event:', 'rocket-books' ), | |
'all_items' => __( 'All Events', 'rocket-books' ), | |
), | |
'public' => true, | |
'hierarchical' => false, | |
'exclude_from_search' => false, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_nav_menus' => true, | |
'show_in_admin_bar' => true, | |
'menu_position' => 20, | |
'menu_icon' => 'dashicons-book-alt', | |
'capability_type' => 'post', | |
'capabilities' => array(), | |
'map_meta_cap' => null, | |
'supports' => array( | |
'title', | |
'editor', | |
'author', | |
'thumbnail', | |
'excerpt', | |
'comments' | |
), | |
// 'register_meta_box_cb' => array( $this, 'register_metabox_book' ), | |
'register_meta_box_cb' => null, | |
'taxonomies' => array( 'genre' ), | |
'has_archive' => true, | |
'rewrite' => array( | |
'slug' => 'book', | |
'with_front' => true, | |
'feeds' => true, | |
'pages' => true, | |
), | |
'query_var' => true, | |
'can_export' => true, | |
'show_in_rest' => true, | |
) | |
) | |
); | |
} | |
/** | |
* Register custom taxonomy | |
*/ | |
public function register_taxonomy_genre() { | |
register_taxonomy( 'genre', array( 'book' ), array( | |
'description' => 'Genre', | |
'labels' => array( | |
'name' => _x( 'Genre', 'taxonomy general name', 'rocket-books' ), | |
'singular_name' => _x( 'Genre', 'taxonomy singular name', 'rocket-books' ), | |
'search_items' => __( 'Search Genre', 'rocket-books' ), | |
'popular_items' => __( 'Popular Genre', 'rocket-books' ), | |
'all_items' => __( 'All Genre', 'rocket-books' ), | |
'parent_item' => __( 'Parent Genre', 'rocket-books' ), | |
'parent_item_colon' => __( 'Parent Genre:', 'rocket-books' ), | |
'edit_item' => __( 'Edit Genre', 'rocket-books' ), | |
'view_item' => __( 'View Genre', 'rocket-books' ), | |
'update_item' => __( 'Update Genre', 'rocket-books' ), | |
'add_new_item' => __( 'Add New Genre', 'rocket-books' ), | |
'new_item_name' => __( 'New Genre Name', 'rocket-books' ), | |
'separate_items_with_commas' => __( 'Separate genre with commas', 'rocket-books' ), | |
'add_or_remove_items' => __( 'Add or remove genre', 'rocket-books' ), | |
'choose_from_most_used' => __( 'Choose from the most used genre', 'rocket-books' ), | |
'not_found' => __( 'No genre found.', 'rocket-books' ), | |
), | |
'public' => true, | |
'show_ui' => true, | |
'show_in_nav_menus' => true, | |
'show_tagcloud' => true, | |
'meta_box_cb' => null, | |
'show_admin_column' => true, | |
'hierarchical' => true, | |
'query_var' => 'genre', | |
'rewrite' => array( | |
'slug' => 'genre', | |
'with_front' => true, | |
'hierarchical' => true, | |
), | |
'capabilities' => array(), | |
'show_in_rest' => true, | |
) ); | |
} | |
/** | |
* filter content for CPT: Book | |
* | |
* @param mixed $the_content the content of the post/post type. | |
*/ | |
public function content_single_book( $the_content ) { | |
// filter contents for just Books | |
if ( in_the_loop() && is_singular( 'book' ) ) { | |
// return "<pre>" . $the_content . "</pre>"; | |
ob_start(); | |
include ROCKET_BOOKS_BASE_DIR . 'templates/book-content.php'; | |
return ob_get_clean(); | |
} | |
return $the_content; | |
} | |
/** | |
* Single Template for CPT: book | |
*/ | |
public function single_template_book( $template ) { | |
if ( is_singular( 'book' ) ) { | |
return $this->template_loader->get_template_part( 'single', 'book', false ); | |
} | |
return $template; | |
} | |
/** | |
* Archive Template for CPT: book | |
*/ | |
public function archive_template_book( $template ) { | |
if ( rbr_is_archive_book() ) { | |
// template for CPT book | |
return $this->template_loader->get_template_part( 'archive', 'book', false ); | |
} | |
return $template; | |
} | |
// public function get_template_loader() { | |
// | |
//// require_once ROCKET_BOOKS_BASE_DIR . 'public/class-rocket-books-template-loader.php'; | |
//// | |
//// return new Rocket_Books_Template_Loader(); | |
// | |
// return Rocket_Books_Global::template_loader(); | |
// | |
// } | |
/** | |
* Register Metabox for CPT: Book | |
*/ | |
public function register_metabox_book( $post ) { | |
$is_gutenberg_active = ( | |
function_exists( 'use_block_editor_for_post_type' ) && | |
use_block_editor_for_post_type( get_post_type() ) | |
); | |
add_meta_box( | |
'book-details', | |
__( 'Book Details', 'rocket-books' ), | |
array( $this, 'book_metabox_display_cb' ), // function name for procedural | |
'book', | |
( $is_gutenberg_active ) ? 'side' : 'normal', | |
'high' | |
); | |
} | |
/** | |
* Display for Metabox for CPT: book | |
*/ | |
public function book_metabox_display_cb( $post ) { | |
// echo 'here, we shall display fields'; | |
wp_nonce_field( 'rbr_meta_box_nonce_action', 'rbr_meta_box_nonce' ); | |
?> | |
<p> | |
<label for="rbr-book-pages"><?php | |
_e( 'Number of Pages', 'rocket-books' ) | |
?></label> | |
<input | |
type="text" | |
name="rbr-book-pages" | |
class="widefat" | |
value="<?php echo esc_html( get_post_meta( get_the_ID(), 'rbr_book_pages', true ) ); ?>" | |
> | |
</p> | |
<p> | |
<label for="rbr-is-featured"><?php | |
_e( 'is Featured Book?', 'rocket-books' ) | |
?></label> | |
<input | |
type="checkbox" | |
name="rbr-is-featured" | |
value="yes" | |
<?php checked( | |
get_post_meta( get_the_ID(), 'rbr_is_featured', true ), | |
"yes" | |
) ?> | |
/> | |
</p> | |
<?php $book_format_from_db = esc_html( get_post_meta( get_the_ID(), 'rbr_book_format', true ) ); ?> | |
<p> | |
<label for="rbr-book-format"><?php _e( 'Book Format', 'rocket-books' ) ?></label> | |
<select id="rbr-book-format" name="rbr-book-format" class="widefat"> | |
<option value="">Select option...</option> | |
<option value="hardcover" | |
<?php selected( $book_format_from_db, "hardcover" ) ?> | |
>Hardcover | |
</option> | |
<option value="audio" | |
<?php selected( $book_format_from_db, "audio" ) ?> | |
>Audio | |
</option> | |
<option value="pdf" | |
<?php selected( $book_format_from_db, "pdf" ) ?> | |
</option> | |
</select> | |
</p> | |
<?php | |
// echo esc_html(get_post_meta( get_the_ID(), 'rbr_book_pages', true )); | |
// <script>alert('hello');</script> | |
// echo "<pre>"; | |
// var_export(get_post_meta($post->ID)); | |
// echo "</pre>"; | |
// | |
// get_post_meta( | |
// '', | |
// '', | |
// '' // true, | |
// ); | |
} | |
/** | |
* Saving Custom fields for CPT: book | |
*/ | |
public function metabox_save_book( $post_id, $post, $update ) { | |
// $current_user = wp_get_current_user(); | |
// $current_user->add_cap('edit_posts'); | |
/** | |
* Prevent saving if its triggered for: | |
* 1. Auto save | |
* 2. User does not have permission to edit | |
* 3. invalid nonce | |
*/ | |
// if this is an autosave, our form has not been submitted, so do nothing | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return; | |
} | |
// check user permission | |
if ( ! current_user_can( 'edit_posts', $post_id ) ) { | |
print __( 'Sorry, you do not have access to edit post', 'rocket-books' ); | |
exit; | |
} | |
// Verify Nonce | |
if ( | |
! isset( $_POST['rbr_meta_box_nonce'] ) | |
|| | |
! wp_verify_nonce( | |
$_POST['rbr_meta_box_nonce'], | |
'rbr_meta_box_nonce_action' | |
) | |
) { | |
return null; | |
} | |
if ( array_key_exists( 'rbr-book-pages', $_POST ) ) { | |
update_post_meta( | |
$post_id, | |
'rbr_book_pages', | |
absint( $_POST['rbr-book-pages'] ) // should be sanitized | |
); | |
} | |
if ( array_key_exists( 'h2tag-eventdate', $_POST ) ) { | |
update_post_meta( | |
$post_id, | |
'var_id_eventdate' | |
); | |
} | |
if ( array_key_exists( 'h2tag-title', $_POST ) ) { | |
update_post_meta( | |
$post_id, | |
'var_id_title' | |
); | |
} | |
if ( array_key_exists( 'h2tag-image', $_POST ) ) { | |
update_post_meta( | |
$post_id, | |
'var_id_image' | |
); | |
} | |
if ( array_key_exists( 'h2tag-check', $_POST ) ) { | |
update_post_meta( | |
$post_id, | |
'var_id_checkbox' | |
); | |
} | |
if ( array_key_exists( 'h2tag-Textleft', $_POST ) ) { | |
update_post_meta( | |
$post_id, | |
'var_id_textleft' | |
); | |
} | |
if ( array_key_exists( 'h2tag-textright', $_POST ) ) { | |
update_post_meta( | |
$post_id, | |
'var_id_textright' | |
); | |
} | |
if ( array_key_exists( 'h2tag-button-background-color', $_POST ) ) { | |
update_post_meta( | |
$post_id, | |
'var_id_button_background_color' | |
); | |
} | |
if ( array_key_exists( 'h2tag-button-text', $_POST ) ) { | |
update_post_meta( | |
$post_id, | |
'var_id_btname' | |
); | |
} | |
if ( array_key_exists( 'h2tag-button-link', $_POST ) ) { | |
update_post_meta( | |
$post_id, | |
'var_id_btlink' | |
); | |
} | |
// Sanitization : We know the type | |
// Validation : We know what are expecting to recieve | |
if ( array_key_exists( 'rbr-is-featured', $_POST ) ) { | |
update_post_meta( | |
$post_id, | |
'rbr_is_featured', | |
( 'yes' === $_POST['rbr-is-featured'] ) ? 'yes' : 'no' // should be sanitized // yes / no | |
); | |
} | |
if ( array_key_exists( 'rbr-book-format', $_POST ) ) { | |
$book_format = ( | |
in_array( | |
$_POST['rbr-book-format'], | |
array( | |
'hardcover', | |
'audio', | |
'pdf' | |
) | |
) ) ? sanitize_key( $_POST['rbr-book-format'] ) : 'no-format'; | |
update_post_meta( | |
$post_id, | |
'rbr_book_format', // hardcover , audio, pdf | |
$book_format | |
); | |
} | |
} | |
/** | |
* Adding Metaboxes using CMB2 framework | |
* https://github.com/CMB2/CMB2/wiki/Field-Types | |
*/ | |
public function register_cmb2_metabox_book() { | |
$metabox = new_cmb2_box( array( | |
'id' => 'book-details', | |
'title' => __( 'Book Details from CMB2', 'rocket-books' ), | |
'object_types' => array( 'book' ), | |
'context' => 'normal' | |
) ); | |
$metabox->add_field( array( | |
'id' => 'h2_information_for_admin', | |
'name' => __( 'Admin-Information', 'rocket-books' ), | |
'type' => 'textarea_small', | |
'desc' => ' | |
1) output-standard-column = 3, for change= go to page-> shortcode input with "code column=2" | |
', | |
) ); | |
$metabox->add_field( array( | |
'id' => 'rbr_book_pages', | |
'name' => __( 'Number of Pages', 'rocket-books' ), | |
'type' => 'text', | |
'sanitization_cb' => 'absint' | |
) ); | |
$metabox->add_field( array( | |
'id' => 'rbr_book_pages', | |
'name' => __( 'Number of Pages', 'rocket-books' ), | |
'type' => 'text', | |
'sanitization_cb' => 'absint' | |
) ); | |
$metabox->add_field( array( | |
'id' => 'var_id_eventdate', | |
'name' => __( 'Event-Date', 'rocket-books' ), | |
'type' => 'text_date', | |
'date_format' => 'Y-m-d', | |
/* => [ | |
'todayBtn' => true, | |
'format' => 'dd-mm-yyyy', | |
'language' => 'en' | |
]*/ | |
) ); | |
$metabox->add_field( array( | |
'id' => 'var_id_title', | |
'name' => __( 'Event-Title', 'rocket-books' ), | |
'type' => 'text' | |
) ); | |
$metabox->add_field( array( | |
'id' => 'var_id_image', | |
'name' => __( 'Event-Image', 'rocket-books' ), | |
'type' => 'file' | |
) ); | |
$metabox->add_field( array( | |
'id' => 'var_id_textleft', | |
'name' => __( 'Text-one', 'rocket-books' ), | |
'type' => 'textarea' | |
) ); | |
$metabox->add_field( array( | |
'id' => 'var_id_textright', | |
'name' => __( 'Text-two', 'rocket-books' ), | |
'type' => 'textarea' | |
) ); | |
$metabox->add_field( array( | |
'id' => 'var_id_checkbox', | |
'name' => __( 'activate', 'rocket-books' ), | |
'type' => 'checkbox' | |
) ); | |
$metabox->add_field( array( | |
'id' => 'var_id_button_background_color', | |
'name' => __( 'Button Background Color', 'rocket-books' ), | |
'type' => 'colorpicker' | |
) ); | |
$metabox->add_field( array( | |
'id' => 'var_id_btname', | |
'name' => __( 'Button Text', 'rocket-books' ), | |
'type' => 'text' | |
) ); | |
$metabox->add_field( array( | |
'id' => 'var_id_btlink', | |
'name' => __( 'Button Link', 'rocket-books' ), | |
'type' => 'text_url' | |
) ); | |
$metabox->add_field( array( | |
'id' => 'rbr_book_format', | |
'name' => __( 'Book Format', 'rocket-books' ), | |
'type' => 'select', | |
'options' => array( | |
'no-format' => __( 'Select Format', 'rocket-books' ), | |
'hardcover' => __( 'Hardcover', 'rocket-books' ), | |
'audio' => __( 'Audio', 'rocket-books' ), | |
'pdf' => __( 'PDF', 'rocket-books' ), | |
), | |
'default' => 'no-format' | |
) ); | |
} | |
} | |
endif; | |
/**********************************/ | |
/****Admin-Board-Custom-Column*****/ | |
/*********************************/ | |
/*Register the custom Columns*/ | |
add_filter( 'manage_edit-book_columns', 'h2_admindashboard_column_register' ); | |
function h2_admindashboard_column_register( $columns ) { | |
$columns['h2_admin_column_id_eventdate'] = 'Event-Date'; | |
return $columns; | |
} | |
/*Display the column-1 content*/ | |
add_action( 'manage_posts_custom_column', 'h2_admindashboard_column_display_eventdate', 10, 2 ); | |
function h2_admindashboard_column_display_eventdate( $column_name, $post_id ) { | |
if ( 'h2_admin_column_id_eventdate' != $column_name ) { | |
return; | |
} | |
//$h2_admin_column_id_eventdate = get_post_meta( $post_id, 'var_id_eventdate', true ); | |
//$h2_admin_column_id_eventdate = date_i18n( 'Y-m-d', $h2_admin_column_id_eventdate ); | |
$h2_admin_column_id_eventdate = esc_html( get_post_meta( get_the_ID(), 'var_id_eventdate', true ) ); | |
if ( ! $h2_admin_column_id_eventdate ) { | |
$h2_admin_column_id_eventdate = '<em>' . 'undefined' . '</em>'; | |
} | |
echo $h2_admin_column_id_eventdate; | |
} | |
//Register the column as sortable | |
add_filter( 'manage_edit-book_sortable_columns', 'h2_admindashboard_column_register_sortable' ); | |
function h2_admindashboard_column_register_sortable( $columns ) { | |
$columns['h2_admin_column_id_eventdate'] = 'sortbydate'; | |
return $columns; | |
} | |
//teach how to sort | |
//add_filter( 'request', 'h2_admindashboard_column_orderby_eventdate' ); | |
function h2_admindashboard_column_orderby_eventdate( $vars ) { | |
if ( isset( $vars['orderby'] ) && 'h2_admin_column_id_eventdate' == $vars['orderby'] ) { | |
$vars = array( | |
'post_status' => 'publish', | |
'posts_per_page' => 6, | |
'paged' => $paged, | |
'meta_key' => 'event_date', | |
'orderby' => 'meta_value_num', | |
'order' => 'ASC' | |
); | |
/* $vars = array_merge( $vars, array( | |
'meta_key' => 'oteventdatecolumn', | |
'orderby' => 'meta_value', | |
'order'=>'DESC' | |
));*/ | |
} | |
return $vars; | |
} | |
function h2_admin_dashboard_column_event_date_orderby( \WP_Query $query ) { | |
if ( ! is_admin() ) { | |
return null; | |
} | |
$orderby = $query->get( 'orderby' ); | |
switch ( $orderby ) { | |
case 'sortbydate': | |
$query->set( 'meta_key', 'var_id_eventdate' ); | |
$query->set( 'orderby', 'meta_value' ); | |
$query->set('meta_type', 'date'); | |
break; | |
default: | |
break; | |
} | |
} | |
add_action( 'pre_get_posts', 'h2_admin_dashboard_column_event_date_orderby' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment