PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.6.2
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.6.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
← All changes | includes/CampaignShortcode.php +69 -102 2.1.72.6.2 View file →
@@ -1,143 +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 - $campaign = new Campaign($id);
25 57
26 - $campaign->loadSettings();
58 + if ( !get_post( $attrs[ 'id' ] ) ) {
59 + return __( 'HurryTimer: Invalid campaign ID.', 'hurrytimer' );
60 + }
27 61
28 - // Don't render the shortcode if the campaign's inactive.
29 - if ( ! $campaign->isActive() || $campaign->isScheduled()
30 - || ! $campaign->showStickyBarOn($this->getCurrentPageId())
31 - ) {
62 + $campaign = hurryt_get_campaign( $attrs[ 'id' ] );
32 63
33 - return '';
64 + if ( $attrs[ 'sticky' ] && !$campaign->show_sticky_on_page( hurryt_current_page_id() ) ) {
65 + return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() );
34 66 }
35 67
36 - // We must check if the campaign is sticky
37 - // If yes, we don't need to run actions as the campaign is no longer active.
38 - // Running actions for dismissed campaigns is bad for UX, so we avoid it.
39 - if($campaign->isDismissed()){
40 - return '';
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() );
41 72 }
42 73
43 - // If it's a regular campaign and expired, run selected actions.
44 - if ($campaign->isRegular() && $campaign->isExpired()) {
45 - $actionManager = new ActionManager($campaign);
46 - $actionManager->run();
74 + // Let developers decide whether to show the campaign or not.
75 + if ( !apply_filters( 'hurryt_show_campaign', true, $campaign->get_id() ) ) {
76 + return '';
47 77 }
48 78
49 79
50 - $template = apply_filters('hurryt_campaign_template', $campaign->buildTemplate());
51 -
52 - return $template === '' ? '': $campaign->wrapTemplate($template);
53 -
80 + return $return;
54 81 }
55 82
56 -
57 83 /**
58 - * Returns campaign ID if set.
84 + * Return shortcode content.
59 85 *
60 86 * @param $attrs
61 87 *
62 - * @return int|null
88 + * @return string
63 89 */
64 - function getId($attrs)
90 + public function content( $attrs )
65 91 {
66 - if ( ! $this->hasId($attrs)) {
67 - return null;
68 - }
69 92
70 - $id = intval($attrs['id']);
93 + $attrs = wp_parse_args( $attrs, $this->default_attrs );
71 94
72 - if ( ! $this->campaignExists($id)) {
73 - return null;
74 - }
95 + $attrs = filter_var_array( $attrs, $this->attrs_filter_def );
75 96
76 - return $id;
77 - }
97 + $campaign = hurryt_get_campaign( absint( $attrs[ 'id' ] ) );
78 98
79 - function getCurrentPageId()
80 - {
99 + if ( $campaign->is_expired() ) {
100 + ( new ActionManager( $campaign ) )->run();
101 + }
81 102
82 - $objectId = get_queried_object_id();
83 - if(! Utils\Helpers::isWcActive()){
84 - return $objectId;
85 - }
86 - $wcIds = [
87 - 'shop' => get_option('woocommerce_shop_page_id'),
88 - 'cart' => get_option('woocommerce_cart_page_id'),
89 - 'checkout' => get_option('woocommerce_checkout_page_id'),
90 - 'checkout_pay' => get_option('woocommerce_pay_page_id'),
91 - 'thanks' => get_option('woocommerce_thanks_page_id'),
92 - 'myaccount' => get_option('woocommerce_myaccount_page_id'),
93 - 'edit_address' => get_option('woocommerce_edit_address_page_id'),
94 - 'view_order' => get_option('woocommerce_view_order_page_id'),
95 - 'terms' => get_option('woocommerce_terms_page_id'),
96 - ];
97 - if (is_shop()) {
98 - $objectId = $wcIds['shop'];
99 - } elseif (is_account_page()) {
100 - $objectId = $wcIds['myaccount'];
101 - } elseif (is_checkout_pay_page()) {
102 - $objectId = $wcIds['checkout_pay'];
103 - } elseif (is_checkout()) {
104 - $objectId = $wcIds['checkout'];
105 - } elseif (is_cart()) {
106 - $objectId = $wcIds['cart'];
107 - } elseif (is_view_order_page()) {
108 - $objectId = $wcIds['view_order'];
109 - } elseif (is_view_order_page()) {
110 - $objectId = $wcIds['view_order'];
111 - } elseif (is_view_order_page()) {
112 - $objectId = $wcIds['view_order'];
113 - } elseif (is_view_order_page()) {
114 - $objectId = $wcIds['view_order'];
115 - }
103 + // TODO: Move `build_template` to `TemplateBuilder->build(int $campaign_id)`.
104 + $template = apply_filters( 'hurryt_campaign_template', $campaign->build_template(), $campaign->get_id() );
116 105
117 - return $objectId;
106 + return !empty( $template ) ? $campaign->wrap_template( $template, $attrs ) : '';
118 107 }
119 108
120 - /**
121 - * Verify if the given attributes contain the campaign ID.
122 - *
123 - * @param $attrs
124 - *
125 - * @return bool
126 - */
127 - function hasId($attrs)
128 - {
129 - return isset($attrs['id']);
130 - }
131 109
132 - /**
133 - * Verify if the given campaign exists in database.
134 - *
135 - * @param $id
136 - *
137 - * @return boolean
138 - */
139 - function campaignExists($id)
140 - {
141 - return get_post($id) instanceof WP_Post;
142 - }
143 -}
110 +}