ldc-poule-et-toque/content/plugins/elasticpress/includes/classes/Feature/RelatedPosts/Widget.php

133 lines
3.6 KiB
PHP
Raw Normal View History

2018-10-31 17:24:06 +01:00
<?php
/**
* Related posts widget
2021-04-12 15:54:29 +02:00
*
2018-10-31 17:24:06 +01:00
* @since 2.2
2021-04-12 15:54:29 +02:00
* @package elasticpress
2018-10-31 17:24:06 +01:00
*/
2021-04-12 15:54:29 +02:00
namespace ElasticPress\Feature\RelatedPosts;
use \WP_Widget as WP_Widget;
use ElasticPress\Features as Features;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Related posts widget class
*/
class Widget extends WP_Widget {
2018-10-31 17:24:06 +01:00
/**
* Initialize the widget
*
* @since 6.4
*/
public function __construct() {
$options = array( 'description' => esc_html__( 'Show related posts using ElasticPress. This widget will only appear on single post, page, and custom type pages.', 'elasticpress' ) );
parent::__construct( 'ep-related-posts', esc_html__( 'ElasticPress - Related Posts', 'elasticpress' ), $options );
}
/**
* Display widget
*
2021-04-12 15:54:29 +02:00
* @param array $args Widget arguments
* @param array $instance Widget instance variables
2018-10-31 17:24:06 +01:00
* @since 2.2
*/
public function widget( $args, $instance ) {
if ( ! is_single() ) {
return;
}
$related_posts = get_transient( 'ep_related_posts_' . get_the_ID() );
if ( false === $related_posts ) {
2021-04-12 15:54:29 +02:00
$related_posts = Features::factory()->get_registered_feature( 'related_posts' )->find_related( get_the_ID(), $instance['num_posts'] );
2018-10-31 17:24:06 +01:00
if ( empty( $related_posts ) ) {
2021-04-12 15:54:29 +02:00
if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
2018-10-31 17:24:06 +01:00
set_transient( 'ep_related_posts_' . get_the_ID(), '', HOUR_IN_SECONDS ); // Let's not spam
}
return;
}
ob_start();
2021-04-12 15:54:29 +02:00
echo wp_kses_post( $args['before_widget'] );
2018-10-31 17:24:06 +01:00
if ( ! empty( $instance['title'] ) ) {
2021-04-12 15:54:29 +02:00
echo wp_kses_post( $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'] );
2018-10-31 17:24:06 +01:00
}
?>
<ul>
2021-04-12 15:54:29 +02:00
<?php foreach ( $related_posts as $post ) : ?>
2018-10-31 17:24:06 +01:00
<li><a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>"><?php echo esc_html( get_the_title( $post->ID ) ); ?></a></li>
<?php endforeach; ?>
</ul>
<?php
wp_reset_postdata();
2021-04-12 15:54:29 +02:00
echo wp_kses_post( $args['after_widget'] );
2018-10-31 17:24:06 +01:00
$related_posts = ob_get_clean();
2021-04-12 15:54:29 +02:00
if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
2018-10-31 17:24:06 +01:00
set_transient( 'ep_related_posts_' . get_the_ID(), $related_posts, HOUR_IN_SECONDS );
}
}
2021-04-12 15:54:29 +02:00
echo wp_kses_post( $related_posts );
2018-10-31 17:24:06 +01:00
}
/**
* Display widget settings form
*
2021-04-12 15:54:29 +02:00
* @param array $instance Widget instance variables
2018-10-31 17:24:06 +01:00
* @since 2.2
*/
public function form( $instance ) {
2021-04-12 15:54:29 +02:00
$title = ( isset( $instance['title'] ) ) ? $instance['title'] : '';
2018-10-31 17:24:06 +01:00
$num_posts = ( isset( $instance['num_posts'] ) ) ? $instance['num_posts'] : 5;
?>
<p>
2021-04-12 15:54:29 +02:00
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
2018-10-31 17:24:06 +01:00
<?php esc_html_e( 'Title:', 'elasticpress' ); ?>
</label>
2021-04-12 15:54:29 +02:00
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
2018-10-31 17:24:06 +01:00
</p>
<p>
2021-04-12 15:54:29 +02:00
<label for="<?php echo esc_attr( $this->get_field_id( 'num_posts' ) ); ?>">
2018-10-31 17:24:06 +01:00
<?php esc_html_e( 'Number of Posts to Show:', 'elasticpress' ); ?>
</label>
2021-04-12 15:54:29 +02:00
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'num_posts' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'num_posts' ) ); ?>" type="text" value="<?php echo absint( $num_posts ); ?>" />
2018-10-31 17:24:06 +01:00
</p>
<?php
}
/**
* Update widget settings
2021-04-12 15:54:29 +02:00
*
* @param array $new_instance New instance settings
* @param array $old_instance Old instance settings
2018-10-31 17:24:06 +01:00
* @since 2.2
* @return array
*/
public function update( $new_instance, $old_instance ) {
2021-04-12 15:54:29 +02:00
$instance = [];
$instance['title'] = sanitize_text_field( $new_instance['title'] );
2018-10-31 17:24:06 +01:00
$instance['num_posts'] = absint( $new_instance['num_posts'] );
return $instance;
}
}