PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/class-wp-press-this.php

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