PageRenderTime 33ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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