*/ public static function connection() { $connection = get_option( self::CONNECTION, array() ); return is_array( $connection ) && ! empty( $connection['secret'] ) ? $connection : array(); } /** * Whether the site is connected. * * @since 1.0.0 * * @return bool */ public static function connected() { return array() !== self::connection(); } /** * The connection's secret; '' when not connected. * * @since 1.0.0 * * @return string */ public static function secret() { $connection = self::connection(); return isset( $connection['secret'] ) ? (string) $connection['secret'] : ''; } /** * Saves the connection. * * @since 1.0.0 * * @param array $connection The connection. * @return void */ public static function save_connection( $connection ) { self::put( self::CONNECTION, $connection ); } /** * Forgets the connection and what arrived over it. The posts, the * settings and the log stay. * * @since 1.0.0 * * @return void */ public static function forget_connection() { delete_option( self::CONNECTION ); delete_option( self::STATUS ); } /** * Post as (a user id) and Category (a term id), 0 for WordPress's own * default; and whether search engines hear of new posts through IndexNow, * on unless switched off. * * @since 1.0.0 * @since 1.1.0 IndexNow. * * @return array{author: int, category: int, indexnow: bool} */ public static function settings() { $settings = get_option( self::SETTINGS, array() ); $settings = is_array( $settings ) ? $settings : array(); return array( 'author' => isset( $settings['author'] ) ? (int) $settings['author'] : 0, 'category' => isset( $settings['category'] ) ? (int) $settings['category'] : 0, 'indexnow' => isset( $settings['indexnow'] ) ? (bool) $settings['indexnow'] : true, ); } /** * Saves the settings. * * @since 1.0.0 * @since 1.1.0 IndexNow. * * @param int $author A user id, or 0. * @param int $category A category id, or 0. * @param bool $indexnow Whether search engines hear of new posts through IndexNow. * @return void */ public static function save_settings( $author, $category, $indexnow ) { self::put( self::SETTINGS, array( 'author' => (int) $author, 'category' => (int) $category, 'indexnow' => (bool) $indexnow, ) ); } /** * What has arrived: the last request that checked out, the last test, * the last article, and the last request refused since. * * @since 1.0.0 * * @return array{last_ok_at: int, last_test_at: int, last_article_at: int, refusal: array{reason: string, at: int, skew: int}|null} */ public static function status() { $status = get_option( self::STATUS, array() ); $status = is_array( $status ) ? $status : array(); $refusal = null; if ( isset( $status['refusal']['reason'] ) && is_array( $status['refusal'] ) ) { $refusal = array( 'reason' => (string) $status['refusal']['reason'], 'at' => isset( $status['refusal']['at'] ) ? (int) $status['refusal']['at'] : 0, 'skew' => isset( $status['refusal']['skew'] ) ? (int) $status['refusal']['skew'] : 0, ); } return array( 'last_ok_at' => isset( $status['last_ok_at'] ) ? (int) $status['last_ok_at'] : 0, 'last_test_at' => isset( $status['last_test_at'] ) ? (int) $status['last_test_at'] : 0, 'last_article_at' => isset( $status['last_article_at'] ) ? (int) $status['last_article_at'] : 0, 'refusal' => $refusal, ); } /** * A request from ThirdDraft checked out. * * @since 1.0.0 * * @param string $kind 'test', 'article' or 'other'. * @return void */ public static function note_arrival( $kind ) { $status = self::status(); $status['last_ok_at'] = time(); $status['refusal'] = null; if ( 'test' === $kind ) { $status['last_test_at'] = time(); } elseif ( 'article' === $kind ) { $status['last_article_at'] = time(); } self::put( self::STATUS, $status ); } /** * A request that looked like ThirdDraft's was refused. Written at most * once a minute, so a flood of bad requests is not a flood of writes. * * @since 1.0.0 * * @param string $reason 'signature' or 'clock'. * @param int $skew Seconds this server is ahead of the request. * @return void */ public static function note_refusal( $reason, $skew ) { if ( ! self::connected() ) { return; } $status = self::status(); if ( null !== $status['refusal'] && time() - (int) $status['refusal']['at'] < MINUTE_IN_SECONDS && $status['refusal']['reason'] === $reason ) { return; } $status['refusal'] = array( 'reason' => (string) $reason, 'at' => time(), 'skew' => (int) $skew, ); self::put( self::STATUS, $status ); } /** * The log, newest first. * * @since 1.0.0 * * @return array */ public static function log() { $log = get_option( self::LOG, array() ); return is_array( $log ) ? array_values( $log ) : array(); } /** * Adds an article taken to the log. * * @since 1.0.0 * * @param int $article ThirdDraft's article id. * @param int $post The post. * @param string $event The event id. * @return void */ public static function log_article( $article, $post, $event ) { $log = array( array( 'article' => (int) $article, 'post' => (int) $post, 'event' => (string) $event, 'at' => time(), ), ); foreach ( self::log() as $entry ) { if ( isset( $entry['article'] ) && (int) $entry['article'] !== (int) $article ) { $log[] = $entry; } } self::put( self::LOG, array_slice( $log, 0, self::LOG_SIZE ) ); } /** * Writes an option with autoload off, whether or not it exists yet. * * @param string $name The option. * @param mixed $value Its value. * @return void */ private static function put( $name, $value ) { update_option( $name, $value, false ); } }