setup_globals(); $this->includes(); $this->setup_actions(); } /** * Admin globals * * @since version (1.0) * @access private * * @uses buddydrive() to get some globals of plugin instance * @uses bp_core_do_network_admin() to define the best menu (network) */ private function setup_globals() { $buddydrive = buddydrive(); $this->admin_dir = trailingslashit( $buddydrive->includes_dir . 'admin' ); // Admin path $this->admin_url = trailingslashit( $buddydrive->includes_url . 'admin' ); // Admin url $this->styles_url = trailingslashit( $this->admin_url . 'css' ); // Admin styles URL*/ $this->js_url = trailingslashit( $this->admin_url . 'js' ); $this->settings_page = 'options-general.php'; $this->notice_hook = 'admin_notices' ; $this->user_columns_filter = 'manage_users_columns'; $this->requires_db_upgrade = buddydrive_get_db_number_version() < buddydrive_get_number_version(); if ( bp_core_do_network_admin() ) { $this->settings_page = 'settings.php'; $this->notice_hook = 'network_admin_notices'; $this->user_columns_filter = 'wpmu_users_columns'; $this->buddydrive_page = esc_url( add_query_arg( 'page','buddydrive-files', network_admin_url( 'admin.php' ) ) ); } else { $this->buddydrive_page = esc_url( add_query_arg( 'page','buddydrive-files', admin_url( 'admin.php' ) ) ); } // We are now using a BackBone UI $this->items_admin_callback = array( $this, 'items_admin_screen' ); /** * Use add_filter( 'buddydrive_use_deprecated_ui', '__return_true' ); to use the deprecated UI */ if ( true === buddydrive_use_deprecated_ui() ) { $this->items_admin_callback = 'buddydrive_files_admin'; } } /** * Include required files * * @since version (1.0) * @access private */ private function includes() { require( $this->admin_dir . 'buddydrive-settings.php' ); /** * Use add_filter( 'buddydrive_use_deprecated_ui', '__return_true' ); to use the deprecated UI */ if ( true === buddydrive_use_deprecated_ui() ) { require( $this->admin_dir . 'buddydrive-items.php' ); } } /** * Setup the admin hooks, actions and filters * * @since version (1.0) * @access private * * @uses add_action() To add various actions * @uses bp_core_admin_hook() to hook the right menu (network or not) * @uses add_filter() To add various filters */ private function setup_actions() { // Bail if config does not match what we need if ( buddydrive::bail() ) return; /** General Actions ***************************************************/ add_action( bp_core_admin_hook(), array( $this, 'admin_menus' ) ); // Add menu item to settings menu add_action( 'buddydrive_admin_head', array( $this, 'admin_head' ) ); // Add some general styling to the admin area add_action( $this->notice_hook, array( $this, 'activation_notice' ), 9 ); // Checks for BuddyDrive Upload directory once activated add_action( 'buddydrive_admin_register_settings', array( $this, 'register_admin_settings' ) ); // Add settings add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 10, 1 ); // Add enqueued JS and CSS add_action( 'wp_ajax_buddydrive_upgrader', array( $this, 'do_upgrade' ) ); /** Filters ***********************************************************/ // Modify BuddyDrive's admin links add_filter( 'plugin_action_links', array( $this, 'modify_plugin_action_links' ), 10, 2 ); add_filter( 'network_admin_plugin_action_links', array( $this, 'modify_plugin_action_links' ), 10, 2 ); if ( ! buddydrive_use_deprecated_ui() ) { add_filter( 'bp_admin_menu_order', array( $this, 'items_admin_menu_order' ), 10, 1 ); } // Allow plugins to modify these actions do_action_ref_array( 'buddydrive_admin_loaded', array( &$this ) ); } /** * Builds BuddyDrive admin menus * * @uses bp_current_user_can() to check for user's capability * @uses add_submenu_page() to add the settings page * @uses add_menu_page() to add the admin area for BuddyDrive items * @uses add_dashboard_page() to add the BuddyDrive Welcome Screen */ public function admin_menus() { // Bail if user cannot manage options if ( ! bp_current_user_can( 'manage_options' ) ) return; $this->hook_suffixes[] = add_submenu_page( $this->settings_page, _x( 'BuddyDrive', 'BuddyDrive Settings page title', 'buddydrive' ), _x( 'BuddyDrive', 'BuddyDrive Settings menu title', 'buddydrive' ), 'manage_options', 'buddydrive', 'buddydrive_admin_settings' ); $hook = add_menu_page( _x( 'BuddyDrive', 'BuddyDrive User Files Admin page title', 'buddydrive' ), _x( 'BuddyDrive', 'BuddyDrive User Files Admin menu title', 'buddydrive' ), 'manage_options', 'buddydrive-files', $this->items_admin_callback, 'div' ); $this->hook_suffixes[] = $hook; // About $this->hook_suffixes[] = add_dashboard_page( __( 'Welcome to BuddyDrive', 'buddydrive' ), __( 'Welcome to BuddyDrive', 'buddydrive' ), 'manage_options', 'buddydrive-about', array( $this, 'about_screen' ) ); // Upgrade DB Screen if ( $this->requires_db_upgrade ) { $this->hook_suffixes['upgrade'] = add_dashboard_page( __( 'BuddyDrive Upgrades', 'buddydrive' ), __( 'BuddyDrive Upgrades', 'buddydrive' ), 'manage_options', 'buddydrive-upgrade', array( $this, 'upgrade_screen' ) ); } /** * Use add_filter( 'buddydrive_use_deprecated_ui', '__return_true' ); to use the deprecated UI */ if ( true === buddydrive_use_deprecated_ui() ) { // Hook into early actions to load custom CSS and our init handler. add_action( "load-$hook", 'buddydrive_files_admin_load' ); } // Putting user edit hooks there, this way we're sure they will load at the right place add_action( 'edit_user_profile', array( $this, 'edit_user_quota' ), 10, 1 ); add_action( 'edit_user_profile_update', array( $this, 'save_user_quota' ), 10, 1 ); add_action( 'set_user_role', array( $this, 'update_user_quota_to_role' ), 10, 2 ); add_filter( $this->user_columns_filter, array( $this, 'user_quota_column' ) ); add_filter( 'manage_users_custom_column', array( $this, 'user_quota_row' ), 10, 3 ); if( is_multisite() ) { $hook_settings = $this->hook_suffixes[0]; add_action( "load-$hook_settings", array( $this, 'multisite_upload_trick' ) ); } } /** * Loads some common css and hides the BuddyDrive about submenu * * @uses remove_submenu_page() to remove the BuddyDrive About submenu */ public function admin_head() { // Hide About page remove_submenu_page( 'index.php', 'buddydrive-about' ); if ( $this->requires_db_upgrade ) { remove_submenu_page( 'index.php', 'buddydrive-upgrade' ); } $version = buddydrive_get_version(); ?> id ) && in_array( $current_screen->id, $this->hook_suffixes ) ) { $display_upgrade_notice = false; } if ( $this->requires_db_upgrade && $display_upgrade_notice ) { bp_core_add_admin_notice( sprintf( __( 'BuddyDrive is almost ready. It needs to update some of the datas it is using. If you have not done a database backup yet, please do it before clicking on this link.', 'buddydrive' ), esc_url( add_query_arg( array( 'page' => 'buddydrive-upgrade' ), bp_get_admin_url( 'index.php' ) ) ) ), 'error' ); } } /** * Registers admin settings for BuddyDrive * * @uses buddydrive_admin_get_settings_sections() to get the settings section * @uses buddydrive_admin_get_settings_fields_for_section() to get the fields * @uses bp_current_user_can() to check for user's capability * @uses add_settings_section() to add the settings section * @uses add_settings_field() to add the fields * @uses register_setting() to fianlly register the settings */ public static function register_admin_settings() { // Bail if no sections available $sections = buddydrive_admin_get_settings_sections(); if ( empty( $sections ) ) return false; // Loop through sections foreach ( (array) $sections as $section_id => $section ) { // Only proceed if current user can see this section if ( ! bp_current_user_can( 'manage_options' ) ) continue; // Only add section and fields if section has fields $fields = buddydrive_admin_get_settings_fields_for_section( $section_id ); if ( empty( $fields ) ) continue; // Add the section add_settings_section( $section_id, $section['title'], $section['callback'], $section['page'] ); // Loop through fields for this section foreach ( (array) $fields as $field_id => $field ) { // Add the field add_settings_field( $field_id, $field['title'], $field['callback'], $section['page'], $section_id, $field['args'] ); // Register the setting register_setting( $section['page'], $field_id, $field['sanitize_callback'] ); } } } /** * Eqnueues scripts and styles if needed * * @param string $hook the WordPress admin page * @uses wp_enqueue_style() to enqueue the style * @uses wp_enqueue_script() to enqueue the script */ public function enqueue_scripts( $hook = false ) { if ( ! in_array( $hook, $this->hook_suffixes ) ) { return; } $min = '.min'; if ( defined( 'SCRIPT_DEBUG' ) && true == SCRIPT_DEBUG ) { $min = ''; } wp_enqueue_style( 'buddydrive-admin-css', $this->styles_url . "buddydrive-admin{$min}.css", array(), buddydrive_get_version() ); if ( ! empty( $this->hook_suffixes[1] ) && $hook == $this->hook_suffixes[1] && ! empty( $_REQUEST['action'] ) && $_REQUEST['action'] == 'edit' ) { /** * Use add_filter( 'buddydrive_use_deprecated_ui', '__return_true' ); to use the deprecated UI */ if ( true === buddydrive_use_deprecated_ui() ) { wp_enqueue_script ( 'buddydrive-admin-js', $this->js_url .'buddydrive-admin.js' ); wp_localize_script( 'buddydrive-admin-js', 'buddydrive_admin', buddydrive_get_js_l10n() ); } } if ( isset( $this->hook_suffixes['upgrade'] ) && $hook === $this->hook_suffixes['upgrade'] ) { wp_register_script( 'buddydrive-upgrader-js', $this->js_url . "buddydrive-upgrader{$min}.js", array( 'jquery', 'json2', 'wp-backbone' ), buddydrive_get_version(), true ); } } /** * Modifies the links in plugins table * * @param array $links the existing links * @param string $file the file of plugins * @uses plugin_basename() to get the file name of BuddyDrive plugin * @uses add_query_arg() to add args to the link * @uses bp_get_admin_url() to build the new links * @return array the existing links + the new ones */ public function modify_plugin_action_links( $links, $file ) { // Return normal links if not BuddyPress if ( plugin_basename( buddydrive()->file ) != $file ) return $links; // Add a few links to the existing links array return array_merge( $links, array( 'settings' => '' . esc_html__( 'Settings', 'buddydrive' ) . '', 'about' => '' . esc_html__( 'About', 'buddydrive' ) . '' ) ); } /** * Displays the Welcome screen * * @uses buddydrive_get_version() to get the current version of the plugin * @uses bp_get_admin_url() to build the url to settings page * @uses add_query_arg() to add args to the url */ public function about_screen() { $display_version = buddydrive_get_version(); $settings_url = add_query_arg( array( 'page' => 'buddydrive'), bp_get_admin_url( $this->settings_page ) ); ?>

<?php esc_attr_e( 'The BuddyDrive Editor', 'buddydrive' ); ?>



ID ) ) return; $user_quota = buddydrive_get_quota_by_user_id( $profileuser->ID ); ?>

get_blog_prefix( bp_get_root_blog_id() ) . 'capabilities', true ); $user_role = bp_get_option( 'default_role' ); if ( ! empty( $user_roles ) && is_array( $user_roles ) ) { $user_role = reset( $user_roles ); } // temporarly setting old role buddydrive()->old_role = $user_role; update_user_meta( $user_id, '_buddydrive_user_quota', intval( $_POST['_buddydrive_user_quota'] ) ); } /** * Updates the user quota on role changed * * @since version 1.1 * * @param integer $user_id the id of the user being edited * @param string $role the new role of the user * @uses bp_get_root_blog_id() to make sure we're on the blog BuddyPress is activated on * @uses buddydrive() to get the old role global * @uses bp_get_option() to get main blog option * @uses update_user_meta() to save user's preference */ public static function update_user_quota_to_role( $user_id, $role ) { if ( ! bp_is_root_blog() ) { return; } $buddydrive = buddydrive(); $old_role = false; if ( ! empty( $buddydrive->old_role ) ) { $old_role = $buddydrive->old_role; } if ( isset( $_POST['_buddydrive_user_quota'] ) && $old_role === $role ) { return; } $option_user_quota = bp_get_option( '_buddydrive_user_quota', 1000 ); if ( is_array( $option_user_quota ) && ! empty( $option_user_quota[ $role ] ) ) { $user_quota = $option_user_quota[ $role ]; } else { $user_quota = $option_user_quota; } update_user_meta( $user_id, '_buddydrive_user_quota', $user_quota ); } /** * Adds a column to admin user listing to show drive usage * * @since version 1.1 * * @param array $columns the different column of the WP_List_Table * @return array the new columns */ public static function user_quota_column( $columns = array() ) { $columns['user_quota'] = __( 'BuddyDrive Usage', 'buddydrive' ); return $columns; } /** * Displays the row data for our new column * * @since version 1.1 * * @param string $retval * @param string $column_name * @param integer $user_id * @uses buddydrive_get_user_space_left() to calculate the disk usage * @return string the user's drive usage */ public static function user_quota_row( $retval = '', $column_name = '', $user_id = 0 ) { if ( 'user_quota' === $column_name && ! empty( $user_id ) ) { $quota = buddydrive_get_user_space_data( $user_id ); if ( ! empty( $quota['percent'] ) && 0 < (float) $quota['percent'] ) { $retval = sprintf( '%3$s', buddydrive()->admin->buddydrive_page . '#user/' . $user_id, esc_attr__( 'View all items for this user', 'buddydrive' ), $quota['percent'] . '%' ); } else { $retval = $quota['percent'] . '%'; } } // Pass retval through return $retval; } public function upgrade_screen() { global $wpdb; ?>

$task ) { if ( ! empty( $task['count'] ) && 'upgrade_db_version' !== $task['action_id'] ) { $tasks[ $key ]['count'] = $wpdb->get_var( $task['count'] ); // If nothing needs to be ugraded, remove the task. if ( empty( $tasks[ $key ]['count'] ) ) { unset( $tasks[ $key ] ); } else { $tasks[ $key ]['message'] = sprintf( $task['message'], $tasks[ $key ]['count'] ); } } } printf( _n( 'BuddyDrive is almost ready, please wait for the %s following task to proceed.', 'BuddyDrive is almost ready, please wait for the %s following tasks to proceed.', count( $tasks ), 'buddydrive' ), number_format_i18n( count( $tasks ) ) ); } ?>

array_values( $tasks ), 'nonce' => wp_create_nonce( 'buddydrive-upgrader' ), ) ); ?> __( 'The task could not process due to an error', 'buddydrive' ), 'type' => 'error' ); if ( empty( $_POST['id'] ) || ! isset( $_POST['count'] ) || ! isset( $_POST['done'] ) ) { wp_send_json_error( $error ); } // Add the action to the error $error['action_id'] = $_POST['id']; // Check nonce if ( empty( $_POST['_buddydrive_nonce'] ) || ! wp_verify_nonce( $_POST['_buddydrive_nonce'], 'buddydrive-upgrader' ) ) { wp_send_json_error( $error ); } // Check capability if ( ! bp_current_user_can( 'bp_moderate' ) ) { wp_send_json_error( $error ); } $tasks = wp_list_pluck( buddydrive_get_upgrade_tasks(), 'callback', 'action_id' ); $did = 0; // Upgrading the DB version if ( 'upgrade_db_version' === $_POST['id'] ) { $did = 1; update_option( '_buddydrive_db_version', buddydrive_get_number_version() ); // Processing any other tasks } elseif ( isset( $tasks[ $_POST['id'] ] ) && function_exists( $tasks[ $_POST['id'] ] ) ) { $did = call_user_func_array( $tasks[ $_POST['id'] ], array( 20 ) ); // This shouldn't happen.. if ( 0 === $did && ( (int) $_POST['count'] > ( (int) $_POST['done'] + (int) $did ) ) ) { wp_send_json_error( array( 'message' => __( '%d item(s) could not be updated', 'buddydrive' ), 'type' => 'warning', 'action_id' => $_POST['id'] ) ); } } else { wp_send_json_error( $error ); } wp_send_json_success( array( 'done' => $did, 'action_id' => $_POST['id'] ) ); } public function items_admin_menu_order( $custom_menus = array() ) { array_push( $custom_menus, 'buddydrive-files' ); return $custom_menus; } public function items_admin_screen() { ?>

admin = new BuddyDrive_Admin(); } add_action( 'buddydrive_init', 'buddydrive_admin', 0 );