Commit b3aeff03 authored by Simonas's avatar Simonas

import endpoint added

parent 7af34033
...@@ -31,6 +31,12 @@ location /wp-json/api/v1/recommended { ...@@ -31,6 +31,12 @@ location /wp-json/api/v1/recommended {
auth_basic_user_file /etc/nginx/.htpasswd; auth_basic_user_file /etc/nginx/.htpasswd;
} }
location /wp-json/api/v1/import-positions {
try_files $uri $uri/ /index.php?$args;
auth_basic "Basic auth";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location = /wp-admin/admin-post.php { location = /wp-admin/admin-post.php {
access_log /var/log/nginx/postdata.log postdata; access_log /var/log/nginx/postdata.log postdata;
......
...@@ -7,10 +7,12 @@ ...@@ -7,10 +7,12 @@
* @package Biuro Jobs importer * @package Biuro Jobs importer
* @used-by \jobs-importer\job-importer.php * @used-by \jobs-importer\job-importer.php
*/ */
class JobsImporter class JobsImporter
{ {
private $data; private $data;
private $log;
// Biuro record unique key // Biuro record unique key
protected $primaryKey = 'livas-id'; protected $primaryKey = 'livas-id';
...@@ -32,9 +34,14 @@ class JobsImporter ...@@ -32,9 +34,14 @@ class JobsImporter
protected $primaryTerms = []; protected $primaryTerms = [];
public function __construct($data) public function __construct($data, $log = true)
{ {
if (!function_exists('is_plugin_active')) {
require_once 'wp-admin/includes/plugin.php';
}
$this->data = $data; $this->data = $data;
$this->log = $log;
$this->setPods(); $this->setPods();
} }
...@@ -67,6 +74,17 @@ class JobsImporter ...@@ -67,6 +74,17 @@ class JobsImporter
return $data; return $data;
} }
public static function importPositions () {
$env = getEnvironment();
$inputFile = getSource($env);
$ads = (new JsonDataCollector($inputFile))->getData();
(new JobsImporter($ads, false))->import();
exit;
}
/** /**
* Import posts from provided data: if post with given Id exists, it rewrites * Import posts from provided data: if post with given Id exists, it rewrites
*/ */
...@@ -110,13 +128,16 @@ class JobsImporter ...@@ -110,13 +128,16 @@ class JobsImporter
$podId = $pod->save($adData); $podId = $pod->save($adData);
// Set post language // Set post language
$this->setPostLang($podId, $ad['lang']); $this->setPostLang($podId, $ad['lang']);
if ($this->log) {
print_r("Job ID: <b>$podId</b> <br>"); print_r("Job ID: <b>$podId</b> <br>");
print_r("Old record ($this->primaryKey: $primaryKey) has been updated. <br>"); print_r("Old record ($this->primaryKey: $primaryKey) has been updated. <br>");
}
} catch (Exception $e) { } catch (Exception $e) {
if ($this->log) {
print_r("Error while updating record ($this->idKey: $primaryKey). Error: $e->getMessage()<br>"); print_r("Error while updating record ($this->idKey: $primaryKey). Error: $e->getMessage()<br>");
} }
}
} else { } else {
...@@ -126,12 +147,15 @@ class JobsImporter ...@@ -126,12 +147,15 @@ class JobsImporter
// Set post language // Set post language
$this->setPostLang($podId, $ad['lang']); $this->setPostLang($podId, $ad['lang']);
$this->getPod($primaryKey)->save($adData); // Save related data, cause didn't saved on creation $this->getPod($primaryKey)->save($adData); // Save related data, cause didn't saved on creation
if ($this->log) {
print_r("New record ($this->primaryKey: $primaryKey) has been created. <br>"); print_r("New record ($this->primaryKey: $primaryKey) has been created. <br>");
}
} catch (Exception $e) { } catch (Exception $e) {
if ($this->log) {
print_r("Error while creating record ($this->primaryKey: $primaryKey). Error: $e->getMessage()<br>"); print_r("Error while creating record ($this->primaryKey: $primaryKey). Error: $e->getMessage()<br>");
} }
} }
}
// Set primary properties // Set primary properties
if (is_plugin_active('wordpress-seo/wp-seo.php')) { if (is_plugin_active('wordpress-seo/wp-seo.php')) {
...@@ -255,9 +279,10 @@ class JobsImporter ...@@ -255,9 +279,10 @@ class JobsImporter
$termId = pods($key)->add($data); $termId = pods($key)->add($data);
} catch (Exception $e) { } catch (Exception $e) {
if ($this->log) {
print_r("Error while creating taxonomies ($key => $data[slug]). Error: $e->getMessage()<br>"); print_r("Error while creating taxonomies ($key => $data[slug]). Error: $e->getMessage()<br>");
} }
}
} else { } else {
try { try {
...@@ -265,9 +290,10 @@ class JobsImporter ...@@ -265,9 +290,10 @@ class JobsImporter
$termId = $term->save($data); $termId = $term->save($data);
} catch (Exception $e) { } catch (Exception $e) {
if ($this->log) {
print_r("Error while updating taxonomies ($key => $data[slug]). Error: $e->getMessage()<br>"); print_r("Error while updating taxonomies ($key => $data[slug]). Error: $e->getMessage()<br>");
} }
}
} }
...@@ -308,9 +334,10 @@ class JobsImporter ...@@ -308,9 +334,10 @@ class JobsImporter
); );
} catch (Exception $e) { } catch (Exception $e) {
if ($this->log) {
print_r("Error while creating custom post ($key => $slug). Error: $e->getMessage()<br>"); print_r("Error while creating custom post ($key => $slug). Error: $e->getMessage()<br>");
} }
}
} else { } else {
try { try {
...@@ -324,10 +351,11 @@ class JobsImporter ...@@ -324,10 +351,11 @@ class JobsImporter
); );
} catch (Exception $e) { } catch (Exception $e) {
if ($this->log) {
print_r("Error while updating custom post ($key => $slug). Error: $e->getMessage()<br>"); print_r("Error while updating custom post ($key => $slug). Error: $e->getMessage()<br>");
} }
} }
}
if (empty($postId)) { if (empty($postId)) {
...@@ -351,13 +379,16 @@ class JobsImporter ...@@ -351,13 +379,16 @@ class JobsImporter
try { try {
PLL()->model->post->set_language($postId, $lang); PLL()->model->post->set_language($postId, $lang);
if ($this->log) {
print_r("Set language code '$lang' for custom post (id: $postId). <br>"); print_r("Set language code '$lang' for custom post (id: $postId). <br>");
}
} catch (Exception $e) { } catch (Exception $e) {
if ($this->log) {
print_r("Error while setting language code for custom post (id: $postId), error: $e->getMessage().<br>"); print_r("Error while setting language code for custom post (id: $postId), error: $e->getMessage().<br>");
} }
} }
}
/** /**
* Set language param for given taxonomy, using Polylang library * Set language param for given taxonomy, using Polylang library
...@@ -370,13 +401,16 @@ class JobsImporter ...@@ -370,13 +401,16 @@ class JobsImporter
try { try {
PLL()->model->term->set_language($termId, $lang); PLL()->model->term->set_language($termId, $lang);
if ($this->log) {
print_r("Set language code '$lang' for taxonomy (id: $termId). <br>"); print_r("Set language code '$lang' for taxonomy (id: $termId). <br>");
}
} catch (Exception $e) { } catch (Exception $e) {
if ($this->log) {
print_r("Error while setting language code for taxonomy (id: $termId), error: $e->getMessage().<br>"); print_r("Error while setting language code for taxonomy (id: $termId), error: $e->getMessage().<br>");
} }
} }
}
/** /**
* Get pod by given id * Get pod by given id
......
...@@ -580,6 +580,24 @@ add_action( 'rest_api_init', function () { ...@@ -580,6 +580,24 @@ add_action( 'rest_api_init', function () {
] ]
)); ));
register_rest_route( 'api/v1', '/import-positions', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'JobsImporter::importPositions',
// 'args' => [
// 'from' => [
// 'validate_callback' => function($param, $request, $key) {
// return is_string( $param );
// }
// ],
// 'by' => [
// 'validate_callback' => function($param, $request, $key) {
// return is_string( $param );
// }
// ]
// ]
));
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment