PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/simple-history/loggers/SimpleMediaLogger.php

https://bitbucket.org/GraphicFusion/crafti-landing
PHP | 375 lines | 221 code | 98 blank | 56 comment | 37 complexity | 9fafc501e5210b6a4e93e9255ac0e6a6 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, Apache-2.0, GPL-3.0
  1. <?php
  2. defined( 'ABSPATH' ) or die();
  3. /**
  4. * Logs media uploads
  5. */
  6. class SimpleMediaLogger extends SimpleLogger
  7. {
  8. public $slug = "SimpleMediaLogger";
  9. /**
  10. * Get array with information about this logger
  11. *
  12. * @return array
  13. */
  14. function getInfo() {
  15. $arr_info = array(
  16. "name" => "Media/Attachments Logger",
  17. "description" => "Logs media uploads and edits",
  18. "capability" => "edit_pages",
  19. "messages" => array(
  20. 'attachment_created' => __('Created {post_type} "{attachment_title}"', 'simple-history'),
  21. 'attachment_updated' => __('Edited {post_type} "{attachment_title}"', 'simple-history'),
  22. 'attachment_deleted' => __('Deleted {post_type} "{attachment_title}" ("{attachment_filename}")', 'simple-history')
  23. ),
  24. "labels" => array(
  25. "search" => array(
  26. "label" => _x("Media", "Media logger: search", "simple-history"),
  27. "label_all" => _x("All media activity", "Media logger: search", "simple-history"),
  28. "options" => array(
  29. _x("Added media", "Media logger: search", "simple-history") => array(
  30. "attachment_created"
  31. ),
  32. _x("Updated media", "Media logger: search", "simple-history") => array(
  33. "attachment_updated"
  34. ),
  35. _x("Deleted media", "Media logger: search", "simple-history") => array(
  36. "attachment_deleted"
  37. ),
  38. )
  39. ) // end search array
  40. ) // end labels
  41. );
  42. return $arr_info;
  43. }
  44. public function loaded() {
  45. add_action("admin_init", array($this, "on_admin_init"));
  46. add_action("xmlrpc_call_success_mw_newMediaObject", array($this, "on_mw_newMediaObject"), 10, 2);
  47. add_filter("simple_history/rss_item_link", array($this, "filter_rss_item_link"), 10, 2);
  48. }
  49. function on_admin_init() {
  50. add_action("add_attachment", array($this, "on_add_attachment"));
  51. add_action("edit_attachment", array($this, "on_edit_attachment"));
  52. add_action("delete_attachment", array($this, "on_delete_attachment"));
  53. }
  54. /**
  55. * Filter that fires after a new attachment has been added via the XML-RPC MovableType API.
  56. *
  57. * @since 2.0.21
  58. *
  59. * @param int $id ID of the new attachment.
  60. * @param array $args An array of arguments to add the attachment.
  61. */
  62. function on_mw_newMediaObject($attachment_id, $args) {
  63. $attachment_post = get_post( $attachment_id );
  64. $filename = esc_html( wp_basename( $attachment_post->guid ) );
  65. $mime = get_post_mime_type( $attachment_post );
  66. $file = get_attached_file( $attachment_id );
  67. $file_size = false;
  68. if ( file_exists( $file ) ) {
  69. $file_size = filesize( $file );
  70. }
  71. $this->infoMessage(
  72. 'attachment_created',
  73. array(
  74. "post_type" => get_post_type( $attachment_post ),
  75. "attachment_id" => $attachment_id,
  76. "attachment_title" => get_the_title( $attachment_post ),
  77. "attachment_filename" => $filename,
  78. "attachment_mime" => $mime,
  79. "attachment_filesize" => $file_size
  80. )
  81. );
  82. }
  83. /**
  84. * Modify plain output to inlcude link to post
  85. */
  86. public function getLogRowPlainTextOutput($row) {
  87. $message = $row->message;
  88. $context = $row->context;
  89. $message_key = $context["_message_key"];
  90. $attachment_id = $context["attachment_id"];
  91. $attachment_post = get_post( $attachment_id );
  92. $attachment_is_available = is_a($attachment_post, "WP_Post");
  93. // Only link to attachment if it is still available
  94. if ( $attachment_is_available ) {
  95. if ( "attachment_updated" == $message_key ) {
  96. $message = __('Edited {post_type} <a href="{edit_link}">"{attachment_title}"</a>', "simple-history");
  97. } else if ( "attachment_created" == $message_key ) {
  98. $message = __('Uploaded {post_type} <a href="{edit_link}">"{attachment_title}"</a>', "simple-history");
  99. }
  100. $context["post_type"] = esc_html( $context["post_type"] );
  101. $context["attachment_filename"] = esc_html( $context["attachment_filename"] );
  102. $context["edit_link"] = get_edit_post_link( $attachment_id );
  103. $message = $this->interpolate($message, $context, $row);
  104. } else {
  105. // Attachment post is not available, attachment has probably been deleted
  106. $message = parent::getLogRowPlainTextOutput( $row );
  107. }
  108. return $message;
  109. }
  110. /**
  111. * Get output for detailed log section
  112. */
  113. function getLogRowDetailsOutput($row) {
  114. $context = $row->context;
  115. $message_key = $context["_message_key"];
  116. $output = "";
  117. $attachment_id = $context["attachment_id"];
  118. $attachment_post = get_post( $attachment_id );
  119. $attachment_is_available = is_a($attachment_post, "WP_Post");
  120. if ( "attachment_updated" == $message_key ) {
  121. // Attachment is changed = don't show thumbs and all
  122. } else if ( "attachment_deleted" == $message_key ) {
  123. // Attachment is deleted = don't show thumbs and all
  124. } else if ( "attachment_created" == $message_key ) {
  125. // Attachment is created/uploaded = show details with image thumbnail
  126. $attachment_id = $context["attachment_id"];
  127. $filetype = wp_check_filetype( $context["attachment_filename"] );
  128. $file_url = wp_get_attachment_url( $attachment_id );
  129. $edit_link = get_edit_post_link( $attachment_id );
  130. $attached_file = get_attached_file( $attachment_id );
  131. $message = "";
  132. $full_src = false;
  133. $is_image = wp_attachment_is_image( $attachment_id );
  134. $is_video = strpos($filetype["type"], "video/") !== false;
  135. $is_audio = strpos($filetype["type"], "audio/") !== false;
  136. $full_image_width = null;
  137. $full_image_height = null;
  138. if ( $is_image ) {
  139. $thumb_src = wp_get_attachment_image_src($attachment_id, array(350,500));
  140. $full_src = wp_get_attachment_image_src($attachment_id, "full");
  141. $full_image_width = $full_src[1];
  142. $full_image_height = $full_src[2];
  143. // is_image is also true for mime types that WP can't create thumbs for
  144. // so we need to check that wp got an resized version
  145. if ( $full_image_width && $full_image_height ) {
  146. $context["full_image_width"] = $full_image_width;
  147. $context["full_image_height"] = $full_image_height;
  148. // Only output thumb if file exists
  149. // For example images deleted on file system but not in WP cause broken images (rare case, but has happened to me.)
  150. if ( file_exists( $attached_file ) && $thumb_src ) {
  151. $context["attachment_thumb"] = sprintf('<div class="SimpleHistoryLogitemThumbnail"><img src="%1$s" alt=""></div>', $thumb_src[0] );
  152. }
  153. }
  154. } else if ( $is_audio ) {
  155. $content = sprintf('[audio src="%1$s"]', $file_url);
  156. $context["attachment_thumb"] = do_shortcode( $content );
  157. } else if ( $is_video ) {
  158. $content = sprintf('[video src="%1$s"]', $file_url);
  159. $context["attachment_thumb"] = do_shortcode( $content );
  160. } else {
  161. // use wordpress icon for other media types
  162. if ( $attachment_is_available ) {
  163. $context["attachment_thumb"] = wp_get_attachment_image( $attachment_id, null, true );
  164. }
  165. /*else {
  166. // Add icon for deleted media?
  167. $context["attachment_thumb"] = "thumb";
  168. }*/
  169. }
  170. $context["attachment_size_format"] = size_format( $row->context["attachment_filesize"] );
  171. $context["attachment_filetype_extension"] = strtoupper( $filetype["ext"] );
  172. if ( ! empty( $context["attachment_thumb"] ) ) {
  173. if ( $is_image ) {
  174. $message .= "<a class='SimpleHistoryLogitemThumbnailLink' href='".$edit_link."'>";
  175. }
  176. $message .= __('{attachment_thumb}', 'simple-history');
  177. if ( $is_image ) {
  178. $message .= "</a>";
  179. }
  180. }
  181. $message .= "<p class='SimpleHistoryLogitem--logger-SimpleMediaLogger--attachment-meta'>";
  182. $message .= "<span class='SimpleHistoryLogitem__inlineDivided'>" . __('{attachment_size_format}', "simple-history") . "</span> ";
  183. $message .= "<span class='SimpleHistoryLogitem__inlineDivided'>" . __('{attachment_filetype_extension}', "simple-history") . "</span>";
  184. if ( $full_image_width && $full_image_height ) {
  185. $message .= " <span class='SimpleHistoryLogitem__inlineDivided'>" . __('{full_image_width} × {full_image_height}', "simple-history") . "</span>";
  186. }
  187. //$message .= " <span class='SimpleHistoryLogitem__inlineDivided'>" . sprintf( __('<a href="%1$s">Edit attachment</a>'), $edit_link ) . "</span>";
  188. $message .= "</p>";
  189. $output .= $this->interpolate( $message, $context, $row );
  190. }
  191. return $output;
  192. }
  193. /**
  194. * Called when an attachment is added
  195. */
  196. function on_add_attachment($attachment_id) {
  197. $attachment_post = get_post( $attachment_id );
  198. $filename = esc_html( wp_basename( $attachment_post->guid ) );
  199. $mime = get_post_mime_type( $attachment_post );
  200. $file = get_attached_file( $attachment_id );
  201. $file_size = false;
  202. if ( file_exists( $file ) ) {
  203. $file_size = filesize( $file );
  204. }
  205. $this->infoMessage(
  206. 'attachment_created',
  207. array(
  208. "post_type" => get_post_type( $attachment_post ),
  209. "attachment_id" => $attachment_id,
  210. "attachment_title" => get_the_title( $attachment_post ),
  211. "attachment_filename" => $filename,
  212. "attachment_mime" => $mime,
  213. "attachment_filesize" => $file_size
  214. )
  215. );
  216. }
  217. /**
  218. * An attachmet is changed
  219. * is this only being called if the title of the attachment is changed?!
  220. *
  221. * @param int $attachment_id
  222. */
  223. function on_edit_attachment($attachment_id) {
  224. $attachment_post = get_post( $attachment_id );
  225. $filename = esc_html( wp_basename( $attachment_post->guid ) );
  226. $mime = get_post_mime_type( $attachment_post );
  227. $file = get_attached_file( $attachment_id );
  228. $this->infoMessage(
  229. "attachment_updated",
  230. array(
  231. "post_type" => get_post_type( $attachment_post ),
  232. "attachment_id" => $attachment_id,
  233. "attachment_title" => get_the_title( $attachment_post ),
  234. "attachment_filename" => $filename,
  235. "attachment_mime" => $mime
  236. )
  237. );
  238. }
  239. /**
  240. * Called when an attachment is deleted
  241. */
  242. function on_delete_attachment($attachment_id) {
  243. $attachment_post = get_post( $attachment_id );
  244. $filename = esc_html( wp_basename( $attachment_post->guid ) );
  245. $mime = get_post_mime_type( $attachment_post );
  246. $file = get_attached_file( $attachment_id );
  247. $this->infoMessage(
  248. "attachment_deleted",
  249. array(
  250. "post_type" => get_post_type( $attachment_post ),
  251. "attachment_id" => $attachment_id,
  252. "attachment_title" => get_the_title( $attachment_post ),
  253. "attachment_filename" => $filename,
  254. "attachment_mime" => $mime
  255. )
  256. );
  257. }
  258. /**
  259. * Modify RSS links so they go directly to the correct media in wp admin
  260. *
  261. * @since 2.0.23
  262. * @param string $link
  263. * @param array $row
  264. */
  265. public function filter_rss_item_link($link, $row) {
  266. if ( $row->logger != $this->slug ) {
  267. return $link;
  268. }
  269. if ( isset( $row->context["attachment_id"] ) ) {
  270. $permalink = add_query_arg(array("action" => "edit", "post" => $row->context["attachment_id"]), admin_url( "post.php" ) );
  271. if ( $permalink ) {
  272. $link = $permalink;
  273. }
  274. }
  275. return $link;
  276. }
  277. }