web/wp-content/plugins/page2cat/page2cat.php
branchwordpress
changeset 109 03b0d1493584
child 194 32102edaa81b
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: Category Page 
       
     4 Plugin URI: http://pixline.net/wordpress-plugins/category-page-wordpress-plugin/en/
       
     5 Description: Use pages as category archive header, or list some posts from a category into a post/page. 
       
     6 Author: Pixline
       
     7 Version: 2.5
       
     8 Author URI: http://pixline.net/
       
     9 
       
    10 Copyright (C) 2008 Paolo Tresso / Pixline (http://pixline.net/)
       
    11 
       
    12 This program is free software; you can redistribute it and/or
       
    13 modify it under the terms of the GNU General Public License
       
    14 as published by the Free Software Foundation; either version 2
       
    15 of the License, or (at your option) any later version.
       
    16 
       
    17 This program is distributed in the hope that it will be useful,
       
    18 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    20 GNU General Public License for more details.
       
    21 
       
    22 You should have received a copy of the GNU General Public License
       
    23 along with this program; if not, write to the Free Software
       
    24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    25 
       
    26 
       
    27 */
       
    28 if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }
       
    29 
       
    30 
       
    31 define("PAGE2CAT_VERSION","2.5");
       
    32 define("PLUGIN_PATH",get_bloginfo('url')."/wp-content/plugins/page2cat/");
       
    33 
       
    34 define('PIXLINE_FOOTER','<p><hr style="margin-top:50px;height:1px;background:#CCC;border:0px;clear:both;" />
       
    35 <strong>
       
    36 <a href="http://pixline.net/wordpress-plugins/category-page-wordpress-plugin/en/">Category Page</a></strong> plugin 
       
    37 <small> | 
       
    38 <a href="http://talks.pixline.net/forum.php?id=3">Support Forum</a> | 
       
    39 GPL2&copy; &ge; 2007 <a href="http://pixline.net/about/en/">Paolo Tresso / Pixline</a> | 
       
    40 If you like this plugin, support its development with a <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=paolo%40pixline%2enet&item_name=Support%20to%20opensource%20projects%20at%20Pixline&no_shipping=1&cn=Something%20to%20say%3f&tax=0&currency_code=EUR&lc=IT&bn=PP%2dDonationsBF&charset=UTF%2d8">small donation</a>.</p>');
       
    41 
       
    42 $wpdb->page2cat	= $table_prefix . 'page2cat';
       
    43 
       
    44 ######################################################
       
    45 # check version. #
       
    46 ######################################################
       
    47 
       
    48 $p2c_version = get_option('pixline_page2cat_version');
       
    49 	register_activation_hook(__FILE__, 'page2cat_install');
       
    50 
       
    51 // DB installation. usually happen just one time.
       
    52 function page2cat_install(){
       
    53 global $wpdb, $p2c_version;
       
    54 
       
    55 if ($version < 6):
       
    56 	$query = "
       
    57 	CREATE TABLE  `".$wpdb->prefix."page2cat` (
       
    58 	 `rel_ID` BIGINT NOT NULL AUTO_INCREMENT ,
       
    59 	 `page_ID` BIGINT NOT NULL ,
       
    60 	 `cat_ID` BIGINT NOT NULL ,
       
    61 	PRIMARY KEY (  `rel_ID` ) ,
       
    62 	INDEX (  `page_ID` ,  `cat_ID` )
       
    63 	);
       
    64 	";
       
    65 
       
    66 	if(is_file(ABSPATH . 'wp-admin/includes/upgrade.php')){ 
       
    67 		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
       
    68 	}elseif(is_file(ABSPATH . 'wp-admin/upgrade-functions.php')){
       
    69 		require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
       
    70 	}
       
    71 	
       
    72 	if($wpdb->get_var("SHOW TABLES LIKE '".$wpdb->prefix."page2cat'") != $wpdb->prefix."page2cat") {
       
    73 	dbDelta($query);
       
    74 	delete_option('pixline_page2cat_version');
       
    75 	add_option('pixline_page2cat_version', 6);
       
    76 	}
       
    77 
       
    78 update_option('p2c_use_empty','false');
       
    79 update_option('p2c_show_used_pages','false');
       
    80 update_option('p2c_catlist_limit',5);
       
    81 update_option('p2c_catlist_title','');
       
    82 endif;
       
    83 
       
    84 delete_option('p2c_use_img');		// deprecated, so we're going to delete it anyway.
       
    85 if( !get_option('p2c_use_empty')) 		update_option('p2c_use_empty','false');
       
    86 if(!get_option('p2c_show_used_pages')) 	update_option('p2c_show_used_pages','false');
       
    87 if(!get_option('p2c_catlist_limit')) 	update_option('p2c_catlist_limit',5);
       
    88 if(!get_option('p2c_catlist_title')) 	update_option('p2c_catlist_title','');
       
    89 }
       
    90 
       
    91 ######################################
       
    92 # setup management and option panels #
       
    93 ######################################
       
    94 
       
    95 // adds a panel under "manage ยป category pages"
       
    96 // shows actual connections, and allow creations of new ones.
       
    97 function page2cat_manage_page(){
       
    98 global $wpdb, $wp_db_version;
       
    99 
       
   100 	#print_r($_POST); #die();
       
   101 	
       
   102 	// multiple delete
       
   103 	if( isset($_POST['deleteit']) && !empty($_POST['p2c_reldel'])):
       
   104 		foreach($_POST['p2c_reldel'] as $key=>$id):
       
   105 			$query = "DELETE FROM {$wpdb->page2cat} WHERE rel_ID = '".$id."' LIMIT 1;";
       
   106 			$daje = $wpdb->get_results($query);
       
   107 		endforeach;
       
   108 	echo '<div id="message" class="updated fade">
       
   109 			<p><strong>Some connections deleted.</strong></p></div>';
       
   110 	endif;
       
   111 
       
   112 	// multiple link
       
   113 	if( isset($_POST['action']) && $_POST['p2c_linkto'] == true && $_POST['multipageid'] != ""):
       
   114 #	print_r($_POST);
       
   115 		$pageid = $_POST['multipageid'][0];
       
   116 		foreach($_POST['p2c_reladd'] as $key=>$id):		
       
   117 		if(trim($id)!=""):
       
   118 			$query = "INSERT INTO {$wpdb->page2cat} (rel_ID, page_ID, cat_ID) VALUES ('', '".$pageid."', '".$id."');";
       
   119 			$daje = $wpdb->get_results($query);
       
   120 		endif;
       
   121 		endforeach;
       
   122 		echo '<div id="message" class="updated fade">
       
   123 			<p><strong>Connections created.</strong></p></div>';
       
   124 	
       
   125 	elseif( isset($_POST['action']) && $_POST['p2c_update'] == true):
       
   126 #	print_r($_POST); die();
       
   127 		$pageid = $_POST['pageid'];
       
   128 		foreach($pageid as $cat_id=>$page_id):
       
   129 			if(trim($page_id) != ""):
       
   130 			$sql = "INSERT INTO {$wpdb->page2cat} (rel_ID, page_ID, cat_ID) VALUES ('', '".$page_id."', '".$cat_id."');";
       
   131 			$result = $wpdb->get_results($sql);
       
   132 			endif;
       
   133 		endforeach;
       
   134 		echo('<div id="message" class="updated fade">
       
   135 		<p><strong>New Category Page created succesfully.</strong></p></div>');
       
   136 	endif;
       
   137 
       
   138 	// single delete, confirm request
       
   139 	if( isset($_REQUEST['p2cdel']) && !isset($_REQUEST['p2c_confirm']) ):
       
   140 		$relid = $wpdb->escape($_REQUEST['p2cdel']);
       
   141 		echo "<div class='wrap'><h2>Delete this Category &rsaquo; Page relation?</h2>";
       
   142 		$opt = $wpdb->get_results("SELECT * FROM {$wpdb->page2cat} WHERE rel_ID = '".$relid."' LIMIT 1;");
       
   143 		$catid = $opt[0]->cat_ID;
       
   144 		$pageid = $opt[0]->page_ID;
       
   145 		$catname = get_cat_name($catid);
       
   146 		$page = get_page($pageid); 
       
   147 		$title = $page->post_title;
       
   148 		echo "<form name='p2c_confirm' method='post'>";
       
   149 		echo "<p>You are going to delete the relationship between 
       
   150 		Category <em>".$catname."</em> and Page <em>".$title."</em>. Confirm?</p>";
       
   151 		echo '<p>
       
   152 		<input type="hidden" name="p2cdel" value="'.$relid.'" />
       
   153 		<input type="hidden" name="p2c_confirm" value="true" />
       
   154 		<input type="reset" name="revert" value="Cancel" />
       
   155 		<input type="submit" name="confirm" value="DELETE &raquo;" />
       
   156 		</p></form>';
       
   157 		echo "</div>";
       
   158 	endif;
       
   159 
       
   160 	// confirmed, so delete this connection
       
   161 	if( isset($_REQUEST['p2cdel']) && $_REQUEST['p2c_confirm'] == 'true'):
       
   162 	$relid = $wpdb->escape($_REQUEST['p2cdel']);
       
   163 	$myopts = $wpdb->get_results("DELETE FROM {$wpdb->page2cat} WHERE rel_ID = '".$relid."' LIMIT 1;");
       
   164 	echo('<div id="message" class="updated fade"><p><strong>You have succesfully deleted a connection.</strong></p></div>');
       
   165 	echo "<div class='wrap'><a href='edit.php?page=category-pages'>Back to Category Pages manage page</a></div>";
       
   166 	endif;
       
   167 
       
   168 
       
   169 	/*
       
   170 		real pages
       
   171 	*/
       
   172 	$count_used = $wpdb->get_var("SELECT count(*) FROM {$wpdb->page2cat};");
       
   173 	
       
   174 	if($wp_db_version >= 6124):	// this is WordPress >= 2.3
       
   175 	$count_full = $wpdb->get_var("SELECT count(*) 
       
   176 		FROM {$wpdb->terms} AS t LEFT JOIN {$wpdb->term_taxonomy} AS tt 
       
   177 		ON t.term_id = tt.term_id WHERE tt.taxonomy='category';");
       
   178 
       
   179 	$count_noempty = $wpdb->get_var("SELECT count(*) 
       
   180 		FROM {$wpdb->terms} AS t LEFT JOIN {$wpdb->term_taxonomy} AS tt 
       
   181 		ON t.term_id = tt.term_id WHERE tt.taxonomy='category' AND tt.count != '0';");
       
   182 	else: // WP < 2.3
       
   183 	$count_full = $wpdb->get_var("SELECT count(*) FROM {$wpdb->categories};");
       
   184 	$count_noempty = $wpdb->get_var("SELECT count(*) FROM {$wpdb->categories}; WHERE category_count != '0';");
       
   185 	endif;
       
   186 	
       
   187 	$count_free = $count_full - $count_used;
       
   188 	if(get_option('p2c_use_empty') == "false"): $count_usable = $count_noempty - $count_used; else : $count_usable = $count_free; endif;
       
   189 	
       
   190 
       
   191 	// this is the already connected page
       
   192 
       
   193 
       
   194 	$myopts = $wpdb->get_results("SELECT * FROM {$wpdb->page2cat} ORDER BY page_ID ASC;");
       
   195 	$usedcats = array();
       
   196 	$usedpages = array();
       
   197 
       
   198 		if($_REQUEST['view'] == 'new'): 
       
   199 			$css1 = ''; 	$css2 = ' class="current"'; 
       
   200 			$pagetitle = "<small>New connection</small>"; 
       
   201 		else: 
       
   202 			$css1 = ' class="current"'; $css2 = ''; 
       
   203 			$pagetitle = "<small>Manage connections</small>"; 
       
   204 		endif;
       
   205 
       
   206 
       
   207 	if( !isset($_REQUEST['p2cdel'])):
       
   208 		echo "<div class='wrap'><h2>Category Pages</h2>";
       
   209 		echo '<ul class="subsubsub">
       
   210 		<li><a href="edit.php?page=category-pages"'.$css1.'>Manage connections ('.$count_used.')</a> |</li>
       
   211 		<li><a href="edit.php?page=category-pages&view=new"'.$css2.'>Free categories ('.$count_usable.'/'.$count_free.')</a></li>
       
   212 		</ul>';
       
   213 
       
   214 if($_REQUEST['view'] != 'new'):
       
   215 
       
   216 		if(count($myopts) != 0):
       
   217 #		echo "<div><p>These categories and these pages are already connected each other. You can view the category page in your website, clicking on the Category name. You can also edit or delete each connection.</p>";
       
   218 
       
   219 		echo "<div class='tablenav'>";
       
   220 		echo "<div class='tablenav-pages'>&nbsp;</div>";
       
   221 		echo '<div style="float: left">
       
   222 			<form id="p2c_del" name="p2c_del" method="post"/>
       
   223 			<input type="hidden" name="page" value="category-pages" />
       
   224 			<input type="hidden" name="view" value="'.$_REQUEST['view'].'" />
       
   225 			<input type="hidden" name="action" value="p2c_del" />
       
   226 			<input type="submit" value="Delete Checked" name="deleteit" class="button-secondary delete" />
       
   227 			</div>';
       
   228 		echo "<br style='clear:both;' />";
       
   229 		echo "</div>";
       
   230 		
       
   231 		echo "<br style='clear:both;' />";
       
   232 		echo '
       
   233 			';
       
   234 		echo "<table class='widefat'>";
       
   235 			echo "<thead>";
       
   236 			echo "<tr>
       
   237 				<th>";
       
   238 			if($wp_db_version >= 7098): echo "<input type='checkbox' onclick='checkAll(document.getElementById(\"p2c_del\"));' />" ;
       
   239 				else: echo "&nbsp;"; endif; 
       
   240 			echo "</th>
       
   241 			<th>Host <strong>Category</strong></th>
       
   242 			<th>Header <strong>Page</strong></th>
       
   243 			<th>&nbsp;</th>
       
   244 			</tr>";
       
   245 			echo "</thead><tbody>";
       
   246 		foreach($myopts as $opt):
       
   247 			$catid = $opt->cat_ID;
       
   248 			$pageid = $opt->page_ID;
       
   249 			$catname = get_cat_name($catid);
       
   250 			$usedcats[] = $catid;
       
   251 			$usedpages[] = $pageid;
       
   252 			$page = get_page($pageid); 
       
   253 			$title = $page->post_title;
       
   254 
       
   255 		echo "<tr>
       
   256 			<td><input type='checkbox' value='".$opt->rel_ID."' name='p2c_reldel[]' class='p2cbox' /></td>
       
   257 			<td><strong><a href='../?cat=".$catid."' title='View this Category Page in your website.'>".$catname."</a></strong>
       
   258 				<!-- (cat #".$catid.") --></td>
       
   259 			<td><strong><a href='page.php?action=edit&post=".$pageid."' title='Edit this page.'>".$title." 
       
   260 				<!-- (page #".$pageid.") --></td>
       
   261 			<td><a href='?page=category-pages&amp;p2cdel=".$opt->rel_ID."' class='delete' title='Delete this connection permanently.'>Delete</a></td>
       
   262 			</tr>";
       
   263 		endforeach;
       
   264 
       
   265 		echo "</tbody>";
       
   266 		echo "</table>";
       
   267 
       
   268 		echo "<div class='tablenav'>";
       
   269 		echo "<div class='tablenav-pages'>&nbsp;</div>";
       
   270 		echo '<div style="float: left">
       
   271 			<form name="p2c_del" method="post"/>
       
   272 			<input type="hidden" name="page" value="category-pages" />
       
   273 			<input type="hidden" name="view" value="'.$_REQUEST['view'].'" />
       
   274 			<input type="hidden" name="action" value="p2c_del" />
       
   275 			<input type="submit" value="Delete Checked" name="deleteit" class="button-secondary delete" />
       
   276 			</form>
       
   277 			</div>';
       
   278 		echo "<br style='clear:both;' />";
       
   279 		echo "</div>";
       
   280 		
       
   281 		else:
       
   282 
       
   283 		echo "<p>Sorry, no Category &raquo; Page connection available (yet). Maybe you want to <a href='edit.php?page=category-pages&view=new'>create some new connections</a>?</p>";
       
   284 		endif;
       
   285 
       
   286 
       
   287 else:
       
   288 // this is the 'new' page
       
   289 		
       
   290 		$usedcats = $wpdb->get_col("SELECT cat_ID FROM {$wpdb->page2cat};");
       
   291 #		print_r($usedcats);
       
   292 		$opt3 = get_option('p2c_use_empty');
       
   293 		if($opt3 == "true"): $empty = false; $excats = ""; elseif($opt3 == "false"): $empty = true; $excats = implode(',',$usedcats); endif;
       
   294 
       
   295 		$catdef = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC',
       
   296 		'hide_empty' => $empty, 'include_last_update_time' => false, 'hierarchical' => true, 
       
   297 		'exclude' => $excats, 'include' => '', 'number' => '', 'pad_counts' => true);
       
   298 		$others = get_categories($catdef);
       
   299 
       
   300 		$used = count($usedcats);
       
   301 		$count = count($others);
       
   302 
       
   303 		if( $count == 0):
       
   304 		echo "<p>Sorry, you don't have any usable category (or there are some, but empty; <a href='options-general.php?page=category-page-options'>check your settings</a>).</p>";
       
   305 		else:
       
   306 #		echo "<h3>New connections</h3>";
       
   307 #		echo "<div><p>These categories aren't connected with any page. Here you can setup a connection with an existent page, or create a brand new one.</p>";
       
   308 
       
   309 		echo "<form id='page2cat_manage' name='page2cat_manage' method='post'>";
       
   310 		$pages_already_used = implode(',',$usedpages);
       
   311 		if(get_option('p2c_show_used_pages')=='true') $exclude = $pages_already_used; else $exclude ="";
       
   312 
       
   313 		// wp_dropdown_pages settings (for both boxes in this page)
       
   314 		$pre_ddprefs = array('depth' => 0, 'child_of' => 0, 'selected' => 0, 
       
   315 			'echo' => 0,'name' => 'multipageid[]', 'exclude' => $exclude, 
       
   316 			'show_option_none' => '(none)');
       
   317 
       
   318 
       
   319 /*		
       
   320 		echo "<div class='tablenav'>";
       
   321 		echo "<div class='tablenav-pages'>
       
   322 			<input type='submit' name='p2c_update' value='Update Category Pages' class='button'/>
       
   323 			<br style='clear:both;' />
       
   324 			</div>";
       
   325 		echo '<div style="float:left;">
       
   326 			<input type="hidden" name="page" value="category-pages" />
       
   327 			<input type="hidden" name="view" value="'.$_REQUEST['view'].'" />
       
   328 			<input type="hidden" name="action" value="p2c_multilink" />
       
   329 			Link checked to: 
       
   330 			'.wp_dropdown_pages($pre_ddprefs).'
       
   331 			<input type="submit" value="Link them!" name="p2c_linkto" class="button-secondary" />
       
   332 			</div>';
       
   333 		echo "<br style='clear:both;'/>&nbsp;<br style='clear:both;' />";
       
   334 		echo "</div>";
       
   335 */
       
   336 			echo "<table class='widefat'>";
       
   337 			echo "<thead>";
       
   338 			echo "<tr>
       
   339 			<th>";
       
   340 			if($wp_db_version >= 7098): echo "<input type='checkbox' onclick='checkAll(document.getElementById(\"page2cat_manage\"));' />"; 
       
   341 				else: echo "&nbsp;"; endif; 
       
   342 			echo "</th>";
       
   343 			echo "<th>Host Category</th>
       
   344 			<th>Choose Page</th>
       
   345 			<th>Create New Page</th>
       
   346 			</tr>";
       
   347 			echo "</thead><tbody>";
       
   348 
       
   349 		
       
   350 			foreach($others as $otro):
       
   351 			switch($wp_db_version):
       
   352 			// "7098"=>"2.5",	"6124"=>"2.3.3", "5183"=>"2.2.1", "4773"=>"2.1.3"
       
   353 				case ( $wp_db_version < 5183 ):	// WP 2.1.3 and smaller
       
   354 				$versioned_catid = $otro->cat_ID;
       
   355 				$versioned_catname = $otro->cat_name;
       
   356 				$versioned_slug = $otro->category_nicename;
       
   357 				$versioned_count = $otro->category_count + $otro->posts_private;
       
   358 				break;
       
   359 				case ( $wp_db_version >= 5183 ):	// WP 2.2.1 and greater
       
   360 				default:
       
   361 				$versioned_catid = $otro->term_id;
       
   362 				$versioned_catname = $otro->name;
       
   363 				$versioned_slug = $otro->category_nicename;
       
   364 				$versioned_count = $otro->count;
       
   365 				break;
       
   366 			endswitch;
       
   367 
       
   368 			// wp_dropdown_pages settings
       
   369 			$ddprefs = array('depth' => 0, 'child_of' => 0, 'selected' => 0, 
       
   370 				'echo' => 0,'name' => 'pageid['.$versioned_catid.']', 
       
   371 				'exclude' => $exclude, 'show_option_none' => '(none)');
       
   372 
       
   373 			$output = "<tr>
       
   374 			<td><input type='checkbox' value='".$versioned_catid."' name='p2c_reladd[]' /></td>
       
   375 			<td>".$versioned_catname." <!-- (".$versioned_count.") --></td>
       
   376 			<td>".wp_dropdown_pages($ddprefs)."</td>";
       
   377 			$output .= "<td><a href='page-new.php?p2c=".$versioned_catid."'>Create New</a></td>
       
   378 			</tr>";
       
   379 			
       
   380 			$stringhe[$versioned_catname] = $output;
       
   381 			endforeach;
       
   382 			
       
   383 			asort($stringhe);
       
   384 			reset($stringhe);
       
   385 			
       
   386 			foreach($stringhe as $stringa):
       
   387 				echo $stringa;
       
   388 			endforeach;
       
   389 
       
   390 			echo "</tbody>";
       
   391 			echo "</table>";
       
   392 
       
   393 			echo "<div class='tablenav'>";
       
   394 			echo "<div style='float:right;'>
       
   395 				<input type='submit' name='p2c_update' value='Update Category Pages' class='button'/>
       
   396 				</div>";
       
   397 			echo '<div style="float:left;">
       
   398 				<input type="hidden" name="page" value="category-pages" />
       
   399 				<input type="hidden" name="view" value="'.$_REQUEST['view'].'" />
       
   400 				<input type="hidden" name="action" value="p2c_multilink" />
       
   401 				Link checked to: 
       
   402 				'.wp_dropdown_pages($pre_ddprefs).'
       
   403 				<input type="submit" value="Link them!" name="p2c_linkto" class="button-secondary" />
       
   404 				</div>';
       
   405 			echo "<br style='clear:both;'/>";
       
   406 			echo "</div>";
       
   407 endif;
       
   408 
       
   409 		echo "</form>";
       
   410 		endif;
       
   411 		echo PIXLINE_FOOTER;
       
   412 		echo "</div>";
       
   413 	endif;
       
   414 }
       
   415 
       
   416 // adds styles in admin head (WP 2.5+)
       
   417 function page2cat_metabox_styles(){
       
   418 ?>
       
   419 <style type="text/css" media="screen">
       
   420 #p2c-scroller{
       
   421 	width:100%;
       
   422 }
       
   423 
       
   424 ul#p2c-bind li, ul#p2c-free li{
       
   425 	list-style-type:none;
       
   426 	width:32%;
       
   427 	float:left;
       
   428 	font-size:80%;
       
   429 }
       
   430 
       
   431 .p2c-cleaner{
       
   432 	clear:both;
       
   433 	height:1px;
       
   434 }
       
   435 .p2c-used{
       
   436 	color:#D66;
       
   437 	text-decoration:line-through;
       
   438 }
       
   439 .p2c-catlink{
       
   440 	color:#000;
       
   441 	text-decoration:none;
       
   442 	border-bottom:1px dotted #CCC;
       
   443 }
       
   444 </style>
       
   445 <?php
       
   446 wp_enqueue_script('admin-forms');
       
   447 }
       
   448 
       
   449 // adds styles in admin head (WP < 2.5)
       
   450 function page2cat_sidebox_styles(){
       
   451 ?>
       
   452 <style type="text/css" media="screen">
       
   453 #p2c-scroller{
       
   454 	height:120px;
       
   455 	overflow:auto;
       
   456 	width:90%;
       
   457 	margin:0% 5%;
       
   458 }
       
   459 
       
   460 #p2c-box p{
       
   461 	margin:0px;
       
   462 	padding:5px;
       
   463 	font-size:80%;
       
   464 }
       
   465 
       
   466 ul#p2c-bind, ul#p2c-free,
       
   467 ul#p2c-bind li, ul#p2c-free li{
       
   468 	margin:0px;
       
   469 	padding:0px;
       
   470 	list-style-type:none;
       
   471 }
       
   472 
       
   473 ul#p2c-bind{
       
   474 	font-size:90%;
       
   475 	margin:1% 0%;
       
   476 }
       
   477 
       
   478 
       
   479 ul#p2c-free{
       
   480 	font-size:80%;
       
   481 	color:#333;
       
   482 	margin:2%;
       
   483 }
       
   484 
       
   485 ul#p2c-free li{
       
   486 	padding:2px 1px;
       
   487 }
       
   488 
       
   489 .p2c-count{
       
   490 	color:#AAA;
       
   491 }
       
   492 
       
   493 .p2c-indent{
       
   494 	padding-left:10px;
       
   495 }
       
   496 
       
   497 .p2c-used{
       
   498 	color:#D66;
       
   499 	text-decoration:line-through;
       
   500 }
       
   501 
       
   502 .p2c-catlink{
       
   503 	color:#000;
       
   504 	text-decoration:none;
       
   505 	border-bottom:1px dotted #CCC;
       
   506 }
       
   507 
       
   508 </style>
       
   509 <?php 
       
   510 }
       
   511 
       
   512 // adds sidebox in page editing (until WP 2.3x)
       
   513 function page2cat_add_sidebox($post_ID){
       
   514 ?>
       
   515 <fieldset id="p2c-select" class="dbx-box">
       
   516 <h3 class="dbx-handle">Category Pages</h3>
       
   517 <div class="dbx-content" id="p2c-box">
       
   518 	<?php page2cat_add_meta_box($post_ID);	?>
       
   519 </div>
       
   520 </fieldset>
       
   521 <?php
       
   522 
       
   523 
       
   524 }
       
   525 
       
   526 function page2cat_add_meta_box($post_ID){
       
   527 	global $wpdb, $post_ID, $_REQUEST;
       
   528 #	if(isset($_REQUEST['p2c'])):
       
   529 #	endif;
       
   530 	
       
   531 	$mypage = $wpdb->get_results("SELECT * FROM {$wpdb->page2cat} WHERE page_ID = '".$post_ID."';",OBJECT);
       
   532 	?>	
       
   533 		<p>Use this page as <a href='edit.php?page=category-pages'>Category Page</a> for these categories.</p>
       
   534 	<?php
       
   535 	$usedcats = array();
       
   536 	if(count($mypage)>0):
       
   537 		echo "<ul id='p2c-bind'>";
       
   538 		foreach($mypage as $connection):
       
   539 	#		print_r($connection);
       
   540 			echo "<li><input type='checkbox' checked='checked' value='".$connection->cat_ID."' name='p2c_bind[]' 
       
   541 			id='p2c".$connection->cat_ID."' /> ".get_cat_name($connection->cat_ID)."</li>
       
   542 			";
       
   543 			$usedcats[] = $connection->cat_ID;
       
   544 		endforeach;
       
   545 		echo "</ul>";
       
   546 	endif;
       
   547 
       
   548 		$opt3 = get_option('p2c_use_empty');
       
   549 		if($opt3 == "true"): $empty = false; $exclude = ""; 
       
   550 		elseif($opt3 == "false"): $empty = true; $exclude = implode(',',$usedcats);
       
   551 		endif;
       
   552 
       
   553 	$catdef = array('type' => 'post', 'child_of' => 0, 'orderby' => 'name', 'order' => 'ASC', 'exclude' => $exclude, 
       
   554 					'hide_empty' => $empty, 'include_last_update_time' => false, 'hierarchical' => true, 'pad_counts' => true);
       
   555 
       
   556 	$others = get_categories($catdef);
       
   557 	echo "<div id='p2c-scroller'>";
       
   558 	echo "<ul id='p2c-free'>";
       
   559 	foreach($others as $cat):
       
   560 	#print_r($cat); exit();
       
   561 	if($cat->cat_ID == $_REQUEST['p2c']): $flag = "checked='checked'"; else: $flag = ""; endif;
       
   562 
       
   563 	$has_page = $wpdb->get_var("SELECT count(*) FROM {$wpdb->page2cat} WHERE cat_ID = '".$cat->cat_ID."'");
       
   564 	if($has_page == 0):
       
   565 	echo "<li><input type='checkbox' name='p2c_bind[]' id='p2c".$cat->cat_ID."' value='".$cat->cat_ID."' ".$flag."/> "
       
   566 	."<a class='p2c-catlink' href='../?cat=".$cat->cat_ID."' title='".get_cat_name($cat->cat_ID)." (".$cat->category_count." items inside).'>".get_cat_name($cat->cat_ID)."</a></li>
       
   567 	";
       
   568 	else:
       
   569 #	echo "<li><input type='checkbox' disabled='disabled' name='p2c_bind[]' id='p2c".$cat->cat_ID."' value='".$cat->cat_ID."'/> <span class='p2c-used'>"
       
   570 #	.get_cat_name($cat->cat_ID)." </span><span class='p2c-count'>(".$cat->category_count." items)</span></li>
       
   571 #	";
       
   572 	$nothing = 0;
       
   573 	endif;
       
   574 	endforeach;
       
   575 	echo "</ul>";
       
   576 	echo "<br style='clear:both;' />";
       
   577 	echo "</div>";
       
   578 	?>
       
   579 	<?php
       
   580 }
       
   581 
       
   582 // trigger page save/edit and make db relations
       
   583 function page2cat_trigger_save($post_ID){
       
   584 global $wpdb;
       
   585 
       
   586 $values = $_POST['p2c_bind'];
       
   587 $via = "DELETE FROM {$wpdb->page2cat} WHERE page_ID = '".$_POST['post_ID']."'";
       
   588 $result = $wpdb->get_results($via);
       
   589 if(count($values) > 0):
       
   590 foreach($values as $key=>$cat){
       
   591 	if($_POST['post_ID'] != ""):	
       
   592 	$sql = "INSERT INTO {$wpdb->page2cat} (rel_ID, page_ID, cat_ID) VALUES ('','".$_POST['post_ID']."','".$cat."')";
       
   593 	$result = $wpdb->get_results($sql);
       
   594 	endif;
       
   595 }
       
   596 endif;
       
   597 }
       
   598 
       
   599 // trigger page deletion and free db relations
       
   600 function page2cat_trigger_delete(){
       
   601 	// yes, there's no trigger, actually....
       
   602 }
       
   603 
       
   604 // filter the content of a page, check for tag and replace it with a list of posts in the requested category.
       
   605 // function heavily inspired from Alex Rabe NextGen Gallery's nggfunctions.php. 
       
   606 function page2cat_content_catlist($content){
       
   607 global $post;
       
   608 	if ( stristr( $content, '[catlist' )) {
       
   609 		$search = "@(?:<p>)*\s*\[catlist\s*=\s*(\w+|^\+)\]\s*(?:</p>)*@i";
       
   610 		if	(preg_match_all($search, $content, $matches)) {
       
   611 			if (is_array($matches)) {
       
   612 				$title = get_option('p2c_catlist_title');
       
   613 				if($title != "") $output = "<h4>".$title."</h4>"; else $output = "";
       
   614 				$output .= "<ul class='p2c_catlist'>";
       
   615 				$limit = get_option('p2c_catlist_limit');
       
   616 				foreach ($matches[1] as $key =>$v0) {
       
   617 					$catposts = get_posts('category='.$v0."&numberposts=".$limit);
       
   618 						foreach($catposts as $single):
       
   619 						$output .= "<li><a href='".get_permalink($single->ID)."'>".$single->post_title."</a></li>";
       
   620 						endforeach;
       
   621 					$search = $matches[0][$key];
       
   622 					$replace= $output;
       
   623 					$content= str_replace ($search, $replace, $content);					
       
   624 				}
       
   625 			$output .= "</ul>";
       
   626 			}
       
   627 		}
       
   628 	}
       
   629 return $content;
       
   630 }
       
   631 
       
   632 // add real option page
       
   633 function page2cat_options_page(){
       
   634 global $wpdb, $styles;
       
   635 
       
   636 $p2c_defaults = array("show_usedpages_yes"=>"","show_usedpages_no"=>"","use_empty_no"=>"", "use_empty_yes"=>"", "catlist_limit"=>"", "catlist_title"=>"");
       
   637 
       
   638 if(isset($_POST['page2cat_action'])):
       
   639 #print_r($_POST); #die();
       
   640 	$sane1 = strip_tags(htmlentities($_POST['p2c_catlist_limit']));
       
   641 	update_option('p2c_catlist_limit',$sane1);
       
   642 	$sane4 = strip_tags(htmlentities($_POST['p2c_catlist_title']));
       
   643 	update_option('p2c_catlist_title',$sane4);
       
   644 	$sane2 = strip_tags(htmlentities($_POST['p2c_show_used_pages']));
       
   645 	update_option('p2c_show_used_pages',$sane2);
       
   646 	$sane3 = strip_tags(htmlentities($_POST['p2c_use_empty']));
       
   647 	update_option('p2c_use_empty',$sane3);
       
   648 	echo('<div id="message" class="updated fade"><p><strong>Settings saved.</strong></p></div>');
       
   649 endif;
       
   650 
       
   651 $opt3 = get_option('p2c_use_empty');
       
   652 if($opt3 == "false"){
       
   653 	$p2c_defaults['use_empty_no'] = ' selected="selected"';
       
   654 }elseif($opt3 == "true"){
       
   655 	$p2c_defaults['use_empty_yes'] = ' selected="selected"';
       
   656 }
       
   657 
       
   658 $opt2 = get_option('p2c_show_used_pages');
       
   659 if($opt2 == "false"){
       
   660 	$p2c_defaults['show_usedpages_no'] = ' selected="selected"';
       
   661 }elseif($opt2 == "true"){
       
   662 	$p2c_defaults['show_usedpages_yes'] = ' selected="selected"';
       
   663 }
       
   664 
       
   665 $p2c_catlist_title = get_option('p2c_catlist_title');
       
   666 
       
   667 
       
   668 	echo "<div class='wrap'>";
       
   669 	echo "<h2>Category Page settings</h2>";
       
   670 	echo "<form method='post' name='page2cat_options' accept-charset='utf-8'>";
       
   671 
       
   672 	echo '
       
   673 	<h3>Management settings</h3>
       
   674 	<p>These settings let you customize your <a href="edit.php?page=category-pages">Category Page management panel</a>.</p>
       
   675 	<table class="form-table">
       
   676 	<tr valign="top">
       
   677 	<th scope="row">Show empty categories</th>
       
   678 	<td>
       
   679 	<select name="p2c_use_empty">
       
   680 	<option value="false"'.$p2c_defaults['use_empty_no'].' label=" No, thanks. "> No, thanks. </option>
       
   681 	<option value="true"'.$p2c_defaults['use_empty_yes'].' label=" Yes, please! "> Yes, please! </option>
       
   682 	</select> <small>(Choose whether empty categories are shown or not. )</small>
       
   683 	</td>
       
   684 	</tr>
       
   685 
       
   686 	<tr valign="top">
       
   687 	<th scope="row">Allow single page sharing</th>
       
   688 	<td>
       
   689 	<select name="p2c_show_used_pages">
       
   690 	<option value="false"'.$p2c_defaults['show_usedpages_no'].' label=" No, thanks. "> No, thanks. </option>
       
   691 	<option value="true"'.$p2c_defaults['show_usedpages_yes'].' label=" Yes, please! "> Yes, please! </option>
       
   692 	</select> <small>(You can share a single page with more than a category, if you like.)</small>
       
   693 	</td>
       
   694 	</tr>
       
   695 
       
   696 	</table>
       
   697 
       
   698 	<h3><code>[catlist]</code> tag settings</h3>
       
   699 	<p>You can use a <em>[catlist=xx]</em> tag in a post/page to show a list of post from a certain category, replacing <em>xx</em> with the category ID.</p>
       
   700 	<table class="form-table">
       
   701 	<tr valign="top">
       
   702 	<th scope="row">Display at most </th>
       
   703 	<td>
       
   704 	<select name="p2c_catlist_limit">';
       
   705 
       
   706 	$limits = array(1,2,3,4,5,6,7,8,9,10,15,20,25);
       
   707 	foreach($limits as $limit):
       
   708 		if(get_option('p2c_catlist_limit') == $limit) $flag = "selected='selected'"; else $flag = "";
       
   709 		echo "<option value='".$limit."' name='opt".$limit."' ".$flag.">".$limit."</option>";
       
   710 	endforeach;
       
   711 
       
   712 	echo '</select> posts.
       
   713 	</td>
       
   714 	</tr>
       
   715 
       
   716 	<tr valign="top">
       
   717 	<th scope="row">Display list title</th>
       
   718 	<td><input type="text" name="p2c_catlist_title" value="'.$p2c_catlist_title.'" size="30"/>
       
   719 	<small>(Leave empty to show a post list without a title.)</small>
       
   720 	</td>
       
   721 	</tr>
       
   722 
       
   723 	</table>
       
   724 	';	
       
   725 
       
   726 	echo '<p class="submit">
       
   727 	<input type="hidden" name="page2cat_action" value="update" />
       
   728 	<input type="submit" name="update" value="Update Settings" />
       
   729 	</p>
       
   730 	</fieldset>';
       
   731 	
       
   732 	echo '</form>';
       
   733 	echo PIXLINE_FOOTER;
       
   734 	echo "</div>";
       
   735 }
       
   736 
       
   737 // template function for manual hacks and widgets :-)
       
   738 function page2cat_output($cat, $style = 'getpost', $useimg = 1){
       
   739 global $wpdb;
       
   740 
       
   741 	$useimg = get_option('p2c_use_img');
       
   742 	$mypage = $wpdb->get_row("SELECT * FROM {$wpdb->page2cat} WHERE cat_ID = '".$cat."';",OBJECT);
       
   743 #	print_r($mypage);
       
   744 	$pageid = $mypage->page_ID;
       
   745 	if($pageid != ""):
       
   746 	
       
   747 	switch($style):
       
   748 	
       
   749 	case 'getpost':
       
   750 	?>
       
   751 	<div id="category-page-header">
       
   752 	<?php
       
   753 	$post = $post_temp;
       
   754 	$post = get_post($pageid);
       
   755 	setup_postdata($post);
       
   756 	
       
   757 	if($post->post_title!=""){
       
   758 				?>
       
   759 				<div id="p2c-header">
       
   760 					<h2><?php echo $post->post_title; ?></h2>
       
   761 					<p><?php the_content(); ?></p>
       
   762 				</div> 
       
   763 		<div class="category-page-cleaner"></div>
       
   764 	</div>
       
   765 	<?php
       
   766 	$post = $post_temp;
       
   767 	}
       
   768 	break;
       
   769 	
       
   770 	case 'widget':
       
   771 	?>
       
   772 	<div id="category-widget-header">
       
   773 	<?php
       
   774 	$pagina = "SELECT * FROM {$wpdb->posts} WHERE ID='".$pageid."' AND post_type = 'page';";
       
   775 	$mine = $wpdb->get_results($pagina);
       
   776 	if($mine[0]->post_title!=""){
       
   777 				?>
       
   778 				<div id="p2c-header">
       
   779 					<!-- h2><?php echo $mine[0]->post_title; ?></h2 -->
       
   780 					<p><?php echo wptexturize($mine[0]->post_content,1); ?></p>
       
   781 				</div> 
       
   782 		<div class="category-widget-cleaner"></div>
       
   783 	</div>
       
   784 	<?php
       
   785 	}
       
   786 
       
   787 	break;
       
   788 	case 'inline':
       
   789 	default:	
       
   790 	?>
       
   791 	<div id="category-page-header">
       
   792 	<?php
       
   793 	$pagina = "SELECT * FROM {$wpdb->posts} WHERE ID='".$pageid."' AND post_type = 'page';";
       
   794 	$mine = $wpdb->get_results($pagina);
       
   795 	if($mine[0]->post_title!=""){
       
   796 				?>
       
   797 				<div id="p2c-header">
       
   798 					<h2><?php echo $mine[0]->post_title; ?></h2>
       
   799 					<p><?php echo wptexturize($mine[0]->post_content,1); ?></p>
       
   800 				</div> 
       
   801 		<div class="category-page-cleaner"></div>
       
   802 	</div>
       
   803 	<?php
       
   804 	}
       
   805 	break;
       
   806 	
       
   807 	
       
   808 	endswitch;
       
   809 	
       
   810 	?> 
       
   811 	<?php
       
   812 	else:
       
   813 	echo '<h2 class="pagetitle">'.single_cat_title('',false).'</h2>';	
       
   814 	endif;
       
   815 }
       
   816 
       
   817 
       
   818 // adds sidebox in page write/edit
       
   819 function page2cat_init(){
       
   820 	if (function_exists('add_meta_box')) {
       
   821 		add_meta_box('page2cat','Category Page Options', 'page2cat_add_meta_box', 'page');
       
   822 		add_action('admin_head','page2cat_metabox_styles');
       
   823 	} else {
       
   824 		add_action('dbx_page_sidebar', 'page2cat_add_sidebox');
       
   825 		add_action('admin_head','page2cat_sidebox_styles');
       
   826 	}	
       
   827 }
       
   828 // install management and options page
       
   829 function page2cat_config_page() {
       
   830 	if ( function_exists('add_submenu_page') && is_admin()):
       
   831 	add_submenu_page('edit.php', __('Category Pages'), __('Category Pages'), 8, 'category-pages', 'page2cat_manage_page');
       
   832 	endif;
       
   833 	if( function_exists('add_options_page') && is_admin()):
       
   834 	add_options_page('Category Pages options','Category Pages',8,'category-page-options','page2cat_options_page');
       
   835 	endif;
       
   836 }
       
   837 
       
   838 
       
   839 if( is_admin() ):
       
   840 	if( preg_match('|page-new.php|i', $_SERVER['REQUEST_URI']) || 
       
   841 		preg_match('|page.php|i', $_SERVER['REQUEST_URI']) || 
       
   842 		preg_match('|edit.php|i', $_SERVER['REQUEST_URI']) ){
       
   843 			add_action('admin_menu', 'page2cat_init');
       
   844 	}
       
   845 	add_action('admin_menu', 'page2cat_config_page');
       
   846 	add_action('save_post','page2cat_trigger_save');
       
   847 endif;
       
   848 
       
   849 add_filter('the_content','page2cat_content_catlist');	// by popular demand :-)
       
   850 ?>