| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class News_Ticker { |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
add_action( 'init', array( $this, 'register' ) ); |
| 10 |
} |
| 11 |
|
| 12 |
public function get_attributes() { |
| 13 |
return array( |
| 14 |
'blockId' => '', |
| 15 |
'previewImg' => '', |
| 16 |
// Label Style |
| 17 |
'tickLabelColor' => '#fff', |
| 18 |
'tickLabelBg' => '#1974d2', |
| 19 |
'tickLabelTypo' => (object) array( |
| 20 |
'openTypography' => 1, |
| 21 |
'size' => (object) array( |
| 22 |
'lg' => '16', |
| 23 |
'unit' => 'px', |
| 24 |
), |
| 25 |
'spacing' => (object) array( |
| 26 |
'lg' => '0', |
| 27 |
'unit' => 'px', |
| 28 |
), |
| 29 |
'height' => (object) array( |
| 30 |
'lg' => '27', |
| 31 |
'unit' => 'px', |
| 32 |
), |
| 33 |
'decoration' => 'none', |
| 34 |
'transform' => '', |
| 35 |
'family' => '', |
| 36 |
'weight' => '500', |
| 37 |
), |
| 38 |
'tickLabelPadding' => '15', |
| 39 |
'tickLabelSpace' => (object) array( 'lg' => '160' ), |
| 40 |
'tickShapeStyle' => 'normal', |
| 41 |
// Ticker body Style |
| 42 |
'tickerContentHeight' => '45', |
| 43 |
'tickBodyColor' => '#444', |
| 44 |
'tickBodyHovColor' => '#7d7d7d', |
| 45 |
'tickBodyListColor' => '#037fff', |
| 46 |
'tickerBodyBg' => '#eeeeee', |
| 47 |
'tickBodyTypo' => (object) array( |
| 48 |
'openTypography' => 1, |
| 49 |
'size' => (object) array( |
| 50 |
'lg' => '16', |
| 51 |
'unit' => 'px', |
| 52 |
), |
| 53 |
'spacing' => (object) array( |
| 54 |
'lg' => '0', |
| 55 |
'unit' => 'px', |
| 56 |
), |
| 57 |
'height' => (object) array( |
| 58 |
'lg' => '16', |
| 59 |
'unit' => 'px', |
| 60 |
), |
| 61 |
'decoration' => 'none', |
| 62 |
'transform' => '', |
| 63 |
'family' => '', |
| 64 |
'weight' => '500', |
| 65 |
), |
| 66 |
'tickBodyBorderColor' => '', |
| 67 |
'tickBodyBorder' => (object) array( |
| 68 |
'openBorder' => 0, |
| 69 |
'disableColor' => true, |
| 70 |
'width' => (object) array( |
| 71 |
'top' => 0, |
| 72 |
'right' => 0, |
| 73 |
'bottom' => 0, |
| 74 |
'left' => 0, |
| 75 |
), |
| 76 |
'type' => 'solid', |
| 77 |
), |
| 78 |
'tickBodyRadius' => (object) array( |
| 79 |
'lg' => '', |
| 80 |
'unit' => 'px', |
| 81 |
), |
| 82 |
'tickTxtStyle' => 'normal', |
| 83 |
'tickImgWidth' => '30', |
| 84 |
'tickImgSpace' => '10', |
| 85 |
'tickImgRadius' => (object) array( |
| 86 |
'lg' => (object) array( |
| 87 |
'top' => '30', |
| 88 |
'bottom' => '30', |
| 89 |
'left' => '30', |
| 90 |
'right' => '30', |
| 91 |
'unit' => 'px', |
| 92 |
), |
| 93 |
), |
| 94 |
'tickBodySpace' => (object) array( 'lg' => '21' ), |
| 95 |
// Body Time Badge Style |
| 96 |
'timeBadgeType' => 'days_ago', |
| 97 |
'timeBadgeDateFormat' => 'M j, Y', |
| 98 |
'tickTimeBadge' => 'Time Badge', |
| 99 |
'timeBadgeColor' => '#fff', |
| 100 |
'timeBadgeBg' => (object) array( |
| 101 |
'openColor' => 1, |
| 102 |
'type' => 'color', |
| 103 |
'color' => '#444', |
| 104 |
), |
| 105 |
'timeBadgeTypo' => (object) array( |
| 106 |
'openTypography' => 1, |
| 107 |
'size' => (object) array( |
| 108 |
'lg' => '12', |
| 109 |
'unit' => 'px', |
| 110 |
), |
| 111 |
'spacing' => (object) array( |
| 112 |
'lg' => '0', |
| 113 |
'unit' => 'px', |
| 114 |
), |
| 115 |
'height' => (object) array( |
| 116 |
'lg' => '16', |
| 117 |
'unit' => 'px', |
| 118 |
), |
| 119 |
'decoration' => 'none', |
| 120 |
'transform' => '', |
| 121 |
'family' => '', |
| 122 |
'weight' => '500', |
| 123 |
), |
| 124 |
'timeBadgeRadius' => (object) array( |
| 125 |
'lg' => (object) array( |
| 126 |
'top' => '100', |
| 127 |
'bottom' => '100', |
| 128 |
'left' => '100', |
| 129 |
'right' => '100', |
| 130 |
'unit' => 'px', |
| 131 |
), |
| 132 |
), |
| 133 |
'timeBadgePadding' => (object) array( |
| 134 |
'lg' => (object) array( |
| 135 |
'top' => 3, |
| 136 |
'bottom' => 3, |
| 137 |
'left' => 6, |
| 138 |
'right' => 6, |
| 139 |
'unit' => 'px', |
| 140 |
), |
| 141 |
), |
| 142 |
// Icon Navigator |
| 143 |
'TickNavStyle' => 'nav1', |
| 144 |
'TickNavIconStyle' => 'Angle2', |
| 145 |
'TickNavColor' => '#fff', |
| 146 |
'TickNavBg' => (object) array( |
| 147 |
'openColor' => 1, |
| 148 |
'type' => 'color', |
| 149 |
'color' => '#037fff', |
| 150 |
), |
| 151 |
'TickNavHovColor' => '#d0d0d0f4', |
| 152 |
'TickNavHovBg' => (object) array( |
| 153 |
'openColor' => 1, |
| 154 |
'type' => 'color', |
| 155 |
'color' => '#53a0ff', |
| 156 |
), |
| 157 |
// Pause Style |
| 158 |
'TickNavPause' => 'Pause Icon Style', |
| 159 |
'PauseColor' => '#fff', |
| 160 |
'PauseHovColor' => '#d0d0d0f4', |
| 161 |
'PauseBg' => (object) array( |
| 162 |
'openColor' => 1, |
| 163 |
'type' => 'color', |
| 164 |
'color' => '#2163ff', |
| 165 |
), |
| 166 |
'PauseHovBg' => (object) array( |
| 167 |
'openColor' => 1, |
| 168 |
'type' => 'color', |
| 169 |
'color' => '#53a0ff', |
| 170 |
), |
| 171 |
// Query Setting |
| 172 |
'queryQuick' => '', |
| 173 |
'queryNumPosts' => (object) array( 'lg' => 4 ), |
| 174 |
'queryNumber' => array( // for compatibility since v.2.5.4 |
| 175 |
'type' => 'string', |
| 176 |
'default' => 4, |
| 177 |
), |
| 178 |
'queryType' => 'post', |
| 179 |
'queryTax' => 'category', |
| 180 |
'queryTaxValue' => '[]', |
| 181 |
'queryRelation' => 'OR', |
| 182 |
'queryOrderBy' => 'date', |
| 183 |
'metaKey' => 'custom_meta_key', |
| 184 |
'queryOrder' => 'desc', |
| 185 |
'queryExclude' => '', |
| 186 |
'queryAuthor' => '[]', |
| 187 |
'queryOffset' => '0', |
| 188 |
'queryExcludeTerm' => '[]', |
| 189 |
'queryExcludeAuthor' => '[]', |
| 190 |
'querySticky' => true, |
| 191 |
'queryUnique' => '', |
| 192 |
'queryPosts' => '[]', |
| 193 |
'queryCustomPosts' => '[]', |
| 194 |
// General Setting |
| 195 |
'tickerType' => 'vertical', |
| 196 |
'tickerPositionEna' => false, |
| 197 |
'tickerPosition' => 'up', |
| 198 |
'tickerHeading' => true, |
| 199 |
'tickTimeShow' => true, |
| 200 |
'tickImageShow' => false, |
| 201 |
'navControlToggle' => true, |
| 202 |
'controlToggle' => true, |
| 203 |
'pauseOnHover' => true, |
| 204 |
'tickerDirectionVer' => 'up', |
| 205 |
'tickerDirectionHorizon' => 'left', |
| 206 |
'tickerSpeed' => '4000', |
| 207 |
'marqueSpeed' => '10', |
| 208 |
'tickerSpeedTypewriter' => '50', |
| 209 |
'tickerAnimation' => 'slide', |
| 210 |
'typeAnimation' => 'fadein', |
| 211 |
'openInTab' => false, |
| 212 |
// Heading Setting/Style |
| 213 |
'headingText' => 'News Ticker', |
| 214 |
// Advanced |
| 215 |
'advanceId' => '', |
| 216 |
'advanceZindex' => '', |
| 217 |
'hideExtraLarge' => false, |
| 218 |
'hideTablet' => false, |
| 219 |
'hideMobile' => false, |
| 220 |
'advanceCss' => '', |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
public function register() { |
| 225 |
register_block_type( |
| 226 |
'ultimate-post/news-ticker', |
| 227 |
array( |
| 228 |
'editor_script' => 'ultp-blocks-editor-script', |
| 229 |
'editor_style' => 'ultp-blocks-editor-css', |
| 230 |
'render_callback' => array( $this, 'content' ), |
| 231 |
) |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
public function content( $attr, $noAjax ) { |
| 236 |
$attr = wp_parse_args( $attr, $this->get_attributes() ); |
| 237 |
global $unique_ID; |
| 238 |
|
| 239 |
if ( ! $noAjax ) { |
| 240 |
$paged = is_front_page() ? get_query_var( 'page' ) : get_query_var( 'paged' ); |
| 241 |
$attr['paged'] = $paged ? $paged : 1; |
| 242 |
} |
| 243 |
|
| 244 |
$block_name = 'news-ticker'; |
| 245 |
$wraper_before = $wraper_after = $post_loop = ''; |
| 246 |
|
| 247 |
$attr['queryNumber'] = ultimate_post()->get_post_number( 4, $attr['queryNumber'], $attr['queryNumPosts'] ); |
| 248 |
$recent_posts = new \WP_Query( ultimate_post()->get_query( $attr ) ); |
| 249 |
|
| 250 |
$arrowLeft = ''; |
| 251 |
$arrowRight = ''; |
| 252 |
if ( $attr['TickNavIconStyle'] != 'icon2' ) { |
| 253 |
$arrowLeft .= ultimate_post()->get_svg_icon( 'left' . $attr['TickNavIconStyle'] ); |
| 254 |
$arrowRight .= ultimate_post()->get_svg_icon( 'right' . $attr['TickNavIconStyle'] ); |
| 255 |
} |
| 256 |
|
| 257 |
$attr['className'] = isset( $attr['className'] ) && $attr['className'] ? preg_replace( '/[^A-Za-z0-9_ -]/', '', $attr['className'] ) : ''; |
| 258 |
$attr['advanceId'] = isset( $attr['advanceId'] ) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 259 |
$attr['blockId'] = isset( $attr['blockId'] ) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 260 |
$attr['TickNavStyle'] = sanitize_html_class( $attr['TickNavStyle'] ); |
| 261 |
$attr['TickNavIconStyle'] = sanitize_html_class( $attr['TickNavIconStyle'] ); |
| 262 |
$attr['tickerType'] = sanitize_html_class( $attr['tickerType'] ); |
| 263 |
$attr['headingText'] = wp_kses( $attr['headingText'], ultimate_post()->ultp_allowed_html_tags() ); |
| 264 |
$attr['typeAnimation'] = sanitize_html_class( $attr['typeAnimation'] ); |
| 265 |
$attr['tickerAnimation'] = sanitize_html_class( $attr['tickerAnimation'] ); |
| 266 |
$attr['tickerDirectionVer'] = sanitize_html_class( $attr['tickerDirectionVer'] ); |
| 267 |
$attr['tickerDirectionHorizon'] = sanitize_html_class( $attr['tickerDirectionHorizon'] ); |
| 268 |
$attr['tickerSpeed'] = sanitize_html_class( $attr['tickerSpeed'] ); |
| 269 |
$attr['marqueSpeed'] = sanitize_html_class( $attr['marqueSpeed'] ); |
| 270 |
$attr['tickTxtStyle'] = sanitize_html_class( $attr['tickTxtStyle'] ); |
| 271 |
$attr['pauseOnHover'] = isset( $attr['pauseOnHover'] ) ? $attr['pauseOnHover'] == true : true; |
| 272 |
|
| 273 |
if ( $recent_posts->have_posts() ) { |
| 274 |
$wraper_before .= '<div ' . ( $attr['advanceId'] ? 'id="' . $attr['advanceId'] . '" ' : '' ) . ' class="wp-block-ultimate-post-' . $block_name . ' ultp-block-' . $attr['blockId'] . '' . ( $attr['className'] ? ' ' . $attr['className'] : '' ) . '">'; |
| 275 |
$wraper_before .= '<div class="ultp-block-wrapper ultp-news-sticky">'; |
| 276 |
$wraper_before .= '<div class="ultp-block-items-wrap">'; |
| 277 |
|
| 278 |
$post_loop .= '<div class="ultp-news-ticker-' . $attr['TickNavStyle'] . ' ultp-nav-' . $attr['TickNavIconStyle'] . ' ultp-newsTicker-wrap ultp-newstick-' . $attr['tickerType'] . ' ">'; |
| 279 |
if ( $attr['tickerHeading'] && $attr['headingText'] ) { |
| 280 |
$post_loop .= '<div class="ultp-news-ticker-label">' . $attr['headingText'] . '</div>'; |
| 281 |
} |
| 282 |
|
| 283 |
$post_loop .= '<div class="ultp-news-ticker-box ultp-animation-' . ( $attr['tickerType'] == 'typewriter' ? $attr['typeAnimation'] : $attr['tickerAnimation'] ) . ' ultp-sliderDir-' . ( $attr['tickerType'] == 'vertical' ? $attr['tickerDirectionVer'] : $attr['tickerDirectionHorizon'] ) . '">'; |
| 284 |
$speed = $attr['tickerType'] != 'marquee' ? $attr['tickerSpeed'] : $attr['marqueSpeed']; |
| 285 |
$post_loop .= '<ul class="ultp-news-ticker" data-type="' . $attr['tickerType'] . '" data-hover="' . $attr['pauseOnHover'] . '" data-direction="' . ( $attr['tickerType'] == 'vertical' ? $attr['tickerDirectionVer'] : $attr['tickerDirectionHorizon'] ) . '" data-speed="' . $speed . '">'; |
| 286 |
while ( $recent_posts->have_posts() ) : |
| 287 |
$recent_posts->the_post(); |
| 288 |
$post_id = get_the_ID(); |
| 289 |
$title = get_the_title( $post_id ); |
| 290 |
$titlelink = get_permalink( $post_id ); |
| 291 |
if ( $attr['queryUnique'] ) { |
| 292 |
$unique_ID[ $attr['queryUnique'] ][] = $post_id; |
| 293 |
} |
| 294 |
$typeStyle = $style = ''; |
| 295 |
if ( $attr['tickerType'] != 'marquee' && $attr['tickerType'] != 'typewriter' && $attr['tickerAnimation'] == 'fadeout' ) { |
| 296 |
$style .= 'animation-delay:' . ( $speed - 1000 ) . 'ms'; |
| 297 |
} |
| 298 |
if ( $attr['tickerType'] == 'typewriter' && $attr['typeAnimation'] == 'fadeout' ) { |
| 299 |
$style .= 'animation-delay:' . ( $speed - 1000 ) . 'ms'; |
| 300 |
} |
| 301 |
if ( $attr['tickerType'] == 'typewriter' ) { |
| 302 |
$typeStyle .= ' animation-duration:' . ( $speed - 1000 ) . 'ms'; |
| 303 |
} |
| 304 |
$post_loop .= '<li style=' . $style . '>'; |
| 305 |
$post_loop .= '<div class="ultp-list-' . $attr['tickTxtStyle'] . '">'; |
| 306 |
$post_loop .= '<a ' . ( $attr['openInTab'] ? 'target="_blank"' : '' ) . ' href="' . $titlelink . '">'; |
| 307 |
if ( $attr['tickImageShow'] && has_post_thumbnail() ) { |
| 308 |
$post_loop .= ultimate_post()->get_image( get_post_thumbnail_id( $post_id ), 'thumbnail', '', $title, '', '' ); |
| 309 |
} |
| 310 |
$post_loop .= '<span style="' . $typeStyle . '" class="title-text" data-text="' . $title . '">' . $title . '</span>'; |
| 311 |
$post_loop .= '</a>'; |
| 312 |
if ( $attr['tickTimeShow'] && ( $attr['tickerType'] != 'typewriter' ) ) { |
| 313 |
$post_loop .= '<span class="ultp-ticker-timebadge">'; |
| 314 |
if ( $attr['timeBadgeType'] == 'date' ) { |
| 315 |
$dateData = get_the_date( ultimate_post()->get_format( $attr['timeBadgeDateFormat'] ) ); |
| 316 |
} else { |
| 317 |
$dateData = human_time_diff( get_the_time( 'U' ), current_time( 'U' ) ) . ' ago'; |
| 318 |
} |
| 319 |
$post_loop .= $dateData . '</span>'; |
| 320 |
} |
| 321 |
$post_loop .= '</div>'; |
| 322 |
$post_loop .= '</li>'; |
| 323 |
endwhile; |
| 324 |
$post_loop .= '</ul>'; |
| 325 |
$post_loop .= '</div>'; |
| 326 |
|
| 327 |
if ( $attr['navControlToggle'] && $attr['TickNavStyle'] ) { |
| 328 |
$post_loop .= '<div class="ultp-news-ticker-controls ultp-news-ticker-vertical-controls">'; |
| 329 |
$post_loop .= '<button aria-label="' . __( 'Show Previous Post', 'ultimate-post' ) . '" class="ultp-news-ticker-arrow ultp-news-ticker-prev prev">' . $arrowLeft . '</button>'; |
| 330 |
if ( $attr['controlToggle'] && ( $attr['TickNavStyle'] == 'nav1' || $attr['TickNavStyle'] == 'nav3' || $attr['TickNavStyle'] == 'nav4' ) ) { |
| 331 |
$post_loop .= '<button aria-label="' . __( 'Pause Current Post', 'ultimate-post' ) . '" id="ultp-pause-btn" class="ultp-news-ticker-pause pause"></button>'; |
| 332 |
} |
| 333 |
$post_loop .= '<button aria-label="' . __( 'Show Next Post', 'ultimate-post' ) . '" class="ultp-news-ticker-arrow ultp-news-ticker-next next">' . $arrowRight . '</button>'; |
| 334 |
$post_loop .= '</div>'; |
| 335 |
} |
| 336 |
$post_loop .= '</div>'; |
| 337 |
$wraper_after .= '</div>'; |
| 338 |
$wraper_after .= '</div>'; |
| 339 |
$wraper_after .= '</div>'; |
| 340 |
|
| 341 |
wp_reset_query(); |
| 342 |
} |
| 343 |
|
| 344 |
return $noAjax ? $post_loop : $wraper_before . $post_loop . $wraper_after; |
| 345 |
} |
| 346 |
} |
| 347 |
|