nggPostThumbnail

NggPostThumbnail - Class for adding the post thumbnail feature.

Defined (1)

The class is defined in the following location(s).

/lib/post-thumbnail.php  
  1. class nggPostThumbnail { 
  2.  
  3. /** 
  4. * Main constructor - Add filter and action hooks 
  5. *  
  6. */  
  7. function __construct() { 
  8.  
  9. add_filter( 'admin_post_thumbnail_html', array( &$this, 'admin_post_thumbnail') ); 
  10. add_action( 'wp_ajax_ngg_set_post_thumbnail', array( &$this, 'ajax_set_post_thumbnail') ); 
  11. // Adding filter for the new post_thumbnail 
  12. add_filter( 'post_thumbnail_html', array( &$this, 'ngg_post_thumbnail'), 10, 5 ); 
  13. return;  
  14.  
  15. /** 
  16. * Filter for the post meta box. look for a NGG image if the ID is "ngg-<imageID>" 
  17. *  
  18. * @param string $content 
  19. * @return string html output 
  20. */ 
  21. function admin_post_thumbnail( $content ) { 
  22. global $post; 
  23.  
  24. if ( !is_object($post) ) 
  25. return $content; 
  26.  
  27. $thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true ); 
  28.  
  29. // in the case it's a ngg image it return ngg-<imageID> 
  30. if ( strpos($thumbnail_id, 'ngg-') === false) 
  31. return $content; 
  32.  
  33. // cut off the 'ngg-' 
  34. $thumbnail_id = substr( $thumbnail_id, 4); 
  35.  
  36. return $this->_wp_post_thumbnail_html( $thumbnail_id );  
  37.  
  38. /** 
  39. * Filter for the post content 
  40. *  
  41. * @param string $html 
  42. * @param int $post_id 
  43. * @param int $post_thumbnail_id 
  44. * @param string|array $size Optional. Image size. Defaults to 'thumbnail'. 
  45. * @param string|array $attr Optional. Query string or array of attributes. 
  46. * @return string html output 
  47. */ 
  48. function ngg_post_thumbnail( $html, $post_id, $post_thumbnail_id, $size = 'post-thumbnail', $attr = '' ) { 
  49.  
  50. global $post, $_wp_additional_image_sizes; 
  51.  
  52. // in the case it's a ngg image it return ngg-<imageID> 
  53. if ( strpos($post_thumbnail_id, 'ngg-') === false) 
  54. return $html; 
  55.  
  56. // cut off the 'ngg-' 
  57. $post_thumbnail_id = substr( $post_thumbnail_id, 4); 
  58.  
  59. // get the options 
  60. $ngg_options = nggGallery::get_option('ngg_options'); 
  61.  
  62. // get the image data 
  63. $image = nggdb::find_image($post_thumbnail_id); 
  64.  
  65. if (!$image)  
  66. return $html; 
  67.  
  68. $img_src = false;  
  69. $class = 'wp-post-image ngg-image-' . $image->pid . ' '; 
  70.  
  71. if (is_array($size) || is_array($_wp_additional_image_sizes) && isset($_wp_additional_image_sizes[$size])) {  
  72. $class .= isset($attr['class']) ? esc_attr($attr['class']) : ''; 
  73.  
  74. if( is_array($size)) { 
  75. //the parameters is given as an array rather than a predfined image 
  76. $width = absint( $size[0] ); 
  77. $height = absint( $size[1] ); 
  78. if(isset($size[2]) && $size[2] === true) { 
  79. $mode = 'crop'; 
  80. } else if(isset($size[2])) { 
  81. $mode = $size[2]; 
  82. } else { 
  83. $mode = '';  
  84. } else { 
  85. $width = absint( $_wp_additional_image_sizes[$size]['width'] ); 
  86. $height = absint( $_wp_additional_image_sizes[$size]['height'] ); 
  87. $mode = ($_wp_additional_image_sizes[$size]['crop']) ? 'crop' : ''; 
  88.  
  89. // check fo cached picture 
  90. if ( $post->post_status == 'publish' ) 
  91. $img_src = $image->cached_singlepic_file( $width, $height, $mode );  
  92.  
  93. // if we didn't use a cached image then we take the on-the-fly mode  
  94. if ($img_src == false)  
  95. $img_src = trailingslashit( home_url() ) . 'index.php?callback=image&pid=' . $image->pid . '&width=' . $width . '&height=' . $height . '&mode=crop'; 
  96.  
  97. } else { 
  98. $img_src = $image->thumbURL; 
  99.  
  100. $alttext = isset($attr['alt']) ? $attr['alt'] : $image->alttext; 
  101. $titletext = isset($attr['title']) ? $attr['title'] : $image->title; 
  102.  
  103. $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alttext) . '" title="' . esc_attr($titletext) .'" class="'.$class.'" />'; 
  104.  
  105. return $html; 
  106.  
  107. /** 
  108. * nggPostThumbnail::ajax_set_post_thumbnail() 
  109. *  
  110. * @return void 
  111. */ 
  112. function ajax_set_post_thumbnail() { 
  113.  
  114. global $post_ID; 
  115.  
  116. // check for correct capability 
  117. if ( !is_user_logged_in() ) 
  118. die( '-1' ); 
  119.  
  120. // get the post id as global variable, otherwise the ajax_nonce failed later 
  121. $post_ID = intval( $_POST['post_id'] ); 
  122.  
  123. if ( !current_user_can( 'edit_post', $post_ID ) ) 
  124. die( '-1' ); 
  125.  
  126. $thumbnail_id = intval( $_POST['thumbnail_id'] ); 
  127.  
  128. // delete the image 
  129. if ( $thumbnail_id == '-1' ) { 
  130. delete_post_meta( $post_ID, '_thumbnail_id' ); 
  131. die( $this->_wp_post_thumbnail_html() ); 
  132.  
  133. // for NGG we look for the image id 
  134. if ( $thumbnail_id && nggdb::find_image($thumbnail_id) ) { 
  135. // to know that we have a NGG image we add "ngg-" before the id 
  136. update_post_meta( $post_ID, '_thumbnail_id', 'ngg-' . $thumbnail_id ); 
  137. die( $this->_wp_post_thumbnail_html( $thumbnail_id ) ); 
  138. die( '0' ); 
  139.  
  140. /** 
  141. * Output HTML for the post thumbnail meta-box. 
  142. * @see wp-admin\includes\post.php 
  143. * @param int $thumbnail_id ID of the image used for thumbnail 
  144. * @return string html output 
  145. */ 
  146. function _wp_post_thumbnail_html( $thumbnail_id = NULL ) { 
  147.  
  148. global $_wp_additional_image_sizes, $post_ID; 
  149.  
  150. $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="' . esc_url( get_upload_iframe_src('image') ) . '" id="set-post-thumbnail" class="thickbox">%s</a></p>'; 
  151. $content = sprintf($set_thumbnail_link, esc_html__( 'Set featured image' )); 
  152.  
  153. $image = nggdb::find_image($thumbnail_id); 
  154. $img_src = false; 
  155.  
  156. // get the options 
  157. $ngg_options = nggGallery::get_option('ngg_options'); 
  158.  
  159. if ( $image ) { 
  160. if ( is_array($_wp_additional_image_sizes) && isset($_wp_additional_image_sizes['post-thumbnail']) ) { 
  161. // Use post thumbnail settings if defined 
  162. $width = absint( $_wp_additional_image_sizes['post-thumbnail']['width'] ); 
  163. $height = absint( $_wp_additional_image_sizes['post-thumbnail']['height'] ); 
  164. $mode = $_wp_additional_image_sizes['post-thumbnail']['crop'] ? 'crop' : ''; 
  165. // check fo cached picture 
  166. $img_src = $image->cached_singlepic_file( $width, $height, $mode );  
  167.  
  168. // if we didn't use a cached image then we take the on-the-fly mode  
  169. if ( $img_src == false )  
  170. $img_src = trailingslashit( home_url() ) . 'index.php?callback=image&pid=' . $image->pid . '&width=' . $width . '&height=' . $height . '&mode=crop'; 
  171.  
  172. $thumbnail_html = '<img width="266" src="'. $img_src . '" alt="'.$image->alttext.'" title="'.$image->alttext.'" />'; 
  173.  
  174. if ( !empty( $thumbnail_html ) ) { 
  175. $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$post_ID" ); 
  176. $content = sprintf($set_thumbnail_link, $thumbnail_html); 
  177. $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>'; 
  178.  
  179. return $content; 
  180. }  
  181.