post_status ) { return; } if ( self::is_ignorable( $post ) ) { return; } self::schedule(); } /** * Revisions, autosaves, attachments and private post types are not the * public pages the crawler reads. * * @param WP_Post|null $post The post. */ private static function is_ignorable( $post ) { if ( ! $post instanceof WP_Post ) { return true; } if ( wp_is_post_revision( $post ) || wp_is_post_autosave( $post ) ) { return true; } if ( 'attachment' === $post->post_type ) { return true; } $type = get_post_type_object( $post->post_type ); return ! $type || empty( $type->public ); } /** * Queue one ping, unless one is already queued. * * Deferring rather than calling inline also keeps a slow or unreachable * backend from ever showing up as a slow "Publish" button. */ private static function schedule() { if ( ! self::enabled() ) { return; } if ( wp_next_scheduled( self::EVENT ) ) { return; } wp_schedule_single_event( time() + self::DEBOUNCE_SECONDS, self::EVENT ); } /** * Fire the ping. Runs on WP-Cron, where the admin-only includes are not * loaded — hence the explicit require. */ public static function send() { if ( ! self::enabled() ) { return; } require_once AIVASTARK_WIDGET_PATH . 'includes/class-aivastark-api.php'; $access = Aivastark_Widget_Api::ensure_fresh_token(); if ( is_wp_error( $access ) ) { return; } // Failure is silently ignored on purpose: this is a nice-to-have badge, // and a backend hiccup must never surface as an error on a site owner's // publish screen. The daily HTTP probe on our side is the backstop. Aivastark_Widget_Api::post( 'kb-change-signal', array( 'reason' => 'content_update' ), $access ); } /** * Connected, and the owner hasn't switched "Knowledge sync" off on * Aivastark → Settings. */ private static function enabled() { $opts = aivastark_widget_get_options(); return ! empty( $opts['change_ping'] ) && Aivastark_Widget_Options::is_connected(); } /** Drop any queued ping (deactivation / uninstall). */ public static function clear_scheduled() { $timestamp = wp_next_scheduled( self::EVENT ); while ( $timestamp ) { wp_unschedule_event( $timestamp, self::EVENT ); $timestamp = wp_next_scheduled( self::EVENT ); } } }