Skip to content

Instantly share code, notes, and snippets.

@ScottDeLuzio
Created October 13, 2017 23:16
Show Gist options
  • Save ScottDeLuzio/7ef75a9d172c10f31de566b0a24fc9b8 to your computer and use it in GitHub Desktop.
Save ScottDeLuzio/7ef75a9d172c10f31de566b0a24fc9b8 to your computer and use it in GitHub Desktop.
Create a task in WP-CRM System programatically
<?php
/*
Plugin Name: WP-CRM System Create Task
Plugin URI: https://www.wp-crm.com
Description: Create a task in WP-CRM System.
Version: 1.0
Author: Scott DeLuzio
Author URI: https://www.wp-crm.com
*/
function wpcrm_system_programmatically_create_task() {
// Setup the author, slug, title
$author_id = get_current_user_id();
$title = $your_title_value;
$slug = preg_replace("/[^A-Za-z0-9]/",'',strtolower($title));
//Validate Organizations
$allowed_orgs = get_posts(array('posts_per_page'=>-1,'post_type' => 'wpcrm-organization'));
$orgs = array('do not show');
foreach ($allowed_orgs as $org) {
$orgs[] = $org->ID;
}
$attach_org = in_array( $your_org_value, $orgs ) ? $your_org_value : '';
//Validate Contacts
$allowed_contacts = get_posts(array('posts_per_page'=>-1,'post_type' => 'wpcrm-contact'));
$contacts = array('do not show');
foreach ($allowed_contacts as $contact) {
$contacts[] = $contact->ID;
}
$attach_contact = in_array( $your_contact_value, $contacts ) ? $your_contact_value : '';
//Validate Projects
$allowed_projects = get_posts(array('posts_per_page'=>-1,'post_type' => 'wpcrm-project'));
$projects = array('do not show');
foreach ($allowed_projects as $project) {
$projects[] = $project->ID;
}
$attach_project = in_array( $your_project_value, $projects ) ? $your_project_value : '';
//Validate WP-CRM System valid users
$users = get_users();
$wp_crm_users = array();
foreach( $users as $user ){
if($user->has_cap(get_option('wpcrm_system_select_user_role'))){
$wp_crm_users[] = $user->data->user_login;
}
}
$attach_user = in_array($your_user_value, $wp_crm_users ) ? $your_user_value : '';
//Validate date fields
$start_date = strtotime($your_startdate_value);
$due_date = strtotime($your_duedate_value);
//Validate progress
$allowed_progress = array('zero',5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100);
$attach_progress = in_array( $your_progress_value, $allowed_progress ) ? $your_progress_value : 'zero';
//Validate priority
$allowed_priority = array('','low','medium','high');
$attach_priority = in_array( $your_priority_value, $allowed_priority ) ? $your_priority_value : '';
//Validate status
$allowed_status = array('not-started','in-progress','complete','on-hold');
$attach_status = in_array( $your_status_value, $allowed_status ) ? $your_status_value : 'not-started';
//Validate description
$attach_description = wp_kses_post( wpautop( $your_description_value ) );
// If the page doesn't already exist, then create it
if( null == get_page_by_title( $title, OBJECT, 'wpcrm-task' ) ) {
// Set the post ID so that we know the post was created successfully
$post_id = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => $author_id,
'post_name' => $slug,
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'wpcrm-task'
)
);
//Add information to task fields.
add_post_meta( $post_id, '_wpcrm_task-attach-to-organization', $attach_org, true );
add_post_meta( $post_id, '_wpcrm_task-attach-to-contact', $attach_contact, true );
add_post_meta( $post_id, '_wpcrm_task-attach-to-project', $attach_project, true );
add_post_meta( $post_id, '_wpcrm_task-assignment', $attach_user, true );
add_post_meta( $post_id, '_wpcrm_task-start-date', $start_date, true );
add_post_meta( $post_id, '_wpcrm_task-due-date', $due_date, true );
add_post_meta( $post_id, '_wpcrm_task-progress', $attach_progress, true );
add_post_meta( $post_id, '_wpcrm_task-priority', $attach_priority, true );
add_post_meta( $post_id, '_wpcrm_task-status', $attach_status, true );
add_post_meta( $post_id, '_wpcrm_task-description', $userPhone, true );
// Otherwise, we'll stop
}
} // end wpcrm_system_programmatically_create_task
add_action( 'some_action_hook', 'wpcrm_system_programmatically_create_task' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment