PluginProbe
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce / 2.6.2
HurryTimer – An Scarcity and Urgency Countdown Timer for WordPress & WooCommerce v2.6.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
← All changes | includes/CampaignShortcode.php +70 -102 2.1.52.6.2 View file →
@@ -1,142 +1,110 @@
1 1 <?php
2 2
3 3 namespace Hurrytimer;
4 4
5 -use \WP_Post;
6 -
7 5 class CampaignShortcode
8 6 {
9 7
10 8 /**
11 - * Return shortcode content.
9 + * @var array The default shortcode attributes.
10 + */
11 + protected $default_attrs = [
12 + 'id' => null,
13 + 'run_in_background' => false,
14 + 'sticky' => false
15 + ];
16 +
17 + /**
18 + * @var array The attributes array filter.
19 + */
20 + protected $attrs_filter_def = [
21 + 'id' => FILTER_VALIDATE_INT,
22 + 'run_in_background' => FILTER_VALIDATE_BOOLEAN,
23 + 'sticky' => FILTER_VALIDATE_BOOLEAN
24 + ];
25 +
26 + /**
27 + * Init shortcode
28 + */
29 + function init()
30 + {
31 + add_shortcode( 'hurrytimer', [ $this, 'content' ] );
32 + add_filter( 'pre_do_shortcode_tag', [ $this, 'maybe_display' ], 10, 3 );
33 + }
34 +
35 + /**
12 36 *
13 - * @param $attrs
37 + * @param $return
38 + * @param $tag
39 + * @param $attr
14 40 *
15 - * @return string
41 + * @return mixed|string|void
16 42 */
17 - function content($attrs)
43 + function maybe_display( $return, $tag, $attrs )
18 44 {
19 - $id = $this->getId($attrs);
45 + if ( $tag !== 'hurrytimer' ) {
46 + return $return;
47 + }
20 48
21 - if ($id === null) {
22 - return '';
49 +
50 + $attrs = wp_parse_args( $attrs, $this->default_attrs );
51 +
52 + $attrs = filter_var_array( $attrs, $this->attrs_filter_def );
53 +
54 + if ( empty( $attrs[ 'id' ] ) ) {
55 + return __( 'HurryTimer: Invalid campaign ID.', 'hurrytimer' );
23 56 }
24 - $campaign = new Campaign($id);
25 57
26 - $campaign->loadSettings();
58 + if ( !get_post( $attrs[ 'id' ] ) ) {
59 + return __( 'HurryTimer: Invalid campaign ID.', 'hurrytimer' );
60 + }
27 61
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 '';
62 + $campaign = hurryt_get_campaign( $attrs[ 'id' ] );
63 +
64 + if ( $attrs[ 'sticky' ] && !$campaign->show_sticky_on_page( hurryt_current_page_id() ) ) {
65 + return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() );
33 66 }
34 67
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 '';
68 + if ( !$campaign->is_running() ) {
69 +
70 + // Let developers show something else whenever the campaign isn't running.
71 + return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() );
40 72 }
41 73
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();
74 + // Let developers decide whether to show the campaign or not.
75 + if ( !apply_filters( 'hurryt_show_campaign', true, $campaign->get_id() ) ) {
76 + return '';
46 77 }
47 78
48 79
49 - $template = apply_filters('hurryt_campaign_template', $campaign->buildTemplate());
50 -
51 - return $template === '' ? '': $campaign->wrapTemplate($template);
52 -
80 + return $return;
53 81 }
54 82
55 -
56 83 /**
57 - * Returns campaign ID if set.
84 + * Return shortcode content.
58 85 *
59 86 * @param $attrs
60 87 *
61 - * @return int|null
88 + * @return string
62 89 */
63 - function getId($attrs)
90 + public function content( $attrs )
64 91 {
65 - if ( ! $this->hasId($attrs)) {
66 - return null;
67 - }
68 92
69 - $id = intval($attrs['id']);
93 + $attrs = wp_parse_args( $attrs, $this->default_attrs );
70 94
71 - if ( ! $this->campaignExists($id)) {
72 - return null;
73 - }
95 + $attrs = filter_var_array( $attrs, $this->attrs_filter_def );
74 96
75 - return $id;
76 - }
97 + $campaign = hurryt_get_campaign( absint( $attrs[ 'id' ] ) );
77 98
78 - function getCurrentPageId()
79 - {
99 + if ( $campaign->is_expired() ) {
100 + ( new ActionManager( $campaign ) )->run();
101 + }
80 102
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 - }
103 + // TODO: Move `build_template` to `TemplateBuilder->build(int $campaign_id)`.
104 + $template = apply_filters( 'hurryt_campaign_template', $campaign->build_template(), $campaign->get_id() );
115 105
116 - return $objectId;
106 + return !empty( $template ) ? $campaign->wrap_template( $template, $attrs ) : '';
117 107 }
118 108
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 109
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 -}
110 +}