Files
IQ-Dynamic-Google-Pricing/informatiq-smart-google-pricing.php
Malin d1f3607895 feat: Migrate to Google Merchant API and remove Composer dependencies
- Fix ISE 500 error on plugin activation:
  - Defer wc_get_logger() call in Logger class (lazy initialization)
  - Move plugin init to plugins_loaded hook with priority 20
  - Remove invalid Google_Service_ShoppingContent_Reports instantiation

- Migrate from deprecated Content API to new Merchant API:
  - Rewrite Google API class with direct REST calls
  - Implement JWT authentication using PHP OpenSSL
  - Use WordPress wp_remote_* functions for HTTP requests
  - Support Price Competitiveness reports for competitor pricing

- Remove all Composer dependencies:
  - Delete vendor/ directory (~50 packages)
  - Delete composer.json and composer.lock
  - Remove setup scripts and related documentation

- Plugin is now fully self-contained with no external dependencies

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-21 08:29:25 +01:00

215 lines
5.4 KiB
PHP

<?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: 2.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', '2.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;
}
// Check for OpenSSL extension (required for JWT signing).
if ( ! extension_loaded( 'openssl' ) ) {
add_action( 'admin_notices', array( $this, 'openssl_missing_notice' ) );
return;
}
// 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
}
/**
* OpenSSL missing notice.
*/
public function openssl_missing_notice() {
?>
<div class="notice notice-error">
<p><?php esc_html_e( 'Informatiq Smart Google Pricing requires the PHP OpenSSL extension for Google API authentication.', '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 after all plugins are loaded.
* This ensures WooCommerce functions are available.
*/
function informatiq_sp_init() {
informatiq_sp();
}
add_action( 'plugins_loaded', 'informatiq_sp_init', 20 );