media.php 111 KB

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