nggTags

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

Defined (1)

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

/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. No enough tags for rename. Too for merge. Choose !', 'nggallery'), $old); 
  122. $return_value['status'] = 'error'; 
  123.  
  124. return $return_value; 
  125.  
  126. /** 
  127. * Delete tags 
  128. */ 
  129. static function delete_tags($delete) { 
  130. $return_value = array( 
  131. 'status' => 'ok',  
  132. 'message' => '' 
  133. ); 
  134.  
  135. if ( trim( str_replace(', ', '', stripslashes($delete)) ) == '' ) { 
  136. $return_value['message'] = __('No tag specified!', 'nggallery'); 
  137. $return_value['status'] = 'error'; 
  138. return $return_value; 
  139.  
  140. // In array + filter 
  141. $delete_tags = explode(', ', $delete); 
  142. $delete_tags = array_filter($delete_tags, 'nggtags_delete_empty_element'); 
  143.  
  144. // Delete tags 
  145. $counter = 0; 
  146. foreach ( (array) $delete_tags as $tag ) { 
  147. $term = get_term_by('name', $tag, 'ngg_tag'); 
  148. $term_id = (int) $term->term_id; 
  149.  
  150. if ( $term_id != 0 ) { 
  151. wp_delete_term( $term_id, 'ngg_tag'); 
  152. clean_term_cache( $term_id, 'ngg_tag'); 
  153. $counter++; 
  154.  
  155. if ( $counter == 0 ) { 
  156. $return_value['message'] = __('No tag deleted.', 'nggallery'); 
  157. } else { 
  158. $return_value['message'] = sprintf(__('%1s tag(s) deleted.', 'nggallery'), $counter); 
  159.  
  160. /** 
  161. * Edit tag slug given the name of the tag 
  162. */ 
  163. static function edit_tag_slug( $names = '', $slugs = '' ) { 
  164. $return_value = array( 
  165. 'status' => 'ok',  
  166. 'message' => '' 
  167. ); 
  168.  
  169. if ( trim( str_replace(', ', '', stripslashes($slugs)) ) == '' ) { 
  170. $return_value['message'] = __('No new slug(s) specified!', 'nggallery'); 
  171. $return_value['status'] = 'error'; 
  172. return $return_value; 
  173.  
  174. $match_names = explode(', ', $names); 
  175. $new_slugs = explode(', ', $slugs); 
  176.  
  177. $match_names = array_filter($match_names, 'nggtags_delete_empty_element'); 
  178. $new_slugs = array_filter($new_slugs, 'nggtags_delete_empty_element'); 
  179.  
  180. if ( count($match_names) != count($new_slugs) ) { 
  181. $return_value['message'] = __('Tags number and slugs number isn\'t the same!', 'nggallery'); 
  182. $return_value['status'] = 'error'; 
  183. return $return_value; 
  184. } else { 
  185. $counter = 0; 
  186. foreach ( (array) $match_names as $i => $match_name ) { 
  187. // Sanitize slug + Escape 
  188. $new_slug = sanitize_title($new_slugs[$i]); 
  189.  
  190. // Get term by name 
  191. $term = get_term_by('name', $match_name, 'ngg_tag'); 
  192. if ( !$term ) { 
  193. continue; 
  194.  
  195. // Increment 
  196. $counter++; 
  197.  
  198. // Update term 
  199. wp_update_term($term->term_id, 'ngg_tag', array('slug' => $new_slug)); 
  200.  
  201. // Clean cache 
  202. clean_term_cache($term->term_id, 'ngg_tag'); 
  203.  
  204. if ( $counter == 0 ) { 
  205. $return_value['message'] = __('No slug edited.', 'nggallery'); 
  206. } else { 
  207. $return_value['message'] = sprintf(__('%s slug(s) edited.', 'nggallery'), $counter); 
  208.  
  209. return $return_value; 
  210.  
  211. /** 
  212. * @param $taglist 
  213. * @return mixed|string 
  214. * Sanitize tag list suppressing spaces, sanitizing every tag and solving issues 
  215. * with unicode support. 
  216. * 20140605: '%' character does not pass through sanitize_title 
  217. * Note: this list cannot be handled with wpdb->prepare since single quotes 
  218. * will be escaped(!) 
  219. */ 
  220. public static function sanitize_taglist($taglist) 
  221. // extract it into a array 
  222. $taglist = explode(", ", $taglist); 
  223. //if we don't have a list, make an array with one item 
  224. if (!is_array($taglist)) 
  225. $taglist = array($taglist); 
  226. //suppress spaces in all terms on the list. 
  227. $taglist = array_map('trim', $taglist); 
  228. //and sanitize every term 
  229. $new_slugarray = array_map('sanitize_title', $taglist); 
  230. //recompose list of terms and make one simple string 
  231. $sluglist = implode("', '", $new_slugarray); 
  232. if (empty($sluglist)) return ""; 
  233. //Treat % as a literal in the database, for unicode support 
  234. // $sluglist = str_replace("%", "%%", $sluglist); 
  235. return "'" . $sluglist . "'"; 
  236.  
  237. /** 
  238. * Get a list of the tags used by the images 
  239. */ 
  240. function find_all_tags() { 
  241. return get_terms('ngg_tag', ''); 
  242.  
  243. /** 
  244. * 20131004: corrected to static to avoid error over strict standards 
  245. */ 
  246. static function find_tags( $args = '', $skip_cache = false ) { 
  247. $taxonomy = 'ngg_tag'; 
  248.  
  249. if ( $skip_cache == true ) { 
  250. $terms = get_terms( $taxonomy, $args ); 
  251. } else { 
  252. $key = md5(serialize($args)); 
  253.  
  254. // Get cache if exist 
  255. //-- 
  256. if ( $cache = wp_cache_get( 'ngg_get_tags', 'nggallery' ) ) { 
  257. if ( isset( $cache[$key] ) ) { 
  258. return apply_filters('get_tags', $cache[$key], $args); 
  259.  
  260. // Get tags 
  261. //-- 
  262. $terms = get_terms( $taxonomy, $args ); 
  263. if ( empty($terms) ) { 
  264. return array(); 
  265.  
  266. $cache[$key] = $terms; 
  267. wp_cache_set( 'ngg_get_tags', $cache, 'nggallery' ); 
  268.  
  269. $terms = apply_filters('get_tags', $terms, $args); 
  270. return $terms; 
  271.  
  272. /** 
  273. * Get images corresponding to a list of tags 
  274. */ 
  275. /** 
  276. * nggTags::find_images_for_tags() 
  277. * 20140120: Mode DESC added. 
  278. * 20140611:Dropped use of wpdb->prepare since single quotes are scaped. 
  279. * @param mixed $taglist 
  280. * @param string $sorting could be 'ASC' or 'RAND' or 'DESC' 
  281. * @return array of images 
  282. */ 
  283. static function find_images_for_tags($taglist, $sorting = "ASC") { 
  284. // return the images based on the tag 
  285. global $wpdb; 
  286. $modes = array ('ASC', 'DESC', 'RAND'); 
  287. //Sanitize & standarize list 
  288. $sluglist = self::sanitize_taglist($taglist); 
  289.  
  290. // first get all $term_ids with this tag 
  291. //Fix for WP 3.9 . See http://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/ 
  292. $prepared = "SELECT term_id FROM $wpdb->terms WHERE slug IN ($sluglist) ORDER BY term_id ASC "; 
  293. $term_ids = $wpdb->get_col( $prepared ); 
  294. //Get the objects related with the taxonomy defined by NextCellent 
  295. $picids = get_objects_in_term($term_ids, 'ngg_tag'); 
  296.  
  297. //Now lookup in the database 
  298. $sorting = in_array($sorting, $modes)?$sorting:'ASC'; 
  299. $pictures = nggdb::find_images_in_list($picids, true, $sorting ); 
  300. return $pictures; 
  301.  
  302. /** 
  303. * Return one image based on the tag. Required for a tag based album overview 
  304. */ 
  305. static function get_album_images($taglist) { 
  306. global $wpdb; 
  307.  
  308. $taxonomy = 'ngg_tag'; 
  309.  
  310. // extract it into a array 
  311. $taglist = explode(', ', $taglist); 
  312.  
  313. if (!is_array($taglist)) { 
  314. $taglist = array($taglist); 
  315.  
  316. $taglist = array_map('trim', $taglist); 
  317. $slugarray = array_map('sanitize_title', $taglist); 
  318. $slugarray = array_unique($slugarray); 
  319.  
  320. $picarray = array(); 
  321.  
  322. foreach($slugarray as $slug) { 
  323. // get random picture of tag 
  324. $tsql = "SELECT p.*, g.*, t.*, tt.* FROM $wpdb->term_relationships AS tr"; 
  325. $tsql .= " INNER JOIN $wpdb->nggpictures AS p ON (tr.object_id = p.pid)"; 
  326. $tsql .= " INNER JOIN $wpdb->nggallery AS g ON (g.gid = p.galleryid)"; 
  327. $tsql .= " INNER JOIN $wpdb->term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)"; 
  328. $tsql .= " INNER JOIN $wpdb->terms AS t ON (tt.term_id = t.term_id)"; 
  329. $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.slug = '$slug' ORDER BY rand() limit 1 "; 
  330. $pic_data = $wpdb->get_row($tsql, OBJECT); 
  331.  
  332. if ($pic_data) $picarray[] = $pic_data; 
  333.  
  334. return $picarray;