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

143 lines 3.7 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 use \WP_Post;
6
7 class CampaignShortcode
8 {
9
10 /**
11 * Return shortcode content.
12 *
13 * @param $attrs
14 *
15 * @return string
16 */
17 function content($attrs)
18 {
19 $id = $this->getId($attrs);
20
21 if ($id === null) {
22 return '';
23 }
24 $campaign = new Campaign($id);
25
26 $campaign->loadSettings();
27
28 // Don't render the shortcode if the campaign's inactive.
29 if ( ! $campaign->isActive() || $campaign->isScheduled()
30 || ! $campaign->showStickyBarOn($this->getCurrentPageId())
31 ) {
32
33 return '';
34 }
35
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 '';
41 }
42
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();
47 }
48
49
50 $template = apply_filters('hurryt_campaign_template', $campaign->buildTemplate());
51
52 return $template === '' ? '': $campaign->wrapTemplate($template);
53
54 }
55
56
57 /**
58 * Returns campaign ID if set.
59 *
60 * @param $attrs
61 *
62 * @return int|null
63 */
64 function getId($attrs)
65 {
66 if ( ! $this->hasId($attrs)) {
67 return null;
68 }
69
70 $id = intval($attrs['id']);
71
72 if ( ! $this->campaignExists($id)) {
73 return null;
74 }
75
76 return $id;
77 }
78
79 function getCurrentPageId()
80 {
81
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 }
116
117 return $objectId;
118 }
119
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
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 }