From 35614c112c9865b41c698c61533dcc79b965feb6 Mon Sep 17 00:00:00 2001 From: Chan Shing Fai Date: Fri, 26 Apr 2013 15:01:52 +0800 Subject: [PATCH 1/6] Show help pages depends on the user role --- wp-help.php | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/wp-help.php b/wp-help.php index 3d6667f..f73eb04 100644 --- a/wp-help.php +++ b/wp-help.php @@ -578,16 +578,45 @@ public function admin_menu() { add_action( "load-{$hook}", array( $this, 'enqueue' ) ); } + function get_editable_roles() { + global $wp_roles; + $roles = $wp_roles->get_names(); + return $roles; + } + + public function submitbox_actions() { if ( self::POST_TYPE !== get_post_type() ) return; global $post; wp_nonce_field( 'cws-wp-help-save', '_cws_wp_help_nonce', false, true ); ?>
ID == get_option( self::default_doc ) ); ?> />  
+
+ Role:
+
+ ID, '_cws_wp_help_permission'); + $role = get_editable_roles(); + $key = array_keys($role); + foreach ($key as $role_name){ + echo ' '.$role[$role_name]['name'].'
'; + } + ?> +
+
filter_wp_list_pages = true; $this->filter_wp_list_pages_sql = true; $status = ( current_user_can( $this->get_cap( 'read_private_posts' ) ) ) ? 'private' : 'publish'; - $defaults = array( 'post_type' => self::POST_TYPE, 'post_status' => $status, 'hierarchical' => true, 'echo' => false, 'title_li' => '' ); + //filter help post with different permission + $help_query = new WP_Query( array( 'post_type' => self::POST_TYPE, 'posts_per_page' => -1, 'post_status' => 'publish' ) ); + if ( $help_query->posts ) { + foreach ( $help_query->posts as $p ) { + $data = get_post_meta($p->ID, '_cws_wp_help_permission'); + $current_role = $current_user->roles[0]; + if (!($data[0][$current_role])){ + $filter_page .= $p->ID .','; + } + } + } + $defaults = array( 'post_type' => self::POST_TYPE, 'post_status' => $status, 'hierarchical' => true, 'echo' => false, 'title_li' => '', 'exclude' => $filter_page ); $output = trim( wp_list_pages( apply_filters( 'cws_wp_help_list_pages', $defaults ) ) ); $this->filter_wp_list_pages = $this->filter_wp_list_pages_sql = false; return $output; From 95be4f4c776b27c994772b3f4a3479ab5c0c8f91 Mon Sep 17 00:00:00 2001 From: Chan Shing Fai Date: Fri, 26 Apr 2013 16:43:12 +0800 Subject: [PATCH 2/6] Fix bug: admin cant read old posts / block direct link access --- templates/list-documents.php | 3 ++- wp-help.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/templates/list-documents.php b/templates/list-documents.php index 349c478..f64e317 100644 --- a/templates/list-documents.php +++ b/templates/list-documents.php @@ -33,7 +33,8 @@ self::POST_TYPE, 'p' => $document_id, 'post_status' => array( 'publish', 'private' ) ) ); ?> have_posts() ) : $document->the_post(); ?> - + + roles[0]]) : ?>

', '' ); ?>explain_slurp( $document_id ); ?>

diff --git a/wp-help.php b/wp-help.php index f73eb04..0874a54 100644 --- a/wp-help.php +++ b/wp-help.php @@ -615,7 +615,6 @@ public function save_post( $post_id ) { foreach ($key as $role_name){ $stack[$role_name] = isset($_POST['help-'.$role[$role_name]['name']] ) ? true:false; } - $stack['administrator'] = true; update_post_meta( $post_id, '_cws_wp_help_permission', $stack); if ( isset( $_POST['_cws_wp_help_nonce'] ) && wp_verify_nonce( $_POST['_cws_wp_help_nonce'], 'cws-wp-help-save' ) ) { if ( isset( $_POST['cws_wp_help_make_default_doc'] ) ) { @@ -684,6 +683,7 @@ private function get_help_topics_html( $with_sort_handles = false ) { foreach ( $help_query->posts as $p ) { $data = get_post_meta($p->ID, '_cws_wp_help_permission'); $current_role = $current_user->roles[0]; + $data[0]['administrator'] = true; if (!($data[0][$current_role])){ $filter_page .= $p->ID .','; } From 5f63f16241f95479640fe34a7bad4a71f4f39996 Mon Sep 17 00:00:00 2001 From: Chan Shing Fai Date: Fri, 26 Apr 2013 17:23:05 +0800 Subject: [PATCH 3/6] Remove 'adminstrator' option to prevent error --- templates/list-documents.php | 2 +- wp-help.php | 29 +++++++++++++++++------------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/templates/list-documents.php b/templates/list-documents.php index f64e317..bb6617e 100644 --- a/templates/list-documents.php +++ b/templates/list-documents.php @@ -34,7 +34,7 @@ self::POST_TYPE, 'p' => $document_id, 'post_status' => array( 'publish', 'private' ) ) ); ?> have_posts() ) : $document->the_post(); ?> - roles[0]]) : ?> + roles[0]] )) || current_user_can('manage_options') ) : ?>

', '' ); ?>explain_slurp( $document_id ); ?>

diff --git a/wp-help.php b/wp-help.php index 0874a54..fe4a64f 100644 --- a/wp-help.php +++ b/wp-help.php @@ -592,16 +592,18 @@ public function submitbox_actions() { wp_nonce_field( 'cws-wp-help-save', '_cws_wp_help_nonce', false, true ); ?>
ID == get_option( self::default_doc ) ); ?> />  
- Role:
+ Permission:
ID, '_cws_wp_help_permission'); $role = get_editable_roles(); $key = array_keys($role); foreach ($key as $role_name){ - echo ' '.$role[$role_name]['name'].'
'; + if ($role_name != 'administrator'){ + echo ' '.$role[$role_name]['name'].'
'; + } } ?>
@@ -613,7 +615,9 @@ public function save_post( $post_id ) { $role = get_editable_roles(); $key = array_keys($role); foreach ($key as $role_name){ - $stack[$role_name] = isset($_POST['help-'.$role[$role_name]['name']] ) ? true:false; + if ($role_name != 'administrator'){ + $stack[$role_name] = isset($_POST['help-'.$role[$role_name]['name']] ) ? true:false; + } } update_post_meta( $post_id, '_cws_wp_help_permission', $stack); if ( isset( $_POST['_cws_wp_help_nonce'] ) && wp_verify_nonce( $_POST['_cws_wp_help_nonce'], 'cws-wp-help-save' ) ) { @@ -679,13 +683,14 @@ private function get_help_topics_html( $with_sort_handles = false ) { $status = ( current_user_can( $this->get_cap( 'read_private_posts' ) ) ) ? 'private' : 'publish'; //filter help post with different permission $help_query = new WP_Query( array( 'post_type' => self::POST_TYPE, 'posts_per_page' => -1, 'post_status' => 'publish' ) ); - if ( $help_query->posts ) { - foreach ( $help_query->posts as $p ) { - $data = get_post_meta($p->ID, '_cws_wp_help_permission'); - $current_role = $current_user->roles[0]; - $data[0]['administrator'] = true; - if (!($data[0][$current_role])){ - $filter_page .= $p->ID .','; + if (current_user_can('manage_options') == false) { + if ( $help_query->posts ) { + foreach ( $help_query->posts as $p ) { + $data = get_post_meta($p->ID, '_cws_wp_help_permission'); + $current_role = $current_user->roles[0]; + if (!($data[0][$current_role])){ + $filter_page .= $p->ID .','; + } } } } From 347d11a66364566b172ae00290277aaecc91fbca Mon Sep 17 00:00:00 2001 From: Chan Shing Fai Date: Fri, 26 Apr 2013 18:09:24 +0800 Subject: [PATCH 4/6] fix error if the role name contain space --- templates/list-documents.php | 2 +- wp-help.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/list-documents.php b/templates/list-documents.php index bb6617e..1e61d05 100644 --- a/templates/list-documents.php +++ b/templates/list-documents.php @@ -34,7 +34,7 @@ self::POST_TYPE, 'p' => $document_id, 'post_status' => array( 'publish', 'private' ) ) ); ?> have_posts() ) : $document->the_post(); ?> - roles[0]] )) || current_user_can('manage_options') ) : ?> + roles[0]]) ) ) : ?>

', '' ); ?>explain_slurp( $document_id ); ?>

diff --git a/wp-help.php b/wp-help.php index fe4a64f..b62bf1a 100644 --- a/wp-help.php +++ b/wp-help.php @@ -600,7 +600,7 @@ public function submitbox_actions() { $key = array_keys($role); foreach ($key as $role_name){ if ($role_name != 'administrator'){ - echo ' '.$role[$role_name]['name'].'
'; } @@ -616,7 +616,7 @@ public function save_post( $post_id ) { $key = array_keys($role); foreach ($key as $role_name){ if ($role_name != 'administrator'){ - $stack[$role_name] = isset($_POST['help-'.$role[$role_name]['name']] ) ? true:false; + $stack[$role_name] = isset($_POST['help-'.$role_name] ) ? true:false; } } update_post_meta( $post_id, '_cws_wp_help_permission', $stack); From 79b52f990f89f0ca5fdaae8b298f1bf09519e978 Mon Sep 17 00:00:00 2001 From: Chan Shing Fai Date: Fri, 28 Jun 2013 19:10:09 +0800 Subject: [PATCH 5/6] Fix php error --- wp-help.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wp-help.php b/wp-help.php index b62bf1a..4a764fe 100644 --- a/wp-help.php +++ b/wp-help.php @@ -596,7 +596,7 @@ public function submitbox_actions() {
ID, '_cws_wp_help_permission'); - $role = get_editable_roles(); + $role = $this->get_editable_roles(); $key = array_keys($role); foreach ($key as $role_name){ if ($role_name != 'administrator'){ @@ -612,7 +612,7 @@ public function submitbox_actions() { } public function save_post( $post_id ) { - $role = get_editable_roles(); + $role = $this->get_editable_roles(); $key = array_keys($role); foreach ($key as $role_name){ if ($role_name != 'administrator'){ From 54622b0117ebab46a9804b4e7eb654fefd25e777 Mon Sep 17 00:00:00 2001 From: Chan Shing Fai Date: Fri, 28 Jun 2013 19:34:43 +0800 Subject: [PATCH 6/6] add flag only if it is a help page --- wp-help.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/wp-help.php b/wp-help.php index 4a764fe..91fcac2 100644 --- a/wp-help.php +++ b/wp-help.php @@ -601,8 +601,9 @@ public function submitbox_actions() { foreach ($key as $role_name){ if ($role_name != 'administrator'){ echo ' '.$role[$role_name]['name'].'
'; + if ( count($data) > 0 ) + echo $data[0][$role_name] ? 'checked' : ''; + echo '> '. ucfirst($role_name) .'
'; } } ?> @@ -612,15 +613,15 @@ public function submitbox_actions() { } public function save_post( $post_id ) { - $role = $this->get_editable_roles(); - $key = array_keys($role); - foreach ($key as $role_name){ - if ($role_name != 'administrator'){ - $stack[$role_name] = isset($_POST['help-'.$role_name] ) ? true:false; - } - } - update_post_meta( $post_id, '_cws_wp_help_permission', $stack); if ( isset( $_POST['_cws_wp_help_nonce'] ) && wp_verify_nonce( $_POST['_cws_wp_help_nonce'], 'cws-wp-help-save' ) ) { + $role = $this->get_editable_roles(); + $key = array_keys($role); + foreach ($key as $role_name){ + if ($role_name != 'administrator'){ + $stack[$role_name] = isset($_POST['help-'.$role_name] ) ? true:false; + } + } + update_post_meta( $post_id, '_cws_wp_help_permission', $stack); if ( isset( $_POST['cws_wp_help_make_default_doc'] ) ) { // Make it the default_doc update_option( self::default_doc, absint( $post_id ) );