Files
DESKTOP-GBA0BK8\Admin 7c8c8b1c76 first commit
2023-04-08 12:19:53 -04:00

330 lines
9.3 KiB
PHP

<?php
namespace WPC\Widgets;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use WP_Query;
if (!defined('ABSPATH')) exit; // Exit if accessed directly
class FeatureArticle extends Widget_Base
{
public function get_name()
{
return 'feature-article';
}
public function get_title()
{
return 'Feature Article';
}
public function get_icon()
{
return 'fas fa-image';
}
public function get_categories()
{
return ['basic'];
}
protected function _register_controls()
{
$this->start_controls_section('section_content', ['label' => 'Settings', ]);
$this->add_control(
'feature_article',
[
'label' => __( 'Feature article', 'plugin-name' ),
'type' => \Elementor\Controls_Manager::SELECT,
'options' => [
'manual' => __( 'Manual', 'plugin-name' ),
'auto' => __( 'Auto', 'plugin-name' ),
],
'default' => 'manual',
]
);
$this->add_control(
'post_select',
[
'label' => __( 'Post select', 'plugin-name' ),
'type' => \Elementor\Controls_Manager::SELECT,
'options' => $this->getPublishedPosts(),
'placeholder' => 'Select post',
'condition' => [
'feature_article' => 'manual'
]
]
);
$this->add_control(
'swap_day',
[
'label' => __( 'Swap day', 'plugin-name' ),
'type' => \Elementor\Controls_Manager::SELECT,
'options' => [
'0' => 'Sunday',
'1' => 'Monday',
'2' => 'Tuesday',
'3' => 'Wednesday',
'4' => 'Thursday',
'5' => 'Friday',
'6' => 'Saturday'
],
'default' => '1',
'condition' => [
'feature_article' => 'auto'
]
]
);
$this->end_controls_section();
$this->start_controls_section(
'style_section',
[
'label' => __( 'Style', 'plugin-name' ),
'tab' => \Elementor\Controls_Manager::TAB_STYLE,
]
);
$this->add_control(
'title_color',
[
'label' => __( 'Title Color', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::COLOR,
'scheme' => [
'type' => \Elementor\Scheme_Color::get_type(),
'value' => \Elementor\Scheme_Color::COLOR_1,
],
'selectors' => [
'{{WRAPPER}} .title' => 'color: {{VALUE}}',
],
'default' => '#FFFFFF'
]
);
$this->add_control(
'divider_color',
[
'label' => __( 'Divider Color', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::COLOR,
'scheme' => [
'type' => \Elementor\Scheme_Color::get_type(),
'value' => \Elementor\Scheme_Color::COLOR_1,
],
'selectors' => [
'{{WRAPPER}} .divider' => 'border-top: 2px solid {{VALUE}}',
],
'default' => '#FFFFFF'
]
);
$this->add_control(
'article_meta_color',
[
'label' => __( 'Meta Color', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::COLOR,
'scheme' => [
'type' => \Elementor\Scheme_Color::get_type(),
'value' => \Elementor\Scheme_Color::COLOR_1,
],
'selectors' => [
'{{WRAPPER}} .article-meta' => 'color: {{VALUE}}',
],
'default' => '#FFFFFF'
]
);
$this->add_control(
'read_more_color',
[
'label' => __( 'Read More Color', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::COLOR,
'scheme' => [
'type' => \Elementor\Scheme_Color::get_type(),
'value' => \Elementor\Scheme_Color::COLOR_1,
],
'selectors' => [
'{{WRAPPER}} .read-more' => 'color: {{VALUE}}; border: 1px solid {{VALUE}};',
],
'default' => '#FFFFFF'
]
);
$this->end_controls_section();
}
protected function render()
{
global $wpdb;
$settings = $this->get_settings_for_display();
$post_id = null;
if ($settings['feature_article'] === 'auto') {
$article_setting = $wpdb->get_results(
'
SELECT *
FROM wp_feature_article
LIMIT 1
'
)[0];
if (empty($article_setting)) {
$wpdb->insert(
'wp_feature_article',
[
'post_id' => $this->getRandomPostID(),
'swap_day' => $settings['swap_day'],
'last_swap' => date('Y-m-d H:i:s')
]
);
} else {
if ($settings['swap_day'] === $article_setting->swap_day
&& date('Y-m-d') > date('Y-m-d', strtotime($article_setting->last_swap))
&& date('w') === $article_setting->swap_day) {
$wpdb->update(
'wp_feature_article',
[
'post_id' => $this->getRandomPostID(),
'swap_day' => $settings['swap_day'],
'last_swap' => date('Y-m-d H:i:s')
],
[
'swap_day' => $settings['swap_day']
]
);
}
if ($settings['swap_day'] !== $article_setting->swap_day) {
$wpdb->update(
'wp_feature_article',
[
'post_id' => $this->getRandomPostID(),
'swap_day' => $settings['swap_day'],
'last_swap' => date('Y-m-d H:i:s')
],
[
'swap_day' => $article_setting->swap_day,
]
);
}
}
$article_setting = $wpdb->get_results(
'
SELECT *
FROM wp_feature_article
LIMIT 1
'
)[0];
$post_id = $article_setting->post_id;
} else {
$post_id = $settings['post_select'];
}
$query = new WP_Query(['p' => $post_id]);
$post = $query->posts[0];
$html =
'<div class="feature-article-block text-center" style="background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(' . get_the_post_thumbnail_url($post->ID) . ');">
<div class="content-wrapper">';
// Get categories
$categories = get_the_category($post->ID);
$categories_template_arr = [];
if (!empty($categories)) {
$html .= '<div class="category-wrapper d-none d-md-block text-uppercase">';
foreach ($categories as $category) {
$category = '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a>';
array_push($categories_template_arr, $category);
}
$html .= join(' / ', $categories_template_arr);
$html .= '</div>';
}
// Get co-authors
$authors = [];
$co_authors = get_coauthors($post->ID);
foreach($co_authors as $co_author) {
if(get_class($co_author) !== 'WP_User') {
array_push($authors, $co_author->display_name);
continue;
}
array_push($authors, $co_author->data->display_name);
}
if (empty($authors)) {
$post_author = get_the_author_meta( 'display_name' , get_post_field('post_author') );
array_push($authors, $post_author);
}
$authors = join(', ', $authors);
$html .=
'<div class="divider"></div>
<a href="' . get_permalink($post->ID) . '"><h2 class="title text-truncate-2-line" title="' . $post->post_title . '">' . $post->post_title . '</h2></a>
<div class="divider"></div>
<p class="article-meta text-uppercase text-truncate" title="' . $authors . '">' . get_the_date( 'F j, Y', $post->ID ) . ' by ' . $authors . '</p>
<a class="read-more text-uppercase" href="' . get_permalink($post->ID) . '">Read more</a>
</div>
</div>';
echo $html;
}
protected function getPublishedPosts() {
$result = [];
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1000,
'ignore_sticky_posts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
];
$post_query = new WP_Query( $args );
$posts = $post_query->posts;
foreach ($posts as $post) {
$result += [$post->ID => $post->post_title];
}
return $result;
}
protected function getRandomPostID() {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 1,
'orderby' => 'rand'
);
$query = new WP_Query ( $args );
$random_post = $query->posts[0];
return $random_post->ID;
}
}