-
Notifications
You must be signed in to change notification settings - Fork 0
/
friends-parser-rss-bridge.php
263 lines (250 loc) · 8.21 KB
/
friends-parser-rss-bridge.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* Plugin name: Friends Parser RSS Bridge
* Plugin author: Alex Kirk
* Plugin URI: https://github.com/akirk/friends-parser-rss-bridge
* Version: 1.3
* Requires Plugins: friends
*
* Description: Provides the parsing capabilities of RSS Bridge.
*
* License: GPL2
* Text Domain: friends
*
* @package Friends\Parser_RSS_Bridge
*/
/**
* This file contains the main plugin functionality.
*/
defined( 'ABSPATH' ) || exit;
add_action(
'friends_load_parsers',
function( Friends\Feed $friends_feed ) {
require_once __DIR__ . '/class-feed-parser-rss-bridge.php';
$friends_feed->register_parser( 'rss-bridge', new Friends\Feed_Parser_RSS_Bridge );
}
);
/**
* Display an about page for the plugin.
*
* @param bool $display_about_friends The display about friends section.
*/
function friends_parser_rss_bridge_about_page( $display_about_friends = false ) {
$nonce_value = 'friends-parser-rss-bridge';
if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], $nonce_value ) ) {
update_option( 'friends-parser-rss-bridge_confidence', intval( $_POST['default_confidence'] ) );
}
?><h1><?php esc_html_e( 'Friends Parser RSS Bridge', 'friends' ); ?></h1>
<form method="post">
<?php wp_nonce_field( $nonce_value ); ?>
<table class="form-table">
<tbody>
<tr>
<th scope="row"><?php esc_html_e( 'Default Confidence', 'friends' ); ?></th>
<td>
<fieldset>
<label for="default_confidence">
<input name="default_confidence" type="number" id="default_confidence" placeholder="10" value="<?php echo esc_attr( get_option( 'friends-parser-rss-bridge_confidence', 10 ) ); ?>" min="0" max="100" />
</label>
<p class="description">
<?php esc_html_e( 'If you set this to a higher value, this parser will take precedence over others that also say they can handle the URL.', 'friends' ); ?>
</p>
</fieldset>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<input type="submit" id="submit" class="button button-primary" value="<?php esc_html_e( 'Save Changes', 'friends' ); ?>">
</p>
</form>
<?php if ( $display_about_friends ) : ?>
<p>
<?php
echo wp_kses(
// translators: %s: URL to the Friends Plugin page on WordPress.org.
sprintf( __( 'The Friends plugin is all about connecting with friends and news. Learn more on its <a href=%s>plugin page on WordPress.org</a>.', 'friends' ), '"https://wordpress.org/plugins/friends" target="_blank" rel="noopener noreferrer"' ),
array(
'a' => array(
'href' => array(),
'rel' => array(),
'target' => array(),
),
)
);
?>
</p>
<?php endif; ?>
<p>
<?php
echo wp_kses(
// translators: %s: URL to the RSS Bridge.
sprintf( __( 'This parser is powered by the open source project <a href=%s>RSS Bridge</a> and provides support to parse the following properties:', 'friends' ), '"https://github.com/RSS-Bridge/rss-bridge" target="_blank" rel="noopener noreferrer"' ),
array(
'a' => array(
'href' => array(),
'rel' => array(),
'target' => array(),
),
)
);
?>
</p>
<ul>
<?php
if ( ! class_exists( 'Friends\Feed_Parser_RSS_Bridge' ) ) {
if ( ! class_exists( 'Friends\Feed_Parser' ) ) {
require_once __DIR__ . '/class-feed-parser.php';
}
if ( ! class_exists( 'Friends\Feed_Parser_V2' ) ) {
require_once __DIR__ . '/class-feed-parser-v2.php';
}
if ( ! class_exists( 'Friends\Feed_Item' ) ) {
require_once __DIR__ . '/class-feed-item.php';
}
require_once __DIR__ . '/class-feed-parser-rss-bridge.php';
}
$parser = new Friends\Feed_Parser_RSS_Bridge;
foreach ( $parser->get_all_bridges() as $slug => $bridge ) {
?>
<li><a href="<?php echo esc_url( $bridge::URI ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $bridge::NAME ); ?></a> <?php echo esc_html( $bridge::DESCRIPTION ); ?></li>
<?php
}
?>
</ul>
<?php
}
/**
* Display an about page for the plugin with the friends section.
*/
function friends_parser_rss_bridge_about_page_with_friends_about() {
return friends_parser_rss_bridge_about_page( true );
}
/**
* Displays the RSS Bridge Tester.
*/
function friends_parser_rss_bridge_tester() {
$url = false;
if ( isset( $_GET['_wpnonce'], $_GET['url'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'friends_parser_rss_bridge_tester' ) ) {
$url = $_GET['url'];
if ( ! parse_url( $url, PHP_URL_SCHEME ) ) {
$url = 'https://' . $url;
}
}
?>
<h1><?php esc_html_e( 'RSS Bridge Tester', 'friends' ); ?></h1>
<p><?php esc_html_e( 'Here you can test what the parser makes of the URL you give it. ', 'friends' ); ?></h1>
<form>
<input type="hidden" name="page" value="<?php echo esc_attr( $_GET['page'] ); ?>">
<?php wp_nonce_field( 'friends_parser_rss_bridge_tester', '_wpnonce', false ); ?>
<label><?php esc_html_e( 'Enter a URL:', 'friends' ); ?> <input type="text" name="url" value="<?php echo esc_attr( $url ); ?>" placeholer="https://" autofocus /></label>
<input type="submit" class="button button-primary" value="<?php echo esc_attr_x( 'Parse Now', 'button', 'friends' ); ?>" />
</form>
<?php
if ( $url ) {
if ( ! class_exists( 'Friends\Feed_Parser_RSS_Bridge' ) ) {
if ( ! class_exists( 'Friends\Feed_Parser' ) ) {
require_once __DIR__ . '/class-feed-parser.php';
}
if ( ! class_exists( 'Friends\Feed_Item' ) ) {
require_once __DIR__ . '/class-feed-item.php';
}
require_once __DIR__ . '/class-feed-parser-rss-bridge.php';
}
$parser = new Friends\Feed_Parser_RSS_Bridge;
$bridge = $parser->get_bridge( $_GET['url'] );
$items = $parser->fetch_feed( $_GET['url'] );
?>
<h2>
<?php
// translators: %s is a URL to be displayed verbatim.
echo esc_html( sprintf( __( 'Parsing Result for %s', 'friends' ), $url ) );
?>
</h2>
<?php
if ( ! is_wp_error( $items ) && empty( $items ) ) {
$items = new \WP_Error( 'empty-feed', __( "This feed doesn't contain any entries. There might be a problem parsing the feed.", 'friends' ) );
}
if ( is_wp_error( $items ) ) {
?>
<div id="message" class="updated notice is-dismissible"><p><?php echo esc_html( $items->get_error_message() ); ?></p>
</div>
<?php
exit;
}
?>
<h3><?php esc_html_e( 'Parser Details', 'friends' ); ?></h3>
<ul id="parser">
<li>
<?php
echo wp_kses(
// translators: %s is the name of a Bridge = specific parser.
sprintf( __( 'Using Bridge: %s', 'friends' ), '<a href="' . esc_url( $bridge::URI ) . '" target="_blank" rel="noopener noreferrer">' . $bridge::NAME . '</a>' ),
array(
'a' => array(
'href' => array(),
'rel' => array(),
'target' => array(),
),
)
);
?>
</li>
<li>
<?php
// translators: %s is an explanation for the Bridge = specific parser.
echo esc_html( sprintf( __( 'Bridge Description: %s', 'friends' ), $bridge::DESCRIPTION ) );
?>
</li>
</ul>
<h3><?php esc_html_e( 'Items in the Feed', 'friends' ); ?></h3>
<ul id="items">
<?php
foreach ( $items as $item ) {
?>
<li><?php echo esc_html( $item->date ); ?>: <a href="<?php echo esc_url( $item->permalink ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $item->title ); ?></a></li>
<?php
}
?>
</ul>
<?php
}
}
add_action(
'admin_menu',
function () {
// Only show the menu if installed standalone.
$friends_settings_exist = '' !== menu_page_url( 'friends', false );
if ( $friends_settings_exist ) {
add_submenu_page(
'friends',
__( 'RSS Bridge', 'friends' ),
__( 'RSS Bridge', 'friends' ),
'edit_private_posts',
'friends-rss-bridge',
'friends_parser_rss_bridge_about_page'
);
} else {
add_menu_page( 'friends', __( 'Friends', 'friends' ), 'edit_private_posts', 'friends', null, 'dashicons-groups', 3 );
add_submenu_page(
'friends',
__( 'About', 'friends' ),
__( 'About', 'friends' ),
'edit_private_posts',
'friends',
'friends_parser_rss_bridge_about_page_with_friends_about'
);
}
if ( apply_filters( 'friends_debug', false ) || ! $friends_settings_exist ) {
add_submenu_page(
'friends',
__( 'RSS Bridge Tester', 'friends' ),
__( 'RSS Bridge Tester', 'friends' ),
'edit_private_posts',
'friends-rss-bridge-tester',
'friends_parser_rss_bridge_tester'
);
}
},
50
);