PageRenderTime 84ms CodeModel.GetById 53ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/nextgen-gallery/lib/tags.php

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