forked from jchristopher/linkmarklet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
611 lines (523 loc) · 22.7 KB
/
index.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<?php
global $linkmarklet_debug;
$linkmarklet_debug = false;
define('IFRAME_REQUEST' , true);
ob_start();
require_once( preg_replace( "/wp-content.*/", "wp-load.php", __FILE__ ) );
require_once( preg_replace( "/wp-content.*/", "/wp-admin/includes/admin.php", __FILE__ ) );
/** WordPress Administration Bootstrap */
require_once( preg_replace( "/wp-content.*/", "/wp-admin/admin.php", __FILE__ ) );
ob_end_clean();
header( 'Content-Type: ' . get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ) );
if ( ! current_user_can( 'edit_posts' ) || ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) )
wp_die( __( 'Access Denied.' ) );
// let's create our post
$post = get_default_post_to_edit( 'post', true );
$post_ID = $post->ID;
if( $linkmarklet_debug )
error_log( '$post_ID = ' . $post_ID );
// Set Variables
$title = isset( $_GET['t'] ) ? trim( strip_tags( html_entity_decode( stripslashes( $_GET['t'] ) , ENT_QUOTES) ) ) : '';
if( $linkmarklet_debug )
error_log( '$title = ' . $title );
$selection = '';
if ( !empty($_GET['s']) ) {
$selection = str_replace( ''', "'", stripslashes( $_GET['s'] ) );
$selection = trim( htmlspecialchars( html_entity_decode($selection, ENT_QUOTES) ) );
}
if ( ! empty($selection) ) {
// $selection = preg_replace('/(\r?\n|\r)/', '</p><p>', $selection);
// $selection = '<p>' . str_replace('<p></p>', '', $selection) . '</p>';
}
if( $linkmarklet_debug )
error_log( '$selection = ' . $selection );
// we stripped the protocol so as to avoid issues with certain
// webhosts (HostGator) that throw 404's if protocols are in GET vars
// but we tracked if it was HTTPS so we'll put the protocol back in
$url = isset( $_GET['u'] ) ? esc_url( ( $_GET['m'] ? 'https://' : 'http://' ) . $_GET['u'] ) : '';
if( $linkmarklet_debug )
error_log( '$url = ' . $url );
$image = isset($_GET['i']) ? $_GET['i'] : '';
if( $linkmarklet_debug )
error_log( '$image = ' . $image );
function linkmarklet_post()
{
global $linkmarklet_debug;
$settings = get_option( LINKMARKLET_PREFIX . 'settings' );
// set our time (if applicable)
$timeframe_min = !isset( $settings['future_publish']['min'] ) || $settings['future_publish']['min'] === '' ? false : intval( $settings['future_publish']['min'] );
$timeframe_max = !isset( $settings['future_publish']['max'] ) || $settings['future_publish']['max'] === '' ? false : intval( $settings['future_publish']['max'] );
$publish_start = !isset( $settings['future_publish']['start'] ) || $settings['future_publish']['start'] === '' ? false : intval( $settings['future_publish']['start'] );
$publish_end = !isset( $settings['future_publish']['end'] ) || $settings['future_publish']['end'] === '' ? false : intval( $settings['future_publish']['end'] );
// by default it'll be right now
$timestamp = (int) current_time( 'timestamp' );
$timestamp_gmt = (int) current_time( 'timestamp', 1 );
if( $linkmarklet_debug )
{
error_log( '$timestamp (source) = ' . $timestamp . ' ' . date( 'Y-m-d H:i:s', $timestamp ) );
error_log( '$timestamp_gmt (source) = ' . $timestamp_gmt . ' ' . date( 'Y-m-d H:i:s', $timestamp_gmt ) );
}
$future_publish = false;
// check to see if we need to bump our publish time
if( $timeframe_min !== false && $timeframe_max !== false )
{
// set the post date
if( $linkmarklet_debug )
error_log( 'trigger: timeframe' );
// figure out our start time which is either right now, or the future-most post
$args = array(
'numberposts' => 1,
'post_status' => array( 'publish', 'pending', 'future' )
);
$posts_array = get_posts( $args );
// if there are any posts, we can check it out
$post_timestamp = false;
if( $posts_array )
{
if( $linkmarklet_debug )
error_log( 'found post' );
foreach( $posts_array as $post )
{
setup_postdata( $post );
$post_timestamp = strtotime( $post->post_date ); // local time
$post_timestamp_gmt = strtotime( $post->post_date_gmt );
if( $linkmarklet_debug )
{
error_log( print_r( $post, true ) );
error_log( '======================' );
}
}
if( $linkmarklet_debug )
{
error_log( '$post_timestamp = ' . $post_timestamp . ' ' . date( 'Y-m-d H:i:s', $post_timestamp ) );
error_log( '$timestamp = ' . $timestamp . ' ' . date( 'Y-m-d H:i:s', $timestamp ) );
}
}
// get the future-most timestamp and use that
if( ( $post_timestamp + ( $timeframe_min * 60 ) ) > $timestamp ) // $timestamp is still now() in local time
{
$future_publish = true;
if( $linkmarklet_debug )
error_log( 'FUTURE PUBLISH' );
// our timestamps need to be adjusted
$timestamp = $post_timestamp;
$timestamp_gmt = $post_timestamp_gmt;
if( $linkmarklet_debug )
{
error_log( '$timestamp (before 1) = ' . $timestamp . ' ' . date( 'Y-m-d H:i:s', $timestamp ) );
error_log( '$timestamp_gmt (before 1) = ' . $timestamp_gmt . ' ' . date( 'Y-m-d H:i:s', $timestamp_gmt ) );
}
// determine how many seconds we'll offset
$offset = rand( $timeframe_min * 60, $timeframe_max * 60 );
if( $linkmarklet_debug )
error_log( '$offset (in seconds) = ' . $offset );
// the post is scheduled so we need to offset both
$timestamp = $timestamp + $offset;
$timestamp_gmt = $timestamp_gmt + $offset;
if( $linkmarklet_debug )
{
error_log( '$timestamp (after) = ' . $timestamp . ' ' . date( 'Y-m-d H:i:s', $timestamp ) );
error_log( '$timestamp_gmt (after) = ' . $timestamp_gmt . ' ' . date( 'Y-m-d H:i:s', $timestamp_gmt ) );
error_log( 'NEW FUTURE PUBLISH TIME: ' . date( 'Y-m-d H:i:s', $timestamp ) );
}
}
}
// we need to check to see if we're within the posting window (if set)
if( $publish_start !== false && $publish_end !== false )
{
if( $linkmarklet_debug )
error_log( 'checking publish window...' );
// our publish window needs to be put within today's context
$publish_start = date( 'U', strtotime( date( 'Y-m-d' ) . ' ' . $publish_start . ':00:00' ) );
$publish_end = date( 'U', strtotime( date( 'Y-m-d' ) . ' ' . $publish_end . ':00:00' ) );
if( $linkmarklet_debug )
error_log( 'window: ' . $publish_start . ' - ' . $publish_end );
// check to see if we're too early
if( $timestamp < $publish_start )
{
if( $linkmarklet_debug )
error_log( 'too early' );
$future_publish = true;
$timestamp = $publish_start;
$timestamp_gmt = $publish_start - ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
if( $linkmarklet_debug )
{
error_log( '$timestamp (after) = ' . $timestamp . ' ' . date( 'Y-m-d H:i:s', $timestamp ) );
error_log( '$timestamp_gmt (after) = ' . $timestamp_gmt . ' ' . date( 'Y-m-d H:i:s', $timestamp_gmt ) );
}
}
// check to see if we're too late
if( $timestamp > $publish_end )
{
if( $linkmarklet_debug )
error_log( 'too late' );
// need to push it to tomorrow's start time
$future_publish = true;
$timestamp = $publish_start + ( 24 * 60 * 60 );
$timestamp_gmt = ( $publish_start + ( 24 * 60 * 60 ) ) - ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
if( $linkmarklet_debug )
{
error_log( '$timestamp (after) = ' . $timestamp . ' ' . date( 'Y-m-d H:i:s', $timestamp ) );
error_log( '$timestamp_gmt (after) = ' . $timestamp_gmt . ' ' . date( 'Y-m-d H:i:s', $timestamp_gmt ) );
}
}
}
$settings = get_option( LINKMARKLET_PREFIX . 'settings' );
$post = get_default_post_to_edit();
$post = get_object_vars( $post );
$post_ID = $post['ID'] = intval( $_POST['post_id'] );
if( !current_user_can( 'edit_post', $post_ID ) )
wp_die( __( 'You are not allowed to edit this post.' ) );
// set our category
$post['post_category'] = !empty( $settings['category'] ) ? intval( $settings['category'] ) : 0;
// set our post properties
$post['post_title'] = isset( $_POST['title'] ) ? sanitize_text_field( $_POST['title'] ) : '';
$content = isset( $_POST['content'] ) ? $_POST['content'] : '';
// Markdown on Save?
if( is_plugin_active( 'markdown-on-save/markdown-on-save.php' ) && !empty( $settings['markdown'] ) )
{
// we need to set up our post data to tell Markdown on Save we want to use it
$post['cws_using_markdown'] = 1;
$post['_cws_markdown_nonce'] = wp_create_nonce( 'cws-markdown-save' );
}
// see if we need to process any images
$images = array();
// $markdown_pattern = "/(!\\[(.*?)\\]\\s?\\([ \\n]*(?:<(\\S*)>|(.*?))[ \\n]*(([\\'\"])(.*?)\\6[ \\n]*)?\\))/ui";
$markdown_pattern = "/(!\\[(.*?)\\]\\s?\\([ \\n]*(?:<(\\S*)>|(.*?))[ \\n]*?\\))/ui";
preg_match_all( $markdown_pattern, $content, $images );
$upload = false;
if( isset( $images[4] ) && !empty( $images[4] ) && current_user_can( 'upload_files' ) )
{
if( $linkmarklet_debug )
error_log( 'attempting sideload' );
foreach( $images[4] as $key => $image)
{
// see if files exist in content - we don't want to upload non-used selected files.
if( strpos( $content, htmlspecialchars( $image ) ) !== false )
{
if( $linkmarklet_debug )
error_log( 'image: ' . $image );
$upload = media_sideload_image( $image, $post_ID, '' );
if ( !is_wp_error( $upload ) )
{
// we only want to strip out the URL at this point so we need just the URL of the upload
$new_image = array();
preg_match( "~https?://.*/(.*?).(jpe?g|gif|png)~ui", $upload, $new_image );
if( !empty( $new_image[0] ) )
{
$url = mysql_real_escape_string( $_POST['url'] );
if( is_plugin_active( 'markdown-on-save/markdown-on-save.php' ) && !empty( $settings['markdown'] ) )
{
$hosted_image_final = str_replace( $image, $new_image[0], $images[0][$key] );
}
else
{
$hosted_image_final = '<img src="' . $new_image[0] . '" alt="' . $images[2][$key] . '" />';
}
$hosted_image = "<a href='$url'>$hosted_image_final</a>";
// swap out the ORIGINAL (offsite) Markdown with our linked, hosted version
$content = str_replace( $images[0][$key] , $hosted_image, $content );
if( $linkmarklet_debug )
error_log( 'upload: ' . $new_image[0] );
}
}
}
}
}
// set the post_content and status
$post['post_content'] = $content;
$post['post_status'] = 'draft';
// set our post format
if( isset( $settings['post_format'] ) )
{
if( current_theme_supports( 'post-formats', $settings['post_format'] ) )
{
set_post_format( $post_ID, $settings['post_format'] );
}
else
{
set_post_format( $post_ID, false );
}
}
// set the category
$post['post_category'] = array( $post['post_category'] );
// set the slug
$post['post_name'] = sanitize_title( $_POST['slug'] );
// update what we've set
$post_ID = wp_update_post( $post );
// we also need to add our custom field link
$custom_field = isset( $settings['custom_field'] ) ? $settings['custom_field'] : '';
if( !empty( $custom_field ) )
update_post_meta( $post_ID, $custom_field, mysql_real_escape_string( $_POST['url'] ) );
// set our post tags if applicable
if( !empty( $settings['support_tags'] ) && !empty( $_POST['tags'] ) )
wp_set_post_tags( $post_ID, $_POST['tags'] );
// mark as published if that's the intention
if ( isset( $_POST['publish'] ) && current_user_can( 'publish_posts' ) )
{
if( $future_publish )
{
$post['post_status'] = 'future';
if( $linkmarklet_debug )
error_log( '*** altering timestamps' );
$post['edit_date'] = date( 'Y-m-d H:i:s', $timestamp );
$post['post_date'] = date( 'Y-m-d H:i:s', $timestamp );
$post['post_date_gmt'] = date( 'Y-m-d H:i:s', $timestamp_gmt );
if( $linkmarklet_debug )
{
error_log( print_r( $post, true ) );
error_log( '======================' );
}
}
else
{
$post['post_status'] = 'publish';
}
}
// our final update
$post_ID = wp_update_post( $post );
return $post_ID;
}
wp_enqueue_script( 'jquery-ui-autocomplete' );
wp_enqueue_style( 'jquery-ui' );
?><!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Linkmarklet</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<?php do_action( 'admin_print_scripts' ); do_action('admin_head'); ?>
<style>
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
body {
background:#fff;
font:12px Helvetica, Arial, sans-serif;
color:#020204;
margin:0;
padding:0;
}
.hidden {
display:none !important;
}
div.actions {
border-top:1px solid #373739;
padding:8px;
overflow:hidden;
zoom:1;
background: #2a292e;
background: -moz-linear-gradient(top, #2a292e 0%, #201f24 70%, #020204 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#2a292e), color-stop(70%,#201f24), color-stop(100%,#020204));
background: -webkit-linear-gradient(top, #2a292e 0%,#201f24 70%,#020204 100%);
background: -o-linear-gradient(top, #2a292e 0%,#201f24 70%,#020204 100%);
background: -ms-linear-gradient(top, #2a292e 0%,#201f24 70%,#020204 100%);
}
div.actions input {
display:block;
float:right;
color:#EAEBED;
font-weight:normal;
margin:0;
border-radius:5px;
border:1px solid #000;
background: #2a292e;
background: -moz-linear-gradient(top, #2a292e 0%, #201f24 70%, #020204 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#2a292e), color-stop(70%,#201f24), color-stop(100%,#020204));
background: -webkit-linear-gradient(top, #2a292e 0%,#201f24 70%,#020204 100%);
background: -o-linear-gradient(top, #2a292e 0%,#201f24 70%,#020204 100%);
background: -ms-linear-gradient(top, #2a292e 0%,#201f24 70%,#020204 100%);
padding:5px 11px;
cursor:pointer;
}
div.actions input:first-of-type {
float:left;
}
div.field {
overflow:hidden;
zoom:1;
position:relative;
border-top:1px solid #CBCBCB;
padding:8px;
}
div.field label {
width:110px;
position:absolute;
left:8px;
top:12px;
color:#bbb;
}
div.field input {
display:block;
width:100%;
padding-left:110px;
font-size:12px;
border:0;
-webkit-appearance:none;
}
div.field input:focus {
outline:none;
}
div.textarea label {
display:none;
}
div.textarea {
min-height:200px;
}
div.field textarea {
display:block;
width:100%;
min-height:200px;
height:100%;
font-size:12px;
border:0;
-webkit-appearance:none;
resize:none;
}
div.field textarea:focus {
outline:none;
}
.message {
padding:15px;
max-width:600px;
margin:0 auto;
}
.message p {
border-radius:5px;
text-align:center;
background:#efefef;
border:1px solid #ccc;
padding:8px 15px;
}
.message a {
color:#238FF1;
}
.ui-autocomplete {
/* By default the list is positioned according to the field, but we want a bit different */
width:200px !important;
left:110px !important;
}
</style>
</head>
<body>
<?php
if( isset( $_REQUEST['_wpnonce'] ) )
{
check_admin_referer( 'linkmarklet-press-this' );
$posted = $post_ID = linkmarklet_post();
?>
<div class="message">
<p>Entry posted. <a onclick="window.opener.location.replace(this.href); window.close();" href="<?php echo get_permalink( $posted ); ?>">View post</a></p>
</div>
<?php } else { ?>
<?php $settings = get_option( LINKMARKLET_PREFIX . 'settings' ); ?>
<form action="" method="post">
<div class="hidden">
<?php wp_nonce_field( 'linkmarklet-press-this' ); ?>
<input type="hidden" name="post_type" id="post_type" value="text"/>
<input type="hidden" name="autosave" id="autosave" />
<input type="hidden" id="original_post_status" name="original_post_status" value="draft" />
<input type="hidden" id="prev_status" name="prev_status" value="draft" />
<input type="hidden" id="post_id" name="post_id" value="<?php echo (int) $post_ID; ?>" />
</div>
<div class="actions" id="row-actions">
<input type="submit" name="save" id="save" value="Save" />
<input type="submit" name="publish" id="publish" value="Publish" />
</div>
<div class="field textfield" id="row-title">
<label for="title">Title</label>
<input type="text" name="title" id="title" value="<?php echo $title; ?>" />
</div>
<div class="field textfield" id="row-url">
<label for="url">Link URL</label>
<input type="text" name="url" id="url" value="<?php echo $url; ?>" />
</div>
<div class="field textfield" id="row-slug">
<label for="slug">Slug</label>
<input type="text" name="slug" id="slug" value="<?php if( isset( $settings['prepopulate_slug'] ) ) { echo sanitize_title( $title ); } ?>" />
</div>
<?php if( !empty( $settings['support_tags'] ) ) : ?>
<div class="field textfield" id="row-tags">
<label for="url">Tags</label>
<input type="text" name="tags" id="tags" value="" />
</div>
<?php endif; ?>
<div class="field textarea" id="row-content">
<label for="content">Content</label>
<textarea name="content" id="content"><?php echo $selection; ?></textarea>
</div>
</form>
<?php } ?>
<script type="text/javascript">
function reposition(){
var window_height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
var actions = document.getElementById('row-actions').offsetHeight;
var title = document.getElementById('row-title').offsetHeight;
var url = document.getElementById('row-url').offsetHeight;
var slug = document.getElementById('row-slug').offsetHeight;
var height = window_height - actions - title - url - slug - 25;
<?php if( !empty( $settings['support_tags'] ) ) : ?>
var tags = document.getElementById('row-tags').offsetHeight;
height = height - tags;
<?php endif; ?>
document.getElementById('content').style.height = height + 'px';
}
reposition();
window.onresize = function(event) {
reposition();
}
</script>
<?php if( !empty( $settings['support_tags'] ) ) : ?>
<?php
$args = array( 'hide_empty' => false );
$tags = get_tags( $args );
foreach( $tags as $tag )
$all_tags[] = '"' . str_replace( '"', '\"', $tag->name ) . '"';
?>
<?php
do_action('admin_footer');
do_action('admin_print_footer_scripts');
?>
<script type="text/javascript">
var LINKMARKLET_TAGS = [<?php echo implode( ',', $all_tags ); ?>];
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
jQuery('#tags')
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === jQuery.ui.keyCode.TAB &&
jQuery( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function( request, response ) {
// delegate back to autocomplete, but extract the last term
response( jQuery.ui.autocomplete.filter(
LINKMARKLET_TAGS, extractLast( request.term ) ) );
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
</script>
<?php
endif;
?>
</body>
</html>