fse-template-data.php 14 KB

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