*/ class Announcement { use Hooker; private $post_type = 'erp_hr_announcement'; private $assign_type = array(); /** * Load autometically all actions */ function __construct() { $this->assign_type = array( '' => __( '-- Select --', 'erp' ), 'all_employee' => __( 'All Employees', 'erp' ), 'selected_employee' => __( 'Selected Employee', 'erp' ), 'by_department' => __( 'By Department', 'erp' ), 'by_designation' => __( 'By Designation', 'erp' ) ); $this->action( 'init', 'post_types' ) ; $this->action( 'do_meta_boxes', 'do_metaboxes' ); $this->action( 'save_post', 'save_announcement_meta', 10, 2 ); $this->filter( 'manage_edit-erp_hr_announcement_columns', 'add_type_columns' ); $this->filter( 'manage_erp_hr_announcement_posts_custom_column', 'assign_type_edit_columns', 10, 2 ); // $this->filter( 'parent_file', 'fix_parent_file', 999 ); // $this->filter( 'submenu_file', 'submenu_file', 999 ); $this->action( 'admin_head', 'filter_admin_sidebar_menu_items' ); } /** * Filter admin sidebar menu items * * Remove HR Announcement items from sidebar generated by register_post_type function. * Highlight Parent menu as active item when we are in questionnaire menu page and sub pages. * * @since 1.1.6 * * @return void */ function filter_admin_sidebar_menu_items() { global $menu, $submenu_file, $typenow; $hr_menu = array_filter( $menu, function ( $item ) { return __( 'HR Announcement', 'erp' ) === $item[0]; } ); $announcement_pages = [ 'post-new.php?post_type=erp_hr_announcement', 'edit.php?post_type=erp_hr_announcement' ]; if ( in_array( $submenu_file , $announcement_pages ) ) { $submenu_file = 'edit.php?post_type=erp_hr_announcement'; $typenow = null; $_SERVER['PHP_SELF'] = 'erp-hr'; add_filter( 'parent_file', function () { return 'edit.php?post_type=erp_hr_announcement'; } ); } $hr_menu_position = key( $hr_menu ); unset( $menu[ $hr_menu_position ] ); } /** * Fix parent file * * @param string $parent_file * * @return string */ function fix_parent_file( $parent_file ) { global $current_screen; if ( $current_screen->post_type == $this->post_type ) { $parent_file = 'erp-hr'; } return $parent_file; } /** * Set submenu file * * @param string $submenu_file * * @return string */ function submenu_file( $submenu_file ) { global $current_screen; if ( $current_screen->post_type == $this->post_type ) { $submenu_file = 'edit.php?post_type=erp_hr_announcement'; } return $submenu_file; } /** * Register Announcement post type * * @since 0.1 * * @return void */ function post_types() { $capability = 'erp_hr_manager'; register_post_type( $this->post_type, array( 'label' => __( 'Announcement', 'erp' ), 'description' => '', 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'exclude_from_search' => false, 'publicly_queryable' => false, 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => array( 'slug' => '' ), 'query_var' => false, 'supports' => array( 'title', 'editor' ), 'capabilities' => array( 'edit_post' => $capability, 'read_post' => $capability, 'delete_posts' => $capability, 'edit_posts' => $capability, 'edit_others_posts' => $capability, 'publish_posts' => $capability, 'read_private_posts' => $capability, 'create_posts' => $capability, 'delete_post' => $capability, ), 'labels' => array( 'name' => __( 'Announcement', 'erp' ), 'singular_name' => __( 'Announcement', 'erp' ), 'menu_name' => __( 'HR Announcement', 'erp' ), 'add_new' => __( 'Add Announcement', 'erp' ), 'add_new_item' => __( 'Add New Announcement', 'erp' ), 'edit' => __( 'Edit', 'erp' ), 'edit_item' => __( 'Edit Announcement', 'erp' ), 'new_item' => __( 'New Announcement', 'erp' ), 'view' => __( 'View Announcement', 'erp' ), 'view_item' => __( 'View Announcement', 'erp' ), 'search_items' => __( 'Search Announcement', 'erp' ), 'not_found' => __( 'No Announcement Found', 'erp' ), 'not_found_in_trash' => __( 'No Announcement found in trash', 'erp' ), 'parent' => __( 'Parent Announcement', 'erp' ) ), ) ); } /** * Initialize metabox for ERP HR announcement post type * * @since 0.1 * * @return void */ function do_metaboxes() { add_meta_box( 'erp-hr-announcement-meta-box', __('Announcement Settings', 'erp'), array( $this, 'meta_boxes_cb' ), $this->post_type, 'advanced', 'high' ); } /** * Announcement metabox callback function * * @param integer $post_id * * @return void */ function meta_boxes_cb( $post_id ) { global $post; $employees = erp_hr_get_employees( [ 'number' => -1, 'no_object' => true ] ); $departments = erp_hr_get_departments( [ 'number' => -1, 'no_object' => true ] ); $designations = erp_hr_get_designations( [ 'number' => -1, 'no_object' => true ] ); $announcement_type = get_post_meta( $post->ID, '_announcement_type', true ); $announcement_users = get_post_meta( $post->ID, '_announcement_selected_user', true ); $announce_departments = get_post_meta( $post->ID, '_announcement_department', true ); $announce_designations = get_post_meta( $post->ID, '_announcement_designation', true ); $announcement_employee = ( $announcement_users ) ? $announcement_users : array(); $announcement_department = ( $announce_departments ) ? $announce_departments : array(); $announcement_designation = ( $announce_designations ) ? $announce_designations : array(); ?>