RoleManagementPageAction.php
1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php namespace MeowCrew\RoleAndCustomerBasedPricing\RoleManagement\Actions;
use Exception;
use MeowCrew\RoleAndCustomerBasedPricing\Core\AdminNotifier;
use MeowCrew\RoleAndCustomerBasedPricing\Core\ServiceContainerTrait;
abstract class RoleManagementPageAction {
use ServiceContainerTrait;
abstract public function handle();
abstract public function getActionSlug();
public function __construct() {
add_action( 'admin_post_' . $this->getActionSlug(), array( $this, 'execute' ) );
}
public function getURL( $role = '' ) {
return wp_nonce_url( add_query_arg( array(
'action' => $this->getActionSlug(),
'role' => $role,
), admin_url( 'admin-post.php' ) ), $this->getActionSlug() );
}
public function execute() {
try {
$this->validate();
$this->handle();
} catch ( Exception $exception ) {
$this->getContainer()->getAdminNotifier()->flash( $exception->getMessage(), AdminNotifier::ERROR, true );
return wp_redirect( wp_get_referer() );
}
}
/**
* Validate request
*
* @throws Exception
*/
public function validate() {
$this->validateNonce();
}
/**
* Validate nonce
*
* @throws Exception
*/
public function validateNonce() {
$nonce = isset($_REQUEST['_wpnonce']) ? sanitize_text_field($_REQUEST['_wpnonce']) : null;
if ( ! wp_verify_nonce( $nonce, $this->getActionSlug() ) ) {
throw new Exception( __( 'Invalid Nonce', 'role-and-customer-based-pricing-for-woocommerce' ) );
}
}
}