$data ) { if ( is_array( $data ) ) { $projects[ $id ] = Connected_Project::from_array( $data ); } } return $projects; } /** * @param string $id Project ID (see id_for()). * * @return Connected_Project|null */ public function get( $id ) { $all = $this->all(); return isset( $all[ $id ] ) ? $all[ $id ] : null; } /** * Find the project tracking a given repo, if any — the lookup a * webhook delivery needs, keyed on the payload's own * "owner/repo" rather than a stored numeric ID GitHub's payload * doesn't reliably echo back in the same shape. * * @param string $owner GitHub owner/org. * @param string $repo Repo name. * * @return Connected_Project|null */ public function find_by_repo( $owner, $repo ) { return $this->get( self::id_for( $owner, $repo ) ); } /** * Create or overwrite a project record. * * @param Connected_Project $project Must have owner/repo set; * $project->id is (re)computed * here rather than trusted. */ public function save( Connected_Project $project ) { $project->id = self::id_for( $project->owner, $project->repo ); $raw = get_option( self::OPTION_KEY, array() ); $raw = is_array( $raw ) ? $raw : array(); $raw[ $project->id ] = $project->to_array(); update_option( self::OPTION_KEY, $raw, false ); } /** * @param string $id Project ID. */ public function delete( $id ) { $raw = get_option( self::OPTION_KEY, array() ); if ( ! is_array( $raw ) || ! isset( $raw[ $id ] ) ) { return; } unset( $raw[ $id ] ); update_option( self::OPTION_KEY, $raw, false ); } }