echo_messages = $echo_messages;
$this->dataupdate_interval = atkp_options::$loader->get_cache_duration();
$this->datacheck_enabled = atkp_options::$loader->get_check_enabled();
$this->datacheck_interval = atkp_options::$loader->get_notification_interval();
$this->csvupdate_interval = atkp_options::$loader->get_access_csv_intervall();
}
private $echo_messages = false;
private $datacheck_enabled = false;
private $datacheck_interval = 4320;
private $dataupdate_interval = 1440;
private $csvupdate_interval = 1440;
private $minutes = 0;
public function clear_deleted_offers( $activityname ) {
$this->send_message( '*** delete offers started ***' );
$lastimport = atkp_options::$loader->get_cron_lastclean();
if ( $lastimport != '' ) {
$diff_lastimport = round( abs( time() - $lastimport ) / 60, 2 );
//wenn keine fortsetzung, dann prüfen
if ( $diff_lastimport <= $this->csvupdate_interval ) {
$this->send_message( 'next delete (hours): ' . round( ( $this->csvupdate_interval - $diff_lastimport ) / 60, 2 ) );
return true;
} else {
$this->send_message( 'delete now' );
}
}
$updatetime = $this->get_datatime();
$offertablehelper = new atkp_offertable_helper();
$productids = $offertablehelper->get_productids();
$productsnotfound = array();
$y = 1;
foreach ( $productids as $productid ) {
$productid = intval( $productid );
if ( $productid == 0 ) {
continue;
}
$post = get_post( $productid );
if ( ! $post ) {
array_push( $productsnotfound, $productid );
}
if ( $y % 25 == 0 ) {
self::update_lastactivity( $activityname );
}
$y ++;
}
if ( count( $productsnotfound ) > 0 ) {
$offertablehelper->delete_productids( $productsnotfound );
}
$listids = $offertablehelper->get_listids();
$listsnotfound = array();
$y = 1;
foreach ( $listids as $listid ) {
$listid = intval( $listid );
if ( $listid == 0 ) {
continue;
}
$post = get_post( $listid );
if ( ! $post ) {
array_push( $listsnotfound, $listid );
}
if ( $y % 25 == 0 ) {
self::update_lastactivity( $activityname );
}
$y ++;
}
if ( count( $listsnotfound ) > 0 ) {
$offertablehelper->delete_listids( $listsnotfound );
}
update_option( ATKP_PLUGIN_PREFIX . '_cron_lastclean', time() );
$this->send_message( '*** delete offers finished ***' );
}
public function do_multiwork( $iswpcronjob = false, $actual_link, $mode = '' ) {
$this->send_message( '### multi cronjob started ###' );
//todo: angebote unabhängig vom shop für jedes produkt generieren und danach die shops aktualsiieren
$activityname = ATKP_PLUGIN_PREFIX . '_cron_lastactivity';
if ( $mode == '' || $mode == 'offerupdate' ) {
//in dem zuge auch gleich die produkte durchgehen und angebote vorfertigen
$this->clear_deleted_offers( $activityname );
}
$shops = atkp_shop::get_list( null, true );
$blankshop = new atkp_shop();
$blankshop->id = - 1;
array_push( $shops, $blankshop );
foreach ( $shops as $shop ) {
$params = array();
$params['shopid'] = $shop->id;
$params['mode'] = $mode;
$this->send_message( 'shop reload: ' . $actual_link . '?shopid=' . $shop->id . '&mode=' . $mode );
$this->curl_request_async( $actual_link . '?shopid=' . $shop->id . '&mode=' . $mode );
}
if ( $this->datacheck_enabled ) {
$this->do_data_check( $activityname );
}
$this->send_message( '### multi cronjob finished ###' );
if ( ! $iswpcronjob ) {
echo 'OK';
exit;
}
}
public function do_work( $iswpcronjob = false, $mode = '' ) {
$this->send_message( '### cronjob started ###' );
$shopidget = ATKPTools::get_get_parameter( 'shopid', 'int' );
$shopidarray = null;
if ( $shopidget == null || $shopidget == '0' ) {
$shopidget = '';
} else {
$shopidarray = array( $shopidget );
}
$activityname = ATKP_PLUGIN_PREFIX . '_cron_lastactivity' . $shopidget;
$meessage = '';
if ( ! self::allow_lastactivity( $activityname, $iswpcronjob, $meessage ) ) {
$this->send_message( $meessage );
exit;
}
self::update_lastactivity( $activityname );
//danach werden die Produkte aktualisiert
if ( $mode == '' || $mode == 'productupdate' ) {
$this->do_product_update( $activityname, $shopidarray );
}
//und dann die Listen
if ( $mode == '' || $mode == 'listupdate' ) {
$this->do_list_update( $activityname, $shopidarray );
}
self::update_lastactivity( $activityname, true );
$this->send_message( '### cronjob finished ###' );
if ( ! $iswpcronjob ) {
echo 'OK';
exit;
}
}
function curl_request_async( $url ) {
$opts = array(
'http' =>
array(
'method' => 'GET',
'header' => "Content-Type: text/html\r\n",
'content' => '',
'timeout' => 1
)
);
$context = stream_context_create( $opts );
$result = file_get_contents( $url, false, $context, - 1, 40000 );
}
public static function allow_lastactivity( $activityname, $iswpcronjob, &$message ) {
$lastactivity = get_option( $activityname );
$from = atkp_options::$loader->get_cron_from();
$to = atkp_options::$loader->get_cron_to();
if($from != '' && $to != '' && ($from != '00:00' || $to != '00:00')) {
$begin = strtotime($from);
$end = strtotime($to);
$now = time();
if($begin > $end)
$end = $end + 86400;
if($now >= $begin && $now <= $end) {
echo 'time '.date( 'd.m.y H:i:s', $now ).' is okay ('.date( 'd.m.y H:i:s', $begin ) . ' and ' . date( 'd.m.y H:i:s', $end ).')... lets do the work';
$message = 'time '.date( 'd.m.y H:i:s', $now ).' is okay ('.date( 'd.m.y H:i:s', $begin ) . ' and ' . date( 'd.m.y H:i:s', $end ).')... lets do the work';
} else {
$message = 'time '.date( 'd.m.y H:i:s', $now ).' is NOT between ' . date( 'd.m.y H:i:s', $begin ) . ' and ' . date( 'd.m.y H:i:s', $end ) . ' - blocking';
return false;
}
}
if ( ! isset( $lastactivity ) || $lastactivity == null || $lastactivity == '' ) {
$message = 'lastactivity is null - lets do the work';
return true;
} else {
$message = 'last activity: ' . date( 'd.m.y H:i:s', $lastactivity );
$diff_lastactivity = round( abs( time() - $lastactivity ) / 60, 2 );
if ( $diff_lastactivity <= 5 ) {
$message = '5 minutes blocking (a task is already active): ' . ( 5 - $diff_lastactivity );
return false;
} else {
$message = 'no activity the last minutes - that is great... lets do the work';
return true;
}
}
}
public static function update_lastactivity( $activityname, $clear = false ) {
$now = time();
if ( $clear ) {
delete_option( $activityname );
} else {
update_option( $activityname, $now );
}
//echo date('H:i:s').' - '. 'lastactivity updated: '.date('H:i:s', $now).'
'.PHP_EOL;
return $now;
}
private function do_csv_import( $activityname, $shopids = null ) {
try {
$this->send_message( '*** csv cronjob for datacheck started ***' );
$lastimport = atkp_options::$loader->get_cron_csv_lastimport();
$lastimport_finished = atkp_options::$loader->get_cron_csv_lastimportfinished();
$isresume = $lastimport_finished != 1;
update_option( ATKP_PLUGIN_PREFIX . '_cron_csv_lastimportfinished', 0 );
$shopidfilter = null;
if ( $shopids != null ) {
$shopidfilter = array(
'post__in' => $shopids
);
}
$updatetime = $this->get_csvtime();
global $wpdb;
$posttype = ATKP_SHOP_POSTTYPE;
$shoptypefield = ATKP_SHOP_POSTTYPE . '_access_webservice';
$updatedonfield = ATKP_SHOP_POSTTYPE . '_updatedon';
$sql = "
SELECT SQL_CALC_FOUND_ROWS {$wpdb->posts}.ID
FROM {$wpdb->posts}
INNER JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '$shoptypefield' and {$wpdb->postmeta}.meta_value = '7')
LEFT JOIN {$wpdb->postmeta} AS mt3 ON ( {$wpdb->posts}.ID = mt3.post_id AND mt3.meta_key = '$updatedonfield')
WHERE
{$wpdb->posts}.post_type = '$posttype' and ({$wpdb->posts}.post_status = 'publish' OR {$wpdb->posts}.post_status = 'draft')
AND ( mt3.post_id IS NULL OR ( mt3.meta_value = '' ) OR ( CAST(mt3.meta_value AS SIGNED) < $updatetime ) )
GROUP BY {$wpdb->posts}.ID
ORDER BY CAST(mt3.meta_value AS SIGNED) ASC
LIMIT 0, 10";
$posts_found = $wpdb->get_results( $sql, OBJECT);
$this->send_message( 'csv posts found: ' . count( $posts_found ) );
//wenn csv feeds vorhanden sind, dann initialisiere den csv cronjob.
if ( $posts_found && count( $posts_found ) > 0 ) {
$postcount = 0;
foreach ( $posts_found as $xx ) {
try {
$csvimport = get_post((int)$xx->ID);
$this->send_message( 'import csv: ' . $csvimport->ID . ' - ' . $csvimport->post_title );
$csvprovider = new atkp_shop_provider_csv();
$csvprovider->import_csv( $csvimport->ID, true, $isresume, array(
'atkp_cronjob_new',
'update_lastactivity'
), $activityname );
unset( $csvprovider );
ATKPTools::set_post_setting( $csvimport->ID, ATKP_SHOP_POSTTYPE . '_updatedon', ATKPTools::get_current_utc() );
ATKPTools::set_post_setting( $csvimport->ID, ATKP_SHOP_POSTTYPE . '_access_message', null);
$this->send_message( 'import finished csv: ' . $csvimport->ID );
$postcount ++;
} catch ( Exception $e ) {
$this->send_message( 'import csv error (' . $csvimport->ID . ') ' . $e->getMessage() );
ATKPTools::set_post_setting( $csvimport->ID, ATKP_SHOP_POSTTYPE . '_access_message', 'last import: '.$e->getMessage());
}
}
//wenn noch eine csv-liste da ist, dann soll das script beendet werden
if ( count( $posts_found ) == 10 ) {
$this->send_message( 'count difference: ' . count( $posts_found ) . ' != ' . $postcount );
return false;
}
}
self::update_lastactivity( $activityname );
update_option( ATKP_PLUGIN_PREFIX . '_cron_csv_lastimportfinished', 1 );
update_option( ATKP_PLUGIN_PREFIX . '_cron_csv_lastimport', ATKPTools::get_current_utc() );
$this->send_message( '*** csv cronjob finished ***' );
return true;
} catch ( Exception $e ) {
ATKPLog::LogError( $e->getMessage() );
}
}
private function get_datatime() {
$updatetime = time();
$this->send_message( 'cnt: ' . $updatetime );
if ( $this->dataupdate_interval > 60 ) {
$updatetime = strtotime( '-' . ( $this->dataupdate_interval / 60 ) . ' hours' );
} else {
$updatetime = strtotime( '-' . ( $this->dataupdate_interval ) . ' min' );
}
$this->send_message( 'cnt: ' . $updatetime );
$updatetime = $this->get_time( $updatetime, 'timestamp' );
$this->send_message( 'timestamp: ' . date( 'd.m.y H:m:s', $updatetime ) );
return $updatetime;
}
private function get_csvtime() {
$updatetime = time();
$this->send_message( 'cnt: ' . $updatetime );
if ( $this->csvupdate_interval > 60 ) {
$updatetime = strtotime( '-' . ( $this->csvupdate_interval / 60 ) . ' hours' );
} else {
$updatetime = strtotime( '-' . ( $this->csvupdate_interval ) . ' min' );
}
$this->send_message( 'cnt: ' . $updatetime );
$updatetime = $this->get_time( $updatetime, 'timestamp' );
$this->send_message( 'timestamp: ' . date( 'd.m.y H:m:s', $updatetime ) );
return $updatetime;
}
/**
* Aktualisiert die AT-Angebote welche vorher von do_product_update zurückgesetzt wurden
*
* @param bool $isresume Gibt an, ob der Cronjob nach einem Abbruch wieder ausgeführt wird
*/
public function do_offer_update( $activityname, $shopids ) {
try {
$this->send_message( '*** offer update started ***' );
$lastimport = atkp_options::$loader->get_cron_offer_lastimport();
$offerservice = new atkp_offerservice();
$offertablehelper = new atkp_offertable_helper();
$idtypes = array( 'ASIN', 'EAN' );
$shops = atkp_shop::get_list( null, true );
$y = 1;
foreach ( $idtypes as $idtype ) {
foreach ( $shops as $shop ) {
//hier darf nicht auf enablepricecomparison abgefragt werden weil sonst manuelle produkte nicht aktualisiert werden
if ( $shopids != null ) {
if ( $shop == null || ! in_array( $shop->id, $shopids ) ) {
continue;
}
}
$posts_exists = true;
$page = 1;
while ( $posts_exists && $page <= 3 ) {
$offers = $offertablehelper->get_offers_for_update( $shop->id, $idtype, $page );
$this->send_message( 'offer shopid: ' . $shop->id . ' (' . $shop->get_title() . '), key: ' . $idtype . ', products found: ' . count( $offers ) );
if ( count( $offers ) > 0 ) {
$offerservice->update_offers( $shop, $offers, $idtype );
}
$posts_exists = count( $offers ) >= 10;
//$page ++;
//resume marker, save position each 50 entries to have a resume mark
if ( $y % 25 == 0 ) {
self::update_lastactivity( $activityname );
}
$y ++;
}
}
}
self::update_lastactivity( $activityname );
/* Restore original Post Data */
wp_reset_postdata();
//der zweite teil aktualisiert die Angeobte
update_option( ATKP_PLUGIN_PREFIX . '_cron_offer_lastimport', time() );
$this->send_message( '*** offer update cronjob finished ***' );
} catch ( Exception $e ) {
ATKPLog::LogError( $e->getMessage() );
}
}
/**
* Kopiert die AT-Produkte nach WooCommerce (oder anderen Export-Provider), wenn das flag _isexported auf false oder null steht
*
* @param $isresume
*/
public
function do_product_export(
$activityname
) {
try {
$this->send_message( '*** product export started ***' );
$lastimport = atkp_options::$loader->get_cron_product_lastexport();
$y = 1;
//TODO: nur produkte laden die noch nicht aktualisiert wurden
$productservice = new atkp_productservice();
$posts_found = array(
'posts_per_page' => 10,
'post_status' => array( 'draft', 'publish' ),
'post_type' => ATKP_PRODUCT_POSTTYPE,
'suppress_filters' =>true,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => ATKP_PRODUCT_POSTTYPE . '_iswoocommerce',
'type' => 'numeric',
'compare' => '==',
'value' => 1,
),
array(
'relation' => 'OR',
array(
'key' => ATKP_PRODUCT_POSTTYPE . '_isexported',
'type' => 'numeric',
'compare' => '==',
'value' => 0,
),
array(
'key' => ATKP_PRODUCT_POSTTYPE . '_isexported',
'compare' => '==',
'value' => '',
),
array(
'key' => ATKP_PRODUCT_POSTTYPE . '_isexported',
'compare' => 'NOT EXISTS',
)
)
)
);
$posts_exists = true;
while ( $posts_exists ) {
$posts = new WP_Query( $posts_found );
$post_count = $posts->found_posts;
$productids = array();
while ( $posts->have_posts() ) {
$posts->the_post();
$post_id = get_the_ID();
array_push( $productids, $post_id );
}
if ( count( $productids ) > 0 ) {
$productservice->export_products( $productids );
}
//resume marker, save position each 50 entries to have a resume mark
if ( $y % 25 == 0 ) {
self::update_lastactivity( $activityname );
}
$y ++;
$posts_exists = $posts->max_num_pages > 1;
}
self::update_lastactivity( $activityname );
/* Restore original Post Data */
wp_reset_postdata();
update_option( ATKP_PLUGIN_PREFIX . '_cron_product_lastexport', time() );
$this->send_message( '*** product export finished ***' );
} catch ( Exception $e ) {
ATKPLog::LogError( $e->getMessage() );
}
}
/**
* Aktualisiert die AT-Produktdaten und setzt die Angebote für das Update zurück
*
* @param bool $isresume Gibt an, ob der Cronjob nach einem Abbruch wieder ausgeführt wird
*/
public
function do_product_update(
$activityname, $shopids
) {
try {
$this->send_message( '*** product update started ***' );
$lastimport = atkp_options::$loader->get_cron_product_lastimport();
//TODO: nur produkte laden die noch nicht aktualisiert wurden
//tabelle für die angebote anlegen
$atkp_offertable_helper = new atkp_offertable_helper();
$productservice = new atkp_productservice();
$updatetime = $this->get_datatime();
$errors = array();
//der erste teil aktualisiert die Produkte
$idtypes = array( 'ASIN', 'EAN', 'TITLE', 'ARTICLENUMBER' );
$shops = atkp_shop::get_list( null, true );
$y = 1;
array_push( $shops, null );
foreach ( $idtypes as $idtype ) {
foreach ( $shops as $shop ) {
$posts_exists = true;
if ( $shopids != null ) {
if ( in_array( - 1, $shopids ) && $shop == null ) {
//manuelles produkt
} else {
if ( $shop == null || ! in_array( $shop->id, $shopids ) ) {
continue;
}
}
}
while ( $posts_exists ) {
global $wpdb;
$posttype = ATKP_PRODUCT_POSTTYPE;
$shopfield = ATKP_PRODUCT_POSTTYPE . '_shopid';
$asintypefield = ATKP_PRODUCT_POSTTYPE . '_asintype';
$updatedonfield = ATKP_PRODUCT_POSTTYPE . '_updatedon';
if($shop == null)
$fieldjoin = " AND ({$wpdb->postmeta}.post_id IS NULL OR ( {$wpdb->postmeta}.meta_value = '' ))";
else
$fieldjoin = " AND ( {$wpdb->postmeta}.meta_value = '{$shop->id}' )";
$sql = "
SELECT SQL_CALC_FOUND_ROWS {$wpdb->posts}.ID
FROM {$wpdb->posts}
LEFT JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '$shopfield')
LEFT JOIN {$wpdb->postmeta} AS mt1 ON ( {$wpdb->posts}.ID = mt1.post_id AND mt1.meta_key = '$asintypefield')
LEFT JOIN {$wpdb->postmeta} AS mt3 ON ( {$wpdb->posts}.ID = mt3.post_id AND mt3.meta_key = '$updatedonfield')
WHERE
{$wpdb->posts}.post_type = '$posttype' and ({$wpdb->posts}.post_status = 'publish' OR {$wpdb->posts}.post_status = 'draft')
$fieldjoin
AND ( mt1.post_id IS NULL OR ( mt1.meta_value = '' ) OR (mt1.meta_value = '$idtype' ) )
AND ( mt3.post_id IS NULL OR ( mt3.meta_value = '' ) OR ( CAST(mt3.meta_value AS SIGNED) < $updatetime ) )
GROUP BY {$wpdb->posts}.ID
ORDER BY CAST(mt3.meta_value AS SIGNED) ASC
LIMIT 0, 10";
$posts_found = $wpdb->get_results( $sql, OBJECT);
if ( $shop == null ) {
$this->send_message( 'manual products, key: ' . $idtype . ', products found: ' . count($posts_found) );
} else {
$this->send_message( 'shopid: ' . $shop->id . ' (' . $shop->get_title() . '), key: ' . $idtype . ', products found: ' . count($posts_found) );
}
$productids = array();
foreach($posts_found as $xx) {
array_push( $productids, (int)$xx->ID );
}
if ( count( $productids ) > 0 ) {
$productservice->update_products( $shop, $productids, $idtype );
}
//resume marker, save position each 50 entries to have a resume mark
if ( $y % 25 == 0 ) {
self::update_lastactivity( $activityname );
}
$y ++;
$posts_exists = count($posts_found) >= 10;
}
}
}
/* Restore original Post Data */
wp_reset_postdata();
self::update_lastactivity( $activityname );
//check invalid shopids
$allshopids = array();
foreach ( $shops as $shop ) {
array_push( $allshopids, $shop == null ? '' : $shop->id );
}
$posts_exists = true;
while ( $posts_exists ) {
$posts_found = array(
'posts_per_page' => 10,
'post_status' => array( 'draft', 'publish' ),
'post_type' => ATKP_PRODUCT_POSTTYPE,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => ATKP_PRODUCT_POSTTYPE . '_shopid',
'compare' => 'NOT IN',
'value' => $allshopids,
),
array(
'relation' => 'OR',
array(
'key' => ATKP_PRODUCT_POSTTYPE . '_updatedon',
'type' => 'numeric',
'compare' => '<',
'value' => $updatetime,
),
array(
'key' => ATKP_PRODUCT_POSTTYPE . '_updatedon',
'compare' => '==',
'value' => '',
),
array(
'key' => ATKP_PRODUCT_POSTTYPE . '_updatedon',
'compare' => 'NOT EXISTS',
)
)
)
);
$posts = new WP_Query( $posts_found );
$post_count = $posts->found_posts;
$productids = array();
while ( $posts->have_posts() ) {
$posts->the_post();
$post_id = get_the_ID();
array_push( $productids, $post_id );
}
if ( count( $productids ) > 0 ) {
$productservice->mark_asinvalid( $productids );
}
//resume marker, save position each 50 entries to have a resume mark
if ( $y % 25 == 0 ) {
self::update_lastactivity( $activityname );
}
$y ++;
$posts_exists = $posts->max_num_pages > 1;
/* Restore original Post Data */
wp_reset_postdata();
}
self::update_lastactivity( $activityname );
//der zweite teil aktualisiert die Angeobte
update_option( ATKP_PLUGIN_PREFIX . '_cron_product_lastimport', time() );
$this->send_message( '*** product update cronjob finished ***' );
} catch ( Exception $e ) {
ATKPLog::LogError( $e->getMessage() );
}
}
function get_time( $time, $type, $gmt = 0 ) {
switch ( $type ) {
case 'mysql':
return ( $gmt ) ? gmdate( $time, 'Y-m-d H:i:s' ) : gmdate( $time, 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) );
case 'timestamp':
return ( $gmt ) ? $time : $time + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
default:
return ( $gmt ) ? date( $time, $type ) : date( $time, $type, $time + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
}
}
public
function do_list_update(
$activityname,
$shopids
) {
try {
$this->send_message( '*** list update cronjob started ***' );
$lastimport = atkp_options::$loader->get_cron_list_lastimport();
if ( $lastimport != '' ) {
$diff_lastimport = round( abs( time() - $lastimport ) / 60, 2 );
//wenn keine fortsetzung, dann prüfen
if ( $diff_lastimport <= $this->dataupdate_interval ) {
//$this->send_message('next import (hours): '. round(($this->dataupdate_interval- $diff_lastimport) / 60,2));
//return;
} else {
$this->send_message( 'import now' );
}
}
//TODO: nur listen laden die noch nicht aktualisiert wurden
$listservice = new atkp_listservice();
$cron = new atkp_cronjob( array() );
$updatetime = $this->get_datatime();
$shops = atkp_shop::get_list( null, true );
$y = 1;
array_push( $shops, null );
foreach ( $shops as $shop ) {
if ( $shopids != null ) {
if ( in_array( - 1, $shopids ) && $shop == null ) {
//manuelles produkt
} else {
if ( $shop == null || ! in_array( $shop->id, $shopids ) ) {
continue;
}
}
}
$errors = array();
$posts_exists = true;
while ( $posts_exists ) {
global $wpdb;
$posttype = ATKP_LIST_POSTTYPE;
$shopfield = ATKP_LIST_POSTTYPE . '_shopid';
$updatedonfield = ATKP_LIST_POSTTYPE . '_updatedon';
if($shop == null)
$fieldjoin = " AND ({$wpdb->postmeta}.post_id IS NULL OR ( {$wpdb->postmeta}.meta_value = '' ))";
else
$fieldjoin = " AND ( {$wpdb->postmeta}.meta_value = '{$shop->id}' )";
$sql = "
SELECT SQL_CALC_FOUND_ROWS {$wpdb->posts}.ID
FROM {$wpdb->posts}
LEFT JOIN {$wpdb->postmeta} ON ( {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = '$shopfield')
LEFT JOIN {$wpdb->postmeta} AS mt3 ON ( {$wpdb->posts}.ID = mt3.post_id AND mt3.meta_key = '$updatedonfield')
WHERE
{$wpdb->posts}.post_type = '$posttype' and ({$wpdb->posts}.post_status = 'publish' OR {$wpdb->posts}.post_status = 'draft')
$fieldjoin
AND ( mt3.post_id IS NULL OR ( mt3.meta_value = '' ) OR ( CAST(mt3.meta_value AS SIGNED) < $updatetime ) )
GROUP BY {$wpdb->posts}.ID
ORDER BY CAST(mt3.meta_value AS SIGNED) ASC
LIMIT 0, 10";
$posts_found = $wpdb->get_results( $sql, OBJECT);
if ( $shop == null ) {
$this->send_message( 'manual list, lists found: ' . count($posts_found) );
} else {
$this->send_message( 'shopid: ' . $shop->id . ' (' . $shop->get_title() . '), lists found: ' . count($posts_found) );
}
$listids = array();
foreach ( $posts_found as $xx ) {
array_push( $listids, $xx->ID );
}
if ( count( $listids ) > 0 ) {
$listservice->update_lists( $shop, $listids );
}
//resume marker, save position each 50 entries to have a resume mark
if ( $y % 25 == 0 ) {
self::update_lastactivity( $activityname );
}
$y ++;
$posts_exists = count($posts_found) >= 10;
/* Restore original Post Data */
wp_reset_postdata();
}
}
self::update_lastactivity( $activityname );
//check invalid shopids
$allshopids = array();
foreach ( $shops as $shop ) {
array_push( $allshopids, $shop == null ? '' : $shop->id );
}
$posts_exists = true;
while ( $posts_exists ) {
$posts_found = array(
'posts_per_page' => 10,
'post_status' => array( 'draft', 'publish' ),
'post_type' => ATKP_LIST_POSTTYPE,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => ATKP_LIST_POSTTYPE . '_shopid',
'compare' => 'NOT IN',
'value' => $allshopids,
),
array(
'relation' => 'OR',
array(
'key' => ATKP_LIST_POSTTYPE . '_updatedon',
'type' => 'numeric',
'compare' => '<',
'value' => $updatetime,
),
array(
'key' => ATKP_LIST_POSTTYPE . '_updatedon',
'compare' => '==',
'value' => '',
),
array(
'key' => ATKP_LIST_POSTTYPE . '_updatedon',
'compare' => 'NOT EXISTS',
)
)
)
);
$posts = new WP_Query( $posts_found );
$post_count = $posts->found_posts;
$listids = array();
while ( $posts->have_posts() ) {
$posts->the_post();
$post_id = get_the_ID();
array_push( $listids, $post_id );
}
if ( count( $listids ) > 0 ) {
$listservice->mark_asinvalid( $listids );
}
//resume marker, save position each 50 entries to have a resume mark
if ( $y % 25 == 0 ) {
self::update_lastactivity( $activityname );
}
$y ++;
$posts_exists = $posts->max_num_pages > 1;
/* Restore original Post Data */
wp_reset_postdata();
}
self::update_lastactivity( $activityname );
update_option( ATKP_PLUGIN_PREFIX . '_cron_list_lastimport', time() );
$this->send_message( '*** list update cronjob finished ***' );
} catch ( Exception $e ) {
ATKPLog::LogError( $e->getMessage() );
}
}
private
function do_data_check(
$activityname
) {
try {
$this->send_message( '*** data check cronjob started ***' );
if ( ! $this->datacheck_enabled ) {
$this->send_message( 'data check disabled' );
return;
}
$lastimport = atkp_options::$loader->get_cron_lastdatacheck();
if ( $lastimport != '' ) {
$diff_lastimport = round( abs( time() - $lastimport ) / 60, 2 );
//wenn keine fortsetzung, dann prüfen
if ( $diff_lastimport <= $this->datacheck_interval ) {
$this->send_message( 'next check (hours): ' . round( ( $this->datacheck_interval - $diff_lastimport ) / 60, 2 ) );
return;
} else {
$this->send_message( 'import now' );
}
}
try {
//TODO: nur listen laden die noch nicht aktualisiert wurden
$cron = new atkp_cronjob( array() );
$cron->do_this_weekly();
} catch ( Exception $e ) {
ATKPLog::LogError( $e->getMessage() );
}
update_option( ATKP_PLUGIN_PREFIX . '_cron_lastdatacheck', time() );
$this->send_message( '*** data check cronjob finished ***' );
} catch ( Exception $e ) {
ATKPLog::LogError( $e->getMessage() );
}
}
public
function send_message(
$message
) {
if ( $this->echo_messages ) {
echo date( 'H:i:s' ) . ' - ' . $message . '
' . PHP_EOL;
}
if ( ATKPLog::$logenabled ) {
ATKPLog::LogDebug( $message );
}
}
}
?>