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

Language PHP Lines 350
MD5 Hash 8caef5be576746d84ff06506286001d6 Estimated Cost $6,540 (why?)
Repository https://bitbucket.org/openfarmtech/weblog-content View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/*
Simple:Press
Admin Tags Support Functions
$LastChangedDate: 2010-03-26 16:38:27 -0700 (Fri, 26 Mar 2010) $
$Rev: 3818 $
*/

if (preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF']))
{
	die('Access Denied');
}

function sfa_save_tags_edit_tags()
{
	global $wpdb, $sfglobals;

    check_admin_referer('forum-adminform_sfedittags', 'forum-adminform_sfedittags');

    $topic_id_list = $_POST['topic_id'];
    $tag_id_list = $_POST['tag_id'];
    $tag_list = $_POST['tags'];

	# take the easy way out and remove all tags and then add back in the new list
	for ($x=0; $x<count($topic_id_list); $x++)
	{
		if (!empty($tag_id_list[$x]))  # if no tags originally, dont delete anything
		{
			# grab all the tag rows and decrement the tag count
			$sql = "SELECT tag_id, tag_count FROM ".SFTAGS." WHERE tag_id IN (".sf_esc_str($tag_id_list[$x]).")";
			$tags = $wpdb->get_results($sql);
			foreach ($tags as $tag)
			{
				# decrement tag count and delete if it gets to zero or update the new count
				$tag->tag_count--;
				if ($tag->tag_count == 0)
				{

					$wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$tag->tag_id); # count is zero so delete
				} else {
					$wpdb->query("UPDATE ".SFTAGS." SET tag_count=".$tag->tag_count." WHERE tag_id=".$tag->tag_id); # update count
				}
			}

			# remove all the tag meta entries for the topic
			$wpdb->query("DELETE FROM ".SFTAGMETA." WHERE topic_id=".sf_esc_int($topic_id_list[$x]));
		}

		# now add the current tags back in for the topic
		if (!empty($tag_list[$x]))
		{
		    $tags = trim(sf_esc_str($tag_list[$x]));
		    $tags = trim($tags, ',');  # no extra commas allowed
			$tags = explode(',', $tags);
			$tags = array_unique($tags);  # remove any duplicates
			$tags = array_values($tags);  # put back in order
			if ($sfglobals['display']['topics']['maxtags'] > 0 && count($tags) > $sfglobals['display']['topics']['maxtags'])
			{
				$tags = array_slice($tags, 0, $sfglobals['display']['topics']['maxtags']);  # limit to maxt tags opton
			}
			$mess = sfc_add_tags($topic_id_list[$x], $tags);
			if ($mess != '')
			{
				return $mess;
			}
		}
	}

	$mess = __("Tags Updated", "sforum");

	return $mess;
}

function sfa_save_tags_rename_tags()
{
	global $wpdb;

    check_admin_referer('forum-adminform_sfrenametags', 'forum-adminform_sfrenametags');

    $oldtags = $_POST['renametag_old'];
    $newtags = $_POST['renametag_new'];

    if (empty($oldtags) || empty($newtags))
    {
    	$mess = __("Renaming Tags requires old Tag(s) and new Tags(s) entries - No Tags renamed!", "sforum");
		return $mess;
    }

    # prep the new tags
    $tags = trim(sf_esc_str($newtags));
    $tags = trim($tags, ',');  # no extra commas allowed
	$tags = explode(',', $tags);
	$tags = array_unique($tags);  # remove any duplicates
	$tags = array_values($tags);  # put back in order

    # prep the old tags
    $otags = trim(sf_esc_str($oldtags));
    $otags = trim($otags, ',');  # no extra commas allowed
	$otags = explode(',', $otags);
	$otags = array_unique($otags);  # remove any duplicates
	$otags = array_values($otags);  # put back in order
    foreach ($otags as $tag)
    {
		$tagslug = sf_create_slug($tag, 'tag', false);
		$tagid = $wpdb->get_var("SELECT tag_id FROM ".SFTAGS." WHERE tag_slug='".$tagslug."'");
		if ($tagid)
		{
			# delete tag itself
			$wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$tagid);

			# find the topics that use this tag
			$topics = $wpdb->get_results("SELECT topic_id FROM ".SFTAGMETA." WHERE tag_id=".$tagid);
			if ($topics)
			{
				foreach ($topics as $topic)
				{
					# delete tag metas for this topic
					$wpdb->query("DELETE FROM ".SFTAGMETA." WHERE topic_id=".$topic->topic_id." AND tag_id=".$tagid);

					# add in the new tags
					sfc_add_tags($topic->topic_id, $tags);
				}
			}
		}
    }

	$mess = __("Tags Renamed or Merged", "sforum");
	return $mess;
}

function sfa_save_tags_delete_tags()
{
	global $wpdb;

    check_admin_referer('forum-adminform_sfdeletetags', 'forum-adminform_sfdeletetags');

    $deltags = $_POST['deletetag_name'];

    if (empty($deltags))
    {
    	$mess = __("Deleting Tags requires Tags(s) entry - No Tags deleted!", "sforum");
		return $mess;
    }

	$deleted = 0; # indicate nothing deleted
    # loop through tags and delete the tag and the tag metas
    $tags = trim(sf_esc_str($deltags));
    $tags = trim($tags, ',');  # no extra commas allowed
	$tags = explode(',', $tags);
	$tags = array_unique($tags);  # remove any duplicates
	$tags = array_values($tags);  # put back in order
    foreach ($tags as $tag)
    {
		$tagslug = sf_create_slug($tag, 'tag');
		$tagid = $wpdb->get_var("SELECT tag_id FROM ".SFTAGS." WHERE tag_slug='".$tagslug."'");
		if ($tagid)
		{
			# delete tag metas with this tag id
			$wpdb->query("DELETE FROM ".SFTAGMETA." WHERE tag_id=".$tagid);

			# delete tag itself
			$wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$tagid);

			# indicate at least some tags deleted
			if ($deleted == 0) $deleted = 1;
		} else {
			if ($deleted == 1) $deleted = 2; # indicate some deleted but some not found
		}
    }

    # output deletion results message
    switch ($deleted)
    {
    	case 0:
			$mess = __("No Tags Matched For Deletion!", "sforum");
			break;
    	case 1:
			$mess = __("Tags Successfully Deleted!", "sforum");
			break;
    	case 2:
			$mess = __("Some Tags Deleted, but Others Not Found!", "sforum");
			break;
    }

	return $mess;
}

function sfa_save_tags_add_tags()
{
	global $wpdb, $sfglobals;

    check_admin_referer('forum-adminform_sfaddtags', 'forum-adminform_sfaddtags');

    $matchtags = $_POST['addtag_match'];
    $addtags = $_POST['addtag_new'];

    if (empty($addtags))
    {
    	$mess = __("Adding Tags requires new Tags(s) entry - No Tags added!", "sforum");
		return $mess;
    }

	# prep the new tags
    $tags = trim(sf_esc_str($addtags));
    $tags = trim($tags, ',');  # no extra commas allowed
	$tags = explode(',', $tags);
	$tags = array_unique($tags);  # remove any duplicates
	$tags = array_values($tags);  # put back in order
	if ($sfglobals['display']['topics']['maxtags'] > 0 && count($tags) > $sfglobals['display']['topics']['maxtags'])
	{
		$tags = array_slice($tags, 0, $sfglobals['display']['topics']['maxtags']);  # limit to max tags opton
	}

    # if not match tags, add the new tags to all topics
    if (empty($matchtags))
    {
    	# get topics
		$topics = $wpdb->get_results("SELECT topic_id FROM ".SFTOPICS);
		if ($topics)
		{
			foreach ($topics as $topic)
			{
				# now add the tags
				sfc_add_tags($topic->topic_id, $tags);
			}

			$mess = __("Tags Added to All Topics!", "sforum");
			return $mess;
		} else {
	    	$mess = __("No Topics to Add Tags to - No Tags added!", "sforum");
			return $mess;
		}
    }

	# alrighty, so need to match tags before we add the new ones
	# prep the match tags
    $mtags = trim(sf_esc_str($matchtags));
    $mtags = trim($mtags, ',');  # no extra commas allowed
	$mtags = explode(',', $mtags);
	$mtags = array_unique($mtags);  # remove any duplicates
	$mtags = array_values($mtags);  # put back in order
	if ($mtags)
	{
		$mtag_list = '(';
		$first = true;
		# Now put the tags back together in list
		foreach ($mtags as $mtag)
		{
			# convert to a tag slug and build slug list
			$mtagslug = sf_create_slug($mtag, 'tag');
			if ($first)
			{
				$mtag_list.= "'".$mtagslug."'";
				$first = false;
			} else {
				$mtag_list.= ",'".$mtagslug."'";
			}
		}
		$mtag_list.= ")";

		# grab any topics that have a matching slug
		$tagids = $wpdb->get_results("SELECT tag_id FROM ".SFTAGS." WHERE tag_slug IN".$mtag_list);
		if ($tagids)
		{
			# now find the topics with these matched tags and add the new tags
			foreach ($tagids as $tagid)
			{
				$topics = $wpdb->get_results("SELECT topic_id FROM ".SFTAGMETA." WHERE tag_id = ".$tagid->tag_id);
				if ($topics)
				{
					foreach ($topics as $topic)
					{
						# now add the tags
						sfc_add_tags($topic->topic_id, $tags);
					}
				}
			}

			$mess = __("Tags Added to Topics with Matched Tags!", "sforum");
			return $mess;
		} else {
			$mess = __("No Tags Matched!", "sforum");
			return $mess;
		}
	} else {
		$mess = __("Invalid Matching Tags Entry - No Tags Added!", "sforum");
		return $mess;
	}

	$mess = __("Oh Oh - This Shouldn't Happen!", "sforum");
	return;
}

function sfa_save_tags_cleanup_tags()
{
	global $wpdb;

    check_admin_referer('forum-adminform_sfcleanup', 'forum-adminform_sfcleanup');

	# remove orphaned tags
	$tagids = $wpdb->get_results("SELECT tag_id FROM ".SFTAGS);
	if ($tagids)
	{
		foreach ($tagids as $tagid)
		{
			$meta = $wpdb->get_results("SELECT meta_id FROM ".SFTAGMETA." WHERE tag_id = ".$tagid->tag_id);
			if (!$meta)
			{
				# no metas so its orphaned and can be deleted
				$wpdb->query("DELETE FROM ".SFTAGS." WHERE tag_id=".$tagid->tag_id);

			}
		}
	}

	# remove orphaned tag meta
	$tagids = $wpdb->get_results("SELECT tag_id FROM ".SFTAGMETA);
	if ($tagids)
	{
		foreach ($tagids as $tagid)
		{
			$tags = $wpdb->get_results("SELECT tag_id FROM ".SFTAGS." WHERE tag_id = ".$tagid->tag_id);
			if (!$tags)
			{
				# no tags so its orphaned and can be deleted
				$wpdb->query("DELETE FROM ".SFTAGMETA." WHERE tag_id=".$tagid->tag_id);

			}
		}
	}

	# clean up the tag counts
	$tagids = $wpdb->get_results("SELECT tag_id FROM ".SFTAGS);
	if ($tagids)
	{
		foreach ($tagids as $tagid)
		{
			# get the number of topics using this tag
			$count = $wpdb->get_var("SELECT COUNT(tag_id) FROM ".SFTAGMETA." WHERE tag_id = ".$tagid->tag_id);

			# set the count to number of topics using
			$wpdb->query("UPDATE ".SFTAGS." SET tag_count=".$count." WHERE tag_id = ".$tagid->tag_id);
		}
	}

	$mess = __("Tags Database Cleaned Up!", "sforum");
	return $mess;
}

?>
Back to Top