1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: MJJ BBP Menu Order Update |
---|
4 | Plugin URI: http://emptywaterbottles.com |
---|
5 | Description: recalculates menu_order in a wp_posts for a given topic |
---|
6 | Author: Mary (JJ) Jay |
---|
7 | Author URI: http://emptywaterbottles.com |
---|
8 | Version: 0.1 |
---|
9 | */ |
---|
10 | |
---|
11 | class MJJBBPUpdateReplyPosition{ |
---|
12 | |
---|
13 | function __construct() { |
---|
14 | add_action( 'admin_menu', array( $this, 'mjj_bbp_update_reply_position_page' ) ); |
---|
15 | } |
---|
16 | |
---|
17 | //add the admin page to users http://codex.wordpress.org/Creating_Options_Pages |
---|
18 | public function mjj_bbp_update_reply_position_page() |
---|
19 | { |
---|
20 | // This page will be under "Settings" |
---|
21 | add_management_page( |
---|
22 | 'MJJ BBP Update Reply Position', |
---|
23 | 'MJJ BBP Update Reply Position', |
---|
24 | 'manage_options', |
---|
25 | 'mjj-bbp-update-reply-position', |
---|
26 | array( $this, 'create_admin_page' ) |
---|
27 | ); |
---|
28 | } |
---|
29 | |
---|
30 | //creates the view page in admin |
---|
31 | public function create_admin_page() |
---|
32 | { ?> |
---|
33 | <div id="mjj-updatable" class="wrap"> |
---|
34 | |
---|
35 | |
---|
36 | <?php |
---|
37 | |
---|
38 | //Marlene Dietrich |
---|
39 | screen_icon(); ?> |
---|
40 | |
---|
41 | <h2>Update menu_order if your permalinks have gone off</h2> |
---|
42 | |
---|
43 | <form action="" class="updatable" method="post"> |
---|
44 | <label>Put in the topic id of the confused topic</label> |
---|
45 | <input type="number" name="topic_id" value="" /> |
---|
46 | <input type="submit" value="submit" /> |
---|
47 | |
---|
48 | </form> |
---|
49 | |
---|
50 | <?php |
---|
51 | if( current_user_can( 'manage_options' ) && isset( $_POST['topic_id'] ) && !empty( $_POST['topic_id'] ) ){ |
---|
52 | $topic_id = $_POST['topic_id']; |
---|
53 | $clean_topic_id = absint( $topic_id ); |
---|
54 | echo '<h2>The results: </h2>'; |
---|
55 | mjj_bbp_update_reply_position( $clean_topic_id ); |
---|
56 | } |
---|
57 | ?> |
---|
58 | |
---|
59 | |
---|
60 | </div> |
---|
61 | |
---|
62 | <?php } |
---|
63 | } |
---|
64 | |
---|
65 | new MJJBBPUpdateReplyPosition(); |
---|
66 | |
---|
67 | |
---|
68 | function mjj_bbp_update_reply_position( $topic_id ) { |
---|
69 | |
---|
70 | $args = array( |
---|
71 | 'orderby' => 'date', |
---|
72 | 'order' => 'ASC', |
---|
73 | 'post_parent' => $topic_id, |
---|
74 | 'post_type' => 'reply' |
---|
75 | ); |
---|
76 | $reply_array = get_children( $args ); |
---|
77 | // Bail if reply_id is empty |
---|
78 | foreach( $reply_array as $reply ){ |
---|
79 | $reply_id = bbp_get_reply_id( $reply->ID ); |
---|
80 | if ( empty( $reply_id ) ){ |
---|
81 | |
---|
82 | return false; |
---|
83 | } |
---|
84 | |
---|
85 | // No if statement here - do them all |
---|
86 | |
---|
87 | $reply_position = bbp_get_reply_position_raw( $reply_id, bbp_get_reply_topic_id( $reply_id ) ); |
---|
88 | |
---|
89 | |
---|
90 | // Update the replies' 'menp_order' with the reply position |
---|
91 | wp_update_post( array( |
---|
92 | 'ID' => $reply_id, |
---|
93 | 'menu_order' => $reply_position |
---|
94 | ) ); |
---|
95 | |
---|
96 | echo 'Post '. $reply_id . ' is now at position ' . $reply_position . '<br />'; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | ?> |
---|