This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilterPostie.php
344 lines (237 loc) · 7.82 KB
/
filterPostie.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/*
* WARNING - WARNING - WARNING
* Do not put any custom filter code in the Postie directory. The standard WordPress
* upgrade process will delete your code.
*
* Instead copy filterPostie.php.sample to the wp-content directory and rename it
* to filterPostie.php and edit to your hearts content.
*
* Another option is to create your own plugin or add this code to your theme.
*/
/*
* Any filter function you write should accept one argument, which is the post
array, which contains the following fields:
'post_author'
'comment_author'
'comment_author_url'
'user_ID'
'email_author'
'post_date'
'post_date_gmt'
'post_content'
'post_title'
'post_modified'
'post_modified_gmt'
'ping_status'
'post_category'
'tags_input'
'comment_status'
'post_name'
'post_excerpt'
'ID'
'customImages'
'post_status'
Your function can modify any of these fields. It should then return the array
back. If you return null then the post will not be created.
*/
/*
add_filter('postie_post', 'filter_title');
add_filter('postie_post', 'filter_content');
add_filter('postie_post', 'auto_tag');
add_filter('postie_filter_email', 'change_email');
*/
//add_filter('postie_post', 'add_custom_field');
/*
Récupérer le plus après l'arobase
*/
add_filter('postie_filter_email2', 'plus_filter', 10, 3);
function plus_filter( $from, $toEmail, $replytoEmail) {
global $project;
DebugEcho("step-01");
DebugDump("from " . $from);
DebugDump("toEmail " . $toEmail);
DebugDump("replytoEmail " . $replytoEmail);
$fromField = $from;
$toField = $toEmail;
$posPlus = strpos($toField, '+');
if ( $posPlus !== false ) {
$toEmailFromPlus = substr($toField, $posPlus +1);
$projectTerm = stristr( $toEmailFromPlus, '@', true );
$project = $projectTerm;
} else {
}
return $fromField;
}
/*
Récupérer le nom de l'expéditeur avant le @
*/
add_filter('postie_filter_email2', 'get_mail_auteur', 10, 3);
function get_mail_auteur( $from, $toEmail, $replytoEmail) {
global $auteur;
global $auteurFullmail;
DebugEcho("step-01b");
DebugDump("from " . $from);
DebugDump("toEmail " . $toEmail);
DebugDump("replytoEmail " . $replytoEmail);
$fromField = $from;
$toField = $toEmail;
$auteurFullmail = $fromField;
$posAt = strpos($fromField, '@');
if ( $posAt !== false ) {
$auteur = substr($fromField, 0, $posAt);
}
return $fromField;
}
/*
Si le sujet contient uniquement "Description"
Alors passer le post en sticky
*/
function sticky_or_not( $post, $post_part_to_check, $isTerm) {
DebugEcho("Check Description sticky");
DebugEcho("--> post[post_part_to_check] " . $post[$post_part_to_check] );
DebugEcho("--> isTerm " . $isTerm );
if( stripos($post[$post_part_to_check], $isTerm) !== false ) {
$post['post_status'] = 'publish';
// A FAIRE : le passer en sticky //////////////////////////////////////////////
array_push($post['tags_input'], 'featured');
DebugEcho("Published / stickied");
} else {
$post['post_status'] = 'private';
$post['meta_featured'] = 'false';
DebugEcho("Private");
}
return $post;
}
function make_sticky_descriptions($post) {
// Check for categories and use in type
$post = sticky_or_not($post,'post_title','Description');
return $post;
}
add_filter('postie_post_before', 'make_sticky_descriptions');
/*
Check sujet si contient [projet]
*/
function check_in_mail( $post, $post_part_to_check, $isTerm) {
global $project;
DebugEcho("Check Description projet (#)");
DebugEcho("--> post[post_part_to_check] " . $post[$post_part_to_check] );
DebugEcho("--> isTerm " . $isTerm );
DebugEcho("Found ? " . strpos($post[$post_part_to_check], $isTerm) );
if( strpos($post[$post_part_to_check], $isTerm) !== false ) {
$posCrochet = strpos($post[$post_part_to_check], $isTerm) + 1;
$toTitreFromCrochet = substr($post[$post_part_to_check], $posCrochet);
$projectTerm = stristr( $toTitreFromCrochet, '#', true );
$project = $projectTerm;
DebugEcho("Trouvé un projet dans le sujet intitulé --- ");
DebugEcho( $project );
DebugEcho(("---"));
$post['post_title'] = str_replace( '#' . $project . '#', '', $post[$post_part_to_check] );
}
return $post;
}
function check_other_project($post) {
// Check for categories and use in type
$post = check_in_mail($post,'post_title','#');
return $post;
}
add_filter('postie_post_before', 'check_other_project');
/*
Ajouter les termes au post
*/
add_filter('postie_post_after', 'tax_tag');
function tax_tag($post) {
global $project;
global $auteur;
global $auteurFullmail;
DebugEcho("step-02");
DebugEcho("project " . $project );
DebugEcho("auteur " . $auteur );
/*
$projectField = array( 'projets' => array( $project ) );
$post['tax_input'] = $projectField;
*/
if( strlen( $project ) > 0 ) {
$project = str_replace( "-", " ", $project);
DebugEcho( "Ajout au post " . $post['ID'] );
DebugEcho( "Du projet " . $project );
DebugEcho( "##" . $project . "##" );
error_log( "le terme existe t'il ? " . term_exists($project, 'projets'));
$term = term_exists($project, 'projets');
if( $term === 0 || $term === null) {
addTermAndCreateDescription( $project, $auteur, true);
} else {
$projetslug = $project;
$projectLink = get_term_link( $projetslug, 'projets');
$lastauteur = $auteur;
$userPrivateTag = $auteurFullmail;
$mailUser = get_user_by( 'email', $auteurFullmail);
if( $mailUser !== false) {
$lastauteur = $mailUser->display_name;
$userPrivateTag = $lastauteur;
}
/*
$userWhoEdited = get_user_by( 'id', $userid);
$usernameWhoEdited = $userWhoEdited->display_name;
*/
DebugEcho("Sending notifications");
DebugEcho("auteur = " . $userPrivateTag);
logActionsToProject( $projetslug, "<span class='edit-by-author'>$lastauteur</span>" . __("Has created a new post by mail.", 'opendoc'));
sendMailToAllProjectContributors( $projetslug,
html_entity_decode( get_bloginfo('name')),
"<strong>" . $userPrivateTag . "</strong>" . " " .
__("has added a new post by mail to the project", 'opendoc') . " " .
"<strong>" . $projetslug . "</strong>" .
"<br/>" .
__("You can see this post and contribute by visiting", 'opendoc') . " " .
esc_url( $projectLink )
);
}
wp_set_object_terms( $post['ID'], array($project), 'projets');
DebugEcho( "Vérification du post ---" );
DebugEcho( $post );
}
if( strlen( $auteur ) > 0 ) {
DebugEcho( "Ajout au post " . $post['ID'] );
DebugEcho( "De l'auteur " . $auteur );
DebugEcho( "--" . $auteur . "--" );
wp_set_object_terms( $post['ID'], array($auteur), 'auteur');
DebugEcho( "Vérification du post ---" );
DebugEcho( $post );
}
return $post;
}
function filter_content($post) {
//this function prepends a link to bookmark the category of the post
$this_cat = get_the_category($post['ID']);
//var_dump($this_cat);
$link = '<a href="' . get_category_link($this_cat[0]->term_id) .
'">Bookmark this category</a>' . "\n";
$post['post_content'] = $link . $post['post_content'];
return $post;
}
function filter_title($post) {
//this function appends "(via postie)" to the title (subject)
$post['post_title'] = $post['post_title'] . ' (via postie)';
return $post;
}
function add_custom_field($post) {
add_post_meta($post['ID'], 'postie', 'postie');
return $post;
}
//changes the email to the admin email if the sender was from the example.com domain
function change_email($email) {
if (stristr($email, '@example.com'))
return get_option("admin_email");
return $email;
}
function auto_tag($post) {
// this function automatically inserts tags for a post
$my_tags = array('cooking', 'latex', 'wordpress');
foreach ($my_tags as $my_tag) {
if (stripos($post['post_content'], $my_tag) !== false)
array_push($post['tags_input'], $my_tag);
}
return $post;
}
?>