setup_globals(); if ( $this->taxonomy && $this->screen_id ) { $this->includes(); $this->setup_hooks(); if ( isset( $_POST['action'] ) || isset( $_GET['action'] ) ) { if ( isset( $_GET['action'] ) ) { $action = wp_unslash( $_GET['action'] ); } else { $action = wp_unslash( $_POST['action'] ); } $this->handle_action( $action ); } } } /** * Register BP Types Admin. * * @since 7.0.0 * * @return BP_Admin_Types */ public static function register_types_admin() { if ( ! is_admin() ) { return; } $bp = buddypress(); if ( empty( $bp->core->types_admin ) ) { $bp->core->types_admin = new self; } return $bp->core->types_admin; } /** * Set the globals. * * @since 7.0.0 */ private function setup_globals() { $current_screen = get_current_screen(); if ( isset( $current_screen->taxonomy ) && $current_screen->taxonomy ) { $this->taxonomies = bp_get_default_taxonomies(); if ( isset( $this->taxonomies[ $current_screen->taxonomy ] ) ) { $this->taxonomy = $current_screen->taxonomy; $this->screen_id = $current_screen->id; } } } /** * Include Admin functions. * * @since 7.0.0 */ private function includes() { require plugin_dir_path( dirname( __FILE__ ) ) . 'admin/bp-core-admin-types.php'; } /** * Set hooks. * * @since 7.0.0 */ private function setup_hooks() { // Actions. add_action( 'admin_head-edit-tags.php', array( $this, 'screen_head' ) ); add_action( 'admin_head-term.php', array( $this, 'screen_head' ) ); add_action( 'bp_admin_enqueue_scripts', array( $this, 'screen_scripts' ) ); add_action( "{$this->taxonomy}_add_form_fields", array( $this, 'add_form_fields' ), 10, 1 ); add_action( "{$this->taxonomy}_edit_form_fields", array( $this, 'edit_form_fields' ), 10, 2 ); // Filters add_filter( 'bp_core_admin_register_scripts', array( $this, 'register_scripts' ) ); add_filter( "manage_{$this->screen_id}_columns", array( $this, 'column_headers' ), 10, 1 ); add_filter( "manage_{$this->taxonomy}_custom_column", array( $this, 'column_contents' ), 10, 3 ); add_filter( "{$this->taxonomy}_row_actions", array( $this, 'row_actions' ), 10, 2 ); add_filter( "bulk_actions-{$this->screen_id}", '__return_empty_array', 10, 1 ); } /** * Handle BP Type actions. * * @since 7.0.0 * * @param string $action Required. The action to handle ('add-tag', 'editedtag' or 'delete' ). */ private function handle_action( $action ) { $referer = wp_get_referer(); if ( ! bp_current_user_can( 'bp_moderate' ) ) { return; } // Adding a new type into the database. if ( 'add-tag' === $action ) { check_admin_referer( 'add-tag', '_wpnonce_add-tag' ); $result = bp_core_admin_insert_type( $_POST ); if ( is_wp_error( $result ) ) { $referer = add_query_arg( array_merge( $result->get_error_data(), array( 'error' => 1, ) ), $referer ); wp_safe_redirect( $referer ); exit; } wp_safe_redirect( add_query_arg( 'message', 2, $referer ) ); exit; // Updating an existing type intot the Database. } elseif ( 'editedtag' === $action ) { $args = $_POST; $args['type_term_id'] = 0; unset( $args['tag_ID'] ); if ( isset( $_POST['tag_ID'] ) ) { $args['type_term_id'] = $_POST['tag_ID']; } if ( isset( $_POST['taxonomy'] ) ) { $args['taxonomy'] = $_POST['taxonomy']; } check_admin_referer( 'update-tag_' . $args['type_term_id'] ); $result = bp_core_admin_update_type( $args ); if ( is_wp_error( $result ) ) { $referer = add_query_arg( array_merge( $result->get_error_data(), array( 'error' => 1, ) ), $referer ); wp_safe_redirect( $referer ); exit; } wp_safe_redirect( add_query_arg( 'message', 4, $referer ) ); exit; // Deletes a type. } elseif ( 'delete' === $action ) { $args = $_GET; $args['type_term_id'] = 0; unset( $args['tag_ID'] ); if ( isset( $_GET['tag_ID'] ) ) { $args['type_term_id'] = $_GET['tag_ID']; } if ( isset( $_GET['taxonomy'] ) ) { $args['taxonomy'] = $_GET['taxonomy']; } check_admin_referer( 'delete-tag_' . $args['type_term_id'] ); $referer = remove_query_arg( array( 'action', 'tag_ID', '_wpnonce' ), $referer ); // Delete the type. $result = bp_core_admin_delete_type( $args ); if ( is_wp_error( $result ) ) { $referer = add_query_arg( array_merge( $result->get_error_data(), array( 'error' => 1, ) ), $referer ); wp_safe_redirect( $referer ); exit; } wp_safe_redirect( add_query_arg( 'message', 9, $referer ) ); exit; } } /** * Override the Admin parent file to highlight the right menu. * * @since 7.0.0 */ public function screen_head() { global $parent_file; if ( 'members' === $this->taxonomies[ $this->taxonomy ]['component'] ) { $parent_file = 'users.php'; } else { $parent_file = 'bp-' . $this->taxonomies[ $this->taxonomy ]['component']; } } /** * Registers script. * * @since 7.0.0 */ public function register_scripts( $scripts = array() ) { // Neutralize WordPress Taxonomy scripts. wp_dequeue_script( 'admin-tags' ); wp_dequeue_script( 'inline-edit-tax' ); // Adapt some styles. wp_add_inline_style( 'common', '.form-field:not(.bp-types-form), .term-bp_type_directory_slug-wrap:not(.bp-set-directory-slug), .edit-tag-actions #delete-link { display: none; }' ); // Register the Types admin script. return array_merge( $scripts, array( 'bp-admin-types' => array( 'file' => sprintf( '%1$sadmin/js/types-admin%2$s.js', plugin_dir_url( dirname( __FILE__ ) ), bp_core_get_minified_asset_suffix() ), 'dependencies' => array(), 'footer' => true, ), ) ); } /** * Enqueues script. * * @since 7.0.0 */ public function screen_scripts() { wp_enqueue_script( 'bp-admin-types' ); } /** * Outputs the BP type add form. * * @since 7.0.0 * * @param string $taxonomy The type taxonomy name. * @param null|object $type The type object, `null` if not passed to the method. */ public function add_form_fields( $taxonomy = '', $type = null ) { $taxonomy_object = get_taxonomy( $taxonomy ); $labels = get_taxonomy_labels( $taxonomy_object ); // Default values for the Type ID field. $type_id_label = __( 'Type ID', 'buddypress' ); $type_id_desc = __( 'Enter a lower-case string without spaces or special characters (used internally to identify the type).', 'buddypress' ); if ( isset( $labels->bp_type_id_label ) && $labels->bp_type_id_label ) { $type_id_label = $labels->bp_type_id_label; } if ( isset( $labels->bp_type_id_description ) && $labels->bp_type_id_description ) { $type_id_desc = $labels->bp_type_id_description; } // Outputs the Type ID field. if ( isset( $type->name ) ) { printf( '
%2$s
%4$s
%3$s
%5$s
%3$s