Guest User

Untitled

a guest
Mar 15th, 2019
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.39 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /**
  5. * Add custom columns on wp-admin cpt list
  6. */
  7. add_filter('manage_tdb_templates_posts_columns', function($columns) {
  8. $date = $columns['date'];
  9. unset($columns['date']);
  10. $columns['tdb_template_type'] = 'Template Type';
  11. $columns['tdb_used_on'] = 'Used On';
  12. $columns['date'] = $date;
  13.  
  14. return $columns;
  15. });
  16.  
  17.  
  18. /**
  19. * Add custom data to the columns on wp-admin cpt list
  20. */
  21. add_action('manage_tdb_templates_posts_custom_column' , function($column, $post_id) {
  22.  
  23. $tdb_template_type = get_post_meta($post_id, 'tdb_template_type', true);
  24.  
  25. switch( $column ) {
  26.  
  27. case 'tdb_template_type':
  28.  
  29. $args = array(
  30. 'post_type' => 'tdb_templates',
  31. 'meta_key' => 'tdb_template_type',
  32. 'meta_value' => $tdb_template_type
  33. );
  34.  
  35. $url = add_query_arg( $args, 'edit.php' );
  36.  
  37. echo sprintf( '<a href="%s">%s</a>', esc_url( $url ), $tdb_template_type );
  38.  
  39. break;
  40.  
  41. case 'tdb_used_on':
  42.  
  43. $items_to_display = array();
  44. $tdb_template_id = 'tdb_template_' . $post_id;
  45.  
  46. /*
  47. * template types
  48. * 'single', 'category', 'author', 'search', 'date', 'tag', 'attachment', '404'
  49. */
  50. switch ( $tdb_template_type ) {
  51.  
  52. case 'single':
  53.  
  54. // read the global single template
  55. $tdb_single_template = td_util::get_option( 'td_default_site_post_template' );
  56.  
  57. // for global single post templates
  58. if ( $tdb_template_id === $tdb_single_template ) {
  59. $items_to_display[] = sprintf( '<a href="%s" title="Change this global ' . $tdb_template_type . ' post template from the theme panel." target="_blank">All Posts</a>', esc_url( admin_url('admin.php?page=td_theme_panel#td-panel-post-settings/box=default_post_template_site_wide') ) );
  60. }
  61.  
  62. // get all published posts
  63. $posts = get_posts(
  64. array(
  65. 'post_status' => 'publish',
  66. 'posts_per_page' => -1,
  67. 'order' => 'ASC',
  68. 'meta_key' => 'td_post_theme_settings',
  69. 'meta_value' => '',
  70. 'meta_compare' => '!='
  71. )
  72. );
  73.  
  74. $index_posts = 0;
  75.  
  76. // for individual single post templates
  77. foreach ( $posts as $post ) {
  78.  
  79. // read the individual single post settings
  80. $tdb_post_theme_settings = td_util::get_post_meta_array($post->ID, 'td_post_theme_settings');
  81.  
  82. if ( !empty( $tdb_post_theme_settings['td_post_template'] ) and $tdb_template_id === $tdb_post_theme_settings['td_post_template'] ) {
  83. $items_to_display[] = sprintf(
  84. '<a href="%s" title="Change this individual ' . $tdb_template_type . ' post template from the `Post Settings` section." target="_blank">%s</a>',
  85. get_edit_post_link( $post->ID ),
  86. $post->post_title
  87. );
  88.  
  89. if ( ++$index_posts >= 5 ) {
  90. break;
  91. }
  92. }
  93. }
  94.  
  95. break;
  96.  
  97. case 'category':
  98.  
  99. // read the global categories template
  100. $tdb_category_template = td_options::get( 'tdb_category_template' );
  101.  
  102. // for global category templates
  103. if ( $tdb_template_id === $tdb_category_template ) {
  104. $items_to_display[] = sprintf( '<a href="%s" title="Change this global ' . $tdb_template_type . ' template from the theme panel." target="_blank">All Categories</a>', esc_url( admin_url('admin.php?page=td_theme_panel#td-panel-categories') ) );
  105. }
  106.  
  107. // get all categories
  108. $categories = get_categories(
  109. array(
  110. 'hide_empty' => false
  111. )
  112. );
  113.  
  114. $index_posts = 0;
  115.  
  116. // for individual category templates
  117. foreach ( $categories as $category ) {
  118.  
  119. // read the individual cat template
  120. $tdb_individual_category_template = td_util::get_category_option( $category->cat_ID, 'tdb_category_template' );
  121.  
  122. if ( $tdb_template_id === $tdb_individual_category_template ) {
  123. $items_to_display[] = sprintf(
  124. '<a href="%s" title="Change this individual ' . $tdb_template_type . ' template from the theme panel." target="_blank">%s</a>',
  125. esc_url( admin_url('admin.php?page=td_theme_panel#td-panel-categories/box=category_individual_settings_cat_' . $category->cat_ID ) ),
  126. $category->name
  127. );
  128.  
  129. if ( ++$index_posts >= 5 ) {
  130. break;
  131. }
  132. }
  133. }
  134.  
  135. break;
  136.  
  137. case 'author':
  138.  
  139. // read the global author template
  140. $tdb_author_template = td_options::get( 'tdb_author_template' );
  141.  
  142. // for global author templates
  143. if ( $tdb_template_id === $tdb_author_template ) {
  144. $items_to_display[] = sprintf( '<a href="%s" title="Change this global ' . $tdb_template_type . ' template from the theme panel." target="_blank">All Authors</a>', esc_url( admin_url('admin.php?page=td_theme_panel#td-panel-template-settings/box=author_template') ) );
  145. }
  146.  
  147. $index_posts = 0;
  148.  
  149. // for individual author templates
  150. foreach ( get_users() as $user ) {
  151.  
  152. // username
  153. $username = '';
  154. if ( $user->first_name && $user->last_name ) {
  155. $username .= "$user->first_name $user->last_name";
  156. } elseif ( $user->first_name ) {
  157. $username .= $user->first_name;
  158. } elseif ( $user->last_name ) {
  159. $username .= $user->last_name;
  160. } else {
  161. $username .= $user->user_login;
  162. }
  163.  
  164. // user templates
  165. $tdb_author_templates = td_util::get_option('tdb_author_templates');
  166.  
  167. // read the individual author template
  168. $tdb_individual_author_template = isset( $tdb_author_templates[$user->ID] ) ? $tdb_author_templates[$user->ID] : '';
  169.  
  170. if ( !empty( $tdb_individual_author_template ) and $tdb_template_id === $tdb_individual_author_template ) {
  171. $items_to_display[] = sprintf(
  172. '<a href="%s" title="Change this `' . $username . '` individual ' . $tdb_template_type . ' template from the theme panel." target="_blank">%s</a>',
  173. esc_url( admin_url('admin.php?page=td_theme_panel#td-panel-template-settings/box=author_template' ) ),
  174. $username
  175. );
  176.  
  177. if ( ++$index_posts >= 5 ) {
  178. break;
  179. }
  180. }
  181. }
  182.  
  183. break;
  184.  
  185. case 'search':
  186.  
  187. // read the global search template
  188. $tdb_search_template = td_options::get( 'tdb_search_template' );
  189.  
  190. if ( $tdb_template_id === $tdb_search_template ) {
  191. $items_to_display[] = sprintf(
  192. '<a href="%s" title="Change this global ' . $tdb_template_type . ' template from the theme panel." target="_blank">Site</a>',
  193. esc_url( admin_url( 'admin.php?page=td_theme_panel#td-panel-template-settings/box=search_template' ) )
  194. );
  195. }
  196.  
  197. break;
  198.  
  199. case 'date':
  200.  
  201. // read the global date template
  202. $tdb_date_template = td_options::get( 'tdb_date_template' );
  203.  
  204. if ( $tdb_template_id === $tdb_date_template ) {
  205. $items_to_display[] = sprintf(
  206. '<a href="%s" title="Change this global ' . $tdb_template_type . ' template from the theme panel." target="_blank">Site</a>',
  207. esc_url( admin_url('admin.php?page=td_theme_panel#td-panel-template-settings/box=archive_template' ) )
  208. );
  209. }
  210.  
  211. break;
  212.  
  213. case 'tag':
  214.  
  215. // read the global tag template
  216. $tdb_tag_template = td_options::get( 'tdb_tag_template' );
  217.  
  218. if ( $tdb_template_id === $tdb_tag_template ) {
  219. $items_to_display[] = sprintf(
  220. '<a href="%s" title="Change this global ' . $tdb_template_type . ' template from the theme panel." target="_blank">Site</a>',
  221. esc_url( admin_url('admin.php?page=td_theme_panel#td-panel-template-settings/box=tag_template' ) )
  222. );
  223. }
  224.  
  225. break;
  226.  
  227. case 'attachment':
  228.  
  229. // read the global attachment template
  230. $tdb_attachment_template = td_options::get( 'tdb_attachment_template' );
  231.  
  232. if ( $tdb_template_id === $tdb_attachment_template ) {
  233. $items_to_display[] = sprintf(
  234. '<a href="%s" title="Change this global ' . $tdb_template_type . ' template from the theme panel." target="_blank">Site</a>',
  235. esc_url( admin_url('admin.php?page=td_theme_panel#td-panel-template-settings/box=attachment_template' ) )
  236. );
  237. }
  238.  
  239. break;
  240.  
  241. case '404':
  242.  
  243. // read the global 404 template
  244. $tdb_404_template = td_options::get( 'tdb_404_template' );
  245.  
  246. if ( $tdb_template_id === $tdb_404_template ) {
  247. $items_to_display[] = sprintf(
  248. '<a href="%s" title="Change this global ' . $tdb_template_type . ' template from the theme panel." target="_blank">Site</a>',
  249. esc_url( admin_url('admin.php?page=td_theme_panel#td-panel-template-settings/box=404_template' ) )
  250. );
  251. }
  252.  
  253. break;
  254.  
  255. }
  256.  
  257. // point to end of the array
  258. end( $items_to_display );
  259.  
  260. // the last element of the array.
  261. $last_element = key( $items_to_display );
  262.  
  263. if ( ! empty( $items_to_display ) ) {
  264. foreach ( $items_to_display as $item_index => $item ) {
  265. if ( $item_index == $last_element ) {
  266. echo $item;
  267. } else {
  268. echo $item . ', ';
  269. }
  270. }
  271. } else {
  272. echo '—';
  273. }
  274.  
  275. break;
  276.  
  277. }
  278.  
  279. }, 10, 2 );
  280.  
  281.  
  282. /**
  283. * add sorting support on wp-admin cpt list
  284. */
  285. add_filter('manage_edit-tdb_templates_sortable_columns', function ( $columns ) {
  286. $columns['tdb_template_type'] = 'tdb_template_type';
  287. return $columns;
  288. });
  289.  
  290. /**
  291. * add filter support on wp-admin cpt list
  292. */
  293. add_action( 'restrict_manage_posts', 'my_restrict_manage_posts' );
  294. function my_restrict_manage_posts($post_type) {
  295.  
  296. // only display these taxonomy filters on desired custom post_type listings
  297. if ( $post_type == 'tdb_templates' ) {
  298.  
  299. // output html for templates type dropdown filter
  300. echo '<select name="template_type" id="template_type" class="postform">';
  301. echo "<option value=''>Show All Types</option>";
  302.  
  303. $filters = array(
  304. 'header',
  305. 'single',
  306. '404',
  307. 'attachment',
  308. 'author',
  309. 'category',
  310. 'date',
  311. 'search',
  312. 'tag'
  313. );
  314.  
  315. foreach ( $filters as $template_type ) {
  316.  
  317. $selected = isset($_GET['template_type'])? $_GET['template_type'] : null;
  318. $template_name = ucfirst($template_type);
  319.  
  320. // output each select option line, check against the last $_GET to show the current option selected
  321. echo '<option value='. $template_type, $selected == $template_type ? ' selected="selected"' : '','>' . $template_name .'</option>';
  322.  
  323. }
  324.  
  325. echo "</select>";
  326. }
  327. }
  328.  
  329. /**
  330. * change the links for each item on wp-admin cpt list
  331. */
  332. add_filter('page_row_actions', function ($actions, $post) {
  333. global $current_screen;
  334. if (!empty($current_screen) && $current_screen->post_type != 'tdb_templates') {
  335. return $actions;
  336. }
  337.  
  338. $tdb_template_type = get_post_meta($post->ID, 'tdb_template_type', true);
  339.  
  340. // remove the default td-composer edit
  341. unset($actions['edit_tdc_composer']);
  342.  
  343. $actions = array_merge(
  344. array(
  345. 'edit_tdc_composer' => '<a href="' . admin_url( 'post.php?post_id=' . $post->ID . '&td_action=tdc&tdbTemplateType=' . $tdb_template_type . '&prev_url=' . rawurlencode(tdc_util::get_current_url() ) ) . '">Edit template</a>'
  346. ),
  347. $actions
  348. );
  349.  
  350. $actions['duplicate'] = '<a data-post-id="' . $post->ID . '" data-template-type="' . $tdb_template_type . '" data-template-name="' . get_the_title( $post->ID ) . '" class="tdb-duplicate-template" href="#" title="Duplicate this template." >Duplicate</a><span class="tdb-working-prompt">Working...</span>';
  351.  
  352. unset($actions['inline hide-if-no-js']); // hide quick edit
  353.  
  354. return $actions;
  355. }, 11, 2 );
Advertisement
Add Comment
Please, Sign In to add comment