Skip to:
Content

BuddyPress.org

Ticket #5669: bp-custom.php

File bp-custom.php, 5.1 KB (added by imath, 12 years ago)
Line 
1<?php
2// Exit if accessed directly
3if ( ! defined( 'ABSPATH' ) ) exit;
4
5/**
6 * @see http://codex.wordpress.org/Function_Reference/register_post_type
7 */
8function bp_custom_register_post_type() {
9 $labels = array(
10 'name' => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
11 'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
12 'menu_name' => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
13 'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
14 'add_new' => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
15 'add_new_item' => __( 'Add New Book', 'your-plugin-textdomain' ),
16 'new_item' => __( 'New Book', 'your-plugin-textdomain' ),
17 'edit_item' => __( 'Edit Book', 'your-plugin-textdomain' ),
18 'view_item' => __( 'View Book', 'your-plugin-textdomain' ),
19 'all_items' => __( 'All Books', 'your-plugin-textdomain' ),
20 'search_items' => __( 'Search Books', 'your-plugin-textdomain' ),
21 'parent_item_colon' => __( 'Parent Books:', 'your-plugin-textdomain' ),
22 'not_found' => __( 'No books found.', 'your-plugin-textdomain' ),
23 'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' ),
24 );
25
26 $args = array(
27 'labels' => $labels,
28 'public' => true,
29 'publicly_queryable' => true,
30 'show_ui' => true,
31 'show_in_menu' => true,
32 'query_var' => true,
33 'rewrite' => array( 'slug' => 'book' ),
34 'capability_type' => 'post',
35 'has_archive' => true,
36 'hierarchical' => false,
37 'menu_position' => null,
38 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
39 );
40
41 register_post_type( 'book', $args );
42}
43add_action( 'init', 'bp_custom_register_post_type' );
44
45function bp_custom_set_activity_tracking( $tracked = array() ) {
46 /**
47 * If buddypress arg is defined, you can set the activity recording for your plugin where
48 * - type : is your plugin's activity type
49 * - description : is your plugin's activity description for the administration screen dropdown
50 * - format_callback : is the format callback to use to build your plugin's activity action
51 * - label : is your plugin's front end dropdown option label
52 *
53 * - context : is an array of the contexts your plugin's dropdown option should show
54 * - activity > the Activity directory page
55 * - member > the user's activity profile pages
56 * - member_groups > the user's groups activity profile page
57 * - group > the group's activity page (generally group's home page)
58 *
59 * Using context can avoid using the front-end dropdown hooks such as bp_activity_filter_options. Although, it's possible
60 * some themes didn't upgraded to BuddyPress 2.1 way of displaying new activity types in front end dropdowns.
61 * So you might check if bp_activity_show_filters() function exists to be sure of it
62 *
63 * - track : whether to record or not an activity for your plugin
64 * - publish_callback (optional) : your specific callback if you want to have more control on the way activities will be recorded
65 * - unpublish_callback (optional) : your specific callback if you want to have more control on the way activities will be removed
66 * - update_callback (optional) : your specific callback if you want to have more control on the way activities will be updated
67 * - untrash_callback (optional) : your specific callback if you want to have more control on the way activities will be reverted
68 * - remove_callback (optional) : your specific callback if you want to have more control on the way activities will be removed
69 *
70 * NB : your plugin's post type will only be tracked if the BuddyPress blogs component is active, as your plugin's post type will
71 * be attached to the BuddyPress blogs component one (post) and the activity will be recorded on multisite child blogs if the discourage
72 * search engines settings is not set.
73 */
74 $tracked['book'] = array(
75 'buddypress' => array(
76 'type' => 'new_book',
77 'description' => __( 'New book published', 'your-plugin-textdomain' ),
78 'format_callback' => 'bp_custom_format_activity_action_new_book_post',
79 'label' => __( 'Books', 'your-plugin-textdomain' ),
80 'context' => array( 'activity' ),
81 'track' => true,
82 )
83 );
84 return $tracked;
85}
86add_filter( 'bp_blogs_register_post_types', 'bp_custom_set_activity_tracking', 10, 1 );
87
88function bp_custom_format_activity_action_new_book_post( $action, $activity ) {
89 if ( empty( $activity ) || ( empty( $action ) && empty( $activity->id ) ) )
90 return false;
91
92 $user_link = bp_core_get_userlink( $activity->user_id );
93 $post_link = add_query_arg( 'p', $activity->secondary_item_id, trailingslashit( get_home_url( $activity->item_id ) ) );
94 $post_link = '<a href="'.$post_link.'">' . __( 'Read it!', 'your-plugin-textdomain' ) . '</a>';
95
96 $action = sprintf( __( '%1$s wrote a new book, %2$s', 'your-plugin-textdomain' ), $user_link, $post_link );
97
98 return $action;
99}