widget_fields = array( array( 'label' => esc_html__( 'Posts Per Page', 'book-review-blog' ), 'id' => 'postsperpage', 'default' => '4', 'type' => 'number', ), array( 'label' => esc_html__( 'Show Post Date', 'book-review-blog' ), 'id' => 'showpostdate', 'default' => '1', 'type' => 'checkbox', ), array( 'label' => esc_html__( 'Category', 'book-review-blog' ), 'id' => 'category', 'default' => '', 'type' => 'select', 'options' => $this->get_category_options(), ), array( 'label' => esc_html__( 'Show Excerpt', 'book-review-blog' ), 'id' => 'showexcerpt', 'default' => '', 'type' => 'checkbox', ), array( 'label' => esc_html__( 'Excerpt Length (words)', 'book-review-blog' ), 'id' => 'excerptlength', 'default' => '20', 'type' => 'number', ), array( 'label' => esc_html__( 'Show Post Author', 'book-review-blog' ), 'id' => 'showpostauthor', 'default' => '', 'type' => 'checkbox', ), ); } private $widget_fields; private function get_category_options() { $categories = get_categories(); $options = array( '' => esc_html__( 'All Categories', 'book-review-blog' ) ); foreach ( $categories as $category ) { $options[$category->term_id] = $category->name; } return $options; } public function widget( $args, $instance ) { echo $args['before_widget']; if ( ! empty( $instance['title'] ) ) { echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; } $posts_per_page = ! empty( $instance['postsperpage'] ) ? absint( $instance['postsperpage'] ) : 4; $show_post_date = ! empty( $instance['showpostdate'] ); $category = ! empty( $instance['category'] ) ? absint( $instance['category'] ) : ''; $show_excerpt = ! empty( $instance['showexcerpt'] ); $excerpt_length = ! empty( $instance['excerptlength'] ) ? absint( $instance['excerptlength'] ) : 20; $show_post_author = ! empty( $instance['showpostauthor'] ); // Generate cache key $cache_key = 'book_review_latest_posts_' . md5( serialize( $instance ) ); $output = get_transient( $cache_key ); if ( false === $output ) { ob_start(); ?> widget_fields as $widget_field ) { $default = isset( $widget_field['default'] ) ? $widget_field['default'] : ''; $widget_value = ! empty( $instance[ $widget_field['id'] ] ) ? $instance[ $widget_field['id'] ] : $default; ?>

id="get_field_id( $widget_field['id'] ) ); ?>" name="get_field_name( $widget_field['id'] ) ); ?>" value="1">

field_generator( $instance ); ?> widget_fields as $widget_field ) { $id = $widget_field['id']; switch ( $widget_field['type'] ) { case 'number': $instance[$id] = ! empty( $new_instance[$id] ) ? absint( $new_instance[$id] ) : ''; break; case 'checkbox': $instance[$id] = ! empty( $new_instance[$id] ) ? '1' : ''; break; case 'select': $instance[$id] = ! empty( $new_instance[$id] ) ? absint( $new_instance[$id] ) : ''; break; default: $instance[$id] = ! empty( $new_instance[$id] ) ? sanitize_text_field( $new_instance[$id] ) : ''; } } // Invalidate cache when settings change $cache_key = 'book_review_latest_posts_' . md5( serialize( $old_instance ) ); delete_transient( $cache_key ); return $instance; } } function book_review_blog_latest_posts_wr() { register_widget( 'Book_Review_Blog_Latest_Posts_Widget' ); } add_action( 'widgets_init', 'book_review_blog_latest_posts_wr' ); // Clear cache when a post is saved function book_review_blog_clear_widget_cache( $post_id ) { $widgets = wp_get_sidebars_widgets(); foreach ( $widgets as $sidebar => $widget_list ) { if ( ! empty( $widget_list ) ) { foreach ( $widget_list as $widget ) { if ( strpos( $widget, 'book_review_blog_latest_posts' ) !== false ) { $settings = get_option( 'widget_book_review_blog_latest_posts' ); foreach ( $settings as $instance ) { if ( is_array( $instance ) ) { $cache_key = 'book_review_latest_posts_' . md5( serialize( $instance ) ); delete_transient( $cache_key ); } } } } } } } add_action( 'save_post', 'book_review_blog_clear_widget_cache' );