/?php
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'woocommerce' ) ) return;
if ( ! class_exists( 'WP_Async_Request', false ) ) {
include_once dirname( WC_PLUGIN_FILE ) . '/includes/libraries/wp-async-request.php';
}
if ( ! class_exists( 'WP_Background_Process', false ) ) {
include_once dirname( WC_PLUGIN_FILE ) . '/includes/libraries/wp-background-process.php';
}
require_once plugin_dir_path( __FILE__ ) . '../inc/common.php';
class Crown_Products_Background_Cleanup extends WP_Background_Process {
protected $action = 'crown_products_cleanup';
protected function task( $item ) {
$log_file = $this->prepare_log_file();
$output = file_get_contents( $log_file );
if ( ! is_array( $item ) && ! isset( $item['product_id'] ) ) {
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Product ID is empty.' . "\n";
file_put_contents( $log_file, $output );
return false;
}
$product_id = $item['product_id'];
$force = $item['force'] ?? FALSE;
$collection_id = get_post_meta( $product_id, 'product_collection_id', true );
if ( !$collection_id ) {
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Product ID: ' . $product_id . ' does not have collection ID.' . "\n";
file_put_contents( $log_file, $output );
$this->process_product($product_id, $force);
return false;
}
$collection = Crown_Amplifi_Api::get_collection( $collection_id );
if ( is_object( $collection ) && property_exists( $collection, 'id' ) ) {
$nsi_default_region_id = get_amplify_region_id();
$is_nsi_default = property_exists( $collection, 'region_ids' ) && is_array( $collection->region_ids ) && in_array( $nsi_default_region_id, $collection->region_ids );
if ( !$is_nsi_default ) {
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Collection ' . $collection_id . ' is not in region '. $nsi_default_region_id .'. Product ID: ' . $product_id . "\n";
file_put_contents( $log_file, $output );
$this->process_product($product_id, $force);
} elseif (get_post_status($product_id) === 'draft') {
wp_update_post( array(
'ID' => $product_id,
'post_status' => 'publish',
) );
if ( defined( 'TMWNI_DIR' ) ) {
require_once(TMWNI_DIR . 'inc/item.php');
$netsuiteClient = new ItemClient();
$netsuiteClient->searchItemBySkuUpdateInventory( get_post_meta( $product_id, '_sku', true ), $product_id, 'all' );
}
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Product ID published and synced with Netsuite: ' . $product_id . "\n";
file_put_contents( $log_file, $output );
}
} else {
if ( is_object( $collection ) && property_exists( $collection, 'message' ) ) {
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Collection ' . $collection_id . ' load failed. Message: "' . $collection->message . '". Product ID: ' . $product_id . "\n";
file_put_contents( $log_file, $output );
if ( $collection->message === 'Not Found' ){
$this->process_product($product_id, $force);
}
return false;
}
if ( isset( $item['retry'] ) && $item['retry'] === false ) {
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Collection ' . $collection_id . ' check failed. Need to retry. Product ID: ' . $product_id . "\n";
file_put_contents( $log_file, $output );
return array( 'product_id' => $product_id, 'retry' => true );
} else {
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Collection ' . $collection_id . ' check failed. Product ID: ' . $product_id . "\n";
file_put_contents( $log_file, $output );
}
}
return false;
}
protected function process_product($product_id, $force = FALSE) {
global $wpdb;
$enable_disabling_products = get_field('enable_disabling_products_auto_sync', 'option') ?? TRUE;
$log_file = $this->prepare_log_file();
$output = file_get_contents( $log_file );
if (!$enable_disabling_products) {
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Products are not allowed to be deleted.' . "\n";
file_put_contents( $log_file, $output );
return;
}
// Check if product is in an order, submittal, or quote
$in_order = $wpdb->get_var("SELECT COUNT(*) FROM wp_wc_order_product_lookup WHERE product_id = {$product_id}" );
if ( $in_order ) {
disable_product($product_id);
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Product ID: ' . $product_id . ' is in order. Disabled for purchase and search.' . "\n";
file_put_contents( $log_file, $output );
return;
}
$in_submittal = $wpdb->get_var(" SELECT COUNT(*) FROM wp_submittal_products WHERE product_id = {$product_id}" );
if ( $in_submittal ) {
disable_product($product_id);
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Product ID: ' . $product_id . ' is in submittal. Disabled for purchase and search.' . "\n";
file_put_contents( $log_file, $output );
return;
}
$in_quote = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->postmeta} WHERE meta_key = 'quote_contents' AND meta_value LIKE '%product_id\";i:{$product_id}%'" );
if ( $in_quote ) {
disable_product($product_id);
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Product ID: ' . $product_id . ' is in quote. Disabled for purchase and search.' . "\n";
file_put_contents( $log_file, $output );
return;
}
if ($force) {
wp_delete_post( $product_id, true );
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Product ID: ' . $product_id . ' DELETED.' . "\n";
} else {
disable_product($product_id);
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Product ID: ' . $product_id . ' disabled for purchase and search.' . "\n";
}
file_put_contents( $log_file, $output );
}
protected function prepare_log_file() {
$file_dir = wp_upload_dir();
$date_current = date("Y-m-d");
$folder_name = 'amplify-logs/amplify-product-cleanup';
$path_folder = $file_dir['basedir'] . '/' . $folder_name;
$file_name = $path_folder . '/products_cleanup-' . $date_current . '.log';
if ( !file_exists($path_folder)) {
mkdir( $path_folder, 0755, true );
}
if(!file_exists($file_name)){
$log_file = fopen($file_name, 'w');
chmod($file_name, 0664);
fclose($log_file);
}
return $file_name;
}
protected function complete() {
parent::complete();
$log_file = $this->prepare_log_file();
$output = file_get_contents( $log_file );
$output .= '[' . date( 'Y-m-d H:i:s' ) . '] Finished processing.' . "\n";
file_put_contents( $log_file, $output );
}
}
Warning: Undefined array key 0 in /var/www/html/nsi-typesense/wp-content/plugins/crown-shop/classes/class-crown-shop-custom-roles.php on line 189
Fatal error: Uncaught Exception: DateTimeZone::__construct(): Unknown or bad timezone () in /var/www/html/nsi-typesense/wp-content/plugins/crown-shop/classes/class-crown-shop-products-importer.php:3419
Stack trace:
#0 /var/www/html/nsi-typesense/wp-content/plugins/crown-shop/classes/class-crown-shop-products-importer.php(3419): DateTimeZone->__construct()
#1 /var/www/html/nsi-typesense/wp-content/plugins/crown-shop/classes/class-crown-shop-products-importer.php(3238): Crown_Shop_Products_Import::get_next_sync_time()
#2 /var/www/html/nsi-typesense/wp-includes/class-wp-hook.php(324): Crown_Shop_Products_Import::init_product_background_cleanup()
#3 /var/www/html/nsi-typesense/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#4 /var/www/html/nsi-typesense/wp-includes/plugin.php(517): WP_Hook->do_action()
#5 /var/www/html/nsi-typesense/wp-content/plugins/woocommerce/includes/class-woocommerce.php(257): do_action()
#6 /var/www/html/nsi-typesense/wp-includes/class-wp-hook.php(324): WooCommerce->on_plugins_loaded()
#7 /var/www/html/nsi-typesense/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#8 /var/www/html/nsi-typesense/wp-includes/plugin.php(517): WP_Hook->do_action()
#9 /var/www/html/nsi-typesense/wp-settings.php(578): do_action()
#10 /var/www/html/nsi-typesense/wp-config.php(104): require_once('...')
#11 /var/www/html/nsi-typesense/wp-load.php(50): require_once('...')
#12 /var/www/html/nsi-typesense/wp-blog-header.php(13): require_once('...')
#13 /var/www/html/nsi-typesense/index.php(17): require('...')
#14 {main}
thrown in /var/www/html/nsi-typesense/wp-content/plugins/crown-shop/classes/class-crown-shop-products-importer.php on line 3419
Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the woocommerce domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/html/nsi-typesense/wp-includes/functions.php on line 6121
Fatal error: Uncaught TypeError: ftp_nlist(): Argument #1 ($ftp) must be of type FTP\Connection, null given in /var/www/html/nsi-typesense/wp-admin/includes/class-wp-filesystem-ftpext.php:438
Stack trace:
#0 /var/www/html/nsi-typesense/wp-admin/includes/class-wp-filesystem-ftpext.php(438): ftp_nlist()
#1 /var/www/html/nsi-typesense/wp-admin/includes/class-wp-filesystem-ftpext.php(456): WP_Filesystem_FTPext->exists()
#2 /var/www/html/nsi-typesense/wp-content/plugins/woocommerce/src/Internal/Admin/Logging/FileV2/File.php(254): WP_Filesystem_FTPext->is_file()
#3 /var/www/html/nsi-typesense/wp-content/plugins/woocommerce/src/Internal/Admin/Logging/FileV2/File.php(437): Automattic\WooCommerce\Internal\Admin\Logging\FileV2\File->is_writable()
#4 /var/www/html/nsi-typesense/wp-content/plugins/woocommerce/src/Internal/Admin/Logging/FileV2/FileController.php(135): Automattic\WooCommerce\Internal\Admin\Logging\FileV2\File->write()
#5 /var/www/html/nsi-typesense/wp-content/plugins/woocommerce/src/Internal/Admin/Logging/LogHandlerFileV2.php(62): Automattic\WooCommerce\Internal\Admin\Logging\FileV2\FileController->write_to_file()
#6 /var/www/html/nsi-typesense/wp-content/plugins/woocommerce/includes/class-wc-logger.php(189): Automattic\WooCommerce\Internal\Admin\Logging\LogHandlerFileV2->handle()
#7 /var/www/html/nsi-typesense/wp-content/plugins/woocommerce/includes/class-wc-logger.php(236): WC_Logger->log()
#8 /var/www/html/nsi-typesense/wp-content/plugins/woocommerce/includes/class-woocommerce.php(422): WC_Logger->critical()
#9 [internal function]: WooCommerce->log_errors()
#10 {main}
thrown in /var/www/html/nsi-typesense/wp-admin/includes/class-wp-filesystem-ftpext.php on line 438