/products/photocrati_nextgen/modules/ngglegacy/lib/tags.php

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