| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
use \WP_Post; |
| 6 |
|
| 7 |
class CampaignShortcode |
| 8 |
{ |
| 9 |
|
| 10 |
/** |
| 11 |
* Return shortcode content. |
| 12 |
* |
| 13 |
* @param $attrs |
| 14 |
* |
| 15 |
* @return string |
| 16 |
*/ |
| 17 |
function content($attrs) |
| 18 |
{ |
| 19 |
$id = $this->getId($attrs); |
| 20 |
|
| 21 |
if ($id === null) { |
| 22 |
return ''; |
| 23 |
} |
| 24 |
|
| 25 |
$campaign = new Campaign($id); |
| 26 |
$campaign->loadSettings(); |
| 27 |
// Don't render the shortcode if the campaign's inactive. |
| 28 |
if ( ! $campaign->isActive() || $campaign->isScheduled()) { |
| 29 |
return ''; |
| 30 |
} |
| 31 |
// Run campaign's actions if expired. |
| 32 |
if ($campaign->isRegular() && $campaign->isExpired()) { |
| 33 |
$actionManager = new ActionManager($campaign); |
| 34 |
$actionManager->run(); |
| 35 |
} |
| 36 |
|
| 37 |
$template = apply_filters('hurryt_campaign_template', $campaign->buildTemplate()); |
| 38 |
return $campaign->wrapTemplate($template); |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns campaign ID if set. |
| 44 |
* |
| 45 |
* @param $attrs |
| 46 |
* |
| 47 |
* @return int|null |
| 48 |
*/ |
| 49 |
function getId($attrs) |
| 50 |
{ |
| 51 |
if ( ! $this->hasId($attrs)) { |
| 52 |
return null; |
| 53 |
} |
| 54 |
|
| 55 |
$id = intval($attrs['id']); |
| 56 |
|
| 57 |
if ( ! $this->campaignExists($id)) { |
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
return $id; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Verify if the given attributes contain the campaign ID. |
| 66 |
* |
| 67 |
* @param $attrs |
| 68 |
* |
| 69 |
* @return bool |
| 70 |
*/ |
| 71 |
function hasId($attrs) |
| 72 |
{ |
| 73 |
return isset($attrs['id']); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Verify if the given campaign exists in database. |
| 78 |
* |
| 79 |
* @param $id |
| 80 |
* |
| 81 |
* @return boolean |
| 82 |
*/ |
| 83 |
function campaignExists($id) |
| 84 |
{ |
| 85 |
return get_post($id) instanceof WP_Post; |
| 86 |
} |
| 87 |
} |