init(); } return self::$instance; } /** * Init plugin hooks. * * @return void */ private function init() { add_action( 'init', array( $this, 'on_init' ), 5 ); add_action( 'admin_init', array( $this, 'maybe_block_multisite' ) ); add_action( 'admin_init', array( $this, 'maybe_redirect_after_activation' ) ); add_filter( 'query_vars', array( $this, 'register_query_vars' ) ); add_filter( 'allowed_redirect_hosts', array( $this, 'allowed_redirect_hosts' ) ); } /** * Boot on init (WordPress 6.7+ requires translations at init or later). * * @return void */ public function on_init(): void { //load_plugin_textdomain( 'afroit-messaging-workflows', false, dirname( plugin_basename( AFROMEWO_PLUGIN_FILE ) ) . '/languages' ); if ( ! class_exists( 'WooCommerce' ) ) { add_action( 'admin_notices', array( $this, 'afromewo_missing_notice' ) ); return; } // Core services (WooCommerce-dependent). ( new ServiceContainer() )->register(); /** * Fires after the free plugin has booted (WooCommerce is active). * * Add-ons can register filters earlier; use this action when they need * core services to already exist. */ do_action( 'afromewo_loaded' ); } /** * Admin notice when WooCommerce is inactive (WooCommerce extension best practice). * * @return void */ public function afromewo_missing_notice(): void { if ( ! current_user_can( 'activate_plugins' ) ) { return; } echo '

'; echo esc_html( sprintf( /* translators: %s: Plugin name */ __( '%s requires WooCommerce to be installed and active.', 'afroit-messaging-workflows' ), 'Afroit Messaging Workflows for WooCommerce' ) ); echo ' '; echo ''; esc_html_e( 'Install WooCommerce', 'afroit-messaging-workflows' ); echo ''; echo '

'; } /** * If a site becomes multisite after activation, self-deactivate and inform admin. * * @return void */ public function maybe_block_multisite() { if ( ! is_admin() || ! current_user_can( 'activate_plugins' ) ) { return; } if ( ! is_multisite() ) { return; } // Avoid running in network admin; activation is blocked by activation hook anyway. if ( function_exists( 'is_network_admin' ) && is_network_admin() ) { return; } if ( function_exists( 'deactivate_plugins' ) ) { deactivate_plugins( plugin_basename( AFROMEWO_PLUGIN_FILE ) ); } add_action( 'admin_notices', static function () { printf( '

%s

', esc_html__( 'Afroit Messaging Workflows for WooCommerce was deactivated because it does not support WordPress Multisite.', 'afroit-messaging-workflows' ) ); } ); } /** * Redirect to the plugin hub after activation. * * @return void */ public function maybe_redirect_after_activation(): void { if ( ! is_admin() || ! current_user_can( 'manage_woocommerce' ) ) { return; } if ( wp_doing_ajax() || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { return; } if ( ! get_option( 'afromewo_activation_redirect', 0 ) ) { return; } delete_option( 'afromewo_activation_redirect' ); wp_safe_redirect( admin_url( add_query_arg( 'page', \AFROMEWO_PLUGIN_SLUG, 'admin.php' ) ) ); exit; } /** * Allow plugin-specific query vars. * * @param string[] $vars Query vars. * @return string[] */ public function register_query_vars( $vars ) { if ( is_array( $vars ) ) { if ( ! in_array( 'tab', $vars, true ) ) { $vars[] = 'tab'; } if ( ! in_array( 'section', $vars, true ) ) { $vars[] = 'section'; } if ( ! in_array( 'afromewo_notice', $vars, true ) ) { $vars[] = 'afromewo_notice'; } if ( ! in_array( 'signature', $vars, true ) ) { $vars[] = 'signature'; } } return $vars; } /** * Allow redirects to Meta OAuth hosts. * * @param string[] $hosts Allowed redirect hosts. * @return string[] */ public function allowed_redirect_hosts( array $hosts ): array { $hosts[] = 'facebook.com'; $hosts[] = 'graph.facebook.com'; $hosts[] = 'www.facebook.com'; return array_values( array_unique( $hosts ) ); } }