Нет описания

media.php 114KB

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