Manage Templates page.
Version: 1.00.A Beta
Author: Binny V A
Author URI: http://www.binnyva.com/
*/
/**
* Add a new menu under Manage, visible for all users with template viewing level.
*/
add_action( 'admin_menu', 'templates_add_menu_links' );
function templates_add_menu_links() {
$view_level= 2;
add_submenu_page( 'edit.php', __('Manage Templates'),
__('Manage Templates'), $view_level,
'edit.php?page=article-templates/manage.php' );
}
/**
* Add a Drop down in the post create page that lists all the templates.
*/
add_action( 'edit_form_advanced', 'templates_post_page_templates_list' );
add_action( 'edit_page_form', 'templates_post_page_templates_list' );
function templates_post_page_templates_list() {
global $current_user;
$templates = templates_get_users_templates($current_user->ID);
$template_js_data = array();
?>
get_results("SELECT ID,post_title,post_name,post_date,post_type,post_content,post_author "
. " FROM {$wpdb->posts} WHERE post_type='template' AND post_author='$user_id' ORDER BY post_date DESC");
}
/**
* Get the default templates for the given user
*/
function templates_get_default_template_for_user($user_id) {
global $wpdb;
return $wpdb->get_var("SELECT post_name FROM {$wpdb->posts} "
. " WHERE post_type='template' AND post_author='$user_id' AND menu_order='1' LIMIT 1");
}
////////////////////////////////////////////////// Library Functions //////////////////////////////////
function template_js_escape($text) {
$safe_text = preg_replace("/\r?\n/", '\n', addslashes($text));
return $safe_text;
}
/**
* Array2json library function - is not needed in PHP 5.2+
* http://www.bin-co.com/php/scripts/array2json/
*/
function templates_array2json($arr) {
$parts = array();
$is_list = false;
//Find out if the given array is a numerical array
$keys = array_keys($arr);
$max_length = count($arr)-1;
if(($keys[0] === 0) and ($keys[$max_length] == $max_length)) {//See if the first key is 0 and last key is length - 1
$is_list = true;
for($i=0; $i$value) {
if(is_array($value)) { //Custom handling for arrays
if($is_list) $parts[] = templates_array2json($value); /* :RECURSION: */
else $parts[] = '"' . $key . '":' . templates_array2json($value); /* :RECURSION: */
} else {
$str = '';
if(!$is_list) $str = '"' . $key . '":';
//Custom handling for multiple data types
if(is_numeric($value)) $str .= $value; //Numbers
elseif($value === false) $str .= 'false'; //The booleans
elseif($value === true) $str .= 'true';
else $str .= '"' . ($value) . '"'; //All other things
// :TODO: Is there any more datatype we should be in the lookout for? (Object?)
$parts[] = $str;
}
}
$json = implode(',',$parts);
if($is_list) return '[' . $json . ']';//Return numerical JSON
return '{' . $json . '}';//Return associative JSON
}