This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsalmon.php
executable file
·299 lines (257 loc) · 8.11 KB
/
salmon.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* Plugin Name: Salmon
* Plugin URI: https://github.com/pfefferle/wordpress-salmon
* Description: Salmon plugin for WordPress.
* Version: 0.9.2
* Author: Matthias Pfefferle
* Author URI: https://notiz.blog/
* License: MIT
* License URI: http://opensource.org/licenses/MIT
* Text Domain: salmon
* Domain Path: /languages
*/
/*
* Thanks to Arne Roomann-Kurrik for his inspiration with salmonpress
*
* salmonpress: http://code.google.com/p/salmonpress/
* Arne Roomann-Kurrik: http://roomanna.com
*/
add_action( 'plugins_loaded', array( 'Salmon_Plugin', 'init' ) );
/**
* Static class to register callback handlers and otherwise configure
* WordPress for accepting Salmon posts.
*
* @author Matthias Pfefferle
*/
class Salmon_Plugin {
public static function init() {
require_once 'includes/functions.php';
require_once 'includes/class-salmon-entry.php';
// add salmon discovery
require_once 'includes/class-salmon-discovery.php';
Salmon_Discovery::init();
require_once 'includes/class-magic-sig.php';
// Query handler
add_action( 'parse_query', array( 'Salmon_Plugin', 'parse_query' ) );
add_filter( 'query_vars', array( 'Salmon_Plugin', 'query_vars' ) );
add_action( 'init', array( 'Salmon_Plugin', 'flush_rewrite_rules' ) );
add_action( 'generate_rewrite_rules', array( 'Salmon_Plugin', 'add_rewrite_rules' ) );
// add avatar filter
add_filter( 'pre_get_avatar_data', array( 'Salmon_Plugin', 'pre_get_avatar_data' ), 10, 2 );
add_action( 'comment_atom_entry', array( 'Salmon_Plugin', 'add_crossposting_extension' ) );
add_action( 'admin_init', array( 'Salmon_Plugin', 'text_domain' ) );
add_action( 'admin_menu', array( 'Salmon_Plugin', 'admin_menu' ) );
}
/**
* Generates the enpoint url
*
* @param Object $user |null
*
* @return string
*/
public static function generate_api_url( $user = null ) {
if ( $user ) {
$url = add_query_arg( 'salmon', 'endpoint', get_author_posts_url( $user->ID, $user->user_nicename ) );
} else {
$url = site_url( '/?salmon=endpoint' );
}
return $url;
}
/**
* Checks a query for the 'Salmon_Plugin' parameter and attempts to parse a
* Salmon post if the parameter exists.
*/
public static function parse_query( $wp_query ) {
if (
isset( $wp_query->query_vars['salmon'] ) ||
isset( $wp_query->query_vars['salmonpress'] )
) {
Salmon_Plugin::parse_salmon_post();
}
}
/**
* Replaces the default avatar with the Salmon photo
*
* @param array $args Arguments passed to get_avatar_data(), after processing.
* @param int|string|object $id_or_email A user ID, email address, or comment object
*
* @return array $args
*/
public static function pre_get_avatar_data( $args, $id_or_email ) {
if ( ! $id_or_email instanceof WP_Comment ||
! isset( $id_or_email->comment_type ) ||
$id_or_email->user_id ) {
return $args;
}
$avatars = get_comment_meta( $id_or_email->comment_ID, '_salmon_avatars', true );
if ( ! $avatars ) {
return $args;
}
$size = $args['size'];
if ( array_key_exists( $size, $avatars ) ) {
$args['url'] = $avatars[ $size ];
} else {
$args['url'] = $avatars[ min( array_diff( array_keys( $avatars ), range( 0, $size ) ) ) ];
}
$args['class'][] = 'avatar-salmon';
$args['class'][] = "avatar-$size";
return $args;
}
/**
* Adds the crossposting extension to the feeds
*
* @param int $comment_id
*/
public static function add_crossposting_extension( $comment_id ) {
// get comment
$comment = get_comment( $comment_id );
// check if comment-type is 'salmon'
if ( get_comment_meta( $comment->comment_ID, '_comment_type', true ) == true ) {
$id = get_comment_meta( $comment->comment_ID, '_crossposting_id', true );
$link = get_comment_meta( $comment->comment_ID, '_crossposting_link', true );
// add extension if id is set
if ( $id ) {
?>
<crosspost:source xmlns:crosspost="http://purl.org/syndication/cross-posting">
<id><?php echo $id; ?></id>
<?php if ( $link ) { ?>
<link rel="alternate" type="text/html" href="<?php echo $link; ?>" />
<?php } ?>
</crosspost:source>
<?php
}
}
}
/**
* Adds the 'Salmon_Plugin' query variable to WordPress.
*/
public static function query_vars( $queryvars ) {
$queryvars[] = 'salmon';
$queryvars[] = 'salmonpress';
return $queryvars;
}
/**
* Clears the cached rewrite rules so that we may add our own.
*/
public static function flush_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
/**
* Adds a rewrite rule so that http://mysite.com/index.php?Salmon_Plugin=true
* can be rewritten as http://mysite.com/Salmon_Plugin
*/
public static function add_rewrite_rules( $wp_rewrite ) {
global $wp_rewrite;
$new_rules = array(
'salmon/?(.+)' => 'index.php?salmon=' . $wp_rewrite->preg_index( 1 ),
'salmon' => 'index.php?salmon=endpoint',
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
/**
* Attempts to parse data sent to the Salmon endpoint and post it as a
* comment for the current blog.
*/
public static function parse_salmon_post() {
$user = null;
// get user by url
if ( get_query_var( 'author_name' ) ) :
$user = get_user_by( 'slug', get_query_var( 'author_name' ) );
else :
$user = get_userdata( get_query_var( 'author' ) );
endif;
// Allow cross domain JavaScript requests, from salmon-playground.
if (
'OPTIONS' === strtoupper( $_SERVER['REQUEST_METHOD'] ) &&
'POST' === strtoupper( $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] )
) {
// See https://developer.mozilla.org/En/HTTP_access_control
header( 'HTTP/1.1 200 OK' );
header( 'Access-Control-Allow-Origin: * ' );
die();
}
if ( strtoupper( $_SERVER['REQUEST_METHOD'] ) !== 'POST' ) {
header( 'HTTP/1.1 400 Bad Request' );
echo Salmon_Plugin::endpoint( 'Error: The posted Salmon entry was malformed.' );
}
$request_body = @file_get_contents( 'php://input' );
$env = Magic_Sig::parse( $request_body );
$data = base64_url_decode( $env['data'] );
do_action( 'salmon_atom_data', $data, $user );
$entry = Salmon_Entry::from_atom( $data );
// Validate the request if the option is set.
if ( get_option( 'salmon_validate' ) ) {
if ( $entry->validate() === false ) {
header( 'HTTP/1.1 403 Forbidden' );
Salmon_Plugin::endpoint( 'Error: The posted Salmon entry was malformed.' );
}
}
$commentdata = $entry->to_commentdata();
do_action( 'salmon_comment_data', $commentdata, $user );
if ( $user ) {
wp_mail( $user->user_email, 'you\'ve fished a salmon', strip_tags( $commentdata['comment_content'] ) );
}
if ( $commentdata['comment_post_ID'] ) {
// save comment
$comment_id = wp_insert_comment( $commentdata );
// add comment meta
update_comment_meta( $comment_id, '_salmon_avatars', $entry->avatars );
update_comment_meta( $comment_id, '_comment_type', 'salmon' );
update_comment_meta( $comment_id, '_crossposting_id', $entry->id );
update_comment_meta( $comment_id, '_crossposting_link', $entry->link );
header( 'HTTP/1.1 201 Created' );
Salmon_Plugin::endpoint( 'The Salmon entry was posted.' );
}
die();
}
/**
*
*/
public static function endpoint( $text ) {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Salmon Endpoint</title>
<?php
wp_admin_css( 'install', true );
do_action( 'admin_head' );
?>
</head>
<body>
<h1>Salmon Endpoint</h1>
<p><?php echo $text; ?></p>
</body>
</html>
<?php
exit;
}
/**
* Load plugin text domain
*/
public static function text_domain() {
load_plugin_textdomain( 'salmon' );
}
/**
* Add admin menu entry
*/
public static function admin_menu() {
add_options_page(
'Salmon',
'Salmon',
'manage_options',
'salmon',
array( 'Salmon_Plugin', 'settings_page' )
);
}
/**
* Load settings page
*/
public static function settings_page() {
load_template( dirname( __FILE__ ) . '/templates/settings-page.php' );
}
}