media.php 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266
  1. <?php
  2. /**
  3. * WordPress Administration Media API.
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Defines the default media upload tabs
  10. *
  11. * @since 2.5.0
  12. *
  13. * @return array default tabs
  14. */
  15. function media_upload_tabs() {
  16. $_default_tabs = array(
  17. 'type' => __('From Computer'), // handler action suffix => tab text
  18. 'type_url' => __('From URL'),
  19. 'gallery' => __('Gallery'),
  20. 'library' => __('Media Library')
  21. );
  22. /**
  23. * Filters the available tabs in the legacy (pre-3.5.0) media popup.
  24. *
  25. * @since 2.5.0
  26. *
  27. * @param array $_default_tabs An array of media tabs.
  28. */
  29. return apply_filters( 'media_upload_tabs', $_default_tabs );
  30. }
  31. /**
  32. * Adds the gallery tab back to the tabs array if post has image attachments
  33. *
  34. * @since 2.5.0
  35. *
  36. * @global wpdb $wpdb WordPress database abstraction object.
  37. *
  38. * @param array $tabs
  39. * @return array $tabs with gallery if post has image attachment
  40. */
  41. function update_gallery_tab($tabs) {
  42. global $wpdb;
  43. if ( !isset($_REQUEST['post_id']) ) {
  44. unset($tabs['gallery']);
  45. return $tabs;
  46. }
  47. $post_id = intval($_REQUEST['post_id']);
  48. if ( $post_id )
  49. $attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) );
  50. if ( empty($attachments) ) {
  51. unset($tabs['gallery']);
  52. return $tabs;
  53. }
  54. $tabs['gallery'] = sprintf(__('Gallery (%s)'), "<span id='attachments-count'>$attachments</span>");
  55. return $tabs;
  56. }
  57. /**
  58. * Outputs the legacy media upload tabs UI.
  59. *
  60. * @since 2.5.0
  61. *
  62. * @global string $redir_tab
  63. */
  64. function the_media_upload_tabs() {
  65. global $redir_tab;
  66. $tabs = media_upload_tabs();
  67. $default = 'type';
  68. if ( !empty($tabs) ) {
  69. echo "<ul id='sidemenu'>\n";
  70. if ( isset($redir_tab) && array_key_exists($redir_tab, $tabs) ) {
  71. $current = $redir_tab;
  72. } elseif ( isset($_GET['tab']) && array_key_exists($_GET['tab'], $tabs) ) {
  73. $current = $_GET['tab'];
  74. } else {
  75. /** This filter is documented in wp-admin/media-upload.php */
  76. $current = apply_filters( 'media_upload_default_tab', $default );
  77. }
  78. foreach ( $tabs as $callback => $text ) {
  79. $class = '';
  80. if ( $current == $callback )
  81. $class = " class='current'";
  82. $href = add_query_arg(array('tab' => $callback, 's' => false, 'paged' => false, 'post_mime_type' => false, 'm' => false));
  83. $link = "<a href='" . esc_url($href) . "'$class>$text</a>";
  84. echo "\t<li id='" . esc_attr("tab-$callback") . "'>$link</li>\n";
  85. }
  86. echo "</ul>\n";
  87. }
  88. }
  89. /**
  90. * Retrieves the image HTML to send to the editor.
  91. *
  92. * @since 2.5.0
  93. *
  94. * @param int $id Image attachment id.
  95. * @param string $caption Image caption.
  96. * @param string $title Image title attribute.
  97. * @param string $align Image CSS alignment property.
  98. * @param string $url Optional. Image src URL. Default empty.
  99. * @param bool|string $rel Optional. Value for rel attribute or whether to add a default value. Default false.
  100. * @param string|array $size Optional. Image size. Accepts any valid image size, or an array of width
  101. * and height values in pixels (in that order). Default 'medium'.
  102. * @param string $alt Optional. Image alt attribute. Default empty.
  103. * @return string The HTML output to insert into the editor.
  104. */
  105. function get_image_send_to_editor( $id, $caption, $title, $align, $url = '', $rel = false, $size = 'medium', $alt = '' ) {
  106. $html = get_image_tag( $id, $alt, '', $align, $size );
  107. if ( $rel ) {
  108. if ( is_string( $rel ) ) {
  109. $rel = ' rel="' . esc_attr( $rel ) . '"';
  110. } else {
  111. $rel = ' rel="attachment wp-att-' . intval( $id ) . '"';
  112. }
  113. } else {
  114. $rel = '';
  115. }
  116. if ( $url )
  117. $html = '<a href="' . esc_attr( $url ) . '"' . $rel . '>' . $html . '</a>';
  118. /**
  119. * Filters the image HTML markup to send to the editor when inserting an image.
  120. *
  121. * @since 2.5.0
  122. *
  123. * @param string $html The image HTML markup to send.
  124. * @param int $id The attachment id.
  125. * @param string $caption The image caption.
  126. * @param string $title The image title.
  127. * @param string $align The image alignment.
  128. * @param string $url The image source URL.
  129. * @param string|array $size Size of image. Image size or array of width and height values
  130. * (in that order). Default 'medium'.
  131. * @param string $alt The image alternative, or alt, text.
  132. */
  133. $html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
  134. return $html;
  135. }
  136. /**
  137. * Adds image shortcode with caption to editor
  138. *
  139. * @since 2.6.0
  140. *
  141. * @param string $html
  142. * @param integer $id
  143. * @param string $caption image caption
  144. * @param string $title image title attribute
  145. * @param string $align image css alignment property
  146. * @param string $url image src url
  147. * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
  148. * @param string $alt image alt attribute
  149. * @return string
  150. */
  151. function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
  152. /**
  153. * Filters the caption text.
  154. *
  155. * Note: If the caption text is empty, the caption shortcode will not be appended
  156. * to the image HTML when inserted into the editor.
  157. *
  158. * Passing an empty value also prevents the {@see 'image_add_caption_shortcode'}
  159. * Filters from being evaluated at the end of image_add_caption().
  160. *
  161. * @since 4.1.0
  162. *
  163. * @param string $caption The original caption text.
  164. * @param int $id The attachment ID.
  165. */
  166. $caption = apply_filters( 'image_add_caption_text', $caption, $id );
  167. /**
  168. * Filters whether to disable captions.
  169. *
  170. * Prevents image captions from being appended to image HTML when inserted into the editor.
  171. *
  172. * @since 2.6.0
  173. *
  174. * @param bool $bool Whether to disable appending captions. Returning true to the filter
  175. * will disable captions. Default empty string.
  176. */
  177. if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
  178. return $html;
  179. $id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
  180. if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) )
  181. return $html;
  182. $width = $matches[1];
  183. $caption = str_replace( array("\r\n", "\r"), "\n", $caption);
  184. $caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
  185. // Convert any remaining line breaks to <br>.
  186. $caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption );
  187. $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
  188. if ( empty($align) )
  189. $align = 'none';
  190. $shcode = '[caption id="' . $id . '" align="align' . $align . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]';
  191. /**
  192. * Filters the image HTML markup including the caption shortcode.
  193. *
  194. * @since 2.6.0
  195. *
  196. * @param string $shcode The image HTML markup with caption shortcode.
  197. * @param string $html The image HTML markup.
  198. */
  199. return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
  200. }
  201. /**
  202. * Private preg_replace callback used in image_add_caption()
  203. *
  204. * @access private
  205. * @since 3.4.0
  206. */
  207. function _cleanup_image_add_caption( $matches ) {
  208. // Remove any line breaks from inside the tags.
  209. return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
  210. }
  211. /**
  212. * Adds image html to editor
  213. *
  214. * @since 2.5.0
  215. *
  216. * @param string $html
  217. */
  218. function media_send_to_editor($html) {
  219. ?>
  220. <script type="text/javascript">
  221. var win = window.dialogArguments || opener || parent || top;
  222. win.send_to_editor( <?php echo wp_json_encode( $html ); ?> );
  223. </script>
  224. <?php
  225. exit;
  226. }
  227. /**
  228. * Save a file submitted from a POST request and create an attachment post for it.
  229. *
  230. * @since 2.5.0
  231. *
  232. * @param string $file_id Index of the `$_FILES` array that the file was sent. Required.
  233. * @param int $post_id The post ID of a post to attach the media item to. Required, but can
  234. * be set to 0, creating a media item that has no relationship to a post.
  235. * @param array $post_data Overwrite some of the attachment. Optional.
  236. * @param array $overrides Override the wp_handle_upload() behavior. Optional.
  237. * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
  238. */
  239. function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false )) {
  240. $time = current_time('mysql');
  241. if ( $post = get_post($post_id) ) {
  242. // The post date doesn't usually matter for pages, so don't backdate this upload.
  243. if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 )
  244. $time = $post->post_date;
  245. }
  246. $file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
  247. if ( isset($file['error']) )
  248. return new WP_Error( 'upload_error', $file['error'] );
  249. $name = $_FILES[$file_id]['name'];
  250. $ext = pathinfo( $name, PATHINFO_EXTENSION );
  251. $name = wp_basename( $name, ".$ext" );
  252. $url = $file['url'];
  253. $type = $file['type'];
  254. $file = $file['file'];
  255. $title = sanitize_text_field( $name );
  256. $content = '';
  257. $excerpt = '';
  258. if ( preg_match( '#^audio#', $type ) ) {
  259. $meta = wp_read_audio_metadata( $file );
  260. if ( ! empty( $meta['title'] ) ) {
  261. $title = $meta['title'];
  262. }
  263. if ( ! empty( $title ) ) {
  264. if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
  265. /* translators: 1: audio track title, 2: album title, 3: artist name */
  266. $content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
  267. } elseif ( ! empty( $meta['album'] ) ) {
  268. /* translators: 1: audio track title, 2: album title */
  269. $content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
  270. } elseif ( ! empty( $meta['artist'] ) ) {
  271. /* translators: 1: audio track title, 2: artist name */
  272. $content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
  273. } else {
  274. /* translators: 1: audio track title */
  275. $content .= sprintf( __( '"%s".' ), $title );
  276. }
  277. } elseif ( ! empty( $meta['album'] ) ) {
  278. if ( ! empty( $meta['artist'] ) ) {
  279. /* translators: 1: audio album title, 2: artist name */
  280. $content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
  281. } else {
  282. $content .= $meta['album'] . '.';
  283. }
  284. } elseif ( ! empty( $meta['artist'] ) ) {
  285. $content .= $meta['artist'] . '.';
  286. }
  287. if ( ! empty( $meta['year'] ) ) {
  288. /* translators: Audio file track information. 1: Year of audio track release */
  289. $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
  290. }
  291. if ( ! empty( $meta['track_number'] ) ) {
  292. $track_number = explode( '/', $meta['track_number'] );
  293. if ( isset( $track_number[1] ) ) {
  294. /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks */
  295. $content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
  296. } else {
  297. /* translators: Audio file track information. 1: Audio track number */
  298. $content .= ' ' . sprintf( __( 'Track %1$s.' ), number_format_i18n( $track_number[0] ) );
  299. }
  300. }
  301. if ( ! empty( $meta['genre'] ) ) {
  302. /* translators: Audio file genre information. 1: Audio genre name */
  303. $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
  304. }
  305. // Use image exif/iptc data for title and caption defaults if possible.
  306. } elseif ( 0 === strpos( $type, 'image/' ) && $image_meta = wp_read_image_metadata( $file ) ) {
  307. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
  308. $title = $image_meta['title'];
  309. }
  310. if ( trim( $image_meta['caption'] ) ) {
  311. $excerpt = $image_meta['caption'];
  312. }
  313. }
  314. // Construct the attachment array
  315. $attachment = array_merge( array(
  316. 'post_mime_type' => $type,
  317. 'guid' => $url,
  318. 'post_parent' => $post_id,
  319. 'post_title' => $title,
  320. 'post_content' => $content,
  321. 'post_excerpt' => $excerpt,
  322. ), $post_data );
  323. // This should never be set as it would then overwrite an existing attachment.
  324. unset( $attachment['ID'] );
  325. // Save the data
  326. $id = wp_insert_attachment( $attachment, $file, $post_id, true );
  327. if ( !is_wp_error($id) ) {
  328. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  329. }
  330. return $id;
  331. }
  332. /**
  333. * Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
  334. *
  335. * @since 2.6.0
  336. *
  337. * @param array $file_array Array similar to a `$_FILES` upload array.
  338. * @param int $post_id The post ID the media is associated with.
  339. * @param string $desc Optional. Description of the side-loaded file. Default null.
  340. * @param array $post_data Optional. Post data to override. Default empty array.
  341. * @return int|object The ID of the attachment or a WP_Error on failure.
  342. */
  343. function media_handle_sideload( $file_array, $post_id, $desc = null, $post_data = array() ) {
  344. $overrides = array('test_form'=>false);
  345. $time = current_time( 'mysql' );
  346. if ( $post = get_post( $post_id ) ) {
  347. if ( substr( $post->post_date, 0, 4 ) > 0 )
  348. $time = $post->post_date;
  349. }
  350. $file = wp_handle_sideload( $file_array, $overrides, $time );
  351. if ( isset($file['error']) )
  352. return new WP_Error( 'upload_error', $file['error'] );
  353. $url = $file['url'];
  354. $type = $file['type'];
  355. $file = $file['file'];
  356. $title = preg_replace('/\.[^.]+$/', '', basename($file));
  357. $content = '';
  358. // Use image exif/iptc data for title and caption defaults if possible.
  359. if ( $image_meta = wp_read_image_metadata( $file ) ) {
  360. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
  361. $title = $image_meta['title'];
  362. if ( trim( $image_meta['caption'] ) )
  363. $content = $image_meta['caption'];
  364. }
  365. if ( isset( $desc ) )
  366. $title = $desc;
  367. // Construct the attachment array.
  368. $attachment = array_merge( array(
  369. 'post_mime_type' => $type,
  370. 'guid' => $url,
  371. 'post_parent' => $post_id,
  372. 'post_title' => $title,
  373. 'post_content' => $content,
  374. ), $post_data );
  375. // This should never be set as it would then overwrite an existing attachment.
  376. unset( $attachment['ID'] );
  377. // Save the attachment metadata
  378. $id = wp_insert_attachment($attachment, $file, $post_id);
  379. if ( !is_wp_error($id) )
  380. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  381. return $id;
  382. }
  383. /**
  384. * Adds the iframe to display content for the media upload page
  385. *
  386. * @since 2.5.0
  387. *
  388. * @global int $body_id
  389. *
  390. * @param string|callable $content_func
  391. */
  392. function wp_iframe($content_func /* ... */) {
  393. _wp_admin_html_begin();
  394. ?>
  395. <title><?php bloginfo('name') ?> &rsaquo; <?php _e('Uploads'); ?> &#8212; <?php _e('WordPress'); ?></title>
  396. <?php
  397. wp_enqueue_style( 'colors' );
  398. // Check callback name for 'media'
  399. if ( ( is_array( $content_func ) && ! empty( $content_func[1] ) && 0 === strpos( (string) $content_func[1], 'media' ) )
  400. || ( ! is_array( $content_func ) && 0 === strpos( $content_func, 'media' ) ) )
  401. wp_enqueue_style( 'deprecated-media' );
  402. wp_enqueue_style( 'ie' );
  403. ?>
  404. <script type="text/javascript">
  405. addLoadEvent = function(func){if(typeof jQuery!="undefined")jQuery(document).ready(func);else if(typeof wpOnload!='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
  406. var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>', pagenow = 'media-upload-popup', adminpage = 'media-upload-popup',
  407. isRtl = <?php echo (int) is_rtl(); ?>;
  408. </script>
  409. <?php
  410. /** This action is documented in wp-admin/admin-header.php */
  411. do_action( 'admin_enqueue_scripts', 'media-upload-popup' );
  412. /**
  413. * Fires when admin styles enqueued for the legacy (pre-3.5.0) media upload popup are printed.
  414. *
  415. * @since 2.9.0
  416. */
  417. do_action( 'admin_print_styles-media-upload-popup' );
  418. /** This action is documented in wp-admin/admin-header.php */
  419. do_action( 'admin_print_styles' );
  420. /**
  421. * Fires when admin scripts enqueued for the legacy (pre-3.5.0) media upload popup are printed.
  422. *
  423. * @since 2.9.0
  424. */
  425. do_action( 'admin_print_scripts-media-upload-popup' );
  426. /** This action is documented in wp-admin/admin-header.php */
  427. do_action( 'admin_print_scripts' );
  428. /**
  429. * Fires when scripts enqueued for the admin header for the legacy (pre-3.5.0)
  430. * media upload popup are printed.
  431. *
  432. * @since 2.9.0
  433. */
  434. do_action( 'admin_head-media-upload-popup' );
  435. /** This action is documented in wp-admin/admin-header.php */
  436. do_action( 'admin_head' );
  437. if ( is_string( $content_func ) ) {
  438. /**
  439. * Fires in the admin header for each specific form tab in the legacy
  440. * (pre-3.5.0) media upload popup.
  441. *
  442. * The dynamic portion of the hook, `$content_func`, refers to the form
  443. * callback for the media upload type. Possible values include
  444. * 'media_upload_type_form', 'media_upload_type_url_form', and
  445. * 'media_upload_library_form'.
  446. *
  447. * @since 2.5.0
  448. */
  449. do_action( "admin_head_{$content_func}" );
  450. }
  451. ?>
  452. </head>
  453. <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?> class="wp-core-ui no-js">
  454. <script type="text/javascript">
  455. document.body.className = document.body.className.replace('no-js', 'js');
  456. </script>
  457. <?php
  458. $args = func_get_args();
  459. $args = array_slice($args, 1);
  460. call_user_func_array($content_func, $args);
  461. /** This action is documented in wp-admin/admin-footer.php */
  462. do_action( 'admin_print_footer_scripts' );
  463. ?>
  464. <script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
  465. </body>
  466. </html>
  467. <?php
  468. }
  469. /**
  470. * Adds the media button to the editor
  471. *
  472. * @since 2.5.0
  473. *
  474. * @global int $post_ID
  475. *
  476. * @staticvar int $instance
  477. *
  478. * @param string $editor_id
  479. */
  480. function media_buttons($editor_id = 'content') {
  481. static $instance = 0;
  482. $instance++;
  483. $post = get_post();
  484. if ( ! $post && ! empty( $GLOBALS['post_ID'] ) )
  485. $post = $GLOBALS['post_ID'];
  486. wp_enqueue_media( array(
  487. 'post' => $post
  488. ) );
  489. $img = '<span class="wp-media-buttons-icon"></span> ';
  490. $id_attribute = $instance === 1 ? ' id="insert-media-button"' : '';
  491. printf( '<button type="button"%s class="button insert-media add_media" data-editor="%s">%s</button>',
  492. $id_attribute,
  493. esc_attr( $editor_id ),
  494. $img . __( 'Add Media' )
  495. );
  496. /**
  497. * Filters the legacy (pre-3.5.0) media buttons.
  498. *
  499. * Use {@see 'media_buttons'} action instead.
  500. *
  501. * @since 2.5.0
  502. * @deprecated 3.5.0 Use {@see 'media_buttons'} action instead.
  503. *
  504. * @param string $string Media buttons context. Default empty.
  505. */
  506. $legacy_filter = apply_filters( 'media_buttons_context', '' );
  507. if ( $legacy_filter ) {
  508. // #WP22559. Close <a> if a plugin started by closing <a> to open their own <a> tag.
  509. if ( 0 === stripos( trim( $legacy_filter ), '</a>' ) )
  510. $legacy_filter .= '</a>';
  511. echo $legacy_filter;
  512. }
  513. }
  514. /**
  515. *
  516. * @global int $post_ID
  517. * @param string $type
  518. * @param int $post_id
  519. * @param string $tab
  520. * @return string
  521. */
  522. function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) {
  523. global $post_ID;
  524. if ( empty( $post_id ) )
  525. $post_id = $post_ID;
  526. $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') );
  527. if ( $type && 'media' != $type )
  528. $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src);
  529. if ( ! empty( $tab ) )
  530. $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src);
  531. /**
  532. * Filters the upload iframe source URL for a specific media type.
  533. *
  534. * The dynamic portion of the hook name, `$type`, refers to the type
  535. * of media uploaded.
  536. *
  537. * @since 3.0.0
  538. *
  539. * @param string $upload_iframe_src The upload iframe source URL by type.
  540. */
  541. $upload_iframe_src = apply_filters( "{$type}_upload_iframe_src", $upload_iframe_src );
  542. return add_query_arg('TB_iframe', true, $upload_iframe_src);
  543. }
  544. /**
  545. * Handles form submissions for the legacy media uploader.
  546. *
  547. * @since 2.5.0
  548. *
  549. * @return mixed void|object WP_Error on failure
  550. */
  551. function media_upload_form_handler() {
  552. check_admin_referer('media-form');
  553. $errors = null;
  554. if ( isset($_POST['send']) ) {
  555. $keys = array_keys( $_POST['send'] );
  556. $send_id = (int) reset( $keys );
  557. }
  558. if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
  559. $post = $_post = get_post($attachment_id, ARRAY_A);
  560. if ( !current_user_can( 'edit_post', $attachment_id ) )
  561. continue;
  562. if ( isset($attachment['post_content']) )
  563. $post['post_content'] = $attachment['post_content'];
  564. if ( isset($attachment['post_title']) )
  565. $post['post_title'] = $attachment['post_title'];
  566. if ( isset($attachment['post_excerpt']) )
  567. $post['post_excerpt'] = $attachment['post_excerpt'];
  568. if ( isset($attachment['menu_order']) )
  569. $post['menu_order'] = $attachment['menu_order'];
  570. if ( isset($send_id) && $attachment_id == $send_id ) {
  571. if ( isset($attachment['post_parent']) )
  572. $post['post_parent'] = $attachment['post_parent'];
  573. }
  574. /**
  575. * Filters the attachment fields to be saved.
  576. *
  577. * @since 2.5.0
  578. *
  579. * @see wp_get_attachment_metadata()
  580. *
  581. * @param array $post An array of post data.
  582. * @param array $attachment An array of attachment metadata.
  583. */
  584. $post = apply_filters( 'attachment_fields_to_save', $post, $attachment );
  585. if ( isset($attachment['image_alt']) ) {
  586. $image_alt = wp_unslash( $attachment['image_alt'] );
  587. if ( $image_alt != get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ) {
  588. $image_alt = wp_strip_all_tags( $image_alt, true );
  589. // Update_meta expects slashed.
  590. update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
  591. }
  592. }
  593. if ( isset($post['errors']) ) {
  594. $errors[$attachment_id] = $post['errors'];
  595. unset($post['errors']);
  596. }
  597. if ( $post != $_post )
  598. wp_update_post($post);
  599. foreach ( get_attachment_taxonomies($post) as $t ) {
  600. if ( isset($attachment[$t]) )
  601. wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
  602. }
  603. }
  604. if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?>
  605. <script type="text/javascript">
  606. var win = window.dialogArguments || opener || parent || top;
  607. win.tb_remove();
  608. </script>
  609. <?php
  610. exit;
  611. }
  612. if ( isset($send_id) ) {
  613. $attachment = wp_unslash( $_POST['attachments'][$send_id] );
  614. $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
  615. if ( !empty($attachment['url']) ) {
  616. $rel = '';
  617. if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
  618. $rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
  619. $html = "<a href='{$attachment['url']}'$rel>$html</a>";
  620. }
  621. /**
  622. * Filters the HTML markup for a media item sent to the editor.
  623. *
  624. * @since 2.5.0
  625. *
  626. * @see wp_get_attachment_metadata()
  627. *
  628. * @param string $html HTML markup for a media item sent to the editor.
  629. * @param int $send_id The first key from the $_POST['send'] data.
  630. * @param array $attachment Array of attachment metadata.
  631. */
  632. $html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment );
  633. return media_send_to_editor($html);
  634. }
  635. return $errors;
  636. }
  637. /**
  638. * Handles the process of uploading media.
  639. *
  640. * @since 2.5.0
  641. *
  642. * @return null|string
  643. */
  644. function wp_media_upload_handler() {
  645. $errors = array();
  646. $id = 0;
  647. if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
  648. check_admin_referer('media-form');
  649. // Upload File button was clicked
  650. $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
  651. unset($_FILES);
  652. if ( is_wp_error($id) ) {
  653. $errors['upload_error'] = $id;
  654. $id = false;
  655. }
  656. }
  657. if ( !empty($_POST['insertonlybutton']) ) {
  658. $src = $_POST['src'];
  659. if ( !empty($src) && !strpos($src, '://') )
  660. $src = "http://$src";
  661. if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
  662. $title = esc_html( wp_unslash( $_POST['title'] ) );
  663. if ( empty( $title ) )
  664. $title = esc_html( basename( $src ) );
  665. if ( $title && $src )
  666. $html = "<a href='" . esc_url($src) . "'>$title</a>";
  667. $type = 'file';
  668. if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) )
  669. && ( 'audio' == $ext_type || 'video' == $ext_type ) )
  670. $type = $ext_type;
  671. /**
  672. * Filters the URL sent to the editor for a specific media type.
  673. *
  674. * The dynamic portion of the hook name, `$type`, refers to the type
  675. * of media being sent.
  676. *
  677. * @since 3.3.0
  678. *
  679. * @param string $html HTML markup sent to the editor.
  680. * @param string $src Media source URL.
  681. * @param string $title Media title.
  682. */
  683. $html = apply_filters( "{$type}_send_to_editor_url", $html, esc_url_raw( $src ), $title );
  684. } else {
  685. $align = '';
  686. $alt = esc_attr( wp_unslash( $_POST['alt'] ) );
  687. if ( isset($_POST['align']) ) {
  688. $align = esc_attr( wp_unslash( $_POST['align'] ) );
  689. $class = " class='align$align'";
  690. }
  691. if ( !empty($src) )
  692. $html = "<img src='" . esc_url($src) . "' alt='$alt'$class />";
  693. /**
  694. * Filters the image URL sent to the editor.
  695. *
  696. * @since 2.8.0
  697. *
  698. * @param string $html HTML markup sent to the editor for an image.
  699. * @param string $src Image source URL.
  700. * @param string $alt Image alternate, or alt, text.
  701. * @param string $align The image alignment. Default 'alignnone'. Possible values include
  702. * 'alignleft', 'aligncenter', 'alignright', 'alignnone'.
  703. */
  704. $html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align );
  705. }
  706. return media_send_to_editor($html);
  707. }
  708. if ( isset( $_POST['save'] ) ) {
  709. $errors['upload_notice'] = __('Saved.');
  710. wp_enqueue_script( 'admin-gallery' );
  711. return wp_iframe( 'media_upload_gallery_form', $errors );
  712. } elseif ( ! empty( $_POST ) ) {
  713. $return = media_upload_form_handler();
  714. if ( is_string($return) )
  715. return $return;
  716. if ( is_array($return) )
  717. $errors = $return;
  718. }
  719. if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) {
  720. $type = 'image';
  721. if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) )
  722. $type = $_GET['type'];
  723. return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
  724. }
  725. return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
  726. }
  727. /**
  728. * Downloads an image from the specified URL and attaches it to a post.
  729. *
  730. * @since 2.6.0
  731. * @since 4.2.0 Introduced the `$return` parameter.
  732. * @since 4.8.0 Introduced the 'id' option within the `$return` parameter.
  733. *
  734. * @param string $file The URL of the image to download.
  735. * @param int $post_id The post ID the media is to be associated with.
  736. * @param string $desc Optional. Description of the image.
  737. * @param string $return Optional. Accepts 'html' (image tag html) or 'src' (URL), or 'id' (attachment ID). Default 'html'.
  738. * @return string|WP_Error Populated HTML img tag on success, WP_Error object otherwise.
  739. */
  740. function media_sideload_image( $file, $post_id, $desc = null, $return = 'html' ) {
  741. if ( ! empty( $file ) ) {
  742. // Set variables for storage, fix file filename for query strings.
  743. preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
  744. if ( ! $matches ) {
  745. return new WP_Error( 'image_sideload_failed', __( 'Invalid image URL' ) );
  746. }
  747. $file_array = array();
  748. $file_array['name'] = basename( $matches[0] );
  749. // Download file to temp location.
  750. $file_array['tmp_name'] = download_url( $file );
  751. // If error storing temporarily, return the error.
  752. if ( is_wp_error( $file_array['tmp_name'] ) ) {
  753. return $file_array['tmp_name'];
  754. }
  755. // Do the validation and storage stuff.
  756. $id = media_handle_sideload( $file_array, $post_id, $desc );
  757. // If error storing permanently, unlink.
  758. if ( is_wp_error( $id ) ) {
  759. @unlink( $file_array['tmp_name'] );
  760. return $id;
  761. // If attachment id was requested, return it early.
  762. } elseif ( $return === 'id' ) {
  763. return $id;
  764. }
  765. $src = wp_get_attachment_url( $id );
  766. }
  767. // Finally, check to make sure the file has been saved, then return the HTML.
  768. if ( ! empty( $src ) ) {
  769. if ( $return === 'src' ) {
  770. return $src;
  771. }
  772. $alt = isset( $desc ) ? esc_attr( $desc ) : '';
  773. $html = "<img src='$src' alt='$alt' />";
  774. return $html;
  775. } else {
  776. return new WP_Error( 'image_sideload_failed' );
  777. }
  778. }
  779. /**
  780. * Retrieves the legacy media uploader form in an iframe.
  781. *
  782. * @since 2.5.0
  783. *
  784. * @return string|null
  785. */
  786. function media_upload_gallery() {
  787. $errors = array();
  788. if ( !empty($_POST) ) {
  789. $return = media_upload_form_handler();
  790. if ( is_string($return) )
  791. return $return;
  792. if ( is_array($return) )
  793. $errors = $return;
  794. }
  795. wp_enqueue_script('admin-gallery');
  796. return wp_iframe( 'media_upload_gallery_form', $errors );
  797. }
  798. /**
  799. * Retrieves the legacy media library form in an iframe.
  800. *
  801. * @since 2.5.0
  802. *
  803. * @return string|null
  804. */
  805. function media_upload_library() {
  806. $errors = array();
  807. if ( !empty($_POST) ) {
  808. $return = media_upload_form_handler();
  809. if ( is_string($return) )
  810. return $return;
  811. if ( is_array($return) )
  812. $errors = $return;
  813. }
  814. return wp_iframe( 'media_upload_library_form', $errors );
  815. }
  816. /**
  817. * Retrieve HTML for the image alignment radio buttons with the specified one checked.
  818. *
  819. * @since 2.7.0
  820. *
  821. * @param WP_Post $post
  822. * @param string $checked
  823. * @return string
  824. */
  825. function image_align_input_fields( $post, $checked = '' ) {
  826. if ( empty($checked) )
  827. $checked = get_user_setting('align', 'none');
  828. $alignments = array('none' => __('None'), 'left' => __('Left'), 'center' => __('Center'), 'right' => __('Right'));
  829. if ( !array_key_exists( (string) $checked, $alignments ) )
  830. $checked = 'none';
  831. $out = array();
  832. foreach ( $alignments as $name => $label ) {
  833. $name = esc_attr($name);
  834. $out[] = "<input type='radio' name='attachments[{$post->ID}][align]' id='image-align-{$name}-{$post->ID}' value='$name'".
  835. ( $checked == $name ? " checked='checked'" : "" ) .
  836. " /><label for='image-align-{$name}-{$post->ID}' class='align image-align-{$name}-label'>$label</label>";
  837. }
  838. return join("\n", $out);
  839. }
  840. /**
  841. * Retrieve HTML for the size radio buttons with the specified one checked.
  842. *
  843. * @since 2.7.0
  844. *
  845. * @param WP_Post $post
  846. * @param bool|string $check
  847. * @return array
  848. */
  849. function image_size_input_fields( $post, $check = '' ) {
  850. /**
  851. * Filters the names and labels of the default image sizes.
  852. *
  853. * @since 3.3.0
  854. *
  855. * @param array $size_names Array of image sizes and their names. Default values
  856. * include 'Thumbnail', 'Medium', 'Large', 'Full Size'.
  857. */
  858. $size_names = apply_filters( 'image_size_names_choose', array(
  859. 'thumbnail' => __( 'Thumbnail' ),
  860. 'medium' => __( 'Medium' ),
  861. 'large' => __( 'Large' ),
  862. 'full' => __( 'Full Size' )
  863. ) );
  864. if ( empty( $check ) ) {
  865. $check = get_user_setting('imgsize', 'medium');
  866. }
  867. $out = array();
  868. foreach ( $size_names as $size => $label ) {
  869. $downsize = image_downsize( $post->ID, $size );
  870. $checked = '';
  871. // Is this size selectable?
  872. $enabled = ( $downsize[3] || 'full' == $size );
  873. $css_id = "image-size-{$size}-{$post->ID}";
  874. // If this size is the default but that's not available, don't select it.
  875. if ( $size == $check ) {
  876. if ( $enabled ) {
  877. $checked = " checked='checked'";
  878. } else {
  879. $check = '';
  880. }
  881. } elseif ( ! $check && $enabled && 'thumbnail' != $size ) {
  882. /*
  883. * If $check is not enabled, default to the first available size
  884. * that's bigger than a thumbnail.
  885. */
  886. $check = $size;
  887. $checked = " checked='checked'";
  888. }
  889. $html = "<div class='image-size-item'><input type='radio' " . disabled( $enabled, false, false ) . "name='attachments[$post->ID][image-size]' id='{$css_id}' value='{$size}'$checked />";
  890. $html .= "<label for='{$css_id}'>$label</label>";
  891. // Only show the dimensions if that choice is available.
  892. if ( $enabled ) {
  893. $html .= " <label for='{$css_id}' class='help'>" . sprintf( "(%d&nbsp;&times;&nbsp;%d)", $downsize[1], $downsize[2] ). "</label>";
  894. }
  895. $html .= '</div>';
  896. $out[] = $html;
  897. }
  898. return array(
  899. 'label' => __( 'Size' ),
  900. 'input' => 'html',
  901. 'html' => join( "\n", $out ),
  902. );
  903. }
  904. /**
  905. * Retrieve HTML for the Link URL buttons with the default link type as specified.
  906. *
  907. * @since 2.7.0
  908. *
  909. * @param WP_Post $post
  910. * @param string $url_type
  911. * @return string
  912. */
  913. function image_link_input_fields($post, $url_type = '') {
  914. $file = wp_get_attachment_url($post->ID);
  915. $link = get_attachment_link($post->ID);
  916. if ( empty($url_type) )
  917. $url_type = get_user_setting('urlbutton', 'post');
  918. $url = '';
  919. if ( $url_type == 'file' )
  920. $url = $file;
  921. elseif ( $url_type == 'post' )
  922. $url = $link;
  923. return "
  924. <input type='text' class='text urlfield' name='attachments[$post->ID][url]' value='" . esc_attr($url) . "' /><br />
  925. <button type='button' class='button urlnone' data-link-url=''>" . __('None') . "</button>
  926. <button type='button' class='button urlfile' data-link-url='" . esc_attr($file) . "'>" . __('File URL') . "</button>
  927. <button type='button' class='button urlpost' data-link-url='" . esc_attr($link) . "'>" . __('Attachment Post URL') . "</button>
  928. ";
  929. }
  930. /**
  931. * Output a textarea element for inputting an attachment caption.
  932. *
  933. * @since 3.4.0
  934. *
  935. * @param WP_Post $edit_post Attachment WP_Post object.
  936. * @return string HTML markup for the textarea element.
  937. */
  938. function wp_caption_input_textarea($edit_post) {
  939. // Post data is already escaped.
  940. $name = "attachments[{$edit_post->ID}][post_excerpt]";
  941. return '<textarea name="' . $name . '" id="' . $name . '">' . $edit_post->post_excerpt . '</textarea>';
  942. }
  943. /**
  944. * Retrieves the image attachment fields to edit form fields.
  945. *
  946. * @since 2.5.0
  947. *
  948. * @param array $form_fields
  949. * @param object $post
  950. * @return array
  951. */
  952. function image_attachment_fields_to_edit($form_fields, $post) {
  953. return $form_fields;
  954. }
  955. /**
  956. * Retrieves the single non-image attachment fields to edit form fields.
  957. *
  958. * @since 2.5.0
  959. *
  960. * @param array $form_fields An array of attachment form fields.
  961. * @param WP_Post $post The WP_Post attachment object.
  962. * @return array Filtered attachment form fields.
  963. */
  964. function media_single_attachment_fields_to_edit( $form_fields, $post ) {
  965. unset($form_fields['url'], $form_fields['align'], $form_fields['image-size']);
  966. return $form_fields;
  967. }
  968. /**
  969. * Retrieves the post non-image attachment fields to edito form fields.
  970. *
  971. * @since 2.8.0
  972. *
  973. * @param array $form_fields An array of attachment form fields.
  974. * @param WP_Post $post The WP_Post attachment object.
  975. * @return array Filtered attachment form fields.
  976. */
  977. function media_post_single_attachment_fields_to_edit( $form_fields, $post ) {
  978. unset($form_fields['image_url']);
  979. return $form_fields;
  980. }
  981. /**
  982. * Filters input from media_upload_form_handler() and assigns a default
  983. * post_title from the file name if none supplied.
  984. *
  985. * Illustrates the use of the {@see 'attachment_fields_to_save'} filter
  986. * which can be used to add default values to any field before saving to DB.
  987. *
  988. * @since 2.5.0
  989. *
  990. * @param array $post The WP_Post attachment object converted to an array.
  991. * @param array $attachment An array of attachment metadata.
  992. * @return array Filtered attachment post object.
  993. */
  994. function image_attachment_fields_to_save( $post, $attachment ) {
  995. if ( substr( $post['post_mime_type'], 0, 5 ) == 'image' ) {
  996. if ( strlen( trim( $post['post_title'] ) ) == 0 ) {
  997. $attachment_url = ( isset( $post['attachment_url'] ) ) ? $post['attachment_url'] : $post['guid'];
  998. $post['post_title'] = preg_replace( '/\.\w+$/', '', wp_basename( $attachment_url ) );
  999. $post['errors']['post_title']['errors'][] = __( 'Empty Title filled from filename.' );
  1000. }
  1001. }
  1002. return $post;
  1003. }
  1004. /**
  1005. * Retrieves the media element HTML to send to the editor.
  1006. *
  1007. * @since 2.5.0
  1008. *
  1009. * @param string $html
  1010. * @param integer $attachment_id
  1011. * @param array $attachment
  1012. * @return string
  1013. */
  1014. function image_media_send_to_editor($html, $attachment_id, $attachment) {
  1015. $post = get_post($attachment_id);
  1016. if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  1017. $url = $attachment['url'];
  1018. $align = !empty($attachment['align']) ? $attachment['align'] : 'none';
  1019. $size = !empty($attachment['image-size']) ? $attachment['image-size'] : 'medium';
  1020. $alt = !empty($attachment['image_alt']) ? $attachment['image_alt'] : '';
  1021. $rel = ( strpos( $url, 'attachment_id') || $url === get_attachment_link( $attachment_id ) );
  1022. return get_image_send_to_editor($attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt);
  1023. }
  1024. return $html;
  1025. }
  1026. /**
  1027. * Retrieves the attachment fields to edit form fields.
  1028. *
  1029. * @since 2.5.0
  1030. *
  1031. * @param WP_Post $post
  1032. * @param array $errors
  1033. * @return array
  1034. */
  1035. function get_attachment_fields_to_edit($post, $errors = null) {
  1036. if ( is_int($post) )
  1037. $post = get_post($post);
  1038. if ( is_array($post) )
  1039. $post = new WP_Post( (object) $post );
  1040. $image_url = wp_get_attachment_url($post->ID);
  1041. $edit_post = sanitize_post($post, 'edit');
  1042. $form_fields = array(
  1043. 'post_title' => array(
  1044. 'label' => __('Title'),
  1045. 'value' => $edit_post->post_title
  1046. ),
  1047. 'image_alt' => array(),
  1048. 'post_excerpt' => array(
  1049. 'label' => __('Caption'),
  1050. 'input' => 'html',
  1051. 'html' => wp_caption_input_textarea($edit_post)
  1052. ),
  1053. 'post_content' => array(
  1054. 'label' => __('Description'),
  1055. 'value' => $edit_post->post_content,
  1056. 'input' => 'textarea'
  1057. ),
  1058. 'url' => array(
  1059. 'label' => __('Link URL'),
  1060. 'input' => 'html',
  1061. 'html' => image_link_input_fields($post, get_option('image_default_link_type')),
  1062. 'helps' => __('Enter a link URL or click above for presets.')
  1063. ),
  1064. 'menu_order' => array(
  1065. 'label' => __('Order'),
  1066. 'value' => $edit_post->menu_order
  1067. ),
  1068. 'image_url' => array(
  1069. 'label' => __('File URL'),
  1070. 'input' => 'html',
  1071. 'html' => "<input type='text' class='text urlfield' readonly='readonly' name='attachments[$post->ID][url]' value='" . esc_attr($image_url) . "' /><br />",
  1072. 'value' => wp_get_attachment_url($post->ID),
  1073. 'helps' => __('Location of the uploaded file.')
  1074. )
  1075. );
  1076. foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
  1077. $t = (array) get_taxonomy($taxonomy);
  1078. if ( ! $t['public'] || ! $t['show_ui'] )
  1079. continue;
  1080. if ( empty($t['label']) )
  1081. $t['label'] = $taxonomy;
  1082. if ( empty($t['args']) )
  1083. $t['args'] = array();
  1084. $terms = get_object_term_cache($post->ID, $taxonomy);
  1085. if ( false === $terms )
  1086. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
  1087. $values = array();
  1088. foreach ( $terms as $term )
  1089. $values[] = $term->slug;
  1090. $t['value'] = join(', ', $values);
  1091. $form_fields[$taxonomy] = $t;
  1092. }
  1093. // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  1094. // The recursive merge is easily traversed with array casting: foreach ( (array) $things as $thing )
  1095. $form_fields = array_merge_recursive($form_fields, (array) $errors);
  1096. // This was formerly in image_attachment_fields_to_edit().
  1097. if ( substr($post->post_mime_type, 0, 5) == 'image' ) {
  1098. $alt = get_post_meta($post->ID, '_wp_attachment_image_alt', true);
  1099. if ( empty($alt) )
  1100. $alt = '';
  1101. $form_fields['post_title']['required'] = true;
  1102. $form_fields['image_alt'] = array(
  1103. 'value' => $alt,
  1104. 'label' => __('Alternative Text'),
  1105. 'helps' => __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;')
  1106. );
  1107. $form_fields['align'] = array(
  1108. 'label' => __('Alignment'),
  1109. 'input' => 'html',
  1110. 'html' => image_align_input_fields($post, get_option('image_default_align')),
  1111. );
  1112. $form_fields['image-size'] = image_size_input_fields( $post, get_option('image_default_size', 'medium') );
  1113. } else {
  1114. unset( $form_fields['image_alt'] );
  1115. }
  1116. /**
  1117. * Filters the attachment fields to edit.
  1118. *
  1119. * @since 2.5.0
  1120. *
  1121. * @param array $form_fields An array of attachment form fields.
  1122. * @param WP_Post $post The WP_Post attachment object.
  1123. */
  1124. $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
  1125. return $form_fields;
  1126. }
  1127. /**
  1128. * Retrieve HTML for media items of post gallery.
  1129. *
  1130. * The HTML markup retrieved will be created for the progress of SWF Upload
  1131. * component. Will also create link for showing and hiding the form to modify
  1132. * the image attachment.
  1133. *
  1134. * @since 2.5.0
  1135. *
  1136. * @global WP_Query $wp_the_query
  1137. *
  1138. * @param int $post_id Optional. Post ID.
  1139. * @param array $errors Errors for attachment, if any.
  1140. * @return string
  1141. */
  1142. function get_media_items( $post_id, $errors ) {
  1143. $attachments = array();
  1144. if ( $post_id ) {
  1145. $post = get_post($post_id);
  1146. if ( $post && $post->post_type == 'attachment' )
  1147. $attachments = array($post->ID => $post);
  1148. else
  1149. $attachments = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );
  1150. } else {
  1151. if ( is_array($GLOBALS['wp_the_query']->posts) )
  1152. foreach ( $GLOBALS['wp_the_query']->posts as $attachment )
  1153. $attachments[$attachment->ID] = $attachment;
  1154. }
  1155. $output = '';
  1156. foreach ( (array) $attachments as $id => $attachment ) {
  1157. if ( $attachment->post_status == 'trash' )
  1158. continue;
  1159. if ( $item = get_media_item( $id, array( 'errors' => isset($errors[$id]) ? $errors[$id] : null) ) )
  1160. $output .= "\n<div id='media-item-$id' class='media-item child-of-$attachment->post_parent preloaded'><div class='progress hidden'><div class='bar'></div></div><div id='media-upload-error-$id' class='hidden'></div><div class='filename hidden'></div>$item\n</div>";
  1161. }
  1162. return $output;
  1163. }
  1164. /**
  1165. * Retrieve HTML form for modifying the image attachment.
  1166. *
  1167. * @since 2.5.0
  1168. *
  1169. * @global string $redir_tab
  1170. *
  1171. * @param int $attachment_id Attachment ID for modification.
  1172. * @param string|array $args Optional. Override defaults.
  1173. * @return string HTML form for attachment.
  1174. */
  1175. function get_media_item( $attachment_id, $args = null ) {
  1176. global $redir_tab;
  1177. if ( ( $attachment_id = intval( $attachment_id ) ) && $thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true ) )
  1178. $thumb_url = $thumb_url[0];
  1179. else
  1180. $thumb_url = false;
  1181. $post = get_post( $attachment_id );
  1182. $current_post_id = !empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;
  1183. $default_args = array(
  1184. 'errors' => null,
  1185. 'send' => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true,
  1186. 'delete' => true,
  1187. 'toggle' => true,
  1188. 'show_title' => true
  1189. );
  1190. $args = wp_parse_args( $args, $default_args );
  1191. /**
  1192. * Filters the arguments used to retrieve an image for the edit image form.
  1193. *
  1194. * @since 3.1.0
  1195. *
  1196. * @see get_media_item
  1197. *
  1198. * @param array $args An array of arguments.
  1199. */
  1200. $r = apply_filters( 'get_media_item_args', $args );
  1201. $toggle_on = __( 'Show' );
  1202. $toggle_off = __( 'Hide' );
  1203. $file = get_attached_file( $post->ID );
  1204. $filename = esc_html( wp_basename( $file ) );
  1205. $title = esc_attr( $post->post_title );
  1206. $post_mime_types = get_post_mime_types();
  1207. $keys = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
  1208. $type = reset( $keys );
  1209. $type_html = "<input type='hidden' id='type-of-$attachment_id' value='" . esc_attr( $type ) . "' />";
  1210. $form_fields = get_attachment_fields_to_edit( $post, $r['errors'] );
  1211. if ( $r['toggle'] ) {
  1212. $class = empty( $r['errors'] ) ? 'startclosed' : 'startopen';
  1213. $toggle_links = "
  1214. <a class='toggle describe-toggle-on' href='#'>$toggle_on</a>
  1215. <a class='toggle describe-toggle-off' href='#'>$toggle_off</a>";
  1216. } else {
  1217. $class = '';
  1218. $toggle_links = '';
  1219. }
  1220. $display_title = ( !empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case
  1221. $display_title = $r['show_title'] ? "<div class='filename new'><span class='title'>" . wp_html_excerpt( $display_title, 60, '&hellip;' ) . "</span></div>" : '';
  1222. $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' == $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' == $redir_tab ) );
  1223. $order = '';
  1224. foreach ( $form_fields as $key => $val ) {
  1225. if ( 'menu_order' == $key ) {
  1226. if ( $gallery )
  1227. $order = "<div class='menu_order'> <input class='menu_order_input' type='text' id='attachments[$attachment_id][menu_order]' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ). "' /></div>";
  1228. else
  1229. $order = "<input type='hidden' name='attachments[$attachment_id][menu_order]' value='" . esc_attr( $val['value'] ) . "' />";
  1230. unset( $form_fields['menu_order'] );
  1231. break;
  1232. }
  1233. }
  1234. $media_dims = '';
  1235. $meta = wp_get_attachment_metadata( $post->ID );
  1236. if ( isset( $meta['width'], $meta['height'] ) )
  1237. $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  1238. /**
  1239. * Filters the media metadata.
  1240. *
  1241. * @since 2.5.0
  1242. *
  1243. * @param string $media_dims The HTML markup containing the media dimensions.
  1244. * @param WP_Post $post The WP_Post attachment object.
  1245. */
  1246. $media_dims = apply_filters( 'media_meta', $media_dims, $post );
  1247. $image_edit_button = '';
  1248. if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  1249. $nonce = wp_create_nonce( "image_editor-$post->ID" );
  1250. $image_edit_button = "<input type='button' id='imgedit-open-btn-$post->ID' onclick='imageEdit.open( $post->ID, \"$nonce\" )' class='button' value='" . esc_attr__( 'Edit Image' ) . "' /> <span class='spinner'></span>";
  1251. }
  1252. $attachment_url = get_permalink( $attachment_id );
  1253. $item = "
  1254. $type_html
  1255. $toggle_links
  1256. $order
  1257. $display_title
  1258. <table class='slidetoggle describe $class'>
  1259. <thead class='media-item-info' id='media-head-$post->ID'>
  1260. <tr>
  1261. <td class='A1B1' id='thumbnail-head-$post->ID'>
  1262. <p><a href='$attachment_url' target='_blank'><img class='thumbnail' src='$thumb_url' alt='' /></a></p>
  1263. <p>$image_edit_button</p>
  1264. </td>
  1265. <td>
  1266. <p><strong>" . __('File name:') . "</strong> $filename</p>
  1267. <p><strong>" . __('File type:') . "</strong> $post->post_mime_type</p>
  1268. <p><strong>" . __('Upload date:') . "</strong> " . mysql2date( __( 'F j, Y' ), $post->post_date ). '</p>';
  1269. if ( !empty( $media_dims ) )
  1270. $item .= "<p><strong>" . __('Dimensions:') . "</strong> $media_dims</p>\n";
  1271. $item .= "</td></tr>\n";
  1272. $item .= "
  1273. </thead>
  1274. <tbody>
  1275. <tr><td colspan='2' class='imgedit-response' id='imgedit-response-$post->ID'></td></tr>\n
  1276. <tr><td style='display:none' colspan='2' class='image-editor' id='image-editor-$post->ID'></td></tr>\n
  1277. <tr><td colspan='2'><p class='media-types media-types-required-info'>" . sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) . "</p></td></tr>\n";
  1278. $defaults = array(
  1279. 'input' => 'text',
  1280. 'required' => false,
  1281. 'value' => '',
  1282. 'extra_rows' => array(),
  1283. );
  1284. if ( $r['send'] ) {
  1285. $r['send'] = get_submit_button( __( 'Insert into Post' ), '', "send[$attachment_id]", false );
  1286. }
  1287. $delete = empty( $r['delete'] ) ? '' : $r['delete'];
  1288. if ( $delete && current_user_can( 'delete_post', $attachment_id ) ) {
  1289. if ( !EMPTY_TRASH_DAYS ) {
  1290. $delete = "<a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete-permanently'>" . __( 'Delete Permanently' ) . '</a>';
  1291. } elseif ( !MEDIA_TRASH ) {
  1292. $delete = "<a href='#' class='del-link' onclick=\"document.getElementById('del_attachment_$attachment_id').style.display='block';return false;\">" . __( 'Delete' ) . "</a>
  1293. <div id='del_attachment_$attachment_id' class='del-attachment' style='display:none;'>" .
  1294. /* translators: %s: file name */
  1295. '<p>' . sprintf( __( 'You are about to delete %s.' ), '<strong>' . $filename . '</strong>' ) . "</p>
  1296. <a href='" . wp_nonce_url( "post.php?action=delete&amp;post=$attachment_id", 'delete-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='button'>" . __( 'Continue' ) . "</a>
  1297. <a href='#' class='button' onclick=\"this.parentNode.style.display='none';return false;\">" . __( 'Cancel' ) . "</a>
  1298. </div>";
  1299. } else {
  1300. $delete = "<a href='" . wp_nonce_url( "post.php?action=trash&amp;post=$attachment_id", 'trash-post_' . $attachment_id ) . "' id='del[$attachment_id]' class='delete'>" . __( 'Move to Trash' ) . "</a>
  1301. <a href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$attachment_id", 'untrash-post_' . $attachment_id ) . "' id='undo[$attachment_id]' class='undo hidden'>" . __( 'Undo' ) . "</a>";
  1302. }
  1303. } else {
  1304. $delete = '';
  1305. }
  1306. $thumbnail = '';
  1307. $calling_post_id = 0;
  1308. if ( isset( $_GET['post_id'] ) ) {
  1309. $calling_post_id = absint( $_GET['post_id'] );
  1310. } elseif ( isset( $_POST ) && count( $_POST ) ) {// Like for async-upload where $_GET['post_id'] isn't set
  1311. $calling_post_id = $post->post_parent;
  1312. }
  1313. if ( 'image' == $type && $calling_post_id && current_theme_supports( 'post-thumbnails', get_post_type( $calling_post_id ) )
  1314. && post_type_supports( get_post_type( $calling_post_id ), 'thumbnail' ) && get_post_thumbnail_id( $calling_post_id ) != $attachment_id ) {
  1315. $calling_post = get_post( $calling_post_id );
  1316. $calling_post_type_object = get_post_type_object( $calling_post->post_type );
  1317. $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$calling_post_id" );
  1318. $thumbnail = "<a class='wp-post-thumbnail' id='wp-post-thumbnail-" . $attachment_id . "' href='#' onclick='WPSetAsThumbnail(\"$attachment_id\", \"$ajax_nonce\");return false;'>" . esc_html( $calling_post_type_object->labels->use_featured_image ) . "</a>";
  1319. }
  1320. if ( ( $r['send'] || $thumbnail || $delete ) && !isset( $form_fields['buttons'] ) ) {
  1321. $form_fields['buttons'] = array( 'tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>" . $r['send'] . " $thumbnail $delete</td></tr>\n" );
  1322. }
  1323. $hidden_fields = array();
  1324. foreach ( $form_fields as $id => $field ) {
  1325. if ( $id[0] == '_' )
  1326. continue;
  1327. if ( !empty( $field['tr'] ) ) {
  1328. $item .= $field['tr'];
  1329. continue;
  1330. }
  1331. $field = array_merge( $defaults, $field );
  1332. $name = "attachments[$attachment_id][$id]";
  1333. if ( $field['input'] == 'hidden' ) {
  1334. $hidden_fields[$name] = $field['value'];
  1335. continue;
  1336. }
  1337. $required = $field['required'] ? '<span class="required">*</span>' : '';
  1338. $required_attr = $field['required'] ? ' required' : '';
  1339. $aria_required = $field['required'] ? " aria-required='true'" : '';
  1340. $class = $id;
  1341. $class .= $field['required'] ? ' form-required' : '';
  1342. $item .= "\t\t<tr class='$class'>\n\t\t\t<th scope='row' class='label'><label for='$name'><span class='alignleft'>{$field['label']}{$required}</span><br class='clear' /></label></th>\n\t\t\t<td class='field'>";
  1343. if ( !empty( $field[ $field['input'] ] ) )
  1344. $item .= $field[ $field['input'] ];
  1345. elseif ( $field['input'] == 'textarea' ) {
  1346. if ( 'post_content' == $id && user_can_richedit() ) {
  1347. // Sanitize_post() skips the post_content when user_can_richedit.
  1348. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1349. }
  1350. // Post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit().
  1351. $item .= "<textarea id='$name' name='$name'{$required_attr}{$aria_required}>" . $field['value'] . '</textarea>';
  1352. } else {
  1353. $item .= "<input type='text' class='text' id='$name' name='$name' value='" . esc_attr( $field['value'] ) . "'{$required_attr}{$aria_required} />";
  1354. }
  1355. if ( !empty( $field['helps'] ) )
  1356. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1357. $item .= "</td>\n\t\t</tr>\n";
  1358. $extra_rows = array();
  1359. if ( !empty( $field['errors'] ) )
  1360. foreach ( array_unique( (array) $field['errors'] ) as $error )
  1361. $extra_rows['error'][] = $error;
  1362. if ( !empty( $field['extra_rows'] ) )
  1363. foreach ( $field['extra_rows'] as $class => $rows )
  1364. foreach ( (array) $rows as $html )
  1365. $extra_rows[$class][] = $html;
  1366. foreach ( $extra_rows as $class => $rows )
  1367. foreach ( $rows as $html )
  1368. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  1369. }
  1370. if ( !empty( $form_fields['_final'] ) )
  1371. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1372. $item .= "\t</tbody>\n";
  1373. $item .= "\t</table>\n";
  1374. foreach ( $hidden_fields as $name => $value )
  1375. $item .= "\t<input type='hidden' name='$name' id='$name' value='" . esc_attr( $value ) . "' />\n";
  1376. if ( $post->post_parent < 1 && isset( $_REQUEST['post_id'] ) ) {
  1377. $parent = (int) $_REQUEST['post_id'];
  1378. $parent_name = "attachments[$attachment_id][post_parent]";
  1379. $item .= "\t<input type='hidden' name='$parent_name' id='$parent_name' value='$parent' />\n";
  1380. }
  1381. return $item;
  1382. }
  1383. /**
  1384. * @since 3.5.0
  1385. *
  1386. * @param int $attachment_id
  1387. * @param array $args
  1388. * @return array
  1389. */
  1390. function get_compat_media_markup( $attachment_id, $args = null ) {
  1391. $post = get_post( $attachment_id );
  1392. $default_args = array(
  1393. 'errors' => null,
  1394. 'in_modal' => false,
  1395. );
  1396. $user_can_edit = current_user_can( 'edit_post', $attachment_id );
  1397. $args = wp_parse_args( $args, $default_args );
  1398. /** This filter is documented in wp-admin/includes/media.php */
  1399. $args = apply_filters( 'get_media_item_args', $args );
  1400. $form_fields = array();
  1401. if ( $args['in_modal'] ) {
  1402. foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
  1403. $t = (array) get_taxonomy($taxonomy);
  1404. if ( ! $t['public'] || ! $t['show_ui'] )
  1405. continue;
  1406. if ( empty($t['label']) )
  1407. $t['label'] = $taxonomy;
  1408. if ( empty($t['args']) )
  1409. $t['args'] = array();
  1410. $terms = get_object_term_cache($post->ID, $taxonomy);
  1411. if ( false === $terms )
  1412. $terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);
  1413. $values = array();
  1414. foreach ( $terms as $term )
  1415. $values[] = $term->slug;
  1416. $t['value'] = join(', ', $values);
  1417. $t['taxonomy'] = true;
  1418. $form_fields[$taxonomy] = $t;
  1419. }
  1420. }
  1421. // Merge default fields with their errors, so any key passed with the error (e.g. 'error', 'helps', 'value') will replace the default
  1422. // The recursive merge is easily traversed with array casting: foreach ( (array) $things as $thing )
  1423. $form_fields = array_merge_recursive($form_fields, (array) $args['errors'] );
  1424. /** This filter is documented in wp-admin/includes/media.php */
  1425. $form_fields = apply_filters( 'attachment_fields_to_edit', $form_fields, $post );
  1426. unset( $form_fields['image-size'], $form_fields['align'], $form_fields['image_alt'],
  1427. $form_fields['post_title'], $form_fields['post_excerpt'], $form_fields['post_content'],
  1428. $form_fields['url'], $form_fields['menu_order'], $form_fields['image_url'] );
  1429. /** This filter is documented in wp-admin/includes/media.php */
  1430. $media_meta = apply_filters( 'media_meta', '', $post );
  1431. $defaults = array(
  1432. 'input' => 'text',
  1433. 'required' => false,
  1434. 'value' => '',
  1435. 'extra_rows' => array(),
  1436. 'show_in_edit' => true,
  1437. 'show_in_modal' => true,
  1438. );
  1439. $hidden_fields = array();
  1440. $item = '';
  1441. foreach ( $form_fields as $id => $field ) {
  1442. if ( $id[0] == '_' )
  1443. continue;
  1444. $name = "attachments[$attachment_id][$id]";
  1445. $id_attr = "attachments-$attachment_id-$id";
  1446. if ( !empty( $field['tr'] ) ) {
  1447. $item .= $field['tr'];
  1448. continue;
  1449. }
  1450. $field = array_merge( $defaults, $field );
  1451. if ( ( ! $field['show_in_edit'] && ! $args['in_modal'] ) || ( ! $field['show_in_modal'] && $args['in_modal'] ) )
  1452. continue;
  1453. if ( $field['input'] == 'hidden' ) {
  1454. $hidden_fields[$name] = $field['value'];
  1455. continue;
  1456. }
  1457. $readonly = ! $user_can_edit && ! empty( $field['taxonomy'] ) ? " readonly='readonly' " : '';
  1458. $required = $field['required'] ? '<span class="required">*</span>' : '';
  1459. $required_attr = $field['required'] ? ' required' : '';
  1460. $aria_required = $field['required'] ? " aria-required='true'" : '';
  1461. $class = 'compat-field-' . $id;
  1462. $class .= $field['required'] ? ' form-required' : '';
  1463. $item .= "\t\t<tr class='$class'>";
  1464. $item .= "\t\t\t<th scope='row' class='label'><label for='$id_attr'><span class='alignleft'>{$field['label']}</span>$required<br class='clear' /></label>";
  1465. $item .= "</th>\n\t\t\t<td class='field'>";
  1466. if ( !empty( $field[ $field['input'] ] ) )
  1467. $item .= $field[ $field['input'] ];
  1468. elseif ( $field['input'] == 'textarea' ) {
  1469. if ( 'post_content' == $id && user_can_richedit() ) {
  1470. // sanitize_post() skips the post_content when user_can_richedit.
  1471. $field['value'] = htmlspecialchars( $field['value'], ENT_QUOTES );
  1472. }
  1473. $item .= "<textarea id='$id_attr' name='$name'{$required_attr}{$aria_required}>" . $field['value'] . '</textarea>';
  1474. } else {
  1475. $item .= "<input type='text' class='text' id='$id_attr' name='$name' value='" . esc_attr( $field['value'] ) . "' $readonly{$required_attr}{$aria_required} />";
  1476. }
  1477. if ( !empty( $field['helps'] ) )
  1478. $item .= "<p class='help'>" . join( "</p>\n<p class='help'>", array_unique( (array) $field['helps'] ) ) . '</p>';
  1479. $item .= "</td>\n\t\t</tr>\n";
  1480. $extra_rows = array();
  1481. if ( !empty( $field['errors'] ) )
  1482. foreach ( array_unique( (array) $field['errors'] ) as $error )
  1483. $extra_rows['error'][] = $error;
  1484. if ( !empty( $field['extra_rows'] ) )
  1485. foreach ( $field['extra_rows'] as $class => $rows )
  1486. foreach ( (array) $rows as $html )
  1487. $extra_rows[$class][] = $html;
  1488. foreach ( $extra_rows as $class => $rows )
  1489. foreach ( $rows as $html )
  1490. $item .= "\t\t<tr><td></td><td class='$class'>$html</td></tr>\n";
  1491. }
  1492. if ( !empty( $form_fields['_final'] ) )
  1493. $item .= "\t\t<tr class='final'><td colspan='2'>{$form_fields['_final']}</td></tr>\n";
  1494. if ( $item ) {
  1495. $item = '<p class="media-types media-types-required-info">' .
  1496. sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) . '</p>
  1497. <table class="compat-attachment-fields">' . $item . '</table>';
  1498. }
  1499. foreach ( $hidden_fields as $hidden_field => $value ) {
  1500. $item .= '<input type="hidden" name="' . esc_attr( $hidden_field ) . '" value="' . esc_attr( $value ) . '" />' . "\n";
  1501. }
  1502. if ( $item )
  1503. $item = '<input type="hidden" name="attachments[' . $attachment_id . '][menu_order]" value="' . esc_attr( $post->menu_order ) . '" />' . $item;
  1504. return array(
  1505. 'item' => $item,
  1506. 'meta' => $media_meta,
  1507. );
  1508. }
  1509. /**
  1510. * Outputs the legacy media upload header.
  1511. *
  1512. * @since 2.5.0
  1513. */
  1514. function media_upload_header() {
  1515. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1516. echo '<script type="text/javascript">post_id = ' . $post_id . ';</script>';
  1517. if ( empty( $_GET['chromeless'] ) ) {
  1518. echo '<div id="media-upload-header">';
  1519. the_media_upload_tabs();
  1520. echo '</div>';
  1521. }
  1522. }
  1523. /**
  1524. * Outputs the legacy media upload form.
  1525. *
  1526. * @since 2.5.0
  1527. *
  1528. * @global string $type
  1529. * @global string $tab
  1530. * @global bool $is_IE
  1531. * @global bool $is_opera
  1532. *
  1533. * @param array $errors
  1534. */
  1535. function media_upload_form( $errors = null ) {
  1536. global $type, $tab, $is_IE, $is_opera;
  1537. if ( ! _device_can_upload() ) {
  1538. echo '<p>' . sprintf( __('The web browser on your device cannot be used to upload files. You may be able to use the <a href="%s">native app for your device</a> instead.'), 'https://apps.wordpress.org/' ) . '</p>';
  1539. return;
  1540. }
  1541. $upload_action_url = admin_url('async-upload.php');
  1542. $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
  1543. $_type = isset($type) ? $type : '';
  1544. $_tab = isset($tab) ? $tab : '';
  1545. $max_upload_size = wp_max_upload_size();
  1546. if ( ! $max_upload_size ) {
  1547. $max_upload_size = 0;
  1548. }
  1549. ?>
  1550. <div id="media-upload-notice"><?php
  1551. if (isset($errors['upload_notice']) )
  1552. echo $errors['upload_notice'];
  1553. ?></div>
  1554. <div id="media-upload-error"><?php
  1555. if (isset($errors['upload_error']) && is_wp_error($errors['upload_error']))
  1556. echo $errors['upload_error']->get_error_message();
  1557. ?></div>
  1558. <?php
  1559. if ( is_multisite() && !is_upload_space_available() ) {
  1560. /**
  1561. * Fires when an upload will exceed the defined upload space quota for a network site.
  1562. *
  1563. * @since 3.5.0
  1564. */
  1565. do_action( 'upload_ui_over_quota' );
  1566. return;
  1567. }
  1568. /**
  1569. * Fires just before the legacy (pre-3.5.0) upload interface is loaded.
  1570. *
  1571. * @since 2.6.0
  1572. */
  1573. do_action( 'pre-upload-ui' );
  1574. $post_params = array(
  1575. "post_id" => $post_id,
  1576. "_wpnonce" => wp_create_nonce('media-form'),
  1577. "type" => $_type,
  1578. "tab" => $_tab,
  1579. "short" => "1",
  1580. );
  1581. /**
  1582. * Filters the media upload post parameters.
  1583. *
  1584. * @since 3.1.0 As 'swfupload_post_params'
  1585. * @since 3.3.0
  1586. *
  1587. * @param array $post_params An array of media upload parameters used by Plupload.
  1588. */
  1589. $post_params = apply_filters( 'upload_post_params', $post_params );
  1590. /*
  1591. * Since 4.9 the `runtimes` setting is hardcoded in our version of Plupload to `html5,html4`,
  1592. * and the `flash_swf_url` and `silverlight_xap_url` are not used.
  1593. */
  1594. $plupload_init = array(
  1595. 'browse_button' => 'plupload-browse-button',
  1596. 'container' => 'plupload-upload-ui',
  1597. 'drop_element' => 'drag-drop-area',
  1598. 'file_data_name' => 'async-upload',
  1599. 'url' => $upload_action_url,
  1600. 'filters' => array(
  1601. 'max_file_size' => $max_upload_size . 'b',
  1602. ),
  1603. 'multipart_params' => $post_params,
  1604. );
  1605. // Currently only iOS Safari supports multiple files uploading but iOS 7.x has a bug that prevents uploading of videos
  1606. // when enabled. See #29602.
  1607. if ( wp_is_mobile() && strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false &&
  1608. strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false ) {
  1609. $plupload_init['multi_selection'] = false;
  1610. }
  1611. /**
  1612. * Filters the default Plupload settings.
  1613. *
  1614. * @since 3.3.0
  1615. *
  1616. * @param array $plupload_init An array of default settings used by Plupload.
  1617. */
  1618. $plupload_init = apply_filters( 'plupload_init', $plupload_init );
  1619. ?>
  1620. <script type="text/javascript">
  1621. <?php
  1622. // Verify size is an int. If not return default value.
  1623. $large_size_h = absint( get_option('large_size_h') );
  1624. if( !$large_size_h )
  1625. $large_size_h = 1024;
  1626. $large_size_w = absint( get_option('large_size_w') );
  1627. if( !$large_size_w )
  1628. $large_size_w = 1024;
  1629. ?>
  1630. var resize_height = <?php echo $large_size_h; ?>, resize_width = <?php echo $large_size_w; ?>,
  1631. wpUploaderInit = <?php echo wp_json_encode( $plupload_init ); ?>;
  1632. </script>
  1633. <div id="plupload-upload-ui" class="hide-if-no-js">
  1634. <?php
  1635. /**
  1636. * Fires before the upload interface loads.
  1637. *
  1638. * @since 2.6.0 As 'pre-flash-upload-ui'
  1639. * @since 3.3.0
  1640. */
  1641. do_action( 'pre-plupload-upload-ui' ); ?>
  1642. <div id="drag-drop-area">
  1643. <div class="drag-drop-inside">
  1644. <p class="drag-drop-info"><?php _e('Drop files here'); ?></p>
  1645. <p><?php _ex('or', 'Uploader: Drop files here - or - Select Files'); ?></p>
  1646. <p class="drag-drop-buttons"><input id="plupload-browse-button" type="button" value="<?php esc_attr_e('Select Files'); ?>" class="button" /></p>
  1647. </div>
  1648. </div>
  1649. <?php
  1650. /**
  1651. * Fires after the upload interface loads.
  1652. *
  1653. * @since 2.6.0 As 'post-flash-upload-ui'
  1654. * @since 3.3.0
  1655. */
  1656. do_action( 'post-plupload-upload-ui' ); ?>
  1657. </div>
  1658. <div id="html-upload-ui" class="hide-if-js">
  1659. <?php
  1660. /**
  1661. * Fires before the upload button in the media upload interface.
  1662. *
  1663. * @since 2.6.0
  1664. */
  1665. do_action( 'pre-html-upload-ui' );
  1666. ?>
  1667. <p id="async-upload-wrap">
  1668. <label class="screen-reader-text" for="async-upload"><?php _e('Upload'); ?></label>
  1669. <input type="file" name="async-upload" id="async-upload" />
  1670. <?php submit_button( __( 'Upload' ), 'primary', 'html-upload', false ); ?>
  1671. <a href="#" onclick="try{top.tb_remove();}catch(e){}; return false;"><?php _e('Cancel'); ?></a>
  1672. </p>
  1673. <div class="clear"></div>
  1674. <?php
  1675. /**
  1676. * Fires after the upload button in the media upload interface.
  1677. *
  1678. * @since 2.6.0
  1679. */
  1680. do_action( 'post-html-upload-ui' );
  1681. ?>
  1682. </div>
  1683. <p class="max-upload-size"><?php printf( __( 'Maximum upload file size: %s.' ), esc_html( size_format( $max_upload_size ) ) ); ?></p>
  1684. <?php
  1685. /**
  1686. * Fires on the post upload UI screen.
  1687. *
  1688. * Legacy (pre-3.5.0) media workflow hook.
  1689. *
  1690. * @since 2.6.0
  1691. */
  1692. do_action( 'post-upload-ui' );
  1693. }
  1694. /**
  1695. * Outputs the legacy media upload form for a given media type.
  1696. *
  1697. * @since 2.5.0
  1698. *
  1699. * @param string $type
  1700. * @param object $errors
  1701. * @param integer $id
  1702. */
  1703. function media_upload_type_form($type = 'file', $errors = null, $id = null) {
  1704. media_upload_header();
  1705. $post_id = isset( $_REQUEST['post_id'] )? intval( $_REQUEST['post_id'] ) : 0;
  1706. $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
  1707. /**
  1708. * Filters the media upload form action URL.
  1709. *
  1710. * @since 2.6.0
  1711. *
  1712. * @param string $form_action_url The media upload form action URL.
  1713. * @param string $type The type of media. Default 'file'.
  1714. */
  1715. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1716. $form_class = 'media-upload-form type-form validate';
  1717. if ( get_user_setting('uploader') )
  1718. $form_class .= ' html-uploader';
  1719. ?>
  1720. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form">
  1721. <?php submit_button( '', 'hidden', 'save', false ); ?>
  1722. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1723. <?php wp_nonce_field('media-form'); ?>
  1724. <h3 class="media-title"><?php _e('Add media files from your computer'); ?></h3>
  1725. <?php media_upload_form( $errors ); ?>
  1726. <script type="text/javascript">
  1727. jQuery(function($){
  1728. var preloaded = $(".media-item.preloaded");
  1729. if ( preloaded.length > 0 ) {
  1730. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1731. }
  1732. updateMediaForm();
  1733. });
  1734. </script>
  1735. <div id="media-items"><?php
  1736. if ( $id ) {
  1737. if ( !is_wp_error($id) ) {
  1738. add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2);
  1739. echo get_media_items( $id, $errors );
  1740. } else {
  1741. echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div></div>';
  1742. exit;
  1743. }
  1744. }
  1745. ?></div>
  1746. <p class="savebutton ml-submit">
  1747. <?php submit_button( __( 'Save all changes' ), '', 'save', false ); ?>
  1748. </p>
  1749. </form>
  1750. <?php
  1751. }
  1752. /**
  1753. * Outputs the legacy media upload form for external media.
  1754. *
  1755. * @since 2.7.0
  1756. *
  1757. * @param string $type
  1758. * @param object $errors
  1759. * @param integer $id
  1760. */
  1761. function media_upload_type_url_form($type = null, $errors = null, $id = null) {
  1762. if ( null === $type )
  1763. $type = 'image';
  1764. media_upload_header();
  1765. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  1766. $form_action_url = admin_url("media-upload.php?type=$type&tab=type&post_id=$post_id");
  1767. /** This filter is documented in wp-admin/includes/media.php */
  1768. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1769. $form_class = 'media-upload-form type-form validate';
  1770. if ( get_user_setting('uploader') )
  1771. $form_class .= ' html-uploader';
  1772. ?>
  1773. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="<?php echo $type; ?>-form">
  1774. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1775. <?php wp_nonce_field('media-form'); ?>
  1776. <h3 class="media-title"><?php _e('Insert media from another website'); ?></h3>
  1777. <script type="text/javascript">
  1778. var addExtImage = {
  1779. width : '',
  1780. height : '',
  1781. align : 'alignnone',
  1782. insert : function() {
  1783. var t = this, html, f = document.forms[0], cls, title = '', alt = '', caption = '';
  1784. if ( '' == f.src.value || '' == t.width )
  1785. return false;
  1786. if ( f.alt.value )
  1787. alt = f.alt.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1788. <?php
  1789. /** This filter is documented in wp-admin/includes/media.php */
  1790. if ( ! apply_filters( 'disable_captions', '' ) ) {
  1791. ?>
  1792. if ( f.caption.value ) {
  1793. caption = f.caption.value.replace(/\r\n|\r/g, '\n');
  1794. caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
  1795. return a.replace(/[\r\n\t]+/, ' ');
  1796. });
  1797. caption = caption.replace(/\s*\n\s*/g, '<br />');
  1798. }
  1799. <?php } ?>
  1800. cls = caption ? '' : ' class="'+t.align+'"';
  1801. html = '<img alt="'+alt+'" src="'+f.src.value+'"'+cls+' width="'+t.width+'" height="'+t.height+'" />';
  1802. if ( f.url.value ) {
  1803. url = f.url.value.replace(/'/g, '&#039;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  1804. html = '<a href="'+url+'">'+html+'</a>';
  1805. }
  1806. if ( caption )
  1807. html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]';
  1808. var win = window.dialogArguments || opener || parent || top;
  1809. win.send_to_editor(html);
  1810. return false;
  1811. },
  1812. resetImageData : function() {
  1813. var t = addExtImage;
  1814. t.width = t.height = '';
  1815. document.getElementById('go_button').style.color = '#bbb';
  1816. if ( ! document.forms[0].src.value )
  1817. document.getElementById('status_img').innerHTML = '';
  1818. else document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/no.png' ) ); ?>" alt="" />';
  1819. },
  1820. updateImageData : function() {
  1821. var t = addExtImage;
  1822. t.width = t.preloadImg.width;
  1823. t.height = t.preloadImg.height;
  1824. document.getElementById('go_button').style.color = '#333';
  1825. document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/yes.png' ) ); ?>" alt="" />';
  1826. },
  1827. getImageData : function() {
  1828. if ( jQuery('table.describe').hasClass('not-image') )
  1829. return;
  1830. var t = addExtImage, src = document.forms[0].src.value;
  1831. if ( ! src ) {
  1832. t.resetImageData();
  1833. return false;
  1834. }
  1835. document.getElementById('status_img').innerHTML = '<img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" alt="" width="16" height="16" />';
  1836. t.preloadImg = new Image();
  1837. t.preloadImg.onload = t.updateImageData;
  1838. t.preloadImg.onerror = t.resetImageData;
  1839. t.preloadImg.src = src;
  1840. }
  1841. };
  1842. jQuery(document).ready( function($) {
  1843. $('.media-types input').click( function() {
  1844. $('table.describe').toggleClass('not-image', $('#not-image').prop('checked') );
  1845. });
  1846. });
  1847. </script>
  1848. <div id="media-items">
  1849. <div class="media-item media-blank">
  1850. <?php
  1851. /**
  1852. * Filters the insert media from URL form HTML.
  1853. *
  1854. * @since 3.3.0
  1855. *
  1856. * @param string $form_html The insert from URL form HTML.
  1857. */
  1858. echo apply_filters( 'type_url_form_media', wp_media_insert_url_form( $type ) );
  1859. ?>
  1860. </div>
  1861. </div>
  1862. </form>
  1863. <?php
  1864. }
  1865. /**
  1866. * Adds gallery form to upload iframe
  1867. *
  1868. * @since 2.5.0
  1869. *
  1870. * @global string $redir_tab
  1871. * @global string $type
  1872. * @global string $tab
  1873. *
  1874. * @param array $errors
  1875. */
  1876. function media_upload_gallery_form($errors) {
  1877. global $redir_tab, $type;
  1878. $redir_tab = 'gallery';
  1879. media_upload_header();
  1880. $post_id = intval($_REQUEST['post_id']);
  1881. $form_action_url = admin_url("media-upload.php?type=$type&tab=gallery&post_id=$post_id");
  1882. /** This filter is documented in wp-admin/includes/media.php */
  1883. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  1884. $form_class = 'media-upload-form validate';
  1885. if ( get_user_setting('uploader') )
  1886. $form_class .= ' html-uploader';
  1887. ?>
  1888. <script type="text/javascript">
  1889. jQuery(function($){
  1890. var preloaded = $(".media-item.preloaded");
  1891. if ( preloaded.length > 0 ) {
  1892. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  1893. updateMediaForm();
  1894. }
  1895. });
  1896. </script>
  1897. <div id="sort-buttons" class="hide-if-no-js">
  1898. <span>
  1899. <?php _e('All Tabs:'); ?>
  1900. <a href="#" id="showall"><?php _e('Show'); ?></a>
  1901. <a href="#" id="hideall" style="display:none;"><?php _e('Hide'); ?></a>
  1902. </span>
  1903. <?php _e('Sort Order:'); ?>
  1904. <a href="#" id="asc"><?php _e('Ascending'); ?></a> |
  1905. <a href="#" id="desc"><?php _e('Descending'); ?></a> |
  1906. <a href="#" id="clear"><?php _ex('Clear', 'verb'); ?></a>
  1907. </div>
  1908. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="gallery-form">
  1909. <?php wp_nonce_field('media-form'); ?>
  1910. <?php //media_upload_form( $errors ); ?>
  1911. <table class="widefat">
  1912. <thead><tr>
  1913. <th><?php _e('Media'); ?></th>
  1914. <th class="order-head"><?php _e('Order'); ?></th>
  1915. <th class="actions-head"><?php _e('Actions'); ?></th>
  1916. </tr></thead>
  1917. </table>
  1918. <div id="media-items">
  1919. <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
  1920. <?php echo get_media_items($post_id, $errors); ?>
  1921. </div>
  1922. <p class="ml-submit">
  1923. <?php submit_button( __( 'Save all changes' ), 'savebutton', 'save', false, array( 'id' => 'save-all', 'style' => 'display: none;' ) ); ?>
  1924. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  1925. <input type="hidden" name="type" value="<?php echo esc_attr( $GLOBALS['type'] ); ?>" />
  1926. <input type="hidden" name="tab" value="<?php echo esc_attr( $GLOBALS['tab'] ); ?>" />
  1927. </p>
  1928. <div id="gallery-settings" style="display:none;">
  1929. <div class="title"><?php _e('Gallery Settings'); ?></div>
  1930. <table id="basic" class="describe"><tbody>
  1931. <tr>
  1932. <th scope="row" class="label">
  1933. <label>
  1934. <span class="alignleft"><?php _e('Link thumbnails to:'); ?></span>
  1935. </label>
  1936. </th>
  1937. <td class="field">
  1938. <input type="radio" name="linkto" id="linkto-file" value="file" />
  1939. <label for="linkto-file" class="radio"><?php _e('Image File'); ?></label>
  1940. <input type="radio" checked="checked" name="linkto" id="linkto-post" value="post" />
  1941. <label for="linkto-post" class="radio"><?php _e('Attachment Page'); ?></label>
  1942. </td>
  1943. </tr>
  1944. <tr>
  1945. <th scope="row" class="label">
  1946. <label>
  1947. <span class="alignleft"><?php _e('Order images by:'); ?></span>
  1948. </label>
  1949. </th>
  1950. <td class="field">
  1951. <select id="orderby" name="orderby">
  1952. <option value="menu_order" selected="selected"><?php _e('Menu order'); ?></option>
  1953. <option value="title"><?php _e('Title'); ?></option>
  1954. <option value="post_date"><?php _e('Date/Time'); ?></option>
  1955. <option value="rand"><?php _e('Random'); ?></option>
  1956. </select>
  1957. </td>
  1958. </tr>
  1959. <tr>
  1960. <th scope="row" class="label">
  1961. <label>
  1962. <span class="alignleft"><?php _e('Order:'); ?></span>
  1963. </label>
  1964. </th>
  1965. <td class="field">
  1966. <input type="radio" checked="checked" name="order" id="order-asc" value="asc" />
  1967. <label for="order-asc" class="radio"><?php _e('Ascending'); ?></label>
  1968. <input type="radio" name="order" id="order-desc" value="desc" />
  1969. <label for="order-desc" class="radio"><?php _e('Descending'); ?></label>
  1970. </td>
  1971. </tr>
  1972. <tr>
  1973. <th scope="row" class="label">
  1974. <label>
  1975. <span class="alignleft"><?php _e('Gallery columns:'); ?></span>
  1976. </label>
  1977. </th>
  1978. <td class="field">
  1979. <select id="columns" name="columns">
  1980. <option value="1">1</option>
  1981. <option value="2">2</option>
  1982. <option value="3" selected="selected">3</option>
  1983. <option value="4">4</option>
  1984. <option value="5">5</option>
  1985. <option value="6">6</option>
  1986. <option value="7">7</option>
  1987. <option value="8">8</option>
  1988. <option value="9">9</option>
  1989. </select>
  1990. </td>
  1991. </tr>
  1992. </tbody></table>
  1993. <p class="ml-submit">
  1994. <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="insert-gallery" id="insert-gallery" value="<?php esc_attr_e( 'Insert gallery' ); ?>" />
  1995. <input type="button" class="button" style="display:none;" onMouseDown="wpgallery.update();" name="update-gallery" id="update-gallery" value="<?php esc_attr_e( 'Update gallery settings' ); ?>" />
  1996. </p>
  1997. </div>
  1998. </form>
  1999. <?php
  2000. }
  2001. /**
  2002. * Outputs the legacy media upload form for the media library.
  2003. *
  2004. * @since 2.5.0
  2005. *
  2006. * @global wpdb $wpdb
  2007. * @global WP_Query $wp_query
  2008. * @global WP_Locale $wp_locale
  2009. * @global string $type
  2010. * @global string $tab
  2011. * @global array $post_mime_types
  2012. *
  2013. * @param array $errors
  2014. */
  2015. function media_upload_library_form($errors) {
  2016. global $wpdb, $wp_query, $wp_locale, $type, $tab, $post_mime_types;
  2017. media_upload_header();
  2018. $post_id = isset( $_REQUEST['post_id'] ) ? intval( $_REQUEST['post_id'] ) : 0;
  2019. $form_action_url = admin_url("media-upload.php?type=$type&tab=library&post_id=$post_id");
  2020. /** This filter is documented in wp-admin/includes/media.php */
  2021. $form_action_url = apply_filters( 'media_upload_form_url', $form_action_url, $type );
  2022. $form_class = 'media-upload-form validate';
  2023. if ( get_user_setting('uploader') )
  2024. $form_class .= ' html-uploader';
  2025. $q = $_GET;
  2026. $q['posts_per_page'] = 10;
  2027. $q['paged'] = isset( $q['paged'] ) ? intval( $q['paged'] ) : 0;
  2028. if ( $q['paged'] < 1 ) {
  2029. $q['paged'] = 1;
  2030. }
  2031. $q['offset'] = ( $q['paged'] - 1 ) * 10;
  2032. if ( $q['offset'] < 1 ) {
  2033. $q['offset'] = 0;
  2034. }
  2035. list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query( $q );
  2036. ?>
  2037. <form id="filter" method="get">
  2038. <input type="hidden" name="type" value="<?php echo esc_attr( $type ); ?>" />
  2039. <input type="hidden" name="tab" value="<?php echo esc_attr( $tab ); ?>" />
  2040. <input type="hidden" name="post_id" value="<?php echo (int) $post_id; ?>" />
  2041. <input type="hidden" name="post_mime_type" value="<?php echo isset( $_GET['post_mime_type'] ) ? esc_attr( $_GET['post_mime_type'] ) : ''; ?>" />
  2042. <input type="hidden" name="context" value="<?php echo isset( $_GET['context'] ) ? esc_attr( $_GET['context'] ) : ''; ?>" />
  2043. <p id="media-search" class="search-box">
  2044. <label class="screen-reader-text" for="media-search-input"><?php _e('Search Media');?>:</label>
  2045. <input type="search" id="media-search-input" name="s" value="<?php the_search_query(); ?>" />
  2046. <?php submit_button( __( 'Search Media' ), '', '', false ); ?>
  2047. </p>
  2048. <ul class="subsubsub">
  2049. <?php
  2050. $type_links = array();
  2051. $_num_posts = (array) wp_count_attachments();
  2052. $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
  2053. foreach ( $matches as $_type => $reals )
  2054. foreach ( $reals as $real )
  2055. if ( isset($num_posts[$_type]) )
  2056. $num_posts[$_type] += $_num_posts[$real];
  2057. else
  2058. $num_posts[$_type] = $_num_posts[$real];
  2059. // If available type specified by media button clicked, filter by that type
  2060. if ( empty($_GET['post_mime_type']) && !empty($num_posts[$type]) ) {
  2061. $_GET['post_mime_type'] = $type;
  2062. list($post_mime_types, $avail_post_mime_types) = wp_edit_attachments_query();
  2063. }
  2064. if ( empty($_GET['post_mime_type']) || $_GET['post_mime_type'] == 'all' )
  2065. $class = ' class="current"';
  2066. else
  2067. $class = '';
  2068. $type_links[] = '<li><a href="' . esc_url(add_query_arg(array('post_mime_type'=>'all', 'paged'=>false, 'm'=>false))) . '"' . $class . '>' . __('All Types') . '</a>';
  2069. foreach ( $post_mime_types as $mime_type => $label ) {
  2070. $class = '';
  2071. if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
  2072. continue;
  2073. if ( isset($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
  2074. $class = ' class="current"';
  2075. $type_links[] = '<li><a href="' . esc_url(add_query_arg(array('post_mime_type'=>$mime_type, 'paged'=>false))) . '"' . $class . '>' . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), '<span id="' . $mime_type . '-counter">' . number_format_i18n( $num_posts[$mime_type] ) . '</span>') . '</a>';
  2076. }
  2077. /**
  2078. * Filters the media upload mime type list items.
  2079. *
  2080. * Returned values should begin with an `<li>` tag.
  2081. *
  2082. * @since 3.1.0
  2083. *
  2084. * @param array $type_links An array of list items containing mime type link HTML.
  2085. */
  2086. echo implode(' | </li>', apply_filters( 'media_upload_mime_type_links', $type_links ) ) . '</li>';
  2087. unset($type_links);
  2088. ?>
  2089. </ul>
  2090. <div class="tablenav">
  2091. <?php
  2092. $page_links = paginate_links( array(
  2093. 'base' => add_query_arg( 'paged', '%#%' ),
  2094. 'format' => '',
  2095. 'prev_text' => __('&laquo;'),
  2096. 'next_text' => __('&raquo;'),
  2097. 'total' => ceil($wp_query->found_posts / 10),
  2098. 'current' => $q['paged'],
  2099. ));
  2100. if ( $page_links )
  2101. echo "<div class='tablenav-pages'>$page_links</div>";
  2102. ?>
  2103. <div class="alignleft actions">
  2104. <?php
  2105. $arc_query = "SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC";
  2106. $arc_result = $wpdb->get_results( $arc_query );
  2107. $month_count = count($arc_result);
  2108. $selected_month = isset( $_GET['m'] ) ? $_GET['m'] : 0;
  2109. if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) { ?>
  2110. <select name='m'>
  2111. <option<?php selected( $selected_month, 0 ); ?> value='0'><?php _e( 'All dates' ); ?></option>
  2112. <?php
  2113. foreach ($arc_result as $arc_row) {
  2114. if ( $arc_row->yyear == 0 )
  2115. continue;
  2116. $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
  2117. if ( $arc_row->yyear . $arc_row->mmonth == $selected_month )
  2118. $default = ' selected="selected"';
  2119. else
  2120. $default = '';
  2121. echo "<option$default value='" . esc_attr( $arc_row->yyear . $arc_row->mmonth ) . "'>";
  2122. echo esc_html( $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear" );
  2123. echo "</option>\n";
  2124. }
  2125. ?>
  2126. </select>
  2127. <?php } ?>
  2128. <?php submit_button( __( 'Filter &#187;' ), '', 'post-query-submit', false ); ?>
  2129. </div>
  2130. <br class="clear" />
  2131. </div>
  2132. </form>
  2133. <form enctype="multipart/form-data" method="post" action="<?php echo esc_url( $form_action_url ); ?>" class="<?php echo $form_class; ?>" id="library-form">
  2134. <?php wp_nonce_field('media-form'); ?>
  2135. <?php //media_upload_form( $errors ); ?>
  2136. <script type="text/javascript">
  2137. <!--
  2138. jQuery(function($){
  2139. var preloaded = $(".media-item.preloaded");
  2140. if ( preloaded.length > 0 ) {
  2141. preloaded.each(function(){prepareMediaItem({id:this.id.replace(/[^0-9]/g, '')},'');});
  2142. updateMediaForm();
  2143. }
  2144. });
  2145. -->
  2146. </script>
  2147. <div id="media-items">
  2148. <?php add_filter('attachment_fields_to_edit', 'media_post_single_attachment_fields_to_edit', 10, 2); ?>
  2149. <?php echo get_media_items(null, $errors); ?>
  2150. </div>
  2151. <p class="ml-submit">
  2152. <?php submit_button( __( 'Save all changes' ), 'savebutton', 'save', false ); ?>
  2153. <input type="hidden" name="post_id" id="post_id" value="<?php echo (int) $post_id; ?>" />
  2154. </p>
  2155. </form>
  2156. <?php
  2157. }
  2158. /**
  2159. * Creates the form for external url
  2160. *
  2161. * @since 2.7.0
  2162. *
  2163. * @param string $default_view
  2164. * @return string the form html
  2165. */
  2166. function wp_media_insert_url_form( $default_view = 'image' ) {
  2167. /** This filter is documented in wp-admin/includes/media.php */
  2168. if ( ! apply_filters( 'disable_captions', '' ) ) {
  2169. $caption = '
  2170. <tr class="image-only">
  2171. <th scope="row" class="label">
  2172. <label for="caption"><span class="alignleft">' . __('Image Caption') . '</span></label>
  2173. </th>
  2174. <td class="field"><textarea id="caption" name="caption"></textarea></td>
  2175. </tr>
  2176. ';
  2177. } else {
  2178. $caption = '';
  2179. }
  2180. $default_align = get_option('image_default_align');
  2181. if ( empty($default_align) )
  2182. $default_align = 'none';
  2183. if ( 'image' == $default_view ) {
  2184. $view = 'image-only';
  2185. $table_class = '';
  2186. } else {
  2187. $view = $table_class = 'not-image';
  2188. }
  2189. return '
  2190. <p class="media-types"><label><input type="radio" name="media_type" value="image" id="image-only"' . checked( 'image-only', $view, false ) . ' /> ' . __( 'Image' ) . '</label> &nbsp; &nbsp; <label><input type="radio" name="media_type" value="generic" id="not-image"' . checked( 'not-image', $view, false ) . ' /> ' . __( 'Audio, Video, or Other File' ) . '</label></p>
  2191. <p class="media-types media-types-required-info">' . sprintf( __( 'Required fields are marked %s' ), '<span class="required">*</span>' ) . '</p>
  2192. <table class="describe ' . $table_class . '"><tbody>
  2193. <tr>
  2194. <th scope="row" class="label" style="width:130px;">
  2195. <label for="src"><span class="alignleft">' . __( 'URL' ) . '</span> <span class="required">*</span></label>
  2196. <span class="alignright" id="status_img"></span>
  2197. </th>
  2198. <td class="field"><input id="src" name="src" value="" type="text" required aria-required="true" onblur="addExtImage.getImageData()" /></td>
  2199. </tr>
  2200. <tr>
  2201. <th scope="row" class="label">
  2202. <label for="title"><span class="alignleft">' . __( 'Title' ) . '</span> <span class="required">*</span></label>
  2203. </th>
  2204. <td class="field"><input id="title" name="title" value="" type="text" required aria-required="true" /></td>
  2205. </tr>
  2206. <tr class="not-image"><td></td><td><p class="help">' . __('Link text, e.g. &#8220;Ransom Demands (PDF)&#8221;') . '</p></td></tr>
  2207. <tr class="image-only">
  2208. <th scope="row" class="label">
  2209. <label for="alt"><span class="alignleft">' . __('Alternative Text') . '</span></label>
  2210. </th>
  2211. <td class="field"><input id="alt" name="alt" value="" type="text" aria-required="true" />
  2212. <p class="help">' . __('Alt text for the image, e.g. &#8220;The Mona Lisa&#8221;') . '</p></td>
  2213. </tr>
  2214. ' . $caption . '
  2215. <tr class="align image-only">
  2216. <th scope="row" class="label"><p><label for="align">' . __('Alignment') . '</label></p></th>
  2217. <td class="field">
  2218. <input name="align" id="align-none" value="none" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'none' ? ' checked="checked"' : '').' />
  2219. <label for="align-none" class="align image-align-none-label">' . __('None') . '</label>
  2220. <input name="align" id="align-left" value="left" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'left' ? ' checked="checked"' : '').' />
  2221. <label for="align-left" class="align image-align-left-label">' . __('Left') . '</label>
  2222. <input name="align" id="align-center" value="center" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'center' ? ' checked="checked"' : '').' />
  2223. <label for="align-center" class="align image-align-center-label">' . __('Center') . '</label>
  2224. <input name="align" id="align-right" value="right" onclick="addExtImage.align=\'align\'+this.value" type="radio"' . ($default_align == 'right' ? ' checked="checked"' : '').' />
  2225. <label for="align-right" class="align image-align-right-label">' . __('Right') . '</label>
  2226. </td>
  2227. </tr>
  2228. <tr class="image-only">
  2229. <th scope="row" class="label">
  2230. <label for="url"><span class="alignleft">' . __('Link Image To:') . '</span></label>
  2231. </th>
  2232. <td class="field"><input id="url" name="url" value="" type="text" /><br />
  2233. <button type="button" class="button" value="" onclick="document.forms[0].url.value=null">' . __('None') . '</button>
  2234. <button type="button" class="button" value="" onclick="document.forms[0].url.value=document.forms[0].src.value">' . __('Link to image') . '</button>
  2235. <p class="help">' . __('Enter a link URL or click above for presets.') . '</p></td>
  2236. </tr>
  2237. <tr class="image-only">
  2238. <td></td>
  2239. <td>
  2240. <input type="button" class="button" id="go_button" style="color:#bbb;" onclick="addExtImage.insert()" value="' . esc_attr__('Insert into Post') . '" />
  2241. </td>
  2242. </tr>
  2243. <tr class="not-image">
  2244. <td></td>
  2245. <td>
  2246. ' . get_submit_button( __( 'Insert into Post' ), '', 'insertonlybutton', false ) . '
  2247. </td>
  2248. </tr>
  2249. </tbody></table>
  2250. ';
  2251. }
  2252. /**
  2253. * Displays the multi-file uploader message.
  2254. *
  2255. * @since 2.6.0
  2256. *
  2257. * @global int $post_ID
  2258. */
  2259. function media_upload_flash_bypass() {
  2260. $browser_uploader = admin_url( 'media-new.php?browser-uploader' );
  2261. if ( $post = get_post() )
  2262. $browser_uploader .= '&amp;post_id=' . intval( $post->ID );
  2263. elseif ( ! empty( $GLOBALS['post_ID'] ) )
  2264. $browser_uploader .= '&amp;post_id=' . intval( $GLOBALS['post_ID'] );
  2265. ?>
  2266. <p class="upload-flash-bypass">
  2267. <?php printf( __( 'You are using the multi-file uploader. Problems? Try the <a href="%1$s" target="%2$s">browser uploader</a> instead.' ), $browser_uploader, '_blank' ); ?>
  2268. </p>
  2269. <?php
  2270. }
  2271. /**
  2272. * Displays the browser's built-in uploader message.
  2273. *
  2274. * @since 2.6.0
  2275. */
  2276. function media_upload_html_bypass() {
  2277. ?>
  2278. <p class="upload-html-bypass hide-if-no-js">
  2279. <?php _e('You are using the browser&#8217;s built-in file uploader. The WordPress uploader includes multiple file selection and drag and drop capability. <a href="#">Switch to the multi-file uploader</a>.'); ?>
  2280. </p>
  2281. <?php
  2282. }
  2283. /**
  2284. * Used to display a "After a file has been uploaded..." help message.
  2285. *
  2286. * @since 3.3.0
  2287. */
  2288. function media_upload_text_after() {}
  2289. /**
  2290. * Displays the checkbox to scale images.
  2291. *
  2292. * @since 3.3.0
  2293. */
  2294. function media_upload_max_image_resize() {
  2295. $checked = get_user_setting('upload_resize') ? ' checked="true"' : '';
  2296. $a = $end = '';
  2297. if ( current_user_can( 'manage_options' ) ) {
  2298. $a = '<a href="' . esc_url( admin_url( 'options-media.php' ) ) . '" target="_blank">';
  2299. $end = '</a>';
  2300. }
  2301. ?>
  2302. <p class="hide-if-no-js"><label>
  2303. <input name="image_resize" type="checkbox" id="image_resize" value="true"<?php echo $checked; ?> />
  2304. <?php
  2305. /* translators: %1$s is link start tag, %2$s is link end tag, %3$d is width, %4$d is height*/
  2306. printf( __( 'Scale images to match the large size selected in %1$simage options%2$s (%3$d &times; %4$d).' ), $a, $end, (int) get_option( 'large_size_w', '1024' ), (int) get_option( 'large_size_h', '1024' ) );
  2307. ?>
  2308. </label></p>
  2309. <?php
  2310. }
  2311. /**
  2312. * Displays the out of storage quota message in Multisite.
  2313. *
  2314. * @since 3.5.0
  2315. */
  2316. function multisite_over_quota_message() {
  2317. echo '<p>' . sprintf( __( 'Sorry, you have used all of your storage quota of %s MB.' ), get_space_allowed() ) . '</p>';
  2318. }
  2319. /**
  2320. * Displays the image and editor in the post editor
  2321. *
  2322. * @since 3.5.0
  2323. *
  2324. * @param WP_Post $post A post object.
  2325. */
  2326. function edit_form_image_editor( $post ) {
  2327. $open = isset( $_GET['image-editor'] );
  2328. if ( $open )
  2329. require_once ABSPATH . 'wp-admin/includes/image-edit.php';
  2330. $thumb_url = false;
  2331. if ( $attachment_id = intval( $post->ID ) )
  2332. $thumb_url = wp_get_attachment_image_src( $attachment_id, array( 900, 450 ), true );
  2333. $alt_text = get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
  2334. $att_url = wp_get_attachment_url( $post->ID ); ?>
  2335. <div class="wp_attachment_holder wp-clearfix">
  2336. <?php
  2337. if ( wp_attachment_is_image( $post->ID ) ) :
  2338. $image_edit_button = '';
  2339. if ( wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) {
  2340. $nonce = wp_create_nonce( "image_editor-$post->ID" );
  2341. $image_edit_button = "<input type='button' id='imgedit-open-btn-$post->ID' onclick='imageEdit.open( $post->ID, \"$nonce\" )' class='button' value='" . esc_attr__( 'Edit Image' ) . "' /> <span class='spinner'></span>";
  2342. }
  2343. ?>
  2344. <div class="imgedit-response" id="imgedit-response-<?php echo $attachment_id; ?>"></div>
  2345. <div<?php if ( $open ) echo ' style="display:none"'; ?> class="wp_attachment_image wp-clearfix" id="media-head-<?php echo $attachment_id; ?>">
  2346. <p id="thumbnail-head-<?php echo $attachment_id; ?>"><img class="thumbnail" src="<?php echo set_url_scheme( $thumb_url[0] ); ?>" style="max-width:100%" alt="" /></p>
  2347. <p><?php echo $image_edit_button; ?></p>
  2348. </div>
  2349. <div<?php if ( ! $open ) echo ' style="display:none"'; ?> class="image-editor" id="image-editor-<?php echo $attachment_id; ?>">
  2350. <?php if ( $open ) wp_image_editor( $attachment_id ); ?>
  2351. </div>
  2352. <?php
  2353. elseif ( $attachment_id && wp_attachment_is( 'audio', $post ) ):
  2354. wp_maybe_generate_attachment_metadata( $post );
  2355. echo wp_audio_shortcode( array( 'src' => $att_url ) );
  2356. elseif ( $attachment_id && wp_attachment_is( 'video', $post ) ):
  2357. wp_maybe_generate_attachment_metadata( $post );
  2358. $meta = wp_get_attachment_metadata( $attachment_id );
  2359. $w = ! empty( $meta['width'] ) ? min( $meta['width'], 640 ) : 0;
  2360. $h = ! empty( $meta['height'] ) ? $meta['height'] : 0;
  2361. if ( $h && $w < $meta['width'] ) {
  2362. $h = round( ( $meta['height'] * $w ) / $meta['width'] );
  2363. }
  2364. $attr = array( 'src' => $att_url );
  2365. if ( ! empty( $w ) && ! empty( $h ) ) {
  2366. $attr['width'] = $w;
  2367. $attr['height'] = $h;
  2368. }
  2369. $thumb_id = get_post_thumbnail_id( $attachment_id );
  2370. if ( ! empty( $thumb_id ) ) {
  2371. $attr['poster'] = wp_get_attachment_url( $thumb_id );
  2372. }
  2373. echo wp_video_shortcode( $attr );
  2374. elseif ( isset( $thumb_url[0] ) ):
  2375. ?>
  2376. <div class="wp_attachment_image wp-clearfix" id="media-head-<?php echo $attachment_id; ?>">
  2377. <p id="thumbnail-head-<?php echo $attachment_id; ?>">
  2378. <img class="thumbnail" src="<?php echo set_url_scheme( $thumb_url[0] ); ?>" style="max-width:100%" alt="" />
  2379. </p>
  2380. </div>
  2381. <?php
  2382. else:
  2383. /**
  2384. * Fires when an attachment type can't be rendered in the edit form.
  2385. *
  2386. * @since 4.6.0
  2387. *
  2388. * @param WP_Post $post A post object.
  2389. */
  2390. do_action( 'wp_edit_form_attachment_display', $post );
  2391. endif; ?>
  2392. </div>
  2393. <div class="wp_attachment_details edit-form-section">
  2394. <p>
  2395. <label for="attachment_caption"><strong><?php _e( 'Caption' ); ?></strong></label><br />
  2396. <textarea class="widefat" name="excerpt" id="attachment_caption"><?php echo $post->post_excerpt; ?></textarea>
  2397. </p>
  2398. <?php if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) : ?>
  2399. <p>
  2400. <label for="attachment_alt"><strong><?php _e( 'Alternative Text' ); ?></strong></label><br />
  2401. <input type="text" class="widefat" name="_wp_attachment_image_alt" id="attachment_alt" value="<?php echo esc_attr( $alt_text ); ?>" />
  2402. </p>
  2403. <?php endif; ?>
  2404. <?php
  2405. $quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
  2406. $editor_args = array(
  2407. 'textarea_name' => 'content',
  2408. 'textarea_rows' => 5,
  2409. 'media_buttons' => false,
  2410. 'tinymce' => false,
  2411. 'quicktags' => $quicktags_settings,
  2412. );
  2413. ?>
  2414. <label for="attachment_content"><strong><?php _e( 'Description' ); ?></strong><?php
  2415. if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
  2416. echo ': ' . __( 'Displayed on attachment pages.' );
  2417. } ?></label>
  2418. <?php wp_editor( $post->post_content, 'attachment_content', $editor_args ); ?>
  2419. </div>
  2420. <?php
  2421. $extras = get_compat_media_markup( $post->ID );
  2422. echo $extras['item'];
  2423. echo '<input type="hidden" id="image-edit-context" value="edit-attachment" />' . "\n";
  2424. }
  2425. /**
  2426. * Displays non-editable attachment metadata in the publish meta box.
  2427. *
  2428. * @since 3.5.0
  2429. */
  2430. function attachment_submitbox_metadata() {
  2431. $post = get_post();
  2432. $file = get_attached_file( $post->ID );
  2433. $filename = esc_html( wp_basename( $file ) );
  2434. $media_dims = '';
  2435. $meta = wp_get_attachment_metadata( $post->ID );
  2436. if ( isset( $meta['width'], $meta['height'] ) )
  2437. $media_dims .= "<span id='media-dims-$post->ID'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
  2438. /** This filter is documented in wp-admin/includes/media.php */
  2439. $media_dims = apply_filters( 'media_meta', $media_dims, $post );
  2440. $att_url = wp_get_attachment_url( $post->ID );
  2441. ?>
  2442. <div class="misc-pub-section misc-pub-attachment">
  2443. <label for="attachment_url"><?php _e( 'File URL:' ); ?></label>
  2444. <input type="text" class="widefat urlfield" readonly="readonly" name="attachment_url" id="attachment_url" value="<?php echo esc_attr( $att_url ); ?>" />
  2445. </div>
  2446. <div class="misc-pub-section misc-pub-filename">
  2447. <?php _e( 'File name:' ); ?> <strong><?php echo $filename; ?></strong>
  2448. </div>
  2449. <div class="misc-pub-section misc-pub-filetype">
  2450. <?php _e( 'File type:' ); ?> <strong><?php
  2451. if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) {
  2452. echo esc_html( strtoupper( $matches[1] ) );
  2453. list( $mime_type ) = explode( '/', $post->post_mime_type );
  2454. if ( $mime_type !== 'image' && ! empty( $meta['mime_type'] ) ) {
  2455. if ( $meta['mime_type'] !== "$mime_type/" . strtolower( $matches[1] ) ) {
  2456. echo ' (' . $meta['mime_type'] . ')';
  2457. }
  2458. }
  2459. } else {
  2460. echo strtoupper( str_replace( 'image/', '', $post->post_mime_type ) );
  2461. }
  2462. ?></strong>
  2463. </div>
  2464. <?php
  2465. $file_size = false;
  2466. if ( isset( $meta['filesize'] ) )
  2467. $file_size = $meta['filesize'];
  2468. elseif ( file_exists( $file ) )
  2469. $file_size = filesize( $file );
  2470. if ( ! empty( $file_size ) ) : ?>
  2471. <div class="misc-pub-section misc-pub-filesize">
  2472. <?php _e( 'File size:' ); ?> <strong><?php echo size_format( $file_size ); ?></strong>
  2473. </div>
  2474. <?php
  2475. endif;
  2476. if ( preg_match( '#^(audio|video)/#', $post->post_mime_type ) ) {
  2477. $fields = array(
  2478. 'length_formatted' => __( 'Length:' ),
  2479. 'bitrate' => __( 'Bitrate:' ),
  2480. );
  2481. /**
  2482. * Filters the audio and video metadata fields to be shown in the publish meta box.
  2483. *
  2484. * The key for each item in the array should correspond to an attachment
  2485. * metadata key, and the value should be the desired label.
  2486. *
  2487. * @since 3.7.0
  2488. * @since 4.9.0 Added the `$post` parameter.
  2489. *
  2490. * @param array $fields An array of the attachment metadata keys and labels.
  2491. * @param WP_Post $post WP_Post object for the current attachment.
  2492. */
  2493. $fields = apply_filters( 'media_submitbox_misc_sections', $fields, $post );
  2494. foreach ( $fields as $key => $label ) {
  2495. if ( empty( $meta[ $key ] ) ) {
  2496. continue;
  2497. }
  2498. ?>
  2499. <div class="misc-pub-section misc-pub-mime-meta misc-pub-<?php echo sanitize_html_class( $key ); ?>">
  2500. <?php echo $label ?> <strong><?php
  2501. switch ( $key ) {
  2502. case 'bitrate' :
  2503. echo round( $meta['bitrate'] / 1000 ) . 'kb/s';
  2504. if ( ! empty( $meta['bitrate_mode'] ) ) {
  2505. echo ' ' . strtoupper( esc_html( $meta['bitrate_mode'] ) );
  2506. }
  2507. break;
  2508. default:
  2509. echo esc_html( $meta[ $key ] );
  2510. break;
  2511. }
  2512. ?></strong>
  2513. </div>
  2514. <?php
  2515. }
  2516. $fields = array(
  2517. 'dataformat' => __( 'Audio Format:' ),
  2518. 'codec' => __( 'Audio Codec:' )
  2519. );
  2520. /**
  2521. * Filters the audio attachment metadata fields to be shown in the publish meta box.
  2522. *
  2523. * The key for each item in the array should correspond to an attachment
  2524. * metadata key, and the value should be the desired label.
  2525. *
  2526. * @since 3.7.0
  2527. * @since 4.9.0 Added the `$post` parameter.
  2528. *
  2529. * @param array $fields An array of the attachment metadata keys and labels.
  2530. * @param WP_Post $post WP_Post object for the current attachment.
  2531. */
  2532. $audio_fields = apply_filters( 'audio_submitbox_misc_sections', $fields, $post );
  2533. foreach ( $audio_fields as $key => $label ) {
  2534. if ( empty( $meta['audio'][ $key ] ) ) {
  2535. continue;
  2536. }
  2537. ?>
  2538. <div class="misc-pub-section misc-pub-audio misc-pub-<?php echo sanitize_html_class( $key ); ?>">
  2539. <?php echo $label; ?> <strong><?php echo esc_html( $meta['audio'][$key] ); ?></strong>
  2540. </div>
  2541. <?php
  2542. }
  2543. }
  2544. if ( $media_dims ) : ?>
  2545. <div class="misc-pub-section misc-pub-dimensions">
  2546. <?php _e( 'Dimensions:' ); ?> <strong><?php echo $media_dims; ?></strong>
  2547. </div>
  2548. <?php
  2549. endif;
  2550. }
  2551. /**
  2552. * Parse ID3v2, ID3v1, and getID3 comments to extract usable data
  2553. *
  2554. * @since 3.6.0
  2555. *
  2556. * @param array $metadata An existing array with data
  2557. * @param array $data Data supplied by ID3 tags
  2558. */
  2559. function wp_add_id3_tag_data( &$metadata, $data ) {
  2560. foreach ( array( 'id3v2', 'id3v1' ) as $version ) {
  2561. if ( ! empty( $data[$version]['comments'] ) ) {
  2562. foreach ( $data[$version]['comments'] as $key => $list ) {
  2563. if ( 'length' !== $key && ! empty( $list ) ) {
  2564. $metadata[$key] = wp_kses_post( reset( $list ) );
  2565. // Fix bug in byte stream analysis.
  2566. if ( 'terms_of_use' === $key && 0 === strpos( $metadata[$key], 'yright notice.' ) )
  2567. $metadata[$key] = 'Cop' . $metadata[$key];
  2568. }
  2569. }
  2570. break;
  2571. }
  2572. }
  2573. if ( ! empty( $data['id3v2']['APIC'] ) ) {
  2574. $image = reset( $data['id3v2']['APIC']);
  2575. if ( ! empty( $image['data'] ) ) {
  2576. $metadata['image'] = array(
  2577. 'data' => $image['data'],
  2578. 'mime' => $image['image_mime'],
  2579. 'width' => $image['image_width'],
  2580. 'height' => $image['image_height']
  2581. );
  2582. }
  2583. } elseif ( ! empty( $data['comments']['picture'] ) ) {
  2584. $image = reset( $data['comments']['picture'] );
  2585. if ( ! empty( $image['data'] ) ) {
  2586. $metadata['image'] = array(
  2587. 'data' => $image['data'],
  2588. 'mime' => $image['image_mime']
  2589. );
  2590. }
  2591. }
  2592. }
  2593. /**
  2594. * Retrieve metadata from a video file's ID3 tags
  2595. *
  2596. * @since 3.6.0
  2597. *
  2598. * @param string $file Path to file.
  2599. * @return array|bool Returns array of metadata, if found.
  2600. */
  2601. function wp_read_video_metadata( $file ) {
  2602. if ( ! file_exists( $file ) ) {
  2603. return false;
  2604. }
  2605. $metadata = array();
  2606. if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
  2607. define( 'GETID3_TEMP_DIR', get_temp_dir() );
  2608. }
  2609. if ( ! class_exists( 'getID3', false ) ) {
  2610. require( ABSPATH . WPINC . '/ID3/getid3.php' );
  2611. }
  2612. $id3 = new getID3();
  2613. $data = $id3->analyze( $file );
  2614. if ( isset( $data['video']['lossless'] ) )
  2615. $metadata['lossless'] = $data['video']['lossless'];
  2616. if ( ! empty( $data['video']['bitrate'] ) )
  2617. $metadata['bitrate'] = (int) $data['video']['bitrate'];
  2618. if ( ! empty( $data['video']['bitrate_mode'] ) )
  2619. $metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
  2620. if ( ! empty( $data['filesize'] ) )
  2621. $metadata['filesize'] = (int) $data['filesize'];
  2622. if ( ! empty( $data['mime_type'] ) )
  2623. $metadata['mime_type'] = $data['mime_type'];
  2624. if ( ! empty( $data['playtime_seconds'] ) )
  2625. $metadata['length'] = (int) round( $data['playtime_seconds'] );
  2626. if ( ! empty( $data['playtime_string'] ) )
  2627. $metadata['length_formatted'] = $data['playtime_string'];
  2628. if ( ! empty( $data['video']['resolution_x'] ) )
  2629. $metadata['width'] = (int) $data['video']['resolution_x'];
  2630. if ( ! empty( $data['video']['resolution_y'] ) )
  2631. $metadata['height'] = (int) $data['video']['resolution_y'];
  2632. if ( ! empty( $data['fileformat'] ) )
  2633. $metadata['fileformat'] = $data['fileformat'];
  2634. if ( ! empty( $data['video']['dataformat'] ) )
  2635. $metadata['dataformat'] = $data['video']['dataformat'];
  2636. if ( ! empty( $data['video']['encoder'] ) )
  2637. $metadata['encoder'] = $data['video']['encoder'];
  2638. if ( ! empty( $data['video']['codec'] ) )
  2639. $metadata['codec'] = $data['video']['codec'];
  2640. if ( ! empty( $data['audio'] ) ) {
  2641. unset( $data['audio']['streams'] );
  2642. $metadata['audio'] = $data['audio'];
  2643. }
  2644. if ( empty( $metadata['created_timestamp'] ) ) {
  2645. $created_timestamp = wp_get_media_creation_timestamp( $data );
  2646. if ( $created_timestamp !== false ) {
  2647. $metadata['created_timestamp'] = $created_timestamp;
  2648. }
  2649. }
  2650. wp_add_id3_tag_data( $metadata, $data );
  2651. $file_format = isset( $metadata['fileformat'] ) ? $metadata['fileformat'] : null;
  2652. /**
  2653. * Filters the array of metadata retrieved from a video.
  2654. *
  2655. * In core, usually this selection is what is stored.
  2656. * More complete data can be parsed from the `$data` parameter.
  2657. *
  2658. * @since 4.9.0
  2659. *
  2660. * @param array $metadata Filtered Video metadata.
  2661. * @param string $file Path to video file.
  2662. * @param string $file_format File format of video, as analyzed by getID3.
  2663. * @param string $data Raw metadata from getID3.
  2664. */
  2665. return apply_filters( 'wp_read_video_metadata', $metadata, $file, $file_format, $data );
  2666. }
  2667. /**
  2668. * Retrieve metadata from a audio file's ID3 tags
  2669. *
  2670. * @since 3.6.0
  2671. *
  2672. * @param string $file Path to file.
  2673. * @return array|bool Returns array of metadata, if found.
  2674. */
  2675. function wp_read_audio_metadata( $file ) {
  2676. if ( ! file_exists( $file ) ) {
  2677. return false;
  2678. }
  2679. $metadata = array();
  2680. if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
  2681. define( 'GETID3_TEMP_DIR', get_temp_dir() );
  2682. }
  2683. if ( ! class_exists( 'getID3', false ) ) {
  2684. require( ABSPATH . WPINC . '/ID3/getid3.php' );
  2685. }
  2686. $id3 = new getID3();
  2687. $data = $id3->analyze( $file );
  2688. if ( ! empty( $data['audio'] ) ) {
  2689. unset( $data['audio']['streams'] );
  2690. $metadata = $data['audio'];
  2691. }
  2692. if ( ! empty( $data['fileformat'] ) )
  2693. $metadata['fileformat'] = $data['fileformat'];
  2694. if ( ! empty( $data['filesize'] ) )
  2695. $metadata['filesize'] = (int) $data['filesize'];
  2696. if ( ! empty( $data['mime_type'] ) )
  2697. $metadata['mime_type'] = $data['mime_type'];
  2698. if ( ! empty( $data['playtime_seconds'] ) )
  2699. $metadata['length'] = (int) round( $data['playtime_seconds'] );
  2700. if ( ! empty( $data['playtime_string'] ) )
  2701. $metadata['length_formatted'] = $data['playtime_string'];
  2702. wp_add_id3_tag_data( $metadata, $data );
  2703. return $metadata;
  2704. }
  2705. /**
  2706. * Parse creation date from media metadata.
  2707. *
  2708. * The getID3 library doesn't have a standard method for getting creation dates,
  2709. * so the location of this data can vary based on the MIME type.
  2710. *
  2711. * @since 4.9.0
  2712. *
  2713. * @link https://github.com/JamesHeinrich/getID3/blob/master/structure.txt
  2714. *
  2715. * @param array $metadata The metadata returned by getID3::analyze().
  2716. * @return int|bool A UNIX timestamp for the media's creation date if available
  2717. * or a boolean FALSE if a timestamp could not be determined.
  2718. */
  2719. function wp_get_media_creation_timestamp( $metadata ) {
  2720. $creation_date = false;
  2721. if ( empty( $metadata['fileformat'] ) ) {
  2722. return $creation_date;
  2723. }
  2724. switch ( $metadata['fileformat'] ) {
  2725. case 'asf':
  2726. if ( isset( $metadata['asf']['file_properties_object']['creation_date_unix'] ) ) {
  2727. $creation_date = (int) $metadata['asf']['file_properties_object']['creation_date_unix'];
  2728. }
  2729. break;
  2730. case 'matroska':
  2731. case 'webm':
  2732. if ( isset( $metadata['matroska']['comments']['creation_time']['0'] ) ) {
  2733. $creation_date = strtotime( $metadata['matroska']['comments']['creation_time']['0'] );
  2734. }
  2735. elseif ( isset( $metadata['matroska']['info']['0']['DateUTC_unix'] ) ) {
  2736. $creation_date = (int) $metadata['matroska']['info']['0']['DateUTC_unix'];
  2737. }
  2738. break;
  2739. case 'quicktime':
  2740. case 'mp4':
  2741. if ( isset( $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'] ) ) {
  2742. $creation_date = (int) $metadata['quicktime']['moov']['subatoms']['0']['creation_time_unix'];
  2743. }
  2744. break;
  2745. }
  2746. return $creation_date;
  2747. }
  2748. /**
  2749. * Encapsulate logic for Attach/Detach actions
  2750. *
  2751. * @since 4.2.0
  2752. *
  2753. * @global wpdb $wpdb WordPress database abstraction object.
  2754. *
  2755. * @param int $parent_id Attachment parent ID.
  2756. * @param string $action Optional. Attach/detach action. Accepts 'attach' or 'detach'.
  2757. * Default 'attach'.
  2758. */
  2759. function wp_media_attach_action( $parent_id, $action = 'attach' ) {
  2760. global $wpdb;
  2761. if ( ! $parent_id ) {
  2762. return;
  2763. }
  2764. if ( ! current_user_can( 'edit_post', $parent_id ) ) {
  2765. wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
  2766. }
  2767. $ids = array();
  2768. foreach ( (array) $_REQUEST['media'] as $att_id ) {
  2769. $att_id = (int) $att_id;
  2770. if ( ! current_user_can( 'edit_post', $att_id ) ) {
  2771. continue;
  2772. }
  2773. $ids[] = $att_id;
  2774. }
  2775. if ( ! empty( $ids ) ) {
  2776. $ids_string = implode( ',', $ids );
  2777. if ( 'attach' === $action ) {
  2778. $result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $ids_string )", $parent_id ) );
  2779. } else {
  2780. $result = $wpdb->query( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $ids_string )" );
  2781. }
  2782. foreach ( $ids as $att_id ) {
  2783. clean_attachment_cache( $att_id );
  2784. }
  2785. }
  2786. if ( isset( $result ) ) {
  2787. $location = 'upload.php';
  2788. if ( $referer = wp_get_referer() ) {
  2789. if ( false !== strpos( $referer, 'upload.php' ) ) {
  2790. $location = remove_query_arg( array( 'attached', 'detach' ), $referer );
  2791. }
  2792. }
  2793. $key = 'attach' === $action ? 'attached' : 'detach';
  2794. $location = add_query_arg( array( $key => $result ), $location );
  2795. wp_redirect( $location );
  2796. exit;
  2797. }
  2798. }