PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.1.2
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.1.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
hurrytimer / includes / CampaignShortcode.php

CampaignShortcode.php in HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce 2.1.2, at includes/CampaignShortcode.php

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