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

137 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 public 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 if ( ! $campaign->isActive()
28 || $campaign->isScheduled()
29 || ! $campaign->showStickyBarOn($this->getCurrentPageId())
30 || $campaign->isStickyDismissed()
31 || ($campaign->isRecurring() && $campaign->getRecurringDeadline() === null)
32 ) {
33 return '';
34 }
35
36 $isOneTimeCampaignExpired = $campaign->isRegular() && $campaign->isExpired();
37 $isRecurringCampaignExpired = $campaign->isRecurring() && $campaign->isRecurringExpired();
38
39 if ($isRecurringCampaignExpired || $isOneTimeCampaignExpired) {
40 $actionManager = new ActionManager($campaign);
41 $actionManager->run();
42 }
43
44 $template = apply_filters('hurryt_campaign_template', $campaign->buildTemplate());
45
46 return $template === '' ? '' : $campaign->wrapTemplate($template);
47
48 }
49
50 /**
51 * Returns campaign ID if set.
52 *
53 * @param $attrs
54 *
55 * @return int|null
56 */
57 public function getId($attrs)
58 {
59 if ( ! $this->hasId($attrs)) {
60 return null;
61 }
62
63 $id = intval($attrs['id']);
64
65 if ( ! $this->campaignExists($id)) {
66 return null;
67 }
68
69 return $id;
70 }
71
72 public function getCurrentPageId()
73 {
74
75 $objectId = get_queried_object_id();
76 if ( ! Utils\Helpers::isWcActive()) {
77 return $objectId;
78 }
79 $wcIds = [
80 'shop' => get_option('woocommerce_shop_page_id'),
81 'cart' => get_option('woocommerce_cart_page_id'),
82 'checkout' => get_option('woocommerce_checkout_page_id'),
83 'checkout_pay' => get_option('woocommerce_pay_page_id'),
84 'thanks' => get_option('woocommerce_thanks_page_id'),
85 'myaccount' => get_option('woocommerce_myaccount_page_id'),
86 'edit_address' => get_option('woocommerce_edit_address_page_id'),
87 'view_order' => get_option('woocommerce_view_order_page_id'),
88 'terms' => get_option('woocommerce_terms_page_id'),
89 ];
90 if (is_shop()) {
91 $objectId = $wcIds['shop'];
92 } elseif (is_account_page()) {
93 $objectId = $wcIds['myaccount'];
94 } elseif (is_checkout_pay_page()) {
95 $objectId = $wcIds['checkout_pay'];
96 } elseif (is_checkout()) {
97 $objectId = $wcIds['checkout'];
98 } elseif (is_cart()) {
99 $objectId = $wcIds['cart'];
100 } elseif (is_view_order_page()) {
101 $objectId = $wcIds['view_order'];
102 } elseif (is_view_order_page()) {
103 $objectId = $wcIds['view_order'];
104 } elseif (is_view_order_page()) {
105 $objectId = $wcIds['view_order'];
106 } elseif (is_view_order_page()) {
107 $objectId = $wcIds['view_order'];
108 }
109
110 return $objectId;
111 }
112
113 /**
114 * Verify if the given attributes contain the campaign ID.
115 *
116 * @param $attrs
117 *
118 * @return bool
119 */
120 public function hasId($attrs)
121 {
122 return isset($attrs['id']);
123 }
124
125 /**
126 * Verify if the given campaign exists in database.
127 *
128 * @param $id
129 *
130 * @return boolean
131 */
132 public function campaignExists($id)
133 {
134 return get_post($id) instanceof WP_Post;
135 }
136 }
137