=')) {
$pTC_table = $wpdb->prefix . "pTC_logs";
add_option("pTC_log_db_version", "0.1");
//Create logging table
if($wpdb->get_var("show tables like '$pTC_table'") != $pTC_table) {
$sql = "CREATE TABLE " . $pTC_table . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime NOT NULL,
userid bigint(20) NOT NULL,
message text NOT NULL,
priority smallint(1) NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
//Begin logging
logMe("Begin Installation");
$role = get_role('administrator');
$role->add_cap('pTypeConverter');
logMe("Installed Admin Role", 2);
$role2 = get_role('editor');
$role2->add_cap('pTypeConverter');
logMe("Installed Editor Role", 2);
//Finished
logMe("Finished Installation");
} else if ( version_compare(get_bloginfo('version'), '3.0', '>=')) {
wp_die('pTypeConverter 0.2 is not compatible with this version of Wordpress. Please either upgrade to Wordpress 3.2+, or use the previous version of pTypeConverter.');
} else {
wp_die('pTypeConverter is not compatible with this version of Wordpress. Please either upgrade to Wordpress 3.2+, or use my previous plugin p2pConverter.');
}
}
//Removes p2p Capabilities from basic roles.
function pTC_uninstall() {
global $wpdb;
global $wp_roles;
$pTC_table = $wpdb->prefix . "pTC_logs";
logMe("Begin Uninstallation");
// get a list of values, containing pairs of: $role_name => $display_name
$pTC_roles = $wp_roles->get_names();
logMe("Roles available before pType Uninstall: \n" . print_r($pTC_roles, TRUE), 3);
foreach ($pTC_roles as $role) {
$the_role = explode("|", $role);
$the_role = get_role(strtolower($the_role[0]));
if ( empty($the_role) )
continue;
$the_role->remove_cap(pTypeConverter) ;
}
$pTC_roles = $wp_roles->get_names();
logMe("Roles now available (no pType): \n" . print_r($pTC_roles, TRUE), 3);
logMe("Almost finished uninstalling ... last step: drop this table!");
//Delete logging table!
$wpdb->query("DROP TABLE IF EXISTS $pTC_table");
}
//Logging capability
function logMe($text,$prio=1) {
global $wpdb;
global $current_user;
$pTC_table = $wpdb->prefix . "pTC_logs";
$userid = $current_user->ID;
$text = esc_attr($text);
$time = date('Y-m-d H:i:s ',time());
$sql="INSERT INTO " . $pTC_table . " (time,userid,message,priority)
VALUES (
'$time',
'$userid',
'$text',
'$prio'
)";
$wpdb->query($wpdb->prepare($sql)) or wp_die("Cant add pTC log to db!\n" . $sql);
unset($sql);
unset($userid);
unset($text);
unset($time);
unset($pTC_table);
unset($current_user);
unset($wpdb);
}
//Add menu item
if (stripos($_SERVER['REQUEST_URI'], '/wp-admin/') !== FALSE) {
add_action('admin_menu', 'pTC_menu');
}
function pTC_menu() {
global $current_user;
//Don't show link to users without permissions
if(current_user_can('pTypeConverter')) {
logMe("User " . $current_user->display_name . " can view pTypeConverter page successfully", 3);
$page = add_management_page('pTypeConverter', 'pTypeConverter', 'pTypeConverter', 'pTC', 'pTC_show_pTC');
add_action('admin_head-' . $page, 'pTC_header');
add_action('admin_print_scripts-' . $page, 'pTC_scripts');
add_action('admin_print_styles-' . $page, 'pTC_styles');
add_option('pTC_show_advanced_post_types', 'false');
add_option('pTC_show_logging', 'false');
} else {
logMe("User " . $current_user->display_name . " is unable to view pTypeConverter link in admin because they lack the capability.", 0);
}
}
//Functions
function pTC_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('jquery-ui-tabs');
wp_register_script('jquery-tablesorter', plugins_url('js/jquery.tablesorter.min.js', __FILE__), array('jquery'));
wp_enqueue_script('jquery-tablesorter');
wp_register_script('jquery-tablesorter-widgets', plugins_url('js/jquery.tablesorter.widgets.js', __FILE__), array('jquery', 'jquery-tablesorter'));
wp_enqueue_script('jquery-tablesorter-widgets');
wp_register_script('jquery-ui-datepicker', plugins_url('js/jquery.ui.datepicker.js', __FILE__), array('jquery', 'jquery-ui-core'));
wp_enqueue_script('jquery-ui-datepicker');
}
function pTC_styles() {
wp_register_style('jquery-ui-smoothness', plugins_url('/css/smoothness/jquery-ui-1.8.7.custom.css', __FILE__));
wp_enqueue_style('jquery-ui-smoothness');
}
function pTC_ajax() {
global $wpdb;
global $current_user;
//Ensure Nonce Protection
check_ajax_referer('pTC-ajax-check', 'security');
//Don't allow bad users
if(!current_user_can('pTypeConverter')) {
logMe("User " . $current_user->display_name . " attempted to execute the pTC_ajax, but was unable to do so because they lack the capability.", 0);
wp_die( __('You do not have sufficient permissions to access this page.') );
}
// Return logging
if ($_POST['method'] == 'logging') {
if($_POST['data']) {
$pTC_logging_level = $_POST['data'];
$pTC_logs_table = $wpdb->prefix . "pTC_logs";
$pTC_logs_query = "SELECT p.time, p.priority, u.user_login, p.message FROM " . $pTC_logs_table . " AS p, " . $wpdb->users . " AS u WHERE p.userid = u.ID AND p.priority <= " . $pTC_logging_level;
logMe("Query: " . $pTC_logs_query, 2);
$pTC_logs = $wpdb->get_results($pTC_logs_query, ARRAY_A);
$pTC_logs = json_encode($pTC_logs);
echo $pTC_logs;
}
// Clear Logging Table
} else if ($_POST['method'] == 'clearlogs') {
$pTC_logs_table = $wpdb->prefix . "pTC_logs";
$pTC_drop_logging_query = "TRUNCATE TABLE " . $pTC_logs_table;
$wpdb->query($pTC_drop_logging_query);
echo json_encode(array('message' => 'Logging cleared! Now go generate some new logs!'));
// Return post types
} else if ($_POST['method'] == 'showtypes') {
if (get_option('pTC_show_advanced_post_types') == "false") {
$pTC_types = get_post_types(array('public' => true));
} else {
$pTC_types = get_post_types();
}
foreach($pTC_types as $pTC_type) {
$pTC_type_array[] = array('ID' => $pTC_type, 'value' => $pTC_type);
}
logMe("Possible PostTypes: \n" . print_r($pTC_type_array, TRUE), 3);
echo json_encode($pTC_type_array);
// Return authors
} else if ($_POST['method'] == 'showauthors') {
$pTC_users_args = array( 'who' => 'authors', 'orderby' => 'nicename', 'fields' => array ( 'ID', 'user_nicename' ) );
$pTC_users = get_users($pTC_users_args);
logMe("Possible Users: \n" . print_r($pTC_users, TRUE), 3);
foreach($pTC_users as $pTC_user) {
$pTC_user_array[] = array('ID' => $pTC_user->ID, 'value' => $pTC_user->user_nicename);
}
echo json_encode($pTC_user_array);
// Return post listings
} else if ($_POST['method'] == 'showposts') {
if ($_POST['data']) {
parse_str($_POST['data']);
$pTC_query_addition = "";
if($pTC_filter_title != "") {
$pTC_filter_title = esc_attr($pTC_filter_title);
$pTC_query_addition .= " AND p.post_title LIKE '%%" . $pTC_filter_title . "%%'";
}
if($pTC_filter_author != "") {
$pTC_query_addition .= " AND u.ID = '" . esc_attr($pTC_filter_author) . "'";
}
if ($pTC_filter_start_date != "") {
$pTC_filter_start_date = date_format(date_create(esc_attr($pTC_filter_start_date)), 'Y-m-d');
if ($pTC_filter_start_date){
$pTC_query_addition .= " AND p.post_date >= '" . $pTC_filter_start_date . "'";
}
}
if ($pTC_filter_end_date != "") {
$pTC_filter_end_date = date_format(date_create(esc_attr($pTC_filter_end_date)), 'Y-m-d');
if ($pTC_filter_end_date){
$pTC_query_addition .= " AND p.post_date <= '" . $pTC_filter_end_date . "'";
}
}
if ($pTC_filter_type != "") {
$pTC_query_addition .= " AND p.post_type = '" . esc_attr($pTC_filter_type) . "'";
} else {
if (get_option('pTC_show_advanced_post_types') == "false") {
$pTC_types = get_post_types(array('public' => true));
} else {
$pTC_types = get_post_types();
}
foreach($pTC_types as $pTC_type) {
$pTC_type_query .= " OR p.post_type = '" . $pTC_type . "'";
}
$pTC_query_addition .= " AND (p.post_type = 1 " . $pTC_type_query . ")";
}
if ($pTC_id != "") {
foreach($pTC_id as $id) {
$pTC_query_addition .= " OR p.id = '" . esc_attr($id) . "'";
}
}
} else {
$pTC_query_addition = " AND 1 = 1";
}
$pTC_posts_query = "SELECT p.id, p.post_title, u.user_nicename, p.post_date, p.post_type FROM " . $wpdb->posts . " AS p, " . $wpdb->users . " AS u WHERE p.post_author = u.ID" . $pTC_query_addition . " ORDER BY p.post_date ASC";
logMe("Query: " . esc_attr($pTC_posts_query), 2);
$pTC_posts = $wpdb->get_results($pTC_posts_query, ARRAY_A);
logMe("Post Dump: \n " . print_r($pTC_posts, TRUE), 3);
if ($pTC_posts) {
echo json_encode($pTC_posts);
} else {
echo json_encode(array('message' => 'No matching posts found!'));
}
// Run convert sequence
} else if ($_POST['method'] == 'convertposts') {
if(@$_POST['data']) {
global $wp_rewrite;
$pTC_ids = @$_POST['data'];
$pTC_type = attribute_escape(array_pop($pTC_ids));
logMe("Prepare for post and type dump: \n Convert to Post Type:" . $pTC_type . "\n Post IDs: " . print_r($pTC_ids, TRUE), 2);
$pTCquery = "UPDATE " . $wpdb->posts . " SET post_type = '" . $pTC_type . "' WHERE ";
$results = array();
if(is_array($pTC_ids) ) {
foreach ($pTC_ids as $pTC_id) {
$pTC_type_check = $wpdb->get_row("SELECT post_title,post_type FROM " . $wpdb->posts . " WHERE id=" . $pTC_id . "");
logMe("PostId: " . $pTC_id . "
Original PostType: " . print_r($pTC_type_check->post_type . "
Converting PostType: " . $pTC_type, TRUE));
if ($pTC_type_check->post_type != $pTC_type) {
$pTCqueryone = $pTCquery . "id=" . $pTC_id;
} else {
logMe("Conversion to " . $pTC_type . " failed because it is already a " . $pTC_type, 0);
array_push($results, array('pTC_id' => $pTC_id, 'result' => 'failed', 'message' => 'The selected item is already a ' . $pTC_type));
continue;
}
logMe("Query: " . $pTCqueryone, 2);
$queryresult = $wpdb->query($pTCqueryone);
$queryerror = $wpdb->print_error();
logMe("Errors: " . $queryerror, 0);
if ($queryresult) {
logMe("Conversion to " . $pTC_type . " succeeded", 0);
$pTC_type_check = $wpdb->get_row("SELECT post_title,post_type FROM " . $wpdb->posts . " WHERE id=" . $pTC_id . "");
logMe("PostId: " . $pTC_id . " Confirmed PostType after conversion: " . print_r($pTC_type_check->post_type, TRUE), 1);
array_push($results, array('pTC_id' => $pTC_id, 'result' => 'succeeded', 'pTC_type' => $pTC_type_check->post_type, 'message' => 'Success!'));
} else if ($queryerror) {
logMe("Conversion to " . $pTC_type . " did not suceed because of SQL Error: " . $queryerror, 0);
array_push($results, array('pTC_id' => $pTC_id, 'result' => 'failed', 'message' => 'SQL Error: ' . $queryerror));
} else {
logMe("Conversion to " . $pTC_type . " did not suceed because of unknown error.", 0);
array_push($results, array('pTC_id' => $pTC_id, 'result' => 'failed', 'message' => 'Unknown Error!'));
}
}
}
//Important! Rewrites permalinks for post/page files
$wp_rewrite->flush_rules();
echo json_encode($results);
}
} else if ($_POST['method'] == 'pTC_advanced_posts') {
if($_POST['data']) {
update_option('pTC_show_advanced_post_types', $_POST['data']);
$message = "Advanced Post Types option saved to " . get_option('pTC_show_advanced_post_types');
} else {
$message = "Unknown error trying to save Advanced Post Type option.";
}
echo json_encode(array('message' => $message));
} else if ($_POST['method'] == 'pTC_show_logging') {
if($_POST['data']) {
update_option('pTC_show_logging', $_POST['data']);
$message = "Show Logging option saved to " . $_POST['data'];
} else {
$message = "Unknown error trying to save Show Logging option.";
}
echo json_encode(array('message' => $message));
} else {
//Unknown, and bad request
logMe("Bad Request!", 0);
wp_die( __('Bad Request!') );
}
die(); //this is required to return a proper result
exit;
}
add_action('wp_ajax_pTC_ajax', 'pTC_ajax');
function pTC_header() {
$pTC_ajax_nonce = wp_create_nonce("pTC-ajax-check");
?>
Enable this setting if you would like to enabled advanced post types that normally wouldn't be shown by default (i.e. are considered non-public, like "revision", or "nav_menu_item" types).
Enable this setting to display the Logging tab. Useful for debugging purposes.
Show Logging Level: