File: /var/www/html/wp-content/plugins/adnkronos-feed-importer-old/adnk-importer.php
<?php
/***************************************************** */
// Function for get XML from feed and convert in array
/**************************************************** */
function adnki_feed2xml($url)
{
$log_file = dirname(__FILE__) . '/../logs/' . date('Ymd') . '_import.log';
$response_xml_data = false;
$clientHttp = new WP_Http();
$response = $clientHttp->request($url, array(
'timeout' => 30,
));
if ($response instanceof WP_Error) {
file_put_contents($log_file, " Response: " . json_encode($response) . "\n ", FILE_APPEND);
return;
}
$response_xml_data = $response['body'];
if (is_string($response_xml_data)) {
libxml_use_internal_errors(true);
$data = simplexml_load_string($response_xml_data, null, LIBXML_NOCDATA);
if (!$data) {
foreach (libxml_get_errors() as $error) {
file_put_contents($log_file, " Errore: " . $error->message . "\n", FILE_APPEND);
}
} else {
return ($data);
}
}
}
function adnki_xml2array($xmlObject, $out = array())
{
foreach ((array)$xmlObject as $index => $node) {
$out[$index] = (is_object($node)) ? adnki_xml2array($node) : $node;
}
return $out;
}
/* create post from Feed */
function adnk_create_post_from_result(
$import_post,
$post_category,
$post_status = 'publish',
$post_owner_email,
$feed_name,
$check_no_import_image,
$image_owner_email)
{
/* Create post object */
$log_file = dirname(__FILE__) . '/../logs/' . date('Ymd') . '_import.log';
$header = false;
if (!file_exists(dirname(__FILE__) . '/../logs/' . date('Ymd') . '_' . $_SERVER['SERVER_NAME'] . '_import.csv')) {
$header = true;
}
$csv_file = dirname(__FILE__) . '/../logs/' . date('Ymd') . '_' . $_SERVER['SERVER_NAME'] . '_import.csv';
if ($header) {
$row = array(
'IMPORTATAZIONE',
'PUBBLICAZIONE',
'DIFFERENZA',
'CATEGORIA',
'TITOLO',
'URL',
'SERVER',
);
file_put_contents($csv_file, implode(';', $row) . "\n", FILE_APPEND);
}
//file_put_contents( $log_file, " Valuto {$import_post->guid} \n", FILE_APPEND );
$c = get_term_by('id', $post_category, 'category');
$category = array($post_category); // category id
$post_owner = get_user_by_email($post_owner_email);
$adkn_guid_post_feed = trim(substr((string)$import_post->guid, strpos((string)$import_post->guid, "_") + 1));
$fullstring = $import_post->pubDate;
$date_article = adnk_import_tdm_get_string_between($fullstring, ',', '+');
$date_post = date('Y-m-d H:i:s', strtotime($date_article));
$new_post_id = '';
global $wpdb;
$hasdata = $wpdb->get_results("select * from $wpdb->postmeta where meta_key='adkn_guid_post_feed' and meta_value='$adkn_guid_post_feed'");
$count = count($hasdata);
file_put_contents($log_file, "{$import_post->guid}\n", FILE_APPEND);
if ($count == 0) {
try {
file_put_contents($log_file, " Importazione\n", FILE_APPEND);
$post_content = '<p>' . $import_post->description . '</p>';
$excerpt = $post_content;
$enclosures = [];
if($import_post->enclosure) {
if (!is_array($import_post->enclosure)) {
$enclosures = [$import_post->enclosure];
} else {
$enclosures = $import_post->enclosure;
}
foreach ($enclosures as $enclosure) {
$enclosure = adnki_xml2array($enclosure)['@attributes'];
if ($enclosure['type'] == 'video/mp4') {
@set_time_limit(120);
file_put_contents($log_file, " Video enclosure: " . json_encode($enclosure) . "\n", FILE_APPEND);
$video_url = $enclosure['url'];
$post_content = '[evp_embed_video url="' . $video_url . '" autoplay="true"] ' . $post_content;
}
}
}
$post_content .= '<p>---</p>';
$post_content .= '<p>' . $import_post->category . '</p>';
$post_content .= '<p>' . $import_post->author . '</p>';
$replace = array(
'(Adnkronos) - ',
'(Adnkronos)',
'Adnkronos',
'"',
' - ',
'- ',
);
$excerpt = esc_html(wp_strip_all_tags($excerpt));
$excerpt = str_replace($replace, array_fill(0, count($replace), ''), $excerpt);
$position = stripos($excerpt, '.'); //find first dot position
$excerpt = substr(trim($excerpt), 0, $position); //put two first sentences under $first_two
$metaInput = array(
'oimmei' => true,
);
if (defined('WPSEO_VERSION')) {
$metaInput['_yoast_wpseo_title'] = (string)$import_post->title;
$metaInput['_yoast_wpseo_focuskw'] = (string)$import_post->title;
$metaInput['_yoast_wpseo_metadesc'] = $excerpt;
$metaInput['_yoast_wpseo_opengraph-description'] = $excerpt;
$metaInput['_yoast_wpseo_twitter-description'] = $excerpt;
}
$new_post = array(
'post_title' => (string)$import_post->title,
'post_content' => esc_html(wp_strip_all_tags($post_content)),
'post_date' => $date_post,
'post_status' => $post_status,
'post_author' => $post_owner->ID,
'post_category' => $category,
'post_excerpt' => $excerpt,
'meta_input' => $metaInput,
);
file_put_contents($log_file, " " . json_encode($new_post) . "\n", FILE_APPEND);
$diff = date_diff(new \DateTime(), \DateTime::createFromFormat('Y-m-d H:i:s', $date_post));
$row = array(
date('Y-m-d H:i:s'),
$date_post,
$diff->format("%H:%I:%S"),
$c->name,
(string)$import_post->title,
$import_post->guid,
$_SERVER['SERVER_NAME'],
);
file_put_contents($csv_file, implode(';', $row) . "\n", FILE_APPEND);
// Insert the post into the database
$new_post_id = wp_insert_post($new_post);
file_put_contents($log_file, " post_id = {$new_post_id} \n", FILE_APPEND);
$adkn_guid_post_feed = sanitize_text_field($adkn_guid_post_feed);
update_post_meta($new_post_id, 'adkn_guid_post_feed', $adkn_guid_post_feed);
$adkn_feed_category = sanitize_text_field($import_post->category->__toString());
update_post_meta($new_post_id, 'adkn_feed_category', $adkn_feed_category);
// assign feautures image from url
foreach ($enclosures as $enclosure) {
$enclosure = adnki_xml2array($enclosure)['@attributes'];
$image_url = $enclosure['url'];
$photo_name = $import_post->title;
if (substr($image_url, -3) != 'mp4') { // && $feed_name != 'video') {
if (!$check_no_import_image == 'on') {
adnk_upload_assign_image($new_post_id, $image_url, $photo_name, $image_owner_email);
}
}
}
// $tags = array('adnkronos', $feed_name); // Array of Tags to add
// wp_set_post_tags($new_post_id, $tags); // Set tags to Post
return $new_post_id;
} catch (Exception $e) {
file_put_contents($log_file, " Errore {$e->getMessage()} \n", FILE_APPEND);
}
}
return null;
}
/*
function adnk_eventual_consistency($ids){
}
*/
/********************************************************** */
/* upload image from URL and assign as post feauture image */
/********************************************************* */
function adnk_upload_assign_image($post_id, $image_url, $photo_name, $owner_image_email)
{
$title = get_the_title($post_id);
$image_owner = get_user_by_email($owner_image_email);
$photo = new WP_Http();
$photo = $photo->request($image_url);
if (is_array($photo)) {
$attachment = wp_upload_bits($photo_name . '.jpg', null, $photo['body'], date("Y-m", strtotime($photo['headers']['last-modified'])));
$filetype = wp_check_filetype(basename($attachment['file']), null);
$postinfo = array(
'post_mime_type' => $filetype['type'],
'post_title' => $title . ' ',
'post_content' => '',
'post_status' => 'inherit',
'post_author' => $image_owner->ID,
);
$filename = $attachment['file'];
$attach_id = wp_insert_attachment($postinfo, $filename, $post_id);
if (!function_exists('wp_generate_attachment_data')) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
}
$attach_data = wp_generate_attachment_metadata($attach_id, $filename);
wp_update_attachment_metadata($attach_id, $attach_data);
set_post_thumbnail($post_id, $attach_id);
}
}
add_action('adki_add_cron_onceaday', 'adnk_import_invio_dati_statistici');
add_action('admin_action_adnkinvio', 'adnk_import_invio_dati_statistici');
/* invio dati statistici */
function adnk_import_invio_dati_statistici()
{
$consent_send_statistical_data = get_adnk_settings_option('consent_send_statistical_data');
if ($consent_send_statistical_data != 'on') {
return;
}
if (!adnk_verify_site()) {
return;
}
$array_dati = adnk_import_dati_statistici();
$yesterday = getdate(time() - 86400);
$yesterdaStr = $yesterday['year'] . '-' . $yesterday['mon'] . '-' . $yesterday['mday'];
$site_url = get_home_url();
$url = 'https://plugin.adnkronos.com/api/v1/stat/send';
foreach ($array_dati as $category => $value) {
$dati_ieri = array('domain' => $site_url,
'category' => $category,
'date' => $yesterdaStr,
'published' => $value['publish'],
'imported' => $value['publish'] + $value['draft']
);
/* chiamata per invio dati */
$response = wp_remote_post($url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.1',
'sslverify' => false,
'blocking' => true,
'headers' => array('Authorization' => 'adn83jhfk823rh2r'),
'body' => json_encode($dati_ieri),
'cookies' => array()
));
}
}
/*{
"domain": "http://www.provadomanio.it",
"date": "2022-06-30",
"category": "sport",
"imported": 36,
"published": 12
} */
function adnk_import_dati_statistici()
{
$yesterday = getdate(time() - 86400);
$y_posts_ids = array();
$agrs = array(
'post_type' => 'post',
'posts_per_page' => -1,
'date_query' => array(
array(
'year' => $yesterday['year'],
'month' => $yesterday['mon'],
'day' => $yesterday['mday'],
),
),
);
$loop = new WP_Query($agrs);
if ($loop->have_posts()) :
while ($loop->have_posts()) : $loop->the_post();
$categoria = get_post_meta(get_the_ID(), 'adkn_feed_category', true);
if (!empty($categoria)) {
if (!array_key_exists($categoria, $y_posts_ids)) {
$y_posts_ids[$categoria] = array();
}
if (get_post_status() == 'publish') {
$y_posts_ids[$categoria]['publish']++;
} else {
$y_posts_ids[$categoria]['draft']++;
}
}
endwhile;
endif;
return $y_posts_ids;
}
function adnk_verify_site()
{
$site_url = get_home_url();
$url = 'https://plugin.adnkronos.com/api/v1/site/verify';
$response = wp_remote_post($url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.1',
//'sslverify' => false,
'blocking' => true,
'headers' => array(),
'body' => array('domain' => $site_url),
'cookies' => array()
));
//var_dump($response);
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
update_option('adn_site_active', 0, true);
return false;
} else {
$body = $response['body'];
$esito = json_decode($body);
if ($esito->response == 'OK') {
update_option('adn_site_active', 1, true);
return true;
}
if ($esito->response == 'KO') {
update_option('adn_site_active', 0, true);
return true;
}
update_option('adn_site_active', 1, true);
return false;
}
}
add_action('admin_action_adnkverify', 'adnk_verify_site');
if (is_admin()) {
remove_action('admin_action_adnk_import_now', 'adnk_import_feed');
add_action('admin_action_adnk_import_now', 'adnk_import_feed');
}
function adnk_import_now()
{
$log_file = dirname(__FILE__) . '/../logs/' . date('Ymd') . '_import.log';
$orario = (new DateTime())->format('Y-m-d H:i:s');
file_put_contents($log_file, $orario . " Inizio importazione in adnk_import_now()\n", FILE_APPEND);
citycomm_feed_importer();
//torno alla pagina chiamante
//$url = wp_get_raw_referer();
//wp_redirect( site_url() . '/wp-admin/edit.php' );
}
function adnk_import_tdm_get_string_between($string, $start, $end)
{
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) {
return '';
}
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}