publish ) ) { return (int) $counts->publish; } return 0; } /** * Common request args. * * @param array $extra Extra args merged on top. * @return array */ private static function args( $extra = array() ) { global $wp_version; $wc_version = defined( 'WC_VERSION' ) ? WC_VERSION : ''; $ua = sprintf( 'LLMs.txt-for-WooCommerce/%s (WP %s; WC %s)', defined( 'LLTXT_VERSION' ) ? LLTXT_VERSION : '1.0.0', $wp_version, $wc_version ); $args = array( 'timeout' => 5, 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-Xpay-Api-Key' => self::api_key_hash(), 'User-Agent' => $ua, ), ); return array_merge( $args, $extra ); } /** * POST /v1/llms-txt/installs. * * @param string $context 'activate' | 'weekly' | 'manual' | 'deactivate'. * (Uninstall has its own fire-and-forget call from * uninstall.php — the plugin code isn't loaded then.) * @return array|WP_Error|null */ public static function ping( $context = 'activate' ) { if ( ! self::is_enabled() ) { return null; } $slug = self::slug(); if ( '' === $slug ) { return new WP_Error( 'lltxt_no_slug', 'Could not derive slug from home_url.' ); } global $wp_version; $payload = array( 'slug' => $slug, 'home_url' => home_url( '/' ), 'wp_version' => $wp_version, 'wc_version' => defined( 'WC_VERSION' ) ? WC_VERSION : null, 'plugin_version' => defined( 'LLTXT_VERSION' ) ? LLTXT_VERSION : null, 'active_products' => self::active_products(), 'version_history_enabled' => true, 'context' => $context, ); $args = self::args( array( 'method' => 'POST', 'body' => wp_json_encode( $payload ), ) ); $res = wp_remote_post( self::base_url() . '/v1/llms-txt/installs', $args ); update_option( self::OPT_LAST_PING_TS, time(), false ); if ( is_wp_error( $res ) ) { return $res; } $decoded = json_decode( wp_remote_retrieve_body( $res ), true ); return is_array( $decoded ) ? $decoded : array(); } /** * DELETE /v1/llms-txt/installs?slug=. Used by the "Delete my install * info" button and by uninstall.php. * * @return array|WP_Error|null */ public static function delete_install() { if ( ! self::is_enabled() ) { return null; } // If no api_key has ever been generated, this install has never sent // anything to the backend — there is nothing to delete. Short-circuit // here so we do NOT generate a persistent identifier purely for a // delete request. if ( '' === (string) get_option( self::OPT_API_KEY, '' ) ) { return null; } $slug = self::slug(); if ( '' === $slug ) { return new WP_Error( 'lltxt_no_slug', 'No slug.' ); } $res = wp_remote_request( self::base_url() . '/v1/llms-txt/installs?slug=' . rawurlencode( $slug ), self::args( array( 'method' => 'DELETE' ) ) ); if ( is_wp_error( $res ) ) { return $res; } $decoded = json_decode( wp_remote_retrieve_body( $res ), true ); return is_array( $decoded ) ? $decoded : array(); } }