Last active
March 5, 2023 16:21
-
-
Save garvs/d33f4c71e4af8d3202ea370fbd63a881 to your computer and use it in GitHub Desktop.
User Role Editor: Show for the role WooCommerce orders with selected status only
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 | |
add_filter('ure_role_additional_options', 'add_show_processing_orders_only_to_admin_option', 10, 1); | |
function add_show_processing_orders_only_to_admin_option($items) { | |
$item = URE_Role_Additional_Options::create_item('show_processing_orders_only', esc_html__('Show orders with =Processing= status only', 'user-role-editor'), 'init', 'show_processing_orders_only'); | |
$items[$item->id] = $item; | |
return $items; | |
} | |
function show_processing_orders_only() { | |
new URE_WC_Order_Status_Restrictor(); | |
} | |
class URE_WC_Order_Status_Restrictor { | |
private $allowed_statuses = array( | |
'wc-processing', // Processing | |
//'wc-cancelled' // Cancelled | |
); | |
private $not_allowed_statuses = array( | |
'all', // All | |
'wc-pending', // Pending payment | |
'wc-on-hold', // On hold | |
'wc-completed', // Completed | |
'wc-refunded', // Refunded | |
'wc-failed' // Failed | |
); | |
function __construct() { | |
add_filter( 'views_edit-shop_order', array($this, 'remove_views'), 10, 1 ); | |
add_action('pre_get_posts', array($this, 'filter_status'), 10, 1); | |
add_action('posts_selection', array($this, 'block_status')); | |
} | |
private function apply_restrictions() { | |
if (!is_admin()) { | |
return false; | |
} | |
if ( !class_exists('URE_Lib_Pro') ) { | |
return; | |
} | |
$lib = URE_Lib_Pro::get_instance(); | |
$page = $lib->extract_command_from_url( $_SERVER['REQUEST_URI'], false ); | |
if ( $page!=='edit.php') { | |
return; | |
} | |
$user = wp_get_current_user(); | |
if (!empty($user) && !empty($user->roles) && in_array('administrator', $user->roles)) { | |
return false; | |
} | |
return true; | |
} | |
// end of apply_restrictions() | |
public function remove_views($views) { | |
if (!$this->apply_restrictions()) { | |
return $views; | |
} | |
foreach($this->not_allowed_statuses as $status) { | |
if (isset($views[$status])) { | |
unset($views[$status]); | |
} | |
} | |
return $views; | |
} | |
// end of remove_views() | |
private function remove_status_query($query) { | |
foreach ($this->not_allowed_statuses as $status) { | |
if (is_array($_GET['post_status'])) { | |
foreach ($_GET['post_status'] as $key => $value) { | |
if ($value == $status) { | |
unset($_GET['post_status'][$key]); | |
unset($_REQUEST['post_status'][$key]); | |
unset($query->query_vars['post_status'][$key]); | |
unset($query->query['post_status'][$key]); | |
} | |
} | |
} elseif ($_GET['post_status'] == $status) { | |
unset($_GET['post_status']); | |
unset($_REQUEST['post_status']); | |
unset($query->query_vars['post_status']); | |
unset($query->query['post_status']); | |
} | |
} | |
} | |
// end of remove_status_query() | |
public function filter_status($query) { | |
if (!$this->apply_restrictions()) { | |
return; | |
} | |
if ( !function_exists('get_current_screen') ) { | |
return; | |
} | |
$screen = get_current_screen(); | |
if (empty($screen) || $screen->id !== 'edit-shop_order') { | |
return; | |
} | |
if (!isset($_REQUEST['post_status'])) { | |
$query->set('post_status', array('wc-processing')); // the 1st from allowed statuses according to the views order | |
} else { | |
$this->remove_status_query($query); | |
} | |
} | |
// end of filter_status() | |
public function block_status() { | |
global $post; | |
if (!$this->apply_restrictions()) { | |
return; | |
} | |
if (empty($post)) { | |
return; | |
} | |
if ($post->post_type!=='shop_order') { | |
return; | |
} | |
if (in_array($post->post_status, $this->allowed_statuses)) { | |
return; | |
} | |
// redirect user to the Orders list | |
$url = get_site_url() .'/wp-admin/edit.php?post_type=shop_order'; | |
if (headers_sent()) { | |
?> | |
<script> | |
document.location.href = '<?php echo $url; ?>'; | |
</script> | |
<?php | |
} else { | |
wp_redirect($url); | |
} | |
die; | |
} | |
// end of block_status() | |
} | |
// end of URE_WC_Order_Status_Restrictor class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Am I doing something wrong?
when I plug this into functions.php it give me this:
Parse error: syntax error, unexpected '<', expecting end of file in /home/clients/5831ef4f77166bf2c0381063b5945122/sites/mywebsite.com/wp-content/themes/PrettyCreative/functions.php on line 728