Commit b3aeff03 authored by Simonas's avatar Simonas

import endpoint added

parent 7af34033
......@@ -31,6 +31,12 @@ location /wp-json/api/v1/recommended {
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 {
access_log /var/log/nginx/postdata.log postdata;
......
......@@ -7,10 +7,12 @@
* @package Biuro Jobs importer
* @used-by \jobs-importer\job-importer.php
*/
class JobsImporter
{
private $data;
private $log;
// Biuro record unique key
protected $primaryKey = 'livas-id';
......@@ -32,9 +34,14 @@ class JobsImporter
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->log = $log;
$this->setPods();
}
......@@ -67,6 +74,17 @@ class JobsImporter
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
*/
......@@ -110,12 +128,15 @@ class JobsImporter
$podId = $pod->save($adData);
// Set post language
$this->setPostLang($podId, $ad['lang']);
print_r("Job ID: <b>$podId</b> <br>");
print_r("Old record ($this->primaryKey: $primaryKey) has been updated. <br>");
if ($this->log) {
print_r("Job ID: <b>$podId</b> <br>");
print_r("Old record ($this->primaryKey: $primaryKey) has been updated. <br>");
}
} catch (Exception $e) {
print_r("Error while updating record ($this->idKey: $primaryKey). Error: $e->getMessage()<br>");
if ($this->log) {
print_r("Error while updating record ($this->idKey: $primaryKey). Error: $e->getMessage()<br>");
}
}
} else {
......@@ -126,10 +147,13 @@ class JobsImporter
// Set post language
$this->setPostLang($podId, $ad['lang']);
$this->getPod($primaryKey)->save($adData); // Save related data, cause didn't saved on creation
print_r("New record ($this->primaryKey: $primaryKey) has been created. <br>");
if ($this->log) {
print_r("New record ($this->primaryKey: $primaryKey) has been created. <br>");
}
} catch (Exception $e) {
print_r("Error while creating record ($this->primaryKey: $primaryKey). Error: $e->getMessage()<br>");
if ($this->log) {
print_r("Error while creating record ($this->primaryKey: $primaryKey). Error: $e->getMessage()<br>");
}
}
}
......@@ -255,8 +279,9 @@ class JobsImporter
$termId = pods($key)->add($data);
} catch (Exception $e) {
print_r("Error while creating taxonomies ($key => $data[slug]). Error: $e->getMessage()<br>");
if ($this->log) {
print_r("Error while creating taxonomies ($key => $data[slug]). Error: $e->getMessage()<br>");
}
}
} else {
......@@ -265,8 +290,9 @@ class JobsImporter
$termId = $term->save($data);
} catch (Exception $e) {
print_r("Error while updating taxonomies ($key => $data[slug]). Error: $e->getMessage()<br>");
if ($this->log) {
print_r("Error while updating taxonomies ($key => $data[slug]). Error: $e->getMessage()<br>");
}
}
}
......@@ -308,8 +334,9 @@ class JobsImporter
);
} catch (Exception $e) {
print_r("Error while creating custom post ($key => $slug). Error: $e->getMessage()<br>");
if ($this->log) {
print_r("Error while creating custom post ($key => $slug). Error: $e->getMessage()<br>");
}
}
} else {
......@@ -324,8 +351,9 @@ class JobsImporter
);
} catch (Exception $e) {
print_r("Error while updating custom post ($key => $slug). Error: $e->getMessage()<br>");
if ($this->log) {
print_r("Error while updating custom post ($key => $slug). Error: $e->getMessage()<br>");
}
}
}
......@@ -351,11 +379,14 @@ class JobsImporter
try {
PLL()->model->post->set_language($postId, $lang);
print_r("Set language code '$lang' for custom post (id: $postId). <br>");
if ($this->log) {
print_r("Set language code '$lang' for custom post (id: $postId). <br>");
}
} catch (Exception $e) {
print_r("Error while setting language code for custom post (id: $postId), error: $e->getMessage().<br>");
if ($this->log) {
print_r("Error while setting language code for custom post (id: $postId), error: $e->getMessage().<br>");
}
}
}
......@@ -370,11 +401,14 @@ class JobsImporter
try {
PLL()->model->term->set_language($termId, $lang);
print_r("Set language code '$lang' for taxonomy (id: $termId). <br>");
if ($this->log) {
print_r("Set language code '$lang' for taxonomy (id: $termId). <br>");
}
} catch (Exception $e) {
print_r("Error while setting language code for taxonomy (id: $termId), error: $e->getMessage().<br>");
if ($this->log) {
print_r("Error while setting language code for taxonomy (id: $termId), error: $e->getMessage().<br>");
}
}
}
......
......@@ -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