| 1 |
<?php namespace TierPricingTable\Addons\GlobalTieredPricing\CPT\Actions; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\GlobalTieredPricingCPT; |
| 4 |
use TierPricingTable\Core\AdminNotifier; |
| 5 |
use TierPricingTable\Core\ServiceContainerTrait; |
| 6 |
use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule; |
| 7 |
use WP_Post; |
| 8 |
|
| 9 |
class ReactivateAction { |
| 10 |
|
| 11 |
const ACTION = 'tpt_reactivate_global_rule'; |
| 12 |
|
| 13 |
use ServiceContainerTrait; |
| 14 |
|
| 15 |
public function __construct() { |
| 16 |
|
| 17 |
add_action( 'admin_post_' . self::ACTION, array( $this, 'handle' ) ); |
| 18 |
|
| 19 |
add_filter( 'post_row_actions', function ( $actions, WP_Post $post ) { |
| 20 |
|
| 21 |
if ( GlobalTieredPricingCPT::SLUG !== $post->post_type ) { |
| 22 |
return $actions; |
| 23 |
} |
| 24 |
|
| 25 |
$rule = GlobalPricingRule::build( $post->ID ); |
| 26 |
|
| 27 |
if ( $rule->isSuspended() ) { |
| 28 |
$actions['reactivate'] = sprintf( '<a href="%s">%s</a>', $this->getRunLink( $post->ID ), $this->getName() ); |
| 29 |
} |
| 30 |
|
| 31 |
return $actions; |
| 32 |
}, 10, 2 ); |
| 33 |
} |
| 34 |
|
| 35 |
public function getName(): string { |
| 36 |
return __( 'Reactivate', 'tier-pricing-table' ); |
| 37 |
} |
| 38 |
|
| 39 |
public function handle(): bool { |
| 40 |
$nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( $_GET['_wpnonce'] ) : false; |
| 41 |
|
| 42 |
if ( wp_verify_nonce( $nonce, self::ACTION ) ) { |
| 43 |
$ruleId = isset( $_GET['rule_id'] ) ? intval( $_GET['rule_id'] ) : false; |
| 44 |
|
| 45 |
if ( $ruleId ) { |
| 46 |
$rule = GlobalPricingRule::build( $ruleId ); |
| 47 |
|
| 48 |
if ( false !== get_post_status( $ruleId ) ) { |
| 49 |
|
| 50 |
$rule->reactivate(); |
| 51 |
$rule->save(); |
| 52 |
|
| 53 |
$this->getContainer()->getAdminNotifier()->flash( __( 'The rule reactivated successfully.', 'tier-pricing-table |
| 54 |
' ), AdminNotifier::SUCCESS, true ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
} else { |
| 59 |
wp_die( 'You\'re not allowed to run this action' ); |
| 60 |
} |
| 61 |
|
| 62 |
return wp_safe_redirect( wp_get_referer() ); |
| 63 |
} |
| 64 |
|
| 65 |
public function getRunLink( $id ): string { |
| 66 |
return add_query_arg( array( |
| 67 |
'rule_id' => $id, |
| 68 |
'action' => self::ACTION, |
| 69 |
), wp_nonce_url( admin_url( 'admin-post.php' ), self::ACTION ) ); |
| 70 |
} |
| 71 |
} |
| 72 |
|