Changeset 31693 for trunk/src/wp-admin/includes/class-wp-press-this.php
- Timestamp:
- 03/09/2015 09:48:38 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/class-wp-press-this.php
r31655 r31693 15 15 class WP_Press_This { 16 16 17 private $images = array(); 18 19 private $embeds = array(); 20 17 21 /** 18 22 * Constructor. … … 32 36 */ 33 37 public function site_settings() { 34 $default_html = array(35 'quote' => '<blockquote>%1$s</blockquote>',36 'link' => '<p>' . _x( 'Source:', 'Used in Press This to indicate where the content comes from.' ) .37 ' <em><a href="%1$s">%2$s</a></em></p>',38 );39 40 38 return array( 41 39 // Used to trigger the bookmarklet update notice. 42 40 // Needs to be set here and in get_shortcut_link() in wp-includes/link-template.php. 43 'version' => ' 6',41 'version' => '7', 44 42 45 43 /** … … 51 49 */ 52 50 'redirInParent' => apply_filters( 'press_this_redirect_in_parent', false ), 53 54 /**55 * Filter the default HTML for the Press This editor.56 *57 * @since 4.2.058 *59 * @param array $default_html Associative array with two keys: 'quote' where %1$s is replaced with the site description60 * or the selected content, and 'link' there %1$s is link href, %2$s is link text.61 */62 'html' => apply_filters( 'press_this_suggested_html', $default_html ),63 51 ); 64 52 } … … 436 424 437 425 private function _process_meta_entry( $meta_name, $meta_value, $data ) { 438 if ( preg_match( '/:?(title|description|keywords )$/', $meta_name ) ) {426 if ( preg_match( '/:?(title|description|keywords|site_name)$/', $meta_name ) ) { 439 427 $data['_meta'][ $meta_name ] = $meta_value; 440 428 } else { … … 445 433 $meta_value = $this->_limit_embed( $meta_value ); 446 434 447 if ( ! isset( $data['_embed '] ) ) {448 $data['_embed '] = array();435 if ( ! isset( $data['_embeds'] ) ) { 436 $data['_embeds'] = array(); 449 437 } 450 438 451 if ( ! empty( $meta_value ) && ! in_array( $meta_value, $data['_embed '] ) ) {452 $data['_embed '][] = $meta_value;439 if ( ! empty( $meta_value ) && ! in_array( $meta_value, $data['_embeds'] ) ) { 440 $data['_embeds'][] = $meta_value; 453 441 } 454 442 … … 462 450 $meta_value = $this->_limit_img( $meta_value ); 463 451 464 if ( ! isset( $data['_im g'] ) ) {465 $data['_im g'] = array();452 if ( ! isset( $data['_images'] ) ) { 453 $data['_images'] = array(); 466 454 } 467 455 468 if ( ! empty( $meta_value ) && ! in_array( $meta_value, $data['_im g'] ) ) {469 $data['_im g'][] = $meta_value;456 if ( ! empty( $meta_value ) && ! in_array( $meta_value, $data['_images'] ) ) { 457 $data['_images'][] = $meta_value; 470 458 } 471 459 … … 478 466 479 467 /** 480 * Fetches and parses _meta, _im g, and _links data from the source.468 * Fetches and parses _meta, _images, and _links data from the source. 481 469 * 482 470 * @since 4.2.0 … … 522 510 523 511 // Fetch and gather <img> data. 524 if ( empty( $data['_im g'] ) ) {525 $data['_im g'] = array();512 if ( empty( $data['_images'] ) ) { 513 $data['_images'] = array(); 526 514 } 527 515 … … 538 526 if ( preg_match( '/src=(\'|")([^\'"]+)\\1/i', $value, $new_matches ) ) { 539 527 $src = $this->_limit_img( $new_matches[2] ); 540 if ( ! empty( $src ) && ! in_array( $src, $data['_im g'] ) ) {541 $data['_im g'][] = $src;528 if ( ! empty( $src ) && ! in_array( $src, $data['_images'] ) ) { 529 $data['_images'][] = $src; 542 530 } 543 531 } … … 546 534 547 535 // Fetch and gather <iframe> data. 548 if ( empty( $data['_embed '] ) ) {549 $data['_embed '] = array();536 if ( empty( $data['_embeds'] ) ) { 537 $data['_embeds'] = array(); 550 538 } 551 539 … … 557 545 $src = $this->_limit_embed( $new_matches[2] ); 558 546 559 if ( ! empty( $src ) && ! in_array( $src, $data['_embed '] ) ) {560 $data['_embed '][] = $src;547 if ( ! empty( $src ) && ! in_array( $src, $data['_embeds'] ) ) { 548 $data['_embeds'][] = $src; 561 549 } 562 550 } … … 573 561 574 562 foreach ( $items as $value ) { 575 if ( preg_match( '/(rel|itemprop)="([^"]+)"[^>]+href="([^"]+)"/', $value, $new_matches ) ) { 576 if ( 'alternate' === $new_matches[2] || 'thumbnailUrl' === $new_matches[2] || 'url' === $new_matches[2] ) { 577 $url = $this->_limit_url( $new_matches[3] ); 578 579 if ( ! empty( $url ) && empty( $data['_links'][ $new_matches[2] ] ) ) { 580 $data['_links'][ $new_matches[2] ] = $url; 581 } 563 if ( preg_match( '/rel=["\'](canonical|shortlink|icon)["\']/i', $value, $matches_rel ) && preg_match( '/href=[\'"]([^\'" ]+)[\'"]/i', $value, $matches_url ) ) { 564 $rel = $matches_rel[1]; 565 $url = $this->_limit_url( $matches_url[1] ); 566 567 if ( ! empty( $url ) && empty( $data['_links'][ $rel ] ) ) { 568 $data['_links'][ $rel ] = $url; 582 569 } 583 570 } … … 601 588 602 589 // Only instantiate the keys we want. Sanity check and sanitize each one. 603 foreach ( array( 'u', 's', 't', 'v' , '_version') as $key ) {590 foreach ( array( 'u', 's', 't', 'v' ) as $key ) { 604 591 if ( ! empty( $_POST[ $key ] ) ) { 605 592 $value = wp_unslash( $_POST[ $key ] ); … … 630 617 if ( apply_filters( 'enable_press_this_media_discovery', true ) ) { 631 618 /* 632 * If no title, _im g, _embed, and _meta was passed via $_POST, fetch data from source as fallback,619 * If no title, _images, _embed, and _meta was passed via $_POST, fetch data from source as fallback, 633 620 * making PT fully backward compatible with the older bookmarklet. 634 621 */ … … 636 623 $data = $this->source_data_fetch_fallback( $data['u'], $data ); 637 624 } else { 638 foreach ( array( '_im g', '_embed', '_meta' ) as $type ) {625 foreach ( array( '_images', '_embeds' ) as $type ) { 639 626 if ( empty( $_POST[ $type ] ) ) { 640 627 continue; … … 643 630 $data[ $type ] = array(); 644 631 $items = $this->_limit_array( $_POST[ $type ] ); 645 $items = wp_unslash( $items );646 632 647 633 foreach ( $items as $key => $value ) { 648 if ( ! is_numeric( $key ) ) { 649 $key = $this->_limit_string( wp_unslash( $key ) ); 650 651 // Sanity check. $key is usually things like 'title', 'description', 'keywords', etc. 652 if ( empty( $key ) || strlen( $key ) > 100 ) { 653 continue; 654 } 634 if ( $type === '_images' ) { 635 $value = $this->_limit_img( wp_unslash( $value ) ); 636 } else { 637 $value = $this->_limit_embed( wp_unslash( $value ) ); 655 638 } 656 639 640 if ( ! empty( $value ) ) { 641 $data[ $type ][] = $value; 642 } 643 } 644 } 645 646 foreach ( array( '_meta', '_links' ) as $type ) { 647 if ( empty( $_POST[ $type ] ) ) { 648 continue; 649 } 650 651 $data[ $type ] = array(); 652 $items = $this->_limit_array( $_POST[ $type ] ); 653 654 foreach ( $items as $key => $value ) { 655 // Sanity check. These are associative arrays, $key is usually things like 'title', 'description', 'keywords', etc. 656 if ( empty( $key ) || strlen( $key ) > 100 ) { 657 continue; 658 } 659 657 660 if ( $type === '_meta' ) { 658 $value = $this->_limit_string( $value);661 $value = $this->_limit_string( wp_unslash( $value ) ); 659 662 660 663 if ( ! empty( $value ) ) { 661 664 $data = $this->_process_meta_entry( $key, $value, $data ); 662 665 } 663 } else if ( $type === '_img' ) { 664 $value = $this->_limit_img( $value ); 665 666 if ( ! empty( $value ) ) { 667 $data[ $type ][] = $value; 668 } 669 } else if ( $type === '_embed' ) { 670 $value = $this->_limit_embed( $value ); 671 672 if ( ! empty( $value ) ) { 673 $data[ $type ][] = $value; 666 } else { 667 if ( in_array( $key, array( 'canonical', 'shortlink', 'icon' ), true ) ) { 668 $data[ $type ][ $key ] = $this->_limit_url( wp_unslash( $value ) ); 674 669 } 675 670 } … … 750 745 <?php 751 746 } 747 752 748 ?> 753 749 </fieldset> … … 852 848 853 849 /** 850 * Get a list of embeds with no duplicates. 851 * 852 * @param array $data The site's data. 853 * @returns array 854 */ 855 public function get_embeds( $data ) { 856 $selected_embeds = array(); 857 858 if ( ! empty( $data['_embeds'] ) ) { 859 foreach( $data['_embeds'] as $src ) { 860 $prot_relative_src = preg_replace( '/^https?:/', '', $src ); 861 862 if ( in_array( $prot_relative_src, $this->embeds ) ) { 863 continue; 864 } 865 866 $selected_embeds[] = $src; 867 $this->embeds[] = $prot_relative_src; 868 } 869 } 870 871 return $selected_embeds; 872 } 873 874 /** 875 * Get a list of images with no duplicates. 876 * 877 * @param array $data The site's data. 878 * @returns array 879 */ 880 public function get_images( $data ) { 881 $selected_images = array(); 882 883 if ( ! empty( $data['_images'] ) ) { 884 foreach( $data['_images'] as $src ) { 885 if ( false !== strpos( $src, 'gravatar.com' ) ) { 886 $src = preg_replace( '%http://[\d]+\.gravatar\.com/%', 'https://secure.gravatar.com/', $src ); 887 } 888 889 $prot_relative_src = preg_replace( '/^https?:/', '', $src ); 890 891 if ( in_array( $prot_relative_src, $this->images ) || 892 ( false !== strpos( $src, 'avatar' ) && count( $this->images ) > 15 ) ) { 893 // Skip: already selected or some type of avatar and we've already gathered more than 15 images. 894 continue; 895 } 896 897 $selected_images[] = $src; 898 $this->images[] = $prot_relative_src; 899 } 900 } 901 902 return $selected_images; 903 } 904 905 /** 906 * Gets the source page's canonical link, based on passed location and meta data. 907 * 908 * @param array $data The site's data. 909 * @returns string Discovered canonical URL, or empty 910 */ 911 public function get_canonical_link( $data ) { 912 $link = ''; 913 914 if ( ! empty( $data['_links']['canonical'] ) ) { 915 $link = $data['_links']['canonical']; 916 } elseif ( ! empty( $data['u'] ) ) { 917 $link = $data['u']; 918 } elseif ( ! empty( $data['_meta'] ) ) { 919 if ( ! empty( $data['_meta']['twitter:url'] ) ) { 920 $link = $data['_meta']['twitter:url']; 921 } else if ( ! empty( $data['_meta']['og:url'] ) ) { 922 $link = $data['_meta']['og:url']; 923 } 924 } 925 926 if ( empty( $link ) && ! empty( $data['_links']['shortlink'] ) ) { 927 $link = $data['_links']['shortlink']; 928 } 929 930 return $link; 931 } 932 933 /** 934 * Gets the source page's site name, based on passed meta data. 935 * 936 * @param array $data The site's data. 937 * @returns string Discovered site name, or empty 938 */ 939 public function get_source_site_name( $data ) { 940 $name = ''; 941 942 if ( ! empty( $data['_meta'] ) ) { 943 if ( ! empty( $data['_meta']['og:site_name'] ) ) { 944 $name = $data['_meta']['og:site_name']; 945 } else if ( ! empty( $data['_meta']['application-name'] ) ) { 946 $name = $data['_meta']['application-name']; 947 } 948 } 949 950 return $name; 951 } 952 953 /** 954 * Gets the source page's title, based on passed title and meta data. 955 * 956 * @param array $data The site's data. 957 * @returns string Discovered page title, or empty 958 */ 959 public function get_suggested_title( $data ) { 960 $title = ''; 961 962 if ( ! empty( $data['t'] ) ) { 963 $title = $data['t']; 964 } elseif( ! empty( $data['_meta'] ) ) { 965 if ( ! empty( $data['_meta']['twitter:title'] ) ) { 966 $title = $data['_meta']['twitter:title']; 967 } else if ( ! empty( $data['_meta']['og:title'] ) ) { 968 $title = $data['_meta']['og:title']; 969 } else if ( ! empty( $data['_meta']['title'] ) ) { 970 $title = $data['_meta']['title']; 971 } 972 } 973 974 return $title; 975 } 976 977 /** 978 * Gets the source page's suggested content, based on passed data (description, selection, etc). 979 * Features a blockquoted excerpt, as well as content attribution, if any. 980 * 981 * @param array $data The site's data. 982 * @returns string Discovered content, or empty 983 */ 984 public function get_suggested_content( $data ) { 985 $content = $text = ''; 986 987 if ( ! empty( $data['s'] ) ) { 988 $text = $data['s']; 989 } else if ( ! empty( $data['_meta'] ) ) { 990 if ( ! empty( $data['_meta']['twitter:description'] ) ) { 991 $text = $data['_meta']['twitter:description']; 992 } else if ( ! empty( $data['_meta']['og:description'] ) ) { 993 $text = $data['_meta']['og:description']; 994 } else if ( ! empty( $data['_meta']['description'] ) ) { 995 $text = $data['_meta']['description']; 996 } 997 } 998 999 $default_html = array( 1000 'quote' => '<blockquote>%1$s</blockquote>', 1001 'link' => '<p>' . _x( 'Source:', 'Used in Press This to indicate where the content comes from.' ) . 1002 ' <em><a href="%1$s">%2$s</a></em></p>', 1003 ); 1004 1005 /** 1006 * Filter the default HTML for the Press This editor. 1007 * 1008 * @since 4.2.0 1009 * 1010 * @param array $default_html Associative array with two keys: 'quote' where %1$s is replaced with the site description 1011 * or the selected content, and 'link' there %1$s is link href, %2$s is link text. 1012 */ 1013 $default_html = apply_filters( 'press_this_suggested_html', $default_html, $data ); 1014 1015 // Wrap suggested content in the specified HTML. 1016 if ( ! empty( $default_html['quote'] ) ) { 1017 $content = sprintf( $default_html['quote'], $text ); 1018 } 1019 1020 // Add source attribution if there is one available. 1021 if ( ! empty( $default_html['link'] ) ) { 1022 $title = $this->get_suggested_title( $data ); 1023 $url = $this->get_canonical_link( $data ); 1024 1025 if ( ! $title ) { 1026 $title = $this->get_source_site_name( $data ); 1027 } 1028 1029 if ( $url && $title ) { 1030 $content .= sprintf( $default_html['link'], $url, $title ); 1031 } 1032 } 1033 1034 return $content; 1035 } 1036 1037 /** 854 1038 * Serves the app's base HTML, which in turns calls the load script. 855 1039 * … … 863 1047 $data = $this->merge_or_fetch_data(); 864 1048 1049 $post_title = $this->get_suggested_title( $data ); 1050 1051 if ( empty( $title ) ) { 1052 $title = __( 'New Post' ); 1053 } 1054 1055 $post_content = $this->get_suggested_content( $data ); 1056 865 1057 // Get site settings array/data. 866 1058 $site_settings = $this->site_settings(); 867 1059 868 // Set the passed data. 869 $data['_version'] = $site_settings['version']; 1060 // Pass the images and embeds 1061 $images = $this->get_images( $data ); 1062 $embeds = $this->get_embeds( $data ); 1063 1064 $site_data = array( 1065 'v' => ! empty( $data['v'] ) ? $data['v'] : '', 1066 'hasData' => ! empty( $data ), 1067 ); 1068 1069 if ( ! empty( $images ) ) { 1070 $site_data['_images'] = $images; 1071 } 1072 1073 if ( ! empty( $embeds ) ) { 1074 $site_data['_embeds'] = $embeds; 1075 } 870 1076 871 1077 // Add press-this-editor.css and remove theme's editor-style.css, if any. … … 891 1097 892 1098 <script> 893 window.wpPressThisData = <?php echo wp_json_encode( $ data )?>;894 window.wpPressThisConfig = <?php echo wp_json_encode( $site_settings ) ?>;1099 window.wpPressThisData = <?php echo wp_json_encode( $site_data ); ?>; 1100 window.wpPressThisConfig = <?php echo wp_json_encode( $site_settings ); ?>; 895 1101 </script> 896 1102 … … 960 1166 $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' ); 961 1167 $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) ); 962 1168 963 1169 /** This filter is documented in wp-admin/admin-header.php */ 964 1170 $admin_body_classes = apply_filters( 'admin_body_class', '' ); … … 1008 1214 <div id='app-container' class="editor"> 1009 1215 <span id="title-container-label" class="post-title-placeholder" aria-hidden="true"><?php _e( 'Post title' ); ?></span> 1010 <h2 id="title-container" class="post-title" contenteditable="true" spellcheck="true" aria-label="<?php esc_attr_e( 'Post title' ); ?>" tabindex="0">< /h2>1216 <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> 1011 1217 <div id='featured-media-container' class="featured-container no-media"> 1012 1218 <div id='all-media-widget' class="all-media"> … … 1016 1222 1017 1223 <?php 1018 wp_editor( '', 'pressthis', array(1224 wp_editor( $post_content, 'pressthis', array( 1019 1225 'drag_drop_upload' => true, 1020 1226 'editor_height' => 600, … … 1040 1246 </div> 1041 1247 1042 <div class="options-panel-back is-hidden" tabindex="-1"></div> 1248 <div class="options-panel-back is-hidden" tabindex="-1"></div> 1043 1249 <div class="options-panel is-off-screen is-hidden" tabindex="-1"> 1044 1250 <div class="post-options">
Note: See TracChangeset
for help on using the changeset viewer.