PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.3.1
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.3.1
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.3.1, at includes/CampaignShortcode.php

112 lines 2.9 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 // TODO: Better to use `hurryt_get_campaign`.
59 if ( ! get_post( $attrs[ 'id' ] ) ) {
60 return __( 'HurryTimer: Invalid campaign ID.', 'hurrytimer' );
61 }
62
63 $campaign = hurryt_get_campaign( $attrs[ 'id' ] );
64
65 if( $attrs['sticky'] && ! $campaign->show_sticky_on_page( hurryt_current_page_id() )){
66 return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() );
67 }
68
69 if ( !$campaign->is_running() ) {
70
71 // Let developers show something else whenever the campaign isn't running.
72 return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() );
73 }
74
75 // Let developers decide whether to show the campaign or not.
76 if ( ! apply_filters( 'hurryt_show_campaign', true, $campaign->get_id() ) ) {
77 return '';
78 }
79
80
81 return $return;
82 }
83
84 /**
85 * Return shortcode content.
86 *
87 * @param $attrs
88 *
89 * @return string
90 */
91 public function content( $attrs )
92 {
93
94 $attrs = wp_parse_args( $attrs, $this->default_attrs);
95
96 $attrs = filter_var_array($attrs, $this->attrs_filter_def);
97
98 $campaign = hurryt_get_campaign( absint( $attrs[ 'id' ] ) );
99
100 if ( $campaign->is_expired() ) {
101 (new ActionManager( $campaign ))->run();
102 }
103
104 // TODO: Move `build_template` to `TemplateBuilder->build(int $campaign_id)`.
105 $template = apply_filters( 'hurryt_campaign_template', $campaign->build_template(), $campaign->get_id() );
106
107 return !empty( $template ) ? $campaign->wrap_template( $template, $attrs) : '';
108 }
109
110
111 }
112