context = $context; $this->options = $options ?: new Options( $this->context ); } /** * Registers functionality through WordPress hooks. * * @since 1.20.0 */ public function register() { add_action( 'googlesitekit_uninstallation', function () { $this->uninstall(); $this->clear_scheduled_events(); } ); add_action( 'googlesitekit_deactivation', function () { $this->clear_scheduled_events(); } ); add_action( 'googlesitekit_reset', function () { $this->clear_scheduled_events(); } ); } /** * Runs necessary logic for uninstallation of the plugin. * * If connected to the proxy, it will issue a request to unregister the site. * * @since 1.20.0 */ private function uninstall() { $credentials = new Credentials( new Encrypted_Options( $this->options ) ); if ( $credentials->has() && $credentials->using_proxy() ) { $google_proxy = new Google_Proxy( $this->context ); $google_proxy->unregister_site( $credentials ); } } /** * Clears all scheduled events. * * @since 1.136.0 */ private function clear_scheduled_events() { foreach ( self::SCHEDULED_EVENTS as $event ) { // Only clear scheduled events that are set, important in E2E // testing. if ( $this->is_event_scheduled( $event ) ) { wp_unschedule_hook( $event ); } } } /** * Determines if an event is scheduled for the given hook, regardless of arguments. * * @since 1.168.0 * * @param string $hook The hook name. * @return bool True if an event is scheduled for the hook, false otherwise. */ private function is_event_scheduled( $hook ) { $crons = _get_cron_array(); if ( ! is_array( $crons ) || empty( $crons ) ) { return false; } foreach ( $crons as $events ) { if ( isset( $events[ $hook ] ) ) { return true; } } return false; } }