PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.7.2
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.7.2
2.15.0 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 All 62 releases
hurrytimer / includes / CampaignShortcode.php

CampaignShortcode.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.7.2, at includes/CampaignShortcode.php

111 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hurrytimer;
4
5 class CampaignShortcode
6 {
7
8 /**
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 /**
36 *
37 * @param $return
38 * @param $tag
39 * @param $attr
40 *
41 * @return mixed|string|void
42 */
43 function maybe_display( $return, $tag, $attrs )
44 {
45 if ( $tag !== 'hurrytimer' ) {
46 return $return;
47 }
48
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' );
56 }
57
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() ) ) {
76 return '';
77 }
78
79
80 return $return;
81 }
82
83 /**
84 * Return shortcode content.
85 *
86 * @param $attrs
87 *
88 * @return string
89 */
90 public function content( $attrs )
91 {
92
93 $attrs = wp_parse_args( $attrs, $this->default_attrs );
94
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();
101 }
102
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 ) : '';
107 }
108
109
110 }
111