| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hurrytimer; |
| 4 |
|
| 5 |
class CampaignShortcode |
| 6 |
{ |
| 7 |
|
| 8 |
/** |
| 9 |
* Init shortcode |
| 10 |
*/ |
| 11 |
function init() |
| 12 |
{ |
| 13 |
add_shortcode( 'hurrytimer', [ $this, 'content' ] ); |
| 14 |
add_filter( 'pre_do_shortcode_tag', [ $this, 'maybe_display' ], 10, 3 ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* |
| 19 |
* @param $return |
| 20 |
* @param $tag |
| 21 |
* @param $attr |
| 22 |
* |
| 23 |
* @return mixed|string|void |
| 24 |
*/ |
| 25 |
function maybe_display( $return, $tag, $attr ) |
| 26 |
{ |
| 27 |
|
| 28 |
if ( $tag !== 'hurrytimer' ) { |
| 29 |
return $return; |
| 30 |
} |
| 31 |
$id = wp_parse_args( $attr, [ 'id' => -1 ] )[ 'id' ]; |
| 32 |
|
| 33 |
// Nothing to display if the campaign doesn't exist. |
| 34 |
if ( !get_post( $id ) ) { |
| 35 |
return __( 'HurryTimer: Invalid campaign ID.', 'hurrytimer' ); |
| 36 |
} |
| 37 |
$campaign = hurryt_get_campaign( $id ); |
| 38 |
|
| 39 |
if ( !$campaign->is_running() ) { |
| 40 |
|
| 41 |
// Show something else. |
| 42 |
return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() ); |
| 43 |
} |
| 44 |
|
| 45 |
// Let developer decide whether to show the campaign or not. |
| 46 |
if ( !apply_filters( 'hurryt_show_campaign', true, $campaign->get_id() ) ) { |
| 47 |
return ''; |
| 48 |
} |
| 49 |
|
| 50 |
return $return; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Return shortcode content. |
| 55 |
* |
| 56 |
* @param $attrs |
| 57 |
* |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function content( $attrs ) |
| 61 |
{ |
| 62 |
|
| 63 |
$campaign = hurryt_get_campaign( absint( $attrs[ 'id' ] ) ); |
| 64 |
|
| 65 |
if ( $campaign->is_expired() ) { |
| 66 |
(new ActionManager( $campaign ))->run(); |
| 67 |
} |
| 68 |
|
| 69 |
$template = apply_filters( 'hurryt_campaign_template', $campaign->build_template(), $campaign->get_id() ); |
| 70 |
|
| 71 |
return !empty( $template ) ? $campaign->wrap_template( $template ) : ''; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
} |
| 76 |
|