nggPostThumbnail

NggPostThumbnail - Class for adding the post thumbnail feature.

Defined (1)

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

/products/photocrati_nextgen/modules/ngglegacy/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, 2 ); 
  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, $post_id = null )  
  22. if ($post_id == null) 
  23. global $post; 
  24.  
  25. if ( !is_object($post) ) 
  26. return $content; 
  27.  
  28. $post_id = $post->ID; 
  29.  
  30. $thumbnail_id = get_post_meta($post_id, '_thumbnail_id', true); 
  31.  
  32. // in the case it's a ngg image it return ngg-<imageID> 
  33. if ( strpos($thumbnail_id, 'ngg-') === false)  
  34. global $wp_version; 
  35.  
  36. if (version_compare($wp_version, '3.5', '>=') && $thumbnail_id <= 0) 
  37. $iframe_src = get_upload_iframe_src('image', $post_id); 
  38. $iframe_src = remove_query_arg('TB_iframe', $iframe_src); 
  39. $iframe_src = add_query_arg('tab', 'nextgen', $iframe_src); 
  40. $iframe_src = add_query_arg('chromeless', '1', $iframe_src); 
  41. $iframe_src = add_query_arg('TB_iframe', '1', $iframe_src); 
  42.  
  43. $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set NextGEN featured image', 'nggallery' ) . '" href="' . nextgen_esc_url( $iframe_src ) . '" id="set-ngg-post-thumbnail" class="thickbox">%s</a></p>'; 
  44. $content .= sprintf($set_thumbnail_link, esc_html__( 'Set NextGEN featured image', 'nggallery' )); 
  45.  
  46. return $content; 
  47.  
  48. // cut off the 'ngg-' 
  49. $thumbnail_id = substr( $thumbnail_id, 4); 
  50.  
  51. return $this->_wp_post_thumbnail_html( $thumbnail_id );  
  52.  
  53. /** 
  54. * Filter for the post content 
  55. *  
  56. * @param string $html 
  57. * @param int $post_id 
  58. * @param int $post_thumbnail_id 
  59. * @param string|array $size Optional. Image size. Defaults to 'thumbnail'. 
  60. * @param string|array $attr Optional. Query string or array of attributes. 
  61. * @return string html output 
  62. */ 
  63. function ngg_post_thumbnail( $html, $post_id, $post_thumbnail_id, $size = 'post-thumbnail', $attr = '' ) { 
  64.  
  65. global $post, $_wp_additional_image_sizes; 
  66.  
  67. // in the case it's a ngg image it return ngg-<imageID> 
  68. if ( strpos($post_thumbnail_id, 'ngg-') === false) 
  69. return $html; 
  70.  
  71. // cut off the 'ngg-' 
  72. $post_thumbnail_id = substr( $post_thumbnail_id, 4); 
  73.  
  74. // get the options 
  75. $ngg_options = nggGallery::get_option('ngg_options'); 
  76.  
  77. // get the image data 
  78. $image = nggdb::find_image($post_thumbnail_id); 
  79.  
  80. if (!$image)  
  81. return $html; 
  82.  
  83. $img_src = false;  
  84. $class = 'wp-post-image ngg-image-' . $image->pid . ' '; 
  85.  
  86. if (is_array($size) || is_array($_wp_additional_image_sizes) && isset($_wp_additional_image_sizes[$size])) {  
  87. $class .= isset($attr['class']) ? esc_attr($attr['class']) : ''; 
  88.  
  89. if( is_array($size)) { 
  90. //the parameters is given as an array rather than a predfined image 
  91. $width = absint( $size[0] ); 
  92. $height = absint( $size[1] ); 
  93. if(isset($size[2]) && $size[2] === true) { 
  94. $mode = 'crop'; 
  95. } else if(isset($size[2])) { 
  96. $mode = $size[2]; 
  97. } else { 
  98. $mode = '';  
  99. } else { 
  100. $width = absint( $_wp_additional_image_sizes[$size]['width'] ); 
  101. $height = absint( $_wp_additional_image_sizes[$size]['height'] ); 
  102. $mode = ($_wp_additional_image_sizes[$size]['crop']) ? 'crop' : ''; 
  103.  
  104. // check fo cached picture 
  105. if ( $post->post_status == 'publish' ) 
  106. $img_src = $image->cached_singlepic_file( $width, $height, $mode );  
  107.  
  108. // if we didn't use a cached image then we take the on-the-fly mode  
  109. if ($img_src == false)  
  110. $img_src = trailingslashit( home_url() ) . 'index.php?callback=image&pid=' . $image->pid . '&width=' . $width . '&height=' . $height . '&mode=crop'; 
  111.  
  112. } else { 
  113. $img_src = $image->thumbURL; 
  114.  
  115. $alttext = isset($attr['alt']) ? $attr['alt'] : $image->alttext; 
  116. $titletext = isset($attr['title']) ? $attr['title'] : $image->title; 
  117.  
  118. $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alttext) . '" title="' . esc_attr($titletext) .'" class="'.$class.'" />'; 
  119.  
  120. return $html; 
  121.  
  122. /** 
  123. * nggPostThumbnail::ajax_set_post_thumbnail() 
  124. *  
  125. * @return void 
  126. */ 
  127. function ajax_set_post_thumbnail()  
  128. // This function does the following: 
  129. // 1) Check if the user is logged in and has permission to edit the post 
  130. // 2) Get the thumbnail id from the POST request. The thumbnail id is actually the NGG image id 
  131. // 3)] 
  132.  
  133. global $post_ID; 
  134.  
  135. // check for correct capability 
  136. if ( !is_user_logged_in() ) 
  137. die( '-1' ); 
  138.  
  139. // get the post id as global variable, otherwise the ajax_nonce failed later 
  140. $post_ID = intval( $_POST['post_id'] ); 
  141.  
  142. if ( !current_user_can( 'edit_post', $post_ID ) ) 
  143. die( '-1' ); 
  144.  
  145. $thumbnail_id = intval( $_POST['thumbnail_id'] ); 
  146.  
  147. // delete the image 
  148. if ( $thumbnail_id == '-1' ) { 
  149. delete_post_meta( $post_ID, '_thumbnail_id' ); 
  150. die('0'); 
  151.  
  152. die(strval(C_Gallery_Storage::get_instance()->set_post_thumbnail($post_ID, $thumbnail_id))); 
  153.  
  154. /** 
  155. * Output HTML for the post thumbnail meta-box. 
  156. * @see wp-admin\includes\post.php 
  157. * @param int $thumbnail_id ID of the image used for thumbnail 
  158. * @return string html output 
  159. */ 
  160. function _wp_post_thumbnail_html( $thumbnail_id = NULL ) { 
  161.  
  162. global $_wp_additional_image_sizes, $post_ID; 
  163.  
  164. $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image', 'nggallery' ) . '" href="' . nextgen_esc_url( get_upload_iframe_src('image') ) . '" id="set-post-thumbnail" class="thickbox">%s</a></p>'; 
  165. $content = sprintf($set_thumbnail_link, esc_html__( 'Set featured image', 'nggallery' )); 
  166.  
  167. $image = nggdb::find_image($thumbnail_id); 
  168. $img_src = false; 
  169.  
  170. // get the options 
  171. $ngg_options = nggGallery::get_option('ngg_options'); 
  172.  
  173. if ( $image ) { 
  174. if ( is_array($_wp_additional_image_sizes) && isset($_wp_additional_image_sizes['post-thumbnail']) ) { 
  175. // Use post thumbnail settings if defined 
  176. $width = absint( $_wp_additional_image_sizes['post-thumbnail']['width'] ); 
  177. $height = absint( $_wp_additional_image_sizes['post-thumbnail']['height'] ); 
  178. $mode = $_wp_additional_image_sizes['post-thumbnail']['crop'] ? 'crop' : ''; 
  179. // check fo cached picture 
  180. $img_src = $image->cached_singlepic_file( $width, $height, $mode );  
  181.  
  182. // if we didn't use a cached image then we take the on-the-fly mode  
  183. if ( $img_src == false )  
  184. $img_src = trailingslashit( home_url() ) . 'index.php?callback=image&pid=' . $image->pid . '&width=' . $width . '&height=' . $height . '&mode=crop'; 
  185.  
  186. $thumbnail_html = '<img width="266" src="'. $img_src . '" alt="'.$image->alttext.'" title="'.$image->alttext.'" />'; 
  187.  
  188. if ( !empty( $thumbnail_html ) ) { 
  189. $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$post_ID" ); 
  190. $content = sprintf($set_thumbnail_link, $thumbnail_html); 
  191. $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>'; 
  192.  
  193. return $content; 
  194. }  
  195.