Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

1561 рядки
49 KiB

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