Pages drawn from data
Every page of Poly Community is one static file of its type, the same for every reader, and one JSON answer that a move asks for. The browser draws the page from the two. This page is for a developer, or a coding agent, adding pages or blocks to Poly Community that work the same way. Moving between pages explains the mechanics underneath, and Building for instant pages and live updates the rows, lists and events.
The rules
- A page type is one file. The page of every member, of every group, of every event is drawn once and kept as a static file, named by a hash of what it is drawn from. It holds templates and marks. It holds nothing of one reader and nothing of one subject.
- A move is the file and one answer. The answer brings the rows, lists, parts and facts the page shows. Nothing else is asked, and nothing PHP draws is fetched after a page has loaded.
- The browser draws everything. The only HTML inside the data is text members wrote (a post, a comment, a bio), already made safe. A section that shows an item, such as a title, its facts or its about, is a template in the file over the data of the item.
- The reader is data too. What a reader may do and what they are (logged in, the owner, a moderator) are facts the answer brings. The file shows a part by them.
- A page loaded whole shows the same. The server still draws a page loaded whole, for a first visit and for search engines. It must show what the file and its data show, and the tests compare the two.
The API
Say once what the pages of your plugin show, on the poly_community_register_page_data action. It passes the registry, Poly_Community_Page_Data.
add_action( 'poly_community_register_page_data', 'my_rooms_page_data' );
function my_rooms_page_data( $data ) {
$data->block( 'my-rooms/room-head' );
$data->part( 'myRoomBooking', array(
'of' => 'post',
'answer' => 'my_rooms_booking_of',
) );
$data->fact( 'myRoomsCanBook', 'my_rooms_can_book' );
$data->owner_block( 'my-rooms/room-manage', array( 'kinds' => array( 'room' ) ) );
$data->mark( 'post', '~(data-my-room=")%d(?=")~' );
}
block
$data->block( $name ) lists a block a page file may hold. List it once it draws what every reader sees, and takes what some readers alone see from parts and facts. A block that is not listed keeps its page from having a file, and the check command says which block it is.
part
$data->part( $name, $args ) is data each reader gets with the move, and on a page loaded whole. A template in the file binds it as state.partHere.
| Option | What it is |
|---|---|
of | What its key is the id of: post (a post, or the item of a component page), profile, space, or page. |
answer | A function ( $subject, $context, $args ) that returns the data for the reader: an array, never HTML. $subject is the post, member or group, already checked to be one the reader may open. $context is the reader, from Poly_Community()->access->viewer(). |
route | Instead of answer, a route of your own under poly-community/v1, with {id} where the subject goes. |
teaser | true where answer is also given a post the reader sees only as its teaser, and returns what the teaser shows. By default a part answers a reader who may open the post. |
A part with an answer needs no route of its own. The registry answers it at page-data/parts/{name}/{id}, and the move asks it inside its one answer.
A block draws its part with one template, on a page loaded whole and in a file alike. here() gives the context of the template: on a page loaded whole it puts the data for the reader, and in a file it names the part, so the move brings it.
<div class="my-rooms-booking" data-wp-context="<?php echo esc_attr( Poly_Community()->page_data->here( 'myRoomBooking', $item->id ) ); ?>">
<span data-wp-text="state.partHere.status"></span>
<a data-wp-bind--href="state.partHere.url" data-wp-bind--hidden="!state.partHere.url" hidden>Book a time</a>
</div>
function my_rooms_booking_of( $item, $context, $args ) {
$booking = my_rooms_booking_for( $item->id, $context['user_id'] );
return array(
'status' => $booking ? $booking['when_words'] : '',
'url' => $context['logged_in'] ? my_rooms_book_url( $item->id ) : '',
);
}
Give here() a third argument for a part that a block draws in more than one way, such as a short and a long look. It is kept in the key of the part and passed to answer as $args. A fourth argument adds keys of your own to the context of the template, such as the id your actions read.
A template draws what every item could show, each section hidden by its data: a room with no booking has the booking line hidden, not left out. One file then serves a room with a booking and a room without.
fact
$data->fact( $name, $callback ) is a fact of the reader the move brings, worked out once a move. A file shows a part by it with Poly_Community_Json_Moves::for_readers( $html, 'myRoomsCanBook' ), and a template reads it from the reader. The callback takes the reader context and returns a boolean, a string or a list.
owner_block
$data->owner_block( $name, array( 'kinds' => array( 'room' ) ) ) is a block drawn for those who may change the item alone, such as a bar to edit or end it. A file of the page draws none of it, and is drawn for readers who may not change the item. Whoever may change it moves to the page by its HTML, so they still see the bar. Administrators may change every item, so they load such a page whole, and the check says so.
Where more readers use the bar than may edit the item, such as the co-hosts of an event or the moderators of its group, give can: a function ( $item, $context ) that says whether the reader is one the bar is drawn for.
$data->owner_block( 'my-rooms/room-manage', array(
'kinds' => array( 'room' ),
'can' => 'my_rooms_can_manage',
) );
managed
$data->managed( $kind, $can ) says that those who manage an item of the kind see its page otherwise, with no bar of their own: a course team sees Edit course where a student sees Start. As with an owner block, no file is drawn as one of them, and they move to the page by its HTML. Those who may change the item count by default, and $can adds others: a function ( $item, $context ).
drawn_whole_for
$data->drawn_whole_for( $kind, $callback ) names readers an item page is drawn whole for, because a part of it shows them something that is not data yet, such as their own review to change. Use * as the kind for every kind. The callback takes the item and the reader context and returns whether the page is drawn whole for them. No file is drawn as one of them, and the check never picks one of them to draw it as.
drawn_whole
$data->drawn_whole( $kind, $callback ) names items of a kind whose page the server still draws whole, because a part of it is not drawn from data yet. The callback takes the item and returns why, or an empty string where its page is drawn from data. The page file serves the other items, and a reader moves to one of these by its HTML. Use it while you convert a page piece by piece, and take it away once the last piece is data.
$data->drawn_whole( 'room', 'my_rooms_drawn_whole' );
function my_rooms_drawn_whole( $item ) {
return my_rooms_takes_deposits( $item->id ) ? 'deposits' : '';
}
mark
$data->mark( $kind, $pattern, $replacement ) says where a block writes the id of the subject of a page of many subjects, so one file serves every member, group or post. $pattern is a pattern for preg_replace() with %d where the id stands. The default replacement keeps the first group and puts {{s}} for the id. Where a block writes the account of the member or of the writer of the post, put %u in the pattern and {{s:user}} in the replacement. For the profile of the writer of a post, put %b and {{s:by}}. A pattern with %u or %b is left out for a subject that has none, such as a post written as no one. The ids in the routes of your parts are marked for you.
A recipe: the page of one item
A component page of one item, such as the page of a room, is one file for every room of its kind. To get it there:
- Run the check.
wp poly-community page-data check kit-roomsays, for each kind of reader, the first reason the page has no file. Fix them in order. - Draw the item from its row. Each section that shows the room (its name, its facts, its about) binds the row of the item in the store,
state.rows.item[ id ], and the data your kind adds to it withpoly_community_item_row_data. The context of the section names the row:data-wp-context='{"rowKey":"item","item":{"id":…}}'. - Put what the reader alone sees in parts. Their booking, their review, whether they may book: a part with an
answer, drawn withhere(). - Show by facts what a reader may do. A Book button for a reader who may book is drawn for every reader and shown by a fact.
- List the owner blocks. The bar its owner uses to change the room is an owner block.
- List the blocks. Each block of the page, once it draws so, with
block(). - Mark the ids. Where a block writes the id of the room in an attribute of its own, a mark.
- Check again, until every kind says it is drawn, then test it: move to the page from a list and compare what it shows with a page loaded whole, for a visitor, a member and the owner.
Items in a group
A room posted in a group has a page type of its own beside the one for rooms outside groups: kit-room-space, as a post in a group has space-post. Its file is drawn for two rooms in two groups named otherwise, so whatever the page draws of one group differs between the drawings and nothing of a group stays in the file. Check it with wp poly-community page-data check kit-room-space.
- The name of the group in the trail of the page is marked for you (
{{s:place}}, from the row of the room). - Anything else of the group, such as its rules or its own widgets beside the page, comes from your part. Put it in the answer, bind the widget to it, and bind any attribute that names the group, such as
data-poly-room-space, from the part too. - A move to the page reads the row of the room in the list it was pressed in. That row says its group (
space,placewith a name and an address), asItem_Rows::move_fields()writes it.
A forum topic is always in a board and has the page type forum-topic. A question in a Q&A topic has qa-question. The topic page and the question page are the worked examples of a page in a group: inc/forum-page-data.php and inc/question-page-data.php. A key your page names in its state that the browser reads after a move, such as the key of a list it draws, must be one the file brings: add it with the filter poly_community_json_move_own.
Your block on a page of ours
A block your plugin adds to a page of ours, such as a button on the question page, keeps that page from a file until it draws from data too. Register a part of your own for it, and draw the block from it in a file:
- Draw every shape of it. A block that shows otherwise to a moderator and to a member draws both in the file, each hidden until the part says it is theirs.
- Take the context from the part. The ids your actions read, such as the question the vote is for, come in the part under a key with
context, taken withpartContextOfanddata-wp-watch---part="callbacks.partContext". A callback that has to run once the id is known is adata-wp-watch, not adata-wp-init. - Mark the rest. An id the block writes in an attribute, such as the name of a radio group, is a mark of yours.
- Bring data in the part. A grid of photos takes its first tiles from the answer, so the move asks nothing more.
Vote to close of the reputation kit is a worked example (kits/reputation/inc/trait-page-data.php, the part closeVote), and so is the gallery of an event in the photo albums kit (kits/photo-albums/inc/trait-page-data.php, the part albumGrid).
Templates in the browser
- Bind a part with
state.partHere, a row with the row named by the context, a list withdata-wp-each. - Hide with
data-wp-bind--hidden, and draw the element hidden on the server where it starts hidden, so a page loaded whole shows the same. - Work out what a template shows from data where it is shown: a date in the reader zone, a count in words. Write it as a getter in the store of your plugin, and the same rule in the server pass.
- Text members wrote is the only HTML a template puts in place, with
data-wp-html. - A template whose own context comes from the reader, such as a Watch button that holds whether they watch, takes it from the part. Put it under a key of the answer as
context('watch' => array( 'context' => array( 'watching' => true ) )). On the element, name that key in its context aspartContextOfand adddata-wp-watch---part="callbacks.partContext". Each answer of the part is taken once, so what the reader changes after stays until the part is answered again. - A list your page holds is named in its file by its key in one of the list slots of the state (
tileKeys,feedKeys,reviewKeys,treeKeys,threadKeys), never put, and asked with the move. Register its kind withregister_list()so the move can answer it: the posts of a topic are the list kindthread.
Checking a page
| How | What it says |
|---|---|
wp poly-community page-data check <page> | Makes the file of the page now and says, for a visitor, a member and an administrator, whether it is drawn and what to change. |
wp poly-community page-data list | Every page and whether it is one drawing, as last made. |
/?poly_moves_made=1, as an administrator | The same in a browser, with each kind and why. |
data-poly-move on <html> | After a move: file where it was drawn from its file, else html: and why. |
Why a page has no file
Each reason the check command gives, by how it begins, and what to change.
| The reason begins | What to change |
|---|---|
its block | A block draws something of one reader or one moment. Draw what every reader sees, put what some readers alone see in a part or behind a fact, then list the block with block(). |
it read | The drawing read rows, lists or parts of the moment. Name lists unread, draw parts with here(), and keep reads of one reader out of what the file draws. |
two readers drawn otherwise | Two readers of one kind got different HTML at the place shown. Something of the reader is drawn there: make it a part, or mark it with a fact. |
parts otherwise for some readers | A part was drawn for some readers and not others. Draw its holder for every reader and let its data or a fact hide it. |
a moderator drawn otherwise | A moderator saw more than a member. Draw the moderator parts for every reader, each shown by a fact. |
a member of another fit | Members of different fits got different drawings. Show the difference by a fact. |
no two readers of the kind | There are not two readers of this kind who reach the page. Nothing to change in code. |
no two | There are not two members, posts or items to draw the page for. Nothing to change: it is made again an hour after, once the site has two. |
no menu links to | Only pages a menu links to, their tabs and pages of many subjects get a file. Link the page from a menu. |
its address answers | The page answers this reader with an error or the login. Those readers load it whole. |
its address asks | The address carries a query only a tab page may carry. Make the view a tab of its page. |
asked for another key | What the page is drawn from changed while it was made. Make it again: it settles. |
its reader may change the post | The page has an owner block. Its file serves the others. Expected for administrators. |
its reader is of no kind | A suspended or blocked reader loads pages whole. |
its widget area holds the widget | A classic widget draws by its own code. Use a block that draws from data, or take it out. |
retry | The site was busy. It is made again later. |
For coding agents
An agent follows the recipe above with the check command as its test: it changes one block, runs the check, and goes on while a kind still gives a reason. The guide to coding agents, out once its skills have a home, has the instructions to give it and the prompts that work.