nggTags

Tag PHP class for the WordPress plugin NextGEN Gallery nggallery.lib.php.

Defined (1)

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

/products/photocrati_nextgen/modules/ngglegacy/lib/tags.php  
  1. class nggTags { 
  2.  
  3. /** 
  4. * Copy tags 
  5. */ 
  6. static function copy_tags($src_pid, $dest_pid) { 
  7. $tags = wp_get_object_terms( $src_pid, 'ngg_tag', 'fields=ids' ); 
  8. $tags = array_map('intval', $tags); 
  9. wp_set_object_terms( $dest_pid, $tags, 'ngg_tag', true ); 
  10.  
  11. return implode(', ', $tags); 
  12.  
  13. /** 
  14. * Rename tags 
  15. */ 
  16. static function rename_tags($old = '', $new = '') { 
  17.  
  18. $return_value = array( 
  19. 'status' => 'ok',  
  20. 'message' => '' 
  21. ); 
  22.  
  23. if ( trim( str_replace(', ', '', stripslashes($new)) ) == '' ) { 
  24. $return_value['message'] = __('No new tag specified!', 'nggallery'); 
  25. $return_value['status'] = 'error'; 
  26. return $return_value; 
  27.  
  28. // String to array 
  29. $old_tags = explode(', ', $old); 
  30. $new_tags = explode(', ', $new); 
  31.  
  32. // Remove empty element and trim 
  33. $old_tags = array_filter($old_tags, 'nggtags_delete_empty_element'); 
  34. $new_tags = array_filter($new_tags, 'nggtags_delete_empty_element'); 
  35.  
  36. // If old/new tag are empty => exit ! 
  37. if ( empty($old_tags) || empty($new_tags) ) { 
  38. $return_value['message'] = __('No new/old valid tag specified!', 'nggallery'); 
  39. $return_value['status'] = 'error'; 
  40. return $return_value; 
  41.  
  42. $counter = 0; 
  43. if( count($old_tags) == count($new_tags) ) { // Rename only 
  44. foreach ( (array) $old_tags as $i => $old_tag ) { 
  45. $new_name = $new_tags[$i]; 
  46.  
  47. // Get term by name 
  48. $term = get_term_by('name', $old_tag, 'ngg_tag'); 
  49. if ( !$term ) { 
  50. continue; 
  51.  
  52. // Get objects from term ID 
  53. $objects_id = get_objects_in_term( $term->term_id, 'ngg_tag', array('fields' => 'all_with_object_id')); 
  54.  
  55. // Delete old term 
  56. wp_delete_term( $term->term_id, 'ngg_tag' ); 
  57.  
  58. // Set objects to new term ! (Append no replace) 
  59. foreach ( (array) $objects_id as $object_id ) { 
  60. wp_set_object_terms( $object_id, $new_name, 'ngg_tag', true ); 
  61.  
  62. // Clean cache 
  63. clean_object_term_cache( $objects_id, 'ngg_tag'); 
  64. clean_term_cache($term->term_id, 'ngg_tag'); 
  65.  
  66. // Increment 
  67. $counter++; 
  68.  
  69. if ( $counter == 0 ) { 
  70. $return_value['message'] = __('No tag renamed.', 'nggallery'); 
  71. } else { 
  72. $return_value['message'] = sprintf(__('Renamed tag(s) «%1$s» to «%2$s»', 'nggallery'), $old, $new); 
  73. elseif ( count($new_tags) == 1 ) { // Merge 
  74. // Set new tag 
  75. $new_tag = $new_tags[0]; 
  76. if ( empty($new_tag) ) { 
  77. $return_value['message'] = __('No valid new tag.', 'nggallery'); 
  78. $return_value['status'] = 'error'; 
  79. return $return_value; 
  80.  
  81. // Get terms ID from old terms names 
  82. $terms_id = array(); 
  83. foreach ( (array) $old_tags as $old_tag ) { 
  84. $term = get_term_by('name', addslashes($old_tag), 'ngg_tag'); 
  85. $terms_id[] = (int) $term->term_id; 
  86.  
  87. // Get objects from terms ID 
  88. $objects_id = get_objects_in_term( $terms_id, 'ngg_tag', array('fields' => 'all_with_object_id')); 
  89.  
  90. // No objects ? exit ! 
  91. if ( !$objects_id ) { 
  92. $return_value['message'] = __('No objects (post/page) found for specified old tags.', 'nggallery'); 
  93. $return_value['status'] = 'error'; 
  94. return $return_value; 
  95.  
  96. // Delete old terms 
  97. foreach ( (array) $terms_id as $term_id ) { 
  98. wp_delete_term( $term_id, 'ngg_tag' ); 
  99.  
  100. // Set objects to new term ! (Append no replace) 
  101. foreach ( (array) $objects_id as $object_id ) { 
  102. wp_set_object_terms( $object_id, $new_tag, 'ngg_tag', true ); 
  103. $counter++; 
  104.  
  105. // Test if term is also a category 
  106. if ( term_exists($new_tag, 'category') ) { 
  107. // Edit the slug to use the new term 
  108. $slug = sanitize_title($new_tag); 
  109. nggTags::edit_tag_slug( $new_tag, $slug ); 
  110. unset($slug); 
  111.  
  112. // Clean cache 
  113. clean_object_term_cache( $objects_id, 'ngg_tag'); 
  114. clean_term_cache($terms_id, 'ngg_tag'); 
  115.  
  116. if ( $counter == 0 ) { 
  117. $return_value['message'] = __('No tag merged.', 'nggallery'); 
  118. } else { 
  119. $return_value['message'] = sprintf(__('Merge tag(s) «%1$s» to «%2$s». %3$s objects edited.', 'nggallery'), $old, $new, $counter); 
  120. } else { // Error 
  121. $return_value['message'] = sprintf(__('Error. Not enough tags provided to rename or merge.', 'nggallery'), $old); 
  122. $return_value['status'] = 'error'; 
  123.  
  124. do_action('ngg_manage_tags', $new_tags); 
  125.  
  126. return $return_value; 
  127.  
  128. /** 
  129. * Delete tags 
  130. */ 
  131. static function delete_tags($delete) { 
  132. $return_value = array( 
  133. 'status' => 'ok',  
  134. 'message' => '' 
  135. ); 
  136.  
  137. if ( trim( str_replace(', ', '', stripslashes($delete)) ) == '' ) { 
  138. $return_value['message'] = __('No tag specified!', 'nggallery'); 
  139. $return_value['status'] = 'error'; 
  140. return $return_value; 
  141.  
  142. // In array + filter 
  143. $delete_tags = explode(', ', $delete); 
  144. $delete_tags = array_filter($delete_tags, 'nggtags_delete_empty_element'); 
  145.  
  146. // Delete tags 
  147. $counter = 0; 
  148. foreach ( (array) $delete_tags as $tag ) { 
  149. $term = get_term_by('name', $tag, 'ngg_tag'); 
  150. $term_id = (int) $term->term_id; 
  151.  
  152. if ( $term_id != 0 ) { 
  153. wp_delete_term( $term_id, 'ngg_tag'); 
  154. clean_term_cache( $term_id, 'ngg_tag'); 
  155. $counter++; 
  156.  
  157. if ( $counter == 0 ) { 
  158. $return_value['message'] = __('No tag deleted.', 'nggallery'); 
  159. } else { 
  160. $return_value['message'] = sprintf(__('%1s tag(s) deleted.', 'nggallery'), $counter); 
  161.  
  162. do_action('ngg_manage_tags', $delete_tags); 
  163.  
  164. return $return_value; 
  165.  
  166. /** 
  167. * Edit tag slug given the name of the tag 
  168. */ 
  169. static function edit_tag_slug( $names = '', $slugs = '' ) { 
  170. $return_value = array( 
  171. 'status' => 'ok',  
  172. 'message' => '' 
  173. ); 
  174.  
  175. if ( trim( str_replace(', ', '', stripslashes($slugs)) ) == '' ) { 
  176. $return_value['message'] = __('No new slug(s) specified!', 'nggallery'); 
  177. $return_value['status'] = 'error'; 
  178. return $return_value; 
  179.  
  180. $match_names = explode(', ', $names); 
  181. $new_slugs = explode(', ', $slugs); 
  182.  
  183. $match_names = array_filter($match_names, 'nggtags_delete_empty_element'); 
  184. $new_slugs = array_filter($new_slugs, 'nggtags_delete_empty_element'); 
  185.  
  186. if ( count($match_names) != count($new_slugs) ) { 
  187. $return_value['message'] = __('Tags number and slugs number isn\'t the same!', 'nggallery'); 
  188. $return_value['status'] = 'error'; 
  189. return $return_value; 
  190. } else { 
  191. $counter = 0; 
  192. foreach ( (array) $match_names as $i => $match_name ) { 
  193. // Sanitize slug + Escape 
  194. $new_slug = sanitize_title($new_slugs[$i]); 
  195.  
  196. // Get term by name 
  197. $term = get_term_by('name', $match_name, 'ngg_tag'); 
  198. if ( !$term ) { 
  199. continue; 
  200.  
  201. // Increment 
  202. $counter++; 
  203.  
  204. // Update term 
  205. wp_update_term($term->term_id, 'ngg_tag', array('slug' => $new_slug)); 
  206.  
  207. // Clean cache 
  208. clean_term_cache($term->term_id, 'ngg_tag'); 
  209.  
  210. if ( $counter == 0 ) { 
  211. $return_value['message'] = __('No slug edited.', 'nggallery'); 
  212. } else { 
  213. $return_value['message'] = sprintf(__('%s slug(s) edited.', 'nggallery'), $counter); 
  214.  
  215. return $return_value; 
  216.  
  217. /** 
  218. * Get a list of the tags used by the images 
  219. */ 
  220. static function find_all_tags() { 
  221. return get_terms('ngg_tag', ''); 
  222.  
  223. /** 
  224. */ 
  225. static function find_tags( $args = '', $skip_cache = false ) { 
  226. $taxonomy = 'ngg_tag'; 
  227.  
  228. if ( $skip_cache == true ) { 
  229. $terms = get_terms( $taxonomy, $args ); 
  230. } else { 
  231. $key = md5(serialize($args)); 
  232.  
  233. // Get cache if exist 
  234. //-- 
  235. if ( $cache = wp_cache_get( 'ngg_get_tags', 'nggallery' ) ) { 
  236. if ( isset( $cache[$key] ) ) { 
  237. return apply_filters('get_tags', $cache[$key], $args); 
  238.  
  239. // Get tags 
  240. //-- 
  241. $terms = get_terms( $taxonomy, $args ); 
  242. if ( empty($terms) ) { 
  243. return array(); 
  244.  
  245. $cache[$key] = $terms; 
  246. wp_cache_set( 'ngg_get_tags', $cache, 'nggallery' ); 
  247.  
  248. $terms = apply_filters('get_tags', $terms, $args); 
  249. return $terms; 
  250.  
  251. /** 
  252. * Get images corresponding to a list of tags 
  253. */ 
  254. /** 
  255. * nggTags::find_images_for_tags() 
  256. * @param mixed $taglist 
  257. * @param string $mode could be 'ASC' or 'RAND' 
  258. * @return array of images 
  259. */ 
  260. static function find_images_for_tags($taglist, $mode = "ASC") { 
  261. // return the images based on the tag 
  262. global $wpdb; 
  263.  
  264. // extract it into a array 
  265. $taglist = explode(", ", $taglist); 
  266.  
  267. if ( !is_array($taglist) ) 
  268. $taglist = array($taglist); 
  269.  
  270. $taglist = array_map('trim', $taglist); 
  271. $new_slugarray = array_map('sanitize_title', $taglist); 
  272. $sluglist = implode("', '", $new_slugarray); 
  273.  
  274. //Treat % as a litteral in the database, for unicode support 
  275. $sluglist = str_replace("%", "%%", $sluglist); 
  276.  
  277. // first get all $term_ids with this tag 
  278. $term_ids = $wpdb->get_col( $wpdb->prepare("SELECT term_id FROM $wpdb->terms WHERE slug IN (%s) ORDER BY term_id ASC ", $sluglist)); 
  279. $picids = get_objects_in_term($term_ids, 'ngg_tag'); 
  280.  
  281. if ($mode == 'RAND') 
  282. shuffle($picids); 
  283.  
  284. // Now lookup in the database 
  285. $mapper = C_Image_Mapper::get_instance(); 
  286. $images = array(); 
  287. foreach ($picids as $image_id) { 
  288. $images[] = $mapper->find($image_id); 
  289.  
  290. return $images; 
  291.  
  292. /** 
  293. * Return one image based on the tag. Required for a tag based album overview 
  294. */ 
  295. static function get_album_images($taglist) { 
  296. global $wpdb; 
  297.  
  298. $taxonomy = 'ngg_tag'; 
  299.  
  300. // extract it into a array 
  301. $taglist = explode(', ', $taglist); 
  302.  
  303. if (!is_array($taglist)) { 
  304. $taglist = array($taglist); 
  305.  
  306. $taglist = array_map('trim', $taglist); 
  307. $slugarray = array_map('sanitize_title', $taglist); 
  308. $slugarray = array_unique($slugarray); 
  309.  
  310. $picarray = array(); 
  311.  
  312. foreach($slugarray as $slug) { 
  313. // get random picture of tag 
  314. $tsql = "SELECT p.*, g.*, t.*, tt.* FROM $wpdb->term_relationships AS tr"; 
  315. $tsql .= " INNER JOIN $wpdb->nggpictures AS p ON (tr.object_id = p.pid)"; 
  316. $tsql .= " INNER JOIN $wpdb->nggallery AS g ON (g.gid = p.galleryid)"; 
  317. $tsql .= " INNER JOIN $wpdb->term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)"; 
  318. $tsql .= " INNER JOIN $wpdb->terms AS t ON (tt.term_id = t.term_id)"; 
  319. $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.slug = '$slug' ORDER BY rand() limit 1 "; 
  320. $pic_data = $wpdb->get_row($tsql, OBJECT); 
  321.  
  322. if ($pic_data) $picarray[] = $pic_data; 
  323.  
  324. return $picarray;