3 * Press This class and display functionality
6 * @subpackage Press_This
17 // Used to trigger the bookmarklet update notice.
20 private $images = array();
22 private $embeds = array();
32 public function __construct() {}
35 * App and site settings data, including i18n strings for the client-side.
40 * @return array Site settings.
42 public function site_settings() {
45 * Filter whether or not Press This should redirect the user in the parent window upon save.
49 * @param bool false Whether to redirect in parent window or not. Default false.
51 'redirInParent' => apply_filters( 'press_this_redirect_in_parent', false ),
56 * Get the source's images and save them locally, for posterity, unless we can't.
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.
65 public function side_load_images( $post_id, $content = '' ) {
66 $content = wp_unslash( $content );
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 ) ) {
75 $image_src = $url_matches[1];
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 ) ) {
82 // Sideload image, which gives us a new image src.
83 $new_src = media_sideload_image( $image_src, $post_id, null, 'src' );
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 );
95 return wp_slash( $content );
99 * AJAX handler for saving the post as draft or published.
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.' ) ) );
109 if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-post_' . $post_id ) ||
110 ! current_user_can( 'edit_post', $post_id ) ) {
112 wp_send_json_error( array( 'errorMessage' => __( 'Invalid post.' ) ) );
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(),
126 if ( ! empty( $_POST['post_status'] ) && 'publish' === $_POST['post_status'] ) {
127 if ( current_user_can( 'publish_posts' ) ) {
128 $post['post_status'] = 'publish';
130 $post['post_status'] = 'pending';
134 $post['post_content'] = $this->side_load_images( $post_id, $post['post_content'] );
136 $updated = wp_update_post( $post, true );
138 if ( is_wp_error( $updated ) ) {
139 wp_send_json_error( array( 'errorMessage' => $updated->get_error_message() ) );
141 if ( isset( $post['post_format'] ) ) {
142 if ( current_theme_supports( 'post-formats', $post['post_format'] ) ) {
143 set_post_format( $post_id, $post['post_format'] );
144 } elseif ( $post['post_format'] ) {
145 set_post_format( $post_id, false );
149 $forceRedirect = false;
151 if ( 'publish' === get_post_status( $post_id ) ) {
152 $redirect = get_post_permalink( $post_id );
153 } elseif ( isset( $_POST['pt-force-redirect'] ) && $_POST['pt-force-redirect'] === 'true' ) {
154 $forceRedirect = true;
155 $redirect = get_edit_post_link( $post_id, 'js' );
161 * Filter the URL to redirect to when Press This saves.
165 * @param string $url Redirect URL. If `$status` is 'publish', this will be the post permalink.
166 * Otherwise, the default is false resulting in no redirect.
167 * @param int $post_id Post ID.
168 * @param string $status Post status.
170 $redirect = apply_filters( 'press_this_save_redirect', $redirect, $post_id, $post['post_status'] );
173 wp_send_json_success( array( 'redirect' => $redirect, 'force' => $forceRedirect ) );
175 wp_send_json_success( array( 'postSaved' => true ) );
181 * AJAX handler for adding a new category.
186 public function add_category() {
187 if ( false === wp_verify_nonce( $_POST['new_cat_nonce'], 'add-category' ) ) {
188 wp_send_json_error();
191 $taxonomy = get_taxonomy( 'category' );
193 if ( ! current_user_can( $taxonomy->cap->edit_terms ) || empty( $_POST['name'] ) ) {
194 wp_send_json_error();
197 $parent = isset( $_POST['parent'] ) && (int) $_POST['parent'] > 0 ? (int) $_POST['parent'] : 0;
198 $names = explode( ',', $_POST['name'] );
199 $added = $data = array();
201 foreach ( $names as $cat_name ) {
202 $cat_name = trim( $cat_name );
203 $cat_nicename = sanitize_title( $cat_name );
205 if ( empty( $cat_nicename ) ) {
209 // @todo Find a more performant way to check existence, maybe get_term() with a separate parent check.
210 if ( term_exists( $cat_name, $taxonomy->name, $parent ) ) {
211 if ( count( $names ) === 1 ) {
212 wp_send_json_error( array( 'errorMessage' => __( 'This category already exists.' ) ) );
218 $cat_id = wp_insert_term( $cat_name, $taxonomy->name, array( 'parent' => $parent ) );
220 if ( is_wp_error( $cat_id ) ) {
222 } elseif ( is_array( $cat_id ) ) {
223 $cat_id = $cat_id['term_id'];
229 if ( empty( $added ) ) {
230 wp_send_json_error( array( 'errorMessage' => __( 'This category cannot be added. Please change the name and try again.' ) ) );
233 foreach ( $added as $new_cat_id ) {
234 $new_cat = get_category( $new_cat_id );
236 if ( is_wp_error( $new_cat ) ) {
237 wp_send_json_error( array( 'errorMessage' => __( 'Error while adding the category. Please try again later.' ) ) );
241 'term_id' => $new_cat->term_id,
242 'name' => $new_cat->name,
243 'parent' => $new_cat->parent,
246 wp_send_json_success( $data );
250 * Downloads the source's HTML via server-side call for the given URL.
255 * @param string $url URL to scan.
256 * @return string Source's HTML sanitized markup
258 public function fetch_source_html( $url ) {
261 if ( empty( $url ) ) {
262 return new WP_Error( 'invalid-url', __( 'A valid URL was not provided.' ) );
265 $remote_url = wp_safe_remote_get( $url, array(
267 // Use an explicit user-agent for Press This
268 'user-agent' => 'Press This (WordPress/' . $wp_version . '); ' . get_bloginfo( 'url' )
271 if ( is_wp_error( $remote_url ) ) {
275 $useful_html_elements = array(
296 $source_content = wp_remote_retrieve_body( $remote_url );
297 $source_content = wp_kses( $source_content, $useful_html_elements );
299 return $source_content;
303 * Utility method to limit an array to 50 values.
308 * @param array $value Array to limit.
309 * @return array Original array if fewer than 50 values, limited array, empty array otherwise.
311 private function _limit_array( $value ) {
312 if ( is_array( $value ) ) {
313 if ( count( $value ) > 50 ) {
314 return array_slice( $value, 0, 50 );
324 * Utility method to limit the length of a given string to 5,000 characters.
329 * @param string $value String to limit.
330 * @return bool|int|string If boolean or integer, that value. If a string, the original value
331 * if fewer than 5,000 characters, a truncated version, otherwise an
334 private function _limit_string( $value ) {
337 if ( is_numeric( $value ) || is_bool( $value ) ) {
339 } else if ( is_string( $value ) ) {
340 if ( mb_strlen( $value ) > 5000 ) {
341 $return = mb_substr( $value, 0, 5000 );
346 $return = html_entity_decode( $return, ENT_QUOTES, 'UTF-8' );
347 $return = sanitize_text_field( trim( $return ) );
354 * Utility method to limit a given URL to 2,048 characters.
359 * @param string $url URL to check for length and validity.
360 * @return string Escaped URL if of valid length (< 2048) and makeup. Empty string otherwise.
362 private function _limit_url( $url ) {
363 if ( ! is_string( $url ) ) {
367 // HTTP 1.1 allows 8000 chars but the "de-facto" standard supported in all current browsers is 2048.
368 if ( strlen( $url ) > 2048 ) {
369 return ''; // Return empty rather than a truncated/invalid URL
372 // Does not look like an URL.
373 if ( ! preg_match( '/^([!#$&-;=?-\[\]_a-z~]|%[0-9a-fA-F]{2})+$/', $url ) ) {
377 // If the URL is root-relative, prepend the protocol and domain name
378 if ( $url && $this->domain && preg_match( '%^/[^/]+%', $url ) ) {
379 $url = $this->domain . $url;
382 // Not absolute or protocol-relative URL.
383 if ( ! preg_match( '%^(?:https?:)?//[^/]+%', $url ) ) {
387 return esc_url_raw( $url, array( 'http', 'https' ) );
391 * Utility method to limit image source URLs.
393 * Excluded URLs include share-this type buttons, loaders, spinners, spacers, WordPress interface images,
394 * tiny buttons or thumbs, mathtag.com or quantserve.com images, or the WordPress.com stats gif.
399 * @param string $src Image source URL.
400 * @return string If not matched an excluded URL type, the original URL, empty string otherwise.
402 private function _limit_img( $src ) {
403 $src = $this->_limit_url( $src );
405 if ( preg_match( '!/ad[sx]?/!i', $src ) ) {
408 } else if ( preg_match( '!(/share-?this[^.]+?\.[a-z0-9]{3,4})(\?.*)?$!i', $src ) ) {
409 // Share-this type button
411 } else if ( preg_match( '!/(spinner|loading|spacer|blank|rss)\.(gif|jpg|png)!i', $src ) ) {
412 // Loaders, spinners, spacers
414 } else if ( preg_match( '!/([^./]+[-_])?(spinner|loading|spacer|blank)s?([-_][^./]+)?\.[a-z0-9]{3,4}!i', $src ) ) {
415 // Fancy loaders, spinners, spacers
417 } else if ( preg_match( '!([^./]+[-_])?thumb[^.]*\.(gif|jpg|png)$!i', $src ) ) {
418 // Thumbnails, too small, usually irrelevant to context
420 } else if ( false !== stripos( $src, '/wp-includes/' ) ) {
421 // Classic WordPress interface images
423 } else if ( preg_match( '![^\d]\d{1,2}x\d+\.(gif|jpg|png)$!i', $src ) ) {
424 // Most often tiny buttons/thumbs (< 100px wide)
426 } else if ( preg_match( '!/pixel\.(mathtag|quantserve)\.com!i', $src ) ) {
427 // See mathtag.com and https://www.quantcast.com/how-we-do-it/iab-standard-measurement/how-we-collect-data/
429 } else if ( preg_match( '!/[gb]\.gif(\?.+)?$!i', $src ) ) {
430 // WordPress.com stats gif
438 * Limit embed source URLs to specific providers.
440 * Not all core oEmbed providers are supported. Supported providers include YouTube, Vimeo,
441 * Vine, Daily Motion, SoundCloud, and Twitter.
446 * @param string $src Embed source URL.
447 * @return string If not from a supported provider, an empty string. Otherwise, a reformattd embed URL.
449 private function _limit_embed( $src ) {
450 $src = $this->_limit_url( $src );
455 if ( preg_match( '!//(m|www)\.youtube\.com/(embed|v)/([^?]+)\?.+$!i', $src, $src_matches ) ) {
456 // Embedded Youtube videos (www or mobile)
457 $src = 'https://www.youtube.com/watch?v=' . $src_matches[3];
458 } else if ( preg_match( '!//player\.vimeo\.com/video/([\d]+)([?/].*)?$!i', $src, $src_matches ) ) {
459 // Embedded Vimeo iframe videos
460 $src = 'https://vimeo.com/' . (int) $src_matches[1];
461 } else if ( preg_match( '!//vimeo\.com/moogaloop\.swf\?clip_id=([\d]+)$!i', $src, $src_matches ) ) {
462 // Embedded Vimeo Flash videos
463 $src = 'https://vimeo.com/' . (int) $src_matches[1];
464 } else if ( preg_match( '!//vine\.co/v/([^/]+)/embed!i', $src, $src_matches ) ) {
465 // Embedded Vine videos
466 $src = 'https://vine.co/v/' . $src_matches[1];
467 } else if ( preg_match( '!//(www\.)?dailymotion\.com/embed/video/([^/?]+)([/?].+)?!i', $src, $src_matches ) ) {
468 // Embedded Daily Motion videos
469 $src = 'https://www.dailymotion.com/video/' . $src_matches[2];
471 require_once( ABSPATH . WPINC . '/class-oembed.php' );
472 $oembed = _wp_oembed_get_object();
474 if ( ! $oembed->get_provider( $src, array( 'discover' => false ) ) ) {
483 * Process a meta data entry from the source.
488 * @param string $meta_name Meta key name.
489 * @param mixed $meta_value Meta value.
490 * @param array $data Associative array of source data.
491 * @return array Processed data array.
493 private function _process_meta_entry( $meta_name, $meta_value, $data ) {
494 if ( preg_match( '/:?(title|description|keywords|site_name)$/', $meta_name ) ) {
495 $data['_meta'][ $meta_name ] = $meta_value;
497 switch ( $meta_name ) {
500 case 'og:video:secure_url':
501 $meta_value = $this->_limit_embed( $meta_value );
503 if ( ! isset( $data['_embeds'] ) ) {
504 $data['_embeds'] = array();
507 if ( ! empty( $meta_value ) && ! in_array( $meta_value, $data['_embeds'] ) ) {
508 $data['_embeds'][] = $meta_value;
513 case 'og:image:secure_url':
514 case 'twitter:image0:src':
515 case 'twitter:image0':
516 case 'twitter:image:src':
517 case 'twitter:image':
518 $meta_value = $this->_limit_img( $meta_value );
520 if ( ! isset( $data['_images'] ) ) {
521 $data['_images'] = array();
524 if ( ! empty( $meta_value ) && ! in_array( $meta_value, $data['_images'] ) ) {
525 $data['_images'][] = $meta_value;
536 * Fetches and parses _meta, _images, and _links data from the source.
541 * @param string $url URL to scan.
542 * @param array $data Optional. Existing data array if you have one. Default empty array.
543 * @return array New data array.
545 public function source_data_fetch_fallback( $url, $data = array() ) {
546 if ( empty( $url ) ) {
550 // Download source page to tmp file.
551 $source_content = $this->fetch_source_html( $url );
552 if ( is_wp_error( $source_content ) ) {
553 return array( 'errors' => $source_content->get_error_messages() );
556 // Fetch and gather <meta> data first, so discovered media is offered 1st to user.
557 if ( empty( $data['_meta'] ) ) {
558 $data['_meta'] = array();
561 if ( preg_match_all( '/<meta [^>]+>/', $source_content, $matches ) ) {
562 $items = $this->_limit_array( $matches[0] );
564 foreach ( $items as $value ) {
565 if ( preg_match( '/(property|name)="([^"]+)"[^>]+content="([^"]+)"/', $value, $new_matches ) ) {
566 $meta_name = $this->_limit_string( $new_matches[2] );
567 $meta_value = $this->_limit_string( $new_matches[3] );
569 // Sanity check. $key is usually things like 'title', 'description', 'keywords', etc.
570 if ( strlen( $meta_name ) > 100 ) {
574 $data = $this->_process_meta_entry( $meta_name, $meta_value, $data );
579 // Fetch and gather <img> data.
580 if ( empty( $data['_images'] ) ) {
581 $data['_images'] = array();
584 if ( preg_match_all( '/<img [^>]+>/', $source_content, $matches ) ) {
585 $items = $this->_limit_array( $matches[0] );
587 foreach ( $items as $value ) {
588 if ( ( preg_match( '/width=(\'|")(\d+)\\1/i', $value, $new_matches ) && $new_matches[2] < 256 ) ||
589 ( preg_match( '/height=(\'|")(\d+)\\1/i', $value, $new_matches ) && $new_matches[2] < 128 ) ) {
594 if ( preg_match( '/src=(\'|")([^\'"]+)\\1/i', $value, $new_matches ) ) {
595 $src = $this->_limit_img( $new_matches[2] );
596 if ( ! empty( $src ) && ! in_array( $src, $data['_images'] ) ) {
597 $data['_images'][] = $src;
603 // Fetch and gather <iframe> data.
604 if ( empty( $data['_embeds'] ) ) {
605 $data['_embeds'] = array();
608 if ( preg_match_all( '/<iframe [^>]+>/', $source_content, $matches ) ) {
609 $items = $this->_limit_array( $matches[0] );
611 foreach ( $items as $value ) {
612 if ( preg_match( '/src=(\'|")([^\'"]+)\\1/', $value, $new_matches ) ) {
613 $src = $this->_limit_embed( $new_matches[2] );
615 if ( ! empty( $src ) && ! in_array( $src, $data['_embeds'] ) ) {
616 $data['_embeds'][] = $src;
622 // Fetch and gather <link> data.
623 if ( empty( $data['_links'] ) ) {
624 $data['_links'] = array();
627 if ( preg_match_all( '/<link [^>]+>/', $source_content, $matches ) ) {
628 $items = $this->_limit_array( $matches[0] );
630 foreach ( $items as $value ) {
631 if ( preg_match( '/rel=["\'](canonical|shortlink|icon)["\']/i', $value, $matches_rel ) && preg_match( '/href=[\'"]([^\'" ]+)[\'"]/i', $value, $matches_url ) ) {
632 $rel = $matches_rel[1];
633 $url = $this->_limit_url( $matches_url[1] );
635 if ( ! empty( $url ) && empty( $data['_links'][ $rel ] ) ) {
636 $data['_links'][ $rel ] = $url;
646 * Handles backward-compat with the legacy version of Press This by supporting its query string params.
653 public function merge_or_fetch_data() {
654 // Get data from $_POST and $_GET, as appropriate ($_POST > $_GET), to remain backward compatible.
657 // Only instantiate the keys we want. Sanity check and sanitize each one.
658 foreach ( array( 'u', 's', 't', 'v' ) as $key ) {
659 if ( ! empty( $_POST[ $key ] ) ) {
660 $value = wp_unslash( $_POST[ $key ] );
661 } else if ( ! empty( $_GET[ $key ] ) ) {
662 $value = wp_unslash( $_GET[ $key ] );
667 if ( 'u' === $key ) {
668 $value = $this->_limit_url( $value );
670 if ( preg_match( '%^(?:https?:)?//[^/]+%i', $value, $domain_match ) ) {
671 $this->domain = $domain_match[0];
674 $value = $this->_limit_string( $value );
677 if ( ! empty( $value ) ) {
678 $data[ $key ] = $value;
683 * Filter whether to enable in-source media discovery in Press This.
687 * @param bool $enable Whether to enable media discovery.
689 if ( apply_filters( 'enable_press_this_media_discovery', true ) ) {
691 * If no title, _images, _embed, and _meta was passed via $_POST, fetch data from source as fallback,
692 * making PT fully backward compatible with the older bookmarklet.
694 if ( empty( $_POST ) && ! empty( $data['u'] ) ) {
695 $data = $this->source_data_fetch_fallback( $data['u'], $data );
697 foreach ( array( '_images', '_embeds' ) as $type ) {
698 if ( empty( $_POST[ $type ] ) ) {
702 $data[ $type ] = array();
703 $items = $this->_limit_array( $_POST[ $type ] );
705 foreach ( $items as $key => $value ) {
706 if ( $type === '_images' ) {
707 $value = $this->_limit_img( wp_unslash( $value ) );
709 $value = $this->_limit_embed( wp_unslash( $value ) );
712 if ( ! empty( $value ) ) {
713 $data[ $type ][] = $value;
718 foreach ( array( '_meta', '_links' ) as $type ) {
719 if ( empty( $_POST[ $type ] ) ) {
723 $data[ $type ] = array();
724 $items = $this->_limit_array( $_POST[ $type ] );
726 foreach ( $items as $key => $value ) {
727 // Sanity check. These are associative arrays, $key is usually things like 'title', 'description', 'keywords', etc.
728 if ( empty( $key ) || strlen( $key ) > 100 ) {
732 if ( $type === '_meta' ) {
733 $value = $this->_limit_string( wp_unslash( $value ) );
735 if ( ! empty( $value ) ) {
736 $data = $this->_process_meta_entry( $key, $value, $data );
739 if ( in_array( $key, array( 'canonical', 'shortlink', 'icon' ), true ) ) {
740 $data[ $type ][ $key ] = $this->_limit_url( wp_unslash( $value ) );
747 // Support passing a single image src as `i`
748 if ( ! empty( $_REQUEST['i'] ) && ( $img_src = $this->_limit_img( wp_unslash( $_REQUEST['i'] ) ) ) ) {
749 if ( empty( $data['_images'] ) ) {
750 $data['_images'] = array( $img_src );
751 } elseif ( ! in_array( $img_src, $data['_images'], true ) ) {
752 array_unshift( $data['_images'], $img_src );
758 * Filter the Press This data array.
762 * @param array $data Press This Data array.
764 return apply_filters( 'press_this_data', $data );
768 * Adds another stylesheet inside TinyMCE.
773 * @param string $styles URL to editor stylesheet.
774 * @return string Possibly modified stylesheets list.
776 public function add_editor_style( $styles ) {
777 if ( ! empty( $styles ) ) {
781 $press_this = admin_url( 'css/press-this-editor.css' );
783 $press_this = str_replace( '.css', '-rtl.css', $press_this );
786 $open_sans_font_url = '';
788 /* translators: If there are characters in your language that are not supported
789 * by Open Sans, translate this to 'off'. Do not translate into your own language.
791 if ( 'off' !== _x( 'on', 'Open Sans font: on or off' ) ) {
792 $subsets = 'latin,latin-ext';
794 /* translators: To add an additional Open Sans character subset specific to your language,
795 * translate this to 'greek', 'cyrillic' or 'vietnamese'. Do not translate into your own language.
797 $subset = _x( 'no-subset', 'Open Sans font: add new subset (greek, cyrillic, vietnamese)' );
799 if ( 'cyrillic' == $subset ) {
800 $subsets .= ',cyrillic,cyrillic-ext';
801 } elseif ( 'greek' == $subset ) {
802 $subsets .= ',greek,greek-ext';
803 } elseif ( 'vietnamese' == $subset ) {
804 $subsets .= ',vietnamese';
808 'family' => urlencode( 'Open Sans:400italic,700italic,400,600,700' ),
809 'subset' => urlencode( $subsets ),
812 $open_sans_font_url = ',' . add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
815 return $styles . $press_this . $open_sans_font_url;
819 * Outputs the post format selection HTML.
824 * @param WP_Post $post Post object.
826 public function post_formats_html( $post ) {
827 if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) {
828 $post_formats = get_theme_support( 'post-formats' );
830 if ( is_array( $post_formats[0] ) ) {
831 $post_format = get_post_format( $post->ID );
833 if ( ! $post_format ) {
837 // Add in the current one if it isn't there yet, in case the current theme doesn't support it.
838 if ( $post_format && ! in_array( $post_format, $post_formats[0] ) ) {
839 $post_formats[0][] = $post_format;
843 <div id="post-formats-select">
844 <fieldset><legend class="screen-reader-text"><?php _e( 'Post Formats' ); ?></legend>
845 <input type="radio" name="post_format" class="post-format" id="post-format-0" value="0" <?php checked( $post_format, '0' ); ?> />
846 <label for="post-format-0" class="post-format-icon post-format-standard"><?php echo get_post_format_string( 'standard' ); ?></label>
849 foreach ( $post_formats[0] as $format ) {
850 $attr_format = esc_attr( $format );
853 <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 ); ?> />
854 <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>
867 * Outputs the categories HTML.
872 * @param WP_Post $post Post object.
874 public function categories_html( $post ) {
875 $taxonomy = get_taxonomy( 'category' );
877 if ( current_user_can( $taxonomy->cap->edit_terms ) ) {
879 <button type="button" class="add-cat-toggle button-link" aria-expanded="false">
880 <span class="dashicons dashicons-plus"></span><span class="screen-reader-text"><?php _e( 'Toggle add category' ); ?></span>
882 <div class="add-category is-hidden">
883 <label class="screen-reader-text" for="new-category"><?php echo $taxonomy->labels->add_new_item; ?></label>
884 <input type="text" id="new-category" class="add-category-name" placeholder="<?php echo esc_attr( $taxonomy->labels->new_item_name ); ?>" value="" aria-required="true">
885 <label class="screen-reader-text" for="new-category-parent"><?php echo $taxonomy->labels->parent_item_colon; ?></label>
886 <div class="postform-wrapper">
888 wp_dropdown_categories( array(
889 'taxonomy' => 'category',
891 'name' => 'new-category-parent',
894 'show_option_none' => '— ' . $taxonomy->labels->parent_item . ' —'
898 <button type="button" class="add-cat-submit"><?php _e( 'Add' ); ?></button>
904 <div class="categories-search-wrapper">
905 <input id="categories-search" type="search" class="categories-search" placeholder="<?php esc_attr_e( 'Search categories by name' ) ?>">
906 <label for="categories-search">
907 <span class="dashicons dashicons-search"></span><span class="screen-reader-text"><?php _e( 'Search categories' ); ?></span>
910 <div aria-label="<?php esc_attr_e( 'Categories' ); ?>">
911 <ul class="categories-select">
912 <?php wp_terms_checklist( $post->ID, array( 'taxonomy' => 'category', 'list_only' => true ) ); ?>
919 * Outputs the tags HTML.
924 * @param WP_Post $post Post object.
926 public function tags_html( $post ) {
927 $taxonomy = get_taxonomy( 'post_tag' );
928 $user_can_assign_terms = current_user_can( $taxonomy->cap->assign_terms );
929 $esc_tags = get_terms_to_edit( $post->ID, 'post_tag' );
931 if ( ! $esc_tags || is_wp_error( $esc_tags ) ) {
936 <div class="tagsdiv" id="post_tag">
938 <input type="hidden" name="tax_input[post_tag]" class="the-tags" value="<?php echo $esc_tags; // escaped in get_terms_to_edit() ?>">
941 if ( $user_can_assign_terms ) {
943 <div class="ajaxtag hide-if-no-js">
944 <label class="screen-reader-text" for="new-tag-post_tag"><?php _e( 'Tags' ); ?></label>
946 <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" />
947 <button type="button" class="tagadd"><?php _e( 'Add' ); ?></button>
950 <p class="howto" id="new-tag-desc">
951 <?php echo $taxonomy->labels->separate_items_with_commas; ?>
958 <div class="tagchecklist"></div>
962 if ( $user_can_assign_terms ) {
964 <button type="button" class="button-link tagcloud-link" id="link-post_tag"><?php echo $taxonomy->labels->choose_from_most_used; ?></button>
970 * Get a list of embeds with no duplicates.
975 * @param array $data The site's data.
976 * @return array Embeds selected to be available.
978 public function get_embeds( $data ) {
979 $selected_embeds = array();
981 // Make sure to add the Pressed page if it's a valid oembed itself
982 if ( ! empty ( $data['u'] ) && $this->_limit_embed( $data['u'] ) ) {
983 $data['_embeds'][] = $data['u'];
986 if ( ! empty( $data['_embeds'] ) ) {
987 foreach ( $data['_embeds'] as $src ) {
988 $prot_relative_src = preg_replace( '/^https?:/', '', $src );
990 if ( in_array( $prot_relative_src, $this->embeds ) ) {
994 $selected_embeds[] = $src;
995 $this->embeds[] = $prot_relative_src;
999 return $selected_embeds;
1003 * Get a list of images with no duplicates.
1008 * @param array $data The site's data.
1011 public function get_images( $data ) {
1012 $selected_images = array();
1014 if ( ! empty( $data['_images'] ) ) {
1015 foreach ( $data['_images'] as $src ) {
1016 if ( false !== strpos( $src, 'gravatar.com' ) ) {
1017 $src = preg_replace( '%http://[\d]+\.gravatar\.com/%', 'https://secure.gravatar.com/', $src );
1020 $prot_relative_src = preg_replace( '/^https?:/', '', $src );
1022 if ( in_array( $prot_relative_src, $this->images ) ||
1023 ( false !== strpos( $src, 'avatar' ) && count( $this->images ) > 15 ) ) {
1024 // Skip: already selected or some type of avatar and we've already gathered more than 15 images.
1028 $selected_images[] = $src;
1029 $this->images[] = $prot_relative_src;
1033 return $selected_images;
1037 * Gets the source page's canonical link, based on passed location and meta data.
1042 * @param array $data The site's data.
1043 * @return string Discovered canonical URL, or empty
1045 public function get_canonical_link( $data ) {
1048 if ( ! empty( $data['_links']['canonical'] ) ) {
1049 $link = $data['_links']['canonical'];
1050 } elseif ( ! empty( $data['u'] ) ) {
1052 } elseif ( ! empty( $data['_meta'] ) ) {
1053 if ( ! empty( $data['_meta']['twitter:url'] ) ) {
1054 $link = $data['_meta']['twitter:url'];
1055 } else if ( ! empty( $data['_meta']['og:url'] ) ) {
1056 $link = $data['_meta']['og:url'];
1060 if ( empty( $link ) && ! empty( $data['_links']['shortlink'] ) ) {
1061 $link = $data['_links']['shortlink'];
1068 * Gets the source page's site name, based on passed meta data.
1073 * @param array $data The site's data.
1074 * @return string Discovered site name, or empty
1076 public function get_source_site_name( $data ) {
1079 if ( ! empty( $data['_meta'] ) ) {
1080 if ( ! empty( $data['_meta']['og:site_name'] ) ) {
1081 $name = $data['_meta']['og:site_name'];
1082 } else if ( ! empty( $data['_meta']['application-name'] ) ) {
1083 $name = $data['_meta']['application-name'];
1091 * Gets the source page's title, based on passed title and meta data.
1096 * @param array $data The site's data.
1097 * @return string Discovered page title, or empty
1099 public function get_suggested_title( $data ) {
1102 if ( ! empty( $data['t'] ) ) {
1103 $title = $data['t'];
1104 } elseif ( ! empty( $data['_meta'] ) ) {
1105 if ( ! empty( $data['_meta']['twitter:title'] ) ) {
1106 $title = $data['_meta']['twitter:title'];
1107 } else if ( ! empty( $data['_meta']['og:title'] ) ) {
1108 $title = $data['_meta']['og:title'];
1109 } else if ( ! empty( $data['_meta']['title'] ) ) {
1110 $title = $data['_meta']['title'];
1118 * Gets the source page's suggested content, based on passed data (description, selection, etc).
1120 * Features a blockquoted excerpt, as well as content attribution, if any.
1125 * @param array $data The site's data.
1126 * @return string Discovered content, or empty
1128 public function get_suggested_content( $data ) {
1129 $content = $text = '';
1131 if ( ! empty( $data['s'] ) ) {
1133 } else if ( ! empty( $data['_meta'] ) ) {
1134 if ( ! empty( $data['_meta']['twitter:description'] ) ) {
1135 $text = $data['_meta']['twitter:description'];
1136 } else if ( ! empty( $data['_meta']['og:description'] ) ) {
1137 $text = $data['_meta']['og:description'];
1138 } else if ( ! empty( $data['_meta']['description'] ) ) {
1139 $text = $data['_meta']['description'];
1142 // If there is an ellipsis at the end, the description is very likely auto-generated. Better to ignore it.
1143 if ( $text && substr( $text, -3 ) === '...' ) {
1148 $default_html = array( 'quote' => '', 'link' => '', 'embed' => '' );
1150 if ( ! empty( $data['u'] ) && $this->_limit_embed( $data['u'] ) ) {
1151 $default_html['embed'] = '<p>[embed]' . $data['u'] . '[/embed]</p>';
1153 if ( ! empty( $data['s'] ) ) {
1154 // If the user has selected some text, do quote it.
1155 $default_html['quote'] = '<blockquote>%1$s</blockquote>';
1158 $default_html['quote'] = '<blockquote>%1$s</blockquote>';
1159 $default_html['link'] = '<p>' . _x( 'Source:', 'Used in Press This to indicate where the content comes from.' ) .
1160 ' <em><a href="%1$s">%2$s</a></em></p>';
1164 * Filter the default HTML for the Press This editor.
1168 * @param array $default_html Associative array with two keys: 'quote' where %1$s is replaced with the site description
1169 * or the selected content, and 'link' there %1$s is link href, %2$s is link text.
1171 $default_html = apply_filters( 'press_this_suggested_html', $default_html, $data );
1173 if ( ! empty( $default_html['embed'] ) ) {
1174 $content .= $default_html['embed'];
1177 // Wrap suggested content in the specified HTML.
1178 if ( ! empty( $default_html['quote'] ) && $text ) {
1179 $content .= sprintf( $default_html['quote'], $text );
1182 // Add source attribution if there is one available.
1183 if ( ! empty( $default_html['link'] ) ) {
1184 $title = $this->get_suggested_title( $data );
1185 $url = $this->get_canonical_link( $data );
1188 $title = $this->get_source_site_name( $data );
1191 if ( $url && $title ) {
1192 $content .= sprintf( $default_html['link'], $url, $title );
1200 * Serves the app's base HTML, which in turns calls the load script.
1205 * @global WP_Locale $wp_locale
1206 * @global string $wp_version
1207 * @global bool $is_IE
1209 public function html() {
1210 global $wp_locale, $wp_version;
1212 // Get data, new (POST) and old (GET).
1213 $data = $this->merge_or_fetch_data();
1215 $post_title = $this->get_suggested_title( $data );
1217 $post_content = $this->get_suggested_content( $data );
1219 // Get site settings array/data.
1220 $site_settings = $this->site_settings();
1222 // Pass the images and embeds
1223 $images = $this->get_images( $data );
1224 $embeds = $this->get_embeds( $data );
1227 'v' => ! empty( $data['v'] ) ? $data['v'] : '',
1228 'u' => ! empty( $data['u'] ) ? $data['u'] : '',
1229 'hasData' => ! empty( $data ),
1232 if ( ! empty( $images ) ) {
1233 $site_data['_images'] = $images;
1236 if ( ! empty( $embeds ) ) {
1237 $site_data['_embeds'] = $embeds;
1240 // Add press-this-editor.css and remove theme's editor-style.css, if any.
1241 remove_editor_styles();
1243 add_filter( 'mce_css', array( $this, 'add_editor_style' ) );
1245 if ( ! empty( $GLOBALS['is_IE'] ) ) {
1246 @header( 'X-UA-Compatible: IE=edge' );
1249 @header( 'Content-Type: ' . get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ) );
1253 <!--[if IE 7]> <html class="lt-ie9 lt-ie8" <?php language_attributes(); ?>> <![endif]-->
1254 <!--[if IE 8]> <html class="lt-ie9" <?php language_attributes(); ?>> <![endif]-->
1255 <!--[if gt IE 8]><!--> <html <?php language_attributes(); ?>> <!--<![endif]-->
1257 <meta http-equiv="Content-Type" content="<?php echo esc_attr( get_bloginfo( 'html_type' ) ); ?>; charset=<?php echo esc_attr( get_option( 'blog_charset' ) ); ?>" />
1258 <meta name="viewport" content="width=device-width">
1259 <title><?php esc_html_e( 'Press This!' ) ?></title>
1262 window.wpPressThisData = <?php echo wp_json_encode( $site_data ); ?>;
1263 window.wpPressThisConfig = <?php echo wp_json_encode( $site_settings ); ?>;
1266 <script type="text/javascript">
1267 var ajaxurl = '<?php echo esc_js( admin_url( 'admin-ajax.php', 'relative' ) ); ?>',
1268 pagenow = 'press-this',
1270 adminpage = 'press-this-php',
1271 thousandsSeparator = '<?php echo addslashes( $wp_locale->number_format['thousands_sep'] ); ?>',
1272 decimalPoint = '<?php echo addslashes( $wp_locale->number_format['decimal_point'] ); ?>',
1273 isRtl = <?php echo (int) is_rtl(); ?>;
1278 * $post->ID is needed for the embed shortcode so we can show oEmbed previews in the editor.
1279 * Maybe find a way without it.
1281 $post = get_default_post_to_edit( 'post', true );
1282 $post_ID = (int) $post->ID;
1284 wp_enqueue_media( array( 'post' => $post_ID ) );
1285 wp_enqueue_style( 'press-this' );
1286 wp_enqueue_script( 'press-this' );
1287 wp_enqueue_script( 'json2' );
1288 wp_enqueue_script( 'editor' );
1290 $supports_formats = false;
1293 if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) ) {
1294 $supports_formats = true;
1296 if ( ! ( $post_format = get_post_format( $post_ID ) ) ) {
1301 /** This action is documented in wp-admin/admin-header.php */
1302 do_action( 'admin_enqueue_scripts', 'press-this.php' );
1304 /** This action is documented in wp-admin/admin-header.php */
1305 do_action( 'admin_print_styles-press-this.php' );
1307 /** This action is documented in wp-admin/admin-header.php */
1308 do_action( 'admin_print_styles' );
1310 /** This action is documented in wp-admin/admin-header.php */
1311 do_action( 'admin_print_scripts-press-this.php' );
1313 /** This action is documented in wp-admin/admin-header.php */
1314 do_action( 'admin_print_scripts' );
1316 /** This action is documented in wp-admin/admin-header.php */
1317 do_action( 'admin_head-press-this.php' );
1319 /** This action is documented in wp-admin/admin-header.php */
1320 do_action( 'admin_head' );
1325 $admin_body_class = 'press-this';
1326 $admin_body_class .= ( is_rtl() ) ? ' rtl' : '';
1327 $admin_body_class .= ' branch-' . str_replace( array( '.', ',' ), '-', floatval( $wp_version ) );
1328 $admin_body_class .= ' version-' . str_replace( '.', '-', preg_replace( '/^([.0-9]+).*/', '$1', $wp_version ) );
1329 $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
1330 $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
1332 /** This filter is documented in wp-admin/admin-header.php */
1333 $admin_body_classes = apply_filters( 'admin_body_class', '' );
1336 <body class="wp-admin wp-core-ui <?php echo $admin_body_classes . ' ' . $admin_body_class; ?>">
1337 <div id="adminbar" class="adminbar">
1338 <h1 id="current-site" class="current-site">
1339 <a class="current-site-link" href="<?php echo esc_url( home_url( '/' ) ); ?>" target="_blank" rel="home">
1340 <span class="dashicons dashicons-wordpress"></span>
1341 <span class="current-site-name"><?php bloginfo( 'name' ); ?></span>
1344 <button type="button" class="options button-link closed">
1345 <span class="dashicons dashicons-tag on-closed"></span>
1346 <span class="screen-reader-text on-closed"><?php _e( 'Show post options' ); ?></span>
1347 <span aria-hidden="true" class="on-open"><?php _e( 'Done' ); ?></span>
1348 <span class="screen-reader-text on-open"><?php _e( 'Hide post options' ); ?></span>
1352 <div id="scanbar" class="scan">
1354 <label for="url-scan" class="screen-reader-text"><?php _e( 'Scan site for content' ); ?></label>
1355 <input type="url" name="u" id="url-scan" class="scan-url" value="" placeholder="<?php esc_attr_e( 'Enter a URL to scan' ) ?>" />
1356 <input type="submit" name="url-scan-submit" id="url-scan-submit" class="scan-submit" value="<?php esc_attr_e( 'Scan' ) ?>" />
1360 <form id="pressthis-form" method="post" action="post.php" autocomplete="off">
1361 <input type="hidden" name="post_ID" id="post_ID" value="<?php echo $post_ID; ?>" />
1362 <input type="hidden" name="action" value="press-this-save-post" />
1363 <input type="hidden" name="post_status" id="post_status" value="draft" />
1364 <input type="hidden" name="wp-preview" id="wp-preview" value="" />
1365 <input type="hidden" name="post_title" id="post_title" value="" />
1366 <input type="hidden" name="pt-force-redirect" id="pt-force-redirect" value="" />
1369 wp_nonce_field( 'update-post_' . $post_ID, '_wpnonce', false );
1370 wp_nonce_field( 'add-category', '_ajax_nonce-add-category', false );
1374 <div class="wrapper">
1375 <div class="editor-wrapper">
1376 <div class="alerts" role="alert" aria-live="assertive" aria-relevant="all" aria-atomic="true">
1379 if ( isset( $data['v'] ) && $this->version > $data['v'] ) {
1381 <p class="alert is-notice">
1382 <?php printf( __( 'You should upgrade <a href="%s" target="_blank">your bookmarklet</a> to the latest version!' ), admin_url( 'tools.php' ) ); ?>
1390 <div id="app-container" class="editor">
1391 <span id="title-container-label" class="post-title-placeholder" aria-hidden="true"><?php _e( 'Post title' ); ?></span>
1392 <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>
1394 <div class="media-list-container">
1395 <div class="media-list-inner-container">
1396 <h2 class="screen-reader-text"><?php _e( 'Suggested media' ); ?></h2>
1397 <ul class="media-list"></ul>
1402 wp_editor( $post_content, 'pressthis', array(
1403 'drag_drop_upload' => true,
1404 'editor_height' => 600,
1405 'media_buttons' => false,
1406 'textarea_name' => 'post_content',
1410 'wordpress_adv_hidden' => false,
1411 'add_unload_trigger' => false,
1412 'statusbar' => false,
1413 'autoresize_min_height' => 600,
1414 'wp_autoresize_on' => true,
1415 'plugins' => 'lists,media,paste,tabfocus,fullscreen,wordpress,wpautoresize,wpeditimage,wpgallery,wplink,wptextpattern,wpview',
1416 'toolbar1' => 'bold,italic,bullist,numlist,blockquote,link,unlink',
1417 'toolbar2' => 'undo,redo',
1419 'quicktags' => array(
1420 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,more',
1428 <div class="options-panel-back is-hidden" tabindex="-1"></div>
1429 <div class="options-panel is-off-screen is-hidden" tabindex="-1">
1430 <div class="post-options">
1432 <?php if ( $supports_formats ) : ?>
1433 <button type="button" class="button-link post-option">
1434 <span class="dashicons dashicons-admin-post"></span>
1435 <span class="post-option-title"><?php _ex( 'Format', 'post format' ); ?></span>
1436 <span class="post-option-contents" id="post-option-post-format"><?php echo esc_html( get_post_format_string( $post_format ) ); ?></span>
1437 <span class="dashicons post-option-forward"></span>
1441 <button type="button" class="button-link post-option">
1442 <span class="dashicons dashicons-category"></span>
1443 <span class="post-option-title"><?php _e( 'Categories' ); ?></span>
1444 <span class="dashicons post-option-forward"></span>
1447 <button type="button" class="button-link post-option">
1448 <span class="dashicons dashicons-tag"></span>
1449 <span class="post-option-title"><?php _e( 'Tags' ); ?></span>
1450 <span class="dashicons post-option-forward"></span>
1454 <?php if ( $supports_formats ) : ?>
1455 <div class="setting-modal is-off-screen is-hidden">
1456 <button type="button" class="button-link modal-close">
1457 <span class="dashicons post-option-back"></span>
1458 <span class="setting-title" aria-hidden="true"><?php _ex( 'Format', 'post format' ); ?></span>
1459 <span class="screen-reader-text"><?php _e( 'Back to post options' ) ?></span>
1461 <?php $this->post_formats_html( $post ); ?>
1465 <div class="setting-modal is-off-screen is-hidden">
1466 <button type="button" class="button-link modal-close">
1467 <span class="dashicons post-option-back"></span>
1468 <span class="setting-title" aria-hidden="true"><?php _e( 'Categories' ); ?></span>
1469 <span class="screen-reader-text"><?php _e( 'Back to post options' ) ?></span>
1471 <?php $this->categories_html( $post ); ?>
1474 <div class="setting-modal tags is-off-screen is-hidden">
1475 <button type="button" class="button-link modal-close">
1476 <span class="dashicons post-option-back"></span>
1477 <span class="setting-title" aria-hidden="true"><?php _e( 'Tags' ); ?></span>
1478 <span class="screen-reader-text"><?php _e( 'Back to post options' ) ?></span>
1480 <?php $this->tags_html( $post ); ?>
1482 </div><!-- .options-panel -->
1483 </div><!-- .wrapper -->
1485 <div class="press-this-actions">
1486 <div class="pressthis-media-buttons">
1487 <button type="button" class="insert-media button-link" data-editor="pressthis">
1488 <span class="dashicons dashicons-admin-media"></span>
1489 <span class="screen-reader-text"><?php _e( 'Add Media' ); ?></span>
1492 <div class="post-actions">
1493 <span class="spinner"> </span>
1494 <div class="split-button">
1495 <div class="split-button-head">
1496 <button type="button" class="publish-button split-button-primary" aria-live="polite">
1497 <span class="publish"><?php echo ( current_user_can( 'publish_posts' ) ) ? __( 'Publish' ) : __( 'Submit for Review' ); ?></span>
1498 <span class="saving-draft"><?php _e( 'Saving…' ); ?></span>
1499 </button><button type="button" class="split-button-toggle" aria-haspopup="true" aria-expanded="false">
1500 <i class="dashicons dashicons-arrow-down-alt2"></i>
1501 <span class="screen-reader-text"><?php _e('More actions'); ?></span>
1504 <ul class="split-button-body">
1505 <li><button type="button" class="button-link draft-button split-button-option"><?php _e( 'Save Draft' ); ?></button></li>
1506 <li><button type="button" class="button-link standard-editor-button split-button-option"><?php _e( 'Standard Editor' ); ?></button></li>
1507 <li><button type="button" class="button-link preview-button split-button-option"><?php _e( 'Preview' ); ?></button></li>
1515 /** This action is documented in wp-admin/admin-footer.php */
1516 do_action( 'admin_footer' );
1518 /** This action is documented in wp-admin/admin-footer.php */
1519 do_action( 'admin_print_footer_scripts' );
1521 /** This action is documented in wp-admin/admin-footer.php */
1522 do_action( 'admin_footer-press-this.php' );
1533 * @global WP_Press_This $wp_press_this
1535 $GLOBALS['wp_press_this'] = new WP_Press_This;