Ical4wp

From The Lyceum Wiki

Jump to: navigation, search

The iCal4WP plugin is one of many WordPress calendar/event plugins, but, after sampling a number of them, I found this one better suited to my purposes. It is in early development, at least as I write this, but it does the job pretty well already.

I made some small modifications to get it to work with Lyceum, which I will document here just so it is documented somewhere. This applies to "version 1 alpha", although I imagine very comparable code will work with future versions.

The only changes I needed to make were in the ical4wp.php file. I should also mention that, though I'm a relatively old hand at PHP, I am new to WordPress and Lyceum, so this being a wiki, feel free to improve on what I did.

Down around line 400, there is a function defined called ical_list_events. The query needs to be updated to limit itself to the active blog.

The original query was:

		$query = "SELECT posts.* 
					FROM $wpdb->posts posts, $wpdb->postmeta postmeta
					WHERE posts.ID = postmeta.post_ID 
					AND posts.post_status = 'publish' 
					AND postmeta.meta_key = '$this->_name'";

Change that to the following (this line immediately follows the original "global" statement, and we need to add one other). This involves adding relations into the categories and post-category association tables (because the blog a post belongs to is, as far as I can see, governed only by the blog field in the categories table). Changes are on lines 1, 3 (a comma), 4, 8-9.

		global $blog;
		$query = "SELECT posts.* 
					FROM $wpdb->posts posts, $wpdb->postmeta postmeta,
					$wpdb->categories cat, $wpdb->post2cat post2cat
					WHERE posts.ID = postmeta.post_ID 
					AND posts.post_status = 'publish' 
					AND post2cat.post_id=posts.ID
					AND cat.cat_id = post2cat.category_id
					AND cat.blog= '$blog' 
					AND postmeta.meta_key = '$this->_name'";

The next function in the file is ical_list_calendars, which needs basically the same change. (Note: I don't really use this function, so I can't guarantee this modification really does what it is supposed to, though I'm sure that it at least almost works).

Add the following line just under the "global" line:

		global $blog;

And change the code immediately below to the code below that. The only change is the addition of the third line from the end of the new version (cat.blog = '$blog').

Before:

		$categories = $wpdb->get_results("
			SELECT DISTINCTROW(cat.cat_id), cat.cat_name 
			FROM $wpdb->categories AS cat, $wpdb->posts AS posts, $wpdb->postmeta AS meta, $wpdb->post2cat AS post2cat
			WHERE posts.ID = meta.post_id
			AND posts.post_status = 'publish'
			AND meta.meta_key = '$this->_name'
			AND post2cat.post_id= posts.ID
			AND cat.cat_id = post2cat.category_id
		",OBJECT);

After:

		$categories = $wpdb->get_results("
			SELECT DISTINCTROW(cat.cat_id), cat.cat_name 
			FROM $wpdb->categories AS cat, $wpdb->posts AS posts, $wpdb->postmeta AS meta, $wpdb->post2cat AS post2cat
			WHERE posts.ID = meta.post_id
			AND posts.post_status = 'publish'
			AND meta.meta_key = '$this->_name'
			AND post2cat.post_id= posts.ID
			AND cat.blog= '$blog' 
			AND cat.cat_id = post2cat.category_id
		",OBJECT);


The last function that needs updating is called send_iCal_file, a couple of functions down. Same basic change. I actually probably changed more than I had to, but I didn't go back to verify this. The first query ($cat=...) probably does not need to be limited to the active blog, since any given category belongs to exactly one blog, and this is handling a request for a specific category. The changes are to the first ($cat=...) query, and to the last ($posts=... in the else block) query.

Before:

		if ($_GET['cat']) { 
			$cat   = $wpdb->get_row("SELECT wcat.* 
									FROM $wpdb->categories wcat 
									WHERE wcat.cat_ID = '".$_GET['cat']."'", OBJECT); 
			$posts = $wpdb->get_results("SELECT posts.* 
										FROM $wpdb->posts posts, $wpdb->postmeta postmeta, $wpdb->post2cat AS post2cat
										WHERE posts.ID = postmeta.post_ID 
											AND posts.post_status = 'publish'
											AND postmeta.meta_key = '$this->_name'
											AND post2cat.category_id=$cat->cat_ID 
											AND post2cat.post_id=posts.ID",OBJECT);
		} else {
			$posts = $wpdb->get_results("SELECT posts.* 
										FROM $wpdb->posts posts, $wpdb->postmeta postmeta
										WHERE posts.ID = postmeta.post_ID 
											AND posts.post_status = 'publish' 
											AND postmeta.meta_key = '$this->_name'",OBJECT);
		}

After:

		global $blog;
		if ($_GET['cat']) { 
			$cat   = $wpdb->get_row("SELECT wcat.* 
									FROM $wpdb->categories wcat, $wpdb->post2cat post2cat
									WHERE post2cat.category_id = wcat.cat_ID
									AND wcat.blog = '$blog' 
									AND wcat.cat_ID = '".$_GET['cat']."'", OBJECT); 
			$posts = $wpdb->get_results("SELECT posts.* 
										FROM $wpdb->posts posts, $wpdb->postmeta postmeta, $wpdb->post2cat AS post2cat
										WHERE posts.ID = postmeta.post_ID 
											AND posts.post_status = 'publish'
											AND postmeta.meta_key = '$this->_name'
											AND post2cat.category_id=$cat->cat_ID 
											AND post2cat.post_id=posts.ID",OBJECT);
		} else {
			$posts = $wpdb->get_results("SELECT posts.* 
										FROM $wpdb->posts posts, $wpdb->postmeta postmeta,
										$wpdb->post2cat post2cat, $wpdb->categories cat
										WHERE posts.ID = postmeta.post_ID 
											AND cat.blog= '$blog' 
											AND post2cat.category_id= cat.cat_ID 
											AND post2cat.post_id= posts.ID 
											AND posts.post_status = 'publish' 
											AND postmeta.meta_key = '$this->_name'",OBJECT);
		}

That was it, the rest worked basically as documented by the plugin author.

No guarantees here, these were a middle-of-the-night hacks to iCal4WP that seemed to be producing the correct results. I have stress-tested neither iCal4WP nor these changes to it, but in case someone else is up late trying to get iCal4WP working properly in Lyceum, this might help.

Personal tools