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 +72 -94 2.1.22.6.2 View file →
@@ -1,132 +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.
12 - *
13 - * @param $attrs
14 - *
15 - * @return string
9 + * @var array The default shortcode attributes.
16 10 */
17 - function content($attrs)
18 - {
19 - $id = $this->getId($attrs);
11 + protected $default_attrs = [
12 + 'id' => null,
13 + 'run_in_background' => false,
14 + 'sticky' => false
15 + ];
20 16
21 - if ($id === null) {
22 - return '';
23 - }
24 - $campaign = new Campaign($id);
25 - $campaign->loadSettings();
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 + ];
26 25
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 -
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 );
43 33 }
44 34
45 -
46 35 /**
47 - * Returns campaign ID if set.
48 36 *
49 - * @param $attrs
37 + * @param $return
38 + * @param $tag
39 + * @param $attr
50 40 *
51 - * @return int|null
41 + * @return mixed|string|void
52 42 */
53 - function getId($attrs)
43 + function maybe_display( $return, $tag, $attrs )
54 44 {
55 - if ( ! $this->hasId($attrs)) {
56 - return null;
45 + if ( $tag !== 'hurrytimer' ) {
46 + return $return;
57 47 }
58 48
59 - $id = intval($attrs['id']);
60 49
61 - if ( ! $this->campaignExists($id)) {
62 - return null;
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' );
63 56 }
64 57
65 - return $id;
66 - }
58 + if ( !get_post( $attrs[ 'id' ] ) ) {
59 + return __( 'HurryTimer: Invalid campaign ID.', 'hurrytimer' );
60 + }
67 61
68 - function getCurrentPageId()
69 - {
62 + $campaign = hurryt_get_campaign( $attrs[ 'id' ] );
70 63
71 - $objectId = get_queried_object_id();
72 - if(! Utils\Helpers::isWcActive()){
73 - return $objectId;
64 + if ( $attrs[ 'sticky' ] && !$campaign->show_sticky_on_page( hurryt_current_page_id() ) ) {
65 + return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() );
74 66 }
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'];
67 +
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() );
104 72 }
105 73
106 - return $objectId;
74 + // Let developers decide whether to show the campaign or not.
75 + if ( !apply_filters( 'hurryt_show_campaign', true, $campaign->get_id() ) ) {
76 + return '';
77 + }
78 +
79 +
80 + return $return;
107 81 }
108 82
109 83 /**
110 - * Verify if the given attributes contain the campaign ID.
84 + * Return shortcode content.
111 85 *
112 86 * @param $attrs
113 87 *
114 - * @return bool
88 + * @return string
115 89 */
116 - function hasId($attrs)
90 + public function content( $attrs )
117 91 {
118 - return isset($attrs['id']);
92 +
93 + $attrs = wp_parse_args( $attrs, $this->default_attrs );
94 +
95 + $attrs = filter_var_array( $attrs, $this->attrs_filter_def );
96 +
97 + $campaign = hurryt_get_campaign( absint( $attrs[ 'id' ] ) );
98 +
99 + if ( $campaign->is_expired() ) {
100 + ( new ActionManager( $campaign ) )->run();
101 + }
102 +
103 + // TODO: Move `build_template` to `TemplateBuilder->build(int $campaign_id)`.
104 + $template = apply_filters( 'hurryt_campaign_template', $campaign->build_template(), $campaign->get_id() );
105 +
106 + return !empty( $template ) ? $campaign->wrap_template( $template, $attrs ) : '';
119 107 }
120 108
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 -}
109 +
110 +}