3 * Press This Display and Handler.
6 * @subpackage Press_This
9 /** WordPress Administration Bootstrap */
10 require_once('admin.php');
12 if ( ! current_user_can('publish_posts') ) wp_die( __( 'Cheatin’ uh?' ) );
15 * Replace forward slash with backslash and slash.
18 * @subpackage Press_This
21 * @param string $string
24 function preg_quote2($string) {
25 return str_replace('/', '\/', preg_quote($string));
32 * @subpackage Press_This
38 function aposfix($text) {
39 $translation_table[chr(34)] = '"';
40 $translation_table[chr(38)] = '&';
41 $translation_table[chr(39)] = ''';
42 return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/","&" , strtr($text, $translation_table));
46 * Press It form handler.
49 * @subpackage Press_This
55 // define some basic variables
56 $quick['post_status'] = 'draft'; // set as draft first
57 $quick['post_category'] = $_REQUEST['post_category'];
58 $quick['tags_input'] = $_REQUEST['tags_input'];
59 $quick['post_title'] = $_REQUEST['title'];
60 $quick['post_content'] = '';
62 // insert the post with nothing in it, to get an ID
63 $post_ID = wp_insert_post($quick, true);
64 $content = $_REQUEST['content'];
66 if($_REQUEST['photo_src'])
67 foreach( (array) $_REQUEST['photo_src'] as $key => $image)
68 // see if files exist in content - we don't want to upload non-used selected files.
69 if( strpos($_REQUEST['content'], $image) !== false ) {
70 $upload = media_sideload_image($image, $post_ID, $_REQUEST['photo_description'][$key]);
72 // Replace the POSTED content <img> with correct uploaded ones.
73 // escape quote for matching
74 $quoted = preg_quote2($image);
75 if( !is_wp_error($upload) ) $content = preg_replace('/<img ([^>]*)src=(\"|\')'.$quoted.'(\2)([^>\/]*)\/*>/is', $upload, $content);
78 // set the post_content and status
79 $quick['post_status'] = isset($_REQUEST['publish']) ? 'publish' : 'draft';
80 $quick['post_content'] = $content;
81 // error handling for $post
82 if ( is_wp_error($post_ID)) {
84 wp_delete_post($post_ID);
85 // error handling for media_sideload
86 } elseif ( is_wp_error($upload)) {
88 wp_delete_post($post_ID);
90 $quick['ID'] = $post_ID;
91 wp_update_post($quick);
96 // For submitted posts.
97 if ( 'post' == $_REQUEST['action'] ) {
98 check_admin_referer('press-this');
99 $post_ID = press_it();
104 $title = wp_specialchars(aposfix(stripslashes($_GET['t'])));
105 $selection = trim( aposfix( stripslashes($_GET['s']) ) );
106 if ( ! empty($selection) ) {
107 $selection = preg_replace('/(\r?\n|\r)/', '</p><p>', $selection);
108 $selection = '<p>'.str_replace('<p></p>', '', $selection).'</p>';
110 $url = clean_url($_GET['u']);
113 if($_REQUEST['ajax']) {
114 switch ($_REQUEST['ajax']) {
116 <script type="text/javascript" charset="utf-8">
117 jQuery('.select').click(function() {
118 append_editor(jQuery('#embed-code').val());
119 jQuery('#extra_fields').hide();
120 jQuery('#extra_fields').html('');
122 jQuery('.close').click(function() {
123 jQuery('#extra_fields').hide();
124 jQuery('#extra_fields').html('');
127 <div class="postbox">
128 <h2><label for="embed-code"><?php _e('Embed Code') ?></label></h2>
130 <textarea name="embed-code" id="embed-code" rows="8" cols="40"><?php echo format_to_edit($selection, true); ?></textarea>
131 <p id="options"><a href="#" class="select button"><?php _e('Insert Video'); ?></a> <a href="#" class="close button"><?php _e('Cancel'); ?></a></p>
136 case 'photo_thickbox': ?>
137 <script type="text/javascript" charset="utf-8">
138 jQuery('.cancel').click(function() {
141 jQuery('.select').click(function() {
145 <h3 class="tb"><label for="this_photo_description"><?php _e('Description') ?></label></h3>
146 <div class="titlediv">
147 <div class="titlewrap">
148 <input id="this_photo_description" name="photo_description" class="tbtitle text" onkeypress="if(event.keyCode==13) image_selector();" value="<?php echo attribute_escape($title);?>"/>
152 <p class="centered"><input type="hidden" name="this_photo" value="<?php echo attribute_escape($image); ?>" id="this_photo" />
153 <a href="#" class="select"><img src="<?php echo clean_url($image); ?>" alt="<?php echo attribute_escape(__('Click to insert.')); ?>" title="<?php echo attribute_escape(__('Click to insert.')); ?>" /></a></p>
155 <p id="options"><a href="#" class="select button"><?php _e('Insert Image'); ?></a> <a href="#" class="cancel button"><?php _e('Cancel'); ?></a></p>
160 case 'photo_thickbox_url': ?>
161 <script type="text/javascript" charset="utf-8">
162 jQuery('.cancel').click(function() {
166 jQuery('.select').click(function() {
170 <h3 class="tb"><label for="this_photo"><?php _e('URL') ?></label></h3>
171 <div class="titlediv">
172 <div class="titlewrap">
173 <input id="this_photo" name="this_photo" class="tbtitle text" onkeypress="if(event.keyCode==13) image_selector();" />
178 <h3 class="tb"><label for="photo_description"><?php _e('Description') ?></label></h3>
180 <div class="titlewrap">
181 <input id="this_photo_description" name="photo_description" class="tbtitle text" onkeypress="if(event.keyCode==13) image_selector();" value="<?php echo attribute_escape($title);?>"/>
185 <p id="options"><a href="#" class="select"><?php _e('Insert Image'); ?></a> | <a href="#" class="cancel"><?php _e('Cancel'); ?></a></p>
189 * Retrieve all image URLs from given URI.
192 * @subpackage Press_This
198 function get_images_from_uri($uri) {
199 if( preg_match('/\.(jpg|jpe|jpeg|png|gif)$/', $uri) && !strpos($uri,'blogger.com') )
201 $content = wp_remote_fopen($uri);
202 if ( false === $content )
204 $host = parse_url($uri);
205 $pattern = '/<img ([^>]*)src=(\"|\')([^<>]+?\.(png|jpeg|jpg|jpe|gif))[^<>\'\"]*(\2)([^>\/]*)\/*>/is';
206 preg_match_all($pattern, $content, $matches);
207 if ( empty($matches[0]) )
210 foreach ($matches[3] as $src) {
212 if(strpos($src, 'http') === false)
213 // if it doesn't have a relative uri
214 if( strpos($src, '../') === false && strpos($src, './') === false && strpos($src, '/') === 0)
215 $src = 'http://'.str_replace('//','/', $host['host'].'/'.$src);
217 $src = 'http://'.str_replace('//','/', $host['host'].'/'.dirname($host['path']).'/'.$src);
218 $sources[] = clean_url($src);
220 return "'" . implode("','", $sources) . "'";
222 $url = urldecode($url);
223 $url = str_replace(' ', '%20', $url);
224 echo 'new Array('.get_images_from_uri($url).')';
229 // gather images and load some default JS
231 var img, img_tag, aspect, w, h, skip, i, strtoappend = "";
235 url: "<?php echo clean_url($_SERVER['PHP_SELF']); ?>",
238 data: "ajax=photo_images&u=<?php echo urlencode($url); ?>",
242 if(my_src.length == 0) {
246 url: "<?php echo clean_url($_SERVER['PHP_SELF']); ?>",
249 data: "ajax=photo_images&u=<?php echo urlencode($url); ?>",
253 if(my_src.length == 0) {
254 strtoappend = '<?php _e('Unable to retrieve images or no images on page.'); ?>';
258 for (i = 0; i < my_src.length; i++) {
261 img_attr = 'id="img' + i + '"';
264 maybeappend = '<a href="?ajax=photo_thickbox&i=' + encodeURIComponent(img.src) + '&u=<?php echo urlencode($url); ?>&height=400&width=500" title="" class="thickbox"><img src="' + img.src + '" ' + img_attr + '/></a>';
266 if (img.width && img.height) {
267 if (img.width >= 30 && img.height >= 30) {
268 aspect = img.width / img.height;
269 scale = (aspect > 1) ? (71 / img.width) : (71 / img.height);
275 w = parseInt(img.width * scale);
276 h = parseInt(img.height * scale);
278 img_attr += ' style="width: ' + w + 'px; height: ' + h + 'px;"';
279 strtoappend += maybeappend;
282 strtoappend += maybeappend;
286 function pick(img, desc) {
288 if('object' == typeof jQuery('.photolist input') && jQuery('.photolist input').length != 0) length = jQuery('.photolist input').length;
289 if(length == 0) length = 1;
290 jQuery('.photolist').append('<input name="photo_src[' + length + ']" value="' + img +'" type="hidden"/>');
291 jQuery('.photolist').append('<input name="photo_description[' + length + ']" value="' + desc +'" type="hidden"/>');
292 insert_editor( "\n\n" + encodeURI('<p style="text-align: center;"><a href="<?php echo $url; ?>"><img src="' + img +'" alt="' + desc + '" /></a></p>'));
297 function image_selector() {
299 desc = jQuery('#this_photo_description').val();
300 src = jQuery('#this_photo').val();
302 jQuery('#extra_fields').hide();
303 jQuery('#extra_fields').html('');
307 jQuery(document).ready(function() {
308 jQuery('#extra_fields').html('<div class="postbox"><h2>Photo <small id="photo_directions">(<?php _e("click images to select") ?>)</small></h2><ul id="actions"><li><a href="#" id="photo_add_url" class="thickbox button"><?php _e("Add from URL") ?> +</a></li></ul><div class="inside"><div class="titlewrap"><div id="img_container"></div></div><p id="options"><a href="#" class="close button"><?php _e('Cancel'); ?></a><a href="#" class="refresh button"><?php _e('Refresh'); ?></a></p></div>');
309 jQuery('.close').click(function() {
310 jQuery('#extra_fields').hide();
311 jQuery('#extra_fields').html('');
313 jQuery('.refresh').click(function() {
316 jQuery('#img_container').html(strtoappend);
317 jQuery('#photo_add_url').attr('href', '?ajax=photo_thickbox_url&height=200&width=500');
318 tb_init('#extra_fields .thickbox');
328 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
329 <html xmlns="http://www.w3.org/1999/xhtml" <?php do_action('admin_xml_ns'); ?> <?php language_attributes(); ?>>
331 <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_option('blog_charset'); ?>" />
332 <title><?php _e('Press This') ?></title>
336 wp_enqueue_style('press-this');
337 wp_enqueue_style('press-this-ie');
338 wp_enqueue_style( 'colors' );
339 wp_enqueue_script( 'post' );
340 wp_enqueue_script('editor');
342 do_action('admin_print_styles');
343 do_action('admin_print_scripts');
344 do_action('admin_head');
346 if ( user_can_richedit() ) {
347 add_filter( 'teeny_mce_before_init', create_function( '$a', '$a["height"] = "400"; $a["onpageload"] = ""; $a["mode"] = "textareas"; $a["editor_selector"] = "mceEditor"; return $a;' ) );
351 <script type="text/javascript">
352 jQuery('#tags-input').hide();
353 tag_update_quickclicks();
354 // add the quickadd form
355 jQuery('#jaxtag').prepend('<span id="ajaxtag"><input type="text" name="newtag" id="newtag" class="form-input-tip" size="16" autocomplete="off" value="'+postL10n.addTag+'" /><input type="submit" class="button" id="tagadd" value="' + postL10n.add + '" tabindex="3" onclick="return false;" /><input type="hidden"/><input type="hidden"/><span class="howto">'+postL10n.separate+'</span></span>');
357 jQuery('#tagadd').click( tag_flush_to_text );
358 jQuery('#newtag').focus(function() {
359 if ( this.value == postL10n.addTag )
360 jQuery(this).val( '' ).removeClass( 'form-input-tip' );
362 jQuery('#newtag').blur(function() {
363 if ( this.value == '' )
364 jQuery(this).val( postL10n.addTag ).addClass( 'form-input-tip' );
366 // auto-save tags on post save/publish
367 jQuery('#publish').click( tag_save_on_publish );
368 jQuery('#save').click( tag_save_on_publish );
369 function insert_plain_editor(text) {
370 edCanvas = document.getElementById('content');
371 edInsertContent(edCanvas, text);
373 function set_editor(text) {
374 if ( '' == text || '<p></p>' == text ) text = '<p><br /></p>';
375 if ( tinyMCE.activeEditor ) tinyMCE.execCommand('mceSetContent', false, text);
377 function insert_editor(text) {
378 if ( '' != text && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden()) {
379 tinyMCE.execCommand('mceInsertContent', false, '<p>' + decodeURI(tinymce.DOM.decode(text)) + '</p>', {format : 'raw'});
381 insert_plain_editor(decodeURI(text));
384 function append_editor(text) {
385 if ( '' != text && tinyMCE.activeEditor && ! tinyMCE.activeEditor.isHidden()) {
386 tinyMCE.execCommand('mceSetContent', false, tinyMCE.activeEditor.getContent({format : 'raw'}) + '<p>' + text + '</p>');
387 tinyMCE.execCommand('mceCleanup');
389 insert_plain_editor(text);
393 function show(tab_name) {
394 jQuery('#extra_fields').html('');
395 jQuery('#extra_fields').show();
398 jQuery('#extra_fields').load('<?php echo clean_url($_SERVER['PHP_SELF']); ?>', { ajax: 'video', s: '<?php echo attribute_escape($selection); ?>'}, function() {
401 if ( preg_match("/youtube\.com\/watch/i", $url) ) {
402 list($domain, $video_id) = split("v=", $url);
403 $content = '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/' . $video_id . '"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/' . $video_id . '" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>';
405 } elseif ( preg_match("/vimeo\.com\/[0-9]+/i", $url) ) {
406 list($domain, $video_id) = split(".com/", $url);
407 $content = '<object width="400" height="225"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://www.vimeo.com/moogaloop.swf?clip_id=' . $video_id . '&server=www.vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1" /> <embed src="http://www.vimeo.com/moogaloop.swf?clip_id=' . $video_id . '&server=www.vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="225"></embed></object>';
409 if ( trim($selection) == '' )
410 $selection = '<p><a href="http://www.vimeo.com/' . $video_id . '?pg=embed&sec=' . $video_id . '">' . $title . '</a> on <a href="http://vimeo.com?pg=embed&sec=' . $video_id . '">Vimeo</a></p>';
412 } elseif ( strpos( $selection, '<object' ) !== false ) {
413 $content = $selection;
416 jQuery('#embed-code').prepend('<?php echo htmlentities($content); ?>');
421 jQuery('#extra_fields').before('<p id="waiting"><img src="images/loading.gif" alt="" /> <?php echo js_escape( __( 'Loading...' ) ); ?></p>');
425 url: "<?php echo clean_url($_SERVER['PHP_SELF']); ?>",
426 data: "ajax=photo_js&u=<?php echo urlencode($url)?>",
428 success : function() {
429 jQuery('#waiting').remove();
436 jQuery(document).ready(function() {
438 window.resizeTo(720,570);
439 // set button actions
440 jQuery('#photo_button').click(function() { show('photo'); return false; });
441 jQuery('#video_button').click(function() { show('video'); return false; });
443 <?php if ( preg_match("/youtube\.com\/watch/i", $url) ) { ?>
445 <?php } elseif ( preg_match("/vimeo\.com\/[0-9]+/i", $url) ) { ?>
447 <?php } elseif ( preg_match("/flickr\.com/i", $url) ) { ?>
450 jQuery('#title').unbind();
451 jQuery('#publish, #save').click(function() { jQuery('#saving').css('display', 'inline'); });
455 <body class="press-this">
456 <div id="wphead"></div>
457 <form action="press-this.php?action=post" method="post">
458 <div id="poststuff" class="metabox-holder">
459 <div id="side-info-column">
461 <h1 id="viewsite"><a class="button" href="<?php echo get_option('home'); ?>/" target="_blank"><?php bloginfo('name'); ?> › <?php _e('Press This') ?></a></span></h1>
463 <?php wp_nonce_field('press-this') ?>
464 <input type="hidden" name="post_type" id="post_type" value="text"/>
465 <input type="hidden" name="autosave" id="autosave" />
466 <input type="hidden" id="original_post_status" name="original_post_status" value="draft" />
467 <input type="hidden" id="prev_status" name="prev_status" value="draft" />
469 <!-- This div holds the photo metadata -->
470 <div class="photolist"></div>
472 <div id="categorydiv" class="stuffbox">
473 <h2><?php _e('Categories') ?></h2>
476 <div id="categories-all" class="ui-tabs-panel">
477 <ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
478 <?php wp_category_checklist($post->ID, false, false, $popular_ids) ?>
482 <div id="category-adder" class="wp-hidden-children">
483 <a id="category-add-toggle" href="#category-add" class="hide-if-no-js" tabindex="3"><?php _e( '+ Add New Category' ); ?></a>
484 <p id="category-add" class="wp-hidden-child">
485 <label class="hidden" for="newcat"><?php _e( 'Add New Category' ); ?></label><input type="text" name="newcat" id="newcat" class="form-required form-input-tip" value="<?php _e( 'New category name' ); ?>" tabindex="3" aria-required="true"/>
486 <label class="hidden" for="newcat_parent"><?php _e('Parent category'); ?>:</label><?php wp_dropdown_categories( array( 'hide_empty' => 0, 'name' => 'newcat_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => __('Parent category'), 'tab_index' => 3 ) ); ?>
487 <input type="button" id="category-add-sumbit" class="add:categorychecklist:category-add button" value="<?php _e( 'Add' ); ?>" tabindex="3" />
488 <?php wp_nonce_field( 'add-category', '_ajax_nonce', false ); ?>
489 <span id="category-ajax-response"></span>
495 <div class="stuffbox">
496 <h2><?php _e('Tags') ?></h2>
500 <label class="hidden" for="newtag"><?php _e('Tags'); ?></label>
501 <input type="text" name="tags_input" class="tags-input" id="tags-input" size="40" tabindex="3" value="<?php echo get_tags_to_edit( $post->ID ); ?>" />
503 <div id="tagchecklist"></div>
506 <div id="submitdiv" class="postbox">
507 <h2><?php _e('Publish') ?></h2>
510 <input class="button" type="submit" name="draft" value="<?php _e('Save Draft') ?>" id="save" />
511 <input class="button-primary" type="submit" name="publish" value="<?php _e('Publish') ?>" id="publish" />
512 <img src="images/loading-publish.gif" alt="" id="saving" style="display:none;"/>
519 <div class="posting">
520 <?php if ( isset($posted) && intval($posted) ) { $post_ID = intval($posted); ?>
521 <div id="message" class="updated fade"><p><strong><?php _e('Your post has been saved.'); ?></strong> <a onclick="window.opener.location.replace(this.href); window.close();" href="<?php echo get_permalink( $post_ID); ?>"><?php _e('View post'); ?></a> | <a href="<?php echo get_edit_post_link( $post_ID ); ?>" onclick="window.opener.location.replace(this.href); window.close();"><?php _e('Edit post'); ?></a> | <a href="#" onclick="window.close();"><?php _e('Close Window'); ?></a></p></div>
525 <div class="titlewrap">
526 <input name="title" id="title" class="text" value="<?php echo attribute_escape($title);?>"/>
530 <div id="extra_fields" style="display: none"></div>
532 <div class="postdivrich">
534 <li id="photo_button">
535 Add: <a title="<?php _e('Insert an Image'); ?>" href="#">
536 <img alt="<?php _e('Insert an Image'); ?>" src="images/media-button-image.gif"/></a>
538 <li id="video_button">
539 <a title="<?php _e('Embed a Video'); ?>" href="#"><img alt="<?php _e('Embed a Video'); ?>" src="images/media-button-video.gif"/></a>
541 <?php if( user_can_richedit() ) { ?>
543 <?php wp_print_scripts( 'quicktags' ); ?>
544 <?php add_filter('the_editor_content', 'wp_richedit_pre'); ?>
545 <a id="edButtonHTML" onclick="switchEditors.go('<?php echo $id; ?>', 'html');"><?php _e('HTML'); ?></a>
546 <a id="edButtonPreview" class="active" onclick="switchEditors.go('<?php echo $id; ?>', 'tinymce');"><?php _e('Visual'); ?></a>
547 <div class="zerosize"><input accesskey="e" type="button" onclick="switchEditors.go('<?php echo $id; ?>')" /></div>
551 <div id="quicktags"></div>
552 <div class="editor-container">
553 <textarea name="content" id="content" style="width:100%;" class="mceEditor" rows="15">
554 <?php if ($selection) echo wp_richedit_pre(htmlspecialchars_decode($selection)); ?>
555 <?php if ($url) { echo '<p>'; if($selection) _e('via '); echo "<a href='$url'>$title</a>."; echo '</p>'; } ?>