media.php 115 KB

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