API
API class.
Namespace: WebberZone\Knowledge_Base\Pro\GitHub
Since: 3.1.0
Source: includes/github/class-api.php line 23
Methods
with_pat()
Return a new instance that uses the given PAT instead of the global setting.
Since: 3.1.0
Line: 42
public function with_pat( string $pat ): self;
| Type | Name | Description |
|---|---|---|
string | $pat | Personal Access Token. |
Returns: self — New API instance with the PAT override applied.
for_mapping() static
Return the API instance to use for a given mapping.
If the mapping defines its own PAT (encrypted), returns a cloned instance configured with that token; otherwise returns the provided default instance.
Since: 3.1.0
Line: 60
static public function for_mapping( array $mapping, self $fallback ): self;
| Type | Name | Description |
|---|---|---|
array | $mapping | Repository mapping array. |
self | $fallback | Fallback API instance used when no per-mapping PAT is set. |
Returns: self — API instance.
request_raw()
Make an authenticated request and return the raw wp_remote response.
Useful for callers that need to inspect the response status code or headers (e.g. permission probes).
Since: 3.1.0
Line: 142
public function request_raw( string $url, string $method = ..., array $extra = ... ): array|\WP_Error;
| Type | Name | Description |
|---|---|---|
string | $url | Full GitHub API URL. |
string | $method | HTTP method. |
array | $extra | Extra args merged into the wp_remote_request() array. |
Returns: array|\WP_Error — wp_remote response array or error.
request()
Make an authenticated GET request to the GitHub API.
Since: 3.1.0
Line: 178
public function request( string $url, array $extra_args = ... ): string|\WP_Error;
| Type | Name | Description |
|---|---|---|
string | $url | Full GitHub API URL. |
array | $extra_args | Extra args merged into the request array. |
Returns: string|\WP_Error — Response body or error.
get_repo_info()
Get repository metadata (default branch, visibility).
Since: 3.1.0
Line: 199
public function get_repo_info( string $owner, string $repo ): array|\WP_Error;
| Type | Name | Description |
|---|---|---|
string | $owner | Repository owner. |
string | $repo | Repository name. |
Returns: array|\WP_Error — Decoded body array or error.
get_file_tree()
Get the full recursive file tree for a given ref.
Since: 3.1.0
Line: 223
public function get_file_tree( string $owner, string $repo, string $ref ): array|\WP_Error;
| Type | Name | Description |
|---|---|---|
string | $owner | Repository owner. |
string | $repo | Repository name. |
string | $ref | Branch, tag, or SHA. |
Returns: array|\WP_Error — Decoded tree body or error.
get_file_content()
Get raw file content from the Contents API.
Since: 3.1.0
Line: 249
public function get_file_content( string $owner, string $repo, string $path, string $ref = ... ): string|\WP_Error;
| Type | Name | Description |
|---|---|---|
string | $owner | Repository owner. |
string | $repo | Repository name. |
string | $path | File path within the repo. |
string | $ref | Optional branch, tag, or SHA. |
Returns: string|\WP_Error — Raw file content or error.
get_default_branch()
Get the default branch name for a repository.
Since: 3.1.0
Line: 279
public function get_default_branch( string $owner, string $repo ): string|\WP_Error;
| Type | Name | Description |
|---|---|---|
string | $owner | Repository owner. |
string | $repo | Repository name. |
Returns: string|\WP_Error — Default branch name or error.
validate_token()
Validate the configured PAT by calling GET /user.
Returns an array with ‘user’ (decoded body) and ‘scopes’ (value of the X-OAuth-Scopes response header, empty string when absent — indicating a fine-grained PAT).
Since: 3.1.0
Line: 298
public function validate_token( ): array{user:;
Returns: array{user: — array<string,mixed>, scopes: string}|\WP_Error
get_file_sha()
Get the current blob SHA for a file in a repository.
Required before calling update_file() — GitHub’s Contents API PUT needs the current SHA to confirm you are updating the right version.
Returns empty string when the file does not exist yet (HTTP 404), which signals to update_file() that it should create the file rather than update it. Returns WP_Error for any other failure (auth, rate-limit, network) so the caller can surface a meaningful message instead of silently attempting a create.
Since: 3.1.0
Line: 337
public function get_file_sha( string $owner, string $repo, string $path, string $ref = ... ): string|\WP_Error;
| Type | Name | Description |
|---|---|---|
string | $owner | Repository owner. |
string | $repo | Repository name. |
string | $path | File path within the repo. |
string | $ref | Optional branch, tag, or SHA. |
Returns: string|\WP_Error — Blob SHA, empty string if the file does not exist, or WP_Error on failure.
update_file()
Create or update a file in a repository via PUT /repos/{owner}/{repo}/contents/{path}.
Since: 3.1.0
Line: 382
public function update_file( string $owner, string $repo, string $path, string $content, string $message, string $sha, string $ref = ..., array $author = ... ): array|\WP_Error;
| Type | Name | Description |
|---|---|---|
string | $owner | Repository owner. |
string | $repo | Repository name. |
string | $path | File path within the repo. |
string | $content | Base64-encoded file content. |
string | $message | Commit message. |
string | $sha | Current blob SHA (from get_file_sha()). Pass empty string for new files. |
string | $ref | Optional branch to commit to. |
array | $author | Optional array with ‘name’ and ‘email’ keys. Omit to use the token owner. |
Returns: array|\WP_Error — Decoded GitHub API response or WP_Error on failure.
compute_blob_sha() static
Compute the git blob SHA for raw file content without any API call.
Git uses SHA1(“blob ” + byte_length + NUL + content) for every blob object. This matches the SHA GitHub stores in tree listings, enabling skip-if-unchanged detection before touching the network.
Since: 3.1.0
Line: 460
static public function compute_blob_sha( string $content ): string;
| Type | Name | Description |
|---|---|---|
string | $content | Raw file content (not base64-encoded). |
Returns: string — 40-character lowercase hex SHA1 digest.
get_branch_info()
Get the latest commit SHA and its root-tree SHA for a branch.
Since: 3.1.0
Line: 474
public function get_branch_info( string $owner, string $repo, string $branch ): array{commit_sha:;
| Type | Name | Description |
|---|---|---|
string | $owner | Repository owner. |
string | $repo | Repository name. |
string | $branch | Branch name. |
Returns: array{commit_sha: — string, tree_sha: string}|\WP_Error
create_blob()
Create a git blob object and return its SHA.
Since: 3.1.0
Line: 505
public function create_blob( string $owner, string $repo, string $content_b64 ): string|\WP_Error;
| Type | Name | Description |
|---|---|---|
string | $owner | Repository owner. |
string | $repo | Repository name. |
string | $content_b64 | Base64-encoded file content. |
Returns: string|\WP_Error — Blob SHA or WP_Error on failure.
create_tree()
Create a git tree object and return its SHA.
Each $items entry must have: path (string), mode (string, e.g. “100644”), type (“blob”), sha (blob SHA from create_blob()).
Since: 3.1.0
Line: 550
public function create_tree( string $owner, string $repo, array $items, string $base_tree_sha ): string|\WP_Error;
| Type | Name | Description |
|---|---|---|
string | $owner | Repository owner. |
string | $repo | Repository name. |
array | $items | Tree items to create or update. |
string | $base_tree_sha | Root-tree SHA of the parent commit (keeps unmodified files). |
Returns: string|\WP_Error — New tree SHA or WP_Error on failure.
create_commit()
Create a git commit object and return its SHA.
Since: 3.1.0
Line: 594
public function create_commit( string $owner, string $repo, string $message, string $tree_sha, string $parent_sha, array $author = ... ): string|\WP_Error;
| Type | Name | Description |
|---|---|---|
string | $owner | Repository owner. |
string | $repo | Repository name. |
string | $message | Commit message. |
string | $tree_sha | SHA of the new root tree. |
string | $parent_sha | SHA of the parent commit. |
array | $author | Optional array with ‘name’ and ‘email’ keys. |
Returns: string|\WP_Error — Commit SHA or WP_Error on failure.
update_ref()
Fast-forward a branch to a new commit SHA.
Uses non-force update; returns WP_Error when the branch cannot be fast-forwarded (e.g. concurrent push landed between list and commit).
Since: 3.1.0
Line: 643
public function update_ref( string $owner, string $repo, string $branch, string $commit_sha ): true|\WP_Error;
| Type | Name | Description |
|---|---|---|
string | $owner | Repository owner. |
string | $repo | Repository name. |
string | $branch | Branch name. |
string | $commit_sha | New commit SHA to point the branch at. |
Returns: true|\WP_Error — True on success, WP_Error on failure.