multi-thumbs.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /*
  3. Thank YOU!
  4. Multiple Post Thumbnails
  5. http://wordpress.org/extend/plugins/multiple-post-thumbnails/
  6. Author: Chris Scott
  7. */
  8. /* Copyright 2010 Chris Scott (cscott@voceconnect.com)
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License, version 2, as
  11. published by the Free Software Foundation.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. if (!class_exists('MultiPostThumbnails')) {
  21. class MultiPostThumbnails {
  22. public function __construct($args = array()) {
  23. $this->register($args);
  24. }
  25. /**
  26. * Register a new post thumbnail.
  27. *
  28. * Required $args contents:
  29. *
  30. * label - The name of the post thumbnail to display in the admin metabox
  31. *
  32. * id - Used to build the CSS class for the admin meta box. Needs to be unique and valid in a CSS class selector.
  33. *
  34. * Optional $args contents:
  35. *
  36. * post_type - The post type to register this thumbnail for. Defaults to post.
  37. *
  38. * priority - The admin metabox priority. Defaults to low to show after normal post thumbnail meta box.
  39. *
  40. * @param array|string $args See above description.
  41. * @return void
  42. */
  43. public function register($args = array()) {
  44. $defaults = array(
  45. 'label' => null,
  46. 'id' => null,
  47. 'post_type' => 'post',
  48. 'priority' => 'low',
  49. );
  50. $args = wp_parse_args($args, $defaults);
  51. // Create and set properties
  52. foreach($args as $k => $v) {
  53. $this->$k = $v;
  54. }
  55. // Need these args to be set at a minimum
  56. if (null === $this->label || null === $this->id) {
  57. if (WP_DEBUG) {
  58. trigger_error(sprintf("The 'label' and 'id' values of the 'args' parameter of '%s::%s()' are required", __CLASS__, __FUNCTION__));
  59. }
  60. return;
  61. }
  62. // add theme support if not already added
  63. if (!current_theme_supports('post-thumbnails')) {
  64. add_theme_support( 'post-thumbnails' );
  65. }
  66. add_action('add_meta_boxes', array($this, 'add_metabox'));
  67. add_filter('attachment_fields_to_edit', array($this, 'add_attachment_field'), 20, 2);
  68. add_action('admin_init', array($this, 'enqueue_admin_scripts'));
  69. add_action("wp_ajax_set-{$this->post_type}-{$this->id}-thumbnail", array($this, 'set_thumbnail'));
  70. add_action('delete_attachment', array($this, 'action_delete_attachment'));
  71. }
  72. /**
  73. * Add admin metabox for thumbnail chooser
  74. *
  75. * @return void
  76. */
  77. public function add_metabox() {
  78. add_meta_box("{$this->post_type}-{$this->id}", __($this->label), array($this, 'thumbnail_meta_box'), $this->post_type, 'side', $this->priority);
  79. }
  80. /**
  81. * Output the thumbnail meta box
  82. *
  83. * @return string HTML output
  84. */
  85. public function thumbnail_meta_box() {
  86. global $post;
  87. $thumbnail_id = get_post_meta($post->ID, "{$this->post_type}_{$this->id}_thumbnail_id", true);
  88. echo $this->post_thumbnail_html($thumbnail_id);
  89. }
  90. /**
  91. * Throw this in the media attachment fields
  92. *
  93. * @param string $form_fields
  94. * @param string $post
  95. * @return void
  96. */
  97. public function add_attachment_field($form_fields, $post) {
  98. $calling_post_id = 0;
  99. if (isset($_GET['post_id']))
  100. $calling_post_id = absint($_GET['post_id']);
  101. elseif (isset($_POST) && count($_POST)) // Like for async-upload where $_GET['post_id'] isn't set
  102. $calling_post_id = $post->post_parent;
  103. // check the post type to see if link needs to be added
  104. $calling_post = get_post($calling_post_id);
  105. if (is_null($calling_post) || $calling_post->post_type != $this->post_type) {
  106. return $form_fields;
  107. }
  108. $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$calling_post_id}");
  109. $link = sprintf('<a id="%4$s-%1$s-thumbnail-%2$s" class="%1$s-thumbnail" href="#" onclick="MultiPostThumbnailsSetAsThumbnail(\'%2$s\', \'%1$s\', \'%4$s\', \'%5$s\');return false;">Set as %3$s</a>', $this->id, $post->ID, $this->label, $this->post_type, $ajax_nonce);
  110. $form_fields["{$this->post_type}-{$this->id}-thumbnail"] = array(
  111. 'label' => $this->label,
  112. 'input' => 'html',
  113. 'html' => $link);
  114. return $form_fields;
  115. }
  116. /**
  117. * Enqueue admin JavaScripts
  118. *
  119. * @return void
  120. */
  121. public function enqueue_admin_scripts() {
  122. //wp_enqueue_script("featured-image-custom", $this->plugins_url('js/multi-post-thumbnails-admin.js', __FILE__), array('jquery'));
  123. wp_enqueue_script( 'multi-thumbs', get_template_directory_uri() . '/js/multi-thumbs.js', 'jquery', '20120206', true );
  124. }
  125. /**
  126. * Deletes the post meta data for posts when an attachment used as a
  127. * multiple post thumbnail is deleted from the Media Libray
  128. *
  129. * @global object $wpdb
  130. * @param int $post_id
  131. */
  132. public function action_delete_attachment($post_id) {
  133. global $wpdb;
  134. $meta_key = "{$this->post_type}_{$this->id}_thumbnail_id";
  135. $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = '%s' AND meta_value = %d", $meta_key, $post_id ));
  136. }
  137. private function plugins_url($relative_path, $plugin_path) {
  138. $template_dir = get_template_directory();
  139. foreach ( array('template_dir', 'plugin_path') as $var ) {
  140. $$var = str_replace('\\' ,'/', $$var); // sanitize for Win32 installs
  141. $$var = preg_replace('|/+|', '/', $$var);
  142. }
  143. if(0 === strpos($plugin_path, $template_dir)) {
  144. $url = get_template_directory_uri();
  145. $folder = str_replace($template_dir, '', dirname($plugin_path));
  146. if ( '.' != $folder ) {
  147. $url .= '/' . ltrim($folder, '/');
  148. }
  149. if ( !empty($relative_path) && is_string($relative_path) && strpos($relative_path, '..') === false ) {
  150. $url .= '/' . ltrim($relative_path, '/');
  151. }
  152. return $url;
  153. } else {
  154. return plugins_url($relative_path, $plugin_path);
  155. }
  156. }
  157. /**
  158. * Check if post has an image attached.
  159. *
  160. * @param string $post_type The post type.
  161. * @param string $id The id used to register the thumbnail.
  162. * @param string $post_id Optional. Post ID.
  163. * @return bool Whether post has an image attached.
  164. */
  165. public static function has_post_thumbnail($post_type, $id, $post_id = null) {
  166. if (null === $post_id) {
  167. $post_id = get_the_ID();
  168. }
  169. if (!$post_id) {
  170. return false;
  171. }
  172. return get_post_meta($post_id, "{$post_type}_{$id}_thumbnail_id", true);
  173. }
  174. /**
  175. * Display Post Thumbnail.
  176. *
  177. * @param string $post_type The post type.
  178. * @param string $thumb_id The id used to register the thumbnail.
  179. * @param string $post_id Optional. Post ID.
  180. * @param int $size Optional. Image size. Defaults to 'post-thumbnail', which theme sets using set_post_thumbnail_size( $width, $height, $crop_flag );.
  181. * @param string|array $attr Optional. Query string or array of attributes.
  182. * @param bool $link_to_original Optional. Wrap link to original image around thumbnail?
  183. */
  184. public static function the_post_thumbnail($post_type, $thumb_id, $post_id = null, $size = 'cover', $attr = '', $link_to_original = false) {
  185. echo self::get_the_post_thumbnail($post_type, $thumb_id, $post_id, $size, $attr, $link_to_original);
  186. }
  187. public static function the_post_thumbnail_thumb($post_type, $thumb_id, $post_id = null, $size = 'small', $attr = '', $link_to_original = false) {
  188. echo self::get_the_post_thumbnail($post_type, $thumb_id, $post_id, $size, $attr, $link_to_original);
  189. }
  190. /**
  191. * Retrieve Post Thumbnail.
  192. *
  193. * @param string $post_type The post type.
  194. * @param string $thumb_id The id used to register the thumbnail.
  195. * @param int $post_id Optional. Post ID.
  196. * @param string $size Optional. Image size. Defaults to 'thumbnail'.
  197. * @param bool $link_to_original Optional. Wrap link to original image around thumbnail?
  198. * @param string|array $attr Optional. Query string or array of attributes.
  199. */
  200. public static function get_the_post_thumbnail($post_type, $thumb_id, $post_id = NULL, $size = 'cover', $attr = '' , $link_to_original = false) {
  201. global $id;
  202. $post_id = (NULL === $post_id) ? get_the_ID() : $post_id;
  203. $post_thumbnail_id = self::get_post_thumbnail_id($post_type, $thumb_id, $post_id);
  204. $size = apply_filters("{$post_type}_{$post_id}_thumbnail_size", $size);
  205. if ($post_thumbnail_id) {
  206. do_action("begin_fetch_multi_{$post_type}_thumbnail_html", $post_id, $post_thumbnail_id, $size); // for "Just In Time" filtering of all of wp_get_attachment_image()'s filters
  207. $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
  208. do_action("end_fetch_multi_{$post_type}_thumbnail_html", $post_id, $post_thumbnail_id, $size);
  209. } else {
  210. $html = '';
  211. }
  212. if ($link_to_original) {
  213. $html = sprintf('<a href="%s">%s</a>', wp_get_attachment_url($post_thumbnail_id), $html);
  214. }
  215. return apply_filters("{$post_type}_{$thumb_id}_thumbnail_html", $html, $post_id, $post_thumbnail_id, $size, $attr);
  216. }
  217. /**
  218. * Retrieve Post Thumbnail ID.
  219. *
  220. * @param string $post_type The post type.
  221. * @param string $id The id used to register the thumbnail.
  222. * @param int $post_id Post ID.
  223. * @return int
  224. */
  225. public static function get_post_thumbnail_id($post_type, $id, $post_id) {
  226. return get_post_meta($post_id, "{$post_type}_{$id}_thumbnail_id", true);
  227. }
  228. /**
  229. *
  230. * @param string $post_type The post type.
  231. * @param string $id The id used to register the thumbnail.
  232. * @param int $post_id Optional. The post ID. If not set, will attempt to get it.
  233. * @return mixed Thumbnail url or false if the post doesn't have a thumbnail for the given post type, and id.
  234. */
  235. public static function get_post_thumbnail_url($post_type, $id, $post_id = 0) {
  236. if (!$post_id) {
  237. $post_id = get_the_ID();
  238. }
  239. $post_thumbnail_id = self::get_post_thumbnail_id($post_type, $id, $post_id);
  240. return wp_get_attachment_url($post_thumbnail_id);
  241. }
  242. /**
  243. * Output the post thumbnail HTML for the metabox and AJAX callbacks
  244. *
  245. * @param string $thumbnail_id The thumbnail's post ID.
  246. * @return string HTML
  247. */
  248. private function post_thumbnail_html($thumbnail_id = NULL) {
  249. global $content_width, $_wp_additional_image_sizes, $post_ID;
  250. $set_thumbnail_link = sprintf('<p class="hide-if-no-js"><a title="%1$s" href="%2$s" id="set-%3$s-%4$s-thumbnail" class="thickbox">%%s</a></p>', esc_attr__( "Set {$this->label}" ), get_upload_iframe_src('image'), $this->post_type, $this->id);
  251. $content = sprintf($set_thumbnail_link, esc_html__( "Set {$this->label}" ));
  252. if ($thumbnail_id && get_post($thumbnail_id)) {
  253. $old_content_width = $content_width;
  254. $content_width = 266;
  255. if ( !isset($_wp_additional_image_sizes["{$this->post_type}-{$this->id}-thumbnail"]))
  256. $thumbnail_html = wp_get_attachment_image($thumbnail_id, array($content_width, $content_width));
  257. else
  258. $thumbnail_html = wp_get_attachment_image($thumbnail_id, "{$this->post_type}-{$this->id}-thumbnail");
  259. if (!empty($thumbnail_html)) {
  260. $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
  261. $content = sprintf($set_thumbnail_link, $thumbnail_html);
  262. $content .= sprintf('<p class="hide-if-no-js"><a href="#" id="remove-%1$s-%2$s-thumbnail" onclick="MultiPostThumbnailsRemoveThumbnail(\'%2$s\', \'%1$s\', \'%4$s\');return false;">%3$s</a></p>', $this->post_type, $this->id, esc_html__( "Remove {$this->label}" ), $ajax_nonce);
  263. }
  264. $content_width = $old_content_width;
  265. }
  266. return $content;
  267. }
  268. /**
  269. * Set/remove the post thumbnail. AJAX handler.
  270. *
  271. * @return string Updated post thumbnail HTML.
  272. */
  273. public function set_thumbnail() {
  274. global $post_ID; // have to do this so get_upload_iframe_src() can grab it
  275. $post_ID = intval($_POST['post_id']);
  276. if ( !current_user_can('edit_post', $post_ID))
  277. die('-1');
  278. $thumbnail_id = intval($_POST['thumbnail_id']);
  279. check_ajax_referer("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
  280. if ($thumbnail_id == '-1') {
  281. delete_post_meta($post_ID, "{$this->post_type}_{$this->id}_thumbnail_id");
  282. die($this->post_thumbnail_html(NULL));
  283. }
  284. if ($thumbnail_id && get_post($thumbnail_id)) {
  285. $thumbnail_html = wp_get_attachment_image($thumbnail_id, 'thumbnail');
  286. if (!empty($thumbnail_html)) {
  287. update_post_meta($post_ID, "{$this->post_type}_{$this->id}_thumbnail_id", $thumbnail_id);
  288. die($this->post_thumbnail_html($thumbnail_id));
  289. }
  290. }
  291. die('0');
  292. }
  293. }
  294. }