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,13 +128,16 @@ class JobsImporter
$podId = $pod->save($adData);
// Set post language
$this->setPostLang($podId, $ad['lang']);
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) {
if ($this->log) {
print_r("Error while updating record ($this->idKey: $primaryKey). Error: $e->getMessage()<br>");
}
}
} else {
......@@ -126,12 +147,15 @@ class JobsImporter
// Set post language
$this->setPostLang($podId, $ad['lang']);
$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>");
}
} catch (Exception $e) {
if ($this->log) {
print_r("Error while creating record ($this->primaryKey: $primaryKey). Error: $e->getMessage()<br>");
}
}
}
// Set primary properties
if (is_plugin_active('wordpress-seo/wp-seo.php')) {
......@@ -255,9 +279,10 @@ class JobsImporter
$termId = pods($key)->add($data);
} catch (Exception $e) {
if ($this->log) {
print_r("Error while creating taxonomies ($key => $data[slug]). Error: $e->getMessage()<br>");
}
}
} else {
try {
......@@ -265,9 +290,10 @@ class JobsImporter
$termId = $term->save($data);
} catch (Exception $e) {
if ($this->log) {
print_r("Error while updating taxonomies ($key => $data[slug]). Error: $e->getMessage()<br>");
}
}
}
......@@ -308,9 +334,10 @@ class JobsImporter
);
} catch (Exception $e) {
if ($this->log) {
print_r("Error while creating custom post ($key => $slug). Error: $e->getMessage()<br>");
}
}
} else {
try {
......@@ -324,10 +351,11 @@ class JobsImporter
);
} catch (Exception $e) {
if ($this->log) {
print_r("Error while updating custom post ($key => $slug). Error: $e->getMessage()<br>");
}
}
}
if (empty($postId)) {
......@@ -351,13 +379,16 @@ class JobsImporter
try {
PLL()->model->post->set_language($postId, $lang);
if ($this->log) {
print_r("Set language code '$lang' for custom post (id: $postId). <br>");
}
} catch (Exception $e) {
if ($this->log) {
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
......@@ -370,13 +401,16 @@ class JobsImporter
try {
PLL()->model->term->set_language($termId, $lang);
if ($this->log) {
print_r("Set language code '$lang' for taxonomy (id: $termId). <br>");
}
} catch (Exception $e) {
if ($this->log) {
print_r("Error while setting language code for taxonomy (id: $termId), error: $e->getMessage().<br>");
}
}
}
/**
* Get pod by given id
......
......@@ -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