Files
IQ-Dynamic-Google-Pricing/informatiq-smart-google-pricing.php

195 lines
4.8 KiB
PHP
Raw Normal View History

<?php
/**
* Plugin Name: informatiq-smart-google-pricing
* Plugin URI: https://informatiq.services
* Description: Automatically adjusts WooCommerce sale prices based on Google Merchant Center competitor data.
* Version: 1.0.0
* Author: Mălin Cenușă
* Author URI: https://malin.ro
* Text Domain: informatiq-smart-pricing
* Domain Path: /languages
* Requires at least: 5.8
* Requires PHP: 7.4
* WC requires at least: 5.0
* WC tested up to: 8.5
*
* @package InformatiqSmartPricing
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Define plugin constants.
define( 'INFORMATIQ_SP_VERSION', '1.0.0' );
define( 'INFORMATIQ_SP_PLUGIN_FILE', __FILE__ );
define( 'INFORMATIQ_SP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'INFORMATIQ_SP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'INFORMATIQ_SP_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
/**
* Main plugin class.
*/
class Informatiq_Smart_Google_Pricing {
/**
* Single instance of the class.
*
* @var Informatiq_Smart_Google_Pricing
*/
protected static $instance = null;
/**
* Admin instance.
*
* @var Informatiq_SP_Admin
*/
public $admin;
/**
* Price updater instance.
*
* @var Informatiq_SP_Price_Updater
*/
public $price_updater;
/**
* Logger instance.
*
* @var Informatiq_SP_Logger
*/
public $logger;
/**
* Get main instance.
*
* @return Informatiq_Smart_Google_Pricing
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*/
private function __construct() {
// Check if WooCommerce is active.
if ( ! $this->is_woocommerce_active() ) {
add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
return;
}
// Load autoloader.
require_once INFORMATIQ_SP_PLUGIN_DIR . 'vendor/autoload.php';
// Load plugin classes.
$this->includes();
// Initialize plugin.
$this->init();
// Register hooks.
$this->register_hooks();
}
/**
* Check if WooCommerce is active.
*
* @return bool
*/
private function is_woocommerce_active() {
return in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ||
( is_multisite() && array_key_exists( 'woocommerce/woocommerce.php', get_site_option( 'active_sitewide_plugins', array() ) ) );
}
/**
* WooCommerce missing notice.
*/
public function woocommerce_missing_notice() {
?>
<div class="notice notice-error">
<p><?php esc_html_e( 'Informatiq Smart Google Pricing requires WooCommerce to be installed and active.', 'informatiq-smart-pricing' ); ?></p>
</div>
<?php
}
/**
* Include required files.
*/
private function includes() {
require_once INFORMATIQ_SP_PLUGIN_DIR . 'includes/class-informatiq-sp-logger.php';
require_once INFORMATIQ_SP_PLUGIN_DIR . 'includes/class-informatiq-sp-google-api.php';
require_once INFORMATIQ_SP_PLUGIN_DIR . 'includes/class-informatiq-sp-price-updater.php';
require_once INFORMATIQ_SP_PLUGIN_DIR . 'includes/class-informatiq-sp-scheduler.php';
require_once INFORMATIQ_SP_PLUGIN_DIR . 'admin/class-informatiq-sp-admin.php';
}
/**
* Initialize plugin components.
*/
private function init() {
$this->logger = new Informatiq_SP_Logger();
$this->price_updater = new Informatiq_SP_Price_Updater( $this->logger );
$this->admin = new Informatiq_SP_Admin( $this->logger, $this->price_updater );
// Initialize scheduler.
new Informatiq_SP_Scheduler( $this->price_updater );
}
/**
* Register hooks.
*/
private function register_hooks() {
register_activation_hook( INFORMATIQ_SP_PLUGIN_FILE, array( $this, 'activate' ) );
register_deactivation_hook( INFORMATIQ_SP_PLUGIN_FILE, array( $this, 'deactivate' ) );
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
}
/**
* Plugin activation.
*/
public function activate() {
// Schedule daily cron event.
if ( ! wp_next_scheduled( 'informatiq_sp_daily_price_update' ) ) {
wp_schedule_event( time(), 'daily', 'informatiq_sp_daily_price_update' );
}
// Create log table if needed.
$this->logger->create_log_table();
}
/**
* Plugin deactivation.
*/
public function deactivate() {
// Clear scheduled cron event.
$timestamp = wp_next_scheduled( 'informatiq_sp_daily_price_update' );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, 'informatiq_sp_daily_price_update' );
}
}
/**
* Load text domain.
*/
public function load_textdomain() {
load_plugin_textdomain( 'informatiq-smart-pricing', false, dirname( INFORMATIQ_SP_PLUGIN_BASENAME ) . '/languages' );
}
}
/**
* Get main instance.
*
* @return Informatiq_Smart_Google_Pricing
*/
function informatiq_sp() {
return Informatiq_Smart_Google_Pricing::instance();
}
// Initialize plugin.
informatiq_sp();