Recent

APIclassPro only

API

API class.

Namespace: WebberZone\Knowledge_Base\Pro\GitHub
Since: 3.1.0
Source: includes/pro/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: 185

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: 206

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: 230

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: 256

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: 286

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: 305

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 update_file(); returns ‘’ on 404 (signals create-not-update) or WP_Error on any other failure.

Since: 3.1.0
Line: 339

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: 384

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: 462

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: 476

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: 507

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”), and type (“blob”), plus either sha (an existing blob SHA) or content (raw file content).

Since: 3.1.0
Line: 552

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: 597

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: 646

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.