['type' => 'int', 'min' => 1, 'max' => 2147483647, 'description' => 'The post or page id from aiwend_list_content.'], 'offset' => ['type' => 'int', 'min' => 0, 'max' => 10000000, 'optional' => true, 'description' => 'Character position to continue from when truncated is true (use next_offset). Default 0.'], ]; } public function mutates(): bool { return false; } /** Object check: only what a visitor could read anyway. A draft, private, protected or other-type item looks the same as a missing one. */ public function authorizeObject(Principal $principal, array $args): bool { if ($principal->siteId !== get_current_blog_id()) { return false; } $p = get_post((int) $args['id']); if (!$p instanceof \WP_Post || !in_array($p->post_type, ['post', 'page'], true) || $p->post_status !== 'publish' || $p->post_password !== '' ) { return false; } // The same query as the list tool, so a query filter of an access-control plugin hides the item here as well. $q = new \WP_Query([ 'p' => (int) $p->ID, 'post_type' => $p->post_type, 'post_status' => 'publish', 'has_password' => false, 'posts_per_page' => 1, 'no_found_rows' => true, 'ignore_sticky_posts' => true, 'fields' => 'ids', ]); return $q->posts === [(int) $p->ID]; } public function execute(Principal $principal, array $args): array { $p = get_post((int) $args['id']); if (!$p instanceof \WP_Post) { return []; } $text = trim((string) preg_replace('/\s+/u', ' ', wp_strip_all_tags(strip_shortcodes((string) $p->post_content)))); // A long item is read in bounded pieces: each call returns at most MAX_CHARS characters from the requested position. $total = mb_strlen($text); $offset = (int) ($args['offset'] ?? 0); $slice = mb_substr($text, $offset, self::MAX_CHARS); $end = $offset + mb_strlen($slice); return [ 'untrusted_text' => true, 'id' => (int) $p->ID, 'type' => $p->post_type, 'title' => mb_substr(wp_strip_all_tags(get_the_title($p)), 0, 200), 'url' => (string) get_permalink($p), 'date' => (string) get_post_time('c', true, $p), 'text' => $slice, 'truncated' => $end < $total, 'offset' => $offset, 'next_offset' => $end < $total ? $end : null, 'total_chars' => $total, ]; } }