class-fl-builder-user-templates-admin-list.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Logic for the user templates admin list table.
  4. *
  5. * @since 1.10
  6. */
  7. final class FLBuilderUserTemplatesAdminList {
  8. /**
  9. * Initialize hooks.
  10. *
  11. * @since 1.10
  12. * @return void
  13. */
  14. static public function init() {
  15. /* Actions */
  16. add_action( 'plugins_loaded', __CLASS__ . '::redirect' );
  17. add_action( 'wp', __CLASS__ . '::page_heading' );
  18. add_action( 'pre_get_posts', __CLASS__ . '::pre_get_posts' );
  19. add_action( 'admin_enqueue_scripts', __CLASS__ . '::admin_enqueue_scripts' );
  20. add_action( 'manage_fl-builder-template_posts_custom_column', __CLASS__ . '::add_column_content', 10, 2 );
  21. /* Filters */
  22. add_filter( 'views_edit-fl-builder-template', __CLASS__ . '::modify_views' );
  23. add_filter( 'manage_fl-builder-template_posts_columns', __CLASS__ . '::add_column_headings' );
  24. add_filter( 'post_row_actions', __CLASS__ . '::row_actions' );
  25. add_action( 'restrict_manage_posts', __CLASS__ . '::restrict_listings' );
  26. }
  27. /**
  28. * Enqueue scripts and styles for user templates.
  29. *
  30. * @since 1.10
  31. * @return void
  32. */
  33. static public function admin_enqueue_scripts() {
  34. global $pagenow;
  35. $screen = get_current_screen();
  36. $slug = 'fl-builder-user-templates-admin-';
  37. $url = VAMTAMEL_B_USER_TEMPLATES_URL;
  38. $version = FL_BUILDER_VERSION;
  39. if ( 'edit.php' == $pagenow && 'fl-builder-template' == $screen->post_type ) {
  40. wp_enqueue_style( $slug . 'list', $url . 'css/' . $slug . 'list.css', array(), $version );
  41. wp_enqueue_script( $slug . 'list', $url . 'js/' . $slug . 'list.js', array(), $version );
  42. wp_localize_script( $slug . 'list', 'FLBuilderConfig', array(
  43. 'userTemplateType' => isset( $_GET['fl-builder-template-type'] ) ? $_GET['fl-builder-template-type'] : 'layout',
  44. 'addNewURL' => admin_url( '/edit.php?post_type=fl-builder-template&page=fl-builder-add-new' ),
  45. ) );
  46. }
  47. }
  48. /**
  49. * Redirects the list table to show layout templates if no
  50. * template type is set. We never want to show all templates
  51. * (layouts, rows, modules) in a list table together.
  52. *
  53. * @since 1.10
  54. * @return void
  55. */
  56. static public function redirect() {
  57. global $pagenow;
  58. $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : null;
  59. $template_type = isset( $_GET['fl-builder-template-type'] ) ? $_GET['fl-builder-template-type'] : null;
  60. $page = isset( $_GET['page'] ) ? $_GET['page'] : null;
  61. if ( 'edit.php' == $pagenow && 'fl-builder-template' == $post_type && ! $template_type && ! $page ) {
  62. $url = admin_url( '/edit.php?post_type=fl-builder-template&fl-builder-template-type=layout' );
  63. wp_redirect( $url );
  64. exit;
  65. }
  66. }
  67. /**
  68. * Overrides the list table page headings for saved rows, cols and modules.
  69. *
  70. * @since 1.10
  71. * @return void
  72. */
  73. static public function page_heading() {
  74. global $pagenow;
  75. global $wp_post_types;
  76. if ( ! is_admin() ) {
  77. return;
  78. }
  79. $screen = get_current_screen();
  80. if ( 'edit.php' == $pagenow && isset( $_GET['fl-builder-template-type'] ) ) {
  81. if ( 'row' == $_GET['fl-builder-template-type'] ) {
  82. $wp_post_types['fl-builder-template']->labels->name = __( 'Saved Rows', 'vamtam-elements-b' );
  83. } elseif ( 'column' == $_GET['fl-builder-template-type'] ) {
  84. $wp_post_types['fl-builder-template']->labels->name = __( 'Saved Columns', 'vamtam-elements-b' );
  85. } elseif ( 'module' == $_GET['fl-builder-template-type'] ) {
  86. $wp_post_types['fl-builder-template']->labels->name = __( 'Saved Modules', 'vamtam-elements-b' );
  87. }
  88. }
  89. }
  90. /**
  91. * Orders templates by title.
  92. *
  93. * @since 2.0.6
  94. * @param object $query
  95. * @return void
  96. */
  97. static public function pre_get_posts( $query ) {
  98. if ( ! isset( $_GET['post_type'] ) || 'fl-builder-template' != $_GET['post_type'] ) {
  99. return;
  100. } elseif ( $query->is_main_query() && ! $query->get( 'orderby' ) ) {
  101. $query->set( 'orderby', 'title' );
  102. $query->set( 'order', 'ASC' );
  103. }
  104. }
  105. /**
  106. * Modifies the views links to remove the counts since they
  107. * are not correct for our list table approach.
  108. *
  109. * @since 1.10
  110. * @param array $views
  111. * @return array
  112. */
  113. static public function modify_views( $views ) {
  114. $slug = 'fl-builder-template';
  115. $type = isset( $_GET['fl-builder-template-type'] ) ? $_GET['fl-builder-template-type'] : 'layout';
  116. foreach ( $views as $key => $view ) {
  117. if ( strstr( $view, $slug ) ) {
  118. $view = str_replace( $slug, $slug . '&#038;fl-builder-template-type=' . $type, $view );
  119. $view = preg_replace( '/<span(.*)span>/', '', $view );
  120. $views[ $key ] = $view;
  121. }
  122. }
  123. return $views;
  124. }
  125. /**
  126. * Adds the custom list table column headings.
  127. *
  128. * @since 1.10
  129. * @param array $columns
  130. * @return array
  131. */
  132. static public function add_column_headings( $columns ) {
  133. if ( ! isset( $_GET['fl-builder-template-type'] ) ) {
  134. return;
  135. }
  136. if ( in_array( $_GET['fl-builder-template-type'], array( 'row', 'column', 'module' ) ) ) {
  137. $columns['fl_global'] = __( 'Global', 'vamtam-elements-b' );
  138. }
  139. unset( $columns['date'] );
  140. return $columns;
  141. }
  142. /**
  143. * Adds the custom list table column content.
  144. *
  145. * @since 1.10
  146. * @param array $columns
  147. * @return array
  148. */
  149. static public function add_column_content( $column, $post_id ) {
  150. if ( 'fl_global' != $column ) {
  151. return;
  152. }
  153. if ( FLBuilderModel::is_post_global_node_template( $post_id ) ) {
  154. echo '<i class="dashicons dashicons-yes"></i>';
  155. } else {
  156. echo '&#8212;';
  157. }
  158. }
  159. /**
  160. * Removes the quick edit link as we don't need it.
  161. *
  162. * @since 1.10
  163. * @param array $actions
  164. * @return array
  165. */
  166. static public function row_actions( $actions = array() ) {
  167. if ( isset( $_GET['post_type'] ) && 'fl-builder-template' == $_GET['post_type'] ) {
  168. unset( $actions['inline hide-if-no-js'] );
  169. }
  170. return $actions;
  171. }
  172. /**
  173. * Add filter dropdown for Categories
  174. *
  175. * @since 1.10.8
  176. */
  177. static public function restrict_listings() {
  178. global $typenow;
  179. if ( 'fl-builder-template' == $typenow ) {
  180. $taxonomy = 'fl-builder-template-category';
  181. $tax = get_taxonomy( $taxonomy );
  182. $term = $_GET['fl-builder-template-type'];
  183. wp_dropdown_categories( array(
  184. 'show_option_all' => __( 'Show All Categories', 'vamtam-elements-b' ),
  185. 'taxonomy' => $taxonomy,
  186. 'value_field' => 'slug',
  187. 'orderby' => 'name',
  188. 'selected' => $term,
  189. 'name' => $taxonomy,
  190. 'depth' => 1,
  191. 'show_count' => false,
  192. 'hide_empty' => false,
  193. )
  194. );
  195. }
  196. }
  197. }
  198. FLBuilderUserTemplatesAdminList::init();