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

115 lines 3.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 class CampaignShortcode
6 {
7
8 /**
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 add_filter( 'shortcode_atts_hurrytimer', [$this, 'translate_shortcode_id' ] );
34
35 }
36
37 function translate_shortcode_id($out){
38 $out['id'] = apply_filters( 'wpml_object_id', $out['id'], HURRYT_POST_TYPE, true );
39
40 return $out;
41 }
42 /**
43 *
44 * @param $return
45 * @param $tag
46 * @param $attr
47 *
48 * @return mixed|string|void
49 */
50 function maybe_display( $return, $tag, $attrs )
51 {
52 if ( $tag !== 'hurrytimer' ) {
53 return $return;
54 }
55
56 $attrs = shortcode_atts($this->default_attrs, $attrs, 'hurrytimer');
57 $attrs = filter_var_array( $attrs, $this->attrs_filter_def );
58
59 if ( empty( $attrs[ 'id' ] ) ) {
60 return 'HurryTimer: ' . __( 'Invalid campaign ID.', 'hurrytimer' );
61 }
62
63 if ( !get_post( $attrs[ 'id' ] ) ) {
64 return 'HurryTimer: ' . __( 'Invalid campaign ID.', 'hurrytimer' );
65 }
66
67 $campaign = hurryt_get_campaign( $attrs[ 'id' ] );
68
69 if ( $attrs[ 'sticky' ] && !$campaign->show_sticky_on_page( hurryt_current_page_id() ) ) {
70 return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() );
71 }
72
73 if ( !$campaign->is_running() ) {
74
75 // Let developers show something else whenever the campaign isn't running.
76 return apply_filters( 'hurryt_no_campaign', '', $campaign->get_id() );
77 }
78
79 // Let developers decide whether to show the campaign or not.
80 if ( !apply_filters( 'hurryt_show_campaign', true, $campaign->get_id() ) ) {
81 return '';
82 }
83
84
85 return $return;
86 }
87
88 /**
89 * Return shortcode content.
90 *
91 * @param $attrs
92 *
93 * @return string
94 */
95 public function content( $attrs )
96 {
97
98 $attrs = shortcode_atts($this->default_attrs, $attrs, 'hurrytimer');
99 $attrs = filter_var_array( $attrs, $this->attrs_filter_def );
100
101 $campaign = hurryt_get_campaign( absint( $attrs[ 'id' ] ) );
102
103 if ( $campaign->is_expired() ) {
104 ( new ActionManager( $campaign ) )->run();
105 }
106
107 // TODO: Move `build_template` to `TemplateBuilder->build(int $campaign_id)`.
108 $template = apply_filters( 'hurryt_campaign_template', $campaign->build_template(), $campaign->get_id() );
109
110 return !empty( $template ) ? $campaign->wrap_template( $template, $attrs ) : '';
111 }
112
113
114 }
115