PluginProbe
Simple Ticker / 3.03
Simple Ticker v3.03
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 3.03, at lib/class-simpleticker.php

491 lines 17.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( 'init', array( $this, 'simpleticker_block_init' ) );
47
48 /* original hook */
49 add_filter( 'smptck_read_tickers', array( $this, 'read_tickers' ), 10, 14 );
50 add_filter( 'smptck_html_text', array( $this, 'html_text' ), 10, 1 );
51
52 add_action( 'wp_print_styles', array( $this, 'load_styles' ) );
53 add_action( 'admin_enqueue_scripts', array( $this, 'load_styles' ) );
54
55 if ( 'none' <> $this->simpleticker_option['insert'] ) {
56 add_filter( 'the_content', array( $this, 'insert_post' ) );
57 }
58 if ( $this->simpleticker_option['insert_body_open'] ) {
59 add_action( 'wp_body_open', array( $this, 'insert_body_open' ) );
60 }
61
62 }
63
64 /** ==================================================
65 * Attribute block
66 *
67 * @since 3.00
68 */
69 public function simpleticker_block_init() {
70
71 $asset_file = include( plugin_dir_path( __DIR__ ) . 'block/dist/simpleticker-block.asset.php' );
72
73 wp_register_script(
74 'simpleticker-block',
75 plugins_url( 'block/dist/simpleticker-block.js', dirname( __FILE__ ) ),
76 $asset_file['dependencies'],
77 $asset_file['version'],
78 true
79 );
80
81 wp_localize_script(
82 'simpleticker-block',
83 'simpleticker_text',
84 array(
85 'color' => __( 'Color' ),
86 'colorsettings' => __( 'Color Settings' ),
87 'ticker1' => __( 'Ticker1', 'simple-ticker' ),
88 'ticker2' => __( 'Ticker2', 'simple-ticker' ),
89 'ticker3' => __( 'Ticker3', 'simple-ticker' ),
90 'stickyposts' => __( 'Sticky Posts', 'simple-ticker' ),
91 'includesticky' => __( 'Include sticky_posts', 'simple-ticker' ),
92 'stickytitlecolor' => __( 'Title color', 'simple-ticker' ),
93 'stickycontentcolor' => __( 'Content color', 'simple-ticker' ),
94 'woosales' => __( 'WooCommerce Sales', 'simple-ticker' ),
95 'includewoo' => __( 'Include WooCommerce Sales', 'simple-ticker' ),
96 )
97 );
98
99 register_block_type(
100 'simple-ticker/simpleticker-block',
101 array(
102 'editor_script' => 'simpleticker-block',
103 'render_callback' => array( $this, 'simpleticker_func' ),
104 'attributes' => array(
105 'ticker1_color' => array(
106 'type' => 'color',
107 'default' => $this->simpleticker_option['ticker1']['color'],
108 ),
109 'ticker1_text' => array(
110 'type' => 'string',
111 'default' => $this->simpleticker_option['ticker1']['text'],
112 ),
113 'ticker1_url' => array(
114 'type' => 'string',
115 'default' => $this->simpleticker_option['ticker1']['url'],
116 ),
117 'ticker2_color' => array(
118 'type' => 'color',
119 'default' => $this->simpleticker_option['ticker2']['color'],
120 ),
121 'ticker2_text' => array(
122 'type' => 'string',
123 'default' => $this->simpleticker_option['ticker2']['text'],
124 ),
125 'ticker2_url' => array(
126 'type' => 'string',
127 'default' => $this->simpleticker_option['ticker2']['url'],
128 ),
129 'ticker3_color' => array(
130 'type' => 'color',
131 'default' => $this->simpleticker_option['ticker3']['color'],
132 ),
133 'ticker3_text' => array(
134 'type' => 'string',
135 'default' => $this->simpleticker_option['ticker3']['text'],
136 ),
137 'ticker3_url' => array(
138 'type' => 'string',
139 'default' => $this->simpleticker_option['ticker3']['url'],
140 ),
141 'sticky_posts_display' => array(
142 'type' => 'boolean',
143 'default' => $this->simpleticker_option['sticky_posts']['display'],
144 ),
145 'sticky_posts_title_color' => array(
146 'type' => 'color',
147 'default' => $this->simpleticker_option['sticky_posts']['title_color'],
148 ),
149 'sticky_posts_content_color' => array(
150 'type' => 'color',
151 'default' => $this->simpleticker_option['sticky_posts']['content_color'],
152 ),
153 'woo_sales_display' => array(
154 'type' => 'boolean',
155 'default' => $this->simpleticker_option['woo_sales']['display'],
156 ),
157 'woo_sales_color' => array(
158 'type' => 'color',
159 'default' => $this->simpleticker_option['woo_sales']['color'],
160 ),
161 ),
162 )
163 );
164
165 add_shortcode( 'simpleticker', array( $this, 'simpleticker_func' ) );
166
167 }
168
169 /** ==================================================
170 * Body open action
171 *
172 * @since 3.00
173 */
174 public function insert_body_open() {
175
176 $allowed_html = array(
177 'div' => array(
178 'class' => array(),
179 ),
180 'span' => array(
181 'style' => array(),
182 ),
183 'a' => array(
184 'href' => array(),
185 ),
186 );
187
188 echo wp_kses( $this->read_tickers( null, null, null, null, null, null, null, null, null, null, null, null, null, null ), $allowed_html );
189
190 }
191
192 /** ==================================================
193 * Contents filter
194 *
195 * @param string $content content.
196 * @return string $content
197 * @since 2.04
198 */
199 public function insert_post( $content ) {
200
201 $custom_content = $this->read_tickers( null, null, null, null, null, null, null, null, null, null, null, null, null, null );
202 switch ( $this->simpleticker_option['insert'] ) {
203 case 'before':
204 $content = $custom_content . $content;
205 break;
206 case 'after':
207 $content .= $custom_content;
208 break;
209 case 'beforeafter':
210 $content = $custom_content . $content . $custom_content;
211 break;
212 }
213
214 return $content;
215
216 }
217
218 /** ==================================================
219 * Main
220 *
221 * @param string $ticker1_color ticker1_color.
222 * @param string $ticker1_text ticker1_text.
223 * @param string $ticker1_url ticker1_url.
224 * @param string $ticker2_color ticker2_color.
225 * @param string $ticker2_text ticker2_text.
226 * @param string $ticker2_url ticker2_url.
227 * @param string $ticker3_color ticker3_color.
228 * @param string $ticker3_text ticker3_text.
229 * @param string $ticker3_url ticker3_url.
230 * @param bool $sticky_posts_display sticky_posts_display.
231 * @param string $sticky_posts_title_color sticky_posts_title_color.
232 * @param string $sticky_posts_content_color sticky_posts_content_color.
233 * @param bool $woo_sales_display woo_sales_display.
234 * @param string $woo_sales_color woo_sales_color.
235 * @return string $ticker_html
236 * @since 1.00
237 */
238 public function read_tickers( $ticker1_color, $ticker1_text, $ticker1_url, $ticker2_color, $ticker2_text, $ticker2_url, $ticker3_color, $ticker3_text, $ticker3_url, $sticky_posts_display, $sticky_posts_title_color, $sticky_posts_content_color, $woo_sales_display, $woo_sales_color ) {
239
240 if ( empty( $ticker1_color ) ) {
241 $ticker1_color = $this->simpleticker_option['ticker1']['color']; }
242 if ( empty( $ticker1_text ) ) {
243 $ticker1_text = $this->simpleticker_option['ticker1']['text']; }
244 if ( empty( $ticker1_url ) ) {
245 $ticker1_url = $this->simpleticker_option['ticker1']['url']; }
246 if ( empty( $ticker2_color ) ) {
247 $ticker2_color = $this->simpleticker_option['ticker2']['color']; }
248 if ( empty( $ticker2_text ) ) {
249 $ticker2_text = $this->simpleticker_option['ticker2']['text']; }
250 if ( empty( $ticker2_url ) ) {
251 $ticker2_url = $this->simpleticker_option['ticker2']['url']; }
252 if ( empty( $ticker3_color ) ) {
253 $ticker3_color = $this->simpleticker_option['ticker3']['color']; }
254 if ( empty( $ticker3_text ) ) {
255 $ticker3_text = $this->simpleticker_option['ticker3']['text']; }
256 if ( empty( $ticker3_url ) ) {
257 $ticker3_url = $this->simpleticker_option['ticker3']['url']; }
258
259 if ( empty( $sticky_posts_display ) ) {
260 $sticky_posts_display = $this->simpleticker_option['sticky_posts']['display']; }
261 if ( empty( $sticky_posts_title_color ) ) {
262 $sticky_posts_title_color = $this->simpleticker_option['sticky_posts']['title_color']; }
263 if ( empty( $sticky_posts_content_color ) ) {
264 $sticky_posts_content_color = $this->simpleticker_option['sticky_posts']['content_color']; }
265
266 if ( empty( $woo_sales_display ) ) {
267 $woo_sales_display = $this->simpleticker_option['woo_sales']['display']; }
268 if ( empty( $woo_sales_color ) ) {
269 $woo_sales_color = $this->simpleticker_option['woo_sales']['color']; }
270
271 $ticker_html = null;
272
273 if ( ! empty( $ticker1_text ) ) {
274 if ( ! empty( $ticker1_url ) ) {
275 $ticker_html .= '<a href="' . $ticker1_url . '">';
276 }
277 $ticker_html .= '<div class="marquee"><div class="marquee-inner"><span style="color: ' . $ticker1_color . '; font-weight: bold;">';
278 $ticker_html .= ' ' . $ticker1_text;
279 $ticker_html .= '</span></div></div>';
280 if ( ! empty( $ticker1_url ) ) {
281 $ticker_html .= '</a>';
282 }
283 }
284 if ( ! empty( $ticker2_text ) ) {
285 if ( ! empty( $ticker2_url ) ) {
286 $ticker_html .= '<a href="' . $ticker2_url . '">';
287 }
288 $ticker_html .= '<div class="marquee"><div class="marquee-inner"><span style="color: ' . $ticker2_color . '; font-weight: bold;">';
289 $ticker_html .= ' ' . $ticker2_text;
290 $ticker_html .= '</span></div></div>';
291 if ( ! empty( $ticker2_url ) ) {
292 $ticker_html .= '</a>';
293 }
294 }
295 if ( ! empty( $ticker3_text ) ) {
296 if ( ! empty( $ticker3_url ) ) {
297 $ticker_html .= '<a href="' . $ticker3_url . '">';
298 }
299 $ticker_html .= '<div class="marquee"><div class="marquee-inner"><span style="color: ' . $ticker3_color . '; font-weight: bold;">';
300 $ticker_html .= ' ' . $ticker3_text;
301 $ticker_html .= '</span></div></div>';
302 if ( ! empty( $ticker3_url ) ) {
303 $ticker_html .= '</a>';
304 }
305 }
306
307 if ( $sticky_posts_display ) {
308 $stickies = get_option( 'sticky_posts' );
309 if ( ! empty( $stickies ) ) {
310 rsort( $stickies );
311 foreach ( $stickies as $sticky ) {
312 $post = null;
313 $title = null;
314 $content = null;
315 $post = get_post( $sticky );
316 $title = $post->post_title;
317 $content = $this->html_text( $post->post_content );
318 $permalink = get_permalink( $post->ID );
319 $ticker_html .= '<a href="' . $permalink . '">';
320 $ticker_html .= '<div class="marquee"><div class="marquee-inner"><span style="color: ' . $sticky_posts_title_color . '; font-weight: bold;">';
321 $ticker_html .= ' ' . $title . ':';
322 $ticker_html .= '</span>';
323 $ticker_html .= '<span style="color: ' . $sticky_posts_content_color . '; font-weight: bold;">';
324 $ticker_html .= $content;
325 $ticker_html .= '</span></div></div>';
326 $ticker_html .= '</a>';
327 }
328 }
329 }
330
331 if ( $woo_sales_display && class_exists( 'WooCommerce' ) ) {
332 global $wpdb;
333 $sales = $wpdb->get_results(
334 "
335 SELECT post_id, meta_key, meta_value
336 FROM {$wpdb->prefix}postmeta
337 WHERE meta_key LIKE '%%_sale_price%%'
338 "
339 );
340 $ticker_html .= '<div class="marquee"><div class="marquee-inner">';
341 $woo_sales_text_tag = $this->simpleticker_option['woo_sales']['text'];
342 $currency = get_option( 'woocommerce_currency' );
343 $currency_symbol = get_woocommerce_currency_symbol( $currency );
344 foreach ( $sales as $sale ) {
345 if ( '_sale_price' === $sale->meta_key ) {
346 if ( ! empty( $sale->meta_value ) ) {
347 $woo_sales_datas = array();
348 $sale_product = get_the_title( $sale->post_id );
349 $sale_link = get_permalink( $sale->post_id );
350 $sale_html = '<a href="' . $sale_link . '">' . $sale_product . '</a>';
351
352 $regular_price = get_post_meta( $sale->post_id, '_regular_price', true );
353 $sale_price = get_post_meta( $sale->post_id, '_sale_price', true );
354 $to_date_ux = intval( get_post_meta( $sale->post_id, '_sale_price_dates_to', true ) );
355 $current_date_ux = time();
356
357 $woo_sales_datas['regular_price'] = sprintf( get_woocommerce_price_format(), $currency_symbol, $regular_price );
358 $woo_sales_datas['sale_price'] = sprintf( get_woocommerce_price_format(), $currency_symbol, $sale_price );
359
360 $dis_amount = sprintf( get_woocommerce_price_format(), $currency_symbol, intval( $regular_price - $sale_price ) );
361 $dis_rate = intval( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
362 /* translators: Woo sale rate price */
363 $woo_sales_datas['dis_rate_price'] = sprintf( __( '%1$s %2$s', 'simple-ticker' ), $dis_amount, __( 'OFF', 'countdown-woocommerce-sale' ) );
364 /* translators: Woo sale rate percent */
365 $woo_sales_datas['dis_rate_percent'] = sprintf( __( '%1$d&#37; %2$s', 'simple-ticker' ), $dis_rate, __( 'OFF', 'countdown-woocommerce-sale' ) );
366
367 if ( function_exists( 'wp_date' ) ) {
368 $woo_sales_datas['to_date'] = wp_date( 'Y-m-d H:i:s', $to_date_ux, new DateTimeZone( 'UTC' ) );
369 } else {
370 $woo_sales_datas['to_date'] = date_i18n( 'Y-m-d H:i:s', $to_date_ux, false );
371 }
372 $interval_day_ux = $to_date_ux - $current_date_ux;
373 if ( $to_date_ux > 0 ) {
374 $woo_sales_datas['interval_day'] = intval( $interval_day_ux / ( 60 * 60 * 24 ) ) . __( 'Days', 'simple-ticker' );
375 } else {
376 $woo_sales_datas['interval_day'] = null;
377 }
378 $woo_sales_datas['interval_time'] = gmdate( 'H:i:s', $interval_day_ux % ( 60 * 60 * 24 ) );
379 $ticker_html .= $sale_html;
380 $ticker_html .= '<span style="color: ' . $woo_sales_color . '; font-weight: bold;"> : ';
381 $woo_sales_text = null;
382 if ( $woo_sales_datas ) {
383 $woo_sales_text = $woo_sales_text_tag;
384 foreach ( $woo_sales_datas as $item => $woo_sale ) {
385 $woo_sales_text = str_replace( '%' . $item . '%', $woo_sale, $woo_sales_text );
386 }
387 preg_match_all( '/%(.*?)%/', $woo_sales_text, $woo_sales_text_per_match );
388 foreach ( $woo_sales_text_per_match as $key1 ) {
389 foreach ( $key1 as $key2 ) {
390 $woo_sales_text = str_replace( '%' . $key2 . '%', '', $woo_sales_text );
391 }
392 }
393 }
394 $ticker_html .= $woo_sales_text;
395 $ticker_html .= '</span> ';
396 }
397 }
398 }
399 $ticker_html .= '</div></div>';
400 }
401
402 if ( empty( $ticker_html ) ) {
403 $ticker_html .= '<div style="text-align: center;">';
404 $ticker_html .= '<div><strong><span class="dashicons dashicons-megaphone" style="position: relative; top: 5px;"></span>Simple Ticker</strong></div>';
405 $ticker_html .= __( 'Please input Ticker.', 'simple-ticker' );
406 $ticker_html .= '</div>';
407 }
408
409 return $ticker_html;
410
411 }
412
413 /** ==================================================
414 * short code
415 *
416 * @param array $atts atts.
417 * @return string $this->read_tickers()
418 */
419 public function simpleticker_func( $atts ) {
420
421 $a = shortcode_atts(
422 array(
423 'ticker1_color' => '',
424 'ticker1_text' => '',
425 'ticker1_url' => '',
426 'ticker2_color' => '',
427 'ticker2_text' => '',
428 'ticker2_url' => '',
429 'ticker3_color' => '',
430 'ticker3_text' => '',
431 'ticker3_url' => '',
432 'sticky_posts_display' => '',
433 'sticky_posts_title_color' => '',
434 'sticky_posts_content_color' => '',
435 'woo_sales_display' => '',
436 'woo_sales_color' => '',
437 ),
438 $atts
439 );
440
441 $ticker1_color = $a['ticker1_color'];
442 $ticker1_text = $a['ticker1_text'];
443 $ticker1_url = $a['ticker1_url'];
444 $ticker2_color = $a['ticker2_color'];
445 $ticker2_text = $a['ticker2_text'];
446 $ticker2_url = $a['ticker2_url'];
447 $ticker3_color = $a['ticker3_color'];
448 $ticker3_text = $a['ticker3_text'];
449 $ticker3_url = $a['ticker3_url'];
450 $sticky_posts_display = $a['sticky_posts_display'];
451 $sticky_posts_title_color = $a['sticky_posts_title_color'];
452 $sticky_posts_content_color = $a['sticky_posts_content_color'];
453 $woo_sales_display = $a['woo_sales_display'];
454 $woo_sales_color = $a['woo_sales_color'];
455
456 return $this->read_tickers( $ticker1_color, $this->html_text( $ticker1_text ), $ticker1_url, $ticker2_color, $this->html_text( $ticker2_text ), $ticker2_url, $ticker3_color, $this->html_text( $ticker3_text ), $ticker3_url, $sticky_posts_display, $sticky_posts_title_color, $sticky_posts_content_color, $woo_sales_display, $woo_sales_color );
457
458 }
459
460 /** ==================================================
461 * Html to text
462 *
463 * @param string $html html.
464 * @return string $text
465 * @since 1.00
466 */
467 public function html_text( $html ) {
468
469 $text = wp_strip_all_tags( $html );
470 $text = str_replace( array( "\r", "\n" ), '', $text );
471
472 return $text;
473
474 }
475
476 /** ==================================================
477 * Load Marquee Style
478 *
479 * @since 2.00
480 */
481 public function load_styles() {
482 $speed = $this->simpleticker_option['speed'];
483 wp_enqueue_style( 'simpleticker', plugin_dir_url( __DIR__ ) . 'css/marquee.css', array(), '1.00' );
484 $css = '.marquee > .marquee-inner { animation-duration: ' . $speed . 's; }';
485 wp_add_inline_style( 'simpleticker', $css );
486 }
487
488 }
489
490
491