PageRenderTime 154ms CodeModel.GetById 40ms app.highlight 15ms RepoModel.GetById 45ms app.codeStats 0ms

/wp-content/plugins/simple-forum/admin/panel-tags/support/sfa-tags-save.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 350 lines | 278 code | 39 blank | 33 comment | 36 complexity | 8caef5be576746d84ff06506286001d6 MD5 | raw file
  1<?php
  2/*
  3Simple:Press
  4Admin Tags Support Functions
  5$LastChangedDate: 2010-03-26 16:38:27 -0700 (Fri, 26 Mar 2010) $
  6$Rev: 3818 $
  7*/
  8
  9if (preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF']))
 10{
 11	die('Access Denied');
 12}
 13
 14function sfa_save_tags_edit_tags()
 15{
 16	global $wpdb, $sfglobals;
 17
 18    check_admin_referer('forum-adminform_sfedittags', 'forum-adminform_sfedittags');
 19
 20    $topic_id_list = $_POST['topic_id'];
 21    $tag_id_list = $_POST['tag_id'];
 22    $tag_list = $_POST['tags'];
 23
 24	# take the easy way out and remove all tags and then add back in the new list
 25	for ($x=0; $x<count($topic_id_list); $x++)
 26	{
 27		if (!empty($tag_id_list[$x]))  # if no tags originally, dont delete anything
 28		{
 29			# grab all the tag rows and decrement the tag count
 30			$sql = "SELECT tag_id, tag_count FROM ".SFTAGS." WHERE tag_id IN (".sf_esc_str($tag_id_list[$x]).")";
 31			$tags = $wpdb->get_results($sql);
 32			foreach ($tags as $tag)
 33			{
 34				# decrement tag count and delete if it gets to zero or update the new count
 35				$tag->tag_count--;
 36				if ($tag->tag_count == 0)
 37				{
 38
 39					$wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$tag->tag_id); # count is zero so delete
 40				} else {
 41					$wpdb->query("UPDATE ".SFTAGS." SET tag_count=".$tag->tag_count." WHERE tag_id=".$tag->tag_id); # update count
 42				}
 43			}
 44
 45			# remove all the tag meta entries for the topic
 46			$wpdb->query("DELETE FROM ".SFTAGMETA." WHERE topic_id=".sf_esc_int($topic_id_list[$x]));
 47		}
 48
 49		# now add the current tags back in for the topic
 50		if (!empty($tag_list[$x]))
 51		{
 52		    $tags = trim(sf_esc_str($tag_list[$x]));
 53		    $tags = trim($tags, ',');  # no extra commas allowed
 54			$tags = explode(',', $tags);
 55			$tags = array_unique($tags);  # remove any duplicates
 56			$tags = array_values($tags);  # put back in order
 57			if ($sfglobals['display']['topics']['maxtags'] > 0 && count($tags) > $sfglobals['display']['topics']['maxtags'])
 58			{
 59				$tags = array_slice($tags, 0, $sfglobals['display']['topics']['maxtags']);  # limit to maxt tags opton
 60			}
 61			$mess = sfc_add_tags($topic_id_list[$x], $tags);
 62			if ($mess != '')
 63			{
 64				return $mess;
 65			}
 66		}
 67	}
 68
 69	$mess = __("Tags Updated", "sforum");
 70
 71	return $mess;
 72}
 73
 74function sfa_save_tags_rename_tags()
 75{
 76	global $wpdb;
 77
 78    check_admin_referer('forum-adminform_sfrenametags', 'forum-adminform_sfrenametags');
 79
 80    $oldtags = $_POST['renametag_old'];
 81    $newtags = $_POST['renametag_new'];
 82
 83    if (empty($oldtags) || empty($newtags))
 84    {
 85    	$mess = __("Renaming Tags requires old Tag(s) and new Tags(s) entries - No Tags renamed!", "sforum");
 86		return $mess;
 87    }
 88
 89    # prep the new tags
 90    $tags = trim(sf_esc_str($newtags));
 91    $tags = trim($tags, ',');  # no extra commas allowed
 92	$tags = explode(',', $tags);
 93	$tags = array_unique($tags);  # remove any duplicates
 94	$tags = array_values($tags);  # put back in order
 95
 96    # prep the old tags
 97    $otags = trim(sf_esc_str($oldtags));
 98    $otags = trim($otags, ',');  # no extra commas allowed
 99	$otags = explode(',', $otags);
100	$otags = array_unique($otags);  # remove any duplicates
101	$otags = array_values($otags);  # put back in order
102    foreach ($otags as $tag)
103    {
104		$tagslug = sf_create_slug($tag, 'tag', false);
105		$tagid = $wpdb->get_var("SELECT tag_id FROM ".SFTAGS." WHERE tag_slug='".$tagslug."'");
106		if ($tagid)
107		{
108			# delete tag itself
109			$wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$tagid);
110
111			# find the topics that use this tag
112			$topics = $wpdb->get_results("SELECT topic_id FROM ".SFTAGMETA." WHERE tag_id=".$tagid);
113			if ($topics)
114			{
115				foreach ($topics as $topic)
116				{
117					# delete tag metas for this topic
118					$wpdb->query("DELETE FROM ".SFTAGMETA." WHERE topic_id=".$topic->topic_id." AND tag_id=".$tagid);
119
120					# add in the new tags
121					sfc_add_tags($topic->topic_id, $tags);
122				}
123			}
124		}
125    }
126
127	$mess = __("Tags Renamed or Merged", "sforum");
128	return $mess;
129}
130
131function sfa_save_tags_delete_tags()
132{
133	global $wpdb;
134
135    check_admin_referer('forum-adminform_sfdeletetags', 'forum-adminform_sfdeletetags');
136
137    $deltags = $_POST['deletetag_name'];
138
139    if (empty($deltags))
140    {
141    	$mess = __("Deleting Tags requires Tags(s) entry - No Tags deleted!", "sforum");
142		return $mess;
143    }
144
145	$deleted = 0; # indicate nothing deleted
146    # loop through tags and delete the tag and the tag metas
147    $tags = trim(sf_esc_str($deltags));
148    $tags = trim($tags, ',');  # no extra commas allowed
149	$tags = explode(',', $tags);
150	$tags = array_unique($tags);  # remove any duplicates
151	$tags = array_values($tags);  # put back in order
152    foreach ($tags as $tag)
153    {
154		$tagslug = sf_create_slug($tag, 'tag');
155		$tagid = $wpdb->get_var("SELECT tag_id FROM ".SFTAGS." WHERE tag_slug='".$tagslug."'");
156		if ($tagid)
157		{
158			# delete tag metas with this tag id
159			$wpdb->query("DELETE FROM ".SFTAGMETA." WHERE tag_id=".$tagid);
160
161			# delete tag itself
162			$wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$tagid);
163
164			# indicate at least some tags deleted
165			if ($deleted == 0) $deleted = 1;
166		} else {
167			if ($deleted == 1) $deleted = 2; # indicate some deleted but some not found
168		}
169    }
170
171    # output deletion results message
172    switch ($deleted)
173    {
174    	case 0:
175			$mess = __("No Tags Matched For Deletion!", "sforum");
176			break;
177    	case 1:
178			$mess = __("Tags Successfully Deleted!", "sforum");
179			break;
180    	case 2:
181			$mess = __("Some Tags Deleted, but Others Not Found!", "sforum");
182			break;
183    }
184
185	return $mess;
186}
187
188function sfa_save_tags_add_tags()
189{
190	global $wpdb, $sfglobals;
191
192    check_admin_referer('forum-adminform_sfaddtags', 'forum-adminform_sfaddtags');
193
194    $matchtags = $_POST['addtag_match'];
195    $addtags = $_POST['addtag_new'];
196
197    if (empty($addtags))
198    {
199    	$mess = __("Adding Tags requires new Tags(s) entry - No Tags added!", "sforum");
200		return $mess;
201    }
202
203	# prep the new tags
204    $tags = trim(sf_esc_str($addtags));
205    $tags = trim($tags, ',');  # no extra commas allowed
206	$tags = explode(',', $tags);
207	$tags = array_unique($tags);  # remove any duplicates
208	$tags = array_values($tags);  # put back in order
209	if ($sfglobals['display']['topics']['maxtags'] > 0 && count($tags) > $sfglobals['display']['topics']['maxtags'])
210	{
211		$tags = array_slice($tags, 0, $sfglobals['display']['topics']['maxtags']);  # limit to max tags opton
212	}
213
214    # if not match tags, add the new tags to all topics
215    if (empty($matchtags))
216    {
217    	# get topics
218		$topics = $wpdb->get_results("SELECT topic_id FROM ".SFTOPICS);
219		if ($topics)
220		{
221			foreach ($topics as $topic)
222			{
223				# now add the tags
224				sfc_add_tags($topic->topic_id, $tags);
225			}
226
227			$mess = __("Tags Added to All Topics!", "sforum");
228			return $mess;
229		} else {
230	    	$mess = __("No Topics to Add Tags to - No Tags added!", "sforum");
231			return $mess;
232		}
233    }
234
235	# alrighty, so need to match tags before we add the new ones
236	# prep the match tags
237    $mtags = trim(sf_esc_str($matchtags));
238    $mtags = trim($mtags, ',');  # no extra commas allowed
239	$mtags = explode(',', $mtags);
240	$mtags = array_unique($mtags);  # remove any duplicates
241	$mtags = array_values($mtags);  # put back in order
242	if ($mtags)
243	{
244		$mtag_list = '(';
245		$first = true;
246		# Now put the tags back together in list
247		foreach ($mtags as $mtag)
248		{
249			# convert to a tag slug and build slug list
250			$mtagslug = sf_create_slug($mtag, 'tag');
251			if ($first)
252			{
253				$mtag_list.= "'".$mtagslug."'";
254				$first = false;
255			} else {
256				$mtag_list.= ",'".$mtagslug."'";
257			}
258		}
259		$mtag_list.= ")";
260
261		# grab any topics that have a matching slug
262		$tagids = $wpdb->get_results("SELECT tag_id FROM ".SFTAGS." WHERE tag_slug IN".$mtag_list);
263		if ($tagids)
264		{
265			# now find the topics with these matched tags and add the new tags
266			foreach ($tagids as $tagid)
267			{
268				$topics = $wpdb->get_results("SELECT topic_id FROM ".SFTAGMETA." WHERE tag_id = ".$tagid->tag_id);
269				if ($topics)
270				{
271					foreach ($topics as $topic)
272					{
273						# now add the tags
274						sfc_add_tags($topic->topic_id, $tags);
275					}
276				}
277			}
278
279			$mess = __("Tags Added to Topics with Matched Tags!", "sforum");
280			return $mess;
281		} else {
282			$mess = __("No Tags Matched!", "sforum");
283			return $mess;
284		}
285	} else {
286		$mess = __("Invalid Matching Tags Entry - No Tags Added!", "sforum");
287		return $mess;
288	}
289
290	$mess = __("Oh Oh - This Shouldn't Happen!", "sforum");
291	return;
292}
293
294function sfa_save_tags_cleanup_tags()
295{
296	global $wpdb;
297
298    check_admin_referer('forum-adminform_sfcleanup', 'forum-adminform_sfcleanup');
299
300	# remove orphaned tags
301	$tagids = $wpdb->get_results("SELECT tag_id FROM ".SFTAGS);
302	if ($tagids)
303	{
304		foreach ($tagids as $tagid)
305		{
306			$meta = $wpdb->get_results("SELECT meta_id FROM ".SFTAGMETA." WHERE tag_id = ".$tagid->tag_id);
307			if (!$meta)
308			{
309				# no metas so its orphaned and can be deleted
310				$wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$tagid->tag_id);
311
312			}
313		}
314	}
315
316	# remove orphaned tag meta
317	$tagids = $wpdb->get_results("SELECT tag_id FROM ".SFTAGMETA);
318	if ($tagids)
319	{
320		foreach ($tagids as $tagid)
321		{
322			$tags = $wpdb->get_results("SELECT tag_id FROM ".SFTAGS." WHERE tag_id = ".$tagid->tag_id);
323			if (!$tags)
324			{
325				# no tags so its orphaned and can be deleted
326				$wpdb->query("DELETE FROM ".SFTAGMETA." WHERE tag_id=".$tagid->tag_id);
327
328			}
329		}
330	}
331
332	# clean up the tag counts
333	$tagids = $wpdb->get_results("SELECT tag_id FROM ".SFTAGS);
334	if ($tagids)
335	{
336		foreach ($tagids as $tagid)
337		{
338			# get the number of topics using this tag
339			$count = $wpdb->get_var("SELECT COUNT(tag_id) FROM ".SFTAGMETA." WHERE tag_id = ".$tagid->tag_id);
340
341			# set the count to number of topics using
342			$wpdb->query("UPDATE ".SFTAGS." SET tag_count=".$count." WHERE tag_id = ".$tagid->tag_id);
343		}
344	}
345
346	$mess = __("Tags Database Cleaned Up!", "sforum");
347	return $mess;
348}
349
350?>