Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/knowledge-building/comments.php
+++ b/knowledge-building/comments.php
@@ -1,12 +1,26 @@
-
-<div><?php knbu_list_comments(); ?></div>
-<br>
-<div id="respond">
- <h3 id="reply-title">Leave a Reply</h3>
-
- <form action="<?php echo site_url(); ?>/wp-comments-post.php" method="post" id="commentform">
- <?php comment_id_fields(); ?>
- <?php knbu_comment_form_map(get_the_ID()); ?>
-<?php do_action( 'comment_form', $post_id ); ?>
- </form>
-</div>
+<?php
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+// Same rule as core themes: hide comments until the post password is given.
+if ( post_password_required() ) {
+ return;
+}
+?>
+<div><?php knbu_list_comments(); ?></div>
+<br>
+<?php if ( comments_open() ) { ?>
+<div id="respond">
+ <h3 id="reply-title">Leave a Reply</h3>
+ <?php if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) { ?>
+ <p class="must-log-in">You must be <a href="<?php echo esc_url( wp_login_url( get_permalink() ) ); ?>">logged in</a> to post a comment.</p>
+ <?php } else { ?>
+ <form action="<?php echo esc_url( site_url( '/wp-comments-post.php' ) ); ?>" method="post" id="commentform">
+ <?php comment_id_fields(); ?>
+ <?php knbu_comment_form_map(get_the_ID()); ?>
+<?php do_action( 'comment_form', get_the_ID() ); ?>
+ </form>
+ <?php } ?>
+</div>
+<?php } ?>
--- a/knowledge-building/knowledge-building.php
+++ b/knowledge-building/knowledge-building.php
@@ -3,7 +3,7 @@
Plugin Name: Knowledge Building
Plugin URI: http://fle4.aalto.fi/kb-wp-plugin
Description: Use post comment threads to facilitate meaningful knowledge building discussions. Comes with several knowledge type sets (eg. progressive inquiry, six hat thinking) that can be used to semantically tag comments, turning your Wordpress into a knowledge building environment. Especially useful in educational settings.
-Version: 0.7.2
+Version: 0.7.3
Author: Tarmo Toikkanen, Antti Sandberg
Author URI: http://tarmo.fi
*/
@@ -25,16 +25,17 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
global $knbu_db_version;
$knbu_db_version='0.12';
-$knbu_plugin_version = '0.7.2';
+$knbu_plugin_version = '0.7.3';
-add_action('init', 'knbu_init_mapview');
add_action('wp_enqueue_scripts', 'knbu_enqueue_scripts');
-//add_action('comment_form', 'knbu_comment_form_map');
-add_action('init','knbu_ajax_hook');
-add_action('wp_print_scripts', 'knbu_script_load');
-add_action('wp_print_styles', 'knbu_custom_stylesheet');
+add_action('wp_enqueue_scripts', 'knbu_script_load');
+add_action('wp_enqueue_scripts', 'knbu_custom_stylesheet');
add_action('comments_array', 'knbu_fetch_ktypes', 10, 2);
add_action('comment_post', 'knbu_store_comment');
add_action('admin_menu', 'knbu_plugin_menu');
@@ -49,44 +50,42 @@
add_filter( 'comments_template', 'knbu_comment_template' );
add_filter('template_include', 'knbu_map_view_template');
-add_filter('comment_save_pre', 'knbu_store_comment');
register_activation_hook(__FILE__,'knbu_install');
function knbu_upgrade_hook() {
global $knbu_plugin_version, $wpdb;
-
+
+ if ( ! current_user_can( 'manage_options' ) ) return;
+
if(get_option( 'knbu_plugin_version' ) != $knbu_plugin_version) {
-
+
$table_name = $wpdb->prefix . 'knowledgetypes';
-
- if( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name )
- { knbu_custom_table_to_comment_meta(); }
-
+
+ if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) ) ) == $table_name ) {
+ // Try again on a later admin page load if the migration fails.
+ if ( ! knbu_custom_table_to_comment_meta() ) return;
+ }
+
update_option( 'knbu_plugin_version', $knbu_plugin_version );
}
}
/*
- * This code snippet loads the XML specifications of KB typesets into memory.
- * Uses wp_cache to optimize performance.
+ * Load the XML specifications of KB typesets into memory.
+ *
+ * The files are small and local, so they are parsed on every request.
+ * SimpleXMLElement objects can't be serialized, so they must not be put into
+ * a persistent object cache.
*/
-$knbu_kbsets = wp_cache_get('knbu_kbsets');
-
-if ( $knbu_kbsets == false ) {
- $knbu_kbsets = array();
-# Read available KBset xml files into memory
- $kbset_dir = __DIR__.DIRECTORY_SEPARATOR.'kbsets';
- $d = dir($kbset_dir);
- while ( false != ($entry = $d->read()) ) {
- if ( preg_match('/.xml$/i',$entry) ) {
- $fname = explode('.',$entry);
- $fname = $fname[0];
- $knbu_kbsets[$fname]=simplexml_load_file($kbset_dir.DIRECTORY_SEPARATOR.$entry);
- }
+$knbu_kbsets = array();
+foreach ( (array) glob( __DIR__ . '/kbsets/*.xml' ) as $knbu_kbset_file ) {
+ $knbu_kbset_xml = simplexml_load_file( $knbu_kbset_file );
+ if ( $knbu_kbset_xml ) {
+ $knbu_kbsets[ pathinfo( $knbu_kbset_file, PATHINFO_FILENAME ) ] = $knbu_kbset_xml;
}
- wp_cache_set('knbu_kbsets',$knbu_kbsets);
}
+unset( $knbu_kbset_file, $knbu_kbset_xml );
/**
* Hooked into plugin activation.
@@ -109,7 +108,7 @@
* Register option page for this plugin.
*/
function knbu_plugin_menu() {
- add_options_page('Knowledge Building Plugin Options', 'Knowledge Building', 'administrator', __FILE__, 'knbu_plugin_options');
+ add_options_page('Knowledge Building Plugin Options', 'Knowledge Building', 'manage_options', 'knowledge-building', 'knbu_plugin_options');
// Temporarily added installation hook here, since register_activation_hook doesn't work properly
knbu_install();
}
@@ -123,48 +122,49 @@
global $knbu_kbsets;
$hidden = 'knbu_form_posted';
+ if ( ! current_user_can( 'manage_options' ) ) {
+ wp_die( 'You do not have sufficient permissions to access this page.' );
+ }
+
# Update options if form is submitted
if ( isset($_POST[$hidden]) && $_POST[$hidden] == 'Y' ) {
+ check_admin_referer( 'knbu_save_options' );
$sels = array();
- foreach ( $knbu_kbsets as $file => $xml ) {
- $fname = explode('.',$file);
- $fname = $fname[0];
- foreach ( get_categories(array('hide_empty' => 0)) as $name => $cat ) {
- $value = $_POST['cat_' . $cat->cat_ID];
- $sels[$cat->cat_ID] = $value;
- }
+ foreach ( get_categories(array('hide_empty' => 0)) as $cat ) {
+ $key = 'cat_' . $cat->cat_ID;
+ $value = isset( $_POST[ $key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) : '';
+ // Only accept the name of an installed type set.
+ $sels[$cat->cat_ID] = isset( $knbu_kbsets[ $value ] ) ? $value : '';
}
update_option('knbu_categories',$sels);
- update_option('knbu_main_title_required', $_POST['main-title-field'] == 'required');
+ update_option('knbu_main_title_required', isset( $_POST['main-title-field'] ) && 'required' === $_POST['main-title-field'] );
}
- $sels = get_option('knbu_categories'); ?>
+ $sels = (array) get_option('knbu_categories', array()); ?>
<div class="wrap">
- <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
- <input type="hidden" name="<?php echo $hidden; ?>" value="Y"/>
- <h2>Select Knowledgetype Sets to use with each Category</h2>
+ <form method="post" action="">
+ <?php wp_nonce_field( 'knbu_save_options' ); ?>
+ <input type="hidden" name="<?php echo esc_attr( $hidden ); ?>" value="Y"/>
+ <h1>Select Knowledgetype Sets to use with each Category</h1>
<p>
<table style="width: 100%; cell-spacing: 10px; padding-left: 40px;">
<tr valign="top">
<td></td>
<?php foreach ( get_categories(array('hide_empty' => 0)) as $name => $cat ) { ?>
- <th style="text-align:center;font-weight: bold;"><?php echo $cat->name; ?></th>
+ <th style="text-align:center;font-weight: bold;"><?php echo esc_html( $cat->name ); ?></th>
<?php } ?>
</tr>
<tr>
<th style="text-align: left">None</th>
<?php foreach ( get_categories(array('hide_empty' => 0)) as $name => $cat ) { ?>
- <td style="text-align:center;padding:10px 0;"><input type="radio" name="<?php echo 'cat_' . $cat->cat_ID;?>" value="" <?php if (!isset($sels[$cat->cat_ID]) || !$sels[$cat->cat_ID]) { echo 'checked="checked"'; } ?>/></td>
+ <td style="text-align:center;padding:10px 0;"><input type="radio" name="<?php echo esc_attr( 'cat_' . $cat->cat_ID ); ?>" value="" <?php if (!isset($sels[$cat->cat_ID]) || !$sels[$cat->cat_ID]) { echo 'checked="checked"'; } ?>/></td>
<?php } ?>
</tr>
- <?php foreach ( $knbu_kbsets as $file => $xml ) {
- $fname = explode('.',$file);
- $fname = $fname[0];
- ?>
+ <?php foreach ( $knbu_kbsets as $fname => $xml ) { ?>
<tr style="padding: 10px 0">
- <th style="padding: 10px 0;text-align: left"><?php echo $xml->KnowledgeTypeSet['Name']; ?>
+ <th style="padding: 10px 0;text-align: left"><?php echo esc_html( $xml->KnowledgeTypeSet['Name'] ); ?>
</th>
<?php foreach ( get_categories(array('hide_empty' => 0)) as $name => $cat ) { ?>
- <td style="text-align:center"><input type="radio" name="<?php echo 'cat_' . $cat->cat_ID; ?>" value="<?php echo $fname; ?>" <?php if (isset($sels[$cat->cat_ID]) && $sels[$cat->cat_ID] == $fname) { echo 'checked="checked"'; } ?>/></td>
+ <td style="text-align:center"><input type="radio" name="<?php echo esc_attr( 'cat_' . $cat->cat_ID ); ?>" value="<?php echo esc_attr( $fname ); ?>" <?php if (isset($sels[$cat->cat_ID]) && $sels[$cat->cat_ID] == $fname) { echo 'checked="checked"'; } ?>/></td>
<?php } ?>
</tr>
<?php } ?>
@@ -181,7 +181,7 @@
</table>
</p>
<p class="submit">
- <input type="submit" class="button-primary" value="<?php _e('Save Changes'); ?>" />
+ <input type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
</p>
</form>
</div>
@@ -199,17 +199,19 @@
* @return string Found KB typeset or false if not found.
*/
function knbu_get_kbset_for_post($id = false) {
- global $post;
-
- if(!$id)
+ global $post, $knbu_kbsets;
+
+ if ( ! $id ) {
+ if ( ! $post instanceof WP_Post ) return false;
$id = $post->ID;
-
+ }
+
$kbset = false;
- $sels = get_option('knbu_categories');
-
+ $sels = (array) get_option('knbu_categories', array());
+
foreach ( get_the_category($id) as $cat ) {
- $value = $sels[$cat->cat_ID];
- if ( $value ) $kbset = $value;
+ $value = isset( $sels[$cat->cat_ID] ) ? $sels[$cat->cat_ID] : '';
+ if ( $value && isset( $knbu_kbsets[$value] ) ) $kbset = $value;
}
return $kbset;
}
@@ -226,13 +228,9 @@
* @return array The KB type for the comment
*/
function knbu_get_ktype_for_comment($comment) {
- global $knbu_kbsets,$post;
- $kbset = knbu_get_kbset_for_post();
- $kbtypes = $knbu_kbsets[$kbset]->KnowledgeTypeSet[0]->KnowledgeType;
- foreach ( $kbtypes as $ktype ) {
- if ( $ktype['ID'] == $comment->ktype ) return $ktype;
- }
- return false;
+ $type_id = get_comment_meta( $comment->comment_ID, 'kbtype', true );
+ if ( '' === $type_id ) return false;
+ return knbu_get_type( $type_id, $comment->comment_post_ID );
}
@@ -245,23 +243,29 @@
*/
function knbu_store_comment($comment) {
global $wpdb;
+ // phpcs:disable WordPress.Security.NonceVerification.Missing -- runs inside core comment submission (wp-comments-post.php), which has no nonce.
if ( !isset( $_POST['knbu_type'] ) ) return $comment;
- $ktype = $_POST['knbu_type'];
-
+
if (is_numeric($comment))
$comment = get_comment($comment);
-
+ if ( ! $comment ) return $comment;
+
+ // Only accept knowledge types that belong to this post's type set.
+ $ktype = knbu_get_type( sanitize_text_field( wp_unslash( $_POST['knbu_type'] ) ), $comment->comment_post_ID );
+ if ( ! $ktype ) return $comment;
+
+ $title = isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : '';
+
// Get map index
// Basically just the node number
$map_index = get_post_meta( $comment->comment_post_ID, 'knbu_map_index', true );
- update_post_meta( $comment->comment_post_ID, 'knbu_map_index', $map_index + 1 );
-
-
- //update_comment_meta( $id, 'knbu_map_comment_index', $var->index );
- update_comment_meta( $comment->comment_ID, 'comment_title', $_POST['title'] );
- update_comment_meta( $comment->comment_ID, 'kbtype', $ktype );
- update_comment_meta( $id, 'knbu_map_comment_index', $map_index );
+ update_post_meta( $comment->comment_post_ID, 'knbu_map_index', (int) $map_index + 1 );
+
+ update_comment_meta( $comment->comment_ID, 'comment_title', $title );
+ update_comment_meta( $comment->comment_ID, 'kbtype', (string) $ktype['ID'] );
+ update_comment_meta( $comment->comment_ID, 'knbu_map_comment_index', $map_index );
return $comment;
+ // phpcs:enable WordPress.Security.NonceVerification.Missing
}
/**
* Fetch the KB types for all comments.
@@ -306,20 +310,20 @@
<div id="comment_sorter">
<?php $sort = '
- <img class="sort-up" src="'.plugins_url().'/knowledge-building/images/sort-up.png">
- <img class="sort-down" src="'.plugins_url().'/knowledge-building/images/sort-down.png">'; ?>
+ <img class="sort-up" src="' . esc_url( plugins_url( 'images/sort-up.png', __FILE__ ) ) . '">
+ <img class="sort-down" src="' . esc_url( plugins_url( 'images/sort-down.png', __FILE__ ) ) . '">'; ?>
<h3>Show notes</h3>
<ul>
- <li data-type="on-a-map" class="selected">on a map <?php echo $sort; ?></li>
- <li data-type="as-thread">as thread <?php echo $sort; ?></li>
- <li data-type="by-knowledge-type">by knowledge type <?php echo $sort; ?></li>
- <li data-type="by-person">by person <?php echo $sort; ?></li>
- <li data-type="by-date">by date <?php echo $sort; ?></li>
+ <li data-type="on-a-map" class="selected">on a map <?php echo $sort; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static markup with escaped URLs ?></li>
+ <li data-type="as-thread">as thread <?php echo $sort; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static markup with escaped URLs ?></li>
+ <li data-type="by-knowledge-type">by knowledge type <?php echo $sort; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static markup with escaped URLs ?></li>
+ <li data-type="by-person">by person <?php echo $sort; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static markup with escaped URLs ?></li>
+ <li data-type="by-date">by date <?php echo $sort; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static markup with escaped URLs ?></li>
</ul>
</div>
<div id="map-frame">
- <a href="<?php echo add_query_arg( 'map-view', '1' ); ?>" target="_blank">
- <img src="<?php echo plugins_url(); ?>/knowledge-building/images/logo.png">
+ <a href="<?php echo esc_url( add_query_arg( 'map-view', '1', get_permalink() ) ); ?>" target="_blank">
+ <img src="<?php echo esc_url( plugins_url( 'images/logo.png', __FILE__ ) ); ?>">
<span>Open map view</span>
</a>
</div>
@@ -359,8 +363,9 @@
* @param int $depth Depth of comment in reference to parents.
* @param array $args
*/
- function start_el(&$output, $comment, $depth, $args) {
- global $comment_number;
+ public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
+ global $comment_number;
+ $comment = $data_object;
$depth++;
$GLOBALS['comment_depth'] = $depth;
@@ -370,187 +375,104 @@
}
$GLOBALS['comment'] = $comment;
- extract($args, EXTR_SKIP);
$ktype = knbu_get_ktype_for_comment($comment);
$avatar = knbu_get_avatar_url($comment->comment_author_email);
-
- echo knbu_comment($comment->comment_ID, array(
- "type" => $ktype["Name"],
- "type_id" => $ktype["ID"],
- "index" => get_comment_meta(get_comment_ID(), 'knbu_map_comment_index', true ),
- "title" => get_comment_meta(get_comment_ID(), 'comment_title', true ),
+ $index = get_comment_meta( $comment->comment_ID, 'knbu_map_comment_index', true );
+
+ // knbu_comment() prints the comment directly.
+ knbu_comment($comment->comment_ID, array(
+ "type" => $ktype ? (string) $ktype["Name"] : '',
+ "type_id" => $ktype ? (string) $ktype["ID"] : '',
+ "index" => $index,
+ "title" => get_comment_meta( $comment->comment_ID, 'comment_title', true ),
"username" => $comment->comment_author,
"content" => $comment->comment_content,
"date" => date(get_option('date_format'), strtotime($comment->comment_date)),
"time" => date(get_option('time_format'), strtotime($comment->comment_date)),
"avatar_src" => $avatar,
- "link_href" => "#".get_comment_meta(get_comment_ID(), 'knbu_map_comment_index', true ),
- "color" => $ktype['Colour'],
+ "link_href" => "#" . $index,
+ "color" => $ktype ? (string) $ktype['Colour'] : '#fff',
"number" => $comment_number++
), false);
-
- return;
-
- if ( 'div' == $args['style'] ) {
- $tag = 'div';
- $add_below = 'comment';
- } else {
- $tag = 'li';
- $add_below = 'div-comment';
- }
- ?>
- <<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>" data-comment-index="<?php echo ++$comment_index; ?>">
- <?php if ( 'ul' == $args['style'] ) : ?>
- <div id="div-comment-<?php comment_ID() ?>" class="comment-body" style="background-color: <?php echo ' ' . $ktype['Colour']; ?>">
- <?php endif; ?>
- <div class="kbtype-label"><?php echo get_comment_meta(get_comment_ID(), 'knbu_map_comment_index', true ); ?> - <span><?php echo $ktype['Name']; ?></span></div>
- <div class="comment-author vcard">
- <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
- <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
- </div>
- <?php if ($comment->comment_approved == '0') : ?>
- <em><?php _e('Your comment is awaiting moderation.') ?></em>
- <br />
- <?php endif; ?>
- <div class="comment-meta commentmetadata">
- <a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>" data-stamp="<?php echo strtotime(get_comment_date('Y-m-d').' '.get_comment_time('H:i:s')); ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),' ','') ?></div>
-
- <?php comment_text() ?>
-
- <div class="reply">
- <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
- </div>
- <?php if ( 'ul' == $args['style'] ) : ?>
- </div>
- <?php endif; ?>
- <?php
}
+
+ /*
+ * Comments are printed flat by start_el(), so the list and element
+ * wrappers of Walker_Comment must not be added to the output.
+ */
+ public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {}
+ public function start_lvl( &$output, $depth = 0, $args = array() ) {}
+ public function end_lvl( &$output, $depth = 0, $args = array() ) {}
}
/*
* Add custom stylesheets to style queue.
*
- * Hooked to wp_print_styles action.
+ * Hooked to wp_enqueue_scripts action.
*/
function knbu_custom_stylesheet() {
- $myStyleUrl = WP_PLUGIN_URL . '/knowledge-building/style.css';
- $myStyleFile = WP_PLUGIN_DIR . '/knowledge-building/style.css';
- if ( file_exists($myStyleFile) ) {
- wp_enqueue_style( 'myStyleSheets', $myStyleUrl);
- }
- //wp_enqueue_style('jquery-simpledialog', WP_PLUGIN_URL . '/knowledge-building/jquery.simpledialog/simpledialog.css');
+ wp_enqueue_style( 'knbu-style', plugins_url( 'style.css', __FILE__ ), array(), $GLOBALS['knbu_plugin_version'] );
}
/**
* Add custom javascript files when needed.
- * Hooked to wp_print_scripts action. Only adds scripts when displaying comments.
+ * Hooked to wp_enqueue_scripts action. Only adds scripts when displaying comments.
*/
function knbu_script_load() {
if ( ( is_single() || is_page() ) && comments_open() ) {
- //wp_enqueue_script('jquery-simpledialog', WP_PLUGIN_URL . '/knowledge-building/jquery.simpledialog/simpledialog.js', array('jquery'));
- wp_enqueue_script('knbu', WP_PLUGIN_URL . '/knowledge-building/knowledgebuilding.js', array('jquery','jquery-color') );
+ wp_enqueue_script( 'knbu', plugins_url( 'knowledgebuilding.js', __FILE__ ), array( 'jquery', 'jquery-color' ), $GLOBALS['knbu_plugin_version'] );
}
}
-
-/**
- * Ajax hook.
- *
- * Hooked to init action to insert our own ajax handler to the wp action
- * whenever the 'knbu_ktype_info' variable is detected.
- */
-function knbu_ajax_hook() {
- if ( isset( $_POST['knbu_ktype_info'] ) ) add_action('wp','knbu_ajax_ktype_provider');
-}
+
/**
* Ajax method called by comment form.
*
- * Hooked to wp action by knbu_ajax_hook. Will die after sending information, so
- * it only returns the json formatted data.
- *
- * @uses knbu_get_kbset_for_post
- * @uses json_encode
+ * Returns the description, checklist and starting phrases of one knowledge
+ * type as JSON.
*/
function knbu_provide_knbu_info() {
- global $knbu_kbsets;
- $kbset = knbu_get_kbset_for_post($_POST['post_ID']);
- $requested = $_POST['knbu_ID'];
-
- foreach ( $knbu_kbsets[$kbset]->KnowledgeTypeSet[0]->KnowledgeType as $ktype ) {
- if ( $ktype['ID'] == $requested ) {
- $cl = (string)$ktype->Checklist;
-
- die(json_encode(array(
- 'name' => (string)$ktype['Name'],
- 'color' => (string)$ktype['Colour'],
- 'phrases' => (string)$ktype->StartingPhrase,
- 'description' => nl2br((string)$ktype->Description),
- 'checklist' => nl2br($cl)
- )
- )
- );
-
- }
- }
- die(json_encode(array( 'error' => 'No knowledge type found' )));
+ // phpcs:disable WordPress.Security.NonceVerification.Missing -- public, read-only lookup of bundled type set data.
+ $post_id = isset( $_POST['post_ID'] ) ? absint( $_POST['post_ID'] ) : 0;
+ $requested = isset( $_POST['knbu_ID'] ) ? sanitize_text_field( wp_unslash( $_POST['knbu_ID'] ) ) : '';
+ // phpcs:enable WordPress.Security.NonceVerification.Missing
+ $post = $post_id ? get_post( $post_id ) : null;
+
+ if ( ! $post || ( ! is_post_publicly_viewable( $post ) && ! current_user_can( 'read_post', $post_id ) ) ) {
+ wp_send_json( array( 'error' => 'No knowledge type found' ) );
+ }
+
+ $ktype = knbu_get_type( $requested, $post_id );
+ if ( ! $ktype ) {
+ wp_send_json( array( 'error' => 'No knowledge type found' ) );
+ }
+
+ // The type set files use simple formatting tags; allow only those.
+ $allowed = array( 'b' => array(), 'strong' => array(), 'i' => array(), 'em' => array(), 'br' => array(), 'p' => array() );
+ wp_send_json( array(
+ 'name' => (string) $ktype['Name'],
+ 'color' => (string) $ktype['Colour'],
+ 'phrases' => (string) $ktype->StartingPhrase,
+ 'description' => wp_kses( nl2br( (string) $ktype->Description ), $allowed ),
+ 'checklist' => wp_kses( nl2br( (string) $ktype->Checklist ), $allowed ),
+ ) );
}
function knbu_get_type($type_id, $post_id = false) {
global $knbu_kbsets;
$kbset = knbu_get_kbset_for_post($post_id);
-
+ if ( ! $kbset || ! isset( $knbu_kbsets[$kbset] ) ) return false;
+
foreach ( $knbu_kbsets[$kbset]->KnowledgeTypeSet[0]->KnowledgeType as $ktype ) {
- if ( $ktype['ID'] == (string)$type_id ) {
+ if ( (string) $ktype['ID'] === (string) $type_id ) {
return $ktype;
}
}
return false;
}
-/**
- * Displays custom additions to comment editing form.
- *
- * Hooked into the comment_form action. If the current post's categories do not have
- * KB typesets mapped to them, returns without doing anything, so the normal comment
- * form is displayed. For KB enabled posts, adds cognitive scaffolding.
- *
- * @param int $post_ID The post for which scaffolding should be displayed.
- */
-function knbu_comment_form($post_ID) {
- global $knbu_kbsets,$post;
- $kbtype = knbu_get_kbset_for_post();
- if ( !$kbtype ) return;
-?>
- <div id="knbu" style="display:none;">
- <div id="knbu_scaffold" style="display:none;">
- <p id="knbu_heading" class="ktype_heading">Knowledge type</p>
- <div id="knbu_checklist">Checklist</div>
- <div id="knbu_popup" style="display:none;"><h3 id="knbu_popup_heading">Knowledge type</h3><p id="knbu_popup_p">Description</p></div>
- <span id="knbu_selector2"><select id="knbu_ktype2">
- <?php foreach ( $knbu_kbsets[$kbtype]->KnowledgeTypeSet[0]->KnowledgeType as $ktype ) {
- echo '<option value="' . $ktype['ID'] . '">' . $ktype['Name'] . '</option>';
- } ?>
- </select></span>
- <input id="knbu_select2" type="button" value="Change"/>
- </div>
- <p id="knbu_init">
- <span id="knbu_selector">Select <select id="knbu_ktype">
- <option value="">knowledge type</option>
- <?php foreach ( $knbu_kbsets[$kbtype]->KnowledgeTypeSet[0]->KnowledgeType as $ktype ) {
- echo '<option value="' . $ktype['ID'] . '">' . $ktype['Name'] . '</option>';
- } ?>
- </select>
- </span>
- <input id="knbu_select" type="button" value="Add comment"/>
- </p>
- <input id="knbu_real_ktype" type="text" style="display:none;"" name="knbu_ktype" value=""/>
- </div>
-<?php
-}
-
function knbu_comment_template( $comment_template ) {
- global $post;
- $knbu = knbu_get_kbset_for_post($post->post_ID);
+ $knbu = knbu_get_kbset_for_post( get_the_ID() );
if(!$knbu)
return $comment_template;
@@ -560,8 +482,9 @@
function knbu_comment_form_map($post_ID) {
?>
<div class="knbu-form">
- <input type="hidden" value="<?php echo admin_url('admin-ajax.php'); ?>" id="admin-ajax-url">
- <input type="hidden" value="<?php echo $post_ID; ?>" id="post-id">
+ <input type="hidden" value="<?php echo esc_url( admin_url('admin-ajax.php') ); ?>" id="admin-ajax-url">
+ <input type="hidden" value="<?php echo esc_attr( wp_create_nonce( 'knbu_new_reply' ) ); ?>" id="knbu-reply-nonce">
+ <input type="hidden" value="<?php echo esc_attr( $post_ID ); ?>" id="post-id">
<input type="hidden" name="parent-comment" id="parent-comment-id">
<?php if(is_user_logged_in()) { ?>
@@ -582,7 +505,7 @@
</p>
<?php } ?>
<p><label for="title">Knowledge type <span class="required">*</span></label>
- <?php echo knbu_get_knowledge_type_select(); ?>
+ <?php echo knbu_get_knowledge_type_select(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped when built ?>
</p>
<p class="knbu-checklist"></p>
<p><label for="comment">Comment <span class="required">*</span></label>
@@ -601,8 +524,11 @@
global $knbu_kbsets;
$value = '<select name="knbu_type" id="knbu-type-selector">
<option disabled selected>Select knowledge type</option>';
- foreach($knbu_kbsets[knbu_get_kbset_for_post(get_the_ID())]->KnowledgeTypeSet->KnowledgeType as $type)
- $value .= '<option value="'.$type['ID'].'">'.$type['Name'].'</option>';
+ $kbset = knbu_get_kbset_for_post(get_the_ID());
+ if ( $kbset ) {
+ foreach($knbu_kbsets[$kbset]->KnowledgeTypeSet->KnowledgeType as $type)
+ $value .= '<option value="' . esc_attr( $type['ID'] ) . '">' . esc_html( $type['Name'] ) . '</option>';
+ }
$value .= '</select>';
return $value;
}
@@ -610,127 +536,148 @@
function knbu_get_legends() {
global $knbu_kbsets;
- foreach($knbu_kbsets[knbu_get_kbset_for_post(get_the_ID())]->KnowledgeTypeSet->KnowledgeType as $type) {
- $color = $type['Colour'];
- echo '<li><span class="color" style="background-color: '.$color.'"></span>'.$type['Name'].'</li>';
+ $kbset = knbu_get_kbset_for_post(get_the_ID());
+ if ( ! $kbset ) return;
+ foreach($knbu_kbsets[$kbset]->KnowledgeTypeSet->KnowledgeType as $type) {
+ echo '<li><span class="color" style="background-color: ' . esc_attr( $type['Colour'] ) . '"></span>' . esc_html( $type['Name'] ) . '</li>';
}
}
-function knbu_init_mapview() {
- //To do: Check category and post (?)
- define('KNBU_MAPVIEW', isset($_GET['map-view']));
+/**
+ * Whether the current request should show the map view.
+ *
+ * Only for a single post or page that the visitor can see, that uses a
+ * knowledge type set and that isn't password protected. Must be called after
+ * the main query has run.
+ *
+ * @return bool
+ */
+function knbu_is_map_view() {
+ if ( ! isset( $_GET['map-view'] ) || ! is_singular() ) return false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display mode flag only.
+ $post_id = get_queried_object_id();
+ return $post_id && knbu_get_kbset_for_post( $post_id ) && ! post_password_required( $post_id );
}
function knbu_enqueue_scripts() {
- if(KNBU_MAPVIEW) {
- wp_register_script('knbu-map-script', plugins_url().'/knowledge-building/knbu-map-script.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-slider' ));
- wp_register_script('jquery-mousewheel', plugins_url().'/knowledge-building/jquery.mousewheel.js', array( 'jquery' ));
- wp_register_script('raphael-js', plugins_url().'/knowledge-building/raphael-min.js');
+ if ( knbu_is_map_view() ) {
+ $ver = $GLOBALS['knbu_plugin_version'];
+ wp_register_script('knbu-map-script', plugins_url( 'knbu-map-script.js', __FILE__ ), array( 'jquery', 'jquery-ui-core', 'jquery-ui-slider' ), $ver );
+ wp_register_script('jquery-mousewheel', plugins_url( 'jquery.mousewheel.js', __FILE__ ), array( 'jquery' ), '3.0.6' );
+ wp_register_script('raphael-js', plugins_url( 'raphael-min.js', __FILE__ ), array(), '2.1.0' );
wp_enqueue_script(array( 'raphael-js', 'knbu-map-script', 'jquery-mousewheel' ));
}
}
+/**
+ * Send an error message to the reply form and stop.
+ *
+ * Always uses HTTP 200 so the existing client code receives the message.
+ *
+ * @param string $message Plain-text message shown to the user.
+ */
+function knbu_reply_error( $message ) {
+ wp_send_json( array( 'Message' => $message ) );
+}
+
+/**
+ * AJAX handler for replies posted from the comment map.
+ *
+ * Replies go through wp_handle_comment_submission(), so they get the same
+ * checks, filtering and moderation as comments posted through the regular
+ * comment form. The response only contains values read back from the saved
+ * comment, never raw request values.
+ */
function knbu_new_reply_ajax() {
-
- $time = current_time('mysql');
- $var = new StdClass();
-
- if(empty($_POST['comment_content'])) {
- $var->Message = 'Content field cannot be empty';
- echo json_encode($var);
- die();
- }
-
- if(get_option('knbu_main_title_required') && empty($_POST['comment_title'])) {
- $var->Message = 'Main idea field cannot be empty';
- echo json_encode($var);
- die();
- }
- if(empty($_POST['comment_knbu_type']) || $_POST['comment_knbu_type'] == 'Select type') {
- $var->Message = 'Please select Knowledge type.';
- echo json_encode($var);
- die();
- }
- if(!is_numeric($_POST['comment_post_ID'])) {
- $var->Message = 'There was an error.';
- echo json_encode($var);
- die();
- }
-
- if(!$_POST['comment_user']) {
- if(strlen($_POST['comment_user_email']) == 0) {
- $var->Message = 'User email field cannot be empty';
- echo json_encode($var);
- die();
+ if ( ! check_ajax_referer( 'knbu_new_reply', 'nonce', false ) ) {
+ knbu_reply_error( 'Your session has expired. Please reload the page and try again.' );
+ }
+
+ $post_id = isset( $_POST['comment_post_ID'] ) ? absint( $_POST['comment_post_ID'] ) : 0;
+ $parent = isset( $_POST['comment_parent'] ) ? absint( $_POST['comment_parent'] ) : 0;
+ $content = isset( $_POST['comment_content'] ) ? trim( wp_unslash( $_POST['comment_content'] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- filtered by wp_handle_comment_submission() like any comment.
+ $title = isset( $_POST['comment_title'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_title'] ) ) : '';
+ $type_id = isset( $_POST['comment_knbu_type'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_knbu_type'] ) ) : '';
+ $name = isset( $_POST['comment_user_name'] ) ? sanitize_text_field( wp_unslash( $_POST['comment_user_name'] ) ) : '';
+ $email = isset( $_POST['comment_user_email'] ) ? sanitize_email( wp_unslash( $_POST['comment_user_email'] ) ) : '';
+
+ if ( ! $post_id || ! knbu_get_kbset_for_post( $post_id ) ) {
+ knbu_reply_error( 'There was an error.' );
+ }
+ if ( '' === $content ) {
+ knbu_reply_error( 'Content field cannot be empty' );
+ }
+ if ( get_option( 'knbu_main_title_required' ) && '' === $title ) {
+ knbu_reply_error( 'Main idea field cannot be empty' );
+ }
+ $ktype = ( '' !== $type_id ) ? knbu_get_type( $type_id, $post_id ) : false;
+ if ( ! $ktype ) {
+ knbu_reply_error( 'Please select Knowledge type.' );
+ }
+ if ( ! is_user_logged_in() ) {
+ if ( '' === $email ) {
+ knbu_reply_error( 'User email field cannot be empty' );
}
-
- if(strlen($_POST['comment_user_name']) == 0) {
- $var->Message = 'User name field cannot be empty';
- echo json_encode($var);
- die();
+ if ( '' === $name ) {
+ knbu_reply_error( 'User name field cannot be empty' );
}
}
- $data = array(
- 'comment_post_ID' => $_POST['comment_post_ID'],
- 'comment_content' => $_POST['comment_content'],
- 'comment_parent' => $_POST['comment_parent'],
- 'comment_date' => $time,
- 'comment_approved' => 1
- );
-
- if($_POST['comment_user']) {
- $i = wp_get_current_user();
- $var->user_id = $i->ID;
- $var->username = get_the_author_meta('display_name', $var->user_id);
- $var->user_email = get_the_author_meta('user_email', $var->user_id);
- $data['user_id'] = $var->user_id;
- }
- else {
- $var->user_email = $_POST['comment_user_email'];
- $var->username = $_POST['comment_user_name'];
-
- $data['comment_author_email'] = $var->user_email;
- }
-
- // Get map index
- // Basically just the node number
- $map_index = get_post_meta( $data['comment_post_ID'], 'knbu_map_index', true);
- $var->index = $map_index;
- update_post_meta( $data['comment_post_ID'], 'knbu_map_index', $map_index + 1 );
-
-
- $data['comment_author'] = $var->username;
- $id = wp_insert_comment($data);
- $var->content = $_POST['comment_content'];
- $var->comment_title = knbu_generate_title($_POST['comment_title'], $var->content);
-
- if($id) {
- $var->Success = true;
- $ktype = $_POST['comment_knbu_type'];
- $var->knbu = $ktype;
-
- update_comment_meta( $id, 'kbtype', $ktype );
- update_comment_meta( $id, 'knbu_map_comment_index', $var->index );
- update_comment_meta( $id, 'comment_title', $var->comment_title );
- }
- $var->avatar = knbu_get_avatar_url($var->user_email);
- $var->parent = $_POST['comment_parent'];
- $var->id = $id;
- $ktype = knbu_get_type( $_POST['comment_knbu_type'], $_POST['comment_post_ID'] );
- $var->color = (string)$ktype['Colour'];
-
- $var->date = get_comment_date(get_option('date_format'),$id);
- $var->timestamp = strtotime(get_comment_date('Y-m-d H:i:s', $id));
- echo json_encode($var);
- die();
+
+ // Checks that comments are open, required fields, flooding and duplicates,
+ // filters the content and applies moderation, then calls wp_new_comment().
+ // For logged-in users the author name and email come from their account.
+ $comment = wp_handle_comment_submission( array(
+ 'comment_post_ID' => $post_id,
+ 'comment_parent' => $parent,
+ 'comment' => $content,
+ 'author' => $name,
+ 'email' => $email,
+ 'url' => '',
+ ) );
+
+ if ( is_wp_error( $comment ) ) {
+ knbu_reply_error( wp_strip_all_tags( $comment->get_error_message() ) );
+ }
+
+ $id = (int) $comment->comment_ID;
+
+ // Map index: basically just the node number.
+ $map_index = get_post_meta( $post_id, 'knbu_map_index', true );
+ update_post_meta( $post_id, 'knbu_map_index', (int) $map_index + 1 );
+
+ $comment_title = knbu_generate_title( $title, wp_strip_all_tags( $comment->comment_content ) );
+ update_comment_meta( $id, 'kbtype', (string) $ktype['ID'] );
+ update_comment_meta( $id, 'knbu_map_comment_index', $map_index );
+ update_comment_meta( $id, 'comment_title', $comment_title );
+
+ if ( '1' !== (string) $comment->comment_approved ) {
+ wp_send_json( array(
+ 'Pending' => true,
+ 'Message' => 'Thank you. Your comment is awaiting moderation.',
+ ) );
+ }
+
+ wp_send_json( array(
+ 'Success' => true,
+ 'id' => $id,
+ 'parent' => (int) $comment->comment_parent,
+ 'index' => $map_index,
+ 'knbu' => (string) $ktype['ID'],
+ 'color' => (string) $ktype['Colour'],
+ 'content' => wp_kses_post( $comment->comment_content ),
+ 'comment_title' => $comment_title,
+ 'username' => $comment->comment_author,
+ 'avatar' => knbu_get_avatar_url( $comment->comment_author_email ),
+ 'date' => get_comment_date( get_option( 'date_format' ), $comment ),
+ 'timestamp' => strtotime( $comment->comment_date ),
+ ) );
}
function knbu_map_view_template($template) {
- if(isset($_GET['map-view'])) {
+ if ( knbu_is_map_view() ) {
return plugin_dir_path(__FILE__).'map.php';
}
return $template;
@@ -738,16 +685,35 @@
/* Get gravatar url of a user */
function knbu_get_avatar_url($email) {
- $a = md5( strtolower( trim( $email ) ) );
- return 'http://www.gravatar.com/avatar/'.$a.'?s=92&d=mm';
+ // Respects the site's avatar settings and filters, and uses https.
+ return (string) get_avatar_url( (string) $email, array( 'size' => 92, 'default' => 'mm' ) );
}
+/**
+ * AJAX handler that saves where a node has been pinned on the map.
+ *
+ * The positions are shared by everyone who views the map, so only users who
+ * can moderate comments may change them.
+ */
function knbu_save_node_position() {
- if(is_numeric($_POST['id']))
- update_comment_meta( $_POST['id'], 'node_position', $_POST['node_position'] );
- die();
+ if ( ! check_ajax_referer( 'knbu_save_node_position', 'nonce', false ) ) {
+ wp_send_json_error( null, 403 );
+ }
+ $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0;
+ if ( ! $id || ! get_comment( $id ) || ! current_user_can( 'edit_comment', $id ) ) {
+ wp_send_json_error( null, 403 );
+ }
+
+ $position = isset( $_POST['node_position'] ) ? json_decode( wp_unslash( $_POST['node_position'] ), true ) : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- decoded and validated below.
+ if ( is_array( $position ) && isset( $position['X'], $position['Y'] ) && is_numeric( $position['X'] ) && is_numeric( $position['Y'] ) ) {
+ update_comment_meta( $id, 'node_position', wp_json_encode( array( 'X' => (float) $position['X'], 'Y' => (float) $position['Y'] ) ) );
+ } else {
+ // Anything else, including "false", unpins the node.
+ delete_comment_meta( $id, 'node_position' );
+ }
+ wp_send_json_success();
}
@@ -759,16 +725,22 @@
global $wpdb;
$table_name = $wpdb->prefix . 'knowledgetypes';
- $rows = $wpdb->get_results("SELECT * FROM $table_name");
+ $rows = $wpdb->get_results("SELECT * FROM $table_name"); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name built from $wpdb->prefix.
$success = true;
foreach($rows as $row) {
- if(!update_comment_meta($row->comment_id, 'kbtype', $row->kbtype))
+ update_comment_meta($row->comment_id, 'kbtype', $row->kbtype);
+ // update_comment_meta() returns false when the value is already
+ // there, so check the stored value instead of the return value.
+ if ( (string) get_comment_meta( $row->comment_id, 'kbtype', true ) !== (string) $row->kbtype )
$success = false;
}
- if(!$success)
- wp_die('Something went wrong. Original data has not been deleted.');
+ // Keep the original table if anything failed; the migration is retried
+ // on a later admin page load.
+ if ( ! $success )
+ return false;
- $wpdb->query("RENAME TABLE ".$table_name." TO ".$table_name."_backup");
+ $wpdb->query("RENAME TABLE ".$table_name." TO ".$table_name."_backup"); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- table name built from $wpdb->prefix.
+ return true;
}
// Comment layout
@@ -796,22 +768,22 @@
}
?>
- <div style="background-color: <?php echo $args['color']; ?>" <?php if($id) { comment_class('knbu-comment', $id); } else { echo 'class="knbu-comment"'; } ?>
- data-id="<?php echo $id; ?>"
- data-stamp="<?php echo strtotime($args['date'].' '.$args['time']); ?>"
- data-date="<?php echo $args['date']; ?>"
- data-type="<?php echo $args['type_id']; ?>"
- data-comment-index="<?php echo $args['number']; ?>">
+ <div style="background-color: <?php echo esc_attr( $args['color'] ); ?>" <?php if($id) { comment_class('knbu-comment', $id); } else { echo 'class="knbu-comment"'; } ?>
+ data-id="<?php echo esc_attr( $id ); ?>"
+ data-stamp="<?php echo esc_attr( strtotime($args['date'].' '.$args['time']) ); ?>"
+ data-date="<?php echo esc_attr( $args['date'] ); ?>"
+ data-type="<?php echo esc_attr( $args['type_id'] ); ?>"
+ data-comment-index="<?php echo esc_attr( $args['number'] ); ?>">
<div class="message-header">
- <h4 class="message-type" data-type="<?php echo $args['type_id']; ?>"><?php echo $args['type']; ?></h4>
- <img class="message-avatar" src="<?php echo $args['avatar_src']; ?>">
- <span class="message-username"><?php echo $args['username']; ?></span>
+ <h4 class="message-type" data-type="<?php echo esc_attr( $args['type_id'] ); ?>"><?php echo esc_html( $args['type'] ); ?></h4>
+ <img class="message-avatar" src="<?php echo esc_url( $args['avatar_src'] ); ?>">
+ <span class="message-username"><?php echo esc_html( $args['username'] ); ?></span>
<br />
- <a class="message-link" onclick="location.reload()" href="<?php echo $args['link_href']; ?>">#<?php echo $args['index']; ?></a> <span class="message-title"><?php echo $args['title']; ?></span>
- <span class="message-date"><a class="message-link" onclick="location.reload()" href="<?php echo $args['link_href']; ?>"><?php echo $args['date'].' '.$args['time']; ?></a></span>
+ <a class="message-link" onclick="location.reload()" href="<?php echo esc_attr( $args['link_href'] ); ?>">#<?php echo esc_html( $args['index'] ); ?></a> <span class="message-title"><?php echo esc_html( $args['title'] ); ?></span>
+ <span class="message-date"><a class="message-link" onclick="location.reload()" href="<?php echo esc_attr( $args['link_href'] ); ?>"><?php echo esc_html( $args['date'].' '.$args['time'] ); ?></a></span>
</div>
<div class="message-content-wrapper">
- <div class="message-content"><?php echo $args['content']; ?></div>
+ <div class="message-content"><?php echo wp_kses_post( $args['content'] ); ?></div>
<div style="clear:both"></div>
<a class="reply-toggle knbu-form-link comment-reply-link" id="open-reply">Reply</a>
<div id="reply-wrapper">
--- a/knowledge-building/map.php
+++ b/knowledge-building/map.php
@@ -1,155 +1,160 @@
-<?php
-$replies = get_comments(array(
- 'status' => 'approve',
- 'post_id' => get_the_ID(),
- 'orderby' => 'date'
- ));
-?>
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
- <title><?php the_title(); ?> (Map) | <?php bloginfo('name'); ?></title>
- <link href='http://fonts.googleapis.com/css?family=Junge' rel='stylesheet' type='text/css'>
- <?php wp_head(); ?>
-</head>
-<body class="knbu-map-view <?php echo isset($_GET['map-frame']) ? 'knbu-map-frame' : 'knbu-map-full'; ?>">
- <div id="map">
- <div id="raven"></div>
- <div id="fps"></div>
- <div id="grouping">
- Show notes: <select id="grouping-select">
- <option value="discussion">as discussion</option>
- <option value="knowledgetypes">by knowledge types</option>
- <option value="users">by authors</option>
- <option value="time">by time</option>
- </select>
- </div>
- <div id="navigation">
- <img src="<?php echo plugins_url('knowledge-building'); ?>/images/toggle-hide.png" width="15" height="15" id="navi-toggle-button">
- <div id="navi-toggle-container">
- <div id="pan">
- <img src="<?php echo plugins_url(); ?>/knowledge-building/images/navi-bg.png" id="navigation-background">
- <img src="<?php echo plugins_url(); ?>/knowledge-building/images/arrow-left.png" class="arrow" id="arrow-left">
- <img src="<?php echo plugins_url(); ?>/knowledge-building/images/arrow-up.png" class="arrow" id="arrow-up">
- <img src="<?php echo plugins_url(); ?>/knowledge-building/images/arrow-right.png" class="arrow" id="arrow-right">
- <img src="<?php echo plugins_url(); ?>/knowledge-building/images/arrow-down.png" class="arrow" id="arrow-down">
- <img src="<?php echo plugins_url(); ?>/knowledge-building/images/center.png" class="arrow" id="arrow-center">
- </div>
- +<div id="zoom"></div>-
- </div>
- </div>
- <div id="legend">
- <ul>
- <?php knbu_get_legends(); ?>
- </ul>
- </div>
- <?php if(isset($_GET['map-frame'])) { ?>
- <div id="full-screen">
- <a href="<?php echo remove_query_arg( 'map-frame' ); ?>" target="_blank">
- <img src="<?php echo plugins_url() ?>/knowledge-building/images/full-screen.png" id="full-screen">
- </a>
- </div>
- <?php } ?>
- </div>
-
- <div id="message"><?php knbu_comment(get_the_ID()); ?></div>
- <?php
- usort($replies, 'knbu_cmp');
- knbu_get_childs(get_the_ID(), $replies);
- ?>
- </body>
-</html>
-<?php
-
-
-function knbu_get_childs($id, $replies) {
- global $knowledgeTypes, $knbu_kbsets, $post;
-
- // The post itself will be the starting node
- $nodes = array(
- array(
- 'id' => 0,
- 'parent' => false,
- 'content' => $post->post_content,
- 'avatar' => knbu_get_avatar_url( $post->user_id ),
- 'username' => get_the_author_meta( 'display_name', $post->post_author ),
- 'email' => $post->user_email,
- 'date' => date(get_option('date_format'), strtotime($post->post_date)),
- 'time' => date(get_option('time_format'), strtotime($post->post_date)),
- 'timestamp' => strtotime( $post->post_date ),
- 'typeName' => 'Start',
- 'title' => $post->post_title,
- 'static' => true,
- 'index' => 1
- )
- );
-
-
- $map_index = get_post_meta( $id, 'knbu_map_index', true);
-
- if(empty($map_index))
- $map_index = 2;
-
- foreach($replies as $reply) {
-
- // Get comment meta data
- // Should be combined to one variable(?)
- $type = get_comment_meta($reply->comment_ID, 'kbtype', true);
- $title = get_comment_meta($reply->comment_ID, 'comment_title', true);
- $node_index = get_comment_meta( $reply->comment_ID, 'knbu_map_comment_index' );
- $anchor = json_decode(get_comment_meta($reply->comment_ID, 'node_position', true));
-
- // Default values
- $name = 'Unspecified';
- $color = '#fff';
-
- // Get type name and color
- foreach($knbu_kbsets[knbu_get_kbset_for_post(get_the_ID())]->KnowledgeTypeSet->KnowledgeType as $t) {
- if($t['ID'] == $type) {
- $name = (string)$t['Name'];
- $color = (string)$t['Colour'];
- }
- }
-
- // If there's no title, get some text from content to be one
- $title = knbu_generate_title($title, $reply->comment_content);
-
-
- // Check if there's no index set for the note
- if(empty($node_index)) {
- $node_index = $map_index;
- $map_index++;
- update_comment_meta( $reply->comment_ID, 'knbu_map_comment_index', $node_index );
- update_post_meta( $id, 'knbu_map_index', $map_index);
- }
-
- $reply->user_email = isset($reply->user_email) ? $reply->user_email : '';
-
- $nodes[] = array(
- 'id' => $reply->comment_ID,
- 'parent' => $reply->comment_parent,
- 'content' => $reply->comment_content,
- 'avatar' => knbu_get_avatar_url($reply->user_id),
- 'username' => $reply->comment_author,
- 'email' => $reply->user_email,
- 'date' => date(get_option('date_format').' '.get_option('time_format'), strtotime($reply->comment_date)),
- 'timestamp' => strtotime($reply->comment_date),
- 'title' => $title,
- 'typeName' => $name,
- 'anchor' => $anchor,
- 'color' => $color,
- 'index' => $node_index
- );
- }
-
- echo '<script type="text/javascript">var NodesFromServer = '.json_encode($nodes).'; var MapIndexFromServer = '.$map_index.'; var plugin_url = "'.plugins_url('knowledge-building').'";</script>';
-}
-
-
-function knbu_cmp($a, $b) {
- if(strtotime($a->comment_date) == strtotime($b->comment_date))
- return 0;
- return strtotime($a->comment_date) > strtotime($b->comment_date) ? 1 : -1;
-}
+<?php
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+$replies = get_comments(array(
+ 'status' => 'approve',
+ 'post_id' => get_the_ID(),
+ 'orderby' => 'date'
+ ));
+?>
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
+ <title><?php the_title(); ?> (Map) | <?php bloginfo('name'); ?></title>
+ <link href='https://fonts.googleapis.com/css?family=Junge' rel='stylesheet' type='text/css'>
+ <?php wp_head(); ?>
+</head>
+<?php // phpcs:disable WordPress.Security.NonceVerification.Recommended -- map-frame is a display mode flag only. ?>
+<body class="knbu-map-view <?php echo isset($_GET['map-frame']) ? 'knbu-map-frame' : 'knbu-map-full'; ?>">
+ <div id="map">
+ <div id="raven"></div>
+ <div id="fps"></div>
+ <div id="grouping">
+ Show notes: <select id="grouping-select">
+ <option value="discussion">as discussion</option>
+ <option value="knowledgetypes">by knowledge types</option>
+ <option value="users">by authors</option>
+ <option value="time">by time</option>
+ </select>
+ </div>
+ <div id="navigation">
+ <img src="<?php echo esc_url( plugins_url( 'images/toggle-hide.png', __FILE__ ) ); ?>" width="15" height="15" id="navi-toggle-button">
+ <div id="navi-toggle-container">
+ <div id="pan">
+ <img src="<?php echo esc_url( plugins_url( 'images/navi-bg.png', __FILE__ ) ); ?>" id="navigation-background">
+ <img src="<?php echo esc_url( plugins_url( 'images/arrow-left.png', __FILE__ ) ); ?>" class="arrow" id="arrow-left">
+ <img src="<?php echo esc_url( plugins_url( 'images/arrow-up.png', __FILE__ ) ); ?>" class="arrow" id="arrow-up">
+ <img src="<?php echo esc_url( plugins_url( 'images/arrow-right.png', __FILE__ ) ); ?>" class="arrow" id="arrow-right">
+ <img src="<?php echo esc_url( plugins_url( 'images/arrow-down.png', __FILE__ ) ); ?>" class="arrow" id="arrow-down">
+ <img src="<?php echo esc_url( plugins_url( 'images/center.png', __FILE__ ) ); ?>" class="arrow" id="arrow-center">
+ </div>
+ +<div id="zoom"></div>-
+ </div>
+ </div>
+ <div id="legend">
+ <ul>
+ <?php knbu_get_legends(); ?>
+ </ul>
+ </div>
+ <?php if(isset($_GET['map-frame'])) { ?>
+ <div id="full-screen">
+ <a href="<?php echo esc_url( remove_query_arg( 'map-frame' ) ); ?>" target="_blank">
+ <img src="<?php echo esc_url( plugins_url( 'images/full-screen.png', __FILE__ ) ); ?>" id="full-screen">
+ </a>
+ </div>
+ <?php } ?>
+ </div>
+
+ <?php // phpcs:enable WordPress.Security.NonceVerification.Recommended ?>
+ <div id="message"><?php knbu_comment(get_the_ID()); ?></div>
+ <?php if ( current_user_can( 'moderate_comments' ) ) { ?>
+ <input type="hidden" id="knbu-node-nonce" value="<?php echo esc_attr( wp_create_nonce( 'knbu_save_node_position' ) ); ?>">
+ <?php } ?>
+ <?php
+ usort($replies, 'knbu_cmp');
+ knbu_get_childs(get_the_ID(), $replies);
+ ?>
+ </body>
+</html>
+<?php
+
+
+function knbu_get_childs($id, $replies) {
+ global $knowledgeTypes, $knbu_kbsets, $post;
+
+ // The post itself will be the starting node
+ $nodes = array(
+ array(
+ 'id' => 0,
+ 'parent' => false,
+ 'content' => $post->post_content,
+ 'avatar' => knbu_get_avatar_url( get_the_author_meta( 'user_email', $post->post_author ) ),
+ 'username' => get_the_author_meta( 'display_name', $post->post_author ),
+ 'date' => date(get_option('date_format'), strtotime($post->post_date)),
+ 'time' => date(get_option('time_format'), strtotime($post->post_date)),
+ 'timestamp' => strtotime( $post->post_date ),
+ 'typeName' => 'Start',
+ 'title' => $post->post_title,
+ 'static' => true,
+ 'index' => 1
+ )
+ );
+
+
+ $map_index = get_post_meta( $id, 'knbu_map_index', true);
+
+ if(empty($map_index))
+ $map_index = 2;
+
+ foreach($replies as $reply) {
+
+ // Get comment meta data
+ // Should be combined to one variable(?)
+ $type = get_comment_meta($reply->comment_ID, 'kbtype', true);
+ $title = get_comment_meta($reply->comment_ID, 'comment_title', true);
+ $node_index = (int) get_comment_meta( $reply->comment_ID, 'knbu_map_comment_index', true );
+ $anchor = json_decode(get_comment_meta($reply->comment_ID, 'node_position', true));
+
+ // Default values
+ $name = 'Unspecified';
+ $color = '#fff';
+
+ // Get type name and color
+ foreach($knbu_kbsets[knbu_get_kbset_for_post(get_the_ID())]->KnowledgeTypeSet->KnowledgeType as $t) {
+ if($t['ID'] == $type) {
+ $name = (string)$t['Name'];
+ $color = (string)$t['Colour'];
+ }
+ }
+
+ // If there's no title, get some text from content to be one
+ $title = knbu_generate_title($title, wp_strip_all_tags( $reply->comment_content ));
+
+
+ // Check if there's no index set for the note
+ if(empty($node_index)) {
+ $node_index = $map_index;
+ $map_index++;
+ update_comment_meta( $reply->comment_ID, 'knbu_map_comment_index', $node_index );
+ update_post_meta( $id, 'knbu_map_index', $map_index);
+ }
+
+ $nodes[] = array(
+ 'id' => $reply->comment_ID,
+ 'parent' => $reply->comment_parent,
+ 'content' => wp_kses_post( $reply->comment_content ),
+ 'avatar' => knbu_get_avatar_url( $reply->comment_author_email ),
+ 'username' => $reply->comment_author,
+ 'date' => date(get_option('date_format').' '.get_option('time_format'), strtotime($reply->comment_date)),
+ 'timestamp' => strtotime($reply->comment_date),
+ 'title' => $title,
+ 'typeName' => $name,
+ 'anchor' => $anchor,
+ 'color' => $color,
+ 'index' => $node_index
+ );
+ }
+
+ echo '<script type="text/javascript">var NodesFromServer = ' . wp_json_encode( $nodes ) . '; var MapIndexFromServer = ' . (int) $map_index . '; var plugin_url = ' . wp_json_encode( plugins_url( '', __FILE__ ) ) . ';</script>';
+}
+
+
+function knbu_cmp($a, $b) {
+ if(strtotime($a->comment_date) == strtotime($b->comment_date))
+ return 0;
+ return strtotime($a->comment_date) > strtotime($b->comment_date) ? 1 : -1;
+}
?>
No newline at end of file