PluginProbe
Simple Ticker / 2.09
Simple Ticker v2.09
trunk 1.0 1.01 1.02 1.03 1.04 1.05 1.06 1.07 2.00 2.01 2.02 2.03 2.04 2.05 2.06 2.07 2.08 2.09 2.10 2.11 3.00 3.01 3.02 3.03 All 33 releases
simple-ticker / lib / class-simpleticker.php

class-simpleticker.php in Simple Ticker 2.09, at lib/class-simpleticker.php

307 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Simple Ticker
4 *
5 * @package Simple Ticker
6 * @subpackage SimpleTicker
7 /*
8 Copyright (c) 2016- Katsushi Kawamori (email : dodesyoswift312@gmail.com)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 $simpleticker = new SimpleTicker();
24
25 /** ==================================================
26 * Main Functions
27 */
28 class SimpleTicker {
29
30 /** ==================================================
31 * Option
32 *
33 * @var $simpleticker_option simpleticker_option.
34 */
35 private $simpleticker_option;
36
37 /** ==================================================
38 * Construct
39 *
40 * @since 1.06
41 */
42 public function __construct() {
43
44 $this->simpleticker_option = get_option( 'simple_ticker' );
45
46 add_action( 'wp_print_styles', array( $this, 'load_styles' ) );
47 add_shortcode( 'simpleticker', array( $this, 'simpleticker_func' ) );
48 if ( 'none' <> $this->simpleticker_option['insert'] ) {
49 add_filter( 'the_content', array( $this, 'insert_post' ) );
50 }
51
52 }
53
54 /** ==================================================
55 * Contents filter
56 *
57 * @param string $content content.
58 * @return string $content
59 * @since 2.04
60 */
61 public function insert_post( $content ) {
62
63 $custom_content = $this->read_tickers( null, null, null, null, null, null, null, null, null, null, null );
64 switch ( $this->simpleticker_option['insert'] ) {
65 case 'before':
66 $content = $custom_content . $content;
67 break;
68 case 'after':
69 $content .= $custom_content;
70 break;
71 case 'beforeafter':
72 $content = $custom_content . $content . $custom_content;
73 break;
74 }
75
76 return $content;
77
78 }
79
80 /** ==================================================
81 * Main
82 *
83 * @param string $ticker1_color ticker1_color.
84 * @param string $ticker1_text ticker1_text.
85 * @param string $ticker2_color ticker2_color.
86 * @param string $ticker2_text ticker2_text.
87 * @param string $ticker3_color ticker3_color.
88 * @param string $ticker3_text ticker3_text.
89 * @param bool $sticky_posts_display sticky_posts_display.
90 * @param string $sticky_posts_title_color sticky_posts_title_color.
91 * @param string $sticky_posts_content_color sticky_posts_content_color.
92 * @param bool $woo_sales_display woo_sales_display.
93 * @param string $woo_sales_color woo_sales_color.
94 * @return string $ticker_html
95 * @since 1.0
96 */
97 public function read_tickers( $ticker1_color, $ticker1_text, $ticker2_color, $ticker2_text, $ticker3_color, $ticker3_text, $sticky_posts_display, $sticky_posts_title_color, $sticky_posts_content_color, $woo_sales_display, $woo_sales_color ) {
98
99 if ( empty( $ticker1_color ) ) {
100 $ticker1_color = '#' . $this->simpleticker_option['ticker1']['color']; }
101 if ( empty( $ticker1_text ) ) {
102 $ticker1_text = $this->simpleticker_option['ticker1']['text']; }
103 if ( empty( $ticker2_color ) ) {
104 $ticker2_color = '#' . $this->simpleticker_option['ticker2']['color']; }
105 if ( empty( $ticker2_text ) ) {
106 $ticker2_text = $this->simpleticker_option['ticker2']['text']; }
107 if ( empty( $ticker3_color ) ) {
108 $ticker3_color = '#' . $this->simpleticker_option['ticker3']['color']; }
109 if ( empty( $ticker3_text ) ) {
110 $ticker3_text = $this->simpleticker_option['ticker3']['text']; }
111
112 if ( empty( $sticky_posts_display ) ) {
113 $sticky_posts_display = $this->simpleticker_option['sticky_posts']['display']; }
114 if ( empty( $sticky_posts_title_color ) ) {
115 $sticky_posts_title_color = '#' . $this->simpleticker_option['sticky_posts']['title_color']; }
116 if ( empty( $sticky_posts_content_color ) ) {
117 $sticky_posts_content_color = '#' . $this->simpleticker_option['sticky_posts']['content_color']; }
118
119 if ( empty( $woo_sales_display ) ) {
120 $woo_sales_display = $this->simpleticker_option['woo_sales']['display']; }
121 if ( empty( $woo_sales_color ) ) {
122 $woo_sales_color = '#' . $this->simpleticker_option['woo_sales']['color']; }
123
124 $ticker_html = null;
125
126 if ( ! empty( $ticker1_text ) ) {
127 $ticker_html .= '<div class="marquee"><div class="marquee-inner"><span style="color: ' . $ticker1_color . '; font-weight: bold;">';
128 $ticker_html .= ' ' . $ticker1_text;
129 $ticker_html .= '</span></div></div>';
130 }
131 if ( ! empty( $ticker2_text ) ) {
132 $ticker_html .= '<div class="marquee"><div class="marquee-inner"><span style="color: ' . $ticker2_color . '; font-weight: bold;">';
133 $ticker_html .= ' ' . $ticker2_text;
134 $ticker_html .= '</span></div></div>';
135 }
136 if ( ! empty( $ticker3_text ) ) {
137 $ticker_html .= '<div class="marquee"><div class="marquee-inner"><span style="color: ' . $ticker3_color . '; font-weight: bold;">';
138 $ticker_html .= ' ' . $ticker3_text;
139 $ticker_html .= '</span></div></div>';
140 }
141
142 if ( 1 == $sticky_posts_display ) {
143 $stickies = get_option( 'sticky_posts' );
144 if ( ! empty( $stickies ) ) {
145 rsort( $stickies );
146 foreach ( $stickies as $sticky ) {
147 $post = null;
148 $title = null;
149 $content = null;
150 $post = get_post( $sticky );
151 $title = $post->post_title;
152 $content = $this->html_text( $post->post_content );
153 $ticker_html .= '<div class="marquee"><div class="marquee-inner"><span style="color: ' . $sticky_posts_title_color . '; font-weight: bold;">';
154 $ticker_html .= ' ' . $title . ':';
155 $ticker_html .= '</span>';
156 $ticker_html .= '<span style="color: ' . $sticky_posts_content_color . '; font-weight: bold;">';
157 $ticker_html .= $content;
158 $ticker_html .= '</span></div></div>';
159 }
160 }
161 }
162
163 if ( 1 == $woo_sales_display && class_exists( 'WooCommerce' ) ) {
164 global $wpdb;
165 $sales = $wpdb->get_results(
166 "
167 SELECT post_id, meta_key, meta_value
168 FROM $wpdb->postmeta
169 WHERE meta_key LIKE '%%_sale_price%%'
170 "
171 );
172 $ticker_html .= '<div class="marquee"><div class="marquee-inner">';
173 $woo_sales_text_tag = $this->simpleticker_option['woo_sales']['text'];
174 $currency = get_option( 'woocommerce_currency' );
175 $currency_symbol = get_woocommerce_currency_symbol( $currency );
176 foreach ( $sales as $sale ) {
177 if ( '_sale_price' === $sale->meta_key ) {
178 if ( ! empty( $sale->meta_value ) ) {
179 $woo_sales_datas = array();
180 $sale_product = get_the_title( $sale->post_id );
181 $sale_link = get_permalink( $sale->post_id );
182 $sale_html = '<a href="' . $sale_link . '">' . $sale_product . '</a>';
183
184 $regular_price = get_post_meta( $sale->post_id, '_regular_price', true );
185 $sale_price = get_post_meta( $sale->post_id, '_sale_price', true );
186 $to_date_ux = intval( get_post_meta( $sale->post_id, '_sale_price_dates_to', true ) );
187 $current_date_ux = time();
188
189 $woo_sales_datas['regular_price'] = sprintf( get_woocommerce_price_format(), $currency_symbol, $regular_price );
190 $woo_sales_datas['sale_price'] = sprintf( get_woocommerce_price_format(), $currency_symbol, $sale_price );
191
192 $dis_amount = sprintf( get_woocommerce_price_format(), $currency_symbol, intval( $regular_price - $sale_price ) );
193 $dis_rate = intval( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
194 /* translators: Woo sale rate price */
195 $woo_sales_datas['dis_rate_price'] = sprintf( __( '%1$s %2$s', 'simple-ticker' ), $dis_amount, __( 'OFF', 'countdown-woocommerce-sale' ) );
196 /* translators: Woo sale rate percent */
197 $woo_sales_datas['dis_rate_percent'] = sprintf( __( '%1$d&#37; %2$s', 'simple-ticker' ), $dis_rate, __( 'OFF', 'countdown-woocommerce-sale' ) );
198
199 $woo_sales_datas['to_date'] = date_i18n( 'Y-m-d H:i:s', $to_date_ux, false );
200 $interval_day_ux = $to_date_ux - $current_date_ux;
201 if ( $to_date_ux > 0 ) {
202 $woo_sales_datas['interval_day'] = intval( $interval_day_ux / ( 60 * 60 * 24 ) ) . __( 'Days', 'simple-ticker' );
203 } else {
204 $woo_sales_datas['interval_day'] = null;
205 }
206 $woo_sales_datas['interval_time'] = gmdate( 'H:i:s', $interval_day_ux % ( 60 * 60 * 24 ) );
207 $ticker_html .= $sale_html;
208 $ticker_html .= '<span style="color: ' . $woo_sales_color . '; font-weight: bold;"> : ';
209 $woo_sales_text = null;
210 if ( $woo_sales_datas ) {
211 $woo_sales_text = $woo_sales_text_tag;
212 foreach ( $woo_sales_datas as $item => $woo_sale ) {
213 $woo_sales_text = str_replace( '%' . $item . '%', $woo_sale, $woo_sales_text );
214 }
215 preg_match_all( '/%(.*?)%/', $woo_sales_text, $woo_sales_text_per_match );
216 foreach ( $woo_sales_text_per_match as $key1 ) {
217 foreach ( $key1 as $key2 ) {
218 $woo_sales_text = str_replace( '%' . $key2 . '%', '', $woo_sales_text );
219 }
220 }
221 }
222 $ticker_html .= $woo_sales_text;
223 $ticker_html .= '</span> ';
224 }
225 }
226 }
227 $ticker_html .= '</div></div>';
228
229 }
230
231 return $ticker_html;
232
233 }
234
235 /** ==================================================
236 * short code
237 *
238 * @param array $atts atts.
239 * @return string $this->read_tickers()
240 */
241 public function simpleticker_func( $atts ) {
242
243 $a = shortcode_atts(
244 array(
245 'ticker1_color' => '',
246 'ticker1_text' => '',
247 'ticker2_color' => '',
248 'ticker2_text' => '',
249 'ticker3_color' => '',
250 'ticker3_text' => '',
251 'sticky_posts_display' => '',
252 'sticky_posts_title_color' => '',
253 'sticky_posts_content_color' => '',
254 'woo_sales_display' => '',
255 'woo_sales_color' => '',
256 ),
257 $atts
258 );
259
260 $ticker1_color = $a['ticker1_color'];
261 $ticker1_text = $a['ticker1_text'];
262 $ticker2_color = $a['ticker2_color'];
263 $ticker2_text = $a['ticker2_text'];
264 $ticker3_color = $a['ticker3_color'];
265 $ticker3_text = $a['ticker3_text'];
266 $sticky_posts_display = $a['sticky_posts_display'];
267 $sticky_posts_title_color = $a['sticky_posts_title_color'];
268 $sticky_posts_content_color = $a['sticky_posts_content_color'];
269 $woo_sales_display = $a['woo_sales_display'];
270 $woo_sales_color = $a['woo_sales_color'];
271
272 return $this->read_tickers( $ticker1_color, $this->html_text( $ticker1_text ), $ticker2_color, $this->html_text( $ticker2_text ), $ticker3_color, $this->html_text( $ticker3_text ), $sticky_posts_display, $sticky_posts_title_color, $sticky_posts_content_color, $woo_sales_display, $woo_sales_color );
273
274 }
275
276 /** ==================================================
277 * Html to text
278 *
279 * @param string $html html.
280 * @return string $text
281 * @since 1.0
282 */
283 public function html_text( $html ) {
284
285 $text = wp_strip_all_tags( $html );
286 $text = str_replace( array( "\r", "\n" ), '', $text );
287
288 return $text;
289
290 }
291
292 /** ==================================================
293 * Load Marquee Style
294 *
295 * @since 2.00
296 */
297 public function load_styles() {
298 $speed = $this->simpleticker_option['speed'];
299 wp_enqueue_style( 'simpleticker', plugin_dir_url( __DIR__ ) . 'css/marquee.css', array(), '1.00' );
300 $css = '.marquee > .marquee-inner { animation-duration: ' . $speed . 's; }';
301 wp_add_inline_style( 'simpleticker', $css );
302 }
303
304 }
305
306
307