fse-template-data.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php declare( strict_types = 1 ); ?>
  2. <?php
  3. /**
  4. * Template data inserter file.
  5. *
  6. * @package full-site-editing
  7. */
  8. /**
  9. * Class A8C_WP_Template_Data_Inserter
  10. */
  11. class A8C_WP_Template_Data_Inserter {
  12. /**
  13. * This function will be called on plugin activation hook.
  14. */
  15. public function insert_default_template_data() {
  16. $current_theme_name = get_option( 'stylesheet' );
  17. /**
  18. * This site option will be used to indicate that template data has already been
  19. * inserted for this theme, in order to prevent this functionality from running
  20. * more than once.
  21. */
  22. $fse_template_data_option = $current_theme_name . '-fse-template-data';
  23. if ( get_option( $fse_template_data_option ) ) {
  24. /*
  25. * Bail here to prevent inserting the FSE data twice for any given theme.
  26. * Multiple themes will still be able to insert different template parts.
  27. */
  28. return;
  29. }
  30. $this->register_template_post_types();
  31. $header_id = wp_insert_post(
  32. [
  33. 'post_title' => 'Header',
  34. 'post_content' => $this->get_header_content(),
  35. 'post_status' => 'publish',
  36. 'post_type' => 'wp_template_part',
  37. 'comment_status' => 'closed',
  38. 'ping_status' => 'closed',
  39. ]
  40. );
  41. if ( ! term_exists( "$current_theme_name-header", 'wp_template_part_type' ) ) {
  42. wp_insert_term( "$current_theme_name-header", 'wp_template_part_type' );
  43. }
  44. wp_set_object_terms( $header_id, "$current_theme_name-header", 'wp_template_part_type' );
  45. $footer_id = wp_insert_post(
  46. [
  47. 'post_title' => 'Footer',
  48. 'post_content' => $this->get_footer_content(),
  49. 'post_status' => 'publish',
  50. 'post_type' => 'wp_template_part',
  51. 'comment_status' => 'closed',
  52. 'ping_status' => 'closed',
  53. ]
  54. );
  55. if ( ! term_exists( "$current_theme_name-footer", 'wp_template_part_type' ) ) {
  56. wp_insert_term( "$current_theme_name-footer", 'wp_template_part_type' );
  57. }
  58. wp_set_object_terms( $footer_id, "$current_theme_name-footer", 'wp_template_part_type' );
  59. $page_template_id = wp_insert_post(
  60. [
  61. 'post_title' => 'Page Template',
  62. 'post_content' => $this->get_template_content( $header_id, $footer_id ),
  63. 'post_status' => 'publish',
  64. 'post_type' => 'wp_template',
  65. 'comment_status' => 'closed',
  66. 'ping_status' => 'closed',
  67. ]
  68. );
  69. if ( ! term_exists( "$current_theme_name-page-template", 'wp_template_type' ) ) {
  70. wp_insert_term( "$current_theme_name-page-template", 'wp_template_type' );
  71. }
  72. wp_set_object_terms( $page_template_id, "$current_theme_name-page-template", 'wp_template_type' );
  73. add_option( $fse_template_data_option, true );
  74. }
  75. /**
  76. * Returns default header template part content.
  77. *
  78. * @return string
  79. */
  80. public function get_header_content() {
  81. // TODO: replace with header blocks once they are ready.
  82. return '<!-- wp:group {"className":"site-header site-branding"} -->' .
  83. '<div class="wp-block-group site-header site-branding">' .
  84. '<div class="wp-block-group__inner-container">' .
  85. '<!-- wp:a8c/site-description /-->' .
  86. '<!-- wp:a8c/site-title /-->' .
  87. '<!-- wp:a8c/navigation-menu /-->' .
  88. '</div></div>' .
  89. '<!-- /wp:group -->';
  90. }
  91. /**
  92. * Returns default footer template part content.
  93. *
  94. * @return string
  95. */
  96. public function get_footer_content() {
  97. return '<!-- wp:group {"align":"full","className":"site-footer"} -->' .
  98. '<div class="wp-block-group alignfull site-footer">' .
  99. '<div class="wp-block-group__inner-container">' .
  100. '<!-- wp:separator {"className":"is-style-default"} -->' .
  101. '<hr class="wp-block-separator is-style-default"/>' .
  102. '<!-- /wp:separator -->' .
  103. '<!-- wp:a8c/navigation-menu /-->' .
  104. '</div>' .
  105. '</div>' .
  106. '<!-- /wp:group -->';
  107. }
  108. /**
  109. * Returns default page template content.
  110. *
  111. * @param int $header_id ID of referenced header template part CPT.
  112. * @param int $footer_id ID of referenced footer template part CPT.
  113. *
  114. * @return string
  115. */
  116. public function get_template_content( $header_id, $footer_id ) {
  117. return "<!-- wp:a8c/template {\"templateId\":$header_id} /-->" .
  118. '<!-- wp:a8c/post-content /-->' .
  119. "<!-- wp:a8c/template {\"templateId\":$footer_id} /-->";
  120. }
  121. /**
  122. * Register post types.
  123. */
  124. public function register_template_post_types() {
  125. register_post_type(
  126. 'wp_template',
  127. array(
  128. 'labels' => array(
  129. 'name' => _x( 'Templates', 'post type general name', 'full-site-editing' ),
  130. 'singular_name' => _x( 'Template', 'post type singular name', 'full-site-editing' ),
  131. 'menu_name' => _x( 'Templates', 'admin menu', 'full-site-editing' ),
  132. 'name_admin_bar' => _x( 'Template', 'add new on admin bar', 'full-site-editing' ),
  133. 'add_new' => _x( 'Add New', 'Template', 'full-site-editing' ),
  134. 'add_new_item' => __( 'Add New Template', 'full-site-editing' ),
  135. 'new_item' => __( 'New Template', 'full-site-editing' ),
  136. 'edit_item' => __( 'Edit Template', 'full-site-editing' ),
  137. 'view_item' => __( 'View Template', 'full-site-editing' ),
  138. 'all_items' => __( 'All Templates', 'full-site-editing' ),
  139. 'search_items' => __( 'Search Templates', 'full-site-editing' ),
  140. 'not_found' => __( 'No templates found.', 'full-site-editing' ),
  141. 'not_found_in_trash' => __( 'No templates found in Trash.', 'full-site-editing' ),
  142. 'filter_items_list' => __( 'Filter templates list', 'full-site-editing' ),
  143. 'items_list_navigation' => __( 'Templates list navigation', 'full-site-editing' ),
  144. 'items_list' => __( 'Templates list', 'full-site-editing' ),
  145. 'item_published' => __( 'Template published.', 'full-site-editing' ),
  146. 'item_published_privately' => __( 'Template published privately.', 'full-site-editing' ),
  147. 'item_reverted_to_draft' => __( 'Template reverted to draft.', 'full-site-editing' ),
  148. 'item_scheduled' => __( 'Template scheduled.', 'full-site-editing' ),
  149. 'item_updated' => __( 'Template updated.', 'full-site-editing' ),
  150. ),
  151. 'menu_icon' => 'dashicons-layout',
  152. 'public' => false,
  153. 'show_ui' => true,
  154. 'show_in_menu' => true,
  155. 'rewrite' => false,
  156. 'show_in_rest' => true,
  157. 'rest_base' => 'templates',
  158. 'rest_controller_class' => 'A8C_REST_Templates_Controller',
  159. 'capability_type' => 'template',
  160. 'capabilities' => array(
  161. // You need to be able to edit posts, in order to read templates in their raw form.
  162. 'read' => 'edit_posts',
  163. // You need to be able to customize, in order to create templates.
  164. 'create_posts' => 'edit_theme_options',
  165. 'edit_posts' => 'edit_theme_options',
  166. 'delete_posts' => 'edit_theme_options',
  167. 'edit_published_posts' => 'edit_theme_options',
  168. 'delete_published_posts' => 'edit_theme_options',
  169. 'edit_others_posts' => 'edit_theme_options',
  170. 'delete_others_posts' => 'edit_theme_options',
  171. 'publish_posts' => 'edit_theme_options',
  172. ),
  173. 'map_meta_cap' => true,
  174. 'supports' => array(
  175. 'title',
  176. 'editor',
  177. ),
  178. )
  179. );
  180. register_post_type(
  181. 'wp_template_part',
  182. array(
  183. 'labels' => array(
  184. 'name' => _x( 'Template Parts', 'post type general name', 'full-site-editing' ),
  185. 'singular_name' => _x( 'Template Part', 'post type singular name', 'full-site-editing' ),
  186. 'menu_name' => _x( 'Template Parts', 'admin menu', 'full-site-editing' ),
  187. 'name_admin_bar' => _x( 'Template Part', 'add new on admin bar', 'full-site-editing' ),
  188. 'add_new' => _x( 'Add New', 'Template Part', 'full-site-editing' ),
  189. 'add_new_item' => __( 'Add New Template Part', 'full-site-editing' ),
  190. 'new_item' => __( 'New Template Part', 'full-site-editing' ),
  191. 'edit_item' => __( 'Edit Template Part', 'full-site-editing' ),
  192. 'view_item' => __( 'View Template Part', 'full-site-editing' ),
  193. 'all_items' => __( 'All Template Parts', 'full-site-editing' ),
  194. 'search_items' => __( 'Search Template Parts', 'full-site-editing' ),
  195. 'not_found' => __( 'No template parts found.', 'full-site-editing' ),
  196. 'not_found_in_trash' => __( 'No template parts found in Trash.', 'full-site-editing' ),
  197. 'filter_items_list' => __( 'Filter template parts list', 'full-site-editing' ),
  198. 'items_list_navigation' => __( 'Template parts list navigation', 'full-site-editing' ),
  199. 'items_list' => __( 'Template parts list', 'full-site-editing' ),
  200. 'item_published' => __( 'Template part published.', 'full-site-editing' ),
  201. 'item_published_privately' => __( 'Template part published privately.', 'full-site-editing' ),
  202. 'item_reverted_to_draft' => __( 'Template part reverted to draft.', 'full-site-editing' ),
  203. 'item_scheduled' => __( 'Template part scheduled.', 'full-site-editing' ),
  204. 'item_updated' => __( 'Template part updated.', 'full-site-editing' ),
  205. ),
  206. 'menu_icon' => 'dashicons-layout',
  207. 'public' => false,
  208. 'show_ui' => true,
  209. 'show_in_menu' => true,
  210. 'rewrite' => false,
  211. 'show_in_rest' => true,
  212. 'rest_base' => 'template_parts',
  213. 'rest_controller_class' => 'A8C_REST_Templates_Controller',
  214. 'capability_type' => 'template_part',
  215. 'capabilities' => array(
  216. // You need to be able to edit posts, in order to read templates in their raw form.
  217. 'read' => 'edit_posts',
  218. // You need to be able to customize, in order to create templates.
  219. 'create_posts' => 'edit_theme_options',
  220. 'edit_posts' => 'edit_theme_options',
  221. 'delete_posts' => 'edit_theme_options',
  222. 'edit_published_posts' => 'edit_theme_options',
  223. 'delete_published_posts' => 'edit_theme_options',
  224. 'edit_others_posts' => 'edit_theme_options',
  225. 'delete_others_posts' => 'edit_theme_options',
  226. 'publish_posts' => 'edit_theme_options',
  227. ),
  228. 'map_meta_cap' => true,
  229. 'supports' => array(
  230. 'title',
  231. 'editor',
  232. ),
  233. )
  234. );
  235. register_taxonomy(
  236. 'wp_template_type',
  237. 'wp_template',
  238. array(
  239. 'labels' => array(
  240. 'name' => _x( 'Template Types', 'taxonomy general name', 'full-site-editing' ),
  241. 'singular_name' => _x( 'Template Type', 'taxonomy singular name', 'full-site-editing' ),
  242. 'menu_name' => _x( 'Template Types', 'admin menu', 'full-site-editing' ),
  243. 'all_items' => __( 'All Template Types', 'full-site-editing' ),
  244. 'edit_item' => __( 'Edit Template Type', 'full-site-editing' ),
  245. 'view_item' => __( 'View Template Type', 'full-site-editing' ),
  246. 'update_item' => __( 'Update Template Type', 'full-site-editing' ),
  247. 'add_new_item' => __( 'Add New Template Type', 'full-site-editing' ),
  248. 'new_item_name' => __( 'New Template Type', 'full-site-editing' ),
  249. 'parent_item' => __( 'Parent Template Type', 'full-site-editing' ),
  250. 'parent_item_colon' => __( 'Parent Template Type:', 'full-site-editing' ),
  251. 'search_items' => __( 'Search Template Types', 'full-site-editing' ),
  252. 'not_found' => __( 'No template types found.', 'full-site-editing' ),
  253. 'back_to_items' => __( 'Back to template types', 'full-site-editing' ),
  254. ),
  255. 'public' => false,
  256. 'publicly_queryable' => true,
  257. 'show_ui' => true,
  258. 'show_in_menu' => false,
  259. 'show_in_nav_menu' => false,
  260. 'show_in_rest' => true,
  261. 'rest_base' => 'template_types',
  262. 'show_tagcloud' => false,
  263. 'show_admin_column' => true,
  264. 'hierarchical' => true,
  265. 'rewrite' => false,
  266. 'capabilities' => array(
  267. 'manage_terms' => 'edit_theme_options',
  268. 'edit_terms' => 'edit_theme_options',
  269. 'delete_terms' => 'edit_theme_options',
  270. 'assign_terms' => 'edit_theme_options',
  271. ),
  272. )
  273. );
  274. register_taxonomy(
  275. 'wp_template_part_type',
  276. 'wp_template_part',
  277. array(
  278. 'labels' => array(
  279. 'name' => _x( 'Template Part Types', 'taxonomy general name', 'full-site-editing' ),
  280. 'singular_name' => _x( 'Template Part Type', 'taxonomy singular name', 'full-site-editing' ),
  281. 'menu_name' => _x( 'Template Part Types', 'admin menu', 'full-site-editing' ),
  282. 'all_items' => __( 'All Template Part Types', 'full-site-editing' ),
  283. 'edit_item' => __( 'Edit Template Part Type', 'full-site-editing' ),
  284. 'view_item' => __( 'View Template Part Type', 'full-site-editing' ),
  285. 'update_item' => __( 'Update Template Part Type', 'full-site-editing' ),
  286. 'add_new_item' => __( 'Add New Template Part Type', 'full-site-editing' ),
  287. 'new_item_name' => __( 'New Template Part Type', 'full-site-editing' ),
  288. 'parent_item' => __( 'Parent Template Part Type', 'full-site-editing' ),
  289. 'parent_item_colon' => __( 'Parent Template Part Type:', 'full-site-editing' ),
  290. 'search_items' => __( 'Search Template Part Types', 'full-site-editing' ),
  291. 'not_found' => __( 'No template part types found.', 'full-site-editing' ),
  292. 'back_to_items' => __( 'Back to template part types', 'full-site-editing' ),
  293. ),
  294. 'public' => false,
  295. 'publicly_queryable' => true,
  296. 'show_ui' => true,
  297. 'show_in_menu' => false,
  298. 'show_in_nav_menu' => false,
  299. 'show_in_rest' => true,
  300. 'rest_base' => 'template_part_types',
  301. 'show_tagcloud' => false,
  302. 'show_admin_column' => true,
  303. 'hierarchical' => true,
  304. 'rewrite' => false,
  305. 'capabilities' => array(
  306. 'manage_terms' => 'edit_theme_options',
  307. 'edit_terms' => 'edit_theme_options',
  308. 'delete_terms' => 'edit_theme_options',
  309. 'assign_terms' => 'edit_theme_options',
  310. ),
  311. )
  312. );
  313. }
  314. }