-
Notifications
You must be signed in to change notification settings - Fork 0
/
handbook.php
253 lines (207 loc) · 7.92 KB
/
handbook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/**
* Plugin Name: Handbook
* Description: Features for a handbook, complete with glossary and table of contents
* Author: Nacin
*/
require_once dirname( __FILE__ ) . '/inc/callout-boxes.php';
require_once dirname( __FILE__ ) . '/inc/glossary.php';
require_once dirname( __FILE__ ) . '/inc/navigation.php';
require_once dirname( __FILE__ ) . '/inc/table-of-contents.php';
require_once dirname( __FILE__ ) . '/inc/email-post-changes.php';
require_once dirname( __FILE__ ) . '/inc/watchlist.php';
WPorg_Handbook_Glossary::init();
/**
* Initialize our handbooks
*
*/
class WPorg_Handbook_Init {
public static function get_post_types() {
return (array) apply_filters( 'handbook_post_types', array( 'handbook' ) );
}
static function init() {
$post_types = self::get_post_types();
new WPorg_Handbook_TOC( $post_types );
foreach ( $post_types as $type ) {
new WPorg_Handbook( $type );
}
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_styles' ) );
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
}
static public function enqueue_styles() {
wp_enqueue_style( 'wporg-handbook-css', plugins_url( '/stylesheets/callout-boxes.css', __FILE__ ), array(), '20160224' );
}
static public function enqueue_scripts() {
wp_enqueue_script( 'wporg-handbook', plugins_url( '/scripts/handbook.js', __FILE__ ), array( 'jquery' ), '20150930' );
}
}
add_action( 'after_setup_theme', array( 'WPorg_Handbook_Init', 'init' ) );
class WPorg_Handbook {
public $post_type = '';
public $setting_name = '';
protected $label = '';
static function caps() {
return array(
'edit_handbook_pages', 'edit_others_handbook_pages',
'edit_published_handbook_pages',
);
}
static function editor_caps() {
return array(
'publish_handbook_pages',
'delete_handbook_pages', 'delete_others_handbook_pages',
'delete_published_handbook_pages', 'delete_private_handbook_pages',
'edit_private_handbook_pages', 'read_private_handbook_pages',
);
}
/**
* Returns the handbook name.
*
* If one isn't set via settings, one is generated.
*
* @param string $post_type Optional. Handbook post type.
* @param bool $raw Optional. Return only explicitly set name without attempting to generate default name?
* @return string
*/
static function get_name( $post_type = 'handbook', $raw = false ) {
// Prefer explicitly configured handbook name.
$name = get_option( $post_type . '_name' );
// If handbook name isn't set, try root relative site path.
if ( ! $raw && empty( $name ) ) {
if ( is_multisite() ) {
$name = trim( get_blog_details()->path, '/' );
} else {
$name = trim( parse_url( get_option( 'home' ), PHP_URL_PATH ), '/' );
}
// If no name defined yet, try handbook post type if not standard.
if ( empty( $name ) && ( 'handbook' != $post_type ) ) {
$name = ucfirst( substr( $post_type, 0, -9 ) );
}
$name .= ' Handbook';
}
return trim( $name );
}
function __construct( $type ) {
if ( 'handbook' != $type ) {
$this->post_type = $type . '-handbook';
} else {
$this->post_type = $type;
}
$this->label = ucwords( str_replace( array( '-', '_' ), ' ', $this->post_type ) );
$this->setting_name = $this->post_type . '_name';
add_filter( 'user_has_cap', array( $this, 'grant_handbook_caps' ) );
add_filter( 'init', array( $this, 'register_post_type' ) );
add_filter( 'post_type_link', array( $this, 'post_type_link' ), 10, 2 );
add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
add_action( 'widgets_init', array( $this, 'handbook_sidebar' ), 11 ); // After P2
add_action( 'wporg_email_changes_for_post_types', array( $this, 'wporg_email_changes_for_post_types' ) );
add_action( 'p2_action_links', array( $this, 'disable_p2_resolved_posts_action_links' ) );
add_action( 'admin_init', array( $this, 'add_name_setting' ) );
}
function add_name_setting() {
register_setting( 'general', $this->setting_name, 'esc_attr' );
$label = ( 'handbook' == $this->post_type ) ?
__( 'Handbook name', 'wporg' ) :
sprintf( __( 'Handbook name (%s)', 'wporg' ), substr( $this->post_type, 0, -9 ) );
add_settings_field(
$this->setting_name,
'<label for="' . esc_attr( $this->setting_name ) . '">' . $label . '</label>',
array( $this, 'name_setting_html' ),
'general'
);
}
function name_setting_html() {
$value = get_option( $this->setting_name, '' );
echo '<input type="text" id="' . esc_attr( $this->setting_name ) . '" name="' . esc_attr( $this->setting_name ) . '" value="' . esc_attr( $value ) . '" class="regular-text ltr" />';
}
function grant_handbook_caps( $caps ) {
if ( ! is_user_member_of_blog() ) {
return $caps;
}
foreach ( self::caps() as $cap ) {
$caps[ $cap ] = true;
}
if ( ! empty( $caps['edit_pages'] ) ) {
foreach ( self::editor_caps() as $cap ) {
$caps[ $cap ] = true;
}
}
return $caps;
}
function register_post_type() {
if ( 'handbook' != $this->post_type ) {
$slug = substr( $this->post_type, 0, -9 );
} else {
$slug = 'handbook';
}
$default_config = array(
'labels' => array(
'name' => $this->label,
'singular_name' => sprintf( __( '%s Page', 'wporg' ), $this->label ),
'menu_name' => $this->label,
'all_items' => sprintf( __( '%s Pages', 'wporg' ), $this->label ),
'add_new' => sprintf( __( 'New %s Page', 'wporg' ), $this->label ),
),
'public' => true,
'show_ui' => true,
'capability_type' => 'handbook_page',
'capabilities' => array(
'read' => 'read_handbook_page',
),
'map_meta_cap' => true,
'has_archive' => true,
'hierarchical' => true,
'menu_position' => 11,
'menu_icon' => 'dashicons-book-alt',
'rewrite' => array(
'feeds' => false,
'slug' => $slug,
'with_front' => false,
),
'delete_with_user' => false,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
);
// Allow customization of the default post type configuration via filter.
$config = apply_filters( 'handbook_post_type_defaults', $default_config, $slug );
register_post_type( $this->post_type, $config );
}
function post_type_link( $link, $post ) {
if ( $post->post_type === $this->post_type && $post->post_name === $this->post_type ) {
return get_post_type_archive_link( $this->post_type );
}
return $link;
}
function pre_get_posts( $query ) {
if ( $query->is_main_query() && ! $query->is_admin && $query->is_post_type_archive( $this->post_type ) ) {
// If the post type has a page to act as an archive index page, get that.
if ( $page = get_page_by_path( $this->post_type, OBJECT, $this->post_type ) ) {
$query->set( 'p', $page->ID );
}
$query->set( 'handbook', $this->post_type );
}
}
function handbook_sidebar() {
register_sidebar( array(
'id' => $this->post_type,
'name' => $this->label,
'description' => sprintf( __( 'Used on %s pages', 'wporg' ), $this->label ),
) );
require_once dirname( __FILE__ ) . '/inc/widgets.php';
register_widget( 'WPorg_Handbook_Pages_Widget' );
}
function wporg_email_changes_for_post_types( $post_types ) {
if ( ! in_array( $this->post_type, $post_types ) ) {
$post_types[] = $this->post_type;
}
return $post_types;
}
/**
* Disable the P2 Resolved Posts plugin's action links (e.g. "Flag Unresolved"),
* if that plugin is active.
*/
function disable_p2_resolved_posts_action_links() {
if ( ( $this->post_type == get_post_type() ) && class_exists( 'P2_Resolved_Posts' ) && isset( $GLOBALS['p2_resolved_posts'] ) && is_object( $GLOBALS['p2_resolved_posts'] ) ) {
remove_filter( 'p2_action_links', array( P2_Resolved_Posts::instance(), 'p2_action_links' ), 100 );
}
}
}