| 1 |
<?php |
| 2 |
/** |
| 3 |
* Pages app — the Pages-specific server surface: the default REST |
| 4 |
* query args, the page-template label map and the |
| 5 |
* `openstation_comment_count` REST field on `page`. |
| 6 |
* |
| 7 |
* Plain PHP part of `apps/pages/pages.os.php`. The list machinery |
| 8 |
* itself (query builder, data envelope, actions) is the Posts app's |
| 9 |
* `apps/posts/parts/query.php`, which the entry requires too. |
| 10 |
* |
| 11 |
* @package OpenStation |
| 12 |
*/ |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Default REST query args for the Pages window. |
| 18 |
* |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
function openstation_pages_window_default_query_args() { |
| 22 |
$args = array( |
| 23 |
// Pages are usually shallow + ordered by menu_order; embed the |
| 24 |
// author + featured media for the table cells. |
| 25 |
'_embed' => 'author,wp:featuredmedia', |
| 26 |
// `slug`, `template`, `link` and the custom `openstation_comment_count` |
| 27 |
// field feed the Slug / Template / Comments columns and the |
| 28 |
// public-URL "View" quick-action. Missing them from the |
| 29 |
// whitelist costs nothing on the wire but silently breaks the |
| 30 |
// column — keep them here. |
| 31 |
'_fields' => |
| 32 |
'id,title,status,date,date_gmt,modified,modified_gmt,author,parent,menu_order,slug,link,template,comment_status,excerpt,openstation_lock,openstation_comment_count,_links,_embedded', |
| 33 |
'orderby' => 'menu_order', |
| 34 |
'order' => 'asc', |
| 35 |
); |
| 36 |
|
| 37 |
/** |
| 38 |
* Filter the default outbound REST query args for the Pages window. |
| 39 |
* |
| 40 |
* @param array $args Default args. |
| 41 |
*/ |
| 42 |
return (array) apply_filters( 'openstation_pages_window_query_args', $args ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* The Pages app's `ctx.extra`: the shared list facts under the pages |
| 47 |
* mode, plus the reading-page assignments (the title cell paints the |
| 48 |
* "Front page" / "Posts page" badges on matching rows; `0` when |
| 49 |
* unset) and the page-template label map. |
| 50 |
* |
| 51 |
* @return array<string,mixed> |
| 52 |
*/ |
| 53 |
function openstation_pages_app_config() { |
| 54 |
$config = openstation_posts_app_config( 'pages', 'menu_order', 'asc' ); |
| 55 |
$config['frontPageId'] = (int) get_option( 'page_on_front', 0 ); |
| 56 |
$config['postsPageId'] = (int) get_option( 'page_for_posts', 0 ); |
| 57 |
$config['pageTemplates'] = openstation_pages_window_template_labels(); |
| 58 |
return $config; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Build the `{ slug: label }` map for the active theme's registered |
| 63 |
* page templates. The Template column paints friendly labels instead |
| 64 |
* of raw filenames. |
| 65 |
* |
| 66 |
* The "default" template (assigned when `page.template` is `''`) is |
| 67 |
* keyed under the empty string for parity with what core returns in |
| 68 |
* `/wp/v2/pages` responses. |
| 69 |
* |
| 70 |
* @return array<string,string> |
| 71 |
*/ |
| 72 |
function openstation_pages_window_template_labels() { |
| 73 |
$labels = array( |
| 74 |
'' => __( 'Default template', 'desktop-mode' ), |
| 75 |
); |
| 76 |
if ( function_exists( 'wp_get_theme' ) ) { |
| 77 |
$theme = wp_get_theme(); |
| 78 |
if ( $theme && method_exists( $theme, 'get_page_templates' ) ) { |
| 79 |
$registered = (array) $theme->get_page_templates( null, 'page' ); |
| 80 |
foreach ( $registered as $slug => $label ) { |
| 81 |
$labels[ (string) $slug ] = (string) $label; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Filter the page-template label map handed to the Pages window. |
| 88 |
* |
| 89 |
* @param array<string,string> $labels Slug → human label. |
| 90 |
*/ |
| 91 |
return (array) apply_filters( 'openstation_pages_window_template_labels', $labels ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Register the `openstation_comment_count` REST field on `page`. |
| 96 |
* |
| 97 |
* The default `/wp/v2/pages` response doesn't include a comment |
| 98 |
* count — surfacing one alongside the row keeps parity with the |
| 99 |
* classic Pages list without forcing the table to embed |
| 100 |
* `_embed=replies`, which is heavy and N+1. |
| 101 |
*/ |
| 102 |
function openstation_pages_window_register_comment_count_field() { |
| 103 |
register_rest_field( |
| 104 |
'page', |
| 105 |
'openstation_comment_count', |
| 106 |
array( |
| 107 |
'get_callback' => static function ( $row ) { |
| 108 |
$id = isset( $row['id'] ) ? (int) $row['id'] : 0; |
| 109 |
if ( $id <= 0 ) { |
| 110 |
return 0; |
| 111 |
} |
| 112 |
return (int) get_comments_number( $id ); |
| 113 |
}, |
| 114 |
'schema' => array( |
| 115 |
'description' => __( 'Total non-trashed comments on this page.', 'desktop-mode' ), |
| 116 |
'type' => 'integer', |
| 117 |
'context' => array( 'view', 'embed' ), |
| 118 |
'readonly' => true, |
| 119 |
), |
| 120 |
) |
| 121 |
); |
| 122 |
} |
| 123 |
add_action( 'rest_api_init', 'openstation_pages_window_register_comment_count_field' ); |
| 124 |
|