TPGWidget.php
94 lines
| 1 | <?php |
| 2 | /** |
| 3 | * TPG Widget Class |
| 4 | * |
| 5 | * @package RT_TPG |
| 6 | */ |
| 7 | |
| 8 | namespace RT\ThePostGrid\Widgets; |
| 9 | |
| 10 | use RT\ThePostGrid\Helpers\Fns; |
| 11 | use WP_Widget; |
| 12 | |
| 13 | // Do not allow directly accessing this file. |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit( 'This script cannot be accessed directly.' ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * TPG Widget Class |
| 20 | * |
| 21 | * @package RT_TPG |
| 22 | */ |
| 23 | class TPGWidget extends WP_Widget { |
| 24 | |
| 25 | public function __construct() { |
| 26 | $widget_ops = [ |
| 27 | 'classname' => 'widget_tpg_post_grid', |
| 28 | 'description' => esc_html__( 'Display the post grid.', 'the-post-grid' ), |
| 29 | ]; |
| 30 | |
| 31 | parent::__construct( 'widget_tpg_post_grid', esc_html__( 'The Post Grid', 'the-post-grid' ), $widget_ops ); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * display the widgets on the screen. |
| 37 | */ |
| 38 | public function widget( $args, $instance ) { |
| 39 | extract( $args ); |
| 40 | $id = ( ! empty( $instance['id'] ) ? absint( $instance['id'] ) : null ); |
| 41 | |
| 42 | echo $before_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 43 | |
| 44 | if ( ! empty( $instance['title'] ) ) { |
| 45 | echo $args['before_title'] . apply_filters( 'widget_title', ( isset( $instance['title'] ) ? $instance['title'] : 'The Post Grid' ) ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 46 | } |
| 47 | |
| 48 | if ( ! empty( $id ) ) { |
| 49 | echo do_shortcode( '[the-post-grid id="' . absint( $id ) . '" ]' ); |
| 50 | } |
| 51 | |
| 52 | echo $after_widget; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 53 | } |
| 54 | |
| 55 | public function form( $instance ) { |
| 56 | |
| 57 | $scList = Fns::getAllTPGShortCodeList(); |
| 58 | $defaults = [ |
| 59 | 'title' => 'The Post Grid', |
| 60 | 'id' => null, |
| 61 | ]; |
| 62 | $instance = wp_parse_args( (array) $instance, $defaults ); ?> |
| 63 | |
| 64 | <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> <?php esc_html_e( 'Title:', 'the-post-grid' ); ?></label> |
| 65 | <input type="text" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" |
| 66 | name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" |
| 67 | style="width:100%;"/></p> |
| 68 | |
| 69 | <p><label for="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>"><?php esc_html_e( 'Select post grid', 'the-post-grid' ); ?></label> |
| 70 | <select id="<?php echo esc_attr( $this->get_field_id( 'id' ) ); ?>" |
| 71 | name="<?php echo esc_attr( $this->get_field_name( 'id' ) ); ?>"> |
| 72 | <option value="">Select one</option> |
| 73 | <?php |
| 74 | if ( ! empty( $scList ) ) { |
| 75 | foreach ( $scList as $scId => $sc ) { |
| 76 | $selected = ( $scId == $instance['id'] ? 'selected' : null ); |
| 77 | echo '<option value="' . absint( $scId ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $sc ) . '</option>'; |
| 78 | } |
| 79 | } |
| 80 | ?> |
| 81 | </select></p> |
| 82 | <?php |
| 83 | } |
| 84 | |
| 85 | public function update( $new_instance, $old_instance ) { |
| 86 | $instance = []; |
| 87 | $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; |
| 88 | $instance['id'] = ( ! empty( $new_instance['id'] ) ) ? absint( $new_instance['id'] ) : ''; |
| 89 | |
| 90 | return $instance; |
| 91 | } |
| 92 | |
| 93 | } |
| 94 |