Scriptlog Docs

Scriptlog Documentation

Code reference for the Scriptlog codebase

User Tools

Site Tools


scriptlog:lib:dao:postdao

PostDao

Layer: DAO · Source: lib/dao/PostDao.php:23 (lines 23–926)


class PostDao extends Dao

Docblock Metadata

Tag Value
@category Dao Class
@author M.Noermoehammad
@license MIT
@version 1.0
@since Since Release 1.0

Inheritance

Relation Type Meaning
extends ''Dao'' parent class/interface

Constants (3)

Visibility Name Value Line
private ALLOWED_SORT_COLUMNS ['ID', 'post_date', 'post_title', 'post_modified']; 30
private PUBLISHED_FILTER “p.post_status = 'publish' AND p.post_visibility = 'public'”; 38
private SELECT_PUBLISHED_COLUMNS “p.ID, p.media_id, p.post_author, p.post_date, p.post_modified, p.post_title, p.post_slug… 45

Properties (0)

None declared.

Methods (31)

Visibility Method Summary Line
public __construct() (undocumented) 50
public findPosts() findPosts 65
public findPublishedPostsPaginated() Find published posts with pagination for API endpoints. 121
public countPublishedPosts() Count published posts. 158
public findPublishedPostById() Find a single published post by ID. 186
public getPostById() Retrieve a single post by its ID regardless of status/visibility. 216
public findAdjacentPost() Find the adjacent published blog post relative to the given ID. 251
public findPost() findPost() 281
public createPost() createPost 333
public updatePost() updatePost 385
public publishDueScheduledPosts() Publish all scheduled posts whose post_date has passed. 460
public nextScheduledPostDate() Get the earliest post_date among still-scheduled posts. 502
public deletePost() DeletePost 519
public anonymizePostAuthor() Reassign every post authored by $authorId to a fallback author. 549
public checkPostId() checkPostId 565
public totalPostRecords() Total posts records. 585
public findArchiveIndex() Get distinct year-month combinations with post counts for archive index. 607
public findPostsByYear() Find published posts by year with pagination. 637
public countPostsByYear() Count published posts for a given year. 664
public findPostsByYearMonth() Find published posts by year and month with pagination. 691
public countPostsByYearMonth() Count published posts for a given year and month. 720
public searchPostsApi() Search posts with LIKE for API query endpoint. 745
public findTopicsByPostId() Find topics/categories attached to a post. 788
public findActiveTopicsByPostId() Find active topics/categories attached to a post. 814
public deletePostTopics() Delete all topic relationships for a post. 833
public setPostTopics() Replace all topic relationships for a post. 849
public deletePostComments() Delete all comments for a post. 867
public insertPostApi() Insert a post with the given data and return the new ID. 879
public updatePostApi() Update specific post fields. 894
private resolveSortColumn() Resolve a safe ORDER BY column from a user-supplied sort key. 907
private resolveSortDirection() Resolve a safe ORDER BY direction ('ASC' or 'DESC'). 922

__construct()

public function __construct()

lines 50–53 (4)

Takes no parameters.

findPosts()

public function findPosts(string $orderBy = 'ID', ?int $author = null, bool $onlyPublished = true): array

lines 65–108 (44)

findPosts

Retrieving all records from table posts

Parameter Type Default Description
$orderBy string 'ID' none
$author ?int null none
$onlyPublished bool true none

Returns: array — array

Throws: ''DbException''

findPublishedPostsPaginated()

public function findPublishedPostsPaginated(int $limit, int $offset, string $sortBy = 'ID', string $sortOrder = 'DESC', ?int $author = null): array

lines 121–149 (29)

Find published posts with pagination for API endpoints.

Parameter Type Default Description
$limit int required none
$offset int required none
$sortBy string 'ID' none
$sortOrder string 'DESC' none
$author ?int null none

Returns: array — array

Throws: ''DbException''

countPublishedPosts()

public function countPublishedPosts(?int $author = null): int

lines 158–177 (20)

Count published posts.

Parameter Type Default Description
$author ?int null none

Returns: int — int

Throws: ''DbException''

findPublishedPostById()

public function findPublishedPostById(int $postId): ?array

lines 186–204 (19)

Find a single published post by ID.

Parameter Type Default Description
$postId int required none

Returns: ?array — array|null

Throws: ''DbException''

getPostById()

public function getPostById(int $postId): ?array

lines 216–234 (19)

Retrieve a single post by its ID regardless of status/visibility.

Used by the API layer so callers never reach into the DAO base class internals (setSQL/findRow) to read a post.

Parameter Type Default Description
$postId int required none

Returns: ?array — array|null

Throws: ''DbException''

findAdjacentPost()

public function findAdjacentPost(int $postId, string $direction = 'previous'): ?array

lines 251–266 (16)

Find the adjacent published blog post relative to the given ID.

Used by the theme previous/next post navigation. When $direction is 'previous' it returns the post with the largest ID below $postId; when 'next' it returns the post with the smallest ID above $postId.

Parameter Type Default Description
$postId int required The reference post ID.
$direction string 'previous' 'previous' or 'next'.

Returns: ?array — array|null Adjacent post row (ID, post_title, post_slug) or null.

Throws: ''DbException''

findPost()

public function findPost(int $ID, Sanitize $sanitize, ?int $author = null, bool $onlyPublished = true): ?array

lines 281–321 (41)

findPost()

Retrieving a single post record by its ID.

Parameter Type Default Description
$ID int required none
$sanitize ''Sanitize'' required none
$author ?int null none
$onlyPublished bool true none

Returns: ?array — array|null

Throws: ''DbException'', \InvalidArgumentException

createPost()

public function createPost(array $bind, $topicId): int

lines 333–371 (39)

createPost

Insert a new post record together with its topic relationships.

Parameter Type Default Description
$bind array required none
$topicId (untyped) required none

Returns: int — int

Throws: \InvalidArgumentException

updatePost()

public function updatePost(Sanitize $sanitize, array $bind, int $ID, $topicId): void

lines 385–447 (63)

updatePost

Updating an existing post record together with its topic relationships.

Parameter Type Default Description
$sanitize ''Sanitize'' required none
$bind array required none
$ID int required none
$topicId (untyped) required none

Returns: void — void

Throws: \InvalidArgumentException

publishDueScheduledPosts()

public function publishDueScheduledPosts(string $now): int

lines 460–495 (36)

Publish all scheduled posts whose post_date has passed.

Promotes scheduled posts to 'publish' and flips the featured image media_access to 'public'. Runs both UPDATEs inside a single transaction and clears the page cache when any post is promoted.

Parameter Type Default Description
$now string required Current datetime in site timezone ('Y-m-d H:i:s')

Returns: int — int Number of posts promoted

Throws: \Throwable

nextScheduledPostDate()

public function nextScheduledPostDate(): ?string

lines 502–509 (8)

Get the earliest post_date among still-scheduled posts.

Takes no parameters.

Returns: ?string — string|null Next scheduled datetime ('Y-m-d H:i:s'), or null when none

deletePost()

public function deletePost(int $ID, Sanitize $sanitize): void

lines 519–527 (9)

DeletePost

Parameter Type Default Description
$ID int required none
$sanitize ''Sanitize'' required none

Returns: void — void

Throws: \InvalidArgumentException

anonymizePostAuthor()

public function anonymizePostAuthor(int $authorId, int $fallbackAuthorId = 1): bool

lines 549–554 (6)

Reassign every post authored by $authorId to a fallback author.

The fallback must be validated by the caller (it should exist and must not be the author being erased); it defaults to user ID 1 for backward compatibility with existing callers.

Parameter Type Default Description
$authorId int required The author whose posts are being reassigned
$fallbackAuthorId int 1 The author the posts are reassigned to

Returns: bool — bool

checkPostId()

public function checkPostId(int $ID, Sanitize $sanitize): bool

lines 565–576 (12)

checkPostId

Parameter Type Default Description
$ID int required none
$sanitize ''Sanitize'' required none

Returns: bool — bool

Throws: \InvalidArgumentException, ''DbException''

totalPostRecords()

public function totalPostRecords(?int $author = null): int

lines 585–599 (15)

Total posts records.

Parameter Type Default Description
$author ?int null none

Returns: int — int

Throws: ''DbException''

findArchiveIndex()

public function findArchiveIndex(): array

lines 607–624 (18)

Get distinct year-month combinations with post counts for archive index.

Takes no parameters.

Returns: array — array

Throws: ''DbException''

findPostsByYear()

public function findPostsByYear(int $year, int $limit, int $offset, string $sortBy = 'ID', string $sortOrder = 'DESC'): array

lines 637–655 (19)

Find published posts by year with pagination.

Parameter Type Default Description
$year int required none
$limit int required none
$offset int required none
$sortBy string 'ID' none
$sortOrder string 'DESC' none

Returns: array — array

Throws: ''DbException''

countPostsByYear()

public function countPostsByYear(int $year): int

lines 664–677 (14)

Count published posts for a given year.

Parameter Type Default Description
$year int required none

Returns: int — int

Throws: ''DbException''

findPostsByYearMonth()

public function findPostsByYearMonth(int $year, int $month, int $limit, int $offset, string $sortBy = 'ID', string $sortOrder = 'DESC'): array

lines 691–710 (20)

Find published posts by year and month with pagination.

Parameter Type Default Description
$year int required none
$month int required none
$limit int required none
$offset int required none
$sortBy string 'ID' none
$sortOrder string 'DESC' none

Returns: array — array

Throws: ''DbException''

countPostsByYearMonth()

public function countPostsByYearMonth(int $year, int $month): int

lines 720–734 (15)

Count published posts for a given year and month.

Parameter Type Default Description
$year int required none
$month int required none

Returns: int — int

Throws: ''DbException''

searchPostsApi()

public function searchPostsApi(string $keyword, string $type = 'all', int $limit = 50): array

lines 745–779 (35)

Search posts with LIKE for API query endpoint.

Parameter Type Default Description
$keyword string required none
$type string 'all' 'blog', 'page', or 'all'
$limit int 50 none

Returns: array — array

Throws: ''DbException''

findTopicsByPostId()

public function findTopicsByPostId(int $postId): array

lines 788–799 (12)

Find topics/categories attached to a post.

Parameter Type Default Description
$postId int required none

Returns: array — array

Throws: ''DbException''

findActiveTopicsByPostId()

public function findActiveTopicsByPostId(int $postId): array

lines 814–825 (12)

Find active topics/categories attached to a post.

Same as findTopicsByPostId() but only returns topics whose topic_status flag is 'Y' (visible on the frontend).

Parameter Type Default Description
$postId int required none

Returns: array — array

Throws: ''DbException''

deletePostTopics()

public function deletePostTopics(int $postId): void

lines 833–836 (4)

Delete all topic relationships for a post.

Parameter Type Default Description
$postId int required none

Returns: void — void

setPostTopics()

public function setPostTopics(int $postId, array $topicIds): void

lines 849–859 (11)

Replace all topic relationships for a post.

Deletes existing relationships, then inserts the given topic IDs. Used by the API layer to persist a post's category assignments.

Parameter Type Default Description
$postId int required none
$topicIds array required List of topic IDs.

Returns: void — void

Throws: \InvalidArgumentException

deletePostComments()

public function deletePostComments(int $postId): void

lines 867–870 (4)

Delete all comments for a post.

Parameter Type Default Description
$postId int required none

Returns: void — void

insertPostApi()

public function insertPostApi(array $data): int

lines 879–884 (6)

Insert a post with the given data and return the new ID.

Parameter Type Default Description
$data array required Column ⇒ value pairs

Returns: int — int

Throws: \InvalidArgumentException

updatePostApi()

public function updatePostApi(int $postId, array $data): void

lines 894–897 (4)

Update specific post fields.

Parameter Type Default Description
$postId int required none
$data array required Column ⇒ value pairs

Returns: void — void

Throws: \InvalidArgumentException

resolveSortColumn()

private function resolveSortColumn(string $sortBy): string

lines 907–912 (6)

Resolve a safe ORDER BY column from a user-supplied sort key.

Returns a fallback of 'ID' when the requested column is not whitelisted.

Parameter Type Default Description
$sortBy string required none

Returns: string — string

resolveSortDirection()

private function resolveSortDirection(string $sortOrder): string

lines 922–925 (4)

Resolve a safe ORDER BY direction ('ASC' or 'DESC').

Any value other than ASC falls back to DESC.

Parameter Type Default Description
$sortOrder string required none

Returns: string — string

Imports


This page is generated from source by 'tools/gendoc'. Edits will be overwritten.

scriptlog/lib/dao/postdao.txt · Last modified: by admin