Нема описа

media.php 115KB

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