SuspendAction.php
1.93 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
62
63
64
65
66
67
68
69
70
<?php namespace MeowCrew\RoleAndCustomerBasedPricing\GlobalRoleSpecificPricing\CPT\Actions;
use MeowCrew\RoleAndCustomerBasedPricing\Core\AdminNotifier;
use MeowCrew\RoleAndCustomerBasedPricing\Core\ServiceContainerTrait;
use MeowCrew\RoleAndCustomerBasedPricing\Entity\GlobalPricingRule;
use WP_Post;
class SuspendAction {
const ACTION = 'rcbp_suspend_global_rule';
use ServiceContainerTrait;
public function __construct() {
add_action( 'admin_post_' . self::ACTION, array( $this, 'handle' ) );
add_filter( 'post_row_actions', function ( $actions, WP_Post $post ) {
$rule = GlobalPricingRule::build( $post->ID );
if ( ! $rule->isSuspended() ) {
$actions['suspend'] = sprintf( '<a href="%s">%s</a>', $this->getRunLink( $post->ID ), $this->getName() );
}
return $actions;
}, 10, 2 );
}
public function getName() {
return __( 'Suspend', 'role-and-customer-based-pricing-for-woocommerce' );
}
public function handle() {
$nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( $_GET['_wpnonce'] ) : false;
if ( wp_verify_nonce( $nonce, self::ACTION ) ) {
$ruleId = isset( $_GET['rule_id'] ) ? intval( $_GET['rule_id'] ) : false;
if ( $ruleId ) {
$rule = GlobalPricingRule::build( $ruleId );
if ( false !== get_post_status( $ruleId ) ) {
$rule->suspend();
try {
GlobalPricingRule::save( $rule, $ruleId );
$this->getContainer()->getAdminNotifier()->flash( __( 'The rule suspended successfully.', 'role-and-customer-based-pricing-for-woocommerce' ), AdminNotifier::SUCCESS, true );
} catch ( \Exception $e ) {
wp_die( 'Invalid rule to suspend.' );
}
}
}
} else {
wp_die( 'You\'re not allowed to run this action' );
}
return wp_safe_redirect( wp_get_referer() );
}
public function getRunLink( $id ) {
return add_query_arg( array(
'rule_id' => $id,
'action' => self::ACTION,
), wp_nonce_url( admin_url( 'admin-post.php' ), self::ACTION ) );
}
}