| @@ -1,87 +1,110 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Hurrytimer; |
| 4 | 4 | |
| 5 | -use \WP_Post; | |
| 6 | - | |
| 7 | 5 | class CampaignShortcode |
| 8 | 6 | { |
| 9 | 7 | |
| 10 | 8 | /** |
| 11 | - * Return shortcode content. | |
| 9 | + * @var array The default shortcode attributes. | |
| 10 | + */ | |
| 11 | + protected $default_attrs = [ | |
| 12 | + 'id' => null, | |
| 13 | + 'run_in_background' => false, | |
| 14 | + 'sticky' => false | |
| 15 | + ]; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * @var array The attributes array filter. | |
| 19 | + */ | |
| 20 | + protected $attrs_filter_def = [ | |
| 21 | + 'id' => FILTER_VALIDATE_INT, | |
| 22 | + 'run_in_background' => FILTER_VALIDATE_BOOLEAN, | |
| 23 | + 'sticky' => FILTER_VALIDATE_BOOLEAN | |
| 24 | + ]; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Init shortcode | |
| 28 | + */ | |
| 29 | + function init() | |
| 30 | + { | |
| 31 | + add_shortcode( 'hurrytimer', [ $this, 'content' ] ); | |
| 32 | + add_filter( 'pre_do_shortcode_tag', [ $this, 'maybe_display' ], 10, 3 ); | |
| 33 | + } | |
| 34 | + | |
| 35 | + /** | |
| 12 | 36 | * |
| 13 | - * @param $attrs | |
| 37 | + * @param $return | |
| 38 | + * @param $tag | |
| 39 | + * @param $attr | |
| 14 | 40 | * |
| 15 | - * @return string | |
| 41 | + * @return mixed|string|void | |
| 16 | 42 | */ |
| 17 | - function content($attrs) | |
| 43 | + function maybe_display( $return, $tag, $attrs ) | |
| 18 | 44 | { |
| 19 | - $id = $this->getId($attrs); | |
| 45 | + if ( $tag !== 'hurrytimer' ) { | |
| 46 | + return $return; | |
| 47 | + } | |
| 20 | 48 | |
| 21 | - if ($id === null) { | |
| 22 | - return ''; | |
| 49 | + | |
| 50 | + $attrs = wp_parse_args( $attrs, $this->default_attrs ); | |
| 51 | + | |
| 52 | + $attrs = filter_var_array( $attrs, $this->attrs_filter_def ); | |
| 53 | + | |
| 54 | + if ( empty( $attrs[ 'id' ] ) ) { | |
| 55 | + return __( 'HurryTimer: Invalid campaign ID.', 'hurrytimer' ); | |
| 23 | 56 | } |
| 24 | 57 | |
| 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()) { | |
| 58 | + if ( !get_post( $attrs[ 'id' ] ) ) { | |
| 59 | + return __( 'HurryTimer: Invalid campaign ID.', 'hurrytimer' ); | |
| 60 | + } | |
| 61 | + | |
| 62 | + $campaign = hurryt_get_campaign( $attrs[ 'id' ] ); | |
| 63 | + | |
| 64 | + if ( $attrs[ 'sticky' ] && !$campaign->show_sticky_on_page( hurryt_current_page_id() ) ) { | |
| 65 | + return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() ); | |
| 66 | + } | |
| 67 | + | |
| 68 | + if ( !$campaign->is_running() ) { | |
| 69 | + | |
| 70 | + // Let developers show something else whenever the campaign isn't running. | |
| 71 | + return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() ); | |
| 72 | + } | |
| 73 | + | |
| 74 | + // Let developers decide whether to show the campaign or not. | |
| 75 | + if ( !apply_filters( 'hurryt_show_campaign', true, $campaign->get_id() ) ) { | |
| 29 | 76 | return ''; |
| 30 | 77 | } |
| 31 | - // Run campaign's actions if expired. | |
| 32 | - if ($campaign->isRegular() && $campaign->isExpired()) { | |
| 33 | - $actionManager = new ActionManager($campaign); | |
| 34 | - $actionManager->run(); | |
| 35 | - } | |
| 36 | 78 | |
| 37 | - $template = apply_filters('hurryt_campaign_template', $campaign->buildTemplate()); | |
| 38 | - return $campaign->wrapTemplate($template); | |
| 39 | 79 | |
| 80 | + return $return; | |
| 40 | 81 | } |
| 41 | 82 | |
| 42 | 83 | /** |
| 43 | - * Returns campaign ID if set. | |
| 84 | + * Return shortcode content. | |
| 44 | 85 | * |
| 45 | 86 | * @param $attrs |
| 46 | 87 | * |
| 47 | - * @return int|null | |
| 88 | + * @return string | |
| 48 | 89 | */ |
| 49 | - function getId($attrs) | |
| 90 | + public function content( $attrs ) | |
| 50 | 91 | { |
| 51 | - if ( ! $this->hasId($attrs)) { | |
| 52 | - return null; | |
| 53 | - } | |
| 54 | 92 | |
| 55 | - $id = intval($attrs['id']); | |
| 93 | + $attrs = wp_parse_args( $attrs, $this->default_attrs ); | |
| 56 | 94 | |
| 57 | - if ( ! $this->campaignExists($id)) { | |
| 58 | - return null; | |
| 95 | + $attrs = filter_var_array( $attrs, $this->attrs_filter_def ); | |
| 96 | + | |
| 97 | + $campaign = hurryt_get_campaign( absint( $attrs[ 'id' ] ) ); | |
| 98 | + | |
| 99 | + if ( $campaign->is_expired() ) { | |
| 100 | + ( new ActionManager( $campaign ) )->run(); | |
| 59 | 101 | } |
| 60 | 102 | |
| 61 | - return $id; | |
| 103 | + // TODO: Move `build_template` to `TemplateBuilder->build(int $campaign_id)`. | |
| 104 | + $template = apply_filters( 'hurryt_campaign_template', $campaign->build_template(), $campaign->get_id() ); | |
| 105 | + | |
| 106 | + return !empty( $template ) ? $campaign->wrap_template( $template, $attrs ) : ''; | |
| 62 | 107 | } |
| 63 | 108 | |
| 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 | 109 | |
| 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 | -} | |
| 110 | +} | |