Make WordPress Core

Changeset 31609


Ignore:
Timestamp:
03/04/2015 07:28:53 PM (12 years ago)
Author:
azaozz
Message:

PressThis:

  • Improve handling of the data, both from the bookmarklet and from server-side parsing.
  • Standardize on processing the data in PHP and remove duplicate code from JS.
  • Improve the bookmarklet code and remove pre-filtering of the data.

Part props stephdau, see #31373.

Location:
trunk/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-press-this.php

    r31607 r31609  
    4141                        // Used to trigger the bookmarklet update notice.
    4242                        // Needs to be set here and in get_shortcut_link() in wp-includes/link-template.php.
    43                         'version' => '5',
     43                        'version' => '6',
    4444
    4545                        /**
     
    279279        public function fetch_source_html( $url ) {
    280280                // Download source page to tmp file.
    281                 $source_tmp_file = ( ! empty( $url ) ) ? download_url( $url ) : '';
     281                $source_tmp_file = ( ! empty( $url ) ) ? download_url( $url, 30 ) : '';
    282282                $source_content  = '';
    283283
     
    319319        }
    320320
     321        private function _limit_array( $value ) {
     322                if ( is_array( $value ) ) {
     323                        if ( count( $value ) > 50 ) {
     324                                return array_slice( $value, 0, 50 );
     325                        }
     326
     327                        return $value;
     328                }
     329
     330                return array();
     331        }
     332
     333        private function _limit_string( $value ) {
     334                $return = '';
     335
     336                if ( is_numeric( $value ) || is_bool( $value ) ) {
     337                        $return = (string) $value;
     338                } else if ( is_string( $value ) ) {
     339                        if ( mb_strlen( $value ) > 5000 ) {
     340                                $return = mb_substr( $value, 0, 5000 );
     341                        } else {
     342                                $return = $value;
     343                        }
     344
     345                        $return = html_entity_decode( $return, ENT_QUOTES, 'UTF-8' );
     346                        $return = sanitize_text_field( trim( $return ) );
     347                }
     348
     349                return $return;
     350        }
     351
     352        private function _limit_url( $url ) {
     353                if ( ! is_string( $url ) ) {
     354                        return '';
     355                }
     356               
     357                $url = $this->_limit_string( $url );
     358
     359                // HTTP 1.1 allows 8000 chars but the "de-facto" standard supported in all current browsers is 2048.
     360                if ( mb_strlen( $url ) > 2048 ) {
     361                        return ''; // Return empty rather than a trunacted/invalid URL
     362                }
     363
     364                // Only allow http(s) or protocol relative URLs.
     365                if ( ! preg_match( '%^(https?:)?//%i', $url ) ) {
     366                        return '';
     367                }
     368
     369                if ( strpos( $url, '"' ) !== false || strpos( $url, ' ' ) !== false ) {
     370                        return '';
     371                }
     372
     373                return $url;
     374        }
     375
     376        private function _limit_img( $src ) {
     377                $src = $this->_limit_url( $src );
     378
     379                if ( preg_match( '/\/ad[sx]{1}?\//', $src ) ) {
     380                        // Ads
     381                        return '';
     382                } else if ( preg_match( '/(\/share-?this[^\.]+?\.[a-z0-9]{3,4})(\?.*)?$/', $src ) ) {
     383                        // Share-this type button
     384                        return '';
     385                } else if ( preg_match( '/\/(spinner|loading|spacer|blank|rss)\.(gif|jpg|png)/', $src ) ) {
     386                        // Loaders, spinners, spacers
     387                        return '';
     388                } else if ( preg_match( '/\/([^\.\/]+[-_]{1})?(spinner|loading|spacer|blank)s?([-_]{1}[^\.\/]+)?\.[a-z0-9]{3,4}/', $src ) ) {
     389                        // Fancy loaders, spinners, spacers
     390                        return '';
     391                } else if ( preg_match( '/([^\.\/]+[-_]{1})?thumb[^.]*\.(gif|jpg|png)$/', $src ) ) {
     392                        // Thumbnails, too small, usually irrelevant to context
     393                        return '';
     394                } else if ( preg_match( '/\/wp-includes\//', $src ) ) {
     395                        // Classic WP interface images
     396                        return '';
     397                } else if ( preg_match( '/[^\d]{1}\d{1,2}x\d+\.(gif|jpg|png)$/', $src ) ) {
     398                        // Most often tiny buttons/thumbs (< 100px wide)
     399                        return '';
     400                } else if ( preg_match( '/\/pixel\.(mathtag|quantserve)\.com/', $src ) ) {
     401                        // See mathtag.com and https://www.quantcast.com/how-we-do-it/iab-standard-measurement/how-we-collect-data/
     402                        return '';
     403                } else if ( false !== strpos( $src, '/g.gif' ) ) {
     404                        // Classic WP stats gif
     405                        return '';
     406                }
     407
     408                return $src;
     409        }
     410
     411        private function _limit_embed( $src ) {
     412                $src = $this->_limit_url( $src );
     413
     414                if ( preg_match( '/\/\/www\.youtube\.com\/(embed|v)\/([^\?]+)\?.+$/', $src, $src_matches ) ) {
     415                        $src = 'https://www.youtube.com/watch?v=' . $src_matches[2];
     416                } else if ( preg_match( '/\/\/player\.vimeo\.com\/video\/([\d]+)([\?\/]{1}.*)?$/', $src, $src_matches ) ) {
     417                        $src = 'https://vimeo.com/' . (int) $src_matches[1];
     418                } else if ( preg_match( '/\/\/vimeo\.com\/moogaloop\.swf\?clip_id=([\d]+)$/', $src, $src_matches ) ) {
     419                        $src = 'https://vimeo.com/' . (int) $src_matches[1];
     420                } else if ( preg_match( '/\/\/vine\.co\/v\/([^\/]+)\/embed/', $src, $src_matches ) ) {
     421                        $src = 'https://vine.co/v/' . $src_matches[1];
     422                } else if ( ! preg_match( '/\/\/(m\.|www\.)?youtube\.com\/watch\?/', $src )
     423                            && ! preg_match( '/\/youtu\.be\/.+$/', $src )
     424                            && ! preg_match( '/\/\/vimeo\.com\/[\d]+$/', $src )
     425                            && ! preg_match( '/\/\/(www\.)?dailymotion\.com\/video\/.+$/', $src )
     426                            && ! preg_match( '/\/\/soundcloud\.com\/.+$/', $src )
     427                            && ! preg_match( '/\/\/twitter\.com\/[^\/]+\/status\/[\d]+$/', $src )
     428                            && ! preg_match( '/\/\/vine\.co\/v\/[^\/]+/', $src ) ) {
     429                        $src = '';
     430                }
     431
     432                return $src;
     433        }
     434
     435        private function _process_meta_entry( $meta_name, $meta_value, $data ) {
     436                if ( preg_match( '/:?(title|description|keywords)$/', $meta_name ) ) {
     437                        $data['_meta'][ $meta_name ] = $meta_value;
     438                } else {
     439                        switch ( $meta_name ) {
     440                                case 'og:url':
     441                                case 'og:video':
     442                                case 'og:video:secure_url':
     443                                        $meta_value = $this->_limit_embed( $meta_value );
     444
     445                                        if ( ! isset( $data['_embed'] ) ) {
     446                                                $data['_embed'] = array();
     447                                        }
     448
     449                                        if ( ! empty( $meta_value ) && ! in_array( $meta_value, $data['_embed'] ) ) {
     450                                                $data['_embed'][] = $meta_value;
     451                                        }
     452
     453                                        break;
     454                                case 'og:image':
     455                                case 'og:image:secure_url':
     456                                case 'twitter:image0:src':
     457                                case 'twitter:image0':
     458                                case 'twitter:image:src':
     459                                case 'twitter:image':
     460                                        $meta_value = $this->_limit_img( $meta_value );
     461
     462                                        if ( ! isset( $data['_img'] ) ) {
     463                                                $data['_img'] = array();
     464                                        }
     465
     466                                        if ( ! empty( $meta_value ) && ! in_array( $meta_value, $data['_img'] ) ) {
     467                                                $data['_img'][] = $meta_value;
     468                                        }
     469
     470                                        break;
     471                        }
     472                }
     473               
     474                return $data;
     475        }
     476
    321477        /**
    322478         * Fetches and parses _meta, _img, and _links data from the source.
     
    340496                }
    341497
     498                // Fetch and gather <meta> data first, so discovered media is offered 1st to user.
     499                if ( empty( $data['_meta'] ) ) {
     500                        $data['_meta'] = array();
     501                }
     502
     503                if ( preg_match_all( '/<meta [^>]+>/', $source_content, $matches ) ) {
     504                        $items = $this->_limit_array( $matches[0] );
     505
     506                        foreach ( $items as $value ) {
     507                                if ( preg_match( '/(property|name)="([^"]+)"[^>]+content="([^"]+)"/', $value, $new_matches ) ) {
     508                                        $meta_name  = $this->_limit_string( $new_matches[2] );
     509                                        $meta_value = $this->_limit_string( $new_matches[3] );
     510
     511                                        // Sanity check. $key is usually things like 'title', 'description', 'keywords', etc.
     512                                        if ( strlen( $meta_name ) > 100 ) {
     513                                                continue;
     514                                        }
     515
     516                                        $data = $this->_process_meta_entry( $meta_name, $meta_value, $data );
     517                                }
     518                        }
     519                }
     520
    342521                // Fetch and gather <img> data.
    343522                if ( empty( $data['_img'] ) ) {
     
    345524                }
    346525
    347                 if ( preg_match_all( '/<img (.+)[\s]?\/>/', $source_content, $matches ) ) {
    348                         if ( ! empty( $matches[0] ) ) {
    349                                 foreach ( $matches[0] as $value ) {
    350                                         if ( preg_match( '/<img[^>]+src="([^"]+)"[^>]+\/>/', $value, $new_matches ) ) {
    351                                                 if ( ! in_array( $new_matches[1], $data['_img'] ) ) {
    352                                                         $data['_img'][] = $new_matches[1];
     526                if ( preg_match_all( '/<img [^>]+>/', $source_content, $matches ) ) {
     527                        $items = $this->_limit_array( $matches[0] );
     528
     529                        foreach ( $items as $value ) {
     530                                if ( preg_match( '/src=(\'|")([^\'"]+)\\1/', $value, $new_matches ) ) {
     531                                        $src = $this->_limit_img( $new_matches[2] );
     532                                        if ( ! empty( $src ) && ! in_array( $src, $data['_img'] ) ) {
     533                                                $data['_img'][] = $src;
     534                                        }
     535                                }
     536                        }
     537                }
     538
     539                // Fetch and gather <iframe> data.
     540                if ( empty( $data['_embed'] ) ) {
     541                        $data['_embed'] = array();
     542                }
     543
     544                if ( preg_match_all( '/<iframe [^>]+>/', $source_content, $matches ) ) {
     545                        $items = $this->_limit_array( $matches[0] );
     546
     547                        foreach ( $items as $value ) {
     548                                if ( preg_match( '/src=(\'|")([^\'"]+)\\1/', $value, $new_matches ) ) {
     549                                        $src = $this->_limit_embed( $new_matches[2] );
     550
     551                                        if ( ! empty( $src ) && ! in_array( $src, $data['_embed'] ) ) {
     552                                                $data['_embed'][] = $src;
     553                                        }
     554                                }
     555                        }
     556                }
     557
     558                // Fetch and gather <link> data
     559                if ( empty( $data['_links'] ) ) {
     560                        $data['_links'] = array();
     561                }
     562
     563                if ( preg_match_all( '/<link [^>]+>/', $source_content, $matches ) ) {
     564                        $items = $this->_limit_array( $matches[0] );
     565
     566                        foreach ( $items as $value ) {
     567                                if ( preg_match( '/(rel|itemprop)="([^"]+)"[^>]+href="([^"]+)"/', $value, $new_matches ) ) {
     568                                        if ( 'alternate' === $new_matches[2] || 'thumbnailUrl' === $new_matches[2] || 'url' === $new_matches[2] ) {
     569                                                $url = $this->_limit_url( $new_matches[3] );
     570
     571                                                if ( ! empty( $url ) && empty( $data['_links'][ $new_matches[2] ] ) ) {
     572                                                        $data['_links'][ $new_matches[2] ] = $url;
    353573                                                }
    354574                                        }
     
    357577                }
    358578
    359                 // Fetch and gather <iframe> data.
    360                 if ( empty( $data['_embed'] ) ) {
    361                         $data['_embed'] = array();
    362                 }
    363 
    364                 if ( preg_match_all( '/<iframe (.+)[\s][^>]*>/', $source_content, $matches ) ) {
    365                         if ( ! empty( $matches[0] ) ) {
    366                                 foreach ( $matches[0] as $value ) {
    367                                         if ( preg_match( '/<iframe[^>]+src=(\'|")([^"]+)(\'|")/', $value, $new_matches ) ) {
    368                                                 if ( ! in_array( $new_matches[2], $data['_embed'] ) ) {
    369                                                         if ( preg_match( '/\/\/www\.youtube\.com\/embed\/([^\?]+)\?.+$/', $new_matches[2], $src_matches ) ) {
    370                                                                 $data['_embed'][] = 'https://www.youtube.com/watch?v=' . $src_matches[1];
    371                                                         } else if ( preg_match( '/\/\/player\.vimeo\.com\/video\/([\d]+)([\?\/]{1}.*)?$/', $new_matches[2], $src_matches ) ) {
    372                                                                 $data['_embed'][] = 'https://vimeo.com/' . (int) $src_matches[1];
    373                                                         } else if ( preg_match( '/\/\/vine\.co\/v\/([^\/]+)\/embed/', $new_matches[2], $src_matches ) ) {
    374                                                                 $data['_embed'][] = 'https://vine.co/v/' . $src_matches[1];
    375                                                         }
    376                                                 }
    377                                         }
    378                                 }
    379                         }
    380                 }
    381 
    382                 // Fetch and gather <meta> data.
    383                 if ( empty( $data['_meta'] ) ) {
    384                         $data['_meta'] = array();
    385                 }
    386 
    387                 if ( preg_match_all( '/<meta ([^>]+)[\s]?\/?>/', $source_content, $matches ) ) {
    388                         if ( ! empty( $matches[0] ) ) {
    389                                 foreach ( $matches[0] as $key => $value ) {
    390                                         if ( preg_match( '/<meta[^>]+(property|name)="(.+)"[^>]+content="(.+)"/', $value, $new_matches ) ) {
    391                                                 if ( empty( $data['_meta'][ $new_matches[2] ] ) ) {
    392                                                         if ( preg_match( '/:?(title|description|keywords)$/', $new_matches[2] ) ) {
    393                                                                 $data['_meta'][ $new_matches[2] ] = str_replace( '&#039;', "'", str_replace( '&#034;', '', html_entity_decode( $new_matches[3] ) ) );
    394                                                         } else {
    395                                                                 $data['_meta'][ $new_matches[2] ] = $new_matches[3];
    396                                                                 if ( 'og:url' == $new_matches[2] ) {
    397                                                                         if ( false !== strpos( $new_matches[3], '//www.youtube.com/watch?' )
    398                                                                              || false !== strpos( $new_matches[3], '//www.dailymotion.com/video/' )
    399                                                                              || preg_match( '/\/\/vimeo\.com\/[\d]+$/', $new_matches[3] )
    400                                                                              || preg_match( '/\/\/soundcloud\.com\/.+$/', $new_matches[3] )
    401                                                                              || preg_match( '/\/\/twitter\.com\/[^\/]+\/status\/[\d]+$/', $new_matches[3] )
    402                                                                              || preg_match( '/\/\/vine\.co\/v\/[^\/]+/', $new_matches[3] ) ) {
    403                                                                                 if ( ! in_array( $new_matches[3], $data['_embed'] ) ) {
    404                                                                                         $data['_embed'][] = $new_matches[3];
    405                                                                                 }
    406                                                                         }
    407                                                                 } else if ( 'og:video' == $new_matches[2] || 'og:video:secure_url' == $new_matches[2] ) {
    408                                                                         if ( preg_match( '/\/\/www\.youtube\.com\/v\/([^\?]+)/', $new_matches[3], $src_matches ) ) {
    409                                                                                 if ( ! in_array( 'https://www.youtube.com/watch?v=' . $src_matches[1], $data['_embed'] ) ) {
    410                                                                                         $data['_embed'][] = 'https://www.youtube.com/watch?v=' . $src_matches[1];
    411                                                                                 }
    412                                                                         } else if ( preg_match( '/\/\/vimeo.com\/moogaloop\.swf\?clip_id=([\d]+)$/', $new_matches[3], $src_matches ) ) {
    413                                                                                 if ( ! in_array( 'https://vimeo.com/' . $src_matches[1], $data['_embed'] ) ) {
    414                                                                                         $data['_embed'][] = 'https://vimeo.com/' . $src_matches[1];
    415                                                                                 }
    416                                                                         }
    417                                                                 } else if ( 'og:image' == $new_matches[2] || 'og:image:secure_url' == $new_matches[2] ) {
    418                                                                         if ( ! in_array( $new_matches[3], $data['_img'] ) ) {
    419                                                                                 $data['_img'][] = $new_matches[3];
    420                                                                         }
    421                                                                 }
    422                                                         }
    423                                                 }
    424                                         }
    425                                 }
    426                         }
    427                 }
    428 
    429                 // Fetch and gather <link> data
    430                 if ( empty( $data['_links'] ) ) {
    431                         $data['_links'] = array();
    432                 }
    433 
    434                 if ( preg_match_all( '/<link ([^>]+)[\s]?\/>/', $source_content, $matches ) ) {
    435                         if ( ! empty( $matches[0] ) ) {
    436                                 foreach ( $matches[0] as $key => $value ) {
    437                                         if ( preg_match( '/<link[^>]+(rel|itemprop)="([^"]+)"[^>]+href="([^"]+)"[^>]+\/>/', $value, $new_matches ) ) {
    438                                                 if ( 'alternate' == $new_matches[2] || 'thumbnailUrl' == $new_matches[2] || 'url' == $new_matches[2] ) {
    439                                                         if ( empty( $data['_links'][ $new_matches[2] ] ) ) {
    440                                                                 $data['_links'][ $new_matches[2] ] = $new_matches[3];
    441                                                         }
    442                                                 }
    443                                         }
    444                                 }
    445                         }
    446                 }
    447 
    448579                return $data;
    449580        }
     
    458589         */
    459590        public function merge_or_fetch_data() {
    460                 // Merge $_POST and $_GET, as appropriate ($_POST > $_GET), to remain backward compatible.
    461                 $data = array_merge_recursive( $_POST, $_GET );
    462 
    463                 // Get the legacy QS params, or equiv POST data
    464                 $data['u'] = ( ! empty( $data['u'] ) && preg_match( '/^https?:/', $data['u'] ) ) ? $data['u'] : '';
    465                 $data['s'] = ( ! empty( $data['s'] ) ) ? $data['s'] : '';
    466                 $data['t'] = ( ! empty( $data['t'] ) ) ? $data['t'] : '';
     591                // Get data from $_POST and $_GET, as appropriate ($_POST > $_GET), to remain backward compatible.
     592                $data = array();
     593
     594                // Only instantiate the keys we want. Sanity check and sanitize each one.
     595                foreach ( array( 'u', 's', 't', 'v', '_version' ) as $key ) {
     596                        if ( ! empty( $_POST[ $key ] ) ) {
     597                                $value = wp_unslash( $_POST[ $key ] );
     598                        } else if ( ! empty( $_GET[ $key ] ) ) {
     599                                $value = wp_unslash( $_GET[ $key ] );
     600                        } else {
     601                                continue;
     602                        }
     603
     604                        if ( 'u' === $key ) {
     605                                $value = $this->_limit_url( $value );
     606                        } else {
     607                                $value = $this->_limit_string( $value );
     608                        }
     609
     610                        if ( ! empty( $value ) ) {
     611                                $data[ $key ] = $value;
     612                        }
     613                }
    467614
    468615                /**
     
    475622                if ( apply_filters( 'enable_press_this_media_discovery', true ) ) {
    476623                        /*
    477                          * If no _meta (a new thing) was passed via $_POST, fetch data from source as fallback,
    478                          * makes PT fully backward compatible
     624                         * If no title, _img, _embed, and _meta was passed via $_POST, fetch data from source as fallback,
     625                         * making PT fully backward compatible with the older bookmarklet.
    479626                         */
    480                         if ( empty( $data['_meta'] ) && ! empty( $data['u'] ) ) {
     627                        if ( empty( $_POST ) && ! empty( $data['u'] ) ) {
    481628                                $data = $this->source_data_fetch_fallback( $data['u'], $data );
    482                         }
    483                 } else {
    484                         if ( ! empty( $data['_img'] ) ) {
    485                                 $data['_img'] = array();
    486                         }
    487                         if ( ! empty( $data['_embed'] ) ) {
    488                                 $data['_embed'] = array();
    489                         }
    490                         if ( ! empty( $data['_meta'] ) ) {
    491                                 $data['_meta'] = array();
     629                        } else {
     630                                foreach ( array( '_img', '_embed', '_meta' ) as $type ) {
     631                                        if ( empty( $_POST[ $type ] ) ) {
     632                                                continue;
     633                                        }
     634
     635                                        $data[ $type ] = array();
     636                                        $items = $this->_limit_array( $_POST[ $type ] );
     637                                        $items = wp_unslash( $items );
     638
     639                                        foreach ( $items as $key => $value ) {
     640                                                $key = $this->_limit_string( wp_unslash( $key ) );
     641
     642                                                // Sanity check. $key is usually things like 'title', 'description', 'keywords', etc.
     643                                                if ( empty( $key ) || strlen( $key ) > 100 ) {
     644                                                        continue;
     645                                                }
     646
     647                                                if ( $type === '_meta' ) {
     648                                                        $value = $this->_limit_string( $value );
     649
     650                                                        if ( ! empty( $value ) ) {
     651                                                                $data = $this->_process_meta_entry( $key, $value, $data );
     652                                                        }
     653                                                } else if ( $type === '_img' ) {
     654                                                        $value = $this->_limit_img( $value );
     655
     656                                                        if ( ! empty( $value ) ) {
     657                                                                $data[ $type ][] = $value;
     658                                                        }
     659                                                } else if ( $type === '_embed' ) {
     660                                                        $value = $this->_limit_embed( $value );
     661
     662                                                        if ( ! empty( $value ) ) {
     663                                                                $data[ $type ][] = $value;
     664                                                        }
     665                                                }
     666                                        }
     667                                }
    492668                        }
    493669                }
  • trunk/src/wp-admin/js/bookmarklet.js

    r31584 r31609  
    88                windowWidth, windowHeight,
    99                metas, links, content, imgs, ifrs,
    10                 vid, selection, newWin;
     10                selection;
    1111
    1212        if ( ! pt_url ) {
     
    2929                selection = document.getSelection() + '';
    3030        } else if ( document.selection ) {
    31                 selection = document.selection.createRange().text;
     31                selection = document.selection.createRange().text || '';
    3232        }
    3333
    3434        pt_url += ( pt_url.indexOf( '?' ) > -1 ? '&' : '?' ) + 'buster=' + ( new Date().getTime() );
    3535
    36         if ( document.title.length && ( document.title.length <= 256 || ! canPost ) ) {
    37                 pt_url += '&t=' + encURI( document.title.substr( 0, 256 ) );
    38         }
     36        if ( ! canPost ) {
     37                if ( document.title ) {
     38                        pt_url += '&t=' + encURI( document.title.substr( 0, 256 ) );
     39                }
    3940
    40         if ( selection && ( selection.length <= 512 || ! canPost ) ) {
    41                 pt_url += '&s=' + encURI( selection.substr( 0, 512 ) );
     41                if ( selection ) {
     42                        pt_url += '&s=' + encURI( selection.substr( 0, 512 ) );
     43                }
    4244        }
    4345
     
    4951
    5052        if ( ! canPost ) {
    51                 newWin = window.open( pt_url, target, 'location,resizable,scrollbars,width=' + windowWidth + ',height=' + windowHeight );
    52                 newWin.focus();
     53                window.open( pt_url, target, 'location,resizable,scrollbars,width=' + windowWidth + ',height=' + windowHeight );
    5354                return;
    5455        }
     
    136137
    137138        for ( var n = 0; n < imgs.length; n++ ) {
    138                 if ( n >= 100 ) {
     139                if ( n >= 50 ) {
    139140                        break;
    140141                }
     
    154155
    155156        for ( var p = 0; p < ifrs.length; p++ ) {
    156                 if ( p >= 100 ) {
     157                if ( p >= 50 ) {
    157158                        break;
    158159                }
    159160
    160                 vid = ifrs[ p ].src.match(/\/\/www\.youtube\.com\/embed\/([^\?]+)\?.+$/);
    161 
    162                 if ( vid && 2 === vid.length ) {
    163                         add( '_embed[]', 'https://www.youtube.com/watch?v=' + vid[1] );
    164                 }
    165 
    166                 vid = ifrs[ p ].src.match( /\/\/player\.vimeo\.com\/video\/([\d]+)$/ );
    167 
    168                 if ( vid && 2 === vid.length ) {
    169                         add( '_embed[]', 'https://vimeo.com/' + vid[1] );
    170                 }
    171 
    172                 vid = ifrs[ p ].src.match( /\/\/vine\.co\/v\/([^\/]+)\/embed/ );
    173 
    174                 if ( vid && 2 === vid.length ) {
    175                         add( '_embed[]', 'https://vine.co/v/' + vid[1] );
    176                 }
     161                add( '_embed[]', ifrs[ p ].src );
    177162        }
    178163
    179         if ( document.title && document.title > 512 ) {
     164        if ( document.title ) {
    180165                add( 't', document.title );
    181166        }
    182167
    183         if ( selection && selection.length > 512 ) {
     168        if ( selection ) {
    184169                add( 's', selection );
    185170        }
     
    190175        form.setAttribute( 'style', 'display: none;' );
    191176
    192         newWin = window.open( 'about:blank', target, 'location,resizable,scrollbars,width=' + windowWidth + ',height=' + windowHeight );
     177        window.open( 'about:blank', target, 'location,resizable,scrollbars,width=' + windowWidth + ',height=' + windowHeight );
    193178
    194179        document.body.appendChild( form );
    195180        form.submit();
    196 
    197         newWin.focus();
    198181} )( window, document, top.location.href, window.pt_url );
  • trunk/src/wp-admin/js/bookmarklet.min.js

    r31584 r31609  
    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",q.appendChild(d)}}var f,g,h,i,j,k,l,m,n,o,p=a.encodeURIComponent,q=b.createElement("form"),r=b.getElementsByTagName("head")[0],s=new Image,t="_press_this_app",u=!0;if(d){if(!c.match(/^https?:/))return void(top.location.href=d);if(d+="&u="+p(c),c.match(/^https:/)&&d.match(/^http:/)&&(u=!1),a.getSelection?n=a.getSelection()+"":b.getSelection?n=b.getSelection()+"":b.selection&&(n=b.selection.createRange().text),d+=(d.indexOf("?")>-1?"&":"?")+"buster="+(new Date).getTime(),b.title.length&&(b.title.length<=256||!u)&&(d+="&t="+p(b.title.substr(0,256))),n&&(n.length<=512||!u)&&(d+="&s="+p(n.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,!u)return o=a.open(d,t,"location,resizable,scrollbars,width="+f+",height="+g),void o.focus();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=r.getElementsByTagName("meta")||[];for(var v=0;v<h.length&&!(v>=50);v++){var w=h[v],x=w.getAttribute("name"),y=w.getAttribute("property"),z=w.getAttribute("content");x?e("_meta["+x+"]",z):y&&e("_meta["+y+"]",z)}i=r.getElementsByTagName("link")||[];for(var A=0;A<i.length&&!(A>=50);A++){var B=i[A],C=B.getAttribute("rel");if(C)switch(C){case"canonical":case"icon":case"shortlink":e("_links["+C+"]",B.getAttribute("href"));break;case"alternate":"application/json+oembed"===B.getAttribute("type")?e("_links["+C+"]",B.getAttribute("href")):"handheld"===B.getAttribute("media")&&e("_links["+C+"]",B.getAttribute("href"))}}b.body.getElementsByClassName&&(j=b.body.getElementsByClassName("hfeed")[0]),j=b.getElementById("content")||j||b.body,k=j.getElementsByTagName("img")||[];for(var D=0;D<k.length&&!(D>=100);D++)k[D].src.indexOf("avatar")>-1||k[D].className.indexOf("avatar")>-1||(s.src=k[D].src,s.width>=256&&s.height>=128&&e("_img[]",s.src));l=b.body.getElementsByTagName("iframe")||[];for(var E=0;E<l.length&&!(E>=100);E++)m=l[E].src.match(/\/\/www\.youtube\.com\/embed\/([^\?]+)\?.+$/),m&&2===m.length&&e("_embed[]","https://www.youtube.com/watch?v="+m[1]),m=l[E].src.match(/\/\/player\.vimeo\.com\/video\/([\d]+)$/),m&&2===m.length&&e("_embed[]","https://vimeo.com/"+m[1]),m=l[E].src.match(/\/\/vine\.co\/v\/([^\/]+)\/embed/),m&&2===m.length&&e("_embed[]","https://vine.co/v/"+m[1]);b.title&&b.title>512&&e("t",b.title),n&&n.length>512&&e("s",n),q.setAttribute("method","POST"),q.setAttribute("action",d),q.setAttribute("target",t),q.setAttribute("style","display: none;"),o=a.open("about:blank",t,"location,resizable,scrollbars,width="+f+",height="+g),b.body.appendChild(q),q.submit(),o.focus()}})(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+=(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);
  • trunk/src/wp-admin/js/press-this.js

    r31601 r31609  
    77                var editor,
    88                        saveAlert             = false,
     9                        textarea              = document.createElement( 'textarea' ),
    910                        siteConfig            = window.wpPressThisConfig || {},
    1011                        data                  = window.wpPressThisData || {},
     
    6162                                .replace( /<!--[\s\S]*?(-->|$)/g, '' )
    6263                                .replace( /<(script|style)[^>]*>[\s\S]*?(<\/\1>|$)/ig, '' )
    63                                 .replace( /<\/?[a-z][^>]*>/ig, '' );
    64                 }
    65 
    66                 /**
    67                  * Strip HTML tags and entity encode some of the HTML special chars.
     64                                .replace( /<\/?[a-z][\s\S]*?(>|$)/ig, '' );
     65                }
     66
     67                /**
     68                 * Strip HTML tags and convert HTML entities.
    6869                 *
    6970                 * @param text string Text.
     
    7273                function sanitizeText( text ) {
    7374                        text = stripTags( text );
    74 
    75                         return text
    76                                 .replace( /\\/, '' )
    77                                 .replace( /</g, '&lt;' )
    78                                 .replace( />/g, '&gt;' )
    79                                 .replace( /"/g, '&quot;' )
    80                                 .replace( /'/g, '&#039;' );
     75                        textarea.innerHTML = text;
     76
     77                        return stripTags( textarea.value );
    8178                }
    8279
     
    215212
    216213                /**
    217                  * Tests if what was passed as an embed URL is deemed to be embeddable in the editor.
    218                  *
    219                  * @param url string Passed URl, usually from WpPressThis_App.data._embed
    220                  * @returns boolean
    221                  */
    222                 function isEmbeddable( url ) {
    223                         if ( ! url ) {
    224                                 return false;
    225                         } else if ( url.match( /\/\/(m\.|www\.)?youtube\.com\/watch\?/ ) || url.match( /\/youtu\.be\/.+$/ ) ) {
    226                                 return true;
    227                         } else if ( url.match( /\/\/vimeo\.com\/(.+\/)?[\d]+$/ ) ) {
    228                                 return true;
    229                         } else if ( url.match( /\/\/(www\.)?dailymotion\.com\/video\/.+$/ ) ) {
    230                                 return true;
    231                         } else if ( url.match( /\/\/soundcloud\.com\/.+$/ ) ) {
    232                                 return true;
    233                         } else if ( url.match( /\/\/twitter\.com\/[^\/]+\/status\/[\d]+$/ ) ) {
    234                                 return true;
    235                         } else if ( url.match( /\/\/vine\.co\/v\/[^\/]+/ ) ) {
    236                                 return true;
    237                         }
    238 
    239                         return false;
    240                 }
    241 
    242                 /**
    243                  * Tests if what was passed as an image URL is deemed to be interesting enough to offer to the user for selection.
    244                  *
    245                  * @param src string Passed URl, usually from WpPressThis_App.data._ing
    246                  * @returns boolean Test for false
    247                  */
    248                 function isSrcUninterestingPath( src ) {
    249                         if ( src.match( /\/ad[sx]{1}?\// ) ) {
    250                                 // Ads
    251                                 return true;
    252                         } else if ( src.match( /(\/share-?this[^\.]+?\.[a-z0-9]{3,4})(\?.*)?$/ ) ) {
    253                                 // Share-this type button
    254                                 return true;
    255                         } else if ( src.match( /\/(spinner|loading|spacer|blank|rss)\.(gif|jpg|png)/ ) ) {
    256                                 // Loaders, spinners, spacers
    257                                 return true;
    258                         } else if ( src.match( /\/([^\.\/]+[-_]{1})?(spinner|loading|spacer|blank)s?([-_]{1}[^\.\/]+)?\.[a-z0-9]{3,4}/ ) ) {
    259                                 // Fancy loaders, spinners, spacers
    260                                 return true;
    261                         } else if ( src.match( /([^\.\/]+[-_]{1})?thumb[^.]*\.(gif|jpg|png)$/ ) ) {
    262                                 // Thumbnails, too small, usually irrelevant to context
    263                                 return true;
    264                         } else if ( src.match( /\/wp-includes\// ) ) {
    265                                 // Classic WP interface images
    266                                 return true;
    267                         } else if ( src.match( /[^\d]{1}\d{1,2}x\d+\.(gif|jpg|png)$/ ) ) {
    268                                 // Most often tiny buttons/thumbs (< 100px wide)
    269                                 return true;
    270                         } else if ( src.indexOf( '/g.gif' ) > -1 ) {
    271                                 // Classic WP stats gif
    272                                 return true;
    273                         } else if ( src.indexOf( '/pixel.mathtag.com' ) > -1 ) {
    274                                 // See mathtag.com
    275                                 return true;
    276                         }
    277                         return false;
    278                 }
    279 
    280                 /**
    281214                 * Get a list of valid embeds from what was passed via WpPressThis_App.data._embed on page load.
    282215                 *
     
    293226                                                // Skip: no src value
    294227                                                return;
    295                                         } else if ( !isEmbeddable( src ) ) {
    296                                                 // Skip: not deemed embeddable
    297                                                 return;
    298228                                        }
    299229
     
    314244
    315245                /**
    316                  * Get what is likely the most valuable image from what was passed via WpPressThis_App.data._img and WpPressThis_App.data._meta on page load.
    317                  *
    318                  * @returns array
    319                  */
    320                 function getFeaturedImage( data ) {
    321                         var featured = '';
    322 
    323                         if ( ! data || ! data._meta ) {
    324                                 return '';
    325                         }
    326 
    327                         if ( data._meta['twitter:image0:src'] && data._meta['twitter:image0:src'].length ) {
    328                                 featured = data._meta['twitter:image0:src'];
    329                         } else if ( data._meta['twitter:image0'] && data._meta['twitter:image0'].length ) {
    330                                 featured = data._meta['twitter:image0'];
    331                         } else if ( data._meta['twitter:image:src'] && data._meta['twitter:image:src'].length ) {
    332                                 featured = data._meta['twitter:image:src'];
    333                         } else if ( data._meta['twitter:image'] && data._meta['twitter:image'].length ) {
    334                                 featured = data._meta['twitter:image'];
    335                         } else if ( data._meta['og:image'] && data._meta['og:image'].length ) {
    336                                 featured = data._meta['og:image'];
    337                         } else if ( data._meta['og:image:secure_url'] && data._meta['og:image:secure_url'].length ) {
    338                                 featured = data._meta['og:image:secure_url'];
    339                         }
    340 
    341                         featured = checkUrl( featured );
    342 
    343                         return ( isSrcUninterestingPath( featured ) ) ? '' : featured;
    344                 }
    345 
    346                 /**
    347246                 * Get a list of valid images from what was passed via WpPressThis_App.data._img and WpPressThis_App.data._meta on page load.
    348247                 *
     
    351250                function getInterestingImages( data ) {
    352251                        var imgs             = data._img || [],
    353                                 featuredPict     = getFeaturedImage( data ) || '',
    354252                                interestingImgs  = [],
    355253                                alreadySelected  = [];
    356 
    357                         if ( featuredPict.length ) {
    358                                 interestingImgs.push( featuredPict );
    359                                 alreadySelected.push( featuredPict.replace(/^https?:/, '') );
    360                         }
    361254
    362255                        if ( imgs.length ) {
     
    374267                                        if ( Array.prototype.indexOf && alreadySelected.indexOf( schemelessSrc ) > -1 ) {
    375268                                                // Skip: already shown
    376                                                 return;
    377                                         } else if ( isSrcUninterestingPath( src ) ) {
    378                                                 // Skip: spinner, stat, ad, or spacer pict
    379269                                                return;
    380270                                        } else if ( src.indexOf( 'avatar' ) > -1 && interestingImgs.length >= 15 ) {
     
    665555                                $.each( interestingEmbeds, function ( i, src ) {
    666556                                        src = checkUrl( src );
    667 
    668                                         if ( ! isEmbeddable( src ) ) {
    669                                                 return;
    670                                         }
    671557
    672558                                        var displaySrc = '',
  • trunk/src/wp-includes/link-template.php

    r31591 r31609  
    25972597        global $is_IE, $wp_version;
    25982598
    2599         $bookmarklet_version = '5';
     2599        $bookmarklet_version = '6';
    26002600        $link = '';
    26012601
Note: See TracChangeset for help on using the changeset viewer.