| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
/** |
| 6 |
* |
| 7 |
* This class handle actions executions |
| 8 |
* |
| 9 |
* Class ActionManager |
| 10 |
* |
| 11 |
* @package Hurrytimer |
| 12 |
*/ |
| 13 |
class ActionManager |
| 14 |
{ |
| 15 |
/** |
| 16 |
* @var Campaign |
| 17 |
*/ |
| 18 |
protected $campaign; |
| 19 |
|
| 20 |
public function __construct( $campaign ) |
| 21 |
{ |
| 22 |
$this->campaign = $campaign; |
| 23 |
add_filter( 'wp_insert_post_data', function ( $data ) { |
| 24 |
global $hurryt_saving_post; |
| 25 |
$hurryt_saving_post = true; |
| 26 |
|
| 27 |
return $data; |
| 28 |
} ); |
| 29 |
add_action( 'save_post', function () { |
| 30 |
global $hurryt_saving_post; |
| 31 |
$hurryt_saving_post = false; |
| 32 |
} ); |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
function is_disabled() |
| 37 |
{ |
| 38 |
$disable_actions = hurryt_is_admin_area() && hurryt_settings()[ 'disable_actions' ]; |
| 39 |
return filter_var( apply_filters( 'hurryt_disable_actions', $disable_actions ), FILTER_VALIDATE_BOOLEAN ); |
| 40 |
} |
| 41 |
|
| 42 |
public function run() |
| 43 |
{ |
| 44 |
|
| 45 |
if ( $this->is_disabled() ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @deprecated 2.3.0 Use `hurryt_campaign_finished` instead. |
| 51 |
*/ |
| 52 |
do_action( "hurryt_{$this->campaign->get_id()}_campaign_ended", $this->campaign ); |
| 53 |
|
| 54 |
do_action( "hurryt_campaign_finished", $this->campaign->get_id() ); |
| 55 |
|
| 56 |
foreach ( $this->campaign->actions as $action ) { |
| 57 |
switch ( $action[ 'id' ] ) { |
| 58 |
|
| 59 |
case C::ACTION_REDIRECT; |
| 60 |
$this->redirect_to( $action[ 'redirectUrl' ] ); |
| 61 |
break; |
| 62 |
case C::ACTION_CHANGE_STOCK_STATUS: |
| 63 |
$this->change_stock_status( $action[ 'wcStockStatus' ] ); |
| 64 |
break; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
function redirect_to( $url ) |
| 70 |
{ |
| 71 |
global $hurryt_saving_post; |
| 72 |
if ( $hurryt_saving_post ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
if ( !empty( trim( $url ) ) ) { |
| 77 |
wp_redirect( esc_url_raw( $url ) ); |
| 78 |
exit; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
function change_stock_status( $stock_status ) |
| 83 |
{ |
| 84 |
$wc_campaign = new WCCampaign(); |
| 85 |
$wc_campaign->change_stock_status( $this->campaign, $stock_status ); |
| 86 |
|
| 87 |
return; |
| 88 |
} |
| 89 |
} |
| 90 |
|