1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/* Exit if accessed directly. */ |
4
|
|
|
if ( ! defined( 'ABSPATH' ) ) { exit; } |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Inman Feed Widget |
8
|
|
|
* |
9
|
|
|
* @package RE-PRO |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* InmanNewsWidget class. |
14
|
|
|
* |
15
|
|
|
* @extends WP_Widget |
16
|
|
|
*/ |
17
|
|
|
class InmanNewsWidget extends WP_Widget { |
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* __construct function. |
22
|
|
|
* |
23
|
|
|
* @access public |
24
|
|
|
* @return void |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
public function __construct() { |
27
|
|
|
|
28
|
|
|
parent::__construct( |
29
|
|
|
'inman_news_widget', |
30
|
|
|
__( 'Inman News', 're-pro' ), |
31
|
|
|
array( |
32
|
|
|
'description' => __( 'Display the lastest news from Inman.', 're-pro' ), |
33
|
|
|
'classname' => 're-pro re-pro-widget inman-widget inman-news-widget', |
34
|
|
|
'customize_selective_refresh' => true, |
35
|
|
|
) |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Widget function. |
41
|
|
|
* |
42
|
|
|
* @access public |
43
|
|
|
* @param mixed $args Arguments. |
44
|
|
|
* @param mixed $instance Instance. |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function widget( $args, $instance ) { |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
$title = ! empty( $instance['title'] ) ? $instance['title'] : ''; |
51
|
|
|
$display_total = ! empty( $instance['display_total'] ) ? $instance['display_total'] : '3'; |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
echo $args['before_widget']; |
55
|
|
|
|
56
|
|
|
echo $args['before_title'] . esc_attr( $title ) . $args['after_title']; |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
$rss = fetch_feed( 'https://feeds.feedburner.com/inmannews' ); |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
if( ! is_wp_error( $rss ) ) { |
64
|
|
|
|
65
|
|
|
$max_items = $rss->get_item_quantity( $display_total ); |
|
|
|
|
66
|
|
|
$rss_items = $rss->get_items( 0, $display_total ); |
67
|
|
|
|
68
|
|
|
// echo $rss->get_title(); |
|
|
|
|
69
|
|
|
// echo '<small>'. $rss->get_description() . '.</small>'; |
|
|
|
|
70
|
|
|
|
71
|
|
|
foreach( $rss_items as $item ) { |
72
|
|
|
|
73
|
|
|
echo '<div style="margin:20px 0;">'; |
74
|
|
|
echo '<h4><a href="'. $item->get_link() .'" rel="nofollow" target="_blank">' . $item->get_title() . '</a></h4>'; |
75
|
|
|
echo '</div>'; |
76
|
|
|
|
77
|
|
|
echo $item->get_content(); |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} else { |
82
|
|
|
echo $rss->get_error_message(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
echo $args['after_widget']; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Form function. |
90
|
|
|
* |
91
|
|
|
* @access public |
92
|
|
|
* @param mixed $instance Instance. |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public function form( $instance ) { |
96
|
|
|
|
97
|
|
|
// Set default values. |
98
|
|
|
$instance = wp_parse_args( (array) $instance, array( |
99
|
|
|
'title' => '', |
100
|
|
|
|
101
|
|
|
)); |
102
|
|
|
|
103
|
|
|
// Retrieve an existing value from the database. |
104
|
|
|
$title = ! empty( $instance['title'] ) ? $instance['title'] : ''; |
105
|
|
|
$display_total = ! empty( $instance['display_total'] ) ? $instance['display_total'] : ''; |
|
|
|
|
106
|
|
|
|
107
|
|
|
// Title. |
108
|
|
|
echo '<p>'; |
109
|
|
|
echo ' <label for="' . $this->get_field_id( 'title' ) . '" class="title-label">' . __( 'Tile:', 're-pro' ) . '</label>'; |
110
|
|
|
echo ' <input id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $title . '" class="widefat">'; |
111
|
|
|
echo '</p>'; |
112
|
|
|
|
113
|
|
|
// TODO: Add field to select display total count, 1 - 20. |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Update. |
119
|
|
|
* |
120
|
|
|
* @access public |
121
|
|
|
* @param mixed $new_instance New Instance. |
122
|
|
|
* @param mixed $old_instance Old Instance. |
123
|
|
|
* @return $instance |
|
|
|
|
124
|
|
|
*/ |
125
|
|
|
public function update( $new_instance, $old_instance ) { |
126
|
|
|
|
127
|
|
|
$instance = $old_instance; |
128
|
|
|
|
129
|
|
|
$instance['title'] = ! empty( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : ''; |
130
|
|
|
$instance['display_total'] = ! empty( $new_instance['display_total'] ) ? strip_tags( $new_instance['display_total'] ) : ''; |
131
|
|
|
|
132
|
|
|
return $instance; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Register Widget. |
138
|
|
|
* |
139
|
|
|
* @access public |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
function repro_inman_news_widget() { |
143
|
|
|
|
144
|
|
|
register_widget( 'InmanNewsWidget' ); |
145
|
|
|
} |
146
|
|
|
add_action( 'widgets_init', 'repro_inman_news_widget' ); |
147
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.