Manage Templates page. Version: 1.05.2 Author: Binny V A Author URI: http://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() { global $wp_version; $view_level= 2; $page = 'edit.php'; if($wp_version >= '2.7') $page = 'tools.php'; add_submenu_page($page, __('Manage Templates', 'article-templates'), __('Manage Templates', 'article-templates'), $view_level, 'article-templates/manage.php' ); $hookname = get_plugin_page_hookname('article-templates/template_form.php', '' ); $GLOBALS['_registered_pages'][$hookname] = true; } /** * 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(); ?>

Manage Templates section.', 'article-templates') ?>



posts} LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id WHERE post_type='template' AND post_author='$user_id' ORDER BY post_date DESC SQL; return $wpdb->get_results($query); } /** * Get the default templates for the given user */ function templates_get_default_template_for_user($user_id) { global $wpdb; $user_selected = $wpdb->get_var("SELECT post_name FROM {$wpdb->posts} WHERE post_type='template' AND post_author='$user_id' AND menu_order='1' LIMIT 1"); if( ! $user_selected ) { $query = <<posts} JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id WHERE post_type='template' AND meta_key='default_for_all' AND meta_value = 1 ORDER BY post_date DESC SQL; return $wpdb->get_var($query); } return $user_selected; } /// Register server side request handling function add_action('save_post', 'article_templates_handle_update', 10, 2); function article_templates_handle_update( $post_id, $post ) { if( ! empty($_REQUEST['available_for_all']) ) { add_post_meta($post_id, 'available_for_all', intval($_REQUEST['available_for_all']), true) or update_post_meta($post_id, 'available_for_all', intval($_REQUEST['available_for_all'])); } else { delete_post_meta($post_id, 'available_for_all'); } if( ! empty($_REQUEST['default_for_all']) ) { // avoid multiple default_for_all templates global $wpdb; $wpdb->get_results("DELETE FROM {$wpdb->postmeta} WHERE meta_key = \"default_for_all\""); add_post_meta($post_id, 'default_for_all', intval($_REQUEST['default_for_all']), true) or update_post_meta($post_id, 'default_for_all', intval($_REQUEST['default_for_all'])); // default_for_all implies available_for_all add_post_meta($post_id, 'available_for_all', 1, true) or update_post_meta($post_id, 'available_for_all', 1, true); } else { delete_post_meta($post_id, 'default_for_all'); } } ////////////////////////////////////////////////// Library Functions ////////////////////////////////// function template_js_escape($text) { $safe_text = preg_replace("/\r?\n/", '\n', addslashes($text)); $safe_text = preg_replace("/\//", '\\/', $safe_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 }