/
post-forking.php
executable file
·418 lines (322 loc) · 11.8 KB
/
post-forking.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
/**
* Plugin Name: Post Forking
* Description: Post forking
* Author: Benjamin J. Balter, Daniel Bachhuber, Aaron Jorbin
* Version: 0.2-alpha
* Plugin URI: http://postforking.wordpress.com
* License: GPLv3 or Later
* Domain Path: /languages
* Text Domain: fork
*/
/* Post Forking
*
* Provides users that would not normally be able to edit a post with the ability to submit revisions.
* This can be users on a site without the `edit_post` or `edit_published_post` capabilities,
* or can be members of the general public. Also allows post authors to edit published posts
* without their changes going appearing publicly until published.
*
* Copyright (C) 2012 Benjamin J. Balter ( Ben@Balter.com | http://ben.balter.com )
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @copyright 2012
* @license GPL v3
* @version 0.1
* @package post_forking
* @author Benjamin J. Balter <ben@balter.com>
*/
require_once dirname( __FILE__ ) . '/includes/capabilities.php';
require_once dirname( __FILE__ ) . '/includes/options.php';
require_once dirname( __FILE__ ) . '/includes/admin.php';
require_once dirname( __FILE__ ) . '/includes/merge.php';
require_once dirname( __FILE__ ) . '/includes/revisions.php';
require_once dirname( __FILE__ ) . '/includes/branches.php';
require_once dirname( __FILE__ ) . '/includes/diff.php';
require_once dirname( __FILE__ ) . '/includes/preview.php';
class Fork {
const post_type = 'fork';
public $post_type_support = 'fork'; //key to register when adding post type support
public $fields = array( 'post_title', 'post_content' ); //post fields to map from post to fork
public $version = '0.2-alpha';
/**
* Register initial hooks with WordPress core
*/
function __construct() {
$this->capabilities = new Fork_Capabilities( $this );
$this->options = new Fork_Options( $this );
$this->branches = new Fork_Branches( $this );
$this->preview = new Fork_Preview( $this );
add_action( 'init', array( $this, 'register_cpt' ) );
add_action( 'init', array( $this, 'action_init' ) );
add_action( 'init', array( $this, 'add_post_type_support'), 999 );
add_action( 'init', array( $this, 'l10n'), 5 );
add_filter( 'the_title', array( $this, 'title_filter'), 10, 3 );
add_action( 'delete_post', array( $this, 'delete_post' ) );
}
/**
* Init i18n files
* Must be done early on init because they need to be in place when register_cpt is called
*/
function l10n() {
load_plugin_textdomain( 'post-forking', false, plugin_basename( dirname( __FILE__ ) ) . '/languages/' );
}
/**
* Pseudo-lazy loading of back-end functionality
*/
function action_init() {
if ( !is_admin() )
return;
$this->admin = new Fork_Admin( $this );
$this->revisions = new Fork_Revisions( $this );
$this->merge = new Fork_Merge( $this );
$this->diff = new Fork_Diff( $this );
}
/**
* Register "fork" custom post type
*/
function register_cpt() {
$labels = array(
'name' => _x( 'Forks', 'post-forking' ),
'singular_name' => _x( 'Fork', 'post-forking' ),
'add_new' => false,
'add_new_item' => false,
'edit_item' => _x( 'Edit Fork', 'post-forking' ),
'new_item' => _x( 'New Fork', 'post-forking' ),
'view_item' => _x( 'View Fork', 'post-forking' ),
'search_items' => _x( 'Search Forks', 'post-forking' ),
'not_found' => _x( 'No forks found', 'post-forking' ),
'not_found_in_trash' => _x( 'No forks found in Trash', 'post-forking' ),
'parent_item_colon' => _x( 'Parent Fork:', 'post-forking' ),
'menu_name' => _x( 'Forks', 'post-forking' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'author', 'revisions' ),
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'publicly_queryable' => true,
'exclude_from_search' => true,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'map_meta_cap' => true,
'capability_type' => array( 'fork', 'forks' ),
'menu_icon' => plugins_url( '/img/menu-icon.png', __FILE__ ),
);
register_post_type( self::post_type, $args );
$status_args = array(
'label' => _x( 'Merged', 'post-forking' ),
'public' => true,
'exclude_from_search' => true,
'label_count' => _n_noop( 'Merged <span class="count">(%s)</span>', 'Merged <span class="count">(%s)</span>' ),
);
register_post_status( 'merged', $status_args );
}
/**
* Load a template. MVC FTW!
* @param string $template the template to load, without extension (assumes .php). File should be in templates/ folder
* @param args array of args to be run through extract and passed to tempalate
*/
function template( $template, $args = array() ) {
extract( $args );
if ( !$template )
return false;
$path = dirname( __FILE__ ) . "/templates/{$template}.php";
$path = apply_filters( 'fork_template', $path, $template );
include $path;
}
/**
* Returns an array of post type => bool to indicate whether the post type(s) supports forking
* All post types will be included
* @param bool $filter whether to return all post types (false) or just the ones toggled (true)
* @return array an array of post types and their forkability
*/
function get_post_types( $filter = false ) {
$active_post_types = $this->options->post_types;
$post_types = array();
foreach ( $this->get_potential_post_types() as $pt )
$post_types[ $pt->name ] = ( array_key_exists( $pt->name, (array) $active_post_types ) && $active_post_types[ $pt->name ] );
if ( $filter )
$post_types = array_keys( array_filter( $post_types ) );
$post_types = apply_filters( 'fork_post_types', $post_types, $filter );
return $post_types;
}
/**
* Returns an array of post type objects for all registered post types other than fork
* @param return array array of post type objects
*/
function get_potential_post_types() {
$post_types = get_post_types( array( 'show_ui' => true ), 'objects' );
unset( $post_types['fork'] );
return $post_types;
}
/**
* Registers post_type_support for forking with all active post types on load
*/
function add_post_type_support() {
foreach ( $this->get_post_types() as $post_type => $status )
if ( $status == true )
add_post_type_support( $post_type, $this->post_type_support );
}
/**
* Checks if a user has a fork for a given post, or optionally for any post
* @param int $parent_id the post_id of the parent post to check
* @param int|string the author to check
* @return int|bool the fork id or false if no fork exists
*/
function user_has_fork( $parent_id = null, $author = null ) {
if ( $author == null )
$author = wp_get_current_user()->nicename;
$args = array(
'post_type' => 'fork',
'author' => $author,
'post_status' => array( 'draft', 'pending' ),
);
if ( $parent_id != null )
$args[ 'post_parent' ] = (int) $parent_id;
$posts = get_posts( $args );
if ( empty( $posts ) )
return false;
return reset( $posts )->ID;
}
/**
* Returns an filterable list of fields to copy from original post to the fork
*/
function get_fork_fields() {
return apply_filters( 'fork_fields', $this->fields );
}
/**
* Main forking function
* @param int|object $p the post_id or post object to fork
* @param string the nicename of author to fork post as
* @return int the ID of the fork
*/
function fork( $p = null, $author = null ) {
global $post;
if ( $p == null )
$p = $post;
if ( !is_object( $p ) )
$p = get_post( $p );
if ( !$p )
return false;
if ( $author == null )
$author = wp_get_current_user()->ID;
//bad post type, enable via forks->options
if ( !post_type_supports( $p->post_type, $this->post_type_support ) )
wp_die( __( 'That post type does not support forking', 'post-forking' ) );
//hook into this cap check via map_meta cap
// for custom capabilities
if ( !user_can( $author, 'fork_post', $p->ID ) )
wp_die( __( 'You are not authorized to fork that post', 'post-forking' ) );
//user already has a fork, just return the existing ID
if ( $fork = $this->user_has_fork( $p->ID, $author ) )
return $fork;
//set up base fork data array
$fork = array(
'post_type' => 'fork',
'post_author' => $author,
'post_status' => 'draft',
'post_parent' => $p->ID,
);
//copy necessary post fields over to fork data array
foreach ( $this->get_fork_fields() as $field )
$fork[ $field ] = $p->$field;
$fork_id = wp_insert_post( $fork );
//something went wrong
if ( !$fork_id )
return false;
//note: $p = parent post object
do_action( 'fork', $fork_id, $p, $author );
return $fork_id;
}
/**
* Wrapper for get_post that auto-adds the argument post_type => 'fork'
* Also applies a filter, which is used consistently internally, to batch filter plugin behavior
* @param array $args the arguments you'd normally pass to get_posts
* @param array array of post objects
*/
function get_forks( $args = array() ) {
$args['post_type'] = 'fork';
$forks = get_posts( $args );
$forks = apply_filters( 'forks_query', $forks );
return $forks;
}
/**
* Given a fork, gets the name of the parent post
* @param int|object $fork the fork ID or or object (optional, falls back to global $post)
* @return string the name of the parent post
*/
function get_parent_name( $fork = null ) {
global $post;
if ( $fork == null )
$fork = $post;
if ( !is_object( $fork ) )
$fork = get_post( $fork );
if ( !$fork )
return;
$parent = get_post( $fork->post_parent );
$author = get_user_by( 'id', $parent->post_author );
$name = $author->user_nicename . ' » ';
$name .= get_the_title( $parent );
return $name;
}
/**
* Given a fork, returns the true name of the fork, filterless
* @param int|object $fork the fork ID or or object (optional, falls back to global $post)
* @return string the name of the fork
*/
function get_fork_name( $fork = null ) {
global $post;
if ( $fork == null )
$fork = $post;
if ( !is_object( $fork ) )
$fork = get_post( $fork );
if ( !$fork )
return;
$author = new WP_User( $fork->post_author );
$parent = get_post( $fork->post_parent );
$name = $author->user_nicename . ' » ';
remove_filter( 'the_title', array( $this, 'title_filter'), 10, 3 );
$name .= get_the_title( $parent->ID );
add_filter( 'the_title', array( $this, 'title_filter'), 10, 3 );
return $name;
}
/**
* Filter fork titles
* @param string $title the post title
* @param int $id the post ID
* @return string the modified post title
*/
function title_filter( $title, $id = 0 ) {
if ( get_post_type( $id ) != 'fork' )
return $title;
return $this->get_fork_name( $id );
}
/**
* When post is deleted, delete posts
* @param int $post_id the parent post
*/
function delete_post( $post_id ) {
//post delete
if ( !get_post( $post_id ) )
return;
foreach( $this->get_forks( array( 'post_parent' => $post_id ) ) as $fork )
wp_delete_post( $fork->ID );
}
}
$fork = new Fork();