class-wp-press-this-plugin.php 51 KB

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