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 = '
'; // Get categories $categories = get_the_category($post->ID); $categories_template_arr = []; if (!empty($categories)) { $html .= '
'; foreach ($categories as $category) { $category = '' . esc_html( $category->name ) . ''; array_push($categories_template_arr, $category); } $html .= join(' / ', $categories_template_arr); $html .= '
'; } // 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 .= '

' . $post->post_title . '

Read more
'; 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; } }