78 lines
2.7 KiB
PHP
78 lines
2.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace RomhackPlaza\Overrides;
|
||
|
|
defined( '\ABSPATH' ) || exit;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Replace 'post' default post type to announcements.
|
||
|
|
*/
|
||
|
|
class Post_Announcements extends Abstract_Overrider {
|
||
|
|
|
||
|
|
protected function can_override(): bool
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function override(): void {
|
||
|
|
|
||
|
|
$this->add_action( 'init', [ $this, 'rename_post_to_announcements' ], 20 );
|
||
|
|
$this->add_action( 'admin_menu', [ $this, 'rename_post_to_announcements_menu' ], 20 );
|
||
|
|
$this->add_action( 'save_post', [ $this, 'can_save_announcement' ], 5, 3 );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public function rename_post_to_announcements() {
|
||
|
|
|
||
|
|
global $_romhackplaza;
|
||
|
|
if( !$_romhackplaza->user_roles->current_is( Roles::Administrator ) )
|
||
|
|
return; // Don't change if the user is not an admin.
|
||
|
|
|
||
|
|
global $wp_post_types;
|
||
|
|
$labels = &$wp_post_types['post']->labels;
|
||
|
|
$labels->name = __( 'Announcements', 'romhackplaza' );
|
||
|
|
$labels->singular_name = __( 'Announcement', 'romhackplaza' );
|
||
|
|
$labels->add_new = __( 'Add Announce', 'romhackplaza' );
|
||
|
|
$labels->add_new_item = __( 'Add Announce', 'romhackplaza' );
|
||
|
|
$labels->edit_item = __( 'Edit Annonce', 'romhackplaza' );
|
||
|
|
$labels->new_item = __( 'Announce', 'romhackplaza' );
|
||
|
|
$labels->view_item = __( 'View Announce', 'romhackplaza' );
|
||
|
|
$labels->search_items = __( 'Search Announcements', 'romhackplaza' );
|
||
|
|
$labels->not_found = __( 'No Announce found', 'romhackplaza' );
|
||
|
|
$labels->not_found_in_trash = __( 'No Announce found in Trash', 'romhackplaza' );
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public function rename_post_to_announcements_menu() {
|
||
|
|
|
||
|
|
global $_romhackplaza;
|
||
|
|
if( !$_romhackplaza->user_roles->current_is( Roles::Administrator ) )
|
||
|
|
return; // Don't change if the user is not an admin.
|
||
|
|
|
||
|
|
global $menu;
|
||
|
|
global $submenu;
|
||
|
|
|
||
|
|
$menu[5][0] = __( 'Announcements', 'romhackplaza' );
|
||
|
|
$menu[5][6] = 'dashicons-megaphone';
|
||
|
|
$submenu['edit.php'][5][0] = __( 'Announcements', 'romhackplaza' );
|
||
|
|
$submenu['edit.php'][10][0] = __( 'Add Announce', 'romhackplaza' );
|
||
|
|
echo '';
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Can be moved in SavePost Extender ???
|
||
|
|
* Only admin can send announcement.
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function can_save_announcement( $post_id, $post, $update ) {
|
||
|
|
|
||
|
|
global $_romhackplaza;
|
||
|
|
|
||
|
|
// Get post type after to avoid conflict or pre-save.
|
||
|
|
if( get_post_type( $post_id ) === 'post' && !$_romhackplaza->user_roles->current_is( Roles::Administrator ) ){
|
||
|
|
wp_die( __( "You don't have the permission to post announcements.", 'romhackplaza' ), __( 'Permission error', 'romhackplaza'), [ 'response' => 403 ] );
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|