class-fl-builder-ui-content-panel.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * Handles logic for the builder's content panel UI.
  4. *
  5. * @since 2.0
  6. */
  7. class FLBuilderUIContentPanel {
  8. /**
  9. * Get data structure required by the content panel.
  10. *
  11. * @since 2.0
  12. * @access public
  13. * @return array
  14. */
  15. public static function get_panel_data() {
  16. // Don't load the panel for module templates.
  17. if ( FLBuilderModel::is_post_user_template( 'module' ) ) {
  18. return array();
  19. }
  20. $data = array(
  21. 'tabs' => array(),
  22. );
  23. $modules_data = self::get_modules_tab_data();
  24. if ( $modules_data['should_display'] ) {
  25. $modules_tab = array(
  26. 'handle' => 'modules',
  27. 'name' => __( 'Modules', 'fl-builder' ),
  28. 'views' => $modules_data['views'],
  29. 'isSearchEnabled' => true,
  30. );
  31. $data['tabs']['modules'] = $modules_tab;
  32. }
  33. $rows_data = self::get_rows_tab_data();
  34. if ( $rows_data['should_display'] ) {
  35. $rows_tab = array(
  36. 'handle' => 'rows',
  37. 'name' => __( 'Rows', 'fl-builder' ),
  38. 'views' => $rows_data['views'],
  39. );
  40. $data['tabs']['rows'] = $rows_tab;
  41. }
  42. $templates_data = self::get_templates_tab_data();
  43. if ( $templates_data['should_display'] ) {
  44. $templates_tab = array(
  45. 'handle' => 'templates',
  46. 'name' => __( 'Templates', 'fl-builder' ),
  47. 'views' => $templates_data['views'],
  48. );
  49. $data['tabs']['templates'] = $templates_tab;
  50. }
  51. /**
  52. * Filter the tabs/views structure
  53. *
  54. * @since 2.0
  55. * @param Array $data the initial tab data
  56. */
  57. return apply_filters( 'fl_builder_content_panel_data', $data );
  58. }
  59. /**
  60. * Get module views for panel.
  61. *
  62. * @since 2.0
  63. * @access private
  64. * @return array
  65. */
  66. private static function get_modules_tab_data() {
  67. $data = array(
  68. 'should_display' => ! FLBuilderModel::is_post_user_template( 'module' ),
  69. 'views' => array(),
  70. );
  71. // Standard Modules View
  72. $data['views'][] = array(
  73. 'handle' => 'standard',
  74. 'name' => __( 'Standard Modules', 'fl-builder' ),
  75. 'query' => array(
  76. 'kind' => 'module',
  77. 'categorized' => true,
  78. 'group' => 'standard',
  79. ),
  80. 'orderedSectionNames' => array_keys( FLBuilderModel::get_module_categories() ),
  81. );
  82. // Third Party Module Groups
  83. $groups = FLBuilderModel::get_module_groups();
  84. if ( ! empty( $groups ) ) {
  85. $data['views'][] = array(
  86. 'type' => 'separator',
  87. );
  88. foreach ( $groups as $slug => $name ) {
  89. $data['views'][] = array(
  90. 'handle' => $slug,
  91. 'name' => $name,
  92. 'query' => array(
  93. 'kind' => array( 'module', 'template' ),
  94. 'content' => 'module',
  95. 'type' => 'core',
  96. 'categorized' => true,
  97. 'group' => $slug,
  98. ),
  99. 'templateName' => 'fl-content-panel-modules-view',
  100. );
  101. }
  102. }
  103. return $data;
  104. }
  105. /**
  106. * Get data for the rows tab.
  107. *
  108. * @since 2.0
  109. * @access private
  110. * @return array
  111. */
  112. private static function get_rows_tab_data() {
  113. $data = array(
  114. 'should_display' => true, /* rows tab shows even if row template */
  115. 'views' => array(),
  116. );
  117. // Columns View
  118. $data['views'][] = array(
  119. 'handle' => 'columns',
  120. 'name' => __( 'Columns', 'fl-builder' ),
  121. 'query' => array(
  122. 'kind' => 'colGroup',
  123. ),
  124. 'templateName' => 'fl-content-panel-col-groups-view',
  125. );
  126. // Row Templates View
  127. $templates = FLBuilderModel::get_row_templates_data();
  128. $is_row_template = FLBuilderModel::is_post_user_template( 'row' );
  129. $is_column_template = FLBuilderModel::is_post_user_template( 'column' );
  130. if ( ! $is_row_template && ! $is_column_template && isset( $templates['groups'] ) && ! empty( $templates['groups'] ) ) {
  131. $data['views'][] = array(
  132. 'type' => 'separator',
  133. );
  134. foreach ( $templates['groups'] as $slug => $group ) {
  135. $data['views'][] = array(
  136. 'handle' => $slug,
  137. 'name' => $group['name'],
  138. 'query' => array(
  139. 'kind' => 'template',
  140. 'type' => 'core',
  141. 'group' => $slug,
  142. 'content' => 'row',
  143. 'categorized' => true,
  144. ),
  145. );
  146. if ( count( $group['categories'] ) < 2 ) {
  147. continue;
  148. }
  149. foreach ( $group['categories'] as $cat_slug => $category ) {
  150. $data['views'][] = array(
  151. 'handle' => $cat_slug,
  152. 'name' => $category['name'],
  153. 'isSubItem' => true,
  154. 'query' => array(
  155. 'kind' => 'template',
  156. 'type' => 'core',
  157. 'content' => 'row',
  158. 'group' => $slug,
  159. 'category' => $cat_slug,
  160. 'categorized' => true,
  161. ),
  162. );
  163. }
  164. }
  165. }
  166. return $data;
  167. }
  168. /**
  169. * Get data for the templates tab.
  170. *
  171. * @since 2.0
  172. * @access private
  173. * @return array
  174. */
  175. private static function get_templates_tab_data() {
  176. $enabled = FLBuilderModel::get_enabled_templates();
  177. $is_module_template = FLBuilderModel::is_post_user_template( 'module' );
  178. $is_column_template = FLBuilderModel::is_post_user_template( 'column' );
  179. $is_row_template = FLBuilderModel::is_post_user_template( 'row' );
  180. $data = array(
  181. 'should_display' => ( ! $is_module_template && ! $is_column_template && ! $is_row_template && 'disabled' !== $enabled ),
  182. 'views' => array(),
  183. );
  184. $templates = FLBuilderModel::get_template_selector_data();
  185. if ( ! isset( $templates['groups'] ) || empty( $templates['groups'] ) ) {
  186. if ( true === FL_BUILDER_LITE ) {
  187. $data['views'][] = array(
  188. 'handle' => 'standard',
  189. 'name' => __( 'Upgrade', 'fl-builder' ),
  190. 'templateName' => 'fl-content-lite-templates-upgrade-view',
  191. );
  192. }
  193. return $data;
  194. }
  195. foreach ( $templates['groups'] as $slug => $group ) {
  196. $data['views'][] = array(
  197. 'handle' => $slug,
  198. 'name' => $group['name'],
  199. 'query' => array(
  200. 'kind' => 'template',
  201. 'type' => 'core',
  202. 'content' => 'layout',
  203. 'group' => $slug,
  204. 'categorized' => true,
  205. ),
  206. );
  207. if ( count( $group['categories'] ) < 2 ) {
  208. continue;
  209. }
  210. foreach ( $group['categories'] as $cat_slug => $category ) {
  211. $data['views'][] = array(
  212. 'handle' => $cat_slug,
  213. 'name' => $category['name'],
  214. 'isSubItem' => true,
  215. 'query' => array(
  216. 'kind' => 'template',
  217. 'type' => 'core',
  218. 'content' => 'layout',
  219. 'group' => $slug,
  220. 'category' => $cat_slug,
  221. 'categorized' => true,
  222. ),
  223. );
  224. }
  225. }
  226. return $data;
  227. }
  228. /**
  229. * Get all the insertable content elements that make up the content library.
  230. *
  231. * @since 2.0
  232. * @access public
  233. * @return array
  234. */
  235. public static function get_content_elements() {
  236. $data = array(
  237. /* Get all modules */
  238. 'module' => FLBuilderModel::get_uncategorized_modules(),
  239. /* Get all column groups */
  240. 'colGroup' => FLBuilderModel::get_column_groups(),
  241. 'template' => array(),
  242. );
  243. $static_modules = FLBuilderModel::get_module_templates_data();
  244. $module_templates = $static_modules['templates'];
  245. foreach ( $module_templates as $template ) {
  246. $data['template'][] = $template;
  247. }
  248. $static_columns = FLBuilderModel::get_column_templates_data();
  249. $column_templates = $static_columns['templates'];
  250. foreach ( $column_templates as $template ) {
  251. $data['template'][] = $template;
  252. }
  253. $static_rows = FLBuilderModel::get_row_templates_data();
  254. $row_templates = $static_rows['templates'];
  255. foreach ( $row_templates as $template ) {
  256. $data['template'][] = $template;
  257. }
  258. $static_templates = FLBuilderModel::get_template_selector_data();
  259. $layout_templates = $static_templates['templates'];
  260. foreach ( $layout_templates as $template ) {
  261. $data['template'][] = $template;
  262. }
  263. /**
  264. * Filter the available content elements
  265. *
  266. * @since 2.0
  267. * @param Array $data the initial content elements
  268. */
  269. return apply_filters( 'fl_builder_content_elements_data', $data );
  270. }
  271. }