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

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