Make WordPress Core

Changeset 31693


Ignore:
Timestamp:
03/09/2015 09:48:38 PM (12 years ago)
Author:
azaozz
Message:

PressThis:

  • Filter and select the content on the PHP side. Then pass only the needed data to JS.
  • Add the suggested post title and contend directly to the HTML.
  • Standardise the data type names.
  • Some cleanup/reduction of the code in the bookmarklet.

See #31373.

Location:
trunk/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/css/press-this.css

    r31680 r31693  
    13891389}
    13901390
     1391/* Make the text inside the editor textarea white. Prevents a "flash" on loading the page */
     1392#pressthis {
     1393        color: #fff;
     1394}
     1395
    13911396@media (min-width: 901px) {
    13921397        .editor {
  • trunk/src/wp-admin/includes/class-wp-press-this.php

    r31655 r31693  
    1515class WP_Press_This {
    1616
     17        private $images = array();
     18
     19        private $embeds = array();
     20
    1721        /**
    1822         * Constructor.
    … …  
    3236         */
    3337        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 
    4038                return array(
    4139                        // Used to trigger the bookmarklet update notice.
    4240                        // Needs to be set here and in get_shortcut_link() in wp-includes/link-template.php.
    43                         'version' => '6',
     41                        'version' => '7',
    4442
    4543                        /**
    … …  
    5149                         */
    5250                        '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.0
    58                          *
    59                          * @param array $default_html Associative array with two keys: 'quote' where %1$s is replaced with the site description
    60                          *                            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 ),
    6351                );
    6452        }
    … …  
    436424
    437425        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 ) ) {
    439427                        $data['_meta'][ $meta_name ] = $meta_value;
    440428                } else {
    … …  
    445433                                        $meta_value = $this->_limit_embed( $meta_value );
    446434
    447                                         if ( ! isset( $data['_embed'] ) ) {
    448                                                 $data['_embed'] = array();
     435                                        if ( ! isset( $data['_embeds'] ) ) {
     436                                                $data['_embeds'] = array();
    449437                                        }
    450438
    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;
    453441                                        }
    454442
    … …  
    462450                                        $meta_value = $this->_limit_img( $meta_value );
    463451
    464                                         if ( ! isset( $data['_img'] ) ) {
    465                                                 $data['_img'] = array();
     452                                        if ( ! isset( $data['_images'] ) ) {
     453                                                $data['_images'] = array();
    466454                                        }
    467455
    468                                         if ( ! empty( $meta_value ) && ! in_array( $meta_value, $data['_img'] ) ) {
    469                                                 $data['_img'][] = $meta_value;
     456                                        if ( ! empty( $meta_value ) && ! in_array( $meta_value, $data['_images'] ) ) {
     457                                                $data['_images'][] = $meta_value;
    470458                                        }
    471459
    … …  
    478466
    479467        /**
    480          * Fetches and parses _meta, _img, and _links data from the source.
     468         * Fetches and parses _meta, _images, and _links data from the source.
    481469         *
    482470         * @since 4.2.0
    … …  
    522510
    523511                // Fetch and gather <img> data.
    524                 if ( empty( $data['_img'] ) ) {
    525                         $data['_img'] = array();
     512                if ( empty( $data['_images'] ) ) {
     513                        $data['_images'] = array();
    526514                }
    527515
    … …  
    538526                                if ( preg_match( '/src=(\'|")([^\'"]+)\\1/i', $value, $new_matches ) ) {
    539527                                        $src = $this->_limit_img( $new_matches[2] );
    540                                         if ( ! empty( $src ) && ! in_array( $src, $data['_img'] ) ) {
    541                                                 $data['_img'][] = $src;
     528                                        if ( ! empty( $src ) && ! in_array( $src, $data['_images'] ) ) {
     529                                                $data['_images'][] = $src;
    542530                                        }
    543531                                }
    … …  
    546534
    547535                // Fetch and gather <iframe> data.
    548                 if ( empty( $data['_embed'] ) ) {
    549                         $data['_embed'] = array();
     536                if ( empty( $data['_embeds'] ) ) {
     537                        $data['_embeds'] = array();
    550538                }
    551539
    … …  
    557545                                        $src = $this->_limit_embed( $new_matches[2] );
    558546
    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;
    561549                                        }
    562550                                }
    … …  
    573561
    574562                        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;
    582569                                        }
    583570                                }
    … …  
    601588
    602589                // 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 ) {
    604591                        if ( ! empty( $_POST[ $key ] ) ) {
    605592                                $value = wp_unslash( $_POST[ $key ] );
    … …  
    630617                if ( apply_filters( 'enable_press_this_media_discovery', true ) ) {
    631618                        /*
    632                          * If no title, _img, _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,
    633620                         * making PT fully backward compatible with the older bookmarklet.
    634621                         */
    … …  
    636623                                $data = $this->source_data_fetch_fallback( $data['u'], $data );
    637624                        } else {
    638                                 foreach ( array( '_img', '_embed', '_meta' ) as $type ) {
     625                                foreach ( array( '_images', '_embeds' ) as $type ) {
    639626                                        if ( empty( $_POST[ $type ] ) ) {
    640627                                                continue;
    … …  
    643630                                        $data[ $type ] = array();
    644631                                        $items = $this->_limit_array( $_POST[ $type ] );
    645                                         $items = wp_unslash( $items );
    646632
    647633                                        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 ) );
    655638                                                }
    656639
     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
    657660                                                if ( $type === '_meta' ) {
    658                                                         $value = $this->_limit_string( $value );
     661                                                        $value = $this->_limit_string( wp_unslash( $value ) );
    659662
    660663                                                        if ( ! empty( $value ) ) {
    661664                                                                $data = $this->_process_meta_entry( $key, $value, $data );
    662665                                                        }
    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 ) );
    674669                                                        }
    675670                                                }
    … …  
    750745                                                <?php
    751746                                         }
     747
    752748                                         ?>
    753749                                </fieldset>
    … …  
    852848
    853849        /**
     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        /**
    8541038         * Serves the app's base HTML, which in turns calls the load script.
    8551039         *
    … …  
    8631047                $data = $this->merge_or_fetch_data();
    8641048
     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
    8651057                // Get site settings array/data.
    8661058                $site_settings = $this->site_settings();
    8671059
    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                }
    8701076
    8711077                // Add press-this-editor.css and remove theme's editor-style.css, if any.
    … …  
    8911097
    8921098        <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 ); ?>;
    8951101        </script>
    8961102
    … …  
    9601166        $admin_body_class .= ' admin-color-' . sanitize_html_class( get_user_option( 'admin_color' ), 'fresh' );
    9611167        $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
    962        
     1168
    9631169        /** This filter is documented in wp-admin/admin-header.php */
    9641170        $admin_body_classes = apply_filters( 'admin_body_class', '' );
    … …  
    10081214                        <div id='app-container' class="editor">
    10091215                                <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>
    10111217                                <div id='featured-media-container' class="featured-container no-media">
    10121218                                        <div id='all-media-widget' class="all-media">
    … …  
    10161222
    10171223                                <?php
    1018                                 wp_editor( '', 'pressthis', array(
     1224                                wp_editor( $post_content, 'pressthis', array(
    10191225                                        'drag_drop_upload' => true,
    10201226                                        'editor_height'    => 600,
    … …  
    10401246                </div>
    10411247
    1042                 <div class="options-panel-back is-hidden" tabindex="-1"></div> 
     1248                <div class="options-panel-back is-hidden" tabindex="-1"></div>
    10431249                <div class="options-panel is-off-screen is-hidden" tabindex="-1">
    10441250                        <div class="post-options">
  • trunk/src/wp-admin/js/bookmarklet.js

    r31609 r31693  
    3232        }
    3333
    34         pt_url += ( pt_url.indexOf( '?' ) > -1 ? '&' : '?' ) + 'buster=' + ( new Date().getTime() );
     34        pt_url += '&buster=' + ( new Date().getTime() );
    3535
    3636        if ( ! canPost ) {
    … …  
    6969        }
    7070
    71         if ( href.match( /\/\/www\.youtube\.com\/watch/ ) ) {
    72                 add( '_embed[]', href );
    73         } else if ( href.match( /\/\/vimeo\.com\/(.+\/)?([\d]+)$/ ) ) {
    74                 add( '_embed[]', href );
    75         } else if ( href.match( /\/\/(www\.)?dailymotion\.com\/video\/.+$/ ) ) {
    76                 add( '_embed[]', href );
    77         } else if ( href.match( /\/\/soundcloud\.com\/.+$/ ) ) {
    78                 add( '_embed[]', href );
    79         } else if ( href.match( /\/\/twitter\.com\/[^\/]+\/status\/[\d]+$/ ) ) {
    80                 add( '_embed[]', href );
    81         } else if ( href.match( /\/\/vine\.co\/v\/[^\/]+/ ) ) {
    82                 add( '_embed[]', href );
     71        if ( href.match( /\/\/www\.youtube\.com\/watch/ ) ||
     72                href.match( /\/\/vimeo\.com\/(.+\/)?([\d]+)$/ ) ||
     73                href.match( /\/\/(www\.)?dailymotion\.com\/video\/.+$/ ) ||
     74                href.match( /\/\/soundcloud\.com\/.+$/ ) ||
     75                href.match( /\/\/twitter\.com\/[^\/]+\/status\/[\d]+$/ ) ||
     76                href.match( /\/\/vine\.co\/v\/[^\/]+/ ) ) {
     77
     78                add( '_embeds[]', href );
    8379        }
    8480
    … …  
    112108                        g_rel = g.getAttribute( 'rel' );
    113109
    114                 if ( g_rel ) {
    115                         switch ( g_rel ) {
    116                                 case 'canonical':
    117                                 case 'icon':
    118                                 case 'shortlink':
    119                                         add( '_links[' + g_rel + ']', g.getAttribute( 'href' ) );
    120                                         break;
    121                                 case 'alternate':
    122                                         if ( 'application/json+oembed' === g.getAttribute( 'type' ) ) {
    123                                                 add( '_links[' + g_rel + ']', g.getAttribute( 'href' ) );
    124                                         } else if ( 'handheld' === g.getAttribute( 'media' ) ) {
    125                                                 add( '_links[' + g_rel + ']', g.getAttribute( 'href' ) );
    126                                         }
    127                         }
     110                if ( g_rel === 'canonical' || g_rel === 'icon' || g_rel === 'shortlink' ) {
     111                        add( '_links[' + g_rel + ']', g.getAttribute( 'href' ) );
    128112                }
    129113        }
    … …  
    148132
    149133                if ( img.width >= 256 && img.height >= 128 ) {
    150                         add( '_img[]', img.src );
     134                        add( '_images[]', img.src );
    151135                }
    152136        }
    … …  
    159143                }
    160144
    161                 add( '_embed[]', ifrs[ p ].src );
     145                add( '_embeds[]', ifrs[ p ].src );
    162146        }
    163147
  • trunk/src/wp-admin/js/bookmarklet.min.js

    r31609 r31693  
    1 (function(a,b,c,d){function e(a,c){if("undefined"!=typeof c){var d=b.createElement("input");d.name=a,d.value=c,d.type="hidden",o.appendChild(d)}}var f,g,h,i,j,k,l,m,n=a.encodeURIComponent,o=b.createElement("form"),p=b.getElementsByTagName("head")[0],q=new Image,r="_press_this_app",s=!0;if(d){if(!c.match(/^https?:/))return void(top.location.href=d);if(d+="&u="+n(c),c.match(/^https:/)&&d.match(/^http:/)&&(s=!1),a.getSelection?m=a.getSelection()+"":b.getSelection?m=b.getSelection()+"":b.selection&&(m=b.selection.createRange().text||""),d+=(d.indexOf("?")>-1?"&":"?")+"buster="+(new Date).getTime(),s||(b.title&&(d+="&t="+n(b.title.substr(0,256))),m&&(d+="&s="+n(m.substr(0,512)))),f=a.outerWidth||b.documentElement.clientWidth||600,g=a.outerHeight||b.documentElement.clientHeight||700,f=800>f||f>5e3?600:.7*f,g=800>g||g>3e3?700:.9*g,!s)return void a.open(d,r,"location,resizable,scrollbars,width="+f+",height="+g);c.match(/\/\/www\.youtube\.com\/watch/)?e("_embed[]",c):c.match(/\/\/vimeo\.com\/(.+\/)?([\d]+)$/)?e("_embed[]",c):c.match(/\/\/(www\.)?dailymotion\.com\/video\/.+$/)?e("_embed[]",c):c.match(/\/\/soundcloud\.com\/.+$/)?e("_embed[]",c):c.match(/\/\/twitter\.com\/[^\/]+\/status\/[\d]+$/)?e("_embed[]",c):c.match(/\/\/vine\.co\/v\/[^\/]+/)&&e("_embed[]",c),h=p.getElementsByTagName("meta")||[];for(var t=0;t<h.length&&!(t>=50);t++){var u=h[t],v=u.getAttribute("name"),w=u.getAttribute("property"),x=u.getAttribute("content");v?e("_meta["+v+"]",x):w&&e("_meta["+w+"]",x)}i=p.getElementsByTagName("link")||[];for(var y=0;y<i.length&&!(y>=50);y++){var z=i[y],A=z.getAttribute("rel");if(A)switch(A){case"canonical":case"icon":case"shortlink":e("_links["+A+"]",z.getAttribute("href"));break;case"alternate":"application/json+oembed"===z.getAttribute("type")?e("_links["+A+"]",z.getAttribute("href")):"handheld"===z.getAttribute("media")&&e("_links["+A+"]",z.getAttribute("href"))}}b.body.getElementsByClassName&&(j=b.body.getElementsByClassName("hfeed")[0]),j=b.getElementById("content")||j||b.body,k=j.getElementsByTagName("img")||[];for(var B=0;B<k.length&&!(B>=50);B++)k[B].src.indexOf("avatar")>-1||k[B].className.indexOf("avatar")>-1||(q.src=k[B].src,q.width>=256&&q.height>=128&&e("_img[]",q.src));l=b.body.getElementsByTagName("iframe")||[];for(var C=0;C<l.length&&!(C>=50);C++)e("_embed[]",l[C].src);b.title&&e("t",b.title),m&&e("s",m),o.setAttribute("method","POST"),o.setAttribute("action",d),o.setAttribute("target",r),o.setAttribute("style","display: none;"),a.open("about:blank",r,"location,resizable,scrollbars,width="+f+",height="+g),b.body.appendChild(o),o.submit()}})(window,document,top.location.href,window.pt_url);
     1(function(a,b,c,d){function e(a,c){if("undefined"!=typeof c){var d=b.createElement("input");d.name=a,d.value=c,d.type="hidden",o.appendChild(d)}}var f,g,h,i,j,k,l,m,n=a.encodeURIComponent,o=b.createElement("form"),p=b.getElementsByTagName("head")[0],q=new Image,r="_press_this_app",s=!0;if(d){if(!c.match(/^https?:/))return void(top.location.href=d);if(d+="&u="+n(c),c.match(/^https:/)&&d.match(/^http:/)&&(s=!1),a.getSelection?m=a.getSelection()+"":b.getSelection?m=b.getSelection()+"":b.selection&&(m=b.selection.createRange().text||""),d+="&buster="+(new Date).getTime(),s||(b.title&&(d+="&t="+n(b.title.substr(0,256))),m&&(d+="&s="+n(m.substr(0,512)))),f=a.outerWidth||b.documentElement.clientWidth||600,g=a.outerHeight||b.documentElement.clientHeight||700,f=800>f||f>5e3?600:.7*f,g=800>g||g>3e3?700:.9*g,!s)return void a.open(d,r,"location,resizable,scrollbars,width="+f+",height="+g);(c.match(/\/\/www\.youtube\.com\/watch/)||c.match(/\/\/vimeo\.com\/(.+\/)?([\d]+)$/)||c.match(/\/\/(www\.)?dailymotion\.com\/video\/.+$/)||c.match(/\/\/soundcloud\.com\/.+$/)||c.match(/\/\/twitter\.com\/[^\/]+\/status\/[\d]+$/)||c.match(/\/\/vine\.co\/v\/[^\/]+/))&&e("_embeds[]",c),h=p.getElementsByTagName("meta")||[];for(var t=0;t<h.length&&!(t>=50);t++){var u=h[t],v=u.getAttribute("name"),w=u.getAttribute("property"),x=u.getAttribute("content");v?e("_meta["+v+"]",x):w&&e("_meta["+w+"]",x)}i=p.getElementsByTagName("link")||[];for(var y=0;y<i.length&&!(y>=50);y++){var z=i[y],A=z.getAttribute("rel");("canonical"===A||"icon"===A||"shortlink"===A)&&e("_links["+A+"]",z.getAttribute("href"))}b.body.getElementsByClassName&&(j=b.body.getElementsByClassName("hfeed")[0]),j=b.getElementById("content")||j||b.body,k=j.getElementsByTagName("img")||[];for(var B=0;B<k.length&&!(B>=50);B++)k[B].src.indexOf("avatar")>-1||k[B].className.indexOf("avatar")>-1||(q.src=k[B].src,q.width>=256&&q.height>=128&&e("_images[]",q.src));l=b.body.getElementsByTagName("iframe")||[];for(var C=0;C<l.length&&!(C>=50);C++)e("_embeds[]",l[C].src);b.title&&e("t",b.title),m&&e("s",m),o.setAttribute("method","POST"),o.setAttribute("action",d),o.setAttribute("target",r),o.setAttribute("style","display: none;"),a.open("about:blank",r,"location,resizable,scrollbars,width="+f+",height="+g),b.body.appendChild(o),o.submit()}})(window,document,top.location.href,window.pt_url);
  • trunk/src/wp-admin/js/press-this.js

    r31677 r31693  
    99                        textarea              = document.createElement( 'textarea' ),
    1010                        sidebarIsOpen         = false,
    11                         siteConfig            = window.wpPressThisConfig || {},
     11                        settings              = window.wpPressThisConfig || {},
    1212                        data                  = window.wpPressThisData || {},
    1313                        smallestWidth         = 128,
    14                         interestingImages         = getInterestingImages( data ) || [],
    15                         interestingEmbeds         = getInterestingEmbeds( data ) || [],
    16                         hasEmptyTitleStr      = false,
    17                         suggestedTitleStr     = getSuggestedTitle( data ),
    18                         suggestedContentStr   = getSuggestedContent( data ),
    1914                        hasSetFocus           = false,
    2015                        catsCache             = [],
    … …  
    7671                 */
    7772                function sanitizeText( text ) {
    78                         text = stripTags( text );
    79                         textarea.innerHTML = text;
    80 
    81                         return stripTags( textarea.value );
     73                        var _text = stripTags( text );
     74
     75                        try {
     76                                textarea.innerHTML = _text;
     77                                _text = stripTags( textarea.value );
     78                        } catch ( er ) {}
     79
     80                        return _text;
    8281                }
    8382
    … …  
    9796
    9897                        return '';
    99                 }
    100 
    101                 /**
    102                  * Gets the source page's canonical link, based on passed location and meta data.
    103                  *
    104                  * @returns string Discovered canonical URL, or empty
    105                  */
    106                 function getCanonicalLink() {
    107                         var link = '';
    108 
    109                         if ( data._links && data._links.canonical ) {
    110                                 link = data._links.canonical;
    111                         }
    112 
    113                         if ( ! link && data.u ) {
    114                                 link = data.u;
    115                         }
    116 
    117                         if ( ! link && data._meta ) {
    118                                 if ( data._meta['twitter:url'] ) {
    119                                         link = data._meta['twitter:url'];
    120                                 } else if ( data._meta['og:url'] ) {
    121                                         link = data._meta['og:url'];
    122                                 }
    123                         }
    124 
    125                         return checkUrl( decodeURI( link ) );
    126                 }
    127 
    128                 /**
    129                  * Gets the source page's site name, based on passed meta data.
    130                  *
    131                  * @returns string Discovered site name, or empty
    132                  */
    133                 function getSourceSiteName() {
    134                         var name = '';
    135 
    136                         if ( data._meta ) {
    137                                 if ( data._meta['og:site_name'] ) {
    138                                         name = data._meta['og:site_name'];
    139                                 } else if ( data._meta['application-name'] ) {
    140                                         name = data._meta['application-name'];
    141                                 }
    142                         }
    143 
    144                         return sanitizeText( name );
    145                 }
    146 
    147                 /**
    148                  * Gets the source page's title, based on passed title and meta data.
    149                  *
    150                  * @returns string Discovered page title, or empty
    151                  */
    152                 function getSuggestedTitle() {
    153                         var title = '';
    154 
    155                         if ( data.t ) {
    156                                 title = data.t;
    157                         }
    158 
    159                         if ( ! title && data._meta ) {
    160                                 if ( data._meta['twitter:title'] ) {
    161                                         title = data._meta['twitter:title'];
    162                                 } else if ( data._meta['og:title'] ) {
    163                                         title = data._meta['og:title'];
    164                                 } else if ( data._meta.title ) {
    165                                         title = data._meta.title;
    166                                 }
    167                         }
    168 
    169                         if ( ! title ) {
    170                                 title = __( 'newPost' );
    171                                 hasEmptyTitleStr = true;
    172                         }
    173 
    174                         return sanitizeText( title );
    175                 }
    176 
    177                 /**
    178                  * Gets the source page's suggested content, based on passed data (description, selection, etc).
    179                  * Features a blockquoted excerpt, as well as content attribution, if any.
    180                  *
    181                  * @returns string Discovered content, or empty
    182                  */
    183                 function getSuggestedContent() {
    184                         var content  = '',
    185                                 text     = '',
    186                                 title    = getSuggestedTitle(),
    187                                 url      = getCanonicalLink(),
    188                                 siteName = getSourceSiteName();
    189 
    190                         if ( data.s ) {
    191                                 text = data.s;
    192                         } else if ( data._meta ) {
    193                                 if ( data._meta['twitter:description'] ) {
    194                                         text = data._meta['twitter:description'];
    195                                 } else if ( data._meta['og:description'] ) {
    196                                         text = data._meta['og:description'];
    197                                 } else if ( data._meta.description ) {
    198                                         text = data._meta.description;
    199                                 }
    200                         }
    201 
    202                         if ( text && siteConfig.html.quote ) {
    203                                 // Wrap suggested content in specified HTML.
    204                                 content = siteConfig.html.quote.replace( /%1\$s/g, sanitizeText( text ) );
    205                         }
    206 
    207                         // Add a source attribution if there is one available.
    208                         if ( url && siteConfig.html.link && ( ( title && __( 'newPost' ) !== title ) || siteName ) ) {
    209                                 content += siteConfig.html.link.replace( /%1\$s/g, encodeURI( url ) ).replace( /%2\$s/g, ( title || siteName ) );
    210                         }
    211 
    212                         return content || '';
    213                 }
    214 
    215                 /**
    216                  * Get a list of valid embeds from what was passed via WpPressThis_App.data._embed on page load.
    217                  *
    218                  * @returns array
    219                  */
    220                 function getInterestingEmbeds() {
    221                         var embeds             = data._embed || [],
    222                                 interestingEmbeds  = [],
    223                                 alreadySelected    = [];
    224 
    225                         if ( embeds.length ) {
    226                                 $.each( embeds, function ( i, src ) {
    227                                         if ( ! src ) {
    228                                                 // Skip: no src value
    229                                                 return;
    230                                         }
    231 
    232                                         var schemelessSrc = src.replace( /^https?:/, '' );
    233 
    234                                         if ( $.inArray( schemelessSrc, alreadySelected ) > -1 ) {
    235                                                 // Skip: already shown
    236                                                 return;
    237                                         }
    238 
    239                                         interestingEmbeds.push( src );
    240                                         alreadySelected.push( schemelessSrc );
    241                                 } );
    242                         }
    243 
    244                         return interestingEmbeds;
    245                 }
    246 
    247                 /**
    248                  * Get a list of valid images from what was passed via WpPressThis_App.data._img and WpPressThis_App.data._meta on page load.
    249                  *
    250                  * @returns array
    251                  */
    252                 function getInterestingImages( data ) {
    253                         var imgs             = data._img || [],
    254                                 interestingImgs  = [],
    255                                 alreadySelected  = [];
    256 
    257                         if ( imgs.length ) {
    258                                 $.each( imgs, function ( i, src ) {
    259                                         src = src.replace( /http:\/\/[\d]+\.gravatar\.com\//, 'https://secure.gravatar.com/' );
    260                                         src = checkUrl( src );
    261 
    262                                         if ( ! src ) {
    263                                                 // Skip: no src value
    264                                                 return;
    265                                         }
    266 
    267                                         var schemelessSrc = src.replace( /^https?:/, '' );
    268 
    269                                         if ( Array.prototype.indexOf && alreadySelected.indexOf( schemelessSrc ) > -1 ) {
    270                                                 // Skip: already shown
    271                                                 return;
    272                                         } else if ( src.indexOf( 'avatar' ) > -1 && interestingImgs.length >= 15 ) {
    273                                                 // Skip:  some type of avatar and we've already gathered more than 23 diff images to show
    274                                                 return;
    275                                         }
    276 
    277                                         interestingImgs.push( src );
    278                                         alreadySelected.push( schemelessSrc );
    279                                 } );
    280                         }
    281 
    282                         return interestingImgs;
    28398                }
    28499
    … …  
    346161                                                hideSpinner();
    347162                                        } else if ( response.data.redirect ) {
    348                                                 if ( window.opener && siteConfig.redirInParent ) {
     163                                                if ( window.opener && settings.redirInParent ) {
    349164                                                        try {
    350165                                                                window.opener.location.href = response.data.redirect;
    … …  
    457272                 */
    458273                function renderToolsVisibility() {
    459                         if ( data.u && data.u.match( /^https?:/ ) ) {
     274                        if ( data.hasData ) {
    460275                                $( '#scanbar' ).hide();
    461276                        }
    … …  
    496311
    497312                        // Prompt user to upgrade their bookmarklet if there is a version mismatch.
    498                         if ( data.v && data._version && ( data.v + '' ) !== ( data._version + '' ) ) {
     313                        if ( data.v && settings.version && ( data.v + '' ) !== ( settings.version + '' ) ) {
    499314                                $( '.should-upgrade-bookmarklet' ).removeClass( 'is-hidden' );
    500                         }
    501                 }
    502 
    503                 /**
    504                  * Render the suggested title, if any
    505                  */
    506                 function renderSuggestedTitle() {
    507                         var suggestedTitle = suggestedTitleStr || '',
    508                                 $title = $( '#title-container' );
    509 
    510                         if ( ! hasEmptyTitleStr ) {
    511                                 $( '#post_title' ).val( suggestedTitle );
    512                                 $title.text( suggestedTitle );
    513                                 $( '.post-title-placeholder' ).addClass( 'is-hidden' );
    514                         }
    515 
    516                         $title.on( 'keyup', function() {
    517                                 saveAlert = true;
    518                         }).on( 'paste', function() {
    519                                 saveAlert = true;
    520 
    521                                 setTimeout( function() {
    522                                         $title.text( $title.text() );
    523                                 }, 100 );
    524                         } );
    525 
    526                 }
    527 
    528                 /**
    529                  * Render the suggested content, if any
    530                  */
    531                 function renderSuggestedContent() {
    532                         if ( ! suggestedContentStr ) {
    533                                 return;
    534                         }
    535 
    536                         if ( ! editor ) {
    537                                 editor = window.tinymce.get( 'pressthis' );
    538                         }
    539 
    540                         if ( editor ) {
    541                                 editor.setContent( suggestedContentStr );
    542                                 editor.on( 'focus', function() {
    543                                         hasSetFocus = true;
    544                                 } );
    545315                        }
    546316                }
    … …  
    556326                        listContainer.empty();
    557327
    558                         if ( interestingEmbeds || interestingImages ) {
    559                                 listContainer.append( '<h2 class="screen-reader-text">' + __( 'allMediaHeading' ) + '</h2><ul class="wppt-all-media-list"/>' );
    560                         }
    561 
    562                         if ( interestingEmbeds ) {
    563                                 $.each( interestingEmbeds, function ( i, src ) {
     328                        if ( data._embeds || data._images ) {
     329                                listContainer.append( '<h2 class="screen-reader-text">' + __( 'allMediaHeading' ) + '</h2><ul class="wppt-all-media-list" />' );
     330                        }
     331
     332                        if ( data._embeds ) {
     333                                $.each( data._embeds, function ( i, src ) {
    564334                                        src = checkUrl( src );
    565335
    … …  
    602372                        }
    603373
    604                         if ( interestingImages ) {
    605                                 $.each( interestingImages, function ( i, src ) {
     374                        if ( data._images ) {
     375                                $.each( data._images, function( i, src ) {
    606376                                        src = checkUrl( src );
    607377
    … …  
    717487                                        $( '.post-options' ).removeClass( offscreenHidden );
    718488                                        $( '.setting-modal').addClass( offscreenHidden );
    719                                 } );
     489                                });
    720490                }
    721491
    … …  
    724494                 */
    725495                function monitorPlaceholder() {
    726                         var $selector = $( '#title-container'),
     496                        var $titleField = $( '#title-container'),
    727497                                $placeholder = $('.post-title-placeholder');
    728498
    729                         $selector.on( 'focus', function() {
     499                        $titleField.on( 'focus', function() {
    730500                                $placeholder.addClass('is-hidden');
    731                         } );
    732 
    733                         $selector.on( 'blur', function() {
    734                                 if ( ! $( this ).text() ) {
     501                        }).on( 'blur', function() {
     502                                if ( ! $titleField.text() ) {
    735503                                        $placeholder.removeClass('is-hidden');
    736504                                }
    737                         } );
     505                        });
     506
     507                        if ( $titleField.text() ) {
     508                                $placeholder.addClass('is-hidden');
     509                        }
    738510                }
    739511
    … …  
    748520                        // We're on!
    749521                        renderToolsVisibility();
    750                         renderSuggestedTitle();
    751522                        renderDetectedMedia();
    752                         $( document ).on( 'tinymce-editor-init', renderSuggestedContent );
    753523                        renderStartupNotices();
    754524
    … …  
    762532                 */
    763533                function monitor(){
     534                        $( document ).on( 'tinymce-editor-init', function( event, ed ) {
     535                                editor = ed;
     536
     537                                ed.on( 'focus', function() {
     538                                        hasSetFocus = true;
     539                                } );
     540                        });
     541
    764542                        $( '#current-site a').click( function( e ) {
    765543                                e.preventDefault();
    766544                        } );
    767545
    768                         // Publish and Draft buttons and submit
    769 
     546                        // Publish, Draft and Preview buttons
    770547
    771548                        $( '.post-actions' ).on( 'click.press-this', function( event ) {
    … …  
    877654                });
    878655
    879                 // Expose public methods
    880                 // TODO: which are needed?
     656                // Expose public methods?
    881657                return {
    882658                        renderNotice: renderNotice,
  • trunk/src/wp-includes/link-template.php

    r31617 r31693  
    26012601        global $is_IE, $wp_version;
    26022602
    2603         $bookmarklet_version = '6';
     2603        $bookmarklet_version = '7';
    26042604        $link = '';
    26052605
Note: See TracChangeset for help on using the changeset viewer.