summaryrefslogtreecommitdiff
path: root/gengo_extra_functions.php
blob: 373a79090ed5204c460a47e17fe8386c0c09d4c1 (plain)
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
<?php

// This is a supplemental file containing non-core functionality.  It provides additional features
// developed under sponsorship and should be placed in the gengo directory.  Gengo will still work
// without this file. For more information about sponsoring features, use the contact form at
// http://jamietalbot.com/about/ .

// All sponsored work will be released back into the community, under the same MIT license as Gengo,
// with a note of appreciation and a link to the sponsor's site.

// Sponsored by Eggplant Media.
// http://www.eggplant.coop
// Outputs a link to the parent of the current page.
function gengo_eggplant_parent() {
	global $gengo, $wpdb, $wp_query;

	if (!$current_page = $wp_query->posts[0]->ID) return;
	if ($page = $wpdb->get_row("SELECT p1.post_title AS post_title, p1.ID AS ID FROM $wpdb->posts p1 INNER JOIN $wpdb->posts p2 ON p1.ID = p2.post_parent WHERE p2.ID = $current_page AND p1.post_type != 'attachment' LIMIT 1")) {
		?>
		<a href="<?php echo get_page_link($page->ID) ?>" title="<?php echo wp_specialchars($page->post_title) ?>"><?php echo $page->post_title ?></a>
		<?php
	}
}

// Sponsored by Eggplant Media.
// http://www.eggplant.coop
// Lists the subpages of the current page, or its siblings if there are none.
function gengo_eggplant_subpages($args = '')
{
	global $gengo, $wpdb, $wp_query;

	parse_str($args, $r);
	if (!isset($r['title_li'])) $r['title_li'] = __('Pages');
	if (!$r['show_date']) $r['show_date'] = '';
	if (!$r['sort_column']) $r['sort_column'] = 'menu_order';
	if (!$r['sort_order']) $r['sort_order'] = 'ASC';
	if ($r['show_date']) $format = $r['date_format'] ? $r['date_format'] : get_settings('date_format');
	if ($r['exclude_above']) {
		$exclude = " AND p2.menu_order <= " . $r['exclude_above'];
	}

	if (!$current_page = $wp_query->posts[0]->ID) return;
	$language_ids = implode(',', $gengo->language_preference_id);

	if (!$pages = $wpdb->get_results("SELECT p2.post_title, p2.ID, p2.post_modified, p2.post_date FROM $gengo->post2lang_table p2l INNER JOIN $wpdb->posts p2 ON p2l.post_id = p2.ID WHERE post_parent = $current_page AND p2l.language_id IN ($language_ids) AND post_type != 'attachment' $exclude ORDER BY $r[sort_column] $r[sort_order]"))
		$pages = $wpdb->get_results("SELECT p2.post_title, p2.ID, p2.post_modified, p2.post_date FROM $wpdb->posts p1 INNER JOIN $wpdb->posts p2 ON (p1.post_parent = p2.post_parent) INNER JOIN $gengo->post2lang_table p2l ON p2.ID = p2l.post_id WHERE p1.ID = $current_page AND p2.post_type = 'page' AND p2l.language_id IN ($language_ids) $exclude ORDER BY p2.$r[sort_column] $r[sort_order]");

	if (!$pages) return;
	?>
	<li class="pagenav"><?php echo $r['title_li'] ?><ul>
	<?php
	foreach ($pages as $page)
	{
		?>
		<li class="page_item">
		<a href="<?php echo get_page_link($page->ID) ?>" title="<?php echo wp_specialchars($page->post_title) ?>"><?php echo $page->post_title ?></a>
		<?php
		if ($format)
		{
		  $date = ('modified' == $r['show_date']) ? $page->post_modified : $page->post_date;
		  echo mysql2date($format, $date);
		}
		?>
		</li>
		<?php
	}
	?>
	</ul></li>
	<?php
}

// Sponsored by Eggplant Media.
// http://www.eggplant.coop
// Returns an array containing the most recent post objects or a single post object in a given category in the current language.
// Can be called with either a category id, or the sanitised category name.
function gengo_eggplant_recent_category($category, $limit = 10)
{
	global $gengo, $wpdb;

	if (!is_numeric($limit)) $limit = 10;
	if (is_numeric($category)) $where = "p2c.category_id = $category";
	else
	{
	  $where = "c.category_nicename = '$category'";
		$join = " INNER JOIN $wpdb->categories AS c ON c.cat_ID = p2c.category_id";
	}
	$language_ids = implode(',', $gengo->language_preference_id);
	$query = "SELECT p.* FROM $wpdb->posts AS p INNER JOIN $gengo->post2lang_table AS p2l ON p.ID = p2l.post_id INNER JOIN $wpdb->post2cat AS p2c ON p2c.post_id = p.ID $join WHERE p2l.language_id IN ($language_ids) AND $where ORDER BY p.post_date DESC LIMIT $limit";

	if (1 == $limit)
	{
	  if ($result = $wpdb->get_row($query)) return $result;
	  else return NULL;
	}
	else
	{
	  if ($results = $wpdb->get_results($query)) return $results;
	  else return array();
	}
}

// Sponsored by Eggplant Media.
// http://www.eggplant.coop
// Returns a single random post from the specified category.
// Can be called with either a category id, or the sanitised category name.
function gengo_eggplant_random_category_post($category) {
	global $gengo, $wpdb;

	if (is_numeric($category)) {
		$where = "p2c.category_id = $category";
	} else {
	  $where = "c.category_nicename = '$category'";
		$join = " INNER JOIN $wpdb->categories AS c ON c.cat_ID = p2c.category_id";
	}
	$language_ids = implode(',', $gengo->language_preference_id);
	$query = "SELECT p.* FROM $wpdb->posts AS p INNER JOIN $gengo->post2lang_table AS p2l ON p.ID = p2l.post_id INNER JOIN $wpdb->post2cat AS p2c ON p2c.post_id = p.ID $join WHERE p2l.language_id IN ($language_ids) AND $where ORDER BY RAND() LIMIT 1";
	return ($result = $wpdb->get_row($query)) ? $result : NULL;
}
?>