-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-grf-frontend.php
executable file
·147 lines (112 loc) · 3.22 KB
/
class-grf-frontend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* Responsible for rendering the feed output on the front-end.
*
* @since 0.1
*/
class GRF_Frontend {
/**
* Feed object.
*
* @since 0.1
* @var object
*/
static private $feed;
/**
* The data of the attributes used on the back-end.
*
* @since 0.1
* @var array
*/
static private $data;
/**
* Default number of posts.
*
* @since 0.1
* @var integer
*/
static private $number_of_posts = 10;
/**
* Render the RSS feed on the front-end.
*
* @since 0.1
* @param array $data
* @return mixed
*/
static function render( $data ) {
if( is_admin() ) {
return;
}
self::$data = $data;
if( ! empty( self::$data[ 'url' ] ) ) {
self::$feed = GRF_Helper::fetch_feed( self::$data[ 'url' ] );
}
$error = self::validate();
if( ! empty( $error ) ) {
return $error;
}
return self::output();
}
/**
* Validate the feed and prepare error messages if any.
*
* @return string
*/
static function validate() {
if( empty( self::$data[ 'url' ] ) ) {
return __( 'Please set the URL of the RSS feed through the WordPress dashboard.' );
}
if( is_wp_error( self::$feed ) || isset( self::$feed->errors ) ) {
return __( 'Please make sure the provided URL is a valid feed.' );
}
if( isset( self::$feed->get_item_quantity ) && ! self::$feed->get_item_quantity() ) {
return __( 'No feed items found.' );
}
return false;
}
/**
* Prepare the html markup of the feed.
*
* @return string
*/
static function output() {
if( ! empty( self::$data[ 'numberOfPosts' ] ) ) {
self::$number_of_posts = intval( self::$data[ 'numberOfPosts' ] );
}
$feed_items = self::$feed->get_items( 0, self::$number_of_posts );
$date_format = get_option( 'date_format' ) . ' | ' . get_option( 'time_format' );
do_action( 'grf_before_items' );
ob_start();
?>
<div class="grf_items">
<?php foreach($feed_items as $item) : ?>
<?php do_action( 'grf_before_item' ); ?>
<div class="grf_item">
<h3 class="grf_item_title">
<a class="grf_item_link" target="_blank" href="<?php echo esc_url( $item->get_permalink() ); ?>">
<?php echo esc_html( $item->get_title() ); ?>
</a>
</h3>
<?php if( isset( self::$data[ 'showDate' ] ) && self::$data[ 'showDate' ] ) : ?>
<p class="grf_item_date"><?php echo $item->get_date( $date_format ); ?></p>
<?php endif; ?>
<?php if( isset( self::$data[ 'showDescription' ] ) && self::$data[ 'showDescription' ] && method_exists( $item, 'get_description' ) && $item->get_description() ) : ?>
<p class="grf_item_description">
<?php echo $item->get_description(); ?>
</p>
<?php endif; ?>
<?php if( isset( self::$data[ 'showContent'] ) && self::$data[ 'showContent' ] && method_exists( $item, 'get_content' ) && $item->get_content() ) : ?>
<p class="grf_item_description">
<?php echo $item->get_content(); ?>
</p>
<?php endif; ?>
<?php do_action( 'grf_after_item' ); ?>
</div>
<?php endforeach; ?>
</div>
<?php
$output = ob_get_clean();
do_action('grf_after_items');
return apply_filters('grf_frontend_output', $output);
}
}