Ctrl K
ComunidadGuías…

plugins/woocommerce-beta-tester/woocommerce-beta-tester.php

Rama trunk · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
<?php
/**
 * Plugin Name: WooCommerce Beta Tester
 * Plugin URI: https://github.com/woocommerce/woocommerce-beta-tester
 * Description: Run bleeding edge versions of WooCommerce. This will replace your installed version of WooCommerce with the latest tagged release - use with caution, and not on production sites.
 * Version: 4.0.0
 * Author: WooCommerce
 * Author URI: https://woocommerce.com/
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Requires at least: 6.8
 * Requires PHP: 7.4
 * Tested up to: 7.1
 * WC requires at least: 9.4
 * WC tested up to: 11.2
 * Text Domain: woocommerce-beta-tester
 *
 * @package WC_Beta_Tester
 */

defined( 'ABSPATH' ) || exit;


if ( defined( 'WP_CLI' ) ) {
	require_once dirname( __FILE__ ) . '/includes/class-wc-beta-tester-cli.php';
	WP_CLI::add_command( 'wc-beta-tester', WC_Beta_Tester_CLI::class );
}

// Define WC_BETA_TESTER_FILE.
if ( ! defined( 'WC_BETA_TESTER_FILE' ) ) {
	define( 'WC_BETA_TESTER_FILE', __FILE__ );
}

if ( ! defined( 'WC_BETA_TESTER_VERSION' ) ) {
	define( 'WC_BETA_TESTER_VERSION', '4.0.0' ); // WRCS: DEFINED_VERSION.
}

/**
 * Load text domain before all other code.
 *
 * @since 2.0.0
 */
function _wc_beta_tester_load_textdomain() {
	load_plugin_textdomain( 'woocommerce-beta-tester', false, basename( dirname( __FILE__ ) ) . '/languages' );
}

add_action( 'plugins_loaded', '_wc_beta_tester_load_textdomain' );

/**
 * Bootstrap plugin.
 */
function _wc_beta_tester_bootstrap() {

	// Check if WooCommerce is enabled.
	if ( ! class_exists( 'WooCommerce' ) ) {
		include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-admin-notices.php';
		$notices = new WC_Beta_Tester_Admin_Notices();

		add_action( 'admin_notices', array( $notices, 'woocoommerce_not_installed' ) );
	} elseif ( ! class_exists( 'WC_Beta_Tester' ) ) {
		include dirname( __FILE__ ) . '/includes/class-wc-beta-tester.php';
		// Settings.
		include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-channel.php';
		include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-import-export.php';
		new WC_Beta_Tester_Import_Export();
		// Tools.
		include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-version-picker.php';
		include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-override-coming-soon-options.php';
		include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-wccom-requests.php';

		register_activation_hook( __FILE__, array( 'WC_Beta_Tester', 'activate' ) );

		add_action( 'admin_init', array( 'WC_Beta_Tester', 'instance' ) );
	}

	// Load admin.
	require 'plugin.php';
}

add_action( 'plugins_loaded', '_wc_beta_tester_bootstrap' );

/**
 * Register the JS.
 */
function add_extension_register_script() {
	// The script calls the WCA Test Helper REST API, which requires install_plugins.
	if ( ! defined( 'WC_ADMIN_APP' ) || ! current_user_can( 'install_plugins' ) ) {
		return;
	}
	$script_path       = '/build/index.js';
	$script_asset_path = dirname( __FILE__ ) . '/build/index.asset.php';
	$script_asset      = file_exists( $script_asset_path )
		? require $script_asset_path
		: array(
			'dependencies' => array(),
			'version'      => filemtime( __DIR__ . $script_path ),
		);
	$script_url        = plugins_url( $script_path, __FILE__ );

	if ( class_exists( '\Automattic\WooCommerce\Admin\PageController' ) && \Automattic\WooCommerce\Admin\PageController::is_admin_page() ) {
		$script_asset['dependencies'][] = WC_ADMIN_APP; // Add WCA as a dependency to ensure it loads first.
	}

	wp_register_script(
		'woocommerce-admin-test-helper',
		$script_url,
		$script_asset['dependencies'],
		$script_asset['version'],
		true
	);
	wp_enqueue_script( 'woocommerce-admin-test-helper' );

	$css_file = dirname( __FILE__ ) . '/build/index.css';
	if ( file_exists( $css_file ) ) {
		$css_file_version = filemtime( $css_file );

		wp_register_style(
			'woocommerce-admin-test-helper',
			plugins_url( '/build/index.css', __FILE__ ),
			// Add any dependencies styles may have, such as wp-components.
			array(),
			$css_file_version
		);

		wp_enqueue_style( 'woocommerce-admin-test-helper' );
	}
}

add_action( 'admin_enqueue_scripts', 'add_extension_register_script' );

add_action(
	'before_woocommerce_init',
	function() {
		if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
		}
	}
);


/**
 * Simulate a WooCommerce error for remote logging testing.
 *
 * This function adds a filter to the 'woocommerce_template_path' hook
 * that throws an exception, then triggers the filter by calling WC()->template_path().
 *
 * @throws Exception A simulated WooCommerce error for testing purposes.
 */
function simulate_woocommerce_error() {
	// Return if WooCommerce is not loaded.
	if ( ! function_exists( 'WC' ) || ! class_exists( 'WooCommerce' ) ) {
		return;
	}

	// Define a constant to prevent the error from being caught by the WP Error Handler.
	if ( ! defined( 'WP_SANDBOX_SCRAPING' ) ) {
		define( 'WP_SANDBOX_SCRAPING', true );
	}

	add_filter(
		'woocommerce_template_path',
		function() {
			throw new Exception( 'Simulated WooCommerce error for remote logging test' );
		}
	);

	WC()->template_path();
}

$simulate_error = get_option( 'wc_beta_tester_simulate_woocommerce_php_error', false );

if ( $simulate_error ) {
	delete_option( 'wc_beta_tester_simulate_woocommerce_php_error' );

	if ( 'core' === $simulate_error ) {
		// Hook into the plugin_loaded action to simulate the error early before WP fully initializes.
		add_action( 'plugin_loaded', 'simulate_woocommerce_error' );
	} elseif ( 'beta-tester' === $simulate_error ) {
		throw new Exception( 'Test PHP exception from WooCommerce Beta Tester' );
	}
}


// Initialize the live branches feature.
require_once dirname( __FILE__ ) . '/includes/class-wc-beta-tester-live-branches.php';