File: /var/www/html/wp-content/plugins/td-oi-plugin/shortcodes/td_cwn_authors_slider.php
<?php
class td_cwn_authors_slider extends td_block {
var string $role;
var string $show_dots;
public function get_custom_css() {
// $unique_block_class - the unique class that is on the block. use this to target the specific instance via css
$unique_block_class = ((td_util::tdc_is_live_editor_iframe() || td_util::tdc_is_live_editor_ajax()) ? 'tdc-row .' : '') . $this->block_uid;
$compiled_css = '';
$raw_css =
"<style>
</style>";
$td_css_res_compiler = new td_css_res_compiler( $raw_css );
$td_css_res_compiler->load_settings( __CLASS__ . '::cssMedia', $this->get_all_atts() );
$compiled_css .= $td_css_res_compiler->compile_css();
return $compiled_css;
}
static function cssMedia( $res_ctx ) {
}
/**
* Disable loop block features. This block does not use a loop and it doesn't need to run a query.
*/
function __construct() {
parent::disable_loop_block_features();
$this->show_dots = 'true';
}
function render( $atts, $content = null ) {
$in_composer = td_util::tdc_is_live_editor_iframe() || td_util::tdc_is_live_editor_ajax();
$uniqClass = 'slick-'.uniqid();
parent::render( $atts ); // sets the live atts, $this->atts, $this->block_uid, $this->td_query (it runs the query)
// $shift_slide = $this->get_att('shift_slide');
// $shift_slide = $shift_slide * -1;
// $shift_slide = $shift_slide.'px';
$trim_title = ( "" == $this->get_att('trim_title') ? 250 : $this->get_att('trim_title'));
$user_role = ('' != $this->get_att('user_role')) ? $this->get_att('user_role') : 'author';
$post_type = ('' != $this->get_att('post_type')) ? $this->get_att('post_type') : 'post';
$authors_count = ( '' != $this->get_att('authors_count') ) ? $this->get_att('authors_count') : -1;
$authors_offset = $this->get_att('authors_offset');
// false == horizzontal, true == vertical;
$slider_vertical_orientation = $this->get_att('slider_orientation');
$this->role = $user_role;
// Rivedere la politica
$max_authors_displayed = ( $authors_count != -1 && $authors_count <= 4 ) ? $authors_count : '4';
if ( $max_authors_displayed < 4 && $max_authors_displayed != -1 ) {
$this->show_dots = 'false';
}
$authors = get_users(
array(
'role' => $user_role
)
);
// $args = array(
// 'role' => $user_role,
// 'number' => -1,
// 'offset' => 6,
// 'orderby' => 'ID',
// 'order' => 'ASC'
// );
//
// $user_query = new WP_User_Query($args);
// $authors = $user_query->get_results();
// echo '<pre><h3>$authors--</h3>';
// print_r($authors);
// echo '</pre>';
$authors_data = array();
foreach ( $authors as $author ) {
$args = array(
'post_type' => $post_type,
'author' => $author->ID,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 1
);
// echo $author->display_name .'<br>';
$recent_post_query = new WP_Query($args);
if ( $recent_post_query->have_posts() )
{
/** Get the users avatar */
$user_avatars = get_user_meta( $author->ID, 'wp_user_avatars', true );
$user_avatar = ('' != $user_avatars) ? $user_avatars['250'] : TD_OI_URL . '/assets/img/user.png';
$recent_post_query->the_post();
if ('post' == $post_type) {
$category = get_the_category( get_the_ID() );
// $category_name = $category[0]->name;
// $category_id = $category[0]->term_id;
// $category_link = get_term_link($category_id);
}else {
$category = wp_get_post_terms( get_the_ID(), 'oi_rubrica_category' );
// $category_name = $category[0]->name;
// $category_id = $category[0]->term_id;
// $category_link = get_term_link($category_id);
}
$category_name = $category[0]->name;
$category_id = $category[0]->term_id;
$category_link = get_term_link($category_id);
// echo '<pre><h3>$author</h3>';
// print_r($author);
// echo '</pre>';
$authors_data[] = array(
'author_id' => $author->ID,
'author_name' => $author->display_name,
'author_image' => $user_avatar,
'author_link' => get_author_posts_url($author->ID),
'last_post_id' => get_the_ID(),
'last_post_title' => get_the_title(),
'last_post_date' => get_the_date('Y-m-d H:i:s'),
'last_post_link' => get_the_permalink(),
'last_post_category_name' => $category_name,
'last_post_category_id' => $category_id,
'last_post_category_link' => $category_link
);
}
else
{
//$no_posts = '<div class="tdcwn-no-posts">There are no posts for the selected options.</div>';
}
wp_reset_postdata();
}
/** Sort authors by last post date */
usort( $authors_data, function($a, $b){
return strtotime($b['last_post_date']) - strtotime($a['last_post_date']);
} );
$authors_data = array_slice($authors_data, $authors_offset );
if ( 0 < $authors_count ) {
array_splice($authors_data, $authors_count );
}
/** Code for splitting the array for offset */
/** $authors_data -> array_slice($authors_data, 0, 1) */
/** (end of) Code for splitting the array for offset */
$buffy = ''; //output buffer
$buffy .= '<div class="' . $this->get_block_classes() . '" ' . $this->get_block_html_atts() . '>';
//get the block css
$buffy .= $this->get_block_css();
//get the js for this block
$buffy .= $this->get_block_js();
$arrows_display = ('false' == $slider_vertical_orientation) ? 'true' : 'false';
$sliders_to_show = ('false' == $slider_vertical_orientation) ? '2' : '4';
$sliders_to_scroll = ('false' == $slider_vertical_orientation) ? '2' : '4';
$buffy .= "
<style>
/* Slick Slider */
.$uniqClass .tdcwn-author {
// text-align: center;
padding: 0 30px;
}
.$uniqClass .author-data {
display: flex;
}
.$uniqClass .author-meta{
padding: 0 0 0 12px;
align-items: center;
}
.$uniqClass .tdcwn-author img {
border-radius: 100%;
margin: 0 auto;
width: 50px;
height: 50px;
max-width: 50px;
display: block !important;
}
.$uniqClass .tdcwn-author .author_name {
font-size: 12px;
font-weight: 400;
margin: 0;
display: block;
color: #808285;
text-decoration: underline;
text-decoration-color: #007acb;
text-underline-offset: 2px;
}
.$uniqClass .tdcwn-author .post_title {
font-weight: 700;
font-size: 16px;
display: block;
color: #000000;
font-family: 'Poppins';
}
.$uniqClass .tdcwn-author .post_category {
font-size: 12px;
font-weight: 600;
color: #007acb;
}
.$uniqClass .slick-arrow:before {
color: #000;
}
.$uniqClass .tdcwn-authors-slider .slick-prev {
left: -30px;
}
// .$uniqClass .tdcwn-authors-slider .slick-next {
// right: 30px;
// }
.$uniqClass .tdcwn-no-posts {
background: #F7F9F9;
border: 1px solid #d0cfcf;
color: #9BA1A8;
font-size: 13px;
font-weight: 600;
text-align: center;
padding: 10px 20px;
}
.$uniqClass.vertical-slider .tdcwn-author {
margin-bottom: 20px;
}
@media (max-width: 480px) {
.$uniqClass .slick-list {
height: unset !important;
}
}
</style>
";
if ( isset($no_posts) )
{
$buffy .= $no_posts;
}
else {
$buffy .= '<div class="tdw-block-inner td-fix-index">';
$buffy .= '<div class="tdcwn-authors-slider '.$uniqClass.('true' == $slider_vertical_orientation ? ' vertical-slider' : '') . '">';
foreach ( $authors_data as $author )
{
ob_start();
?>
<div class="tdcwn-author">
<div class="author-data">
<div class="author-image">
<a href="<?php echo $author['author_link']; ?>">
<img src="<?php echo $author['author_image']; ?>" alt="<?php echo $author['author_name']; ?>" />
</a>
</div>
<div class="author-meta">
<a href="<?php echo $author['last_post_category_link']; ?>" class="post_category"><?php echo $author['last_post_category_name']; ?></a>
<a href="<?php echo $author['author_link']; ?>" class="author_name"><?php echo $author['author_name']; ?></a>
</div>
</div>
<a href="<?php echo $author['last_post_link']; ?>" class="post_title"><?php echo wp_trim_words($author['last_post_title'], $trim_title); ?></a>
</div>
<?php
$buffy .= ob_get_clean();
}
$buffy .= '</div>';
$buffy .= '</div>';
}
$buffy .= '</div>';
ob_start();
?>
<script>
document.addEventListener('DOMContentLoaded', function () {
// Pure JavaScript for other interactions
// Example: Hide all images on click
document.querySelectorAll('img').forEach(function(img) {
img.addEventListener('click', function() {
this.style.display = 'none';
});
});
// Minimal jQuery for Slick Slider
jQuery(function($) {
jQuery('.<?php echo $uniqClass; ?>').slick({
dots: <?php echo $this->show_dots; ?>,
arrows: <?php echo $arrows_display; ?>,
infinite: false,
speed: 500,
slidesToShow: <?php echo $max_authors_displayed; ?>,
slidesToScroll: <?php echo $max_authors_displayed; ?>,
adaptiveHeight: false,
vertical: <?php echo $slider_vertical_orientation; ?>,
responsive: [
{
breakpoint: 768,
settings: {
arrows: false,
centerMode: true,
centerPadding: '40px',
slidesToShow: <?php echo $sliders_to_show; ?>,
slidesToScroll: <?php echo $sliders_to_scroll; ?>,
centerMode: false
}
},
{
breakpoint: 480,
settings: {
arrows: false,
centerMode: true,
centerPadding: '40px',
slidesToShow: <?php echo 'true' == $slider_vertical_orientation ? count($authors_data) : 1 ?>,
slidesToScroll: <?php echo 'true' == $slider_vertical_orientation ? count($authors_data) : 1 ?>,
centerMode: false
}
}
]
});
});
});
</script>
<?php
$buffy .= ob_get_clean();
return $buffy;
}
}