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

142 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 return '';
33 }
34
35 // We must check if the campaign is sticky
36 // If yes, we don't need to run actions as the campaign is no longer active.
37 // Running actions for dismissed campaigns is bad for UX, so we avoid it.
38 if($campaign->isDismissed()){
39 return '';
40 }
41
42 // If it's a regular campaign and expired, run selected actions.
43 if ($campaign->isRegular() && $campaign->isExpired()) {
44 $actionManager = new ActionManager($campaign);
45 $actionManager->run();
46 }
47
48
49 $template = apply_filters('hurryt_campaign_template', $campaign->buildTemplate());
50
51 return $template === '' ? '': $campaign->wrapTemplate($template);
52
53 }
54
55
56 /**
57 * Returns campaign ID if set.
58 *
59 * @param $attrs
60 *
61 * @return int|null
62 */
63 function getId($attrs)
64 {
65 if ( ! $this->hasId($attrs)) {
66 return null;
67 }
68
69 $id = intval($attrs['id']);
70
71 if ( ! $this->campaignExists($id)) {
72 return null;
73 }
74
75 return $id;
76 }
77
78 function getCurrentPageId()
79 {
80
81 $objectId = get_queried_object_id();
82 if(! Utils\Helpers::isWcActive()){
83 return $objectId;
84 }
85 $wcIds = [
86 'shop' => get_option('woocommerce_shop_page_id'),
87 'cart' => get_option('woocommerce_cart_page_id'),
88 'checkout' => get_option('woocommerce_checkout_page_id'),
89 'checkout_pay' => get_option('woocommerce_pay_page_id'),
90 'thanks' => get_option('woocommerce_thanks_page_id'),
91 'myaccount' => get_option('woocommerce_myaccount_page_id'),
92 'edit_address' => get_option('woocommerce_edit_address_page_id'),
93 'view_order' => get_option('woocommerce_view_order_page_id'),
94 'terms' => get_option('woocommerce_terms_page_id'),
95 ];
96 if (is_shop()) {
97 $objectId = $wcIds['shop'];
98 } elseif (is_account_page()) {
99 $objectId = $wcIds['myaccount'];
100 } elseif (is_checkout_pay_page()) {
101 $objectId = $wcIds['checkout_pay'];
102 } elseif (is_checkout()) {
103 $objectId = $wcIds['checkout'];
104 } elseif (is_cart()) {
105 $objectId = $wcIds['cart'];
106 } elseif (is_view_order_page()) {
107 $objectId = $wcIds['view_order'];
108 } elseif (is_view_order_page()) {
109 $objectId = $wcIds['view_order'];
110 } elseif (is_view_order_page()) {
111 $objectId = $wcIds['view_order'];
112 } elseif (is_view_order_page()) {
113 $objectId = $wcIds['view_order'];
114 }
115
116 return $objectId;
117 }
118
119 /**
120 * Verify if the given attributes contain the campaign ID.
121 *
122 * @param $attrs
123 *
124 * @return bool
125 */
126 function hasId($attrs)
127 {
128 return isset($attrs['id']);
129 }
130
131 /**
132 * Verify if the given campaign exists in database.
133 *
134 * @param $id
135 *
136 * @return boolean
137 */
138 function campaignExists($id)
139 {
140 return get_post($id) instanceof WP_Post;
141 }
142 }