d91b15f7 by Jeremy Groot

brokers cli

1 parent 26611cc6
......@@ -14,6 +14,7 @@ require_once 'inc/shortcodes-resources.php';
require_once 'inc/shortcodes-circulars.php';
require_once 'inc/blocks.php';
require_once 'inc/breadcrumb.php';
require_once 'inc/brokers_cli.php';
function load_custom_wp_admin_style(){
wp_register_style( 'custom_wp_admin_css', get_bloginfo('template_url') . '/style.css', false, '1.0.0' );
......
<?php
if ( class_exists( 'WP_CLI' ) ) {
class TZ_BROKER_COMMANDS {
function rebuild_brokers_assigment($args ) {
// WP_CLI::log(wp_get_upload_dir()['basedir'] . "/" . $args[0]);
$new_brokers = [];
$row = 0;
if (($handle = fopen(wp_get_upload_dir()['basedir'] . "/" . $args[0], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row == 0) {
++$row;
continue;
}
$broker_name = $data[0];
$broker_id = $data[2];
$new_brokers[] = ["broker_name"=>$broker_name,"broker_id"=>$broker_id];
++$row;
}
fclose($handle);
}
$replacements = [];
$brokers = getBrokerageList();
foreach($new_brokers as $nb) {
$broker_user = get_user_by('login',$nb['broker_name']);
if($broker_user) {
$old_broker_id = get_user_meta($broker_user->ID, 'broker_id', true);
$old_brokerage = get_user_meta($broker_user->ID, 'brokerage', true);
if(!empty($nb['broker_id']) && $old_broker_id != $nb['broker_id']) {
$new_brokerage = null;
foreach($brokers as $broker) {
if($broker['broker_id'] == $nb['broker_id']) {
$new_brokerage = $broker['brokerage'];
}
}
if(!empty($new_brokerage)) {
$replacements[] = ['user_id'=>$broker_user->ID,
'new_broker_id'=>$nb['broker_id'],'new_brokerage'=>$new_brokerage,
'old_broker_id'=>$old_broker_id,'old_brokerage'=>$old_brokerage];
}
}
}
}
$old_assigns_brokers = get_option('old_broker_replacements');
if(!$old_assigns_brokers) {
update_option('old_broker_replacements', serialize($replacements));
}
foreach($replacements as $replacer) {
update_user_meta($replacer['user_id'], 'broker_id', $replacer['new_broker_id']);
update_user_meta($replacer['user_id'], 'brokerage', $replacer['new_brokerage']);
}
}
function rebuild_broker_list_from_csv($args ) {
// WP_CLI::log(wp_get_upload_dir()['basedir'] . "/" . $args[0]);
$new_brokers = [];
$row = 0;
if (($handle = fopen(wp_get_upload_dir()['basedir'] . "/" . $args[0], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row == 0) {
++$row;
continue;
}
$new_id = $data[0];
$name = $data[1];
$new_brokers[] = ["broker_id"=>$new_id,"brokerage"=>$name];
// echo $name;
++$row;
}
fclose($handle);
}
$old_brokers = getBrokerageList();
$old_saved_brokers = get_option('old_broker_list');
if(!$old_saved_brokers) {
update_option('old_broker_list', serialize($old_brokers));
}
update_option('broker_list', serialize($new_brokers));
}
private function cleanTerm($str) {
return trim($str);
}
private function cutOffDomain($str) {
if(strpos($str, 'https://brokers.thecommonwell.ca/wp-content/uploads') !== false) {
return str_replace( 'https://brokers.thecommonwell.ca/wp-content/uploads','',$str);
} else {
return $str;
}
}
private static function setPostData($post_id, $categories, $doc_type) {
wp_set_post_terms($post_id,$categories, 'media_category');
wp_set_post_terms($post_id, $doc_type, 'document_type');
}
function import_media_files( $args ) {
// WP_CLI::log(wp_get_upload_dir()['basedir'] . "/" . $args[0]);
$new_media = [];
$row = 0;
if (($handle = fopen(wp_get_upload_dir()['basedir'] . "/" . $args[0], "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row == 0) {
++$row;
continue;
}
$new_media[] = ['title'=>trim($data[0]),'file_name'=>trim($data[1]),'link'=>$this->cutOffDomain(trim($data[2])),
'type'=>trim($data[3]),'product_line'=>trim($data[4]),'keywords'=>trim($data[5])];
++$row;
}
fclose($handle);
}
$document_types = [];
foreach($new_media as $nm) {
if(!file_exists(wp_get_upload_dir()['basedir'] . $nm['link'])) {
error_log($nm['link'] . " does not exist!");
} else {
global $wpdb;
$post_id = $wpdb->get_results('select ID from wp_posts where guid like "%'.$nm['link'].'%"');
if($post_id) {
$post_id = $post_id[0]->ID;
if(!$post_id) {
error_log($nm['link'] . " no attachment id!");
} else {
$doc_type = $nm['type'];
$product_line = $nm['product_line'];
$this->setPostData($post_id, $product_line, $doc_type);
// if(!isset($document_types[$nm['type']])) {
// $document_types[$nm['type']] = 1;
// } else {
// $document_types[$nm['type']]++;
// }
// $this->setPostData($post_id,);
}
} else {
error_log($nm['link'] . " no attachment id!");
}
}
}
}
}
WP_CLI::add_command( 'tz_brokers', 'TZ_BROKER_COMMANDS' );
}
\ No newline at end of file