(string) $app_id, 'app_secret' => self::enc( $app_secret ), ); foreach ( $extra as $k => $v ) { if ( 'app_id' === $k || 'app_secret' === $k ) { continue; } $row[ $k ] = $v; } $all[ $platform ] = $row; update_option( self::OPT_APPS, $all, false ); } public static function get_app( $platform ) { if ( ! self::valid_platform( $platform ) ) { return array(); } $all = get_option( self::OPT_APPS, array() ); if ( ! is_array( $all ) || empty( $all[ $platform ] ) || ! is_array( $all[ $platform ] ) ) { return array(); } $row = $all[ $platform ]; $out = array(); foreach ( $row as $k => $v ) { $out[ $k ] = ( 'app_secret' === $k ) ? self::dec( $v ) : $v; } if ( ! isset( $out['app_id'] ) ) { $out['app_id'] = ''; } if ( ! isset( $out['app_secret'] ) ) { $out['app_secret'] = ''; } return $out; } public static function app_configured( $platform ) { $app = self::get_app( $platform ); return ! empty( $app['app_id'] ) && ! empty( $app['app_secret'] ); } public static function redirect_uri( $platform ) { return add_query_arg( 'platform', rawurlencode( (string) $platform ), rest_url( 'aieo/v1/social/oauth/callback' ) ); } private static function raw_accounts() { $all = get_option( self::OPT_ACCOUNTS, array() ); return is_array( $all ) ? $all : array(); } private static function hydrate( array $row ) { $row['access_token'] = isset( $row['access_token'] ) ? self::dec( $row['access_token'] ) : ''; $row['refresh_token'] = isset( $row['refresh_token'] ) ? self::dec( $row['refresh_token'] ) : ''; if ( '' === $row['access_token'] ) { $row['token_status'] = self::NOT_CONNECTED; } return $row; } public static function can_post( $account ) { return is_array( $account ) && '' !== (string) ( $account['access_token'] ?? '' ) && ! in_array( (string) ( $account['token_status'] ?? '' ), array( 'revoked', self::NOT_CONNECTED ), true ); } public static function save_account( $platform, array $data ) { $platform = (string) $platform; $external_id = isset( $data['external_id'] ) ? (string) $data['external_id'] : ''; $now = current_time( 'mysql', true ); $all = self::raw_accounts(); $match_id = 0; foreach ( $all as $id => $row ) { if ( isset( $row['platform'], $row['external_id'] ) && (string) $row['platform'] === $platform && (string) $row['external_id'] === $external_id ) { $match_id = (int) $id; break; } } if ( $match_id > 0 ) { $row = $all[ $match_id ]; } else { $match_id = self::next_id(); $row = array( 'id' => $match_id, 'platform' => $platform, 'token_status' => 'valid', 'connected_at' => $now, ); } foreach ( array( 'name', 'type', 'parent_external_id', 'token_expires_at', 'scopes' ) as $k ) { if ( array_key_exists( $k, $data ) ) { $row[ $k ] = $data[ $k ]; } elseif ( ! isset( $row[ $k ] ) ) { $row[ $k ] = ''; } } $row['id'] = $match_id; $row['platform'] = $platform; $row['external_id'] = $external_id; if ( array_key_exists( 'access_token', $data ) ) { $row['access_token'] = self::enc( $data['access_token'] ); } elseif ( ! isset( $row['access_token'] ) ) { $row['access_token'] = ''; } if ( array_key_exists( 'refresh_token', $data ) ) { $row['refresh_token'] = self::enc( $data['refresh_token'] ); } elseif ( ! isset( $row['refresh_token'] ) ) { $row['refresh_token'] = ''; } if ( array_key_exists( 'access_token', $data ) || array_key_exists( 'refresh_token', $data ) ) { $row['token_status'] = 'valid'; $row['last_refreshed_at'] = $now; } elseif ( ! isset( $row['last_refreshed_at'] ) ) { $row['last_refreshed_at'] = ''; } if ( ! isset( $row['token_status'] ) ) { $row['token_status'] = 'valid'; } if ( '' === self::dec( (string) $row['access_token'] ) ) { $row['token_status'] = self::NOT_CONNECTED; } if ( ! isset( $row['connected_at'] ) ) { $row['connected_at'] = $now; } $all[ $match_id ] = $row; update_option( self::OPT_ACCOUNTS, $all, false ); return $match_id; } private static function next_id() { $next = (int) get_option( self::OPT_ACCOUNTS_SEQ, 0 ); $next++; update_option( self::OPT_ACCOUNTS_SEQ, $next, false ); return $next; } public static function get_accounts( $platform = null ) { $out = array(); foreach ( self::raw_accounts() as $row ) { if ( ! is_array( $row ) ) { continue; } if ( null !== $platform && (string) $row['platform'] !== (string) $platform ) { continue; } $out[] = self::hydrate( $row ); } return $out; } public static function get_account( $id ) { $all = self::raw_accounts(); $id = (int) $id; if ( empty( $all[ $id ] ) || ! is_array( $all[ $id ] ) ) { return null; } return self::hydrate( $all[ $id ] ); } public static function set_token_status( $id, $status ) { $all = self::raw_accounts(); $id = (int) $id; if ( empty( $all[ $id ] ) || ! is_array( $all[ $id ] ) ) { return; } $all[ $id ]['token_status'] = (string) $status; update_option( self::OPT_ACCOUNTS, $all, false ); } public static function delete_account( $id ) { $all = self::raw_accounts(); $id = (int) $id; if ( ! isset( $all[ $id ] ) ) { return; } unset( $all[ $id ] ); update_option( self::OPT_ACCOUNTS, $all, false ); } }