b1a485bd by Jeremy Groot

file upload script

1 parent 5fea301d
1 <?php
2
3 require_once( ABSPATH . 'wp-admin/includes/file.php' );
4
5 class SYNC_FILES {
6
7 private static function cleanTerm($str) {
8 return trim($str);
9 }
10
11 private static function setPostData($post_id, $extracted_data) {
12
13 update_field('authors', $extracted_data['authors'], $post_id);
14 update_field('document_year', $extracted_data['year'], $post_id);
15 update_field('version', $extracted_data['version'], $post_id);
16 update_field('msf_initiative', $extracted_data['initiative'], $post_id);
17 update_field('document_link', $extracted_data['link'], $post_id);
18 update_field('description', $extracted_data['description'], $post_id);
19
20
21
22 wp_set_post_terms($post_id,implode(",",$extracted_data['tags']), 'post_tag');
23 wp_set_post_terms($post_id,$extracted_data['category'], 'categories');
24 wp_set_post_terms($post_id,$extracted_data['continent'], 'continent');
25 wp_set_post_terms($post_id,$extracted_data['country'], 'country');
26 wp_set_post_terms($post_id,$extracted_data['doc_cat'], 'document-category');
27 wp_set_post_terms($post_id,$extracted_data['doc_format'], 'document-format');
28 wp_set_post_terms($post_id,$extracted_data['language'], 'language');
29 wp_set_post_terms($post_id,$extracted_data['office'], 'msf-office');
30 wp_set_post_terms($post_id,$extracted_data['oc'], 'oc');
31 wp_set_post_terms($post_id,$extracted_data['region'], 'region');
32
33 }
34
35 private static function extractData($data) {
36
37 $tags = [self::cleanTerm($data[self::$COLUMN_MAP['key_1']]),self::cleanTerm($data[self::$COLUMN_MAP['key_2']])
38 ,self::cleanTerm($data[self::$COLUMN_MAP['key_3']]),self::cleanTerm($data[self::$COLUMN_MAP['key_4']]),self::cleanTerm($data[self::$COLUMN_MAP['key_5']])];
39
40 $category = self::cleanTerm($data[self::$COLUMN_MAP['cat']]);
41 $continent = self::cleanTerm($data[self::$COLUMN_MAP['continent']]);
42 $country = self::cleanTerm($data[self::$COLUMN_MAP['country']]);
43 $doc_cat = self::cleanTerm($data[self::$COLUMN_MAP['category']]);
44 $doc_format = self::cleanTerm($data[self::$COLUMN_MAP['format']]);
45 $language = self::cleanTerm($data[self::$COLUMN_MAP['language']]);
46 $office = self::cleanTerm($data[self::$COLUMN_MAP['office']]);
47 $oc = self::cleanTerm($data[self::$COLUMN_MAP['oc']]);
48 $region = self::cleanTerm($data[self::$COLUMN_MAP['region']]);
49 $authors = self::cleanTerm($data[self::$COLUMN_MAP['author']]);
50 $year = self::cleanTerm($data[self::$COLUMN_MAP['year']]);
51 $version = self::cleanTerm($data[self::$COLUMN_MAP['version']]);
52 $initiative = self::cleanTerm($data[self::$COLUMN_MAP['initiative']]);
53 $link = self::cleanTerm($data[self::$COLUMN_MAP['link']]);
54 $title = self::cleanTerm($data[self::$COLUMN_MAP['title']]);
55 $description = self::cleanTerm($data[self::$COLUMN_MAP['description']]);
56
57 $safe_data = [
58 'tags'=>$tags,
59 'category'=>$category,
60 'continent'=>$continent,
61 'country'=>$country,
62 'doc_cat'=>$doc_cat,
63 'doc_format'=>$doc_format,
64 'language'=>$language,
65 'office'=>$office,
66 'oc'=>$oc,
67 'region'=>$region,
68 'authors'=>$authors,
69 'year'=>$year,
70 'version'=>$version,
71 'initiative'=>$initiative,
72 'link'=>$link,
73 'title'=>$title,
74 'description'=>$description
75 ];
76
77 return $safe_data;
78
79 }
80
81 public static $COLUMN_MAP = [
82 '',
83 '',
84 'ref_number'=>2,
85 'title'=>3,
86 'link'=>4,
87 'author'=>5,
88 'oc'=>6,
89 'office'=>7,
90 'initiative'=>8,
91 'year'=>9,
92 'format'=>10,
93 'category'=>11,
94 'version'=>12,
95 'language'=>13,
96 'continent'=>14,
97 'region'=>15,
98 'country'=>16,
99 'multiple_countries'=>17,
100 'key_1'=>18,
101 'key_2'=>19,
102 'key_3'=>20,
103 'key_4'=>21,
104 'key_5'=>22,
105 'description'=>23,
106 'cat'=>24
107 ];
108
109 private static function createDocument($file) {
110
111 $data = self::extractData($file);
112
113 if(!post_exists($data['title'],null,null,'documents')) {
114 $post = wp_insert_post([
115 'post_title' => $data['title'],
116 'post_status'=>'publish',
117 'post_type'=>'documents'
118 ]);
119 } else {
120 $post = get_posts([
121 'post_type'=>'documents',
122 'title' => $data['title']
123 ]);
124 if(count($post) > 0) {
125 $post = $post[0];
126 }
127 }
128
129
130 if($post) {
131 self::setPostData($post->ID, $data);
132 }
133
134 }
135
136 private static function uploadLocalFile($file) {
137
138 $data = self::extractData($file);
139
140 $link = $file[4];
141 $title = $file[3];
142
143
144 $filepath = wp_upload_dir()['basedir'] . "/document_list_files/" . $link;
145
146 if(file_exists($filepath)) {
147
148 if(!post_exists($title,null,null,'attachment')) {
149
150 $wp_filetype = wp_check_filetype( $filepath, null );
151
152 $attachment = array(
153 'post_mime_type' => $wp_filetype['type'],
154 'post_title' => $title,
155 'post_content' => '',
156 'post_status' => 'inherit'
157 );
158
159 $attach_id = wp_insert_attachment( $attachment, $filepath );
160 require_once( ABSPATH . 'wp-admin/includes/image.php' );
161 $attach_data = wp_generate_attachment_metadata( $attach_id, $filepath );
162 wp_update_attachment_metadata( $attach_id, $attach_data );
163
164 }
165
166 self::setPostData($attach_id, $data);
167
168 } else {
169 WP_CLI::line( "FILE DOES NOT EXIST = " . $filepath );
170 }
171
172 }
173
174 public function sync_files() {
175 // WP_CLI::line( 'Hello World!' );
176
177 $external = [];
178 $local = [];
179
180 $store_search_file = fopen(wp_upload_dir()['basedir']."/documents_list.csv", "r+");
181 $row = 1;
182
183 while (($data = fgetcsv($store_search_file)) !== FALSE) {
184
185 $is_external = false;
186 $link = $data[4];
187 if(strpos($link,'http') !== false) {
188 $is_external = true;
189 }
190
191 if($is_external) {
192 $external[] = $data;
193 } else {
194 $local[] = $data;
195 }
196
197 }
198
199 WP_CLI::line( "EXTERNAL COUNT = " . count($external) );
200 WP_CLI::line( "LOCAL COUNT = " . count($local) );
201
202 foreach($external as $ext) {
203 self::createDocument($ext);
204 }
205
206 foreach($local as $file) {
207 self::uploadLocalFile($file);
208 }
209
210
211
212 }
213
214
215
216 }
217
218 /**
219 * Registers our command when cli get's initialized.
220 *
221 * @since 1.0.0
222 * @author Scott Anderson
223 */
224 function sync_files_cli_register_commands() {
225 WP_CLI::add_command( 'tz', 'SYNC_FILES' );
226 }
227
228 add_action( 'cli_init', 'sync_files_cli_register_commands' );
...\ No newline at end of file ...\ No newline at end of file
...@@ -9,3 +9,4 @@ require_once 'disable-comments.php'; ...@@ -9,3 +9,4 @@ require_once 'disable-comments.php';
9 require_once 'blocks.php'; 9 require_once 'blocks.php';
10 require_once 'side-menu.php'; 10 require_once 'side-menu.php';
11 require_once 'widgets-area.php'; 11 require_once 'widgets-area.php';
12 require_once 'commands.php';
......