WordPress 4.7
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-press-this.php
1 <?php
2 /**
3  * Press This class and display functionality
4  *
5  * @package WordPress
6  * @subpackage Press_This
7  * @since 4.2.0
8  */
9
10 /**
11  * Press This class.
12  *
13  * @since 4.2.0
14  */
15 class WP_Press_This {
16         // Used to trigger the bookmarklet update notice.
17         const VERSION = 8;
18         public $version = 8;
19
20         private $images = array();
21
22         private $embeds = array();
23
24         private $domain = '';
25
26         /**
27          * Constructor.
28          *
29          * @since 4.2.0
30          * @access public
31          */
32         public function __construct() {}
33
34         /**
35          * App and site settings data, including i18n strings for the client-side.
36          *
37          * @since 4.2.0
38          * @access public
39          *
40          * @return array Site settings.
41          */
42         public function site_settings() {
43                 return array(
44                         /**
45                          * Filters whether or not Press This should redirect the user in the parent window upon save.
46                          *
47                          * @since 4.2.0
48                          *
49                          * @param bool $redirect Whether to redirect in parent window or not. Default false.
50                          */
51                         'redirInParent' => apply_filters( 'press_this_redirect_in_parent', false ),
52                 );
53         }
54
55         /**
56          * Get the source's images and save them locally, for posterity, unless we can't.
57          *
58          * @since 4.2.0
59          * @access public
60          *
61          * @param int    $post_id Post ID.
62          * @param string $content Optional. Current expected markup for Press This. Expects slashed. Default empty.
63          * @return string New markup with old image URLs replaced with the local attachment ones if swapped.
64          */
65         public function side_load_images( $post_id, $content = '' ) {
66                 $content = wp_unslash( $content );
67
68                 if ( preg_match_all( '/<img [^>]+>/', $content, $matches ) && current_user_can( 'upload_files' ) ) {
69                         foreach ( (array) $matches[0] as $image ) {
70                                 // This is inserted from our JS so HTML attributes should always be in double quotes.
71                                 if ( ! preg_match( '/src="([^"]+)"/', $image, $url_matches ) ) {
72                                         continue;
73                                 }
74
75                                 $image_src = $url_matches[1];
76
77                                 // Don't try to sideload a file without a file extension, leads to WP upload error.
78                                 if ( ! preg_match( '/[^\?]+\.(?:jpe?g|jpe|gif|png)(?:\?|$)/i', $image_src ) ) {
79                                         continue;
80                                 }
81
82                                 // Sideload image, which gives us a new image src.
83                                 $new_src = media_sideload_image( $image_src, $post_id, null, 'src' );
84
85                                 if ( ! is_wp_error( $new_src ) ) {
86                                         // Replace the POSTED content <img> with correct uploaded ones.
87                                         // Need to do it in two steps so we don't replace links to the original image if any.
88                                         $new_image = str_replace( $image_src, $new_src, $image );
89                                         $content = str_replace( $image, $new_image, $content );
90                                 }
91                         }
92                 }
93
94                 // Expected slashed
95                 return wp_slash( $content );
96         }
97
98         /**
99          * Ajax handler for saving the post as draft or published.
100          *
101          * @since 4.2.0
102          * @access public
103          */
104         public function save_post() {
105                 if ( empty( $_POST['post_ID'] ) || ! $post_id = (int) $_POST['post_ID'] ) {
106                         wp_send_json_error( array( 'errorMessage' => __( 'Missing post ID.' ) ) );
107                 }
108
109                 if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-post_' . $post_id ) ||
110                         ! current_user_can( 'edit_post', $post_id ) ) {
111
112                         wp_send_json_error( array( 'errorMessage' => __( 'Invalid post.' ) ) );
113                 }
114
115                 $post_data = array(
116                         'ID'            => $post_id,
117                         'post_title'    => ( ! empty( $_POST['post_title'] ) ) ? sanitize_text_field( trim( $_POST['post_title'] ) ) : '',
118                         'post_content'  => ( ! empty( $_POST['post_content'] ) ) ? trim( $_POST['post_content'] ) : '',
119                         'post_type'     => 'post',
120                         'post_status'   => 'draft',
121                         'post_format'   => ( ! empty( $_POST['post_format'] ) ) ? sanitize_text_field( $_POST['post_format'] ) : '',
122                         'tax_input'     => ( ! empty( $_POST['tax_input'] ) ) ? $_POST['tax_input'] : array(),
123                         'post_category' => ( ! empty( $_POST['post_category'] ) ) ? $_POST['post_category'] : array(),
124                 );
125
126                 if ( ! empty( $_POST['post_status'] ) && 'publish' === $_POST['post_status'] ) {
127                         if ( current_user_can( 'publish_posts' ) ) {
128                                 $post_data['post_status'] = 'publish';
129                         } else {
130                                 $post_data['post_status'] = 'pending';
131                         }
132                 }
133
134                 $post_data['post_content'] = $this->side_load_images( $post_id, $post_data['post_content'] );
135
136                 /**
137                  * Filters the post data of a Press This post before saving/updating.
138                  *
139                  * The {@see 'side_load_images'} action has already run at this point.
140                  *
141                  * @since 4.5.0
142                  *
143                  * @param array $post_data The post data.
144                  */
145                 $post_data = apply_filters( 'press_this_save_post', $post_data );
146
147                 $updated = wp_update_post( $post_data, true );
148
149                 if ( is_wp_error( $updated ) ) {
150                         wp_send_json_error( array( 'errorMessage' => $updated->get_error_message() ) );
151                 } else {
152                         if ( isset( $post_data['post_format'] ) ) {
153                                 if ( current_theme_supports( 'post-formats', $post_data['post_format'] ) ) {
154                                         set_post_format( $post_id, $post_data['post_format'] );
155                                 } elseif ( $post_data['post_format'] ) {
156                                         set_post_format( $post_id, false );
157                                 }
158                         }
159
160                         $forceRedirect = false;
161
162                         if ( 'publish' === get_post_status( $post_id ) ) {
163                                 $redirect = get_post_permalink( $post_id );
164                         } elseif ( isset( $_POST['pt-force-redirect'] ) && $_POST['pt-force-redirect'] === 'true' ) {
165                                 $forceRedirect = true;
166                                 $redirect = get_edit_post_link( $post_id, 'js' );
167                         } else {
168                                 $redirect = false;
169                         }
170
171                         /**
172                          * Filters the URL to redirect to when Press This saves.
173                          *
174                          * @since 4.2.0
175                          *
176                          * @param string $url     Redirect URL. If `$status` is 'publish', this will be the post permalink.
177                          *                        Otherwise, the default is false resulting in no redirect.
178                          * @param int    $post_id Post ID.
179                          * @param string $status  Post status.
180                          */
181                         $redirect = apply_filters( 'press_this_save_redirect', $redirect, $post_id, $post_data['post_status'] );
182
183                         if ( $redirect ) {
184                                 wp_send_json_success( array( 'redirect' => $redirect, 'force' => $forceRedirect ) );
185                         } else {
186                                 wp_send_json_success( array( 'postSaved' => true ) );
187                         }
188                 }
189         }
190
191         /**
192          * Ajax handler for adding a new category.
193          *
194          * @since 4.2.0
195          * @access public
196          */
197         public function add_category() {
198                 if ( false === wp_verify_nonce( $_POST['new_cat_nonce'], 'add-category' ) ) {
199                         wp_send_json_error();
200                 }
201
202                 $taxonomy = get_taxonomy( 'category' );
203
204                 if ( ! current_user_can( $taxonomy->cap->edit_terms ) || empty( $_POST['name'] ) ) {
205                         wp_send_json_error();
206                 }
207
208                 $parent = isset( $_POST['parent'] ) && (int) $_POST['parent'] > 0 ? (int) $_POST['parent'] : 0;
209                 $names = explode( ',', $_POST['name'] );
210                 $added = $data = array();
211
212                 foreach ( $names as $cat_name ) {
213                         $cat_name = trim( $cat_name );
214                         $cat_nicename = sanitize_title( $cat_name );
215
216                         if ( empty( $cat_nicename ) ) {
217                                 continue;
218                         }
219
220                         // @todo Find a more performant way to check existence, maybe get_term() with a separate parent check.
221                         if ( term_exists( $cat_name, $taxonomy->name, $parent ) ) {
222                                 if ( count( $names ) === 1 ) {
223                                         wp_send_json_error( array( 'errorMessage' => __( 'This category already exists.' ) ) );
224                                 } else {
225                                         continue;
226                                 }
227                         }
228
229                         $cat_id = wp_insert_term( $cat_name, $taxonomy->name, array( 'parent' => $parent ) );
230
231                         if ( is_wp_error( $cat_id ) ) {
232                                 continue;
233                         } elseif ( is_array( $cat_id ) ) {
234                                 $cat_id = $cat_id['term_id'];
235                         }
236
237                         $added[] = $cat_id;
238                 }
239
240                 if ( empty( $added ) ) {
241                         wp_send_json_error( array( 'errorMessage' => __( 'This category cannot be added. Please change the name and try again.' ) ) );
242                 }
243
244                 foreach ( $added as $new_cat_id ) {
245                         $new_cat = get_category( $new_cat_id );
246
247                         if ( is_wp_error( $new_cat ) ) {
248                                 wp_send_json_error( array( 'errorMessage' => __( 'Error while adding the category. Please try again later.' ) ) );
249                         }
250
251                         $data[] = array(
252                                 'term_id' => $new_cat->term_id,
253                                 'name' => $new_cat->name,
254                                 'parent' => $new_cat->parent,
255                         );
256                 }
257                 wp_send_json_success( $data );
258         }
259
260         /**
261          * Downloads the source's HTML via server-side call for the given URL.
262          *
263          * @since 4.2.0
264          * @access public
265          *
266          * @param string $url URL to scan.
267          * @return string Source's HTML sanitized markup
268          */
269         public function fetch_source_html( $url ) {
270                 if ( empty( $url ) ) {
271                         return new WP_Error( 'invalid-url', __( 'A valid URL was not provided.' ) );
272                 }
273
274                 $remote_url = wp_safe_remote_get( $url, array(
275                         'timeout' => 30,
276                         // Use an explicit user-agent for Press This
277                         'user-agent' => 'Press This (WordPress/' . get_bloginfo( 'version' ) . '); ' . get_bloginfo( 'url' )
278                 ) );
279
280                 if ( is_wp_error( $remote_url ) ) {
281                         return $remote_url;
282                 }
283
284                 $allowed_elements = array(
285                         'img' => array(
286                                 'src'      => true,
287                                 'width'    => true,
288                                 'height'   => true,
289                         ),
290                         'iframe' => array(
291                                 'src'      => true,
292                         ),
293                         'link' => array(
294                                 'rel'      => true,
295                                 'itemprop' => true,
296                                 'href'     => true,
297                         ),
298                         'meta' => array(
299                                 'property' => true,
300                                 'name'     => true,
301                                 'content'  => true,
302                         )
303                 );
304
305                 $source_content = wp_remote_retrieve_body( $remote_url );
306                 $source_content = wp_kses( $source_content, $allowed_elements );
307
308                 return $source_content;
309         }
310
311         /**
312          * Utility method to limit an array to 50 values.
313          *
314          * @ignore
315          * @since 4.2.0
316          *
317          * @param array $value Array to limit.
318          * @return array Original array if fewer than 50 values, limited array, empty array otherwise.
319          */
320         private function _limit_array( $value ) {
321                 if ( is_array( $value ) ) {
322                         if ( count( $value ) > 50 ) {
323                                 return array_slice( $value, 0, 50 );
324                         }
325
326                         return $value;
327                 }
328
329                 return array();
330         }
331
332         /**
333          * Utility method to limit the length of a given string to 5,000 characters.
334          *
335          * @ignore
336          * @since 4.2.0
337          *
338          * @param string $value String to limit.
339          * @return bool|int|string If boolean or integer, that value. If a string, the original value
340          *                         if fewer than 5,000 characters, a truncated version, otherwise an
341          *                         empty string.
342          */
343         private function _limit_string( $value ) {
344                 $return = '';
345
346                 if ( is_numeric( $value ) || is_bool( $value ) ) {
347                         $return = $value;
348                 } else if ( is_string( $value ) ) {
349                         if ( mb_strlen( $value ) > 5000 ) {
350                                 $return = mb_substr( $value, 0, 5000 );
351                         } else {
352                                 $return = $value;
353                         }
354
355                         $return = html_entity_decode( $return, ENT_QUOTES, 'UTF-8' );
356                         $return = sanitize_text_field( trim( $return ) );
357                 }
358
359                 return $return;
360         }
361
362         /**
363          * Utility method to limit a given URL to 2,048 characters.
364          *
365          * @ignore
366          * @since 4.2.0
367          *
368          * @param string $url URL to check for length and validity.
369          * @return string Escaped URL if of valid length (< 2048) and makeup. Empty string otherwise.
370          */
371         private function _limit_url( $url ) {
372                 if ( ! is_string( $url ) ) {
373                         return '';
374                 }
375
376                 // HTTP 1.1 allows 8000 chars but the "de-facto" standard supported in all current browsers is 2048.
377                 if ( strlen( $url ) > 2048 ) {
378                         return ''; // Return empty rather than a truncated/invalid URL
379                 }
380
381                 // Does not look like a URL.
382                 if ( ! preg_match( '/^([!#$&-;=?-\[\]_a-z~]|%[0-9a-fA-F]{2})+$/', $url ) ) {
383                         return '';
384                 }
385
386                 // If the URL is root-relative, prepend the protocol and domain name
387                 if ( $url && $this->domain && preg_match( '%^/[^/]+%', $url ) ) {
388                         $url = $this->domain . $url;
389                 }
390
391                 // Not absolute or protocol-relative URL.
392                 if ( ! preg_match( '%^(?:https?:)?//[^/]+%', $url ) ) {
393                         return '';
394                 }
395
396                 return esc_url_raw( $url, array( 'http', 'https' ) );
397         }
398
399         /**
400          * Utility method to limit image source URLs.
401          *
402          * Excluded URLs include share-this type buttons, loaders, spinners, spacers, WordPress interface images,
403          * tiny buttons or thumbs, mathtag.com or quantserve.com images, or the WordPress.com stats gif.
404          *
405          * @ignore
406          * @since 4.2.0
407          *
408          * @param string $src Image source URL.
409          * @return string If not matched an excluded URL type, the original URL, empty string otherwise.
410          */
411         private function _limit_img( $src ) {
412                 $src = $this->_limit_url( $src );
413
414                 if ( preg_match( '!/ad[sx]?/!i', $src ) ) {
415                         // Ads
416                         return '';
417                 } else if ( preg_match( '!(/share-?this[^.]+?\.[a-z0-9]{3,4})(\?.*)?$!i', $src ) ) {
418                         // Share-this type button
419                         return '';
420                 } else if ( preg_match( '!/(spinner|loading|spacer|blank|rss)\.(gif|jpg|png)!i', $src ) ) {
421                         // Loaders, spinners, spacers
422                         return '';
423                 } else if ( preg_match( '!/([^./]+[-_])?(spinner|loading|spacer|blank)s?([-_][^./]+)?\.[a-z0-9]{3,4}!i', $src ) ) {
424                         // Fancy loaders, spinners, spacers
425                         return '';
426                 } else if ( preg_match( '!([^./]+[-_])?thumb[^.]*\.(gif|jpg|png)$!i', $src ) ) {
427                         // Thumbnails, too small, usually irrelevant to context
428                         return '';
429                 } else if ( false !== stripos( $src, '/wp-includes/' ) ) {
430                         // Classic WordPress interface images
431                         return '';
432                 } else if ( preg_match( '![^\d]\d{1,2}x\d+\.(gif|jpg|png)$!i', $src ) ) {
433                         // Most often tiny buttons/thumbs (< 100px wide)
434                         return '';
435                 } else if ( preg_match( '!/pixel\.(mathtag|quantserve)\.com!i', $src ) ) {
436                         // See mathtag.com and https://www.quantcast.com/how-we-do-it/iab-standard-measurement/how-we-collect-data/
437                         return '';
438                 } else if ( preg_match( '!/[gb]\.gif(\?.+)?$!i', $src ) ) {
439                         // WordPress.com stats gif
440                         return '';
441                 }
442
443                 return $src;
444         }
445
446         /**
447          * Limit embed source URLs to specific providers.
448          *
449          * Not all core oEmbed providers are supported. Supported providers include YouTube, Vimeo,
450          * Vine, Daily Motion, SoundCloud, and Twitter.
451          *
452          * @ignore
453          * @since 4.2.0
454          *
455          * @param string $src Embed source URL.
456          * @return string If not from a supported provider, an empty string. Otherwise, a reformattd embed URL.
457          */
458         private function _limit_embed( $src ) {
459                 $src = $this->_limit_url( $src );
460
461                 if ( empty( $src ) )
462                         return '';
463
464                 if ( preg_match( '!//(m|www)\.youtube\.com/(embed|v)/([^?]+)\?.+$!i', $src, $src_matches ) ) {
465                         // Embedded Youtube videos (www or mobile)
466                         $src = 'https://www.youtube.com/watch?v=' . $src_matches[3];
467                 } else if ( preg_match( '!//player\.vimeo\.com/video/([\d]+)([?/].*)?$!i', $src, $src_matches ) ) {
468                         // Embedded Vimeo iframe videos
469                         $src = 'https://vimeo.com/' . (int) $src_matches[1];
470                 } else if ( preg_match( '!//vimeo\.com/moogaloop\.swf\?clip_id=([\d]+)$!i', $src, $src_matches ) ) {
471                         // Embedded Vimeo Flash videos
472                         $src = 'https://vimeo.com/' . (int) $src_matches[1];
473                 } else if ( preg_match( '!//vine\.co/v/([^/]+)/embed!i', $src, $src_matches ) ) {
474                         // Embedded Vine videos
475                         $src = 'https://vine.co/v/' . $src_matches[1];
476                 } else if ( preg_match( '!//(www\.)?dailymotion\.com/embed/video/([^/?]+)([/?].+)?!i', $src, $src_matches ) ) {
477                         // Embedded Daily Motion videos
478                         $src = 'https://www.dailymotion.com/video/' . $src_matches[2];
479                 } else {
480                         $oembed = _wp_oembed_get_object();
481
482                         if ( ! $oembed->get_provider( $src, array( 'discover' => false ) ) ) {
483                                 $src = '';
484                         }
485                 }
486
487                 return $src;
488         }
489
490         /**
491          * Process a meta data entry from the source.
492          *
493          * @ignore
494          * @since 4.2.0
495          *
496          * @param string $meta_name  Meta key name.
497          * @param mixed  $meta_value Meta value.
498          * @param array  $data       Associative array of source data.
499          * @return array Processed data array.
500          */
501         private function _process_meta_entry( $meta_name, $meta_value, $data ) {
502                 if ( preg_match( '/:?(title|description|keywords|site_name)$/', $meta_name ) ) {
503                         $data['_meta'][ $meta_name ] = $meta_value;
504                 } else {
505                         switch ( $meta_name ) {
506                                 case 'og:url':
507                                 case 'og:video':
508                                 case 'og:video:secure_url':
509                                         $meta_value = $this->_limit_embed( $meta_value );
510
511                                         if ( ! isset( $data['_embeds'] ) ) {
512                                                 $data['_embeds'] = array();
513                                         }
514
515                                         if ( ! empty( $meta_value ) && ! in_array( $meta_value, $data['_embeds'] ) ) {
516                                                 $data['_embeds'][] = $meta_value;
517                                         }
518
519                                         break;
520                                 case 'og:image':
521                                 case 'og:image:secure_url':
522                                 case 'twitter:image0:src':
523                                 case 'twitter:image0':
524                                 case 'twitter:image:src':
525                                 case 'twitter:image':
526                                         $meta_value = $this->_limit_img( $meta_value );
527
528                                         if ( ! isset( $data['_images'] ) ) {
529                                                 $data['_images'] = array();
530                                         }
531
532                                         if ( ! empty( $meta_value ) && ! in_array( $meta_value, $data['_images'] ) ) {
533                                                 $data['_images'][] = $meta_value;
534                                         }
535
536                                         break;
537                         }
538                 }
539
540                 return $data;
541         }
542
543         /**
544          * Fetches and parses _meta, _images, and _links data from the source.
545          *
546          * @since 4.2.0
547          * @access public
548          *
549          * @param string $url  URL to scan.
550          * @param array  $data Optional. Existing data array if you have one. Default empty array.
551          * @return array New data array.
552          */
553         public function source_data_fetch_fallback( $url, $data = array() ) {
554                 if ( empty( $url ) ) {
555                         return array();
556                 }
557
558                 // Download source page to tmp file.
559                 $source_content = $this->fetch_source_html( $url );
560                 if ( is_wp_error( $source_content ) ) {
561                         return array( 'errors' => $source_content->get_error_messages() );
562                 }
563
564                 // Fetch and gather <meta> data first, so discovered media is offered 1st to user.
565                 if ( empty( $data['_meta'] ) ) {
566                         $data['_meta'] = array();
567                 }
568
569                 if ( preg_match_all( '/<meta [^>]+>/', $source_content, $matches ) ) {
570                         $items = $this->_limit_array( $matches[0] );
571
572                         foreach ( $items as $value ) {
573                                 if ( preg_match( '/(property|name)="([^"]+)"[^>]+content="([^"]+)"/', $value, $new_matches ) ) {
574                                         $meta_name  = $this->_limit_string( $new_matches[2] );
575                                         $meta_value = $this->_limit_string( $new_matches[3] );
576
577                                         // Sanity check. $key is usually things like 'title', 'description', 'keywords', etc.
578                                         if ( strlen( $meta_name ) > 100 ) {
579                                                 continue;
580                                         }
581
582                                         $data = $this->_process_meta_entry( $meta_name, $meta_value, $data );
583                                 }
584                         }
585                 }
586
587                 // Fetch and gather <img> data.
588                 if ( empty( $data['_images'] ) ) {
589                         $data['_images'] = array();
590                 }
591
592                 if ( preg_match_all( '/<img [^>]+>/', $source_content, $matches ) ) {
593                         $items = $this->_limit_array( $matches[0] );
594
595                         foreach ( $items as $value ) {
596                                 if ( ( preg_match( '/width=(\'|")(\d+)\\1/i', $value, $new_matches ) && $new_matches[2] < 256 ) ||
597                                         ( preg_match( '/height=(\'|")(\d+)\\1/i', $value, $new_matches ) && $new_matches[2] < 128 ) ) {
598
599                                         continue;
600                                 }
601
602                                 if ( preg_match( '/src=(\'|")([^\'"]+)\\1/i', $value, $new_matches ) ) {
603                                         $src = $this->_limit_img( $new_matches[2] );
604                                         if ( ! empty( $src ) && ! in_array( $src, $data['_images'] ) ) {
605                                                 $data['_images'][] = $src;
606                                         }
607                                 }
608                         }
609                 }
610
611                 // Fetch and gather <iframe> data.
612                 if ( empty( $data['_embeds'] ) ) {
613                         $data['_embeds'] = array();
614                 }
615
616                 if ( preg_match_all( '/<iframe [^>]+>/', $source_content, $matches ) ) {
617                         $items = $this->_limit_array( $matches[0] );
618
619                         foreach ( $items as $value ) {
620                                 if ( preg_match( '/src=(\'|")([^\'"]+)\\1/', $value, $new_matches ) ) {
621                                         $src = $this->_limit_embed( $new_matches[2] );
622
623                                         if ( ! empty( $src ) && ! in_array( $src, $data['_embeds'] ) ) {
624                                                 $data['_embeds'][] = $src;
625                                         }
626                                 }
627                         }
628                 }
629
630                 // Fetch and gather <link> data.
631                 if ( empty( $data['_links'] ) ) {
632                         $data['_links'] = array();
633                 }
634
635                 if ( preg_match_all( '/<link [^>]+>/', $source_content, $matches ) ) {
636                         $items = $this->_limit_array( $matches[0] );
637
638                         foreach ( $items as $value ) {
639                                 if ( preg_match( '/rel=["\'](canonical|shortlink|icon)["\']/i', $value, $matches_rel ) && preg_match( '/href=[\'"]([^\'" ]+)[\'"]/i', $value, $matches_url ) ) {
640                                         $rel = $matches_rel[1];
641                                         $url = $this->_limit_url( $matches_url[1] );
642
643                                         if ( ! empty( $url ) && empty( $data['_links'][ $rel ] ) ) {
644                                                 $data['_links'][ $rel ] = $url;
645                                         }
646                                 }
647                         }
648                 }
649
650                 return $data;
651         }
652
653         /**
654          * Handles backward-compat with the legacy version of Press This by supporting its query string params.
655          *
656          * @since 4.2.0
657          * @access public
658          *
659          * @return array
660          */
661         public function merge_or_fetch_data() {
662                 // Get data from $_POST and $_GET, as appropriate ($_POST > $_GET), to remain backward compatible.
663                 $data = array();
664
665                 // Only instantiate the keys we want. Sanity check and sanitize each one.
666                 foreach ( array( 'u', 's', 't', 'v' ) as $key ) {
667                         if ( ! empty( $_POST[ $key ] ) ) {
668                                 $value = wp_unslash( $_POST[ $key ] );
669                         } else if ( ! empty( $_GET[ $key ] ) ) {
670                                 $value = wp_unslash( $_GET[ $key ] );
671                         } else {
672                                 continue;
673                         }
674
675                         if ( 'u' === $key ) {
676                                 $value = $this->_limit_url( $value );
677
678                                 if ( preg_match( '%^(?:https?:)?//[^/]+%i', $value, $domain_match ) ) {
679                                         $this->domain = $domain_match[0];
680                                 }
681                         } else {
682                                 $value = $this->_limit_string( $value );
683                         }
684
685                         if ( ! empty( $value ) ) {
686                                 $data[ $key ] = $value;
687                         }
688                 }
689
690                 /**
691                  * Filters whether to enable in-source media discovery in Press This.
692                  *
693                  * @since 4.2.0
694                  *
695                  * @param bool $enable Whether to enable media discovery.
696                  */
697                 if ( apply_filters( 'enable_press_this_media_discovery', true ) ) {
698                         /*
699                          * If no title, _images, _embed, and _meta was passed via $_POST, fetch data from source as fallback,
700                          * making PT fully backward compatible with the older bookmarklet.
701                          */
702                         if ( empty( $_POST ) && ! empty( $data['u'] ) ) {
703                                 $data = $this->source_data_fetch_fallback( $data['u'], $data );
704                         } else {
705                                 foreach ( array( '_images', '_embeds' ) as $type ) {
706                                         if ( empty( $_POST[ $type ] ) ) {
707                                                 continue;
708                                         }
709
710                                         $data[ $type ] = array();
711                                         $items = $this->_limit_array( $_POST[ $type ] );
712
713                                         foreach ( $items as $key => $value ) {
714                                                 if ( $type === '_images' ) {
715                                                         $value = $this->_limit_img( wp_unslash( $value ) );
716                                                 } else {
717                                                         $value = $this->_limit_embed( wp_unslash( $value ) );
718                                                 }
719
720                                                 if ( ! empty( $value ) ) {
721                                                         $data[ $type ][] = $value;
722                                                 }
723                                         }
724                                 }
725
726                                 foreach ( array( '_meta', '_links' ) as $type ) {
727                                         if ( empty( $_POST[ $type ] ) ) {
728                                                 continue;
729                                         }
730
731                                         $data[ $type ] = array();
732                                         $items = $this->_limit_array( $_POST[ $type ] );
733
734                                         foreach ( $items as $key => $value ) {
735                                                 // Sanity check. These are associative arrays, $key is usually things like 'title', 'description', 'keywords', etc.
736                                                 if ( empty( $key ) || strlen( $key ) > 100 ) {
737                                                         continue;
738                                                 }
739
740                                                 if ( $type === '_meta' ) {
741                                                         $value = $this->_limit_string( wp_unslash( $value ) );
742
743                                                         if ( ! empty( $value ) ) {
744                                                                 $data = $this->_process_meta_entry( $key, $value, $data );
745                                                         }
746                                                 } else {
747                                                         if ( in_array( $key, array( 'canonical', 'shortlink', 'icon' ), true ) ) {
748                                                                 $data[ $type ][ $key ] = $this->_limit_url( wp_unslash( $value ) );
749                                                         }
750                                                 }
751                                         }
752                                 }
753                         }
754
755                         // Support passing a single image src as `i`
756                         if ( ! empty( $_REQUEST['i'] ) && ( $img_src = $this->_limit_img( wp_unslash( $_REQUEST['i'] ) ) ) ) {
757                                 if ( empty( $data['_images'] ) ) {
758                                         $data['_images'] = array( $img_src );
759                                 } elseif ( ! in_array( $img_src, $data['_images'], true ) ) {
760                                         array_unshift( $data['_images'], $img_src );
761                                 }
762                         }
763                 }
764
765                 /**
766                  * Filters the Press This data array.
767                  *
768                  * @since 4.2.0
769                  *
770                  * @param array $data Press This Data array.
771                  */
772                 return apply_filters( 'press_this_data', $data );
773         }
774
775         /**
776          * Adds another stylesheet inside TinyMCE.
777          *
778          * @since 4.2.0
779          * @access public
780          *
781          * @param string $styles URL to editor stylesheet.
782          * @return string Possibly modified stylesheets list.
783          */
784         public function add_editor_style( $styles ) {
785                 if ( ! empty( $styles ) ) {
786                         $styles .= ',';
787                 }
788
789                 $press_this = admin_url( 'css/press-this-editor.css' );
790                 if ( is_rtl() ) {
791                         $press_this = str_replace( '.css', '-rtl.css', $press_this );
792                 }
793
794                 return $styles . $press_this;
795         }
796
797         /**
798          * Outputs the post format selection HTML.
799          *
800          * @since 4.2.0
801          * @access public
802          *
803          * @param WP_Post $post Post object.
804          */
805         public function post_formats_html( $post ) {
806                 if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) {
807                         $post_formats = get_theme_support( 'post-formats' );
808
809                         if ( is_array( $post_formats[0] ) ) {
810                                 $post_format = get_post_format( $post->ID );
811
812                                 if ( ! $post_format ) {
813                                         $post_format = '0';
814                                 }
815
816                                 // Add in the current one if it isn't there yet, in case the current theme doesn't support it.
817                                 if ( $post_format && ! in_array( $post_format, $post_formats[0] ) ) {
818                                         $post_formats[0][] = $post_format;
819                                 }
820
821                                 ?>
822                                 <div id="post-formats-select">
823                                 <fieldset><legend class="screen-reader-text"><?php _e( 'Post Formats' ); ?></legend>
824                                         <input type="radio" name="post_format" class="post-format" id="post-format-0" value="0" <?php checked( $post_format, '0' ); ?> />
825                                         <label for="post-format-0" class="post-format-icon post-format-standard"><?php echo get_post_format_string( 'standard' ); ?></label>
826                                         <?php
827
828                                         foreach ( $post_formats[0] as $format ) {
829                                                 $attr_format = esc_attr( $format );
830                                                 ?>
831                                                 <br />
832                                                 <input type="radio" name="post_format" class="post-format" id="post-format-<?php echo $attr_format; ?>" value="<?php echo $attr_format; ?>" <?php checked( $post_format, $format ); ?> />
833                                                 <label for="post-format-<?php echo $attr_format ?>" class="post-format-icon post-format-<?php echo $attr_format; ?>"><?php echo esc_html( get_post_format_string( $format ) ); ?></label>
834                                                 <?php
835                                          }
836
837                                          ?>
838                                 </fieldset>
839                                 </div>
840                                 <?php
841                         }
842                 }
843         }
844
845         /**
846          * Outputs the categories HTML.
847          *
848          * @since 4.2.0
849          * @access public
850          *
851          * @param WP_Post $post Post object.
852          */
853         public function categories_html( $post ) {
854                 $taxonomy = get_taxonomy( 'category' );
855
856                 if ( current_user_can( $taxonomy->cap->edit_terms ) ) {
857                         ?>
858                         <button type="button" class="add-cat-toggle button-link" aria-expanded="false">
859                                 <span class="dashicons dashicons-plus"></span><span class="screen-reader-text"><?php _e( 'Toggle add category' ); ?></span>
860                         </button>
861                         <div class="add-category is-hidden">
862                                 <label class="screen-reader-text" for="new-category"><?php echo $taxonomy->labels->add_new_item; ?></label>
863                                 <input type="text" id="new-category" class="add-category-name" placeholder="<?php echo esc_attr( $taxonomy->labels->new_item_name ); ?>" value="" aria-required="true">
864                                 <label class="screen-reader-text" for="new-category-parent"><?php echo $taxonomy->labels->parent_item_colon; ?></label>
865                                 <div class="postform-wrapper">
866                                         <?php
867                                         wp_dropdown_categories( array(
868                                                 'taxonomy'         => 'category',
869                                                 'hide_empty'       => 0,
870                                                 'name'             => 'new-category-parent',
871                                                 'orderby'          => 'name',
872                                                 'hierarchical'     => 1,
873                                                 'show_option_none' => '&mdash; ' . $taxonomy->labels->parent_item . ' &mdash;'
874                                         ) );
875                                         ?>
876                                 </div>
877                                 <button type="button" class="add-cat-submit"><?php _e( 'Add' ); ?></button>
878                         </div>
879                         <?php
880
881                 }
882                 ?>
883                 <div class="categories-search-wrapper">
884                         <input id="categories-search" type="search" class="categories-search" placeholder="<?php esc_attr_e( 'Search categories by name' ) ?>">
885                         <label for="categories-search">
886                                 <span class="dashicons dashicons-search"></span><span class="screen-reader-text"><?php _e( 'Search categories' ); ?></span>
887                         </label>
888                 </div>
889                 <div aria-label="<?php esc_attr_e( 'Categories' ); ?>">
890                         <ul class="categories-select">
891                                 <?php wp_terms_checklist( $post->ID, array( 'taxonomy' => 'category', 'list_only' => true ) ); ?>
892                         </ul>
893                 </div>
894                 <?php
895         }
896
897         /**
898          * Outputs the tags HTML.
899          *
900          * @since 4.2.0
901          * @access public
902          *
903          * @param WP_Post $post Post object.
904          */
905         public function tags_html( $post ) {
906                 $taxonomy              = get_taxonomy( 'post_tag' );
907                 $user_can_assign_terms = current_user_can( $taxonomy->cap->assign_terms );
908                 $esc_tags              = get_terms_to_edit( $post->ID, 'post_tag' );
909
910                 if ( ! $esc_tags || is_wp_error( $esc_tags ) ) {
911                         $esc_tags = '';
912                 }
913
914                 ?>
915                 <div class="tagsdiv" id="post_tag">
916                         <div class="jaxtag">
917                         <input type="hidden" name="tax_input[post_tag]" class="the-tags" value="<?php echo $esc_tags; // escaped in get_terms_to_edit() ?>">
918                         <?php
919
920                         if ( $user_can_assign_terms ) {
921                                 ?>
922                                 <div class="ajaxtag hide-if-no-js">
923                                         <label class="screen-reader-text" for="new-tag-post_tag"><?php _e( 'Tags' ); ?></label>
924                                         <p>
925                                                 <input type="text" id="new-tag-post_tag" name="newtag[post_tag]" class="newtag form-input-tip" size="16" autocomplete="off" value="" aria-describedby="new-tag-desc" />
926                                                 <button type="button" class="tagadd"><?php _e( 'Add' ); ?></button>
927                                         </p>
928                                 </div>
929                                 <p class="howto" id="new-tag-desc">
930                                         <?php echo $taxonomy->labels->separate_items_with_commas; ?>
931                                 </p>
932                                 <?php
933                         }
934
935                         ?>
936                         </div>
937                         <div class="tagchecklist"></div>
938                 </div>
939                 <?php
940
941                 if ( $user_can_assign_terms ) {
942                         ?>
943                         <button type="button" class="button-link tagcloud-link" id="link-post_tag" aria-expanded="false"><?php echo $taxonomy->labels->choose_from_most_used; ?></button>
944                         <?php
945                 }
946         }
947
948         /**
949          * Get a list of embeds with no duplicates.
950          *
951          * @since 4.2.0
952          * @access public
953          *
954          * @param array $data The site's data.
955          * @return array Embeds selected to be available.
956          */
957         public function get_embeds( $data ) {
958                 $selected_embeds = array();
959
960                 // Make sure to add the Pressed page if it's a valid oembed itself
961                 if ( ! empty ( $data['u'] ) && $this->_limit_embed( $data['u'] ) ) {
962                         $data['_embeds'][] = $data['u'];
963                 }
964
965                 if ( ! empty( $data['_embeds'] ) ) {
966                         foreach ( $data['_embeds'] as $src ) {
967                                 $prot_relative_src = preg_replace( '/^https?:/', '', $src );
968
969                                 if ( in_array( $prot_relative_src, $this->embeds ) ) {
970                                         continue;
971                                 }
972
973                                 $selected_embeds[] = $src;
974                                 $this->embeds[] = $prot_relative_src;
975                         }
976                 }
977
978                 return $selected_embeds;
979         }
980
981         /**
982          * Get a list of images with no duplicates.
983          *
984          * @since 4.2.0
985          * @access public
986          *
987          * @param array $data The site's data.
988          * @return array
989          */
990         public function get_images( $data ) {
991                 $selected_images = array();
992
993                 if ( ! empty( $data['_images'] ) ) {
994                         foreach ( $data['_images'] as $src ) {
995                                 if ( false !== strpos( $src, 'gravatar.com' ) ) {
996                                         $src = preg_replace( '%http://[\d]+\.gravatar\.com/%', 'https://secure.gravatar.com/', $src );
997                                 }
998
999                                 $prot_relative_src = preg_replace( '/^https?:/', '', $src );
1000
1001                                 if ( in_array( $prot_relative_src, $this->images ) ||
1002                                         ( false !== strpos( $src, 'avatar' ) && count( $this->images ) > 15 ) ) {
1003                                         // Skip: already selected or some type of avatar and we've already gathered more than 15 images.
1004                                         continue;
1005                                 }
1006
1007                                 $selected_images[] = $src;
1008                                 $this->images[] = $prot_relative_src;
1009                         }
1010                 }
1011
1012                 return $selected_images;
1013         }
1014
1015         /**
1016          * Gets the source page's canonical link, based on passed location and meta data.
1017          *
1018          * @since 4.2.0
1019          * @access public
1020          *
1021          * @param array $data The site's data.
1022          * @return string Discovered canonical URL, or empty
1023          */
1024         public function get_canonical_link( $data ) {
1025                 $link = '';
1026
1027                 if ( ! empty( $data['_links']['canonical'] ) ) {
1028                         $link = $data['_links']['canonical'];
1029                 } elseif ( ! empty( $data['u'] ) ) {
1030                         $link = $data['u'];
1031                 } elseif ( ! empty( $data['_meta'] ) ) {
1032                         if ( ! empty( $data['_meta']['twitter:url'] ) ) {
1033                                 $link = $data['_meta']['twitter:url'];
1034                         } else if ( ! empty( $data['_meta']['og:url'] ) ) {
1035                                 $link = $data['_meta']['og:url'];
1036                         }
1037                 }
1038
1039                 if ( empty( $link ) && ! empty( $data['_links']['shortlink'] ) ) {
1040                         $link = $data['_links']['shortlink'];
1041                 }
1042
1043                 return $link;
1044         }
1045
1046         /**
1047          * Gets the source page's site name, based on passed meta data.
1048          *
1049          * @since 4.2.0
1050          * @access public
1051          *
1052          * @param array $data The site's data.
1053          * @return string Discovered site name, or empty
1054          */
1055         public function get_source_site_name( $data ) {
1056                 $name = '';
1057
1058                 if ( ! empty( $data['_meta'] ) ) {
1059                         if ( ! empty( $data['_meta']['og:site_name'] ) ) {
1060                                 $name = $data['_meta']['og:site_name'];
1061                         } else if ( ! empty( $data['_meta']['application-name'] ) ) {
1062                                 $name = $data['_meta']['application-name'];
1063                         }
1064                 }
1065
1066                 return $name;
1067         }
1068
1069         /**
1070          * Gets the source page's title, based on passed title and meta data.
1071          *
1072          * @since 4.2.0
1073          * @access public
1074          *
1075          * @param array $data The site's data.
1076          * @return string Discovered page title, or empty
1077          */
1078         public function get_suggested_title( $data ) {
1079                 $title = '';
1080
1081                 if ( ! empty( $data['t'] ) ) {
1082                         $title = $data['t'];
1083                 } elseif ( ! empty( $data['_meta'] ) ) {
1084                         if ( ! empty( $data['_meta']['twitter:title'] ) ) {
1085                                 $title = $data['_meta']['twitter:title'];
1086                         } else if ( ! empty( $data['_meta']['og:title'] ) ) {
1087                                 $title = $data['_meta']['og:title'];
1088                         } else if ( ! empty( $data['_meta']['title'] ) ) {
1089                                 $title = $data['_meta']['title'];
1090                         }
1091                 }
1092
1093                 return $title;
1094         }
1095
1096         /**
1097          * Gets the source page's suggested content, based on passed data (description, selection, etc).
1098          *
1099          * Features a blockquoted excerpt, as well as content attribution, if any.
1100          *
1101          * @since 4.2.0
1102          * @access public
1103          *
1104          * @param array $data The site's data.
1105          * @return string Discovered content, or empty
1106          */
1107         public function get_suggested_content( $data ) {
1108                 $content = $text = '';
1109
1110                 if ( ! empty( $data['s'] ) ) {
1111                         $text = $data['s'];
1112                 } else if ( ! empty( $data['_meta'] ) ) {
1113                         if ( ! empty( $data['_meta']['twitter:description'] ) ) {
1114                                 $text = $data['_meta']['twitter:description'];
1115                         } else if ( ! empty( $data['_meta']['og:description'] ) ) {
1116                                 $text = $data['_meta']['og:description'];
1117                         } else if ( ! empty( $data['_meta']['description'] ) ) {
1118                                 $text = $data['_meta']['description'];
1119                         }
1120
1121                         // If there is an ellipsis at the end, the description is very likely auto-generated. Better to ignore it.
1122                         if ( $text && substr( $text, -3 ) === '...' ) {
1123                                 $text = '';
1124                         }
1125                 }
1126
1127                 $default_html = array( 'quote' => '', 'link' => '', 'embed' => '' );
1128
1129                 if ( ! empty( $data['u'] ) && $this->_limit_embed( $data['u'] ) ) {
1130                         $default_html['embed'] = '<p>[embed]' . $data['u'] . '[/embed]</p>';
1131
1132                         if ( ! empty( $data['s'] ) ) {
1133                                 // If the user has selected some text, do quote it.
1134                                 $default_html['quote'] = '<blockquote>%1$s</blockquote>';
1135                         }
1136                 } else {
1137                         $default_html['quote'] = '<blockquote>%1$s</blockquote>';
1138                         $default_html['link'] = '<p>' . _x( 'Source:', 'Used in Press This to indicate where the content comes from.' ) .
1139                                 ' <em><a href="%1$s">%2$s</a></em></p>';
1140                 }
1141
1142                 /**
1143                  * Filters the default HTML tags used in the suggested content for the editor.
1144                  *
1145                  * The HTML strings use printf format. After filtering the content is added at the specified places with `sprintf()`.
1146                  *
1147                  * @since 4.2.0
1148                  *
1149                  * @param array $default_html Associative array with three possible keys:
1150                  *                                - 'quote' where %1$s is replaced with the site description or the selected content.
1151                  *                                - 'link' where %1$s is link href, %2$s is link text, usually the source page title.
1152                  *                                - 'embed' which contains an [embed] shortcode when the source page offers embeddable content.
1153                  * @param array $data         Associative array containing the data from the source page.
1154                  */
1155                 $default_html = apply_filters( 'press_this_suggested_html', $default_html, $data );
1156
1157                 if ( ! empty( $default_html['embed'] ) ) {
1158                         $content .= $default_html['embed'];
1159                 }
1160
1161                 // Wrap suggested content in the specified HTML.
1162                 if ( ! empty( $default_html['quote'] ) && $text ) {
1163                         $content .= sprintf( $default_html['quote'], $text );
1164                 }
1165
1166                 // Add source attribution if there is one available.
1167                 if ( ! empty( $default_html['link'] ) ) {
1168                         $title = $this->get_suggested_title( $data );
1169                         $url = $this->get_canonical_link( $data );
1170
1171                         if ( ! $title ) {
1172                                 $title = $this->get_source_site_name( $data );
1173                         }
1174
1175                         if ( $url && $title ) {
1176                                 $content .= sprintf( $default_html['link'], $url, $title );
1177                         }
1178                 }
1179
1180                 return $content;
1181         }
1182
1183         /**
1184          * Serves the app's base HTML, which in turns calls the load script.
1185          *
1186          * @since 4.2.0
1187          * @access public
1188          *
1189          * @global WP_Locale $wp_locale
1190          * @global bool      $is_IE
1191          */
1192         public function html() {
1193                 global $wp_locale;
1194
1195                 $wp_version = get_bloginfo( 'version' );
1196
1197                 // Get data, new (POST) and old (GET).
1198                 $data = $this->merge_or_fetch_data();
1199
1200                 $post_title = $this->get_suggested_title( $data );
1201
1202                 $post_content = $this->get_suggested_content( $data );
1203
1204                 // Get site settings array/data.
1205                 $site_settings = $this->site_settings();
1206
1207                 // Pass the images and embeds
1208                 $images = $this->get_images( $data );
1209                 $embeds = $this->get_embeds( $data );
1210
1211                 $site_data = array(
1212                         'v' => ! empty( $data['v'] ) ? $data['v'] : '',
1213                         'u' => ! empty( $data['u'] ) ? $data['u'] : '',
1214                         'hasData' => ! empty( $data ),
1215                 );
1216
1217                 if ( ! empty( $images ) ) {
1218                         $site_data['_images'] = $images;
1219                 }
1220
1221                 if ( ! empty( $embeds ) ) {
1222                         $site_data['_embeds'] = $embeds;
1223                 }
1224
1225                 // Add press-this-editor.css and remove theme's editor-style.css, if any.
1226                 remove_editor_styles();
1227
1228                 add_filter( 'mce_css', array( $this, 'add_editor_style' ) );
1229
1230                 if ( ! empty( $GLOBALS['is_IE'] ) ) {
1231                         @header( 'X-UA-Compatible: IE=edge' );
1232                 }
1233
1234                 @header( 'Content-Type: ' . get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ) );
1235
1236 ?>
1237 <!DOCTYPE html>
1238 <!--[if IE 7]>         <html class="lt-ie9 lt-ie8" <?php language_attributes(); ?>> <![endif]-->
1239 <!--[if IE 8]>         <html class="lt-ie9" <?php language_attributes(); ?>> <![endif]-->
1240 <!--[if gt IE 8]><!--> <html <?php language_attributes(); ?>> <!--<![endif]-->
1241 <head>
1242         <meta http-equiv="Content-Type" content="<?php echo esc_attr( get_bloginfo( 'html_type' ) ); ?>; charset=<?php echo esc_attr( get_option( 'blog_charset' ) ); ?>" />
1243         <meta name="viewport" content="width=device-width">
1244         <title><?php esc_html_e( 'Press This!' ) ?></title>
1245
1246         <script>
1247                 window.wpPressThisData   = <?php echo wp_json_encode( $site_data ); ?>;
1248                 window.wpPressThisConfig = <?php echo wp_json_encode( $site_settings ); ?>;
1249         </script>
1250
1251         <script type="text/javascript">
1252                 var ajaxurl = '<?php echo esc_js( admin_url( 'admin-ajax.php', 'relative' ) ); ?>',
1253                         pagenow = 'press-this',
1254                         typenow = 'post',
1255                         adminpage = 'press-this-php',
1256                         thousandsSeparator = '<?php echo addslashes( $wp_locale->number_format['thousands_sep'] ); ?>',
1257                         decimalPoint = '<?php echo addslashes( $wp_locale->number_format['decimal_point'] ); ?>',
1258                         isRtl = <?php echo (int) is_rtl(); ?>;
1259         </script>
1260
1261         <?php
1262                 /*
1263                  * $post->ID is needed for the embed shortcode so we can show oEmbed previews in the editor.
1264                  * Maybe find a way without it.
1265                  */
1266                 $post = get_default_post_to_edit( 'post', true );
1267                 $post_ID = (int) $post->ID;
1268
1269                 wp_enqueue_media( array( 'post' => $post_ID ) );
1270                 wp_enqueue_style( 'press-this' );
1271                 wp_enqueue_script( 'press-this' );
1272                 wp_enqueue_script( 'json2' );
1273                 wp_enqueue_script( 'editor' );
1274
1275                 $supports_formats = false;
1276                 $post_format      = 0;
1277
1278                 if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) {
1279                         $supports_formats = true;
1280
1281                         if ( ! ( $post_format = get_post_format( $post_ID ) ) ) {
1282                                 $post_format = 0;
1283                         }
1284                 }
1285
1286                 /** This action is documented in wp-admin/admin-header.php */
1287                 do_action( 'admin_enqueue_scripts', 'press-this.php' );
1288
1289                 /** This action is documented in wp-admin/admin-header.php */
1290                 do_action( 'admin_print_styles-press-this.php' );
1291
1292                 /** This action is documented in wp-admin/admin-header.php */
1293                 do_action( 'admin_print_styles' );
1294
1295                 /** This action is documented in wp-admin/admin-header.php */
1296                 do_action( 'admin_print_scripts-press-this.php' );
1297
1298                 /** This action is documented in wp-admin/admin-header.php */
1299                 do_action( 'admin_print_scripts' );
1300
1301                 /** This action is documented in wp-admin/admin-header.php */
1302                 do_action( 'admin_head-press-this.php' );
1303
1304                 /** This action is documented in wp-admin/admin-header.php */
1305                 do_action( 'admin_head' );
1306         ?>
1307 </head>
1308 <?php
1309
1310         $admin_body_class  = 'press-this';
1311         $admin_body_class .= ( is_rtl() ) ? ' rtl' : '';
1312         $admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', floatval( $wp_version ) );
1313         $admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '/^([.0-9]+).*/', '$1', $wp_version ) );
1314         $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
1315         $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_user_locale() ) ) );
1316
1317         /** This filter is documented in wp-admin/admin-header.php */
1318         $admin_body_classes = apply_filters( 'admin_body_class', '' );
1319
1320 ?>
1321 <body class="wp-admin wp-core-ui <?php echo $admin_body_classes . ' ' . $admin_body_class; ?>">
1322         <div id="adminbar" class="adminbar">
1323                 <h1 id="current-site" class="current-site">
1324                         <a class="current-site-link" href="<?php echo esc_url( home_url( '/' ) ); ?>" target="_blank" rel="home">
1325                                 <span class="dashicons dashicons-wordpress"></span>
1326                                 <span class="current-site-name"><?php bloginfo( 'name' ); ?></span>
1327                         </a>
1328                 </h1>
1329                 <button type="button" class="options button-link closed">
1330                         <span class="dashicons dashicons-tag on-closed"></span>
1331                         <span class="screen-reader-text on-closed"><?php _e( 'Show post options' ); ?></span>
1332                         <span aria-hidden="true" class="on-open"><?php _e( 'Done' ); ?></span>
1333                         <span class="screen-reader-text on-open"><?php _e( 'Hide post options' ); ?></span>
1334                 </button>
1335         </div>
1336
1337         <div id="scanbar" class="scan">
1338                 <form method="GET">
1339                         <label for="url-scan" class="screen-reader-text"><?php _e( 'Scan site for content' ); ?></label>
1340                         <input type="url" name="u" id="url-scan" class="scan-url" value="" placeholder="<?php esc_attr_e( 'Enter a URL to scan' ) ?>" />
1341                         <input type="submit" name="url-scan-submit" id="url-scan-submit" class="scan-submit" value="<?php esc_attr_e( 'Scan' ) ?>" />
1342                 </form>
1343         </div>
1344
1345         <form id="pressthis-form" method="post" action="post.php" autocomplete="off">
1346                 <input type="hidden" name="post_ID" id="post_ID" value="<?php echo $post_ID; ?>" />
1347                 <input type="hidden" name="action" value="press-this-save-post" />
1348                 <input type="hidden" name="post_status" id="post_status" value="draft" />
1349                 <input type="hidden" name="wp-preview" id="wp-preview" value="" />
1350                 <input type="hidden" name="post_title" id="post_title" value="" />
1351                 <input type="hidden" name="pt-force-redirect" id="pt-force-redirect" value="" />
1352                 <?php
1353
1354                 wp_nonce_field( 'update-post_' . $post_ID, '_wpnonce', false );
1355                 wp_nonce_field( 'add-category', '_ajax_nonce-add-category', false );
1356
1357                 ?>
1358
1359         <div class="wrapper">
1360                 <div class="editor-wrapper">
1361                         <div class="alerts" role="alert" aria-live="assertive" aria-relevant="all" aria-atomic="true">
1362                                 <?php
1363
1364                                 if ( isset( $data['v'] ) && $this->version > $data['v'] ) {
1365                                         ?>
1366                                         <p class="alert is-notice">
1367                                                 <?php printf( __( 'You should upgrade <a href="%s" target="_blank">your bookmarklet</a> to the latest version!' ), admin_url( 'tools.php' ) ); ?>
1368                                         </p>
1369                                         <?php
1370                                 }
1371
1372                                 ?>
1373                         </div>
1374
1375                         <div id="app-container" class="editor">
1376                                 <span id="title-container-label" class="post-title-placeholder" aria-hidden="true"><?php _e( 'Post title' ); ?></span>
1377                                 <h2 id="title-container" class="post-title" contenteditable="true" spellcheck="true" aria-label="<?php esc_attr_e( 'Post title' ); ?>" tabindex="0"><?php echo esc_html( $post_title ); ?></h2>
1378
1379                                 <div class="media-list-container">
1380                                         <div class="media-list-inner-container">
1381                                                 <h2 class="screen-reader-text"><?php _e( 'Suggested media' ); ?></h2>
1382                                                 <ul class="media-list"></ul>
1383                                         </div>
1384                                 </div>
1385
1386                                 <?php
1387                                 wp_editor( $post_content, 'pressthis', array(
1388                                         'drag_drop_upload' => true,
1389                                         'editor_height'    => 600,
1390                                         'media_buttons'    => false,
1391                                         'textarea_name'    => 'post_content',
1392                                         'teeny'            => true,
1393                                         'tinymce'          => array(
1394                                                 'resize'                => false,
1395                                                 'wordpress_adv_hidden'  => false,
1396                                                 'add_unload_trigger'    => false,
1397                                                 'statusbar'             => false,
1398                                                 'autoresize_min_height' => 600,
1399                                                 'wp_autoresize_on'      => true,
1400                                                 'plugins'               => 'lists,media,paste,tabfocus,fullscreen,wordpress,wpautoresize,wpeditimage,wpgallery,wplink,wptextpattern,wpview',
1401                                                 'toolbar1'              => 'bold,italic,bullist,numlist,blockquote,link,unlink',
1402                                                 'toolbar2'              => 'undo,redo',
1403                                         ),
1404                                         'quicktags' => array(
1405                                                 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,more',
1406                                         ),
1407                                 ) );
1408
1409                                 ?>
1410                         </div>
1411                 </div>
1412
1413                 <div class="options-panel-back is-hidden" tabindex="-1"></div>
1414                 <div class="options-panel is-off-screen is-hidden" tabindex="-1">
1415                         <div class="post-options">
1416
1417                                 <?php if ( $supports_formats ) : ?>
1418                                         <button type="button" class="button-link post-option">
1419                                                 <span class="dashicons dashicons-admin-post"></span>
1420                                                 <span class="post-option-title"><?php _ex( 'Format', 'post format' ); ?></span>
1421                                                 <span class="post-option-contents" id="post-option-post-format"><?php echo esc_html( get_post_format_string( $post_format ) ); ?></span>
1422                                                 <span class="dashicons post-option-forward"></span>
1423                                         </button>
1424                                 <?php endif; ?>
1425
1426                                 <button type="button" class="button-link post-option">
1427                                         <span class="dashicons dashicons-category"></span>
1428                                         <span class="post-option-title"><?php _e( 'Categories' ); ?></span>
1429                                         <span class="dashicons post-option-forward"></span>
1430                                 </button>
1431
1432                                 <button type="button" class="button-link post-option">
1433                                         <span class="dashicons dashicons-tag"></span>
1434                                         <span class="post-option-title"><?php _e( 'Tags' ); ?></span>
1435                                         <span class="dashicons post-option-forward"></span>
1436                                 </button>
1437                         </div>
1438
1439                         <?php if ( $supports_formats ) : ?>
1440                                 <div class="setting-modal is-off-screen is-hidden">
1441                                         <button type="button" class="button-link modal-close">
1442                                                 <span class="dashicons post-option-back"></span>
1443                                                 <span class="setting-title" aria-hidden="true"><?php _ex( 'Format', 'post format' ); ?></span>
1444                                                 <span class="screen-reader-text"><?php _e( 'Back to post options' ) ?></span>
1445                                         </button>
1446                                         <?php $this->post_formats_html( $post ); ?>
1447                                 </div>
1448                         <?php endif; ?>
1449
1450                         <div class="setting-modal is-off-screen is-hidden">
1451                                 <button type="button" class="button-link modal-close">
1452                                         <span class="dashicons post-option-back"></span>
1453                                         <span class="setting-title" aria-hidden="true"><?php _e( 'Categories' ); ?></span>
1454                                         <span class="screen-reader-text"><?php _e( 'Back to post options' ) ?></span>
1455                                 </button>
1456                                 <?php $this->categories_html( $post ); ?>
1457                         </div>
1458
1459                         <div class="setting-modal tags is-off-screen is-hidden">
1460                                 <button type="button" class="button-link modal-close">
1461                                         <span class="dashicons post-option-back"></span>
1462                                         <span class="setting-title" aria-hidden="true"><?php _e( 'Tags' ); ?></span>
1463                                         <span class="screen-reader-text"><?php _e( 'Back to post options' ) ?></span>
1464                                 </button>
1465                                 <?php $this->tags_html( $post ); ?>
1466                         </div>
1467                 </div><!-- .options-panel -->
1468         </div><!-- .wrapper -->
1469
1470         <div class="press-this-actions">
1471                 <div class="pressthis-media-buttons">
1472                         <button type="button" class="insert-media button-link" data-editor="pressthis">
1473                                 <span class="dashicons dashicons-admin-media"></span>
1474                                 <span class="screen-reader-text"><?php _e( 'Add Media' ); ?></span>
1475                         </button>
1476                 </div>
1477                 <div class="post-actions">
1478                         <span class="spinner">&nbsp;</span>
1479                         <div class="split-button">
1480                                 <div class="split-button-head">
1481                                         <button type="button" class="publish-button split-button-primary" aria-live="polite">
1482                                                 <span class="publish"><?php echo ( current_user_can( 'publish_posts' ) ) ? __( 'Publish' ) : __( 'Submit for Review' ); ?></span>
1483                                                 <span class="saving-draft"><?php _e( 'Saving&hellip;' ); ?></span>
1484                                         </button><button type="button" class="split-button-toggle" aria-haspopup="true" aria-expanded="false">
1485                                                 <i class="dashicons dashicons-arrow-down-alt2"></i>
1486                                                 <span class="screen-reader-text"><?php _e('More actions'); ?></span>
1487                                         </button>
1488                                 </div>
1489                                 <ul class="split-button-body">
1490                                         <li><button type="button" class="button-link draft-button split-button-option"><?php _e( 'Save Draft' ); ?></button></li>
1491                                         <li><button type="button" class="button-link standard-editor-button split-button-option"><?php _e( 'Standard Editor' ); ?></button></li>
1492                                         <li><button type="button" class="button-link preview-button split-button-option"><?php _e( 'Preview' ); ?></button></li>
1493                                 </ul>
1494                         </div>
1495                 </div>
1496         </div>
1497         </form>
1498
1499         <?php
1500         /** This action is documented in wp-admin/admin-footer.php */
1501         do_action( 'admin_footer' );
1502
1503         /** This action is documented in wp-admin/admin-footer.php */
1504         do_action( 'admin_print_footer_scripts-press-this.php' );
1505
1506         /** This action is documented in wp-admin/admin-footer.php */
1507         do_action( 'admin_print_footer_scripts' );
1508
1509         /** This action is documented in wp-admin/admin-footer.php */
1510         do_action( 'admin_footer-press-this.php' );
1511         ?>
1512 </body>
1513 </html>
1514 <?php
1515                 die();
1516         }
1517 }