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

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