| @@ -1,49 +1,89 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace Hurrytimer; |
| 3 | 4 | |
| 5 | +/** | |
| 6 | + * | |
| 7 | + * This class handle actions executions | |
| 8 | + * | |
| 9 | + * Class ActionManager | |
| 10 | + * | |
| 11 | + * @package Hurrytimer | |
| 12 | + */ | |
| 4 | 13 | class ActionManager |
| 5 | 14 | { |
| 6 | 15 | /** |
| 7 | - * @var \Hurrytimer\Campaign | |
| 16 | + * @var Campaign | |
| 8 | 17 | */ |
| 9 | 18 | protected $campaign; |
| 10 | 19 | |
| 11 | - public function __construct($campaign) | |
| 20 | + public function __construct( $campaign ) | |
| 12 | 21 | { |
| 13 | 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 | + | |
| 14 | 34 | } |
| 15 | 35 | |
| 16 | - function redirect($url) | |
| 36 | + function is_disabled() | |
| 17 | 37 | { |
| 18 | - ob_start(); | |
| 19 | - header("Location: $url"); | |
| 20 | - exit; | |
| 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 ); | |
| 21 | 40 | } |
| 22 | 41 | |
| 23 | - function run() | |
| 42 | + public function run() | |
| 24 | 43 | { |
| 25 | - foreach ($this->campaign->actions as $action) { | |
| 26 | - switch ($action['id']) { | |
| 27 | - case C::ACTION_HIDE: | |
| 28 | - add_filter('hurryt_campaign_template', function () { return ''; }, 1); | |
| 29 | - break; | |
| 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 | + | |
| 30 | 59 | case C::ACTION_REDIRECT; |
| 31 | - $this->redirect($action['redirectUrl']); | |
| 60 | + $this->redirect_to( $action[ 'redirectUrl' ] ); | |
| 32 | 61 | break; |
| 33 | 62 | case C::ACTION_CHANGE_STOCK_STATUS: |
| 34 | - $wcCampaign = new WCCampaign(); | |
| 35 | - $wcCampaign->changeStockStatus($this->campaign, $action['wcStockStatus']); | |
| 63 | + $this->change_stock_status( $action[ 'wcStockStatus' ] ); | |
| 36 | 64 | break; |
| 37 | - case C::ACTION_DISPLAY_MESSAGE: | |
| 38 | - add_filter('hurryt_campaign_template', | |
| 39 | - function ($template) use ($action) { | |
| 40 | - return | |
| 41 | - "<div class='hurrytimer-campaign-message'>{$action['message']}</div>"; | |
| 42 | - }, 2); | |
| 43 | - break; | |
| 44 | 65 | } |
| 45 | 66 | } |
| 46 | 67 | } |
| 47 | 68 | |
| 69 | + function redirect_to( $url ) | |
| 70 | + { | |
| 71 | + global $hurryt_saving_post; | |
| 72 | + if ( $hurryt_saving_post ) { | |
| 73 | + return; | |
| 74 | + } | |
| 48 | 75 | |
| 49 | -} | |
| 76 | + if ( !empty( trim( $url ) ) ) { | |
| 77 | + wp_redirect( $url ); | |
| 78 | + return; | |
| 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 | +} | |