| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
|
| 6 |
/** |
| 7 |
* |
| 8 |
* This class handle actions executions |
| 9 |
* |
| 10 |
* Class ActionManager |
| 11 |
* |
| 12 |
* @package Hurrytimer |
| 13 |
*/ |
| 14 |
class ActionManager |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @var Campaign |
| 18 |
*/ |
| 19 |
protected $campaign; |
| 20 |
|
| 21 |
public function __construct( $campaign ) |
| 22 |
{ |
| 23 |
$this->campaign = $campaign; |
| 24 |
add_filter( 'wp_insert_post_data', function ( $data ) { |
| 25 |
global $hurryt_saving_post; |
| 26 |
$hurryt_saving_post = true; |
| 27 |
|
| 28 |
return $data; |
| 29 |
} ); |
| 30 |
add_action( 'save_post', function () { |
| 31 |
global $hurryt_saving_post; |
| 32 |
$hurryt_saving_post = false; |
| 33 |
} ); |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
function is_enabled() |
| 38 |
{ |
| 39 |
return !( hurryt_is_admin_area() && hurryt_settings()[ 'disable_actions' ] ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
public function run() |
| 44 |
{ |
| 45 |
|
| 46 |
if ( !$this->is_enabled() ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
do_action( "hurryt_{$this->campaign->get_id()}_campaign_ended", $this->campaign ); |
| 51 |
|
| 52 |
foreach ( $this->campaign->actions as $action ) { |
| 53 |
switch ( $action[ 'id' ] ) { |
| 54 |
case C::ACTION_HIDE: |
| 55 |
$this->hide_campaign(); |
| 56 |
break; |
| 57 |
case C::ACTION_REDIRECT; |
| 58 |
$this->redirect_to( $action[ 'redirectUrl' ] ); |
| 59 |
break; |
| 60 |
case C::ACTION_CHANGE_STOCK_STATUS: |
| 61 |
$this->change_stock_status( $action[ 'wcStockStatus' ] ); |
| 62 |
break; |
| 63 |
case C::ACTION_DISPLAY_MESSAGE: |
| 64 |
$this->display_message( $action[ 'message' ] ); |
| 65 |
break; |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
function hide_campaign() |
| 71 |
{ |
| 72 |
add_filter( 'hurryt_campaign_template', function ( $template, $campaign_id ) { |
| 73 |
if ( $campaign_id === $this->campaign->get_id() ) { |
| 74 |
return ''; |
| 75 |
} |
| 76 |
return $template; |
| 77 |
}, 10, 2 ); |
| 78 |
} |
| 79 |
|
| 80 |
function redirect_to( $url ) |
| 81 |
{ |
| 82 |
global $hurryt_saving_post; |
| 83 |
if ( $hurryt_saving_post ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
wp_redirect( $url ); |
| 87 |
|
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
function change_stock_status( $stock_status ) |
| 92 |
{ |
| 93 |
$wc_campaign = new WCCampaign(); |
| 94 |
$wc_campaign->change_stock_status( $this->campaign, $stock_status ); |
| 95 |
|
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
function display_message( $message ) |
| 100 |
{ |
| 101 |
add_filter( 'hurryt_campaign_template', |
| 102 |
function ( $template, $campaign_id ) use ( $message ) { |
| 103 |
if ( $campaign_id === $this->campaign->get_id() ) { |
| 104 |
return "<div class='hurrytimer-campaign-message'>{$message}</div>"; |
| 105 |
} |
| 106 |
return $template; |
| 107 |
}, 10, 2 ); |
| 108 |
|
| 109 |
return; |
| 110 |
} |
| 111 |
} |
| 112 |
|