d91b15f7 by Jeremy Groot

brokers cli

1 parent 26611cc6
...@@ -14,6 +14,7 @@ require_once 'inc/shortcodes-resources.php'; ...@@ -14,6 +14,7 @@ require_once 'inc/shortcodes-resources.php';
14 require_once 'inc/shortcodes-circulars.php'; 14 require_once 'inc/shortcodes-circulars.php';
15 require_once 'inc/blocks.php'; 15 require_once 'inc/blocks.php';
16 require_once 'inc/breadcrumb.php'; 16 require_once 'inc/breadcrumb.php';
17 require_once 'inc/brokers_cli.php';
17 18
18 function load_custom_wp_admin_style(){ 19 function load_custom_wp_admin_style(){
19 wp_register_style( 'custom_wp_admin_css', get_bloginfo('template_url') . '/style.css', false, '1.0.0' ); 20 wp_register_style( 'custom_wp_admin_css', get_bloginfo('template_url') . '/style.css', false, '1.0.0' );
......
1 <?php
2
3 if ( class_exists( 'WP_CLI' ) ) {
4 class TZ_BROKER_COMMANDS {
5
6
7
8 function rebuild_brokers_assigment($args ) {
9
10 // WP_CLI::log(wp_get_upload_dir()['basedir'] . "/" . $args[0]);
11
12 $new_brokers = [];
13
14 $row = 0;
15 if (($handle = fopen(wp_get_upload_dir()['basedir'] . "/" . $args[0], "r")) !== FALSE) {
16 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
17 if($row == 0) {
18 ++$row;
19 continue;
20 }
21 $broker_name = $data[0];
22 $broker_id = $data[2];
23 $new_brokers[] = ["broker_name"=>$broker_name,"broker_id"=>$broker_id];
24 ++$row;
25 }
26 fclose($handle);
27 }
28
29 $replacements = [];
30 $brokers = getBrokerageList();
31
32 foreach($new_brokers as $nb) {
33
34 $broker_user = get_user_by('login',$nb['broker_name']);
35
36 if($broker_user) {
37
38 $old_broker_id = get_user_meta($broker_user->ID, 'broker_id', true);
39 $old_brokerage = get_user_meta($broker_user->ID, 'brokerage', true);
40
41 if(!empty($nb['broker_id']) && $old_broker_id != $nb['broker_id']) {
42
43 $new_brokerage = null;
44 foreach($brokers as $broker) {
45 if($broker['broker_id'] == $nb['broker_id']) {
46 $new_brokerage = $broker['brokerage'];
47 }
48 }
49
50 if(!empty($new_brokerage)) {
51 $replacements[] = ['user_id'=>$broker_user->ID,
52 'new_broker_id'=>$nb['broker_id'],'new_brokerage'=>$new_brokerage,
53 'old_broker_id'=>$old_broker_id,'old_brokerage'=>$old_brokerage];
54 }
55
56 }
57 }
58
59 }
60
61 $old_assigns_brokers = get_option('old_broker_replacements');
62 if(!$old_assigns_brokers) {
63 update_option('old_broker_replacements', serialize($replacements));
64 }
65
66 foreach($replacements as $replacer) {
67
68 update_user_meta($replacer['user_id'], 'broker_id', $replacer['new_broker_id']);
69 update_user_meta($replacer['user_id'], 'brokerage', $replacer['new_brokerage']);
70
71 }
72
73
74
75 }
76
77 function rebuild_broker_list_from_csv($args ) {
78
79 // WP_CLI::log(wp_get_upload_dir()['basedir'] . "/" . $args[0]);
80
81 $new_brokers = [];
82
83 $row = 0;
84 if (($handle = fopen(wp_get_upload_dir()['basedir'] . "/" . $args[0], "r")) !== FALSE) {
85 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
86 if($row == 0) {
87 ++$row;
88 continue;
89 }
90 $new_id = $data[0];
91 $name = $data[1];
92 $new_brokers[] = ["broker_id"=>$new_id,"brokerage"=>$name];
93 // echo $name;
94 ++$row;
95 }
96 fclose($handle);
97 }
98
99 $old_brokers = getBrokerageList();
100
101 $old_saved_brokers = get_option('old_broker_list');
102 if(!$old_saved_brokers) {
103 update_option('old_broker_list', serialize($old_brokers));
104 }
105
106 update_option('broker_list', serialize($new_brokers));
107
108
109 }
110
111 private function cleanTerm($str) {
112 return trim($str);
113 }
114
115 private function cutOffDomain($str) {
116 if(strpos($str, 'https://brokers.thecommonwell.ca/wp-content/uploads') !== false) {
117 return str_replace( 'https://brokers.thecommonwell.ca/wp-content/uploads','',$str);
118 } else {
119 return $str;
120 }
121 }
122
123 private static function setPostData($post_id, $categories, $doc_type) {
124
125 wp_set_post_terms($post_id,$categories, 'media_category');
126 wp_set_post_terms($post_id, $doc_type, 'document_type');
127
128 }
129
130 function import_media_files( $args ) {
131
132 // WP_CLI::log(wp_get_upload_dir()['basedir'] . "/" . $args[0]);
133
134 $new_media = [];
135
136 $row = 0;
137 if (($handle = fopen(wp_get_upload_dir()['basedir'] . "/" . $args[0], "r")) !== FALSE) {
138 while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
139 if($row == 0) {
140 ++$row;
141 continue;
142 }
143 $new_media[] = ['title'=>trim($data[0]),'file_name'=>trim($data[1]),'link'=>$this->cutOffDomain(trim($data[2])),
144 'type'=>trim($data[3]),'product_line'=>trim($data[4]),'keywords'=>trim($data[5])];
145 ++$row;
146 }
147 fclose($handle);
148 }
149 $document_types = [];
150 foreach($new_media as $nm) {
151
152 if(!file_exists(wp_get_upload_dir()['basedir'] . $nm['link'])) {
153 error_log($nm['link'] . " does not exist!");
154 } else {
155
156 global $wpdb;
157
158 $post_id = $wpdb->get_results('select ID from wp_posts where guid like "%'.$nm['link'].'%"');
159
160 if($post_id) {
161 $post_id = $post_id[0]->ID;
162 if(!$post_id) {
163 error_log($nm['link'] . " no attachment id!");
164 } else {
165
166 $doc_type = $nm['type'];
167 $product_line = $nm['product_line'];
168
169 $this->setPostData($post_id, $product_line, $doc_type);
170
171 // if(!isset($document_types[$nm['type']])) {
172 // $document_types[$nm['type']] = 1;
173 // } else {
174 // $document_types[$nm['type']]++;
175 // }
176 // $this->setPostData($post_id,);
177 }
178 } else {
179 error_log($nm['link'] . " no attachment id!");
180 }
181
182
183
184 }
185
186 }
187
188 }
189
190 }
191
192 WP_CLI::add_command( 'tz_brokers', 'TZ_BROKER_COMMANDS' );
193 }
...\ No newline at end of file ...\ No newline at end of file