BP_Docs_Admin

Dashboard functions for BuddyPress Docs.

Defined (1)

The class is defined in the following location(s).

/includes/admin.php  
  1. class BP_Docs_Admin { 
  2. /** 
  3. * Constructor 
  4. * @since 1.1.8 
  5. */ 
  6. function __construct() { 
  7. // Replace the Dashboard widget 
  8. if ( !defined( BP_DOCS_REPLACE_RECENT_COMMENTS_DASHBOARD_WIDGET ) || !BP_DOCS_REPLACE_RECENT_COMMENTS_DASHBOARD_WIDGET ) { 
  9. add_action( 'wp_dashboard_setup', array( $this, 'replace_recent_comments_dashboard_widget' ) ); 
  10.  
  11. // Set up menus 
  12. add_action( 'admin_menu', array( $this, 'setup_menus' ) ); 
  13. add_action( 'admin_menu', array( $this, 'setup_settings' ) ); 
  14.  
  15. public function setup_menus() { 
  16. // Settings 
  17. add_submenu_page( 
  18. 'edit.php?post_type=' . bp_docs_get_post_type_name(),  
  19. __( 'BuddyPress Docs Settings', 'bp-docs' ),  
  20. __( 'Settings', 'bp-docs' ),  
  21. 'bp_moderate',  
  22. 'bp-docs-settings',  
  23. array( $this, 'settings_cb' ) 
  24. ); 
  25.  
  26. public function settings_cb() { 
  27. ?> 
  28. <div class="wrap"> 
  29. <form method="post" action="<?php echo admin_url( 'options.php' ) ?>"> 
  30. <h2><?php _e( 'BuddyPress Docs Settings', 'bp-docs' ) ?></h2> 
  31. <?php settings_fields( 'bp-docs-settings' ) ?> 
  32. <?php do_settings_sections( 'bp-docs-settings' ) ?> 
  33. <?php submit_button() ?> 
  34. </form> 
  35. </div> 
  36. <?php 
  37.  
  38. public function setup_settings() { 
  39. // General 
  40. add_settings_section( 
  41. 'bp-docs-general',  
  42. __( 'General', 'bp-docs' ),  
  43. array( $this, 'general_section' ),  
  44. 'bp-docs-settings' 
  45. ); 
  46.  
  47. // General - Docs slug 
  48. add_settings_field( 
  49. 'bp-docs-slug',  
  50. __( 'Slug', 'bp-docs' ),  
  51. array( $this, 'slug_setting_markup' ),  
  52. 'bp-docs-settings',  
  53. 'bp-docs-general' 
  54. ); 
  55. register_setting( 'bp-docs-settings', 'bp-docs-slug', 'rawurlencode' ); 
  56.  
  57. // General - Excerpt length 
  58. add_settings_field( 
  59. 'bp-docs-excerpt-length',  
  60. __( 'Directory Excerpt Length', 'bp-docs' ),  
  61. array( $this, 'excerpt_length_setting_markup' ),  
  62. 'bp-docs-settings',  
  63. 'bp-docs-general' 
  64. ); 
  65. register_setting( 'bp-docs-settings', 'bp-docs-excerpt-length', 'absint' ); 
  66.  
  67. // Users 
  68. add_settings_section( 
  69. 'bp-docs-users',  
  70. __( 'Users', 'bp-docs' ),  
  71. array( $this, 'users_section' ),  
  72. 'bp-docs-settings' 
  73. ); 
  74.  
  75. // Users - Tab name 
  76. add_settings_field( 
  77. 'bp-docs-user-tab-name',  
  78. __( 'User Tab Name', 'bp-docs' ),  
  79. array( $this, 'user_tab_name_setting_markup' ),  
  80. 'bp-docs-settings',  
  81. 'bp-docs-users' 
  82. ); 
  83. register_setting( 'bp-docs-settings', 'bp-docs-user-tab-name' ); 
  84.  
  85. // Groups 
  86. if ( bp_is_active( 'groups' ) ) { 
  87. add_settings_section( 
  88. 'bp-docs-groups',  
  89. __( 'Groups', 'bp-docs' ),  
  90. array( $this, 'groups_section' ),  
  91. 'bp-docs-settings' 
  92. ); 
  93.  
  94. // Groups - Tab name 
  95. add_settings_field( 
  96. 'bp-docs-tab-name',  
  97. __( 'Group Tab Name', 'bp-docs' ),  
  98. array( $this, 'group_tab_name_setting_markup' ),  
  99. 'bp-docs-settings',  
  100. 'bp-docs-groups' 
  101. ); 
  102. register_setting( 'bp-docs-settings', 'bp-docs-tab-name' ); 
  103.  
  104. // Attachments 
  105. add_settings_section( 
  106. 'bp-docs-attachments',  
  107. __( 'Attachments', 'bp-docs' ),  
  108. array( $this, 'attachments_section' ),  
  109. 'bp-docs-settings' 
  110. ); 
  111.  
  112. // Users - Tab name 
  113. add_settings_field( 
  114. 'bp-docs-enable-attachments',  
  115. __( 'Enable Attachments', 'bp-docs' ),  
  116. array( $this, 'enable_attachments_setting_markup' ),  
  117. 'bp-docs-settings',  
  118. 'bp-docs-attachments' 
  119. ); 
  120. register_setting( 'bp-docs-settings', 'bp-docs-enable-attachments' ); 
  121.  
  122. public function general_section() {} 
  123. public function users_section() {} 
  124. public function groups_section() {} 
  125. public function attachments_section() {} 
  126.  
  127. public function slug_setting_markup() { 
  128. global $bp; 
  129.  
  130. $slug = bp_docs_get_docs_slug(); 
  131. $is_in_wp_config = 1 === $bp->bp_docs->slug_defined_in_wp_config['slug']; 
  132.  
  133. ?> 
  134. <label for="bp-docs-slug" class="screen-reader-text"><?php _e( "Change the slug used to build Docs URLs.", 'bp-docs' ) ?></label> 
  135. <input name="bp-docs-slug" id="bp-docs-slug" type="text" value="<?php echo esc_html( $slug ) ?>" <?php if ( $is_in_wp_config ) : ?>disabled="disabled" <?php endif ?>/> 
  136. <p class="description"><?php _e( "Change the slug used to build Docs URLs.", 'bp-docs' ) ?><?php if ( $is_in_wp_config ) : ?> <?php _e( 'You have already defined this value in <code>wp-config.php</code>, so it cannot be edited here.', 'bp-docs' ) ?><?php endif ?></p> 
  137.  
  138. <?php 
  139.  
  140. public function excerpt_length_setting_markup() { 
  141. $length = bp_docs_get_excerpt_length(); 
  142.  
  143. ?> 
  144. <label for="bp-docs-excerpt-length" class="screen-reader-text"><?php _e( "Change the value for longer or shorter excerpts.", 'bp-docs' ) ?></label> 
  145. <input name="bp-docs-excerpt-length" id="bp-docs-excerpt-length" type="text" value="<?php echo esc_html( $length ) ?>" /> 
  146. <p class="description"><?php _e( "Excerpts are shown on Docs directories, to provide better context. If your theme or language requires longer or shorter excerpts, change this value. Set to <code>0</code> to disable these excerpts.", 'bp-docs' ) ?></p> 
  147.  
  148. <?php 
  149.  
  150. public function group_tab_name_setting_markup() { 
  151. if ( ! bp_is_active( 'groups' ) ) { 
  152. return; 
  153. $name = bp_docs_get_group_tab_name(); 
  154.  
  155. ?> 
  156. <label for="bp-docs-tab-name" class="screen-reader-text"><?php _e( "Change the word on groups' Docs tab.", 'bp-docs' ) ?></label> 
  157. <input name="bp-docs-tab-name" id="bp-docs-tab-name" type="text" value="<?php echo esc_html( $name ) ?>" /> 
  158. <p class="description"><?php _e( "Change the word on the BuddyPress group tab from 'Docs' to whatever you'd like. Keep in mind that this will not change the text anywhere else on the page. For a more thorough text change, create a <a href='http://codex.buddypress.org/extending-buddypress/customizing-labels-messages-and-urls/'>language file</a> for BuddyPress Docs.", 'bp-docs' ) ?></p> 
  159.  
  160. <?php 
  161.  
  162. public function user_tab_name_setting_markup() { 
  163. $name = bp_docs_get_user_tab_name(); 
  164.  
  165. ?> 
  166. <label for="bp-docs-user-tab-name" class="screen-reader-text"><?php _e( "Change the word on users' Docs tab.", 'bp-docs' ) ?></label> 
  167. <input name="bp-docs-user-tab-name" id="bp-docs-user-tab-name" type="text" value="<?php echo esc_html( $name ) ?>" /> 
  168. <p class="description"><?php _e( "Change the word on users' Docs tabs from 'Docs' to whatever you'd like. Keep in mind that this will not change the text anywhere else on the page. For a more thorough text change, create a <a href='http://codex.buddypress.org/extending-buddypress/customizing-labels-messages-and-urls/'>language file</a> for BuddyPress Docs.", 'bp-docs' ) ?></p> 
  169.  
  170. <?php 
  171.  
  172. public function enable_attachments_setting_markup() { 
  173. $enabled = bp_docs_enable_attachments(); 
  174.  
  175. ?> 
  176. <label for="bp-docs-enable-attachments" class="screen-reader-text"><?php _e( "Allow users to add attachments.", 'bp-docs' ) ?></label> 
  177. <select name="bp-docs-enable-attachments" id="bp-docs-enable-attachments"> 
  178. <option value="yes" <?php selected( $enabled, true ) ?>><?php _e( 'Enabled', 'bp-docs' ) ?></option> 
  179. <option value="no" <?php selected( $enabled, false ) ?>><?php _e( 'Disabled', 'bp-docs' ) ?></option> 
  180. </select> 
  181. <p class="description"><?php _e( "Allow users to add attachments to their Docs.", 'bp-docs' ) ?></p> 
  182.  
  183. <?php 
  184.  
  185. function replace_recent_comments_dashboard_widget() { 
  186. global $wp_meta_boxes; 
  187.  
  188. // Find the recent comments widget 
  189. foreach ( $wp_meta_boxes['dashboard'] as $context => $widgets ) { 
  190. if ( !empty( $widgets ) && !empty( $widgets['core'] ) && is_array( $widgets['core'] ) && array_key_exists( 'dashboard_recent_comments', $widgets['core'] ) ) { 
  191. // Take note of the context for when we add our widget 
  192. $drc_widget_context = $context; 
  193.  
  194. // Store the widget so that we have access to its information 
  195. $drc_widget = $widgets['core']['dashboard_recent_comments']; 
  196.  
  197. // Store the array keys, so that we can reorder things later 
  198. $widget_order = array_keys( $widgets['core'] ); 
  199.  
  200. // Remove the core widget 
  201. remove_meta_box( 'dashboard_recent_comments', 'dashboard', $drc_widget_context ); 
  202.  
  203. // No need to continue the loop 
  204. break; 
  205.  
  206. // If we couldn't find the recent comments widget, it must have been removed. We'll 
  207. // assume this means we shouldn't add our own 
  208. if ( empty( $drc_widget ) ) 
  209. return; 
  210.  
  211. // Set up and add our widget 
  212. $recent_comments_title = __( 'Recent Comments', 'bp-docs' ); 
  213.  
  214. // Add our widget in the same location 
  215. wp_add_dashboard_widget( 'dashboard_recent_comments_bp_docs', $recent_comments_title, array( $this, 'wp_dashboard_recent_comments' ), 'wp_dashboard_recent_comments_control' ); 
  216.  
  217. // Restore the previous widget order. File this under "good citizenship" 
  218. $wp_meta_boxes['dashboard'][$context]['core']['dashboard_recent_comments'] = $wp_meta_boxes['dashboard'][$context]['core']['dashboard_recent_comments_bp_docs']; 
  219.  
  220. unset( $wp_meta_boxes['dashboard'][$context]['core']['dashboard_recent_comments_bp_docs'] ); 
  221.  
  222. // In order to inherit the styles, we're going to spoof the widget ID. Sadness 
  223. $wp_meta_boxes['dashboard'][$context]['core']['dashboard_recent_comments']['id'] = 'dashboard_recent_comments'; 
  224.  
  225. /** 
  226. * Replicates WP's native recent comments dashboard widget. 
  227. * @since 1.1.8 
  228. */ 
  229. function wp_dashboard_recent_comments() { 
  230. global $wpdb, $bp; 
  231.  
  232. if ( current_user_can('edit_posts') ) 
  233. $allowed_states = array('0', '1'); 
  234. else 
  235. $allowed_states = array('1'); 
  236.  
  237. // Select all comment types and filter out spam later for better query performance. 
  238. $comments = array(); 
  239. $start = 0; 
  240.  
  241. $widgets = get_option( 'dashboard_widget_options' ); 
  242. $total_items = isset( $widgets['dashboard_recent_comments'] ) && isset( $widgets['dashboard_recent_comments']['items'] ) 
  243. ? absint( $widgets['dashboard_recent_comments']['items'] ) : 5; 
  244.  
  245. while ( count( $comments ) < $total_items && $possible = $wpdb->get_results( "SELECT c.*, p.post_type AS comment_post_post_type FROM $wpdb->comments c LEFT JOIN $wpdb->posts p ON c.comment_post_ID = p.ID WHERE p.post_status != 'trash' ORDER BY c.comment_date_gmt DESC LIMIT $start, 50" ) ) { 
  246.  
  247. foreach ( $possible as $comment ) { 
  248. if ( count( $comments ) >= $total_items ) 
  249. break; 
  250.  
  251. // Is the user allowed to read this doc? 
  252. if ( $bp->bp_docs->post_type_name == $comment->comment_post_post_type && !bp_docs_user_can( 'read', get_current_user_ID(), $comment->comment_post_ID ) ) 
  253. continue; 
  254.  
  255. if ( in_array( $comment->comment_approved, $allowed_states ) && current_user_can( 'read_post', $comment->comment_post_ID ) ) 
  256. $comments[] = $comment; 
  257.  
  258. $start = $start + 50; 
  259.  
  260. if ( $comments ) : 
  261. ?> 
  262.  
  263. <div id="the-comment-list" class="list:comment"> 
  264. <?php 
  265. foreach ( $comments as $comment ) 
  266. _wp_dashboard_recent_comments_row( $comment ); 
  267. ?> 
  268.  
  269. </div> 
  270.  
  271. <?php 
  272. if ( current_user_can('edit_posts') ) { ?> 
  273. <?php _get_list_table('WP_Comments_List_Table')->views(); ?> 
  274. <?php } 
  275.  
  276. wp_comment_reply( -1, false, 'dashboard', false ); 
  277. wp_comment_trashnotice(); 
  278.  
  279. else : 
  280. ?> 
  281.  
  282. <p><?php _e( 'No comments yet.', 'bp-docs' ); ?></p> 
  283.  
  284. <?php 
  285. endif; // $comments;