register();
// HPOS compatibility declaration.
add_action(
'before_woocommerce_init',
function () {
if ( class_exists( FeaturesUtil::class ) ) {
FeaturesUtil::declare_compatibility( 'custom_order_tables', plugin_basename( __FILE__ ), true );
}
}
);
/**
* The plugin loader class.
*
* @since 1.10.0
*/
class WC_Facebook_Loader {
/**
* @var string the plugin version. This must be in the main plugin file to be automatically bumped by Woorelease.
*/
const PLUGIN_VERSION = '3.7.8'; // WRCS: DEFINED_VERSION.
// Minimum PHP version required by this plugin.
const MINIMUM_PHP_VERSION = '7.4.0';
// Minimum WordPress version required by this plugin.
const MINIMUM_WP_VERSION = '4.4';
// Minimum WooCommerce version required by this plugin.
const MINIMUM_WC_VERSION = '5.3';
// SkyVerge plugin framework version used by this plugin.
const FRAMEWORK_VERSION = '5.10.0';
// The plugin name, for displaying notices.
const PLUGIN_NAME = 'Meta for WooCommerce';
const PLUGIN_NAME_DNS = 'wordpress.org';
/**
* User meta key storing dismissed disabled-notice signature.
*/
const DISABLED_NOTICE_DISMISSED_META_KEY = 'wc_facebook_disabled_notice_dismissed_signature';
/**
* Transient fallback key for plugin disable flag.
*/
const DISABLE_FLAG_TRANSIENT = 'wc_facebook_plugin_disabled';
/**
* Relative path for plugin disable flag file inside wp-content.
*/
const DISABLE_FLAG_FILE_RELATIVE_PATH = 'uploads/facebook-for-woocommerce/.disabled';
/**
* This class instance.
*
* @var \WC_Facebook_Loader single instance of this class.
*/
private static $instance;
/**
* Admin notices to add.
*
* @var array Array of admin notices.
*/
private $notices = array();
/**
* @var object|null
*/
private static $compat_cached_entry = null;
/**
* Constructs the class.
*
* @since 1.10.0
*/
protected function __construct() {
register_activation_hook( __FILE__, array( $this, 'activation_check' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivation_cleanup' ) );
register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall_cleanup' ) );
add_action( 'admin_init', array( $this, 'check_environment' ) );
add_action( 'admin_notices', array( $this, 'admin_notices' ), 15 );
add_action( 'after_plugin_row_' . plugin_basename( __FILE__ ), array( $this, 'render_disabled_plugin_notice' ), 10, 2 );
add_action( 'admin_footer', array( $this, 'print_disabled_notice_dismiss_script' ), 999 );
add_action( 'admin_post_wc_facebook_clear_disable_flag', array( $this, 'handle_clear_disable_flag_action' ) );
add_action( 'wp_ajax_wc_facebook_dismiss_disabled_notice', array( $this, 'handle_dismiss_disabled_notice_ajax' ) );
add_action( 'action_scheduler_init', array( BatchLogHandler::class, 'register_batch_sender' ), 5 );
// Flush rewrite rules if flagged (runs once after activation/upgrade).
// Priority 99 ensures all rewrite rules are registered before flushing.
add_action( 'init', array( $this, 'maybe_flush_rewrite_rules' ), 99 );
// If the environment check fails, initialize the plugin.
if ( $this->is_environment_compatible() ) {
add_action( 'plugins_loaded', array( $this, 'init_plugin' ) );
}
if ( ! self::is_wp_com() ) {
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'compat_capture_entry' ), 11 );
add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'compat_verify_entry' ), PHP_INT_MAX );
}
}
/**
* Cloning instances is forbidden due to singleton pattern.
*
* @since 1.10.0
*/
public function __clone() {
wc_doing_it_wrong( __FUNCTION__, sprintf( 'You cannot clone instances of %s.', get_class( $this ) ), '1.10.0' );
}
/**
* Unserializing instances is forbidden due to singleton pattern.
*
* @since 1.10.0
*/
public function __wakeup() {
wc_doing_it_wrong( __FUNCTION__, sprintf( 'You cannot unserialize instances of %s.', get_class( $this ) ), '1.10.0' );
}
/**
* Initializes the plugin.
*
* @since 1.10.0
*/
public function init_plugin() {
if ( ! Checker::instance()->is_compatible( __FILE__, self::PLUGIN_VERSION ) ) {
return;
}
if ( $this->has_valid_disable_flag() ) {
$this->register_disabled_mode_services();
error_log( 'Meta for WooCommerce is disabled via crash flag. Skipping full plugin initialization.' ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
return;
}
self::set_wc_facebook_svr_flags();
$commerce_class_file = plugin_dir_path( __FILE__ ) . 'class-wc-facebookcommerce.php';
if ( ! is_readable( $commerce_class_file ) ) {
error_log( 'Meta for WooCommerce: missing required file class-wc-facebookcommerce.php. Skipping full plugin initialization.' ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
return;
}
require_once $commerce_class_file;
// fire it up!
if ( function_exists( 'facebook_for_woocommerce' ) ) {
facebook_for_woocommerce();
PluginCrashHandler::mark_plugin_initialized();
}
}
/**
* Checks whether the crash guard currently pauses the plugin.
*
* Reads both the flag file and the transient fallback and lets the newer one decide. That
* matters when the file is stale: a transient written after it, such as the idle tombstone
* that "Resume now" leaves when the file cannot be deleted, must be honored here or the
* plugin would stay paused while the notice disappears.
*
* @since 3.6.4
*
* @return bool
*/
private function has_valid_disable_flag() {
return CrashGuardState::is_disabled( CrashGuardState::read() );
}
/**
* Returns the pause length for a crash guard escalation level.
*
* Levels escalate 10 minutes, 1 hour, 6 hours, 24 hours and cap there; there is no permanent pause.
*
* @since 3.6.4
*
* @param int $level escalation level.
* @return int seconds.
*/
private function get_disable_window_seconds( $level ) {
return CrashGuardState::get_window_seconds( $level );
}
/**
* Registers lightweight services when plugin is disabled.
*
* Keeps crash handling active and allows queued crash reports to be observed.
*
* @since 3.6.4
*/
private function register_disabled_mode_services() {
( new PluginCrashHandler() )->register();
new ErrorLogHandler();
add_action( ErrorLogHandler::META_LOG_API, array( $this, 'handle_disabled_mode_crash_report' ), 10, 1 );
}
/**
* Lightweight crash-report handler used while plugin is disabled.
*
* @since 3.6.4
*
* @param array $context queued crash report payload.
*/
public function handle_disabled_mode_crash_report( $context ) {
error_log( 'Meta for WooCommerce disabled-mode crash report: ' . wp_json_encode( $context ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
}
/**
* Renders a plugins-page-only notice row when plugin is disabled by crash flag.
*
* @since 3.6.4
*
* @param string $plugin_file plugin basename.
* @param array $plugin_data plugin row data.
*/
public function render_disabled_plugin_notice( $plugin_file = '', $plugin_data = [] ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
global $pagenow;
if ( 'plugins.php' !== $pagenow ) {
return;
}
if ( plugin_basename( __FILE__ ) !== $plugin_file ) {
return;
}
$state = CrashGuardState::read();
if ( ! CrashGuardState::is_disabled( $state ) ) {
if ( $this->has_unverified_crashes( $state ) ) {
$this->render_unverified_crashes_notice( $state );
}
return;
}
$signature = $this->get_disabled_notice_signature( $state );
if ( $this->is_disabled_notice_dismissed_for_current_user( $signature ) ) {
return;
}
$clear_url = wp_nonce_url(
admin_url( 'admin-post.php?action=wc_facebook_clear_disable_flag' ),
'wc_facebook_clear_disable_flag'
);
$level = (int) $state['level'];
$datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
$paused_since = wp_date( $datetime_format, (int) $state['disabled_at'] );
$resumes_at = wp_date( $datetime_format, (int) $state['disabled_until'] );
$pause_length = human_time_diff( 0, $this->get_disable_window_seconds( $level ) );
$last_crash = get_transient( PluginCrashHandler::LAST_CRASH_TRANSIENT );
$last_crash_label = '';
if ( is_array( $last_crash ) && ! empty( $last_crash['message'] ) ) {
$last_crash_label = (string) $last_crash['message'];
if ( ! empty( $last_crash['file'] ) ) {
$last_crash_label .= ' (' . (string) $last_crash['file'] . ':' . (int) ( $last_crash['line'] ?? 0 ) . ')';
}
}
echo '
';
echo '
' . esc_html__( 'Meta for WooCommerce is paused after repeated crashes.', 'facebook-for-woocommerce' ) . '
';
echo '
' . sprintf(
/* translators: %s: date and time when the plugin resumes automatically. */
esc_html__( 'It resumes automatically at %s. If it keeps crashing after resuming, the pause grows: 10 minutes, 1 hour, 6 hours, then 24 hours.', 'facebook-for-woocommerce' ),
'' . esc_html( $resumes_at ) . ''
) . '
';
}
/**
* Whether crashes reached the pause threshold without the self-check being able to confirm them.
*
* Such crashes do not pause the plugin (an outsider must not be able to switch the
* integration off), so the admin is told instead.
*
* @since 3.7.9
*
* @param array|null $state crash guard state, read when null.
* @return bool
*/
private function has_unverified_crashes( $state = null ) {
if ( null === $state ) {
$state = CrashGuardState::read();
}
return ! CrashGuardState::is_disabled( $state ) && (int) $state['unverified']['count'] > 0;
}
/**
* Renders the plugins-page notice for crashes that could not be verified.
*
* @since 3.7.9
*
* @param array $state crash guard state.
*/
private function render_unverified_crashes_notice( array $state ) {
$signature = md5( 'unverified|' . (int) $state['unverified']['last_at'] . '|' . (int) $state['unverified']['count'] );
if ( $this->is_disabled_notice_dismissed_for_current_user( $signature ) ) {
return;
}
$clear_url = wp_nonce_url(
admin_url( 'admin-post.php?action=wc_facebook_clear_disable_flag' ),
'wc_facebook_clear_disable_flag'
);
$count = (int) $state['unverified']['count'];
$last_at = wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), (int) $state['unverified']['last_at'] );
$reason = (string) $state['unverified']['reason'];
$last_crash = get_transient( PluginCrashHandler::LAST_CRASH_TRANSIENT );
$last_crash_label = '';
if ( is_array( $last_crash ) && ! empty( $last_crash['message'] ) ) {
$last_crash_label = (string) $last_crash['message'];
if ( ! empty( $last_crash['file'] ) ) {
$last_crash_label .= ' (' . (string) $last_crash['file'] . ':' . (int) ( $last_crash['line'] ?? 0 ) . ')';
}
}
echo '
';
echo '
' . esc_html__( 'Meta for WooCommerce has crashed repeatedly but was not paused.', 'facebook-for-woocommerce' ) . '
';
echo '
' . sprintf(
/* translators: 1: number of unverified crashes, 2: date and time of the latest one. */
esc_html( _n( 'Visitor requests crashed the plugin %1$d time (last at %2$s), but the plugin could not confirm that a plain request crashes too, so it stays on rather than let outside requests switch it off. Check the error log for the crash.', 'Visitor requests crashed the plugin %1$d times (last at %2$s), but the plugin could not confirm that a plain request crashes too, so it stays on rather than let outside requests switch it off. Check the error log for the crash.', $count, 'facebook-for-woocommerce' ) ),
(int) $count,
'' . esc_html( $last_at ) . ''
) . '