Commit 99eccbaf authored by Simon's avatar Simon

Salary type added

parent 06bd4cc3
<?php <?php
class JsonDataCollector class JsonDataCollector
{ {
// Job properties data map // Job properties data map
protected $properties = [ protected $properties = [
'livas-id' => 'id', 'livas-id' => 'id',
'valid' => 'galioja', 'valid' => 'galioja',
'post_date' => 'data', 'post_date' => 'data',
'post_modified' => 'atnaujinta', 'post_modified' => 'atnaujinta',
'title' => 'pareigos', 'title' => 'pareigos',
'post_content' => 'aprasymas', 'post_content' => 'aprasymas',
'slug' => 'seo_url', 'slug' => 'seo_url',
'contact' => 'contacts', 'contact' => 'contacts',
'salary_from' => 'atlygis_nuo', 'salary_from' => 'atlygis_nuo',
'salary_to' => 'atlygis_iki', 'salary_to' => 'atlygis_iki',
'video_url' => 'video_url', 'video_url' => 'video_url',
]; 'salary_type' => 'salary_type',
];
// Taxonomies data map
protected $terms = [ // Taxonomies data map
'city' => [ protected $terms = [
'slug' => 'miestas_id', 'city' => [
'name' => 'regionas', 'slug' => 'miestas_id',
'__primary' => true, 'name' => 'regionas',
'additionalCities' => [ '__primary' => true,
'slug' => 'city_id', 'additionalCities' => [
'name' => 'city', 'slug' => 'city_id',
] 'name' => 'city',
], ]
'field' => [ ],
'slug' => 'kategorija', 'field' => [
'name' => 'sritis', 'slug' => 'kategorija',
'__primary' => true, 'name' => 'sritis',
'additionalCategories' => [ '__primary' => true,
'slug' => 'category_id', 'additionalCategories' => [
'name' => 'category_name', 'slug' => 'category_id',
] 'name' => 'category_name',
], ]
'company' => [ ],
'name' => 'imone' 'company' => [
], 'name' => 'imone'
'type' => [ ],
'types' => [ 'type' => [
'slug' => 'type_id', 'types' => [
'name' => 'type_name' 'slug' => 'type_id',
] 'name' => 'type_name'
], ]
]; ],
];
// Yoast SEO properties
protected $seoProperties = [ // Yoast SEO properties
'metadesc' => 'seo_description', protected $seoProperties = [
'title' => 'seo_title' 'metadesc' => 'seo_description',
]; 'title' => 'seo_title'
];
// Custom posts data map
protected $posts = [ // Custom posts data map
'division' => [ protected $posts = [
'title' => 'padalinys_name', 'division' => [
'slug' => 'padalinys' 'title' => 'padalinys_name',
], 'slug' => 'padalinys'
]; ],
];
// Language map
protected $lang = 'kalba'; // Language map
protected $lang = 'kalba';
// Relations mapping
protected $relationsMap = [ // Relations mapping
'division-id' => 'division', protected $relationsMap = [
]; 'division-id' => 'division',
];
// Source data
protected $sourceData; // Source data
protected $sourceData;
protected $data;
protected $data;
public function __construct($source)
{ public function __construct($source)
$this->setData($source); {
$this->data = $this->transformData(); $this->setData($source);
} $this->data = $this->transformData();
}
/**
* Set data source: /**
* get data from source and transform into array * Set data source:
* * get data from source and transform into array
* @param $source *
*/ * @param $source
protected function setData($source) */
{ protected function setData($source)
$this->sourceData = $this->loadData($source); {
} $this->sourceData = $this->loadData($source);
}
/**
* Parse xml data /**
* * Parse xml data
* @param $source *
* * @param $source
* @return SimpleXMLElement *
*/ * @return SimpleXMLElement
protected function loadData($source) */
{ protected function loadData($source)
$context = stream_context_create([ {
'http' => [ $context = stream_context_create([
'header' => 'Accept: application/json', 'http' => [
], 'header' => 'Accept: application/json',
'ssl' => [ ],
'verify_peer' => false, 'ssl' => [
'verify_peer_name' => false, 'verify_peer' => false,
], 'verify_peer_name' => false,
]); ],
$json = file_get_contents($source, false, $context); ]);
return json_decode($json); $json = file_get_contents($source, false, $context);
} return json_decode($json);
}
/**
* Get data /**
* * Get data
* @return mixed *
*/ * @return mixed
public function getData() */
{ public function getData()
return $this->data; {
} return $this->data;
}
protected function transformData()
{ protected function transformData()
$result = []; {
foreach ($this->sourceData as $ad) { $result = [];
$item = []; foreach ($this->sourceData as $ad) {
$item = [];
$item['properties'] = $this->getProperties($ad);
$item['lang'] = $ad->{$this->lang}; $item['properties'] = $this->getProperties($ad);
$item['terms'] = $this->getTerms($ad); $item['lang'] = $ad->{$this->lang};
$item['posts'] = $this->getPosts($ad); $item['terms'] = $this->getTerms($ad);
$item['relations'] = $this->relationsMap; $item['posts'] = $this->getPosts($ad);
$item['seo_properties'] = $this->getSeoProperties($ad); $item['relations'] = $this->relationsMap;
$item['seo_properties'] = $this->getSeoProperties($ad);
$result[] = $item;
}; $result[] = $item;
};
return $result;
} return $result;
}
private function getProperties($ad)
{ private function getProperties($ad)
$result = []; {
$result = [];
foreach ($this->properties as $propertyKey => $propertyValue) {
if (isset($ad->{$propertyValue})) { foreach ($this->properties as $propertyKey => $propertyValue) {
$dataValue = $ad->{$propertyValue}; if (isset($ad->{$propertyValue})) {
} $dataValue = $ad->{$propertyValue};
$result[$propertyKey] = $this->getValue($dataValue); }
} $result[$propertyKey] = $this->getValue($dataValue);
}
return $result;
} return $result;
}
private function getSeoProperties($ad)
{ private function getSeoProperties($ad)
$result = []; {
$result = [];
foreach ($this->seoProperties as $propertyKey => $propertyValue) {
if (isset($ad->{$propertyValue})) { foreach ($this->seoProperties as $propertyKey => $propertyValue) {
$dataValue = $ad->{$propertyValue}; if (isset($ad->{$propertyValue})) {
} $dataValue = $ad->{$propertyValue};
$result[$propertyKey] = $this->getValue($dataValue); }
} $result[$propertyKey] = $this->getValue($dataValue);
}
return $result;
} return $result;
}
private function getTerms($ad)
{ private function getTerms($ad)
$result = []; {
$result = [];
foreach ($this->terms as $termKey => $termProperties) {
foreach ($this->terms as $termKey => $termProperties) {
$entity = $this->getEntity($ad, $termProperties);
$entity = $this->getEntity($ad, $termProperties);
if ( ! empty($entity)) {
if ( ! empty($entity)) {
$result[$termKey][] = $entity;
} $result[$termKey][] = $entity;
}
$appends = array_filter($termProperties, function($item) {
return is_array($item); $appends = array_filter($termProperties, function($item) {
}); return is_array($item);
});
foreach ($appends as $propertyKey => $propertyValues) {
foreach ($appends as $propertyKey => $propertyValues) {
if (isset($ad->{$propertyKey}) && !empty($ad->{$propertyKey})) {
foreach ($ad->{$propertyKey} as $data) { if (isset($ad->{$propertyKey}) && !empty($ad->{$propertyKey})) {
$result[$termKey][] = $this->getEntity($data, $propertyValues); foreach ($ad->{$propertyKey} as $data) {
} $result[$termKey][] = $this->getEntity($data, $propertyValues);
} }
} }
} }
}
return $result;
} return $result;
}
private function getEntity($ad, $propertyMap)
{ private function getEntity($ad, $propertyMap)
$entity = []; {
foreach ($propertyMap as $propertyKey => $propertyValue) { $entity = [];
foreach ($propertyMap as $propertyKey => $propertyValue) {
if ( ! is_array($propertyValue)) {
if ( ! is_array($propertyValue)) {
if (preg_match('/^\_\_/', $propertyKey)) {
$entity[$propertyKey] = $propertyValue; if (preg_match('/^\_\_/', $propertyKey)) {
} $entity[$propertyKey] = $propertyValue;
elseif (isset($ad->{$propertyValue})) { }
$dataValue = $ad->{$propertyValue}; elseif (isset($ad->{$propertyValue})) {
$entity[$propertyKey] = $this->getValue($dataValue); $dataValue = $ad->{$propertyValue};
} $entity[$propertyKey] = $this->getValue($dataValue);
} }
} }
}
return $entity;
} return $entity;
}
private function getPosts($ad)
{ private function getPosts($ad)
$result = []; {
$result = [];
foreach ($this->posts as $postKey => $postProperties) {
foreach ($postProperties as $propertyKey => $propertyValue) { foreach ($this->posts as $postKey => $postProperties) {
if (isset($ad->{$propertyValue})) { foreach ($postProperties as $propertyKey => $propertyValue) {
$dataValue = $ad->{$propertyValue}; if (isset($ad->{$propertyValue})) {
$result[$postKey][$propertyKey] = $this->getValue($dataValue); $dataValue = $ad->{$propertyValue};
} $result[$postKey][$propertyKey] = $this->getValue($dataValue);
} }
} }
}
return $result;
} return $result;
}
private function getValue($value)
{ private function getValue($value)
return (is_array($value)) ? $value : html_entity_decode($value); {
} return (is_array($value)) ? $value : html_entity_decode($value);
} }
}
<?php <?php
/* /*
Plugin Name: Biuro Jobs importer Plugin Name: Biuro Jobs importer
description: A plugin for import jobs ads from Biuro endpoint description: A plugin for import jobs ads from Biuro endpoint
Version: 1.0.0 Version: 1.0.0
*/ */
set_time_limit(300); set_time_limit(300);
// Data collector: prepare data for import // Data collector: prepare data for import
require_once 'JsonDataCollector.php'; require_once 'JsonDataCollector.php';
// Data importer: imports data to WP posts // Data importer: imports data to WP posts
require_once 'JobsImporter.php'; require_once 'JobsImporter.php';
// Initial data importer // Initial data importer
require_once 'importInitData.php'; require_once 'importInitData.php';
if (function_exists('importer_admin_menu')) { if (function_exists('importer_admin_menu')) {
// Add item to admin menu // Add item to admin menu
add_action('admin_menu', 'importer_admin_menu'); add_action('admin_menu', 'importer_admin_menu');
} }
/** /**
* Set admin menu item for plugin * Set admin menu item for plugin
*/ */
function importer_admin_menu() function importer_admin_menu()
{ {
add_submenu_page( add_submenu_page(
'options-general.php', 'options-general.php',
'Biuro Jobs importer', 'Biuro Jobs importer',
'Biuro Jobs importer', 'Biuro Jobs importer',
'data_importer', 'data_importer',
'jobs-importer', 'jobs-importer',
'do_import' 'do_import'
); );
} }
/** /**
* Imports posts from Biuro endpoint * Imports posts from Biuro endpoint
*/ */
function do_import() function do_import()
{ {
$env = getEnvironment(); $env = getEnvironment();
$inputFile = getSource($env); $inputFile = getSource($env);
print_r("<div style='background: lightgrey;'> print_r("<div style='background: lightgrey;'>
<small style='float: right;'>Working in <strong>$env</strong> mode</small> <small style='float: right;'>Working in <strong>$env</strong> mode</small>
<div style='clear: both;'></div> <div style='clear: both;'></div>
</div>"); </div>");
echo "<pre>"; echo "<pre>";
print_r('<h4>Biuro Jobs importer</h4>'); print_r('<h4>Biuro Jobs importer</h4>');
// if (pll_current_language() != '') // if (pll_current_language() != '')
// { // {
// print_r("<h5 class='warning'>Polylang must be set in 'All languages' mode.</h5>"); // print_r("<h5 class='warning'>Polylang must be set in 'All languages' mode.</h5>");
// pll_the_languages( array( 'dropdown' => 1 ) ); // pll_the_languages( array( 'dropdown' => 1 ) );
// exit(); // exit();
// } // }
print_r("Data source: $inputFile<br><br>"); print_r("Data source: $inputFile<br><br>");
// JSON reader // JSON reader
$ads = (new JsonDataCollector($inputFile))->getData(); $ads = (new JsonDataCollector($inputFile))->getData();
// print_r($ads); // print_r($ads);
print_r("Found " . count($ads) . " ads from Biuro.<br>"); print_r("Found " . count($ads) . " ads from Biuro.<br>");
if (count($ads) > 0) { if (count($ads) > 0) {
if (empty($_POST)) { if (empty($_POST)) {
// Form for import start, set polylang to 'All lang.' for action // Form for import start, set polylang to 'All lang.' for action
print_r("<form method='post' action='" . current_location() . "&lang=all'>"); print_r("<form method='post' action='" . current_location() . "&lang=all'>");
print_r("<div style='margin-top: 10px;'><button name='import' value='true'>Import</button></div>"); print_r("<div style='margin-top: 10px;'><button name='import' value='true'>Import</button></div>");
print_r("</form>"); print_r("</form>");
} }
// for cron, needs add to url ...&lang=all&proceed=1 // for cron, needs add to url ...&lang=all&proceed=1
if (isset($_POST['import']) || isset($_GET['proceed'])) { if (isset($_POST['import']) || isset($_GET['proceed'])) {
print_r("importing....<br><br>"); print_r("importing....<br><br>");
// Posts (via Pods framework) creator // Posts (via Pods framework) creator
(new JobsImporter($ads))->import(); (new JobsImporter($ads))->import();
} }
} else { } else {
print_r("<br>There is nothing more to do."); print_r("<br>There is nothing more to do.");
} }
if (isset($_GET['init'])) { if (isset($_GET['init'])) {
print_r("<p><h3>Importing initial data:</h3>"); print_r("<p><h3>Importing initial data:</h3>");
(new importInitData())->import(); (new importInitData())->import();
print_r("</p>"); print_r("</p>");
} }
echo "</pre>"; echo "</pre>";
} }
function getSource($env) function getSource($env)
{ {
$url = parse_url(site_url()); $url = parse_url(site_url());
$exploded = explode('.', $url['host']); $exploded = explode('.', $url['host']);
$site = strtoupper(end($exploded)); $site = strtoupper(end($exploded));
switch ($site) { switch ($site) {
case 'LT': case 'LT':
$script = 'new_wp.php'; $script = 'new_wp.php';
break; break;
case 'LV': case 'LV':
$script = 'new_wp_lv.php'; $script = 'new_wp_lv.php';
break; break;
case 'EE': case 'EE':
$script = 'new_wp_ee.php'; $script = 'new_wp_ee.php';
break; break;
} }
// Set data source path by environment type // Set data source path by environment type
if ($env === 'dev') { if ($env === 'dev') {
// $inputFile = plugin_dir_path(__FILE__) . "source_data/wp_biuro.php.xml"; // $inputFile = plugin_dir_path(__FILE__) . "source_data/wp_biuro.php.xml";
// $inputFile = "https://base.biuro.lt/_export/wp_biuro.php"; // $inputFile = "https://base.biuro.lt/_export/wp_biuro.php";
$inputFile = "https://base.biuro.lt/_export/{$script}"; $inputFile = "https://base.biuro.lt/_export/{$script}";
} elseif ($env === 'prod') { } elseif ($env === 'prod') {
// $inputFile = "http://export.biuro.lt/wp_biuro.php"; // $inputFile = "http://export.biuro.lt/wp_biuro.php";
$inputFile = "http://export.biuro.lt/{$script}"; $inputFile = "http://export.biuro.lt/{$script}";
} }
return $inputFile; return $inputFile;
} }
function getEnvironment() function getEnvironment()
{ {
// Set environment type // Set environment type
$env = 'prod'; $env = 'prod';
//Set dev env. only on dev subdomain //Set dev env. only on dev subdomain
$subDomain = strstr($_SERVER['SERVER_NAME'], '.biuro', true); $subDomain = strstr($_SERVER['SERVER_NAME'], '.biuro', true);
if ($subDomain == 'dev') { if ($subDomain == 'dev') {
$env = 'dev'; $env = 'dev';
} }
return $env; return $env;
} }
function current_location() function current_location()
{ {
if (isset($_SERVER['HTTPS']) && if (isset($_SERVER['HTTPS']) &&
($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$protocol = 'https://'; $protocol = 'https://';
} else { } else {
$protocol = 'http://'; $protocol = 'http://';
} }
return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
} }
...@@ -276,7 +276,23 @@ get_header(); ...@@ -276,7 +276,23 @@ get_header();
echo __('to', 'biuro') . ' ' . $pod->field( 'salary_to' ) . ' '; echo __('to', 'biuro') . ' ' . $pod->field( 'salary_to' ) . ' ';
endif; endif;
_e('(neto)', 'biuro'); switch ($pod->field('salary_type')):
case 'neto_month':
_e('(neto per month)', 'biuro');
break;
case 'bruto_month':
_e('(bruto per month)', 'biuro');
break;
case 'neto_hour':
_e('(neto per hour)', 'biuro');
break;
case 'bruto_hour':
_e('(bruto per hour)', 'biuro');
break;
default:
_e('(neto)', 'biuro');
break;
endswitch;
endif; endif;
......
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