Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Template Name: Export
*
* @package Genesis CRM
* @author Bill Erickson <bill@billerickson.net>
* @copyright Copyright (c) 2011, Bill Erickson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*
*/
/**
* Export
*
*/
function be_export_content() {
// Initialize DB
require_once( 'meekrodb.2.2.class.php' ); // DB handler class
// -- update to use DB credentials for new CRM
$db = new MeekroDB( 'localhost', $db_user, $db_pass, $db_name );
$referral_sources = array(
'already-a-client' => 1,
'codepoet' => 2,
'diythemes' => 3,
'google' => 4,
'other-referral' => 5,
'studiopress' => 6,
'unknown' => 7,
'yoast' => 8,
);
$statuses = array(
'active-project' => 4,
'closed' => 1,
'complete' => 7,
'outstanding-quote' => 1,
'prospect' => 1,
'scheduled-project' => 2,
'share-with-sp' => 7,
);
// Query posts
$imported = 0;
$loop = new WP_Query( array(
'order' => 'ASC',
'posts_per_page' => 500,
'meta_query' => array(
array(
'key' => 'be_exported',
'value' => 1,
'compare' => 'NOT EXISTS',
)
)
) );
if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post();
$entry = array();
$entry['name'] = get_the_title();
$entry['email'] = get_post_meta( get_the_ID(), '_crm_client_email', true );
$entry['phone'] = get_post_meta( get_the_ID(), '_crm_client_phone', true );
$entry['url'] = get_post_meta( get_the_ID(), '_crm_client_url', true );
$entry['date'] = get_the_date( 'Y-m-d' );
$entry['project_name'] = get_post_meta( get_the_ID(), '_crm_project_name', true );
$source = array_shift( get_the_terms( get_the_ID(), 'sources' ) );
$entry['referral'] = $source ? $referral_sources[$source->slug] : '';
$entry['referral_other'] = get_post_meta( get_the_ID(), '_crm_other_referral', true );
$needs_work = get_post_meta( get_the_ID(), '_crm_needs_work', true );
$entry['needs_work'] = 'yes' == $needs_work ? 1 : 0;
$status = array_shift( get_the_terms( get_the_ID(), 'category' ) );
$entry['status'] = $status ? $statuses[$status->slug] : '';
$delayed = get_post_meta( get_the_ID(), '_crm_needs_work', true );
if( 'delayed' == $delayed )
$entry['status'] = 3;
$project_status = get_post_meta( get_the_ID(), '_crm_project_status', true );
if( 4 == $entry['status'] && in_array( $project_status, array( 'edit', 'dev-complete', 'project-complete', 'maintenance' ) ) )
$entry['status'] = 5;
$entry['status_summary'] = get_post_meta( get_the_ID(), '_crm_status_summary', true );
$entry['budget'] = 7;
$entry['timeframe'] = '';
$start = get_post_meta( get_the_ID(), '_crm_date_dev_start', true );
$entry['date_start'] = $start ? date( 'Y-m-d', $start ) : '';
$entry['date_review'] = '';
$end = get_post_meta( get_the_ID(), '_crm_date_complete', true );
$entry['date_complete'] = $end ? date( 'Y-m-d', $end ) : '';
$entry['revenue'] = get_post_meta( get_the_ID(), '_crm_revenue', true );
$entry['expenses'] = get_post_meta( get_the_ID(), '_crm_expense', true );
$entry['hours'] = get_post_meta( get_the_ID(), '_crm_time', true );
$entry['hours_cr'] = get_post_meta( get_the_ID(), '_crm_time_changes', true );
$entry['inquiry'] = get_the_content();
$entry['scope'] = get_post_meta( get_the_ID(), '_crm_scope', true );
$entry['notes'] = get_post_meta( get_the_ID(), '_crm_server_information', true );
// Fields we are using
$fields = array(
'name' => $entry['name'],
'email' => $entry['email'],
'phone' => $entry['phone'],
'url' => $entry['url'],
'date' => $entry['date'],
'project_name' => $entry['project_name'],
'referral' => $entry['referral'],
'referral_other' => $entry['referral_other'],
'needs_work' => $entry['needs_work'],
'status' => $entry['status'],
'status_summary' => $entry['status_summary'],
'budget' => $entry['budget'],
'timeframe' => $entry['timeframe'],
'date_start' => $entry['date_start'],
'date_review' => $entry['date_review'],
'date_complete' => $entry['date_complete'],
'revenue' => str_replace(',', '', $entry['revenue'] ),
'expenses' => str_replace(',', '', $entry['expenses'] ),
'hours' => $entry['hours'],
'hours_cr' => $entry['hours_cr'],
'inquiry' => $entry['inquiry'],
'scope' => $entry['scope'],
'notes' => $entry['notes'],
);
// Insert values into database
$add = $db->insert( 'entries', $fields );
$id = $db->insertId();
// Mark it as imported
update_post_meta( get_the_ID(), 'be_exported', 1 );
$imported++;
endwhile; endif; wp_reset_postdata();
echo '<p>Imported ' . $imported . ' of ' . $loop->found_posts . ' entries</p>';
}
add_action( 'genesis_after_loop', 'be_export_content' );
genesis();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.