Disqus Comment System - Version 2.64

Version Description

Download this release

Release Info

Developer Disqus
Plugin Icon Disqus Comment System
Version 2.64
Comparing to
See all releases

Code changes from version 2.61 to 2.64

comments.php CHANGED
@@ -1,123 +1,121 @@
1
  <?php
2
  if (DISQUS_DEBUG) {
3
- echo "<p><strong>Disqus Debug</strong> thread_id: ".get_post_meta($post->ID, 'dsq_thread_id', true)."</p>";
4
  }
5
  ?>
6
 
7
  <div id="disqus_thread">
8
- <?php if (!get_option('disqus_disable_ssr')): ?>
9
- <?php
10
- // if (is_file(TEMPLATEPATH . '/comments.php')) {
11
- // include(TEMPLATEPATH . '/comments.php');
12
- // }
13
- ?>
14
- <div id="dsq-content">
15
- <ul id="dsq-comments">
16
- <?php foreach ($comments as $comment) : ?>
17
- <li id="dsq-comment-<?php echo comment_ID(); ?>">
18
- <div id="dsq-comment-header-<?php echo comment_ID(); ?>" class="dsq-comment-header">
19
- <cite id="dsq-cite-<?php echo comment_ID(); ?>">
20
- <?php if(comment_author_url()) : ?>
21
- <a id="dsq-author-user-<?php echo comment_ID(); ?>" href="<?php echo comment_author_url(); ?>" target="_blank" rel="nofollow"><?php echo comment_author(); ?></a>
22
- <?php else : ?>
23
- <span id="dsq-author-user-<?php echo comment_ID(); ?>"><?php echo comment_author(); ?></span>
24
- <?php endif; ?>
25
- </cite>
26
- </div>
27
- <div id="dsq-comment-body-<?php echo comment_ID(); ?>" class="dsq-comment-body">
28
- <div id="dsq-comment-message-<?php echo comment_ID(); ?>" class="dsq-comment-message"><?php echo wp_filter_kses(comment_text()); ?></div>
29
- </div>
30
- </li>
31
- <?php endforeach; ?>
32
- </ul>
33
- </div>
34
- <?php endif; ?>
35
  </div>
36
 
37
- <a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>
38
-
39
  <script type="text/javascript">
40
  /* <![CDATA[ */
41
- var disqus_url = '<?php echo get_permalink(); ?> ';
42
- var disqus_identifier = '<?php echo dsq_identifier_for_post($post); ?>';
43
- var disqus_container_id = 'disqus_thread';
44
- var disqus_domain = '<?php echo DISQUS_DOMAIN; ?>';
45
- var disqus_shortname = '<?php echo strtolower(get_option('disqus_forum_url')); ?>';
46
- var disqus_title = <?php echo cf_json_encode(dsq_title_for_post($post)); ?>;
47
- <?php if (false && get_option('disqus_developer')): ?>
48
- var disqus_developer = 1;
49
- <?php endif; ?>
50
- var disqus_config = function () {
51
- var config = this; // Access to the config object
52
 
53
- /*
54
- All currently supported events:
55
- * preData — fires just before we request for initial data
56
- * preInit - fires after we get initial data but before we load any dependencies
57
- * onInit - fires when all dependencies are resolved but before dtpl template is rendered
58
- * afterRender - fires when template is rendered but before we show it
59
- * onReady - everything is done
60
- */
61
 
62
- config.callbacks.preData.push(function() {
63
- // clear out the container (its filled for SEO/legacy purposes)
64
- document.getElementById(disqus_container_id).innerHTML = '';
65
- });
66
- <?php if (!get_option('disqus_manual_sync')): ?>
67
- config.callbacks.onReady.push(function() {
68
- // sync comments in the background so we don't block the page
69
- DISQUS.request.get('?cf_action=sync_comments&post_id=<?php echo $post->ID; ?>');
70
- });
71
- <?php endif; ?>
72
- <?php
73
- $sso = dsq_sso();
74
- if ($sso) {
75
- foreach ($sso as $k=>$v) {
76
- echo "this.page.{$k} = '{$v}';\n";
77
- }
78
- }
79
- ?>
80
- };
81
- var facebookXdReceiverPath = '<?php echo DSQ_PLUGIN_URL . '/xd_receiver.htm' ?>';
82
  /* ]]> */
83
  </script>
84
 
85
  <script type="text/javascript">
86
  /* <![CDATA[ */
87
- var DsqLocal = {
88
- 'trackbacks': [
89
  <?php
90
- $count = 0;
91
- foreach ($comments as $comment) {
92
- $comment_type = get_comment_type();
93
- if ( $comment_type != 'comment' ) {
94
- if( $count ) { echo ','; }
95
  ?>
96
- {
97
- 'author_name': <?php echo cf_json_encode(get_comment_author()); ?>,
98
- 'author_url': <?php echo cf_json_encode(get_comment_author_url()); ?>,
99
- 'date': <?php echo cf_json_encode(get_comment_date('m/d/Y h:i A')); ?>,
100
- 'excerpt': <?php echo cf_json_encode(str_replace(array("\r\n", "\n", "\r"), '<br />', get_comment_excerpt())); ?>,
101
- 'type': <?php echo cf_json_encode($comment_type); ?>
102
- }
103
  <?php
104
- $count++;
105
- }
106
- }
107
  ?>
108
- ],
109
- 'trackback_url': <?php echo cf_json_encode(get_trackback_url()); ?>
110
- };
111
  /* ]]> */
112
  </script>
113
 
114
  <script type="text/javascript">
115
  /* <![CDATA[ */
116
  (function() {
117
- var dsq = document.createElement('script'); dsq.type = 'text/javascript';
118
- dsq.async = true;
119
- dsq.src = 'http://' + disqus_shortname + '.' + disqus_domain + '/embed.js?pname=wordpress&pver=<?php echo DISQUS_VERSION; ?>';
120
- (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
121
  })();
122
  /* ]]> */
123
  </script>
1
  <?php
2
  if (DISQUS_DEBUG) {
3
+ echo "<p><strong>Disqus Debug</strong> thread_id: ".get_post_meta($post->ID, 'dsq_thread_id', true)."</p>";
4
  }
5
  ?>
6
 
7
  <div id="disqus_thread">
8
+ <?php if (!get_option('disqus_disable_ssr')): ?>
9
+ <?php
10
+ // if (is_file(TEMPLATEPATH . '/comments.php')) {
11
+ // include(TEMPLATEPATH . '/comments.php');
12
+ // }
13
+ ?>
14
+ <div id="dsq-content">
15
+ <ul id="dsq-comments">
16
+ <?php foreach ($comments as $comment) : ?>
17
+ <li id="dsq-comment-<?php echo comment_ID(); ?>">
18
+ <div id="dsq-comment-header-<?php echo comment_ID(); ?>" class="dsq-comment-header">
19
+ <cite id="dsq-cite-<?php echo comment_ID(); ?>">
20
+ <?php if(comment_author_url()) : ?>
21
+ <a id="dsq-author-user-<?php echo comment_ID(); ?>" href="<?php echo comment_author_url(); ?>" target="_blank" rel="nofollow"><?php echo comment_author(); ?></a>
22
+ <?php else : ?>
23
+ <span id="dsq-author-user-<?php echo comment_ID(); ?>"><?php echo comment_author(); ?></span>
24
+ <?php endif; ?>
25
+ </cite>
26
+ </div>
27
+ <div id="dsq-comment-body-<?php echo comment_ID(); ?>" class="dsq-comment-body">
28
+ <div id="dsq-comment-message-<?php echo comment_ID(); ?>" class="dsq-comment-message"><?php echo wp_filter_kses(comment_text()); ?></div>
29
+ </div>
30
+ </li>
31
+ <?php endforeach; ?>
32
+ </ul>
33
+ </div>
34
+ <?php endif; ?>
35
  </div>
36
 
 
 
37
  <script type="text/javascript">
38
  /* <![CDATA[ */
39
+ var disqus_url = '<?php echo get_permalink(); ?> ';
40
+ var disqus_identifier = '<?php echo dsq_identifier_for_post($post); ?>';
41
+ var disqus_container_id = 'disqus_thread';
42
+ var disqus_domain = '<?php echo DISQUS_DOMAIN; ?>';
43
+ var disqus_shortname = '<?php echo strtolower(get_option('disqus_forum_url')); ?>';
44
+ var disqus_title = <?php echo cf_json_encode(dsq_title_for_post($post)); ?>;
45
+ <?php if (false && get_option('disqus_developer')): ?>
46
+ var disqus_developer = 1;
47
+ <?php endif; ?>
48
+ var disqus_config = function () {
49
+ var config = this; // Access to the config object
50
 
51
+ /*
52
+ All currently supported events:
53
+ * preData — fires just before we request for initial data
54
+ * preInit - fires after we get initial data but before we load any dependencies
55
+ * onInit - fires when all dependencies are resolved but before dtpl template is rendered
56
+ * afterRender - fires when template is rendered but before we show it
57
+ * onReady - everything is done
58
+ */
59
 
60
+ config.callbacks.preData.push(function() {
61
+ // clear out the container (its filled for SEO/legacy purposes)
62
+ document.getElementById(disqus_container_id).innerHTML = '';
63
+ });
64
+ <?php if (!get_option('disqus_manual_sync')): ?>
65
+ config.callbacks.onReady.push(function() {
66
+ // sync comments in the background so we don't block the page
67
+ DISQUS.request.get('?cf_action=sync_comments&post_id=<?php echo $post->ID; ?>');
68
+ });
69
+ <?php endif; ?>
70
+ <?php
71
+ $sso = dsq_sso();
72
+ if ($sso) {
73
+ foreach ($sso as $k=>$v) {
74
+ echo "this.page.{$k} = '{$v}';\n";
75
+ }
76
+ }
77
+ ?>
78
+ };
79
+ var facebookXdReceiverPath = '<?php echo DSQ_PLUGIN_URL . '/xd_receiver.htm' ?>';
80
  /* ]]> */
81
  </script>
82
 
83
  <script type="text/javascript">
84
  /* <![CDATA[ */
85
+ var DsqLocal = {
86
+ 'trackbacks': [
87
  <?php
88
+ $count = 0;
89
+ foreach ($comments as $comment) {
90
+ $comment_type = get_comment_type();
91
+ if ( $comment_type != 'comment' ) {
92
+ if( $count ) { echo ','; }
93
  ?>
94
+ {
95
+ 'author_name': <?php echo cf_json_encode(get_comment_author()); ?>,
96
+ 'author_url': <?php echo cf_json_encode(get_comment_author_url()); ?>,
97
+ 'date': <?php echo cf_json_encode(get_comment_date('m/d/Y h:i A')); ?>,
98
+ 'excerpt': <?php echo cf_json_encode(str_replace(array("\r\n", "\n", "\r"), '<br />', get_comment_excerpt())); ?>,
99
+ 'type': <?php echo cf_json_encode($comment_type); ?>
100
+ }
101
  <?php
102
+ $count++;
103
+ }
104
+ }
105
  ?>
106
+ ],
107
+ 'trackback_url': <?php echo cf_json_encode(get_trackback_url()); ?>
108
+ };
109
  /* ]]> */
110
  </script>
111
 
112
  <script type="text/javascript">
113
  /* <![CDATA[ */
114
  (function() {
115
+ var dsq = document.createElement('script'); dsq.type = 'text/javascript';
116
+ dsq.async = true;
117
+ dsq.src = 'http://' + disqus_shortname + '.' + disqus_domain + '/embed.js?pname=wordpress&pver=<?php echo DISQUS_VERSION; ?>';
118
+ (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
119
  })();
120
  /* ]]> */
121
  </script>
disqus.php CHANGED
@@ -4,68 +4,86 @@ Plugin Name: Disqus Comment System
4
  Plugin URI: http://disqus.com/
5
  Description: The Disqus comment system replaces your WordPress comment system with your comments hosted and powered by Disqus. Head over to the Comments admin page to set up your DISQUS Comment System.
6
  Author: Disqus <team@disqus.com>
7
- Version: 2.61
8
  Author URI: http://disqus.com/
9
  */
10
 
11
- require_once(dirname(__FILE__) . '/lib/wpapi.php');
12
-
13
- define('DISQUS_URL', 'http://disqus.com/');
14
- define('DISQUS_API_URL', DISQUS_URL . 'api/');
15
- define('DISQUS_DOMAIN', 'disqus.com');
16
- define('DISQUS_IMPORTER_URL', 'http://import.disqus.com/');
17
- define('DISQUS_MEDIA_URL', 'http://disqus.com/media/');
18
- define('DISQUS_RSS_PATH', '/latest.rss');
19
- define('DISQUS_CAN_EXPORT', is_file(dirname(__FILE__) . '/export.php'));
 
 
 
 
 
 
 
 
 
 
 
20
  if (!defined('DISQUS_DEBUG')) {
21
- define('DISQUS_DEBUG', false);
22
  }
23
- define('DISQUS_VERSION', '2.61');
24
 
25
  /**
26
  * Returns an array of all option identifiers used by DISQUS.
 
27
  */
28
  function dsq_options() {
29
- return array(
30
- 'disqus_public_key',
31
- 'disqus_secret_key',
32
- 'disqus_forum_url',
33
- 'disqus_api_key',
34
- 'disqus_user_api_key',
35
- 'disqus_partner_key',
36
- 'disqus_replace',
37
- 'disqus_cc_fix',
38
- # disables automatic sync via cron
39
- 'disqus_manual_sync',
40
- # disables server side rendering
41
- 'disqus_disable_ssr',
42
- # the last sync comment id (from get_forum_posts)
43
- 'disqus_last_comment_id',
44
- 'disqus_version',
45
- );
 
 
46
  }
47
 
 
 
 
 
48
  function dsq_plugin_basename($file) {
49
- $file = dirname($file);
50
 
51
- // From WP2.5 wp-includes/plugin.php:plugin_basename()
52
- $file = str_replace('\\','/',$file); // sanitize for Win32 installs
53
- $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
54
- $file = preg_replace('|^.*/' . PLUGINDIR . '/|','',$file); // get relative path from plugins dir
55
 
56
- if ( strstr($file, '/') === false ) {
57
- return $file;
58
- }
59
 
60
- $pieces = explode('/', $file);
61
- return !empty($pieces[count($pieces)-1]) ? $pieces[count($pieces)-1] : $pieces[count($pieces)-2];
62
  }
63
 
64
  if ( !defined('WP_CONTENT_URL') ) {
65
- define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
66
  }
67
  if ( !defined('PLUGINDIR') ) {
68
- define('PLUGINDIR', 'wp-content/plugins'); // Relative to ABSPATH. For back compat.
69
  }
70
 
71
  define('DSQ_PLUGIN_URL', WP_CONTENT_URL . '/plugins/' . dsq_plugin_basename(__FILE__));
@@ -74,31 +92,31 @@ $mt_disqus_version = '2.01';
74
  /**
75
  * Response from Disqus get_thread API call for comments template.
76
  *
77
- * @global string $dsq_response
78
- * @since 1.0
79
  */
80
  $dsq_response = '';
81
  /**
82
  * Disqus API instance.
83
  *
84
- * @global string $dsq_api
85
- * @since 1.0
86
  */
87
  $dsq_api = new DisqusWordPressAPI(get_option('disqus_forum_url'), get_option('disqus_api_key'));
88
 
89
  /**
90
  * DISQUS currently unsupported dev toggle to output comments for this query.
91
  *
92
- * @global bool $dsq_comments_for_query
93
- * @since ?
94
  */
95
  $DSQ_QUERY_COMMENTS = false;
96
 
97
  /**
98
  * DISQUS array to store post_ids from WP_Query for comment JS output.
99
  *
100
- * @global array $DSQ_QUERY_POST_IDS
101
- * @since 2.2
102
  */
103
  $DSQ_QUERY_POST_IDS = array();
104
 
@@ -108,417 +126,445 @@ $DSQ_QUERY_POST_IDS = array();
108
 
109
  /**
110
  * Tests if required options are configured to display the Disqus embed.
 
111
  */
112
  function dsq_is_installed() {
113
- return get_option('disqus_forum_url') && get_option('disqus_api_key');
114
  }
115
 
 
 
 
116
  function dsq_can_replace() {
117
- global $id, $post;
118
- $replace = get_option('disqus_replace');
 
 
 
119
 
120
  if ( is_feed() ) { return false; }
121
- if ( 'draft' == $post->post_status ) { return false; }
122
- if ( !get_option('disqus_forum_url') ) { return false; }
123
- else if ( 'all' == $replace ) { return true; }
124
-
125
- if ( !isset($post->comment_count) ) {
126
- $num_comments = 0;
127
- } else {
128
- if ( 'empty' == $replace ) {
129
- // Only get count of comments, not including pings.
130
-
131
- // If there are comments, make sure there are comments (that are not track/pingbacks)
132
- if ( $post->comment_count > 0 ) {
133
- // Yuck, this causes a DB query for each post. This can be
134
- // replaced with a lighter query, but this is still not optimal.
135
- $comments = get_approved_comments($post->ID);
136
- foreach ( $comments as $comment ) {
137
- if ( $comment->comment_type != 'trackback' && $comment->comment_type != 'pingback' ) {
138
- $num_comments++;
139
- }
140
- }
141
- } else {
142
- $num_comments = 0;
143
- }
144
- }
145
- else {
146
- $num_comments = $post->comment_count;
147
- }
148
- }
149
-
150
- return ( ('empty' == $replace && 0 == $num_comments)
151
- || ('closed' == $replace && 'closed' == $post->comment_status) );
152
  }
153
 
154
  function dsq_manage_dialog($message, $error = false) {
155
- global $wp_version;
156
-
157
- echo '<div '
158
- . ( $error ? 'id="disqus_warning" ' : '')
159
- . 'class="updated fade'
160
- . ( (version_compare($wp_version, '2.5', '<') && $error) ? '-ff0000' : '' )
161
- . '"><p><strong>'
162
- . $message
163
- . '</strong></p></div>';
164
- }
165
-
166
- function dsq_sync_comments($comments) {
167
- global $wpdb;
168
-
169
- // user MUST be logged out during this process
170
- wp_set_current_user(0);
171
-
172
- // we need the thread_ids so we can map them to posts
173
- $thread_map = array();
174
- foreach ( $comments as $comment ) {
175
- $thread_map[$comment->thread->id] = null;
176
- }
177
- $thread_ids = "'" . implode("', '", array_keys($thread_map)) . "'";
178
-
179
- $results = $wpdb->get_results( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = 'dsq_thread_id' AND meta_value IN ({$thread_ids}) LIMIT 1");
180
- foreach ( $results as $result ) {
181
- $thread_map[$result->meta_value] = $result->post_id;
182
- }
183
-
184
- foreach ( $comments as $comment ) {
185
- $ts = strtotime($comment->created_at);
186
- if (!$thread_map[$comment->thread->id] && !empty($comment->thread->identifier)) {
187
- // legacy threads dont already have their meta stored
188
- foreach ( $comment->thread->identifier as $identifier ) {
189
- // we know identifier starts with post_ID
190
- if ($post_ID = (int)substr($identifier, 0, strpos($identifier, ' '))) {
191
- $thread_map[$comment->thread->id] = $post_ID;
192
- update_post_meta($post_ID, 'dsq_thread_id', $comment->thread->id);
193
- }
194
- }
195
- }
196
- if (!$thread_map[$comment->thread->id]) {
197
- // shouldn't ever happen, but we can't be certain
198
- if (DISQUS_DEBUG) {
199
- echo "skipped {$comment->id}: missing thread for identifier ({$comment->thread->identifier})\n";
200
- }
201
- continue;
202
- }
203
- if ($wpdb->get_row($wpdb->prepare( "SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = 'dsq_post_id' AND meta_value = %s LIMIT 1", $comment->id))) {
204
- // already exists
205
- if (DISQUS_DEBUG) {
206
- echo "skipped {$comment->id}: comment already exists\n";
207
- }
208
- continue;
209
- }
210
-
211
- $commentdata = false;
212
-
213
- // first lets check by the id we have stored
214
- if ($comment->meta) {
215
- $meta = explode(';', $comment->meta);
216
- foreach ($meta as $value) {
217
- $value = explode('=', $value);
218
- $meta[$value[0]] = $value[1];
219
- }
220
- if ($meta['wp_id']) {
221
- $commentdata = $wpdb->get_row($wpdb->prepare( "SELECT comment_ID, comment_parent FROM $wpdb->comments WHERE comment_ID = %s LIMIT 1", $meta['wp_id']), ARRAY_A);
222
- }
223
- }
224
-
225
- // skip comments that were imported but are missing meta information
226
- if (!$commentdata && $comment->imported) {
227
- if (DISQUS_DEBUG) {
228
- echo "skipped {$comment->id}: comment not found and marked as imported\n";
229
- }
230
- continue;
231
- }
232
-
233
-
234
-
235
- // and follow up using legacy Disqus agent
236
- if (!$commentdata) {
237
- $commentdata = $wpdb->get_row($wpdb->prepare( "SELECT comment_ID, comment_parent FROM $wpdb->comments WHERE comment_agent LIKE 'Disqus/%%:{$comment->id}' LIMIT 1"), ARRAY_A);
238
- }
239
- if (!$commentdata) {
240
- // Comment doesnt exist yet, lets insert it
241
- if ($comment->status == 'approved') {
242
- $approved = 1;
243
- } elseif ($comment->status == 'spam') {
244
- $approved = 'spam';
245
- } else {
246
- $approved = 0;
247
- }
248
- $commentdata = array(
249
- 'comment_post_ID' => $thread_map[$comment->thread->id],
250
- 'comment_date' => $comment->created_at,
251
- 'comment_date_gmt' => $comment->created_at,
252
- 'comment_content' => apply_filters('pre_comment_content', $comment->message),
253
- 'comment_approved' => $approved,
254
- 'comment_agent' => 'Disqus/1.1('.DISQUS_VERSION.'):'.intval($comment->id),
255
- 'comment_type' => '',
256
- );
257
- if ($comment->is_anonymous) {
258
- $commentdata['comment_author'] = $comment->anonymous_author->name;
259
- $commentdata['comment_author_email'] = $comment->anonymous_author->email;
260
- $commentdata['comment_author_url'] = $comment->anonymous_author->url;
261
- $commentdata['comment_author_IP'] = $comment->anonymous_author->ip_address;
262
- } else {
263
- $commentdata['comment_author'] = $comment->author->display_name;
264
- $commentdata['comment_author_email'] = $comment->author->email;
265
- $commentdata['comment_author_url'] = $comment->author->url;
266
- $commentdata['comment_author_IP'] = $comment->author->ip_address;
267
- }
268
- $commentdata = wp_filter_comment($commentdata);
269
- if ($comment->parent_post) {
270
- $parent_id = $wpdb->get_var($wpdb->prepare( "SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = 'dsq_post_id' AND meta_value = %s LIMIT 1", $comment->parent_post));
271
- if ($parent_id) {
272
- $commentdata['comment_parent'] = $parent_id;
273
- }
274
- }
275
- $commentdata['comment_ID'] = wp_insert_comment($commentdata);
276
- if (DISQUS_DEBUG) {
277
- echo "inserted {$comment->id}: id is {$commentdata[comment_ID]}\n";
278
- }
279
- }
280
- if (!$commentdata['comment_parent'] && $comment->parent_post) {
281
- $parent_id = $wpdb->get_var($wpdb->prepare( "SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = 'dsq_post_id' AND meta_value = %s LIMIT 1", $comment->parent_post));
282
- if ($parent_id) {
283
- $wpdb->query($wpdb->prepare( "UPDATE $wpdb->comments SET comment_parent = %s WHERE comment_id = %s", $parent_id, $commentdata['comment_ID']));
284
- if (DISQUS_DEBUG) {
285
- echo "updated {$comment->id}: comment_parent changed to {$parent_id}\n";
286
- }
287
-
288
- }
289
- }
290
- $comment_id = $commentdata['comment_ID'];
291
- update_comment_meta($comment_id, 'dsq_parent_post_id', $comment->parent_post);
292
- update_comment_meta($comment_id, 'dsq_post_id', $comment->id);
293
- }
294
-
295
- if( isset($_POST['dsq_api_key']) && $_POST['dsq_api_key'] == get_option('disqus_api_key') ) {
296
- if( isset($_GET['dsq_sync_action']) && isset($_GET['dsq_sync_comment_id']) ) {
297
- $comment_parts = explode('=', $_GET['dsq_sync_comment_id']);
298
-
299
- if (!($comment_id = intval($comment_parts[1])) > 0) {
300
- return;
301
- }
302
-
303
- if( 'wp_id' != $comment_parts[0] ) {
304
- $comment_id = $wpdb->get_var($wpdb->prepare('SELECT comment_ID FROM ' . $wpdb->comments . ' WHERE comment_post_ID = %d AND comment_agent LIKE %s', intval($post->ID), 'Disqus/1.0:' . $comment_id));
305
- }
306
-
307
- switch( $_GET['dsq_sync_action'] ) {
308
- case 'mark_spam':
309
- wp_set_comment_status($comment_id, 'spam');
310
- echo "<!-- dsq_sync: wp_set_comment_status($comment_id, 'spam') -->";
311
- break;
312
- case 'mark_approved':
313
- wp_set_comment_status($comment_id, 'approve');
314
- echo "<!-- dsq_sync: wp_set_comment_status($comment_id, 'approve') -->";
315
- break;
316
- case 'mark_killed':
317
- wp_set_comment_status($comment_id, 'hold');
318
- echo "<!-- dsq_sync: wp_set_comment_status($comment_id, 'hold') -->";
319
- break;
320
- }
321
- }
322
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
  }
324
 
325
  function dsq_request_handler() {
326
- global $dsq_response;
327
- global $dsq_api;
328
- global $post;
329
- global $wpdb;
330
-
331
- if (!empty($_GET['cf_action'])) {
332
- switch ($_GET['cf_action']) {
333
- case 'sync_comments':
334
- if( !( $post_id = $_GET['post_id'] ) ) {
335
- header("HTTP/1.0 400 Bad Request");
336
- die();
337
- }
338
- // schedule the event for 30 seconds from now in case they
339
- // happen to make a quick post
340
- if (DISQUS_DEBUG) {
341
- dsq_sync_post($post_id);
342
- $response = dsq_sync_forum();
343
- if (!$response) {
344
- die('// error: '.$dsq_api->get_last_error());
345
- } else {
346
- list($last_comment_id, $comments) = $response;
347
- die('// synced '.$comments.' comments');
348
- }
349
- } else {
350
- wp_schedule_single_event(time(), 'dsq_sync_post', array($post_id));
351
- wp_schedule_single_event(time(), 'dsq_sync_forum');
352
- die('// sync scheduled');
353
- }
354
- break;
355
- case 'export_comments':
356
- if (current_user_can('manage_options') && DISQUS_CAN_EXPORT) {
357
- $timestamp = intval($_GET['timestamp']);
358
- $post_id = intval($_GET['post_id']);
359
- $limit = 2;
360
- global $wpdb, $dsq_api;
361
- $posts = $wpdb->get_results($wpdb->prepare("
362
- SELECT *
363
- FROM $wpdb->posts
364
- WHERE post_type != 'revision'
365
- AND post_status = 'publish'
366
- AND comment_count > 0
367
- AND ID > %d
368
- ORDER BY ID ASC
369
- LIMIT $limit
370
- ", $post_id));
371
- $first_post_id = $posts[0]->ID;
372
- $last_post_id = $posts[(count($posts) - 1)]->ID;
373
- $max_post_id = $wpdb->get_var($wpdb->prepare("
374
- SELECT MAX(ID)
375
- FROM $wpdb->posts
376
- WHERE post_type != 'revision'
377
- AND post_status = 'publish'
378
- AND comment_count > 0
379
- AND ID > %d
380
- ", $post_id));
381
- $eof = (int)($last_post_id == $max_post_id);
382
- if ($eof) {
383
- $status = 'complete';
384
- $msg = 'Your comments have been sent to Disqus and queued for import!<br/><a href="'.DISQUS_IMPORTER_URL.'" target="_blank">See the status of your import at Disqus</a>';
385
- }
386
- else {
387
- $status = 'partial';
388
- if (count($posts) == 1) {
389
- $msg = dsq_i('Processed comments on post #%s&hellip;', $first_post_id);
390
- }
391
- else {
392
- $msg = dsq_i('Processed comments on posts #%s-%s&hellip;', $first_post_id, $last_post_id);
393
- }
394
- }
395
- $result = 'fail';
396
- $response = null;
397
- if (count($posts)) {
398
- include_once(dirname(__FILE__) . '/export.php');
399
- $wxr = dsq_export_wp($posts);
400
- $response = $dsq_api->import_wordpress_comments($wxr, $timestamp, $eof);
401
- if (!($response['group_id'] > 0)) {
402
- $result = 'fail';
403
- $msg = '<p class="status dsq-export-fail">'. dsq_i('Sorry, something unexpected happened with the export. Please <a href="#" id="dsq_export_retry">try again</a></p><p>If your API key has changed, you may need to reinstall Disqus (deactivate the plugin and then reactivate it). If you are still having issues, refer to the <a href="%s" onclick="window.open(this.href); return false">WordPress help page</a>.', 'http://disqus.com/help/wordpress'). '</p>';
404
- $response = $dsq_api->get_last_error();
405
- }
406
- else {
407
- if ($eof) {
408
- $msg = dsq_i('Your comments have been sent to Disqus and queued for import!<br/><a href="%s" target="_blank">See the status of your import at Disqus</a>', $response['link']);
409
-
410
- }
411
- $result = 'success';
412
- }
413
- }
414
  // send AJAX response
415
- $response = compact('result', 'timestamp', 'status', 'last_post_id', 'msg', 'eof', 'response');
416
- header('Content-type: text/javascript');
417
- echo cf_json_encode($response);
418
- die();
419
- }
420
- break;
421
- case 'import_comments':
422
- if (current_user_can('manage_options')) {
423
- if (!isset($_GET['last_comment_id'])) $last_comment_id = false;
424
- else $last_comment_id = $_GET['last_comment_id'];
425
-
426
- if ($_GET['wipe'] == '1') {
427
- $wpdb->query("DELETE FROM `".$wpdb->prefix."commentmeta` WHERE meta_key IN ('dsq_post_id', 'dsq_parent_post_id')");
428
- $wpdb->query("DELETE FROM `".$wpdb->prefix."comments` WHERE comment_agent LIKE 'Disqus/%%'");
429
- }
430
-
431
- ob_start();
432
- $response = dsq_sync_forum($last_comment_id);
433
- $debug = ob_get_clean();
434
- if (!$response) {
435
- $status = 'error';
436
- $result = 'fail';
437
- $error = $dsq_api->get_last_error();
438
- $msg = '<p class="status dsq-export-fail">'.dsq_i('There was an error downloading your comments from Disqus.').'<br/>'.htmlspecialchars($error).'</p>';
439
- } else {
440
- list($comments, $last_comment_id) = $response;
441
- if (!$comments) {
442
- $status = 'complete';
443
- $msg = dsq_i('Your comments have been downloaded from Disqus and saved in your local database.');
444
- } else {
445
- $status = 'partial';
446
- $msg = dsq_i('Import in progress (last post id: %s) &hellip;', $last_comment_id);
447
- }
448
- $result = 'success';
449
- }
450
- $debug = explode("\n", $debug);
451
- $response = compact('result', 'status', 'comments', 'msg', 'last_comment_id', 'debug');
452
- header('Content-type: text/javascript');
453
- echo cf_json_encode($response);
454
- die();
455
- }
456
- break;
457
- }
458
- }
459
  }
460
 
461
  add_action('init', 'dsq_request_handler');
462
 
463
  function dsq_sync_post($post_id) {
464
- global $dsq_api, $wpdb;
465
-
466
- $post = get_post($post_id);
467
 
468
- // Call update_thread to ensure our permalink is up to date
469
- dsq_update_permalink($post);
470
  }
471
 
472
  add_action('dsq_sync_post', 'dsq_sync_post');
473
 
474
  function dsq_sync_forum($last_comment_id=false) {
475
- global $dsq_api, $wpdb;
476
-
477
- if ($last_comment_id === false) {
478
- $last_comment_id = get_option('disqus_last_comment_id');
479
- if (!$last_comment_id) {
480
- $last_comment_id = 0;
481
- }
482
- }
483
- if ($last_comment_id) {
484
- $last_comment_id++;
485
- }
486
-
487
- //$last_comment_id = 0;
488
-
489
- // Pull comments from API
490
- $dsq_response = $dsq_api->get_forum_posts($last_comment_id);
491
- if( $dsq_response < 0 || $dsq_response === false ) {
492
- return false;
493
- }
494
-
495
- // Sync comments with database.
496
- dsq_sync_comments($dsq_response);
497
- if ($dsq_response) {
498
- foreach ($dsq_response as $comment) {
499
- if ($comment->id > $last_comment_id) $last_comment_id = $comment->id;
500
- }
501
- if ($last_comment_id > get_option('disqus_last_comment_id')) {
502
- update_option('disqus_last_comment_id', $last_comment_id);
503
- }
504
- }
505
- return array(count($dsq_response), $last_comment_id);
 
 
 
506
  }
507
 
508
  add_action('dsq_sync_forum', 'dsq_sync_forum');
509
 
510
- function dsq_update_permalink($post) {
511
- global $dsq_api;
 
 
 
 
 
 
 
 
512
 
513
- $response = $dsq_api->api->update_thread(null, array(
514
- 'thread_identifier' => dsq_identifier_for_post($post),
515
- 'title' => dsq_title_for_post($post),
516
- 'url' => dsq_link_for_post($post)
517
- ));
518
-
519
- update_post_meta($post->ID, 'dsq_thread_id', $response->id);
520
-
521
- return $response;
522
  }
523
 
524
  /**
@@ -527,122 +573,122 @@ function dsq_update_permalink($post) {
527
 
528
  if (!function_exists ( '_wp_specialchars' ) ) {
529
  function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
530
- $string = (string) $string;
531
-
532
- if ( 0 === strlen( $string ) ) {
533
- return '';
534
- }
535
-
536
- // Don't bother if there are no specialchars - saves some processing
537
- if ( !preg_match( '/[&<>"\']/', $string ) ) {
538
- return $string;
539
- }
540
-
541
- // Account for the previous behaviour of the function when the $quote_style is not an accepted value
542
- if ( empty( $quote_style ) ) {
543
- $quote_style = ENT_NOQUOTES;
544
- } elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
545
- $quote_style = ENT_QUOTES;
546
- }
547
-
548
- // Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
549
- if ( !$charset ) {
550
- static $_charset;
551
- if ( !isset( $_charset ) ) {
552
- $alloptions = wp_load_alloptions();
553
- $_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
554
- }
555
- $charset = $_charset;
556
- }
557
- if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) ) {
558
- $charset = 'UTF-8';
559
- }
560
-
561
- $_quote_style = $quote_style;
562
-
563
- if ( $quote_style === 'double' ) {
564
- $quote_style = ENT_COMPAT;
565
- $_quote_style = ENT_COMPAT;
566
- } elseif ( $quote_style === 'single' ) {
567
- $quote_style = ENT_NOQUOTES;
568
- }
569
-
570
- // Handle double encoding ourselves
571
- if ( !$double_encode ) {
572
- $string = wp_specialchars_decode( $string, $_quote_style );
573
- $string = preg_replace( '/&(#?x?[0-9a-z]+);/i', '|wp_entity|$1|/wp_entity|', $string );
574
- }
575
-
576
- $string = @htmlspecialchars( $string, $quote_style, $charset );
577
-
578
- // Handle double encoding ourselves
579
- if ( !$double_encode ) {
580
- $string = str_replace( array( '|wp_entity|', '|/wp_entity|' ), array( '&', ';' ), $string );
581
- }
582
-
583
- // Backwards compatibility
584
- if ( 'single' === $_quote_style ) {
585
- $string = str_replace( "'", '&#039;', $string );
586
- }
587
-
588
- return $string;
589
  }
590
  }
591
 
592
  if (!function_exists ( 'wp_check_invalid_utf8' ) ) {
593
  function wp_check_invalid_utf8( $string, $strip = false ) {
594
- $string = (string) $string;
595
 
596
- if ( 0 === strlen( $string ) ) {
597
- return '';
598
- }
599
 
600
- // Store the site charset as a static to avoid multiple calls to get_option()
601
- static $is_utf8;
602
- if ( !isset( $is_utf8 ) ) {
603
- $is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
604
- }
605
- if ( !$is_utf8 ) {
606
- return $string;
607
- }
608
 
609
- // Check for support for utf8 in the installed PCRE library once and store the result in a static
610
- static $utf8_pcre;
611
- if ( !isset( $utf8_pcre ) ) {
612
- $utf8_pcre = @preg_match( '/^./u', 'a' );
613
- }
614
- // We can't demand utf8 in the PCRE installation, so just return the string in those cases
615
- if ( !$utf8_pcre ) {
616
- return $string;
617
- }
618
 
619
- // preg_match fails when it encounters invalid UTF8 in $string
620
- if ( 1 === @preg_match( '/^./us', $string ) ) {
621
- return $string;
622
- }
623
 
624
- // Attempt to strip the bad chars if requested (not recommended)
625
- if ( $strip && function_exists( 'iconv' ) ) {
626
- return iconv( 'utf-8', 'utf-8', $string );
627
- }
628
 
629
- return '';
630
  }
631
  }
632
 
633
  if (!function_exists ( 'esc_html' ) ) {
634
  function esc_html( $text ) {
635
- $safe_text = wp_check_invalid_utf8( $text );
636
- $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
637
- return apply_filters( 'esc_html', $safe_text, $text );
638
  }
639
  }
640
 
641
  if (!function_exists ( 'esc_attr' ) ) {
642
  function esc_attr( $text ) {
643
- $safe_text = wp_check_invalid_utf8( $text );
644
- $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
645
- return apply_filters( 'attribute_escape', $safe_text, $text );
646
  }
647
  }
648
 
@@ -650,320 +696,302 @@ function esc_attr( $text ) {
650
  * Filters/Actions
651
  */
652
 
653
- function dsq_get_style() {
654
- global $post;
655
-
656
- if ( !( is_singular() && ( have_comments() || 'open' == $post->comment_status ) ) ) {
657
- return;
658
- }
659
-
660
- if ( !dsq_is_installed() || !dsq_can_replace() ) {
661
- return;
662
- }
663
-
664
- echo "<link rel=\"stylesheet\" href=\"" . DISQUS_URL ."stylesheets/" . strtolower(get_option('disqus_forum_url')) . "/disqus.css?v=2.0\" type=\"text/css\" media=\"screen\" />";
665
- }
666
-
667
- add_action('wp_head','dsq_get_style');
668
-
669
  // ugly global hack for comments closing
670
  $EMBED = false;
671
  function dsq_comments_template($value) {
672
- global $EMBED;
673
- global $post;
674
- global $comments;
675
 
676
- if ( !( is_singular() && ( have_comments() || 'open' == $post->comment_status ) ) ) {
677
- return;
678
- }
679
 
680
- if ( !dsq_is_installed() || !dsq_can_replace() ) {
681
- return $value;
682
- }
683
 
684
- // TODO: If a disqus-comments.php is found in the current template's
685
- // path, use that instead of the default bundled comments.php
686
- //return TEMPLATEPATH . '/disqus-comments.php';
687
- $EMBED = true;
688
- return dirname(__FILE__) . '/comments.php';
689
  }
690
 
691
  // Mark entries in index to replace comments link.
 
692
  function dsq_comments_number($count) {
693
  global $post;
694
 
695
- if ( dsq_can_replace() ) {
696
- return '<span class="dsq-postid" rel="'.htmlspecialchars(dsq_identifier_for_post($post)).'">'.$count.'</span>';
697
- } else {
698
- return $count;
699
- }
700
  }
701
 
702
  function dsq_comments_text($comment_text) {
703
- global $post;
704
 
705
- if ( dsq_can_replace() ) {
706
- return '<span class="dsq-postid" rel="'.htmlspecialchars(dsq_identifier_for_post($post)).'">View Comments</span>';
707
- } else {
708
- return $comment_text;
709
- }
710
  }
711
 
712
  function dsq_bloginfo_url($url) {
713
- if ( get_feed_link('comments_rss2') == $url ) {
714
- return 'http://' . strtolower(get_option('disqus_forum_url')) . '.' . DISQUS_DOMAIN . DISQUS_RSS_PATH;
715
- } else {
716
- return $url;
717
- }
718
  }
719
 
720
  function dsq_plugin_action_links($links, $file) {
721
- $plugin_file = basename(__FILE__);
722
- if (basename($file) == $plugin_file) {
723
- $settings_link = '<a href="edit-comments.php?page=disqus#adv">'.__('Settings', 'disqus-comment-system').'</a>';
724
- array_unshift($links, $settings_link);
725
- }
726
- return $links;
727
  }
728
  add_filter('plugin_action_links', 'dsq_plugin_action_links', 10, 2);
729
 
730
- /**
731
  * Hide the default comment form to stop spammers by marking all comments
732
  * as closed.
733
  */
734
  function dsq_comments_open($open, $post_id=null) {
735
- global $EMBED;
736
- if ($EMBED) return false;
737
- return $open;
738
  }
739
  add_filter('comments_open', 'dsq_comments_open');
740
 
741
  // Always add Disqus management page to the admin menu
742
  function dsq_add_pages() {
743
- add_submenu_page(
744
- 'edit-comments.php',
745
- 'Disqus',
746
- 'Disqus',
747
- 'moderate_comments',
748
- 'disqus',
749
- 'dsq_manage'
750
- );
751
  }
752
  add_action('admin_menu', 'dsq_add_pages', 10);
753
 
754
  // a little jQuery goodness to get comments menu working as desired
755
- function sdq_menu_admin_head() {
756
  ?>
757
  <script type="text/javascript">
758
  jQuery(function($) {
759
  // fix menu
760
- var mc = $('#menu-comments');
761
- mc.find('a.wp-has-submenu').attr('href', 'edit-comments.php?page=disqus').end().find('.wp-submenu li:has(a[href=edit-comments.php?page=disqus])').prependTo(mc.find('.wp-submenu ul'));
762
  });
763
  </script>
764
  <?php
765
  }
766
- add_action('admin_head', 'sdq_menu_admin_head');
767
 
768
  // only active on dashboard
769
  function dsq_dash_comment_counts() {
770
- global $wpdb;
771
  // taken from wp-includes/comment.php - WP 2.8.5
772
- $count = $wpdb->get_results("
773
- SELECT comment_approved, COUNT( * ) AS num_comments
774
- FROM {$wpdb->comments}
775
- WHERE comment_type != 'trackback'
776
- AND comment_type != 'pingback'
777
- GROUP BY comment_approved
778
- ", ARRAY_A );
779
- $total = 0;
780
- $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam');
781
- $known_types = array_keys( $approved );
782
- foreach( (array) $count as $row_num => $row ) {
783
- $total += $row['num_comments'];
784
- if ( in_array( $row['comment_approved'], $known_types ) )
785
- $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
786
- }
787
-
788
- $stats['total_comments'] = $total;
789
- foreach ( $approved as $key ) {
790
- if ( empty($stats[$key]) )
791
- $stats[$key] = 0;
792
- }
793
- $stats = (object) $stats;
794
  ?>
795
  <style type="text/css">
796
  #dashboard_right_now .inside,
797
  #dashboard_recent_comments div.trackback {
798
- display: none;
799
  }
800
  </style>
801
  <script type="text/javascript">
802
  jQuery(function($) {
803
- $('#dashboard_right_now').find('.b-comments a').html('<?php echo $stats->total_comments; ?>').end().find('.b_approved a').html('<?php echo $stats->approved; ?>').end().find('.b-waiting a').html('<?php echo $stats->moderated; ?>').end().find('.b-spam a').html('<?php echo $stats->spam; ?>').end().find('.inside').slideDown();
804
- $('#dashboard_recent_comments div.trackback').remove();
805
- $('#dashboard_right_now .inside table td.last a, #dashboard_recent_comments .inside .textright a.button').attr('href', 'edit-comments.php?page=disqus');
806
  });
807
  </script>
808
  <?php
809
  }
810
  function dsq_wp_dashboard_setup() {
811
- add_action('admin_head', 'dsq_dash_comment_counts');
812
  }
813
  add_action('wp_dashboard_setup', 'dsq_wp_dashboard_setup');
814
 
815
  function dsq_manage() {
816
- if (dsq_does_need_update() && isset($_POST['upgrade'])) {
817
- dsq_install();
818
- }
819
-
820
- if (dsq_does_need_update() && !$_POST['uninstall']) {
821
- include_once(dirname(__FILE__) . '/upgrade.php');
822
- } else {
823
- include_once(dirname(__FILE__) . '/manage.php');
824
- }
825
  }
826
 
827
  function dsq_admin_head() {
828
- if (isset($_GET['page']) && $_GET['page'] == 'disqus') {
829
  ?>
830
- <link rel='stylesheet' href='<?php echo DSQ_PLUGIN_URL; ?>/styles/manage.css' type='text/css' />
831
  <style type="text/css">
832
  .dsq-importing, .dsq-imported, .dsq-import-fail, .dsq-exporting, .dsq-exported, .dsq-export-fail {
833
- background: url(<?php echo admin_url('images/loading.gif'); ?>) left center no-repeat;
834
- line-height: 16px;
835
- padding-left: 20px;
836
  }
837
  p.status {
838
- padding-top: 0;
839
- padding-bottom: 0;
840
- margin: 0;
841
  }
842
  .dsq-imported, .dsq-exported {
843
- background: url(<?php echo admin_url('images/yes.png'); ?>) left center no-repeat;
844
  }
845
  .dsq-import-fail, .dsq-export-fail {
846
- background: url(<?php echo admin_url('images/no.png'); ?>) left center no-repeat;
847
  }
848
  </style>
849
  <script type="text/javascript">
850
  jQuery(function($) {
851
- $('#dsq-tabs li').click(function() {
852
- $('#dsq-tabs li.selected').removeClass('selected');
853
- $(this).addClass('selected');
854
- $('.dsq-main, .dsq-advanced').hide();
855
- $('.' + $(this).attr('rel')).show();
856
- });
857
- if (location.href.indexOf('#adv') != -1) {
858
- $('#dsq-tab-advanced').click();
859
- }
860
- dsq_fire_export();
861
- dsq_fire_import();
862
  });
863
  dsq_fire_export = function() {
864
- var $ = jQuery;
865
- $('#dsq_export a.button, #dsq_export_retry').unbind().click(function() {
866
- $('#dsq_export').html('<p class="status"></p>');
867
- $('#dsq_export .status').removeClass('dsq-export-fail').addClass('dsq-exporting').html('Processing...');
868
- dsq_export_comments();
869
- return false;
870
- });
871
  }
872
  dsq_export_comments = function() {
873
- var $ = jQuery;
874
- var status = $('#dsq_export .status');
875
- var export_info = (status.attr('rel') || '0|' + (new Date().getTime()/1000)).split('|');
876
- $.get(
877
- '<?php echo admin_url('index.php'); ?>',
878
- {
879
- cf_action: 'export_comments',
880
- post_id: export_info[0],
881
- timestamp: export_info[1]
882
- },
883
- function(response) {
884
- switch (response.result) {
885
- case 'success':
886
- status.html(response.msg).attr('rel', response.last_post_id + '|' + response.timestamp);
887
- switch (response.status) {
888
- case 'partial':
889
- dsq_export_comments();
890
- break;
891
- case 'complete':
892
- status.removeClass('dsq-exporting').addClass('dsq-exported');
893
- break;
894
- }
895
- break;
896
- case 'fail':
897
- status.parent().html(response.msg);
898
- dsq_fire_export();
899
- break;
900
- }
901
- },
902
- 'json'
903
- );
904
  }
905
  dsq_fire_import = function() {
906
- var $ = jQuery;
907
- $('#dsq_import a.button, #dsq_import_retry').unbind().click(function() {
908
- var wipe = $('#dsq_import_wipe').is(':checked');
909
- $('#dsq_import').html('<p class="status"></p>');
910
- $('#dsq_import .status').removeClass('dsq-import-fail').addClass('dsq-importing').html('Processing...');
911
- dsq_import_comments(wipe);
912
- return false;
913
- });
914
  }
915
  dsq_import_comments = function(wipe) {
916
- var $ = jQuery;
917
- var status = $('#dsq_import .status');
918
- var last_comment_id = status.attr('rel') || '0';
919
- $.get(
920
- '<?php echo admin_url('index.php'); ?>',
921
- {
922
- cf_action: 'import_comments',
923
- last_comment_id: last_comment_id,
924
- wipe: (wipe ? 1 : 0)
925
- },
926
- function(response) {
927
- switch (response.result) {
928
- case 'success':
929
- status.html(response.msg).attr('rel', response.last_comment_id);
930
- switch (response.status) {
931
- case 'partial':
932
- dsq_import_comments();
933
- break;
934
- case 'complete':
935
- status.removeClass('dsq-importing').addClass('dsq-imported');
936
- break;
937
- }
938
- break;
939
- case 'fail':
940
- status.parent().html(response.msg);
941
- dsq_fire_import();
942
- break;
943
- }
944
- },
945
- 'json'
946
- );
947
  }
948
  </script>
949
  <?php
950
  // HACK: Our own styles for older versions of WordPress.
951
- global $wp_version;
952
- if ( version_compare($wp_version, '2.5', '<') ) {
953
- echo "<link rel='stylesheet' href='" . DSQ_PLUGIN_URL . "/styles/manage-pre25.css' type='text/css' />";
954
- }
955
- }
956
  }
957
  add_action('admin_head', 'dsq_admin_head');
958
 
959
  function dsq_warning() {
960
- if ( !get_option('disqus_forum_url') && !isset($_POST['forum_url']) && $_GET['page'] != 'disqus' ) {
961
- dsq_manage_dialog('You must <a href="edit-comments.php?page=disqus">configure the plugin</a> to enable Disqus Comments.', true);
962
- }
 
963
 
964
- if ( !dsq_is_installed() && $_GET['page'] == 'disqus' && !$_GET['step'] && !$_POST['uninstall'] ) {
965
- dsq_manage_dialog('Disqus Comments has not yet been configured. (<a href="edit-comments.php?page=disqus">Click here to configure</a>)');
966
- }
967
  }
968
 
969
  /**
@@ -971,59 +999,59 @@ function dsq_warning() {
971
  * the disqus domain and supports variable interpolation.
972
  */
973
  function dsq_i($text, $params=null) {
974
- if (!is_array($params))
975
- {
976
- $params = func_get_args();
977
- $params = array_slice($params, 1);
978
- }
979
- return vsprintf(__($text, 'disqus'), $params);
980
  }
981
 
982
  // catch original query
983
  function dsq_parse_query($query) {
984
- add_action('the_posts', 'dsq_add_request_post_ids', 999);
985
  }
986
  add_action('parse_request', 'dsq_parse_query');
987
 
988
  // track the original request post_ids, only run once
989
- function dsq_add_request_post_ids($posts) {
990
- dsq_add_query_posts($posts);
991
- remove_action('the_posts', 'dsq_log_request_post_ids', 999);
992
- return $posts;
993
  }
994
 
995
- function dsq_maybe_add_post_ids($posts) {
996
- global $DSQ_QUERY_COMMENTS;
997
- if ($DSQ_QUERY_COMMENTS) {
998
- dsq_add_query_posts($posts);
999
- }
1000
- return $posts;
1001
  }
1002
  add_action('the_posts', 'dsq_maybe_add_post_ids');
1003
 
1004
- function dsq_add_query_posts($posts) {
1005
- global $DSQ_QUERY_POST_IDS;
1006
- if (count($posts)) {
1007
- foreach ($posts as $post) {
1008
- $post_ids[] = intval($post->ID);
1009
- }
1010
- $DSQ_QUERY_POST_IDS[md5(serialize($post_ids))] = $post_ids;
1011
- }
1012
  }
1013
 
1014
  // check to see if the posts in the loop match the original request or an explicit request, if so output the JS
1015
  function dsq_loop_end($query) {
1016
- if ( get_option('disqus_cc_fix') == '1' || !count($query->posts) || is_single() || is_page() || is_feed() ) {
1017
- return;
1018
- }
1019
- global $DSQ_QUERY_POST_IDS;
1020
- foreach ($query->posts as $post) {
1021
- $loop_ids[] = intval($post->ID);
1022
- }
1023
- $posts_key = md5(serialize($loop_ids));
1024
- if (isset($DSQ_QUERY_POST_IDS[$posts_key])) {
1025
- dsq_output_loop_comment_js($DSQ_QUERY_POST_IDS[$posts_key]);
1026
- }
1027
  }
1028
  add_action('loop_end', 'dsq_loop_end');
1029
 
@@ -1032,64 +1060,64 @@ add_action('loop_end', 'dsq_loop_end');
1032
  $_HAS_COUNTS = false;
1033
 
1034
  function dsq_output_loop_comment_js($post_ids = null) {
1035
- global $_HAS_COUNTS;
1036
- if ($_HAS_COUNTS) return;
1037
- $_HAS_COUNTS = true;
1038
- if (count($post_ids)) {
1039
  ?>
1040
- <script type="text/javascript">
1041
- // <![CDATA[
1042
- var disqus_shortname = '<?php echo strtolower(get_option('disqus_forum_url')); ?>';
1043
- var disqus_domain = '<?php echo DISQUS_DOMAIN; ?>';
1044
- (function () {
1045
- var nodes = document.getElementsByTagName('span');
1046
- for (var i = 0, url; i < nodes.length; i++) {
1047
- if (nodes[i].className.indexOf('dsq-postid') != -1) {
1048
- nodes[i].parentNode.setAttribute('data-disqus-identifier', nodes[i].getAttribute('rel'));
1049
- url = nodes[i].parentNode.href.split('#', 1);
1050
- if (url.length == 1) url = url[0];
1051
- else url = url[1]
1052
- nodes[i].parentNode.href = url + '#disqus_thread';
1053
- }
1054
- }
1055
- var s = document.createElement('script'); s.async = true;
1056
- s.type = 'text/javascript';
1057
- s.src = 'http://' + disqus_domain + '/forums/' + disqus_shortname + '/count.js';
1058
- (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
1059
- }());
1060
- //]]>
1061
- </script>
1062
  <?php
1063
- }
1064
  }
1065
 
1066
  function dsq_output_footer_comment_js() {
1067
- if (get_option('disqus_cc_fix') == '1') {
 
1068
  ?>
1069
- <script type="text/javascript">
1070
- // <![CDATA[
1071
- var disqus_shortname = '<?php echo strtolower(get_option('disqus_forum_url')); ?>';
1072
- var disqus_domain = '<?php echo DISQUS_DOMAIN; ?>';
1073
- (function () {
1074
- var nodes = document.getElementsByTagName('span');
1075
- for (var i = 0, url; i < nodes.length; i++) {
1076
- if (nodes[i].className.indexOf('dsq-postid') != -1) {
1077
- nodes[i].parentNode.setAttribute('data-disqus-identifier', nodes[i].getAttribute('rel'));
1078
- url = nodes[i].parentNode.href.split('#', 1);
1079
- if (url.length == 1) url = url[0];
1080
- else url = url[1]
1081
- nodes[i].parentNode.href = url + '#disqus_thread';
1082
- }
1083
- }
1084
- var s = document.createElement('script'); s.async = true;
1085
- s.type = 'text/javascript';
1086
- s.src = 'http://' + disqus_domain + '/forums/' + disqus_shortname + '/count.js';
1087
- (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
1088
- }());
1089
- //]]>
1090
- </script>
1091
  <?php
1092
- }
1093
  }
1094
  add_action('wp_footer', 'dsq_output_footer_comment_js');
1095
 
@@ -1099,21 +1127,21 @@ $dsq_prev_permalinks = array();
1099
 
1100
  function dsq_prev_permalink($post_id) {
1101
  // if post not published, return
1102
- $post = &get_post($post_id);
1103
- if ($post->post_status != 'publish') {
1104
- return;
1105
- }
1106
- global $dsq_prev_permalinks;
1107
- $dsq_prev_permalinks['post_'.$post_id] = get_permalink($post_id);
1108
  }
1109
  add_action('pre_post_update', 'dsq_prev_permalink');
1110
 
1111
  function dsq_check_permalink($post_id) {
1112
- global $dsq_prev_permalinks;
1113
- if (!empty($dsq_prev_permalinks['post_'.$post_id]) && $dsq_prev_permalinks['post_'.$post_id] != get_permalink($post_id)) {
1114
- $post = get_post($post_id);
1115
- dsq_update_permalink($post);
1116
- }
1117
  }
1118
  add_action('edit_post', 'dsq_check_permalink');
1119
 
@@ -1130,134 +1158,134 @@ add_filter('bloginfo_url', 'dsq_bloginfo_url');
1130
  * Checks if json_encode is not available and defines json_encode
1131
  * to use php_json_encode in its stead
1132
  * Works on iteratable objects as well - stdClass is iteratable, so all WP objects are gonna be iteratable
1133
- */
1134
  if(!function_exists('cf_json_encode')) {
1135
- function cf_json_encode($data) {
1136
  // json_encode is sending an application/x-javascript header on Joyent servers
1137
  // for some unknown reason.
1138
- // if(function_exists('json_encode')) { return json_encode($data); }
1139
- // else { return cfjson_encode($data); }
1140
- return cfjson_encode($data);
1141
- }
1142
-
1143
- function cfjson_encode_string($str) {
1144
- if(is_bool($str)) {
1145
- return $str ? 'true' : 'false';
1146
- }
1147
-
1148
- return str_replace(
1149
- array(
1150
- '"'
1151
- , '/'
1152
- , "\n"
1153
- , "\r"
1154
- )
1155
- , array(
1156
- '\"'
1157
- , '\/'
1158
- , '\n'
1159
- , '\r'
1160
- )
1161
- , $str
1162
- );
1163
- }
1164
-
1165
- function cfjson_encode($arr) {
1166
- $json_str = '';
1167
- if (is_array($arr)) {
1168
- $pure_array = true;
1169
- $array_length = count($arr);
1170
- for ( $i = 0; $i < $array_length ; $i++) {
1171
- if (!isset($arr[$i])) {
1172
- $pure_array = false;
1173
- break;
1174
- }
1175
- }
1176
- if ($pure_array) {
1177
- $json_str = '[';
1178
- $temp = array();
1179
- for ($i=0; $i < $array_length; $i++) {
1180
- $temp[] = sprintf("%s", cfjson_encode($arr[$i]));
1181
- }
1182
- $json_str .= implode(',', $temp);
1183
- $json_str .="]";
1184
- }
1185
- else {
1186
- $json_str = '{';
1187
- $temp = array();
1188
- foreach ($arr as $key => $value) {
1189
- $temp[] = sprintf("\"%s\":%s", $key, cfjson_encode($value));
1190
- }
1191
- $json_str .= implode(',', $temp);
1192
- $json_str .= '}';
1193
- }
1194
- }
1195
- else if (is_object($arr)) {
1196
- $json_str = '{';
1197
- $temp = array();
1198
- foreach ($arr as $k => $v) {
1199
- $temp[] = '"'.$k.'":'.cfjson_encode($v);
1200
- }
1201
- $json_str .= implode(',', $temp);
1202
- $json_str .= '}';
1203
- }
1204
- else if (is_string($arr)) {
1205
- $json_str = '"'. cfjson_encode_string($arr) . '"';
1206
- }
1207
- else if (is_numeric($arr)) {
1208
- $json_str = $arr;
1209
- }
1210
- else if (is_bool($arr)) {
1211
- $json_str = $arr ? 'true' : 'false';
1212
- }
1213
- else {
1214
- $json_str = '"'. cfjson_encode_string($arr) . '"';
1215
- }
1216
- return $json_str;
1217
- }
1218
  }
1219
 
1220
  // Single Sign-on Integration
1221
 
1222
  function dsq_sso() {
1223
- if ($key = get_option('disqus_partner_key')) {
1224
- // use old style SSO
1225
- $new = false;
1226
- } elseif (($key = get_option('disqus_secret_key')) && ($public = get_option('disqus_public_key'))) {
1227
- // use new style SSO
1228
- $new = true;
1229
- } else {
1230
- // sso is not configured
1231
- return array();
1232
- }
1233
- global $current_user, $dsq_api;
1234
- get_currentuserinfo();
1235
- if ($current_user->ID) {
1236
- $avatar_tag = get_avatar($current_user->ID);
1237
- $avatar_data = array();
1238
- preg_match('/(src)=((\'|")[^(\'|")]*(\'|"))/i', $avatar_tag, $avatar_data);
1239
- $avatar = str_replace(array('"', "'"), '', $avatar_data[2]);
1240
- $user_data = array(
1241
- 'username' => $current_user->display_name,
1242
- 'id' => $current_user->ID,
1243
- 'avatar' => $avatar,
1244
- 'email' => $current_user->user_email,
1245
- );
1246
- }
1247
- else {
1248
- $user_data = array();
1249
- }
1250
- $user_data = base64_encode(cf_json_encode($user_data));
1251
- $time = time();
1252
- $hmac = dsq_hmacsha1($user_data.' '.$time, $key);
1253
-
1254
- $payload = $user_data.' '.$hmac.' '.$time;
1255
-
1256
- if ($new) {
1257
- return array('remote_auth_s3'=>$payload, 'api_key'=>$public);
1258
- } else {
1259
- return array('remote_auth_s2'=>$payload);
1260
- }
1261
  }
1262
 
1263
  // from: http://www.php.net/manual/en/function.sha1.php#39492
@@ -1284,63 +1312,70 @@ function dsq_hmacsha1($data, $key) {
1284
  }
1285
 
1286
  function dsq_identifier_for_post($post) {
1287
- return $post->ID . ' ' . $post->guid;
1288
  }
1289
 
1290
  function dsq_title_for_post($post) {
1291
- $title = get_the_title($post);
1292
- $title = strip_tags($title, DISQUS_ALLOWED_HTML);
1293
- return $title;
1294
  }
1295
 
1296
  function dsq_link_for_post($post) {
1297
- return get_permalink($post);
1298
  }
1299
 
1300
  function dsq_does_need_update() {
1301
- $version = (string)get_option('disqus_version');
1302
- if (empty($version)) {
1303
- $version = '0';
1304
- }
1305
-
1306
- if (version_compare($version, '2.49', '<')) {
1307
- return true;
1308
- }
1309
-
1310
- return false;
1311
  }
1312
 
1313
  function dsq_install($allow_database_install=true) {
1314
- global $wpdb, $userdata;
 
 
 
 
 
1315
 
1316
- $version = (string)get_option('disqus_version');
1317
- if (!$version) {
1318
- $version = '0';
1319
- }
1320
 
1321
- if ($allow_database_install)
1322
- {
1323
- dsq_install_database($version);
1324
- }
1325
-
1326
- if (version_compare($version, DISQUS_VERSION, '=')) return;
1327
 
1328
- update_option('disqus_version', DISQUS_VERSION);
 
 
 
 
 
 
 
1329
  }
1330
 
1331
  /**
1332
  * Initializes the database if it's not already present.
1333
  */
1334
  function dsq_install_database($version=0) {
1335
- global $wpdb;
1336
-
1337
- if (version_compare($version, '2.49', '<')) {
1338
- $wpdb->query("CREATE INDEX disqus_dupecheck ON `".$wpdb->prefix."commentmeta` (meta_key, meta_value(11));");
1339
- }
1340
- }
1341
- function dsq_uninstall_database() {
1342
- if (version_compare($version, '2.49', '>=')) {
1343
- $wpdb->query("DROP INDEX disqus_dupecheck ON `".$wpdb->prefix."commentmeta` (meta_key, meta_value(11));");
1344
- }
1345
  }
1346
  ?>
4
  Plugin URI: http://disqus.com/
5
  Description: The Disqus comment system replaces your WordPress comment system with your comments hosted and powered by Disqus. Head over to the Comments admin page to set up your DISQUS Comment System.
6
  Author: Disqus <team@disqus.com>
7
+ Version: 2.64
8
  Author URI: http://disqus.com/
9
  */
10
 
11
+ /*.
12
+ require_module 'standard';
13
+ require_module 'pcre';
14
+ require_module 'mysql';
15
+ .*/
16
+
17
+ require_once(dirname(__FILE__) . '/lib/wp-api.php');
18
+
19
+ if (defined('DISQUS_LOCAL')) { // DISQUS defines this for local development purposes
20
+ define('DISQUS_DOMAIN', 'disqus.com');
21
+ define('DISQUS_IMPORTER_URL', 'http://import.disqus.com/');
22
+ } else {
23
+ define('DISQUS_DOMAIN', 'disqus.com');
24
+ define('DISQUS_IMPORTER_URL', 'http://import.disqus.com/');
25
+ }
26
+ define('DISQUS_URL', 'http://' . DISQUS_DOMAIN . '/');
27
+ define('DISQUS_MEDIA_URL', 'http://' . DISQUS_DOMAIN . '/media/');
28
+ define('DISQUS_API_URL', 'http://' . DISQUS_DOMAIN . '/api/');
29
+ define('DISQUS_RSS_PATH', '/latest.rss');
30
+ define('DISQUS_CAN_EXPORT', is_file(dirname(__FILE__) . '/export.php'));
31
  if (!defined('DISQUS_DEBUG')) {
32
+ define('DISQUS_DEBUG', false);
33
  }
34
+ define('DISQUS_VERSION', '2.64');
35
 
36
  /**
37
  * Returns an array of all option identifiers used by DISQUS.
38
+ * @return array[int]string
39
  */
40
  function dsq_options() {
41
+ return array(
42
+ # render disqus in the embed
43
+ 'disqus_active',
44
+ 'disqus_public_key',
45
+ 'disqus_secret_key',
46
+ 'disqus_forum_url',
47
+ 'disqus_api_key',
48
+ 'disqus_user_api_key',
49
+ 'disqus_partner_key',
50
+ 'disqus_replace',
51
+ 'disqus_cc_fix',
52
+ # disables automatic sync via cron
53
+ 'disqus_manual_sync',
54
+ # disables server side rendering
55
+ 'disqus_disable_ssr',
56
+ # the last sync comment id (from get_forum_posts)
57
+ 'disqus_last_comment_id',
58
+ 'disqus_version',
59
+ );
60
  }
61
 
62
+ /**
63
+ * @param string $file
64
+ * @return string
65
+ */
66
  function dsq_plugin_basename($file) {
67
+ $file = dirname($file);
68
 
69
+ // From WP2.5 wp-includes/plugin.php:plugin_basename()
70
+ $file = str_replace('\\','/',$file); // sanitize for Win32 installs
71
+ $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
72
+ $file = preg_replace('|^.*/' . PLUGINDIR . '/|','',$file); // get relative path from plugins dir
73
 
74
+ if ( strstr($file, '/') === false ) {
75
+ return $file;
76
+ }
77
 
78
+ $pieces = explode('/', $file);
79
+ return !empty($pieces[count($pieces)-1]) ? $pieces[count($pieces)-1] : $pieces[count($pieces)-2];
80
  }
81
 
82
  if ( !defined('WP_CONTENT_URL') ) {
83
+ define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
84
  }
85
  if ( !defined('PLUGINDIR') ) {
86
+ define('PLUGINDIR', 'wp-content/plugins'); // Relative to ABSPATH. For back compat.
87
  }
88
 
89
  define('DSQ_PLUGIN_URL', WP_CONTENT_URL . '/plugins/' . dsq_plugin_basename(__FILE__));
92
  /**
93
  * Response from Disqus get_thread API call for comments template.
94
  *
95
+ * @global string $dsq_response
96
+ * @since 1.0
97
  */
98
  $dsq_response = '';
99
  /**
100
  * Disqus API instance.
101
  *
102
+ * @global string $dsq_api
103
+ * @since 1.0
104
  */
105
  $dsq_api = new DisqusWordPressAPI(get_option('disqus_forum_url'), get_option('disqus_api_key'));
106
 
107
  /**
108
  * DISQUS currently unsupported dev toggle to output comments for this query.
109
  *
110
+ * @global bool $DSQ_QUERY_COMMENTS
111
+ * @since ?
112
  */
113
  $DSQ_QUERY_COMMENTS = false;
114
 
115
  /**
116
  * DISQUS array to store post_ids from WP_Query for comment JS output.
117
  *
118
+ * @global array $DSQ_QUERY_POST_IDS
119
+ * @since 2.2
120
  */
121
  $DSQ_QUERY_POST_IDS = array();
122
 
126
 
127
  /**
128
  * Tests if required options are configured to display the Disqus embed.
129
+ * @return bool
130
  */
131
  function dsq_is_installed() {
132
+ return get_option('disqus_forum_url') && get_option('disqus_api_key');
133
  }
134
 
135
+ /**
136
+ * @return bool
137
+ */
138
  function dsq_can_replace() {
139
+ global $id, $post;
140
+
141
+ if (get_option('disqus_active') === '0'){ return false; }
142
+
143
+ $replace = get_option('disqus_replace');
144
 
145
  if ( is_feed() ) { return false; }
146
+ if ( 'draft' == $post->post_status ) { return false; }
147
+ if ( !get_option('disqus_forum_url') ) { return false; }
148
+ else if ( 'all' == $replace ) { return true; }
149
+
150
+ if ( !isset($post->comment_count) ) {
151
+ $num_comments = 0;
152
+ } else {
153
+ if ( 'empty' == $replace ) {
154
+ // Only get count of comments, not including pings.
155
+
156
+ // If there are comments, make sure there are comments (that are not track/pingbacks)
157
+ if ( $post->comment_count > 0 ) {
158
+ // Yuck, this causes a DB query for each post. This can be
159
+ // replaced with a lighter query, but this is still not optimal.
160
+ $comments = get_approved_comments($post->ID);
161
+ foreach ( $comments as $comment ) {
162
+ if ( $comment->comment_type != 'trackback' && $comment->comment_type != 'pingback' ) {
163
+ $num_comments++;
164
+ }
165
+ }
166
+ } else {
167
+ $num_comments = 0;
168
+ }
169
+ }
170
+ else {
171
+ $num_comments = $post->comment_count;
172
+ }
173
+ }
174
+
175
+ return ( ('empty' == $replace && 0 == $num_comments)
176
+ || ('closed' == $replace && 'closed' == $post->comment_status) );
177
  }
178
 
179
  function dsq_manage_dialog($message, $error = false) {
180
+ global $wp_version;
181
+
182
+ echo '<div '
183
+ . ( $error ? 'id="disqus_warning" ' : '')
184
+ . 'class="updated fade'
185
+ . ( (version_compare($wp_version, '2.5', '<') && $error) ? '-ff0000' : '' )
186
+ . '"><p><strong>'
187
+ . $message
188
+ . '</strong></p></div>';
189
+ }
190
+
191
+ function dsq_sync_comments(&$comments) {
192
+ global $wpdb;
193
+
194
+ // user MUST be logged out during this process
195
+ wp_set_current_user(0);
196
+
197
+ // we need the thread_ids so we can map them to posts
198
+ $thread_map = array();
199
+ foreach ( $comments as $comment ) {
200
+ $thread_map[$comment->thread->id] = null;
201
+ }
202
+ $thread_ids = "'" . implode("', '", array_keys($thread_map)) . "'";
203
+
204
+ $results = $wpdb->get_results( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = 'dsq_thread_id' AND meta_value IN ({$thread_ids}) LIMIT 1");
205
+ foreach ( $results as $result ) {
206
+ $thread_map[$result->meta_value] = $result->post_id;
207
+ }
208
+ unset($result);
209
+
210
+ foreach ( $comments as $comment ) {
211
+ $ts = strtotime($comment->created_at);
212
+ if (!$thread_map[$comment->thread->id] && !empty($comment->thread->identifier)) {
213
+ // legacy threads dont already have their meta stored
214
+ foreach ( $comment->thread->identifier as $identifier ) {
215
+ // we know identifier starts with post_ID
216
+ if ($post_ID = (int)substr($identifier, 0, strpos($identifier, ' '))) {
217
+ $thread_map[$comment->thread->id] = $post_ID;
218
+ update_post_meta($post_ID, 'dsq_thread_id', $comment->thread->id);
219
+ }
220
+ }
221
+ unset($identifier);
222
+ }
223
+ if (!$thread_map[$comment->thread->id]) {
224
+ // shouldn't ever happen, but we can't be certain
225
+ if (DISQUS_DEBUG) {
226
+ if (!empty($comment->thread->identifier)) {
227
+ $idents = implode(', ', $comment->thread->identifier);
228
+ echo "skipped {$comment->id}: missing thread for identifiers ({$idents})\n";
229
+ } else {
230
+ echo "skipped {$comment->id}: missing thread (no identifier)\n";
231
+ }
232
+ }
233
+ continue;
234
+ }
235
+ $results = $wpdb->get_results($wpdb->prepare("SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = 'dsq_post_id' AND meta_value = %s LIMIT 1", $comment->id));
236
+ if (count($results)) {
237
+ // already exists
238
+ if (DISQUS_DEBUG) {
239
+ echo "skipped {$comment->id}: comment already exists\n";
240
+ }
241
+ if (count($results) > 1) {
242
+ // clean up duplicates -- fixes an issue where a race condition allowed comments to be synced multiple times
243
+ $results = array_slice($results, 1);
244
+ foreach ($results as $result) {
245
+ $wpdb->prepare("DELETE FROM $wpdb->commentmeta WHERE comment_id = %s LIMIT 1", $result);
246
+ }
247
+ }
248
+ continue;
249
+ }
250
+
251
+ $commentdata = false;
252
+
253
+ // first lets check by the id we have stored
254
+ if ($comment->meta) {
255
+ $meta = explode(';', $comment->meta);
256
+ foreach ($meta as $value) {
257
+ $value = explode('=', $value);
258
+ $meta[$value[0]] = $value[1];
259
+ }
260
+ unset($value);
261
+ if ($meta['wp_id']) {
262
+ $commentdata = $wpdb->get_row($wpdb->prepare( "SELECT comment_ID, comment_parent FROM $wpdb->comments WHERE comment_ID = %s LIMIT 1", $meta['wp_id']), ARRAY_A);
263
+ }
264
+ }
265
+
266
+ // skip comments that were imported but are missing meta information
267
+ if (!$commentdata && $comment->imported) {
268
+ if (DISQUS_DEBUG) {
269
+ echo "skipped {$comment->id}: comment not found and marked as imported\n";
270
+ }
271
+ continue;
272
+ }
273
+
274
+ // and follow up using legacy Disqus agent
275
+ if (!$commentdata) {
276
+ $commentdata = $wpdb->get_row($wpdb->prepare( "SELECT comment_ID, comment_parent FROM $wpdb->comments WHERE comment_agent = 'Disqus/1.0:{$comment->id}' LIMIT 1"), ARRAY_A);
277
+ }
278
+ if (!$commentdata) {
279
+ // Comment doesnt exist yet, lets insert it
280
+ if ($comment->status == 'approved') {
281
+ $approved = 1;
282
+ } elseif ($comment->status == 'spam') {
283
+ $approved = 'spam';
284
+ } else {
285
+ $approved = 0;
286
+ }
287
+ $commentdata = array(
288
+ 'comment_post_ID' => $thread_map[$comment->thread->id],
289
+ 'comment_date' => $comment->created_at,
290
+ 'comment_date_gmt' => $comment->created_at,
291
+ 'comment_content' => apply_filters('pre_comment_content', $comment->message),
292
+ 'comment_approved' => $approved,
293
+ 'comment_agent' => 'Disqus/1.1('.DISQUS_VERSION.'):'.intval($comment->id),
294
+ 'comment_type' => '',
295
+ );
296
+ if ($comment->is_anonymous) {
297
+ $commentdata['comment_author'] = $comment->anonymous_author->name;
298
+ $commentdata['comment_author_email'] = $comment->anonymous_author->email;
299
+ $commentdata['comment_author_url'] = $comment->anonymous_author->url;
300
+ $commentdata['comment_author_IP'] = $comment->anonymous_author->ip_address;
301
+ } else {
302
+ $commentdata['comment_author'] = $comment->author->display_name;
303
+ $commentdata['comment_author_email'] = $comment->author->email;
304
+ $commentdata['comment_author_url'] = $comment->author->url;
305
+ $commentdata['comment_author_IP'] = $comment->author->ip_address;
306
+ }
307
+ $commentdata = wp_filter_comment($commentdata);
308
+ if ($comment->parent_post) {
309
+ $parent_id = $wpdb->get_var($wpdb->prepare( "SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = 'dsq_post_id' AND meta_value = %s LIMIT 1", $comment->parent_post));
310
+ if ($parent_id) {
311
+ $commentdata['comment_parent'] = $parent_id;
312
+ }
313
+ }
314
+
315
+ // due to a race condition we need to test again for coment existance
316
+ if ($wpdb->get_row($wpdb->prepare( "SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = 'dsq_post_id' AND meta_value = %s LIMIT 1", $comment->id))) {
317
+ // already exists
318
+ if (DISQUS_DEBUG) {
319
+ echo "skipped {$comment->id}: comment already exists (second check)\n";
320
+ }
321
+ continue;
322
+ }
323
+
324
+ $commentdata['comment_ID'] = wp_insert_comment($commentdata);
325
+ if (DISQUS_DEBUG) {
326
+ echo "inserted {$comment->id}: id is {$commentdata[comment_ID]}\n";
327
+ }
328
+ }
329
+ if (!$commentdata['comment_parent'] && $comment->parent_post) {
330
+ $parent_id = $wpdb->get_var($wpdb->prepare( "SELECT comment_id FROM $wpdb->commentmeta WHERE meta_key = 'dsq_post_id' AND meta_value = %s LIMIT 1", $comment->parent_post));
331
+ if ($parent_id) {
332
+ $wpdb->query($wpdb->prepare( "UPDATE $wpdb->comments SET comment_parent = %s WHERE comment_id = %s", $parent_id, $commentdata['comment_ID']));
333
+ if (DISQUS_DEBUG) {
334
+ echo "updated {$comment->id}: comment_parent changed to {$parent_id}\n";
335
+ }
336
+
337
+ }
338
+ }
339
+ $comment_id = $commentdata['comment_ID'];
340
+ update_comment_meta($comment_id, 'dsq_parent_post_id', $comment->parent_post);
341
+ update_comment_meta($comment_id, 'dsq_post_id', $comment->id);
342
+ }
343
+ unset($comment);
344
+
345
+ if( isset($_POST['dsq_api_key']) && $_POST['dsq_api_key'] == get_option('disqus_api_key') ) {
346
+ if( isset($_GET['dsq_sync_action']) && isset($_GET['dsq_sync_comment_id']) ) {
347
+ $comment_parts = explode('=', $_GET['dsq_sync_comment_id']);
348
+
349
+ if (!($comment_id = intval($comment_parts[1])) > 0) {
350
+ return;
351
+ }
352
+
353
+ if( 'wp_id' != $comment_parts[0] ) {
354
+ $comment_id = $wpdb->get_var($wpdb->prepare('SELECT comment_ID FROM ' . $wpdb->comments . ' WHERE comment_post_ID = %d AND comment_agent LIKE %s', intval($post->ID), 'Disqus/1.0:' . $comment_id));
355
+ }
356
+
357
+ switch( $_GET['dsq_sync_action'] ) {
358
+ case 'mark_spam':
359
+ wp_set_comment_status($comment_id, 'spam');
360
+ echo "<!-- dsq_sync: wp_set_comment_status($comment_id, 'spam') -->";
361
+ break;
362
+ case 'mark_approved':
363
+ wp_set_comment_status($comment_id, 'approve');
364
+ echo "<!-- dsq_sync: wp_set_comment_status($comment_id, 'approve') -->";
365
+ break;
366
+ case 'mark_killed':
367
+ wp_set_comment_status($comment_id, 'hold');
368
+ echo "<!-- dsq_sync: wp_set_comment_status($comment_id, 'hold') -->";
369
+ break;
370
+ }
371
+ }
372
+ }
373
  }
374
 
375
  function dsq_request_handler() {
376
+ global $dsq_response;
377
+ global $dsq_api;
378
+ global $post;
379
+ global $wpdb;
380
+
381
+ if (!empty($_GET['cf_action'])) {
382
+ switch ($_GET['cf_action']) {
383
+ case 'sync_comments':
384
+ if( !( $post_id = $_GET['post_id'] ) ) {
385
+ header("HTTP/1.0 400 Bad Request");
386
+ die();
387
+ }
388
+ // schedule the event for 30 seconds from now in case they
389
+ // happen to make a quick post
390
+ if (DISQUS_DEBUG) {
391
+ dsq_sync_post($post_id);
392
+ $response = dsq_sync_forum();
393
+ if (!$response) {
394
+ die('// error: '.$dsq_api->get_last_error());
395
+ } else {
396
+ list($last_comment_id, $comments) = $response;
397
+ die('// synced '.$comments.' comments');
398
+ }
399
+ } else {
400
+ wp_schedule_single_event(time(), 'dsq_sync_post', array($post_id));
401
+ wp_schedule_single_event(time(), 'dsq_sync_forum');
402
+ die('// sync scheduled');
403
+ }
404
+ break;
405
+ case 'export_comments':
406
+ if (current_user_can('manage_options') && DISQUS_CAN_EXPORT) {
407
+ $timestamp = intval($_GET['timestamp']);
408
+ $post_id = intval($_GET['post_id']);
409
+ global $wpdb, $dsq_api;
410
+ $post = $wpdb->get_results($wpdb->prepare("
411
+ SELECT *
412
+ FROM $wpdb->posts
413
+ WHERE post_type != 'revision'
414
+ AND post_status = 'publish'
415
+ AND comment_count > 0
416
+ AND ID > %d
417
+ ORDER BY ID ASC
418
+ LIMIT 1
419
+ ", $post_id));
420
+ $post = $post[0];
421
+ $post_id = $post->ID;
422
+ $max_post_id = $wpdb->get_var($wpdb->prepare("
423
+ SELECT MAX(ID)
424
+ FROM $wpdb->posts
425
+ WHERE post_type != 'revision'
426
+ AND post_status = 'publish'
427
+ AND comment_count > 0
428
+ ", $post_id));
429
+ $eof = (int)($post_id == $max_post_id);
430
+ if ($eof) {
431
+ $status = 'complete';
432
+ $msg = 'Your comments have been sent to Disqus and queued for import!<br/><a href="'.DISQUS_IMPORTER_URL.'" target="_blank">See the status of your import at Disqus</a>';
433
+ }
434
+ else {
435
+ $status = 'partial';
436
+ $msg = dsq_i('Processed comments on post #%s&hellip;', $post_id);
437
+ }
438
+ $result = 'fail';
439
+ $response = null;
440
+ if ($post) {
441
+ require_once(dirname(__FILE__) . '/export.php');
442
+ $wxr = dsq_export_wp($post);
443
+ $response = $dsq_api->import_wordpress_comments($wxr, $timestamp, $eof);
444
+ if (!($response['group_id'] > 0)) {
445
+ $result = 'fail';
446
+ $msg = '<p class="status dsq-export-fail">'. dsq_i('Sorry, something unexpected happened with the export. Please <a href="#" id="dsq_export_retry">try again</a></p><p>If your API key has changed, you may need to reinstall Disqus (deactivate the plugin and then reactivate it). If you are still having issues, refer to the <a href="%s" onclick="window.open(this.href); return false">WordPress help page</a>.', 'http://disqus.com/help/wordpress'). '</p>';
447
+ $response = $dsq_api->get_last_error();
448
+ }
449
+ else {
450
+ if ($eof) {
451
+ $msg = dsq_i('Your comments have been sent to Disqus and queued for import!<br/><a href="%s" target="_blank">See the status of your import at Disqus</a>', $response['link']);
452
+
453
+ }
454
+ $result = 'success';
455
+ }
456
+ }
 
 
 
 
 
 
 
457
  // send AJAX response
458
+ $response = compact('result', 'timestamp', 'status', 'post_id', 'msg', 'eof', 'response');
459
+ header('Content-type: text/javascript');
460
+ echo cf_json_encode($response);
461
+ die();
462
+ }
463
+ break;
464
+ case 'import_comments':
465
+ if (current_user_can('manage_options')) {
466
+ if (!isset($_GET['last_comment_id'])) $last_comment_id = false;
467
+ else $last_comment_id = $_GET['last_comment_id'];
468
+
469
+ if ($_GET['wipe'] == '1') {
470
+ $wpdb->query("DELETE FROM `".$wpdb->prefix."commentmeta` WHERE meta_key IN ('dsq_post_id', 'dsq_parent_post_id')");
471
+ $wpdb->query("DELETE FROM `".$wpdb->prefix."comments` WHERE comment_agent LIKE 'Disqus/%%'");
472
+ }
473
+
474
+ ob_start();
475
+ $response = dsq_sync_forum($last_comment_id);
476
+ $debug = ob_get_clean();
477
+ if (!$response) {
478
+ $status = 'error';
479
+ $result = 'fail';
480
+ $error = $dsq_api->get_last_error();
481
+ $msg = '<p class="status dsq-export-fail">'.dsq_i('There was an error downloading your comments from Disqus.').'<br/>'.htmlspecialchars($error).'</p>';
482
+ } else {
483
+ list($comments, $last_comment_id) = $response;
484
+ if (!$comments) {
485
+ $status = 'complete';
486
+ $msg = dsq_i('Your comments have been downloaded from Disqus and saved in your local database.');
487
+ } else {
488
+ $status = 'partial';
489
+ $msg = dsq_i('Import in progress (last post id: %s) &hellip;', $last_comment_id);
490
+ }
491
+ $result = 'success';
492
+ }
493
+ $debug = explode("\n", $debug);
494
+ $response = compact('result', 'status', 'comments', 'msg', 'last_comment_id', 'debug');
495
+ header('Content-type: text/javascript');
496
+ echo cf_json_encode($response);
497
+ die();
498
+ }
499
+ break;
500
+ }
501
+ }
502
  }
503
 
504
  add_action('init', 'dsq_request_handler');
505
 
506
  function dsq_sync_post($post_id) {
507
+ global $dsq_api, $wpdb;
508
+
509
+ $post = get_post($post_id);
510
 
511
+ // Call update_thread to ensure our permalink is up to date
512
+ dsq_update_permalink($post);
513
  }
514
 
515
  add_action('dsq_sync_post', 'dsq_sync_post');
516
 
517
  function dsq_sync_forum($last_comment_id=false) {
518
+ global $dsq_api, $wpdb;
519
+
520
+ if ($last_comment_id === false) {
521
+ $last_comment_id = get_option('disqus_last_comment_id');
522
+ if (!$last_comment_id) {
523
+ $last_comment_id = 0;
524
+ }
525
+ }
526
+ if ($last_comment_id) {
527
+ $last_comment_id++;
528
+ }
529
+
530
+ //$last_comment_id = 0;
531
+
532
+ // Pull comments from API
533
+ $dsq_response = $dsq_api->get_forum_posts($last_comment_id);
534
+ if( $dsq_response < 0 || $dsq_response === false ) {
535
+ return false;
536
+ }
537
+
538
+ // Sync comments with database.
539
+ dsq_sync_comments($dsq_response);
540
+ $total = 0;
541
+ if ($dsq_response) {
542
+ foreach ($dsq_response as $comment) {
543
+ $total += 1;
544
+ if ($comment->id > $last_comment_id) $last_comment_id = $comment->id;
545
+ }
546
+ if ($last_comment_id > get_option('disqus_last_comment_id')) {
547
+ update_option('disqus_last_comment_id', $last_comment_id);
548
+ }
549
+ }
550
+ unset($comment);
551
+ return array($total, $last_comment_id);
552
  }
553
 
554
  add_action('dsq_sync_forum', 'dsq_sync_forum');
555
 
556
+ function dsq_update_permalink(&$post) {
557
+ global $dsq_api;
558
+
559
+ $response = $dsq_api->api->update_thread(null, array(
560
+ 'thread_identifier' => dsq_identifier_for_post($post),
561
+ 'title' => dsq_title_for_post($post),
562
+ 'url' => dsq_link_for_post($post)
563
+ ));
564
+
565
+ update_post_meta($post->ID, 'dsq_thread_id', $response->id);
566
 
567
+ return $response;
 
 
 
 
 
 
 
 
568
  }
569
 
570
  /**
573
 
574
  if (!function_exists ( '_wp_specialchars' ) ) {
575
  function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
576
+ $string = (string) $string;
577
+
578
+ if ( 0 === strlen( $string ) ) {
579
+ return '';
580
+ }
581
+
582
+ // Don't bother if there are no specialchars - saves some processing
583
+ if ( !preg_match( '/[&<>"\']/', $string ) ) {
584
+ return $string;
585
+ }
586
+
587
+ // Account for the previous behaviour of the function when the $quote_style is not an accepted value
588
+ if ( empty( $quote_style ) ) {
589
+ $quote_style = ENT_NOQUOTES;
590
+ } elseif ( !in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) ) {
591
+ $quote_style = ENT_QUOTES;
592
+ }
593
+
594
+ // Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
595
+ if ( !$charset ) {
596
+ static $_charset;
597
+ if ( !isset( $_charset ) ) {
598
+ $alloptions = wp_load_alloptions();
599
+ $_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
600
+ }
601
+ $charset = $_charset;
602
+ }
603
+ if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) ) {
604
+ $charset = 'UTF-8';
605
+ }
606
+
607
+ $_quote_style = $quote_style;
608
+
609
+ if ( $quote_style === 'double' ) {
610
+ $quote_style = ENT_COMPAT;
611
+ $_quote_style = ENT_COMPAT;
612
+ } elseif ( $quote_style === 'single' ) {
613
+ $quote_style = ENT_NOQUOTES;
614
+ }
615
+
616
+ // Handle double encoding ourselves
617
+ if ( !$double_encode ) {
618
+ $string = wp_specialchars_decode( $string, $_quote_style );
619
+ $string = preg_replace( '/&(#?x?[0-9a-z]+);/i', '|wp_entity|$1|/wp_entity|', $string );
620
+ }
621
+
622
+ $string = @htmlspecialchars( $string, $quote_style, $charset );
623
+
624
+ // Handle double encoding ourselves
625
+ if ( !$double_encode ) {
626
+ $string = str_replace( array( '|wp_entity|', '|/wp_entity|' ), array( '&', ';' ), $string );
627
+ }
628
+
629
+ // Backwards compatibility
630
+ if ( 'single' === $_quote_style ) {
631
+ $string = str_replace( "'", '&#039;', $string );
632
+ }
633
+
634
+ return $string;
635
  }
636
  }
637
 
638
  if (!function_exists ( 'wp_check_invalid_utf8' ) ) {
639
  function wp_check_invalid_utf8( $string, $strip = false ) {
640
+ $string = (string) $string;
641
 
642
+ if ( 0 === strlen( $string ) ) {
643
+ return '';
644
+ }
645
 
646
+ // Store the site charset as a static to avoid multiple calls to get_option()
647
+ static $is_utf8;
648
+ if ( !isset( $is_utf8 ) ) {
649
+ $is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) );
650
+ }
651
+ if ( !$is_utf8 ) {
652
+ return $string;
653
+ }
654
 
655
+ // Check for support for utf8 in the installed PCRE library once and store the result in a static
656
+ static $utf8_pcre;
657
+ if ( !isset( $utf8_pcre ) ) {
658
+ $utf8_pcre = @preg_match( '/^./u', 'a' );
659
+ }
660
+ // We can't demand utf8 in the PCRE installation, so just return the string in those cases
661
+ if ( !$utf8_pcre ) {
662
+ return $string;
663
+ }
664
 
665
+ // preg_match fails when it encounters invalid UTF8 in $string
666
+ if ( 1 === @preg_match( '/^./us', $string ) ) {
667
+ return $string;
668
+ }
669
 
670
+ // Attempt to strip the bad chars if requested (not recommended)
671
+ if ( $strip && function_exists( 'iconv' ) ) {
672
+ return iconv( 'utf-8', 'utf-8', $string );
673
+ }
674
 
675
+ return '';
676
  }
677
  }
678
 
679
  if (!function_exists ( 'esc_html' ) ) {
680
  function esc_html( $text ) {
681
+ $safe_text = wp_check_invalid_utf8( $text );
682
+ $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
683
+ return apply_filters( 'esc_html', $safe_text, $text );
684
  }
685
  }
686
 
687
  if (!function_exists ( 'esc_attr' ) ) {
688
  function esc_attr( $text ) {
689
+ $safe_text = wp_check_invalid_utf8( $text );
690
+ $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES );
691
+ return apply_filters( 'attribute_escape', $safe_text, $text );
692
  }
693
  }
694
 
696
  * Filters/Actions
697
  */
698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
699
  // ugly global hack for comments closing
700
  $EMBED = false;
701
  function dsq_comments_template($value) {
702
+ global $EMBED;
703
+ global $post;
704
+ global $comments;
705
 
706
+ if ( !( is_singular() && ( have_comments() || 'open' == $post->comment_status ) ) ) {
707
+ return;
708
+ }
709
 
710
+ if ( !dsq_is_installed() || !dsq_can_replace() ) {
711
+ return $value;
712
+ }
713
 
714
+ // TODO: If a disqus-comments.php is found in the current template's
715
+ // path, use that instead of the default bundled comments.php
716
+ //return TEMPLATEPATH . '/disqus-comments.php';
717
+ $EMBED = true;
718
+ return dirname(__FILE__) . '/comments.php';
719
  }
720
 
721
  // Mark entries in index to replace comments link.
722
+ // As of WordPress 3.1 this is required to return a numerical value
723
  function dsq_comments_number($count) {
724
  global $post;
725
 
726
+ return $count;
 
 
 
 
727
  }
728
 
729
  function dsq_comments_text($comment_text) {
730
+ global $post;
731
 
732
+ if ( dsq_can_replace() ) {
733
+ return '<span class="dsq-postid" rel="'.htmlspecialchars(dsq_identifier_for_post($post)).'">'.$comment_text.'</span>';
734
+ } else {
735
+ return $comment_text;
736
+ }
737
  }
738
 
739
  function dsq_bloginfo_url($url) {
740
+ if ( get_feed_link('comments_rss2') == $url && dsq_can_replace() ) {
741
+ return 'http://' . strtolower(get_option('disqus_forum_url')) . '.' . DISQUS_DOMAIN . DISQUS_RSS_PATH;
742
+ } else {
743
+ return $url;
744
+ }
745
  }
746
 
747
  function dsq_plugin_action_links($links, $file) {
748
+ $plugin_file = basename(__FILE__);
749
+ if (basename($file) == $plugin_file) {
750
+ $settings_link = '<a href="edit-comments.php?page=disqus#adv">'.__('Settings', 'disqus-comment-system').'</a>';
751
+ array_unshift($links, $settings_link);
752
+ }
753
+ return $links;
754
  }
755
  add_filter('plugin_action_links', 'dsq_plugin_action_links', 10, 2);
756
 
757
+ /**
758
  * Hide the default comment form to stop spammers by marking all comments
759
  * as closed.
760
  */
761
  function dsq_comments_open($open, $post_id=null) {
762
+ global $EMBED;
763
+ if ($EMBED) return false;
764
+ return $open;
765
  }
766
  add_filter('comments_open', 'dsq_comments_open');
767
 
768
  // Always add Disqus management page to the admin menu
769
  function dsq_add_pages() {
770
+ add_submenu_page(
771
+ 'edit-comments.php',
772
+ 'Disqus',
773
+ 'Disqus',
774
+ 'moderate_comments',
775
+ 'disqus',
776
+ 'dsq_manage'
777
+ );
778
  }
779
  add_action('admin_menu', 'dsq_add_pages', 10);
780
 
781
  // a little jQuery goodness to get comments menu working as desired
782
+ function dsq_menu_admin_head() {
783
  ?>
784
  <script type="text/javascript">
785
  jQuery(function($) {
786
  // fix menu
787
+ var mc = $('#menu-comments');
788
+ mc.find('a.wp-has-submenu').attr('href', 'edit-comments.php?page=disqus').end().find('.wp-submenu li:has(a[href=edit-comments.php?page=disqus])').prependTo(mc.find('.wp-submenu ul'));
789
  });
790
  </script>
791
  <?php
792
  }
793
+ add_action('admin_head', 'dsq_menu_admin_head');
794
 
795
  // only active on dashboard
796
  function dsq_dash_comment_counts() {
797
+ global $wpdb;
798
  // taken from wp-includes/comment.php - WP 2.8.5
799
+ $count = $wpdb->get_results("
800
+ SELECT comment_approved, COUNT( * ) AS num_comments
801
+ FROM {$wpdb->comments}
802
+ WHERE comment_type != 'trackback'
803
+ AND comment_type != 'pingback'
804
+ GROUP BY comment_approved
805
+ ", ARRAY_A );
806
+ $total = 0;
807
+ $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam');
808
+ $known_types = array_keys( $approved );
809
+ foreach( (array) $count as $row_num => $row ) {
810
+ $total += $row['num_comments'];
811
+ if ( in_array( $row['comment_approved'], $known_types ) )
812
+ $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
813
+ }
814
+
815
+ $stats['total_comments'] = $total;
816
+ foreach ( $approved as $key ) {
817
+ if ( empty($stats[$key]) )
818
+ $stats[$key] = 0;
819
+ }
820
+ $stats = (object) $stats;
821
  ?>
822
  <style type="text/css">
823
  #dashboard_right_now .inside,
824
  #dashboard_recent_comments div.trackback {
825
+ display: none;
826
  }
827
  </style>
828
  <script type="text/javascript">
829
  jQuery(function($) {
830
+ $('#dashboard_right_now').find('.b-comments a').html('<?php echo $stats->total_comments; ?>').end().find('.b_approved a').html('<?php echo $stats->approved; ?>').end().find('.b-waiting a').html('<?php echo $stats->moderated; ?>').end().find('.b-spam a').html('<?php echo $stats->spam; ?>').end().find('.inside').slideDown();
831
+ $('#dashboard_recent_comments div.trackback').remove();
832
+ $('#dashboard_right_now .inside table td.last a, #dashboard_recent_comments .inside .textright a.button').attr('href', 'edit-comments.php?page=disqus');
833
  });
834
  </script>
835
  <?php
836
  }
837
  function dsq_wp_dashboard_setup() {
838
+ add_action('admin_head', 'dsq_dash_comment_counts');
839
  }
840
  add_action('wp_dashboard_setup', 'dsq_wp_dashboard_setup');
841
 
842
  function dsq_manage() {
843
+ if (dsq_does_need_update() && isset($_POST['upgrade'])) {
844
+ dsq_install();
845
+ }
846
+
847
+ if (dsq_does_need_update() && isset($_POST['uninstall'])) {
848
+ include_once(dirname(__FILE__) . '/upgrade.php');
849
+ } else {
850
+ include_once(dirname(__FILE__) . '/manage.php');
851
+ }
852
  }
853
 
854
  function dsq_admin_head() {
855
+ if (isset($_GET['page']) && $_GET['page'] == 'disqus') {
856
  ?>
857
+ <link rel='stylesheet' href='<?php echo DSQ_PLUGIN_URL; ?>/media/styles/manage.css' type='text/css' />
858
  <style type="text/css">
859
  .dsq-importing, .dsq-imported, .dsq-import-fail, .dsq-exporting, .dsq-exported, .dsq-export-fail {
860
+ background: url(<?php echo admin_url('images/loading.gif'); ?>) left center no-repeat;
861
+ line-height: 16px;
862
+ padding-left: 20px;
863
  }
864
  p.status {
865
+ padding-top: 0;
866
+ padding-bottom: 0;
867
+ margin: 0;
868
  }
869
  .dsq-imported, .dsq-exported {
870
+ background: url(<?php echo admin_url('images/yes.png'); ?>) left center no-repeat;
871
  }
872
  .dsq-import-fail, .dsq-export-fail {
873
+ background: url(<?php echo admin_url('images/no.png'); ?>) left center no-repeat;
874
  }
875
  </style>
876
  <script type="text/javascript">
877
  jQuery(function($) {
878
+ $('#dsq-tabs li').click(function() {
879
+ $('#dsq-tabs li.selected').removeClass('selected');
880
+ $(this).addClass('selected');
881
+ $('.dsq-main, .dsq-advanced').hide();
882
+ $('.' + $(this).attr('rel')).show();
883
+ });
884
+ if (location.href.indexOf('#adv') != -1) {
885
+ $('#dsq-tab-advanced').click();
886
+ }
887
+ dsq_fire_export();
888
+ dsq_fire_import();
889
  });
890
  dsq_fire_export = function() {
891
+ var $ = jQuery;
892
+ $('#dsq_export a.button, #dsq_export_retry').unbind().click(function() {
893
+ $('#dsq_export').html('<p class="status"></p>');
894
+ $('#dsq_export .status').removeClass('dsq-export-fail').addClass('dsq-exporting').html('Processing...');
895
+ dsq_export_comments();
896
+ return false;
897
+ });
898
  }
899
  dsq_export_comments = function() {
900
+ var $ = jQuery;
901
+ var status = $('#dsq_export .status');
902
+ var export_info = (status.attr('rel') || '0|' + (new Date().getTime()/1000)).split('|');
903
+ $.get(
904
+ '<?php echo admin_url('index.php'); ?>',
905
+ {
906
+ cf_action: 'export_comments',
907
+ post_id: export_info[0],
908
+ timestamp: export_info[1]
909
+ },
910
+ function(response) {
911
+ switch (response.result) {
912
+ case 'success':
913
+ status.html(response.msg).attr('rel', response.post_id + '|' + response.timestamp);
914
+ switch (response.status) {
915
+ case 'partial':
916
+ dsq_export_comments();
917
+ break;
918
+ case 'complete':
919
+ status.removeClass('dsq-exporting').addClass('dsq-exported');
920
+ break;
921
+ }
922
+ break;
923
+ case 'fail':
924
+ status.parent().html(response.msg);
925
+ dsq_fire_export();
926
+ break;
927
+ }
928
+ },
929
+ 'json'
930
+ );
931
  }
932
  dsq_fire_import = function() {
933
+ var $ = jQuery;
934
+ $('#dsq_import a.button, #dsq_import_retry').unbind().click(function() {
935
+ var wipe = $('#dsq_import_wipe').is(':checked');
936
+ $('#dsq_import').html('<p class="status"></p>');
937
+ $('#dsq_import .status').removeClass('dsq-import-fail').addClass('dsq-importing').html('Processing...');
938
+ dsq_import_comments(wipe);
939
+ return false;
940
+ });
941
  }
942
  dsq_import_comments = function(wipe) {
943
+ var $ = jQuery;
944
+ var status = $('#dsq_import .status');
945
+ var last_comment_id = status.attr('rel') || '0';
946
+ $.get(
947
+ '<?php echo admin_url('index.php'); ?>',
948
+ {
949
+ cf_action: 'import_comments',
950
+ last_comment_id: last_comment_id,
951
+ wipe: (wipe ? 1 : 0)
952
+ },
953
+ function(response) {
954
+ switch (response.result) {
955
+ case 'success':
956
+ status.html(response.msg).attr('rel', response.last_comment_id);
957
+ switch (response.status) {
958
+ case 'partial':
959
+ dsq_import_comments();
960
+ break;
961
+ case 'complete':
962
+ status.removeClass('dsq-importing').addClass('dsq-imported');
963
+ break;
964
+ }
965
+ break;
966
+ case 'fail':
967
+ status.parent().html(response.msg);
968
+ dsq_fire_import();
969
+ break;
970
+ }
971
+ },
972
+ 'json'
973
+ );
974
  }
975
  </script>
976
  <?php
977
  // HACK: Our own styles for older versions of WordPress.
978
+ global $wp_version;
979
+ if ( version_compare($wp_version, '2.5', '<') ) {
980
+ echo "<link rel='stylesheet' href='" . DSQ_PLUGIN_URL . "/media/styles/manage-pre25.css' type='text/css' />";
981
+ }
982
+ }
983
  }
984
  add_action('admin_head', 'dsq_admin_head');
985
 
986
  function dsq_warning() {
987
+ $page = (isset($_GET['page']) ? $_GET['page'] : null);
988
+ if ( !get_option('disqus_forum_url') && !isset($_POST['forum_url']) && $page != 'disqus' ) {
989
+ dsq_manage_dialog('You must <a href="edit-comments.php?page=disqus">configure the plugin</a> to enable Disqus Comments.', true);
990
+ }
991
 
992
+ if ( !dsq_is_installed() && $page != 'disqus' && !empty($_GET['step']) && !isset($_POST['uninstall']) ) {
993
+ dsq_manage_dialog('Disqus Comments has not yet been configured. (<a href="edit-comments.php?page=disqus">Click here to configure</a>)');
994
+ }
995
  }
996
 
997
  /**
999
  * the disqus domain and supports variable interpolation.
1000
  */
1001
  function dsq_i($text, $params=null) {
1002
+ if (!is_array($params))
1003
+ {
1004
+ $params = func_get_args();
1005
+ $params = array_slice($params, 1);
1006
+ }
1007
+ return vsprintf(__($text, 'disqus'), $params);
1008
  }
1009
 
1010
  // catch original query
1011
  function dsq_parse_query($query) {
1012
+ add_action('the_posts', 'dsq_add_request_post_ids', 999);
1013
  }
1014
  add_action('parse_request', 'dsq_parse_query');
1015
 
1016
  // track the original request post_ids, only run once
1017
+ function dsq_add_request_post_ids(&$posts) {
1018
+ dsq_add_query_posts($posts);
1019
+ remove_action('the_posts', 'dsq_log_request_post_ids', 999);
1020
+ return $posts;
1021
  }
1022
 
1023
+ function dsq_maybe_add_post_ids(&$posts) {
1024
+ global $DSQ_QUERY_COMMENTS;
1025
+ if ($DSQ_QUERY_COMMENTS) {
1026
+ dsq_add_query_posts($posts);
1027
+ }
1028
+ return $posts;
1029
  }
1030
  add_action('the_posts', 'dsq_maybe_add_post_ids');
1031
 
1032
+ function dsq_add_query_posts(&$posts) {
1033
+ global $DSQ_QUERY_POST_IDS;
1034
+ if (count($posts)) {
1035
+ foreach ($posts as $post) {
1036
+ $post_ids[] = intval($post->ID);
1037
+ }
1038
+ $DSQ_QUERY_POST_IDS[md5(serialize($post_ids))] = $post_ids;
1039
+ }
1040
  }
1041
 
1042
  // check to see if the posts in the loop match the original request or an explicit request, if so output the JS
1043
  function dsq_loop_end($query) {
1044
+ if ( get_option('disqus_cc_fix') == '1' || !count($query->posts) || is_single() || is_page() || is_feed() || !dsq_can_replace() ) {
1045
+ return;
1046
+ }
1047
+ global $DSQ_QUERY_POST_IDS;
1048
+ foreach ($query->posts as $post) {
1049
+ $loop_ids[] = intval($post->ID);
1050
+ }
1051
+ $posts_key = md5(serialize($loop_ids));
1052
+ if (isset($DSQ_QUERY_POST_IDS[$posts_key])) {
1053
+ dsq_output_loop_comment_js($DSQ_QUERY_POST_IDS[$posts_key]);
1054
+ }
1055
  }
1056
  add_action('loop_end', 'dsq_loop_end');
1057
 
1060
  $_HAS_COUNTS = false;
1061
 
1062
  function dsq_output_loop_comment_js($post_ids = null) {
1063
+ global $_HAS_COUNTS;
1064
+ if ($_HAS_COUNTS) return;
1065
+ $_HAS_COUNTS = true;
1066
+ if (count($post_ids)) {
1067
  ?>
1068
+ <script type="text/javascript">
1069
+ // <![CDATA[
1070
+ var disqus_shortname = '<?php echo strtolower(get_option('disqus_forum_url')); ?>';
1071
+ var disqus_domain = '<?php echo DISQUS_DOMAIN; ?>';
1072
+ (function () {
1073
+ var nodes = document.getElementsByTagName('span');
1074
+ for (var i = 0, url; i < nodes.length; i++) {
1075
+ if (nodes[i].className.indexOf('dsq-postid') != -1) {
1076
+ nodes[i].parentNode.setAttribute('data-disqus-identifier', nodes[i].getAttribute('rel'));
1077
+ url = nodes[i].parentNode.href.split('#', 1);
1078
+ if (url.length == 1) { url = url[0]; }
1079
+ else { url = url[1]; }
1080
+ nodes[i].parentNode.href = url + '#disqus_thread';
1081
+ }
1082
+ }
1083
+ var s = document.createElement('script'); s.async = true;
1084
+ s.type = 'text/javascript';
1085
+ s.src = 'http://' + disqus_domain + '/forums/' + disqus_shortname + '/count.js';
1086
+ (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
1087
+ }());
1088
+ //]]>
1089
+ </script>
1090
  <?php
1091
+ }
1092
  }
1093
 
1094
  function dsq_output_footer_comment_js() {
1095
+ if (!dsq_can_replace()) return;
1096
+ if (get_option('disqus_cc_fix') != '1') return;
1097
  ?>
1098
+ <script type="text/javascript">
1099
+ // <![CDATA[
1100
+ var disqus_shortname = '<?php echo strtolower(get_option('disqus_forum_url')); ?>';
1101
+ var disqus_domain = '<?php echo DISQUS_DOMAIN; ?>';
1102
+ (function () {
1103
+ var nodes = document.getElementsByTagName('span');
1104
+ for (var i = 0, url; i < nodes.length; i++) {
1105
+ if (nodes[i].className.indexOf('dsq-postid') != -1) {
1106
+ nodes[i].parentNode.setAttribute('data-disqus-identifier', nodes[i].getAttribute('rel'));
1107
+ url = nodes[i].parentNode.href.split('#', 1);
1108
+ if (url.length == 1) url = url[0];
1109
+ else url = url[1]
1110
+ nodes[i].parentNode.href = url + '#disqus_thread';
1111
+ }
1112
+ }
1113
+ var s = document.createElement('script'); s.async = true;
1114
+ s.type = 'text/javascript';
1115
+ s.src = 'http://' + disqus_domain + '/forums/' + disqus_shortname + '/count.js';
1116
+ (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
1117
+ }());
1118
+ //]]>
1119
+ </script>
1120
  <?php
 
1121
  }
1122
  add_action('wp_footer', 'dsq_output_footer_comment_js');
1123
 
1127
 
1128
  function dsq_prev_permalink($post_id) {
1129
  // if post not published, return
1130
+ $post = &get_post($post_id);
1131
+ if ($post->post_status != 'publish') {
1132
+ return;
1133
+ }
1134
+ global $dsq_prev_permalinks;
1135
+ $dsq_prev_permalinks['post_'.$post_id] = get_permalink($post_id);
1136
  }
1137
  add_action('pre_post_update', 'dsq_prev_permalink');
1138
 
1139
  function dsq_check_permalink($post_id) {
1140
+ global $dsq_prev_permalinks;
1141
+ if (!empty($dsq_prev_permalinks['post_'.$post_id]) && $dsq_prev_permalinks['post_'.$post_id] != get_permalink($post_id)) {
1142
+ $post = get_post($post_id);
1143
+ dsq_update_permalink($post);
1144
+ }
1145
  }
1146
  add_action('edit_post', 'dsq_check_permalink');
1147
 
1158
  * Checks if json_encode is not available and defines json_encode
1159
  * to use php_json_encode in its stead
1160
  * Works on iteratable objects as well - stdClass is iteratable, so all WP objects are gonna be iteratable
1161
+ */
1162
  if(!function_exists('cf_json_encode')) {
1163
+ function cf_json_encode($data) {
1164
  // json_encode is sending an application/x-javascript header on Joyent servers
1165
  // for some unknown reason.
1166
+ // if(function_exists('json_encode')) { return json_encode($data); }
1167
+ // else { return cfjson_encode($data); }
1168
+ return cfjson_encode($data);
1169
+ }
1170
+
1171
+ function cfjson_encode_string($str) {
1172
+ if(is_bool($str)) {
1173
+ return $str ? 'true' : 'false';
1174
+ }
1175
+
1176
+ return str_replace(
1177
+ array(
1178
+ '"'
1179
+ , '/'
1180
+ , "\n"
1181
+ , "\r"
1182
+ )
1183
+ , array(
1184
+ '\"'
1185
+ , '\/'
1186
+ , '\n'
1187
+ , '\r'
1188
+ )
1189
+ , $str
1190
+ );
1191
+ }
1192
+
1193
+ function cfjson_encode($arr) {
1194
+ $json_str = '';
1195
+ if (is_array($arr)) {
1196
+ $pure_array = true;
1197
+ $array_length = count($arr);
1198
+ for ( $i = 0; $i < $array_length ; $i++) {
1199
+ if (!isset($arr[$i])) {
1200
+ $pure_array = false;
1201
+ break;
1202
+ }
1203
+ }
1204
+ if ($pure_array) {
1205
+ $json_str = '[';
1206
+ $temp = array();
1207
+ for ($i=0; $i < $array_length; $i++) {
1208
+ $temp[] = sprintf("%s", cfjson_encode($arr[$i]));
1209
+ }
1210
+ $json_str .= implode(',', $temp);
1211
+ $json_str .="]";
1212
+ }
1213
+ else {
1214
+ $json_str = '{';
1215
+ $temp = array();
1216
+ foreach ($arr as $key => $value) {
1217
+ $temp[] = sprintf("\"%s\":%s", $key, cfjson_encode($value));
1218
+ }
1219
+ $json_str .= implode(',', $temp);
1220
+ $json_str .= '}';
1221
+ }
1222
+ }
1223
+ else if (is_object($arr)) {
1224
+ $json_str = '{';
1225
+ $temp = array();
1226
+ foreach ($arr as $k => $v) {
1227
+ $temp[] = '"'.$k.'":'.cfjson_encode($v);
1228
+ }
1229
+ $json_str .= implode(',', $temp);
1230
+ $json_str .= '}';
1231
+ }
1232
+ else if (is_string($arr)) {
1233
+ $json_str = '"'. cfjson_encode_string($arr) . '"';
1234
+ }
1235
+ else if (is_numeric($arr)) {
1236
+ $json_str = $arr;
1237
+ }
1238
+ else if (is_bool($arr)) {
1239
+ $json_str = $arr ? 'true' : 'false';
1240
+ }
1241
+ else {
1242
+ $json_str = '"'. cfjson_encode_string($arr) . '"';
1243
+ }
1244
+ return $json_str;
1245
+ }
1246
  }
1247
 
1248
  // Single Sign-on Integration
1249
 
1250
  function dsq_sso() {
1251
+ if ($key = get_option('disqus_partner_key')) {
1252
+ // use old style SSO
1253
+ $new = false;
1254
+ } elseif (($key = get_option('disqus_secret_key')) && ($public = get_option('disqus_public_key'))) {
1255
+ // use new style SSO
1256
+ $new = true;
1257
+ } else {
1258
+ // sso is not configured
1259
+ return array();
1260
+ }
1261
+ global $current_user, $dsq_api;
1262
+ get_currentuserinfo();
1263
+ if ($current_user->ID) {
1264
+ $avatar_tag = get_avatar($current_user->ID);
1265
+ $avatar_data = array();
1266
+ preg_match('/(src)=((\'|")[^(\'|")]*(\'|"))/i', $avatar_tag, $avatar_data);
1267
+ $avatar = str_replace(array('"', "'"), '', $avatar_data[2]);
1268
+ $user_data = array(
1269
+ 'username' => $current_user->display_name,
1270
+ 'id' => $current_user->ID,
1271
+ 'avatar' => $avatar,
1272
+ 'email' => $current_user->user_email,
1273
+ );
1274
+ }
1275
+ else {
1276
+ $user_data = array();
1277
+ }
1278
+ $user_data = base64_encode(cf_json_encode($user_data));
1279
+ $time = time();
1280
+ $hmac = dsq_hmacsha1($user_data.' '.$time, $key);
1281
+
1282
+ $payload = $user_data.' '.$hmac.' '.$time;
1283
+
1284
+ if ($new) {
1285
+ return array('remote_auth_s3'=>$payload, 'api_key'=>$public);
1286
+ } else {
1287
+ return array('remote_auth_s2'=>$payload);
1288
+ }
1289
  }
1290
 
1291
  // from: http://www.php.net/manual/en/function.sha1.php#39492
1312
  }
1313
 
1314
  function dsq_identifier_for_post($post) {
1315
+ return $post->ID . ' ' . $post->guid;
1316
  }
1317
 
1318
  function dsq_title_for_post($post) {
1319
+ $title = get_the_title($post);
1320
+ $title = strip_tags($title, DISQUS_ALLOWED_HTML);
1321
+ return $title;
1322
  }
1323
 
1324
  function dsq_link_for_post($post) {
1325
+ return get_permalink($post);
1326
  }
1327
 
1328
  function dsq_does_need_update() {
1329
+ $version = (string)get_option('disqus_version');
1330
+ if (empty($version)) {
1331
+ $version = '0';
1332
+ }
1333
+
1334
+ if (version_compare($version, '2.49', '<')) {
1335
+ return true;
1336
+ }
1337
+
1338
+ return false;
1339
  }
1340
 
1341
  function dsq_install($allow_database_install=true) {
1342
+ global $wpdb, $userdata;
1343
+
1344
+ $version = (string)get_option('disqus_version');
1345
+ if (!$version) {
1346
+ $version = '0';
1347
+ }
1348
 
1349
+ if ($allow_database_install)
1350
+ {
1351
+ dsq_install_database($version);
1352
+ }
1353
 
1354
+ if (version_compare($version, DISQUS_VERSION, '=')) return;
 
 
 
 
 
1355
 
1356
+ // if this is a new install, we should not set disqus active
1357
+ if ($version == '0') {
1358
+ add_option('disqus_active', 0);
1359
+ } else {
1360
+ add_option('disqus_active', 1);
1361
+ }
1362
+
1363
+ update_option('disqus_version', DISQUS_VERSION);
1364
  }
1365
 
1366
  /**
1367
  * Initializes the database if it's not already present.
1368
  */
1369
  function dsq_install_database($version=0) {
1370
+ global $wpdb;
1371
+
1372
+ if (version_compare($version, '2.49', '<')) {
1373
+ $wpdb->query("CREATE INDEX disqus_dupecheck ON `".$wpdb->prefix."commentmeta` (meta_key, meta_value(11));");
1374
+ }
1375
+ }
1376
+ function dsq_uninstall_database($version=0) {
1377
+ if (version_compare($version, '2.49', '>=')) {
1378
+ $wpdb->query("DROP INDEX disqus_dupecheck ON `".$wpdb->prefix."commentmeta` (meta_key, meta_value(11));");
1379
+ }
1380
  }
1381
  ?>
export.php CHANGED
@@ -6,14 +6,6 @@
6
  @ini_set('memory_limit', '256M');
7
  define('WXR_VERSION', '1.0');
8
 
9
- // receives an array of posts to export
10
- function dsq_export_wp($posts) {
11
-
12
- global $wpdb;
13
-
14
- $categories = (array) get_categories('get=all');
15
- $tags = (array) get_tags('get=all');
16
-
17
  /**
18
  * {@internal Missing Short Description}}
19
  *
@@ -21,40 +13,20 @@ $tags = (array) get_tags('get=all');
21
  *
22
  * @param unknown_type $categories
23
  */
24
- function wxr_missing_parents($categories) {
25
- if ( !is_array($categories) || empty($categories) )
26
- return array();
27
-
28
- foreach ( $categories as $category )
29
- $parents[$category->term_id] = $category->parent;
30
-
31
- $parents = array_unique(array_diff($parents, array_keys($parents)));
32
 
33
- if ( $zero = array_search('0', $parents) )
34
- unset($parents[$zero]);
35
 
36
- return $parents;
37
- }
38
 
39
- while ( $parents = wxr_missing_parents($categories) ) {
40
- $found_parents = get_categories("include=" . join(', ', $parents));
41
- if ( is_array($found_parents) && count($found_parents) )
42
- $categories = array_merge($categories, $found_parents);
43
- else
44
- break;
45
- }
46
 
47
- // Put them in order to be inserted with no child going before its parent
48
- $pass = 0;
49
- $passes = 1000 + count($categories);
50
- while ( ( $cat = array_shift($categories) ) && ++$pass < $passes ) {
51
- if ( $cat->parent == 0 || isset($cats[$cat->parent]) ) {
52
- $cats[$cat->term_id] = $cat;
53
- } else {
54
- $categories[] = $cat;
55
- }
56
  }
57
- unset($categories);
58
 
59
  /**
60
  * Place string in CDATA tag.
@@ -63,15 +35,15 @@ unset($categories);
63
  *
64
  * @param string $str String to place in XML CDATA tag.
65
  */
66
- function wxr_cdata($str) {
67
- if ( seems_utf8($str) == false )
68
- $str = utf8_encode($str);
69
 
70
- // $str = ent2ncr(esc_html($str));
71
 
72
- $str = "<![CDATA[$str" . ( ( substr($str, -1) == ']' ) ? ' ' : '') . "]]>";
73
 
74
- return $str;
75
  }
76
 
77
  /**
@@ -81,17 +53,17 @@ function wxr_cdata($str) {
81
  *
82
  * @return string Site URL.
83
  */
84
- function wxr_site_url() {
85
- global $current_site;
86
-
87
- // mu: the base url
88
- if ( isset($current_site->domain) ) {
89
- return 'http://'.$current_site->domain.$current_site->path;
90
- }
91
- // wp: the blog url
92
- else {
93
- return get_bloginfo_rss('url');
94
- }
95
  }
96
 
97
  /**
@@ -101,11 +73,11 @@ function wxr_site_url() {
101
  *
102
  * @param object $c Category Object
103
  */
104
- function wxr_cat_name($c) {
105
- if ( empty($c->name) )
106
- return;
107
 
108
- echo '<wp:cat_name>' . wxr_cdata($c->name) . '</wp:cat_name>';
109
  }
110
 
111
  /**
@@ -115,11 +87,11 @@ function wxr_cat_name($c) {
115
  *
116
  * @param object $c Category Object
117
  */
118
- function wxr_category_description($c) {
119
- if ( empty($c->description) )
120
- return;
121
 
122
- echo '<wp:category_description>' . wxr_cdata($c->description) . '</wp:category_description>';
123
  }
124
 
125
  /**
@@ -129,11 +101,11 @@ function wxr_category_description($c) {
129
  *
130
  * @param object $t Tag Object
131
  */
132
- function wxr_tag_name($t) {
133
- if ( empty($t->name) )
134
- return;
135
 
136
- echo '<wp:tag_name>' . wxr_cdata($t->name) . '</wp:tag_name>';
137
  }
138
 
139
  /**
@@ -143,149 +115,81 @@ function wxr_tag_name($t) {
143
  *
144
  * @param object $t Tag Object
145
  */
146
- function wxr_tag_description($t) {
147
- if ( empty($t->description) )
148
- return;
149
 
150
- echo '<wp:tag_description>' . wxr_cdata($t->description) . '</wp:tag_description>';
151
  }
152
 
153
- /**
154
- * {@internal Missing Short Description}}
155
- *
156
- * @since unknown
157
- */
158
- function wxr_post_taxonomy() {
159
- $categories = get_the_category();
160
- $tags = get_the_tags();
161
- $the_list = '';
162
- $filter = 'rss';
163
-
164
- if ( !empty($categories) ) foreach ( (array) $categories as $category ) {
165
- $cat_name = sanitize_term_field('name', $category->name, $category->term_id, 'category', $filter);
166
- // for backwards compatibility
167
- $the_list .= "\n\t\t<category><![CDATA[$cat_name]]></category>\n";
168
- // forwards compatibility: use a unique identifier for each cat to avoid clashes
169
- // http://trac.wordpress.org/ticket/5447
170
- $the_list .= "\n\t\t<category domain=\"category\" nicename=\"{$category->slug}\"><![CDATA[$cat_name]]></category>\n";
171
- }
172
-
173
- if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
174
- $tag_name = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
175
- $the_list .= "\n\t\t<category domain=\"tag\"><![CDATA[$tag_name]]></category>\n";
176
- // forwards compatibility as above
177
- $the_list .= "\n\t\t<category domain=\"tag\" nicename=\"{$tag->slug}\"><![CDATA[$tag_name]]></category>\n";
178
- }
179
-
180
- echo $the_list;
181
- }
182
-
183
- // start catching output
184
- ob_start();
185
 
 
 
 
186
 
187
- echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
 
188
 
 
189
  ?>
190
- <!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your blog. -->
191
- <!-- It contains information about your blog's posts, comments, and categories. -->
192
- <!-- You may use this file to transfer that content from one site to another. -->
193
- <!-- This file is not intended to serve as a complete backup of your blog. -->
194
-
195
- <!-- To import this information into a WordPress blog follow these steps. -->
196
- <!-- 1. Log into that blog as an administrator. -->
197
- <!-- 2. Go to Tools: Import in the blog's admin panels (or Manage: Import in older versions of WordPress). -->
198
- <!-- 3. Choose "WordPress" from the list. -->
199
- <!-- 4. Upload this file using the form provided on that page. -->
200
- <!-- 5. You will first be asked to map the authors in this export file to users -->
201
- <!-- on the blog. For each author, you may choose to map to an -->
202
- <!-- existing user on the blog or to create a new user -->
203
- <!-- 6. WordPress will then import each of the posts, comments, and categories -->
204
- <!-- contained in this file into your blog -->
205
-
206
  <?php the_generator('export');?>
207
  <rss version="2.0"
208
- xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/"
209
- xmlns:content="http://purl.org/rss/1.0/modules/content/"
210
- xmlns:dsq="http://www.disqus.com/"
211
- xmlns:wfw="http://wellformedweb.org/CommentAPI/"
212
- xmlns:dc="http://purl.org/dc/elements/1.1/"
213
- xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
214
  >
215
 
216
  <channel>
217
- <title><?php bloginfo_rss('name'); ?></title>
218
- <link><?php bloginfo_rss('url') ?></link>
219
- <description><?php bloginfo_rss("description") ?></description>
220
- <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></pubDate>
221
- <generator>http://wordpress.org/?v=<?php bloginfo_rss('version'); ?></generator>
222
- <language><?php echo get_option('rss_language'); ?></language>
223
- <wp:wxr_version><?php echo WXR_VERSION; ?></wp:wxr_version>
224
- <wp:base_site_url><?php echo wxr_site_url(); ?></wp:base_site_url>
225
- <wp:base_blog_url><?php bloginfo_rss('url'); ?></wp:base_blog_url>
226
- <?php if ( $cats ) : foreach ( $cats as $c ) : ?>
227
- <wp:category><wp:category_nicename><?php echo $c->slug; ?></wp:category_nicename><wp:category_parent><?php echo $c->parent ? $cats[$c->parent]->name : ''; ?></wp:category_parent><?php wxr_cat_name($c); ?><?php wxr_category_description($c); ?></wp:category>
228
- <?php endforeach; endif; ?>
229
- <?php if ( $tags ) : foreach ( $tags as $t ) : ?>
230
- <wp:tag><wp:tag_slug><?php echo $t->slug; ?></wp:tag_slug><?php wxr_tag_name($t); ?><?php wxr_tag_description($t); ?></wp:tag>
231
- <?php endforeach; endif; ?>
232
  <?php
233
- if (count($posts)) {
234
- global $wp_query, $post;
235
- $wp_query->in_the_loop = true; // Fake being in the loop.
236
- foreach ($posts as $post) {
237
- setup_postdata($post); ?>
238
  <item>
239
  <title><?php echo apply_filters('the_title_rss', $post->post_title); ?></title>
240
  <link><?php the_permalink_rss() ?></link>
241
  <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
242
- <dc:creator><?php echo wxr_cdata(get_the_author()); ?></dc:creator>
243
- <?php wxr_post_taxonomy() ?>
244
-
245
  <guid isPermaLink="false"><?php the_guid(); ?></guid>
246
- <description></description>
247
- <content:encoded><?php echo wxr_cdata( apply_filters('the_content_export', $post->post_content) ); ?></content:encoded>
248
- <excerpt:encoded><?php echo wxr_cdata( apply_filters('the_excerpt_export', $post->post_excerpt) ); ?></excerpt:encoded>
249
  <dsq:thread_identifier><?php echo dsq_identifier_for_post($post); ?></dsq:thread_identifier>
250
  <wp:post_id><?php echo $post->ID; ?></wp:post_id>
251
- <wp:post_date><?php echo $post->post_date; ?></wp:post_date>
252
  <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
253
  <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
254
- <wp:ping_status><?php echo $post->ping_status; ?></wp:ping_status>
255
- <wp:post_name><?php echo $post->post_name; ?></wp:post_name>
256
- <wp:status><?php echo $post->post_status; ?></wp:status>
257
- <wp:post_parent><?php echo $post->post_parent; ?></wp:post_parent>
258
- <wp:menu_order><?php echo $post->menu_order; ?></wp:menu_order>
259
- <wp:post_type><?php echo $post->post_type; ?></wp:post_type>
260
  <?php
261
- $comments = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_agent NOT LIKE 'Disqus/%%'", $post->ID) );
262
  if ( $comments ) { foreach ( $comments as $c ) { ?>
263
  <wp:comment>
264
  <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
265
- <wp:comment_author><?php echo wxr_cdata($c->comment_author); ?></wp:comment_author>
266
  <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
267
  <wp:comment_author_url><?php echo $c->comment_author_url; ?></wp:comment_author_url>
268
  <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
269
  <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
270
  <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
271
- <wp:comment_content><?php echo wxr_cdata($c->comment_content) ?></wp:comment_content>
272
  <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
273
  <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
274
  <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
275
- <wp:comment_user_id><?php echo $c->user_id; ?></wp:comment_user_id>
276
  </wp:comment>
277
  <?php } } // comments ?>
278
- </item>
279
- <?php } } // count(posts) ?>
280
  </channel>
281
  </rss>
282
  <?php
283
 
284
- // end of WXR output
285
- $output = ob_get_contents();
286
- ob_end_clean();
287
 
288
- return $output;
289
  }
290
 
291
  ?>
6
  @ini_set('memory_limit', '256M');
7
  define('WXR_VERSION', '1.0');
8
 
 
 
 
 
 
 
 
 
9
  /**
10
  * {@internal Missing Short Description}}
11
  *
13
  *
14
  * @param unknown_type $categories
15
  */
16
+ function dsq_export_wxr_missing_parents($categories) {
17
+ if ( !is_array($categories) || empty($categories) )
18
+ return array();
 
 
 
 
 
19
 
20
+ foreach ( $categories as $category )
21
+ $parents[$category->term_id] = $category->parent;
22
 
23
+ $parents = array_unique(array_diff($parents, array_keys($parents)));
 
24
 
25
+ if ( $zero = array_search('0', $parents) )
26
+ unset($parents[$zero]);
 
 
 
 
 
27
 
28
+ return $parents;
 
 
 
 
 
 
 
 
29
  }
 
30
 
31
  /**
32
  * Place string in CDATA tag.
35
  *
36
  * @param string $str String to place in XML CDATA tag.
37
  */
38
+ function dsq_export_wxr_cdata($str) {
39
+ if ( seems_utf8($str) == false )
40
+ $str = utf8_encode($str);
41
 
42
+ // $str = ent2ncr(esc_html($str));
43
 
44
+ $str = "<![CDATA[$str" . ( ( substr($str, -1) == ']' ) ? ' ' : '') . "]]>";
45
 
46
+ return $str;
47
  }
48
 
49
  /**
53
  *
54
  * @return string Site URL.
55
  */
56
+ function dsq_export_wxr_site_url() {
57
+ global $current_site;
58
+
59
+ // mu: the base url
60
+ if ( isset($current_site->domain) ) {
61
+ return 'http://'.$current_site->domain.$current_site->path;
62
+ }
63
+ // wp: the blog url
64
+ else {
65
+ return get_bloginfo_rss('url');
66
+ }
67
  }
68
 
69
  /**
73
  *
74
  * @param object $c Category Object
75
  */
76
+ function dsq_export_wxr_cat_name($c) {
77
+ if ( empty($c->name) )
78
+ return;
79
 
80
+ echo '<wp:cat_name>' . dsq_export_wxr_cdata($c->name) . '</wp:cat_name>';
81
  }
82
 
83
  /**
87
  *
88
  * @param object $c Category Object
89
  */
90
+ function dsq_export_wxr_category_description($c) {
91
+ if ( empty($c->description) )
92
+ return;
93
 
94
+ echo '<wp:category_description>' . dsq_export_wxr_cdata($c->description) . '</wp:category_description>';
95
  }
96
 
97
  /**
101
  *
102
  * @param object $t Tag Object
103
  */
104
+ function dsq_export_wxr_tag_name($t) {
105
+ if ( empty($t->name) )
106
+ return;
107
 
108
+ echo '<wp:tag_name>' . dsq_export_wxr_cdata($t->name) . '</wp:tag_name>';
109
  }
110
 
111
  /**
115
  *
116
  * @param object $t Tag Object
117
  */
118
+ function dsq_export_wxr_tag_description($t) {
119
+ if ( empty($t->description) )
120
+ return;
121
 
122
+ echo '<wp:tag_description>' . dsq_export_wxr_cdata($t->description) . '</wp:tag_description>';
123
  }
124
 
125
+ // receives an array of posts to export
126
+ function dsq_export_wp($post, $comments=null) {
127
+ global $wpdb;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
+ if (!$comments) {
130
+ $comments = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_agent NOT LIKE 'Disqus/%%'", $post->ID) );
131
+ }
132
 
133
+ // start catching output
134
+ ob_start();
135
 
136
+ echo '<?xml version="1.0" encoding="' . get_bloginfo('charset') . '"?' . ">\n";
137
  ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  <?php the_generator('export');?>
139
  <rss version="2.0"
140
+ xmlns:excerpt="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/excerpt/"
141
+ xmlns:content="http://purl.org/rss/1.0/modules/content/"
142
+ xmlns:dsq="http://www.disqus.com/"
143
+ xmlns:wfw="http://wellformedweb.org/CommentAPI/"
144
+ xmlns:dc="http://purl.org/dc/elements/1.1/"
145
+ xmlns:wp="http://wordpress.org/export/<?php echo WXR_VERSION; ?>/"
146
  >
147
 
148
  <channel>
149
+ <title><?php bloginfo_rss('name'); ?></title>
150
+ <link><?php bloginfo_rss('url') ?></link>
151
+ <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></pubDate>
152
+ <generator>WordPress <?php bloginfo_rss('version'); ?>; Disqus <?php echo DISQUS_VERSION; ?></generator>
 
 
 
 
 
 
 
 
 
 
 
153
  <?php
154
+ global $wp_query, $post;
155
+ $wp_query->in_the_loop = true; // Fake being in the loop.
156
+ setup_postdata($post); ?>
 
 
157
  <item>
158
  <title><?php echo apply_filters('the_title_rss', $post->post_title); ?></title>
159
  <link><?php the_permalink_rss() ?></link>
160
  <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
161
+ <dc:creator><?php echo dsq_export_wxr_cdata(get_the_author()); ?></dc:creator>
 
 
162
  <guid isPermaLink="false"><?php the_guid(); ?></guid>
163
+ <content:encoded><?php echo dsq_export_wxr_cdata( apply_filters('the_content_export', $post->post_content) ); ?></content:encoded>
 
 
164
  <dsq:thread_identifier><?php echo dsq_identifier_for_post($post); ?></dsq:thread_identifier>
165
  <wp:post_id><?php echo $post->ID; ?></wp:post_id>
 
166
  <wp:post_date_gmt><?php echo $post->post_date_gmt; ?></wp:post_date_gmt>
167
  <wp:comment_status><?php echo $post->comment_status; ?></wp:comment_status>
 
 
 
 
 
 
168
  <?php
 
169
  if ( $comments ) { foreach ( $comments as $c ) { ?>
170
  <wp:comment>
171
  <wp:comment_id><?php echo $c->comment_ID; ?></wp:comment_id>
172
+ <wp:comment_author><?php echo dsq_export_wxr_cdata($c->comment_author); ?></wp:comment_author>
173
  <wp:comment_author_email><?php echo $c->comment_author_email; ?></wp:comment_author_email>
174
  <wp:comment_author_url><?php echo $c->comment_author_url; ?></wp:comment_author_url>
175
  <wp:comment_author_IP><?php echo $c->comment_author_IP; ?></wp:comment_author_IP>
176
  <wp:comment_date><?php echo $c->comment_date; ?></wp:comment_date>
177
  <wp:comment_date_gmt><?php echo $c->comment_date_gmt; ?></wp:comment_date_gmt>
178
+ <wp:comment_content><?php echo dsq_export_wxr_cdata($c->comment_content) ?></wp:comment_content>
179
  <wp:comment_approved><?php echo $c->comment_approved; ?></wp:comment_approved>
180
  <wp:comment_type><?php echo $c->comment_type; ?></wp:comment_type>
181
  <wp:comment_parent><?php echo $c->comment_parent; ?></wp:comment_parent>
 
182
  </wp:comment>
183
  <?php } } // comments ?>
184
+ </item>
 
185
  </channel>
186
  </rss>
187
  <?php
188
 
189
+ // end of WXR output
190
+ $output = ob_get_clean();
 
191
 
192
+ return $output;
193
  }
194
 
195
  ?>
lib/api/disqus/disqus.php CHANGED
@@ -45,7 +45,7 @@ if (!extension_loaded('json')) {
45
  } else {
46
  function dsq_json_decode($data) {
47
  return json_decode($data);
48
- }
49
  }
50
 
51
  /**
@@ -78,7 +78,7 @@ class DisqusAPI {
78
  $this->api_url = $api_url;
79
  $this->last_error = null;
80
  }
81
-
82
  /**
83
  * Makes a call to a Disqus API method.
84
  *
@@ -93,7 +93,7 @@ class DisqusAPI {
93
  */
94
  function call($method, $args=array(), $post=false) {
95
  $url = $this->api_url . $method . '/';
96
-
97
  if (!isset($args['user_api_key'])) {
98
  $args['user_api_key'] = $this->user_api_key;
99
  }
@@ -103,38 +103,62 @@ class DisqusAPI {
103
  if (!isset($args['api_version'])) {
104
  $args['api_version'] = $this->api_version;
105
  }
106
-
107
  foreach ($args as $key=>$value) {
108
  // XXX: Disqus is lacking some exception handling and we sometimes
109
  // end up with 500s when passing invalid values
110
  if (empty($value)) unset($args[$key]);
111
  }
112
-
113
  if (!$post) {
114
  $url .= '?' . dsq_get_query_string($args);
115
  $args = null;
116
  }
117
-
118
  if (!($response = dsq_urlopen($url, $args)) || !$response['code']) {
119
  $this->last_error = 'Unable to connect to the Disqus API servers';
120
  return false;
121
  }
122
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  $data = dsq_json_decode($response['data']);
124
-
125
- if(!$data || !$data->succeeded) {
126
- $this->last_error = $data->message;
127
  return false;
128
  }
129
-
130
- if ($response['code'] != 200) {
131
- $this->last_error = 'Unknown error';
 
 
 
 
132
  return false;
133
  }
134
-
135
  return $data->message;
136
  }
137
-
138
  /**
139
  * Retrieve the last error message recorded.
140
  *
@@ -143,6 +167,9 @@ class DisqusAPI {
143
  */
144
  function get_last_error() {
145
  if (empty($this->last_error)) return;
 
 
 
146
  return $this->last_error;
147
  }
148
 
@@ -155,7 +182,7 @@ class DisqusAPI {
155
  function get_user_name() {
156
  return $this->call('get_user_name', array(), true);
157
  }
158
-
159
  /**
160
  * Returns an array of hashes representing all forums the user owns.
161
  *
@@ -178,10 +205,10 @@ class DisqusAPI {
178
  $params = array(
179
  'forum_id' => $forum_id,
180
  );
181
-
182
  return $this->call('get_forum_api_key', $params);
183
  }
184
-
185
  /**
186
  * Get a list of comments on a website.
187
  *
@@ -202,7 +229,7 @@ class DisqusAPI {
202
  */
203
  function get_forum_posts($forum_id, $params=array()) {
204
  $params['forum_id'] = $forum_id;
205
-
206
  return $this->call('get_forum_posts', $params);
207
  }
208
 
@@ -218,23 +245,23 @@ class DisqusAPI {
218
  $params = array(
219
  'thread_ids' => is_array($thread_ids) ? implode(',', $thread_ids) : $thread_ids,
220
  );
221
-
222
  return $this->call('get_num_posts', $params);
223
  }
224
-
225
  /**
226
  * Returns a list of categories that were created for a website (forum) provided.
227
  *
228
  * @param $forum_id
229
  * the unique of the forum
230
  * @return
231
- * A hash containing category_id, title, forum_id, and is_default.
232
  */
233
  function get_categories_list($forum_id) {
234
  $params = array(
235
  'forum_id' => $forum_id,
236
  );
237
-
238
  return $this->call('get_categories_list', $params);
239
  }
240
 
@@ -252,7 +279,7 @@ class DisqusAPI {
252
  */
253
  function get_thread_list($forum_id, $params=array()) {
254
  $params['forum_id'] = $forum_id;
255
-
256
  return $this->call('get_thread_list', $params);
257
  }
258
 
@@ -271,7 +298,7 @@ class DisqusAPI {
271
  'forum_id' => $forum_id,
272
  'since' => is_string($since) ? $string : strftime('%Y-%m-%dT%H:%M', $since),
273
  );
274
-
275
  return $this->call('get_updated_threads', $params);
276
  }
277
 
@@ -302,7 +329,7 @@ class DisqusAPI {
302
 
303
  return $this->call('get_thread_posts', $params);
304
  }
305
-
306
  /**
307
  * Get or create thread by identifier.
308
  *
@@ -327,10 +354,10 @@ class DisqusAPI {
327
  function thread_by_identifier($identifier, $title, $params=array()) {
328
  $params['identifier'] = $identifier;
329
  $params['title'] = $title;
330
-
331
  return $this->call('thread_by_identifier', $params, true);
332
  }
333
-
334
  /**
335
  * Get thread by URL.
336
  *
@@ -348,10 +375,10 @@ class DisqusAPI {
348
  'url' => $url,
349
  'partner_api_key' => $partner_api_key,
350
  );
351
-
352
  return $this->call('get_thread_by_url', $params);
353
  }
354
-
355
  /**
356
  * Updates thread.
357
  *
@@ -372,10 +399,10 @@ class DisqusAPI {
372
  */
373
  function update_thread($thread_id, $params=array()) {
374
  $params['thread_id'] = $thread_id;
375
-
376
  return $this->call('update_thread', $params, true);
377
  }
378
-
379
  /**
380
  * Creates a new post.
381
  *
@@ -405,10 +432,10 @@ class DisqusAPI {
405
  $params['message'] = $message;
406
  $params['author_name'] = $author_name;
407
  $params['author_email'] = $author_email;
408
-
409
  return $this->call('create_post', $params, true);
410
  }
411
-
412
  /**
413
  * Delete a comment or mark it as spam (or not spam).
414
  *
@@ -424,7 +451,7 @@ class DisqusAPI {
424
  'post_id' => $post_id,
425
  'action' => $action,
426
  );
427
-
428
  return $this->call('moderate_post', $params, true);
429
  }
430
  }
45
  } else {
46
  function dsq_json_decode($data) {
47
  return json_decode($data);
48
+ }
49
  }
50
 
51
  /**
78
  $this->api_url = $api_url;
79
  $this->last_error = null;
80
  }
81
+
82
  /**
83
  * Makes a call to a Disqus API method.
84
  *
93
  */
94
  function call($method, $args=array(), $post=false) {
95
  $url = $this->api_url . $method . '/';
96
+
97
  if (!isset($args['user_api_key'])) {
98
  $args['user_api_key'] = $this->user_api_key;
99
  }
103
  if (!isset($args['api_version'])) {
104
  $args['api_version'] = $this->api_version;
105
  }
106
+
107
  foreach ($args as $key=>$value) {
108
  // XXX: Disqus is lacking some exception handling and we sometimes
109
  // end up with 500s when passing invalid values
110
  if (empty($value)) unset($args[$key]);
111
  }
112
+
113
  if (!$post) {
114
  $url .= '?' . dsq_get_query_string($args);
115
  $args = null;
116
  }
117
+
118
  if (!($response = dsq_urlopen($url, $args)) || !$response['code']) {
119
  $this->last_error = 'Unable to connect to the Disqus API servers';
120
  return false;
121
  }
122
+
123
+ if ($response['code'] != 200) {
124
+ if ($response['code'] == 500) {
125
+ // Try to grab the exception ID for better reporting
126
+ if (!empty($response['headers']['X-Sentry-ID'])) {
127
+ $this->last_error = 'DISQUS returned a bad response (HTTP '.$response['code'].', ReferenceID: '.$response['headers']['X-Sentry-ID'].')';
128
+ return false;
129
+ }
130
+ } elseif ($response['code'] == 400) {
131
+ $data = dsq_json_decode($response['data']);
132
+ if ($data && $data->message) {
133
+ $this->last_error = $data->message;
134
+ } else {
135
+ $this->last_error = "DISQUS returned a bad response (HTTP ".$response['code'].")";
136
+ }
137
+ return false;
138
+ }
139
+ $this->last_error = "DISQUS returned a bad response (HTTP ".$response['code'].")";
140
+ return false;
141
+ }
142
+
143
  $data = dsq_json_decode($response['data']);
144
+
145
+ if (!$data) {
146
+ $this->last_error = 'No valid JSON content returned from Disqus';
147
  return false;
148
  }
149
+
150
+ if (!$data->succeeded) {
151
+ if (!$data->message) {
152
+ $this->last_error = '(No error message was received)';
153
+ } else {
154
+ $this->last_error = $data->message;
155
+ }
156
  return false;
157
  }
158
+
159
  return $data->message;
160
  }
161
+
162
  /**
163
  * Retrieve the last error message recorded.
164
  *
167
  */
168
  function get_last_error() {
169
  if (empty($this->last_error)) return;
170
+ if (!is_string($this->last_error)) {
171
+ return var_export($this->last_error);
172
+ }
173
  return $this->last_error;
174
  }
175
 
182
  function get_user_name() {
183
  return $this->call('get_user_name', array(), true);
184
  }
185
+
186
  /**
187
  * Returns an array of hashes representing all forums the user owns.
188
  *
205
  $params = array(
206
  'forum_id' => $forum_id,
207
  );
208
+
209
  return $this->call('get_forum_api_key', $params);
210
  }
211
+
212
  /**
213
  * Get a list of comments on a website.
214
  *
229
  */
230
  function get_forum_posts($forum_id, $params=array()) {
231
  $params['forum_id'] = $forum_id;
232
+
233
  return $this->call('get_forum_posts', $params);
234
  }
235
 
245
  $params = array(
246
  'thread_ids' => is_array($thread_ids) ? implode(',', $thread_ids) : $thread_ids,
247
  );
248
+
249
  return $this->call('get_num_posts', $params);
250
  }
251
+
252
  /**
253
  * Returns a list of categories that were created for a website (forum) provided.
254
  *
255
  * @param $forum_id
256
  * the unique of the forum
257
  * @return
258
+ * A hash containing category_id, title, forum_id, and is_default.
259
  */
260
  function get_categories_list($forum_id) {
261
  $params = array(
262
  'forum_id' => $forum_id,
263
  );
264
+
265
  return $this->call('get_categories_list', $params);
266
  }
267
 
279
  */
280
  function get_thread_list($forum_id, $params=array()) {
281
  $params['forum_id'] = $forum_id;
282
+
283
  return $this->call('get_thread_list', $params);
284
  }
285
 
298
  'forum_id' => $forum_id,
299
  'since' => is_string($since) ? $string : strftime('%Y-%m-%dT%H:%M', $since),
300
  );
301
+
302
  return $this->call('get_updated_threads', $params);
303
  }
304
 
329
 
330
  return $this->call('get_thread_posts', $params);
331
  }
332
+
333
  /**
334
  * Get or create thread by identifier.
335
  *
354
  function thread_by_identifier($identifier, $title, $params=array()) {
355
  $params['identifier'] = $identifier;
356
  $params['title'] = $title;
357
+
358
  return $this->call('thread_by_identifier', $params, true);
359
  }
360
+
361
  /**
362
  * Get thread by URL.
363
  *
375
  'url' => $url,
376
  'partner_api_key' => $partner_api_key,
377
  );
378
+
379
  return $this->call('get_thread_by_url', $params);
380
  }
381
+
382
  /**
383
  * Updates thread.
384
  *
399
  */
400
  function update_thread($thread_id, $params=array()) {
401
  $params['thread_id'] = $thread_id;
402
+
403
  return $this->call('update_thread', $params, true);
404
  }
405
+
406
  /**
407
  * Creates a new post.
408
  *
432
  $params['message'] = $message;
433
  $params['author_name'] = $author_name;
434
  $params['author_email'] = $author_email;
435
+
436
  return $this->call('create_post', $params, true);
437
  }
438
+
439
  /**
440
  * Delete a comment or mark it as spam (or not spam).
441
  *
451
  'post_id' => $post_id,
452
  'action' => $action,
453
  );
454
+
455
  return $this->call('moderate_post', $params, true);
456
  }
457
  }
lib/api/disqus/url.php CHANGED
@@ -16,7 +16,7 @@ function dsq_get_query_string($postdata) {
16
 
17
 
18
  function dsq_get_post_content($boundary, $postdata, $file_name, $file_field) {
19
- if(!$file_name || !$file_field) {
20
  return dsq_get_query_string($postdata);
21
  }
22
 
@@ -60,6 +60,7 @@ function _dsq_curl_urlopen($url, $postdata, &$response, $file_name, $file_field)
60
  CURLOPT_USERAGENT => USER_AGENT,
61
  CURLOPT_RETURNTRANSFER => true,
62
  CURLOPT_POST => ($postdata_str ? 1 : 0),
 
63
  CURLOPT_HTTPHEADER => array('Expect:'),
64
  CURLOPT_TIMEOUT => SOCKET_TIMEOUT
65
  );
@@ -73,10 +74,28 @@ function _dsq_curl_urlopen($url, $postdata, &$response, $file_name, $file_field)
73
  }
74
  curl_setopt_array($c, $c_options);
75
 
76
- $response['data'] = curl_exec($c);
 
 
 
77
  $response['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE);
78
  }
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  function _dsq_fsockopen_urlopen($url, $postdata, &$response, $file_name, $file_field) {
82
  $buf = '';
@@ -133,21 +152,11 @@ function _dsq_fsockopen_urlopen($url, $postdata, &$response, $file_name, $file_f
133
  list($headers, $response['data']) = explode("\r\n\r\n", $buf, 2);
134
 
135
  // Get status code from headers.
136
- $headers = explode("\r\n", $headers);
137
- list($unused, $response['code'], $unused) = explode(' ', $headers[0], 3);
138
- $headers = array_slice($headers, 1);
139
-
140
- // Convert headers into associative array.
141
- foreach($headers as $unused=>$header) {
142
- $header = explode(':', $header);
143
- $header[0] = trim($header[0]);
144
- $header[1] = trim($header[1]);
145
- $headers[strtolower($header[0])] = strtolower($header[1]);
146
- }
147
 
148
  // If transfer-coding is set to chunked, we need to join the message body
149
  // together.
150
- if(isset($headers['transfer-encoding']) && 'chunked' == $headers['transfer-encoding']) {
151
  $chunk_data = $response['data'];
152
  $joined_data = '';
153
  while(true) {
@@ -164,6 +173,7 @@ function _dsq_fsockopen_urlopen($url, $postdata, &$response, $file_name, $file_f
164
  } else {
165
  $length = $headers['content-length'];
166
  }
 
167
  }
168
 
169
 
@@ -190,7 +200,7 @@ function _dsq_fopen_urlopen($url, $postdata, &$response, $file_name, $file_field
190
  ));
191
  }
192
  }
193
-
194
 
195
  ini_set('user_agent', USER_AGENT);
196
  $ctx = stream_context_create($params);
@@ -212,6 +222,7 @@ function _dsq_fopen_urlopen($url, $postdata, &$response, $file_name, $file_field
212
  }
213
 
214
  $response['data'] = stream_get_contents($fp);
 
215
  }
216
 
217
 
@@ -239,7 +250,7 @@ function dsq_urlopen($url, $postdata=false, $file=false) {
239
  if($file) {
240
  extract($file, EXTR_PREFIX_ALL, 'file');
241
  }
242
- if(!$file_name || !$file_field) {
243
  $file_name = false;
244
  $file_field = false;
245
  }
@@ -254,7 +265,7 @@ function dsq_urlopen($url, $postdata=false, $file=false) {
254
  foreach ($curl_options as $option => $value) {
255
  if (!curl_setopt($ch, $option, $value)) {
256
  return false;
257
- }
258
  }
259
  return true;
260
  }
16
 
17
 
18
  function dsq_get_post_content($boundary, $postdata, $file_name, $file_field) {
19
+ if(empty($file_name) || empty($file_field)) {
20
  return dsq_get_query_string($postdata);
21
  }
22
 
60
  CURLOPT_USERAGENT => USER_AGENT,
61
  CURLOPT_RETURNTRANSFER => true,
62
  CURLOPT_POST => ($postdata_str ? 1 : 0),
63
+ CURLOPT_HEADER => true,
64
  CURLOPT_HTTPHEADER => array('Expect:'),
65
  CURLOPT_TIMEOUT => SOCKET_TIMEOUT
66
  );
74
  }
75
  curl_setopt_array($c, $c_options);
76
 
77
+ $data = curl_exec($c);
78
+ list($headers, $response['data']) = explode("\r\n\r\n", $data, 2);
79
+
80
+ $response['headers'] = _dsq_get_response_headers($headers, $response);
81
  $response['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE);
82
  }
83
 
84
+ function _dsq_get_response_headers($headers, &$response) {
85
+ $headers = explode("\r\n", $headers);
86
+ list($unused, $response['code'], $unused) = explode(' ', $headers[0], 3);
87
+ $headers = array_slice($headers, 1);
88
+
89
+ // Convert headers into associative array.
90
+ foreach($headers as $unused=>$header) {
91
+ $header = explode(':', $header);
92
+ $header[0] = trim($header[0]);
93
+ $header[1] = trim($header[1]);
94
+ $headers[strtolower($header[0])] = $header[1];
95
+ }
96
+
97
+ return $headers;
98
+ }
99
 
100
  function _dsq_fsockopen_urlopen($url, $postdata, &$response, $file_name, $file_field) {
101
  $buf = '';
152
  list($headers, $response['data']) = explode("\r\n\r\n", $buf, 2);
153
 
154
  // Get status code from headers.
155
+ $headers = _dsq_get_response_headers($headers, $response);
 
 
 
 
 
 
 
 
 
 
156
 
157
  // If transfer-coding is set to chunked, we need to join the message body
158
  // together.
159
+ if(isset($headers['transfer-encoding']) && 'chunked' == strtolower($headers['transfer-encoding'])) {
160
  $chunk_data = $response['data'];
161
  $joined_data = '';
162
  while(true) {
173
  } else {
174
  $length = $headers['content-length'];
175
  }
176
+ $response['headers'] = $headers;
177
  }
178
 
179
 
200
  ));
201
  }
202
  }
203
+
204
 
205
  ini_set('user_agent', USER_AGENT);
206
  $ctx = stream_context_create($params);
222
  }
223
 
224
  $response['data'] = stream_get_contents($fp);
225
+ $response['headers'] = $headers;
226
  }
227
 
228
 
250
  if($file) {
251
  extract($file, EXTR_PREFIX_ALL, 'file');
252
  }
253
+ if(empty($file_name) || empty($file_field)) {
254
  $file_name = false;
255
  $file_field = false;
256
  }
265
  foreach ($curl_options as $option => $value) {
266
  if (!curl_setopt($ch, $option, $value)) {
267
  return false;
268
+ }
269
  }
270
  return true;
271
  }
lib/api/tests.php DELETED
@@ -1,166 +0,0 @@
1
- <?php
2
-
3
- date_default_timezone_set('America/Los_Angeles');
4
-
5
- define('DISQUS_API_URL', 'http://dev.disqus.org:8000/api/');
6
-
7
- require_once('PHPUnit/Framework.php');
8
- require_once('disqus.php');
9
- require_once('json.php');
10
-
11
- define('USER_API_KEY', $_SERVER['argv'][count($_SERVER['argv'])-1]);
12
-
13
- if (strlen(USER_API_KEY) != 64) {
14
- die('Syntax: phpunit tests.php <user_api_key>');
15
- }
16
-
17
- class DisqusAPITest extends PHPUnit_Framework_TestCase {
18
- public function test_get_user_name() {
19
- $dsq = new DisqusAPI(USER_API_KEY, null, DISQUS_API_URL);
20
- $response = $dsq->get_user_name();
21
-
22
- $this->assertTrue($response !== false);
23
- }
24
-
25
- public function test_get_forum_list() {
26
- $dsq = new DisqusAPI(USER_API_KEY, null, DISQUS_API_URL);
27
- $response = $dsq->get_forum_list();
28
-
29
- $this->assertTrue($response !== false);
30
- }
31
-
32
- /**
33
- * @depends test_get_forum_list
34
- */
35
- public function test_get_forum_api_key() {
36
- $dsq = new DisqusAPI(USER_API_KEY, null, DISQUS_API_URL);
37
-
38
- $response = $dsq->get_forum_list();
39
- $forum_id = $response[0]->id;
40
-
41
- $response = $dsq->get_forum_api_key($forum_id);
42
-
43
- $this->assertTrue($response !== false);
44
- }
45
-
46
- /**
47
- * @depends test_get_forum_list
48
- */
49
- public function test_get_forum_posts() {
50
- $dsq = new DisqusAPI(USER_API_KEY, null, DISQUS_API_URL);
51
-
52
- $response = $dsq->get_forum_list();
53
- $this->assertTrue($response !== false);
54
-
55
- $forum_id = $response[0]->id;
56
-
57
- $response = $dsq->get_forum_posts($forum_id);
58
- $this->assertTrue($response !== false);
59
- }
60
-
61
- /**
62
- * @depends test_get_forum_posts
63
- */
64
- public function test_get_num_posts() {
65
- $dsq = new DisqusAPI(USER_API_KEY, null, DISQUS_API_URL);
66
-
67
- $response = $dsq->get_forum_list();
68
- $this->assertTrue($response !== false);
69
-
70
- $forum_id = $response[0]->id;
71
-
72
- $response = $dsq->get_forum_posts($forum_id);
73
- $this->assertTrue($response !== false);
74
-
75
- $thread_id = $response[0]->thread->id;
76
-
77
- $response = $dsq->get_num_posts(array($thread_id));
78
- $this->assertTrue($response !== false);
79
- }
80
-
81
- /**
82
- * @depends test_get_forum_list
83
- */
84
- public function test_get_categories_list() {
85
- $dsq = new DisqusAPI(USER_API_KEY, null, DISQUS_API_URL);
86
-
87
- $response = $dsq->get_forum_list();
88
- $this->assertTrue($response !== false);
89
-
90
- $forum_id = $response[0]->id;
91
-
92
- $response = $dsq->get_categories_list($forum_id);
93
- $this->assertTrue($response !== false);
94
- }
95
-
96
- /**
97
- * @depends test_get_forum_list
98
- */
99
- public function test_get_thread_list() {
100
- $dsq = new DisqusAPI(USER_API_KEY, null, DISQUS_API_URL);
101
-
102
- $response = $dsq->get_forum_list();
103
- $this->assertTrue($response !== false);
104
-
105
- $forum_id = $response[0]->id;
106
-
107
- $response = $dsq->get_thread_list($forum_id);
108
- $this->assertTrue($response !== false);
109
- }
110
-
111
- /**
112
- * @depends test_get_forum_list
113
- */
114
- public function test_get_updated_threads() {
115
- $dsq = new DisqusAPI(USER_API_KEY, null, DISQUS_API_URL);
116
-
117
- $response = $dsq->get_forum_list();
118
- $this->assertTrue($response !== false);
119
-
120
- $forum_id = $response[0]->id;
121
-
122
- $response = $dsq->get_updated_threads($forum_id, time());
123
- $this->assertTrue($response !== false);
124
- }
125
-
126
- /**
127
- * @depends test_get_forum_posts
128
- */
129
- public function test_get_thread_posts() {
130
- $dsq = new DisqusAPI(USER_API_KEY, null, DISQUS_API_URL);
131
-
132
- $response = $dsq->get_forum_list();
133
- $this->assertTrue($response !== false);
134
-
135
- $forum_id = $response[0]->id;
136
-
137
- $response = $dsq->get_forum_posts($forum_id);
138
- $this->assertTrue($response !== false);
139
-
140
- $thread_id = $response[0]->thread->id;
141
-
142
- $response = $dsq->get_thread_posts($thread_id);
143
- $this->assertTrue($response !== false);
144
- }
145
-
146
- public function test_json() {
147
- $subjects = array(
148
- "[1, 2, 3]",
149
- "{foo: 'bar'}",
150
- "{foo: 'bar', 1: true, 2: false, 3: nil, 4: [1, 2, 3]}",
151
- // "'hello'",
152
- // "true",
153
- // "false",
154
- // "nil",
155
- // "1",
156
- );
157
-
158
- foreach ($subjects as $v) {
159
- $json = new JSON;
160
-
161
- $this->assertEquals($json->unserialize($v), json_decode($v));
162
- }
163
- }
164
- }
165
-
166
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
lib/wp-api.php ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Implementation of the Disqus API designed for WordPress.
4
+ *
5
+ * @author Disqus <team@disqus.com>
6
+ * @copyright 2007-2010 Big Head Labs
7
+ * @link http://disqus.com/
8
+ * @package Disqus
9
+ * @subpackage DisqusWordPressAPI
10
+ * @version 2.0
11
+ */
12
+
13
+ require_once(ABSPATH.WPINC.'/http.php');
14
+ require_once(dirname(__FILE__) . '/api/disqus/disqus.php');
15
+ /** @#+
16
+ * Constants
17
+ */
18
+ /**
19
+ * Base URL for Disqus.
20
+ */
21
+ define('DISQUS_ALLOWED_HTML', '<b><u><i><h1><h2><h3><code><blockquote><br><hr>');
22
+
23
+ /**
24
+ * Helper methods for all of the Disqus v2 API methods.
25
+ *
26
+ * @package Disqus
27
+ * @subpackage DisqusWordPressAPI
28
+ * @author DISQUS.com <team@disqus.com>
29
+ * @copyright 2007-2008 Big Head Labs
30
+ * @version 1.0
31
+ */
32
+ class DisqusWordPressAPI {
33
+ var $short_name;
34
+ var $forum_api_key;
35
+
36
+ function DisqusWordPressAPI($short_name=null, $forum_api_key=null, $user_api_key=null) {
37
+ $this->short_name = $short_name;
38
+ $this->forum_api_key = $forum_api_key;
39
+ $this->user_api_key = $user_api_key;
40
+ $this->api = new DisqusAPI($user_api_key, $forum_api_key, DISQUS_API_URL);
41
+ }
42
+
43
+ function get_last_error() {
44
+ return $this->api->get_last_error();
45
+ }
46
+
47
+ function get_user_api_key($username, $password) {
48
+ $response = $this->api->call('get_user_api_key', array(
49
+ 'username' => $username,
50
+ 'password' => $password,
51
+ ), true);
52
+ return $response;
53
+ }
54
+
55
+ function get_forum_list($user_api_key) {
56
+ $this->api->user_api_key = $user_api_key;
57
+ return $this->api->get_forum_list();
58
+ }
59
+
60
+ function get_forum_api_key($user_api_key, $id) {
61
+ $this->api->user_api_key = $user_api_key;
62
+ return $this->api->get_forum_api_key($id);
63
+ }
64
+
65
+ function get_forum_posts($start_id=0) {
66
+ $response = $this->api->get_forum_posts(null, array(
67
+ 'filter' => 'approved',
68
+ 'start_id' => $start_id,
69
+ 'limit' => 100,
70
+ 'order' => 'asc',
71
+ 'full_info' => 1
72
+ ));
73
+ return $response;
74
+ }
75
+
76
+ function import_wordpress_comments(&$wxr, $timestamp, $eof=true) {
77
+ $http = new WP_Http();
78
+ $response = $http->request(
79
+ DISQUS_IMPORTER_URL . 'api/import-wordpress-comments/',
80
+ array(
81
+ 'method' => 'POST',
82
+ 'body' => array(
83
+ 'forum_url' => $this->short_name,
84
+ 'forum_api_key' => $this->forum_api_key,
85
+ 'response_type' => 'php',
86
+ 'wxr' => $wxr,
87
+ 'timestamp' => $timestamp,
88
+ 'eof' => (int)$eof
89
+ )
90
+ )
91
+ );
92
+ if ($response->errors) {
93
+ // hack
94
+ $this->api->last_error = $response->errors;
95
+ return -1;
96
+ }
97
+ $data = unserialize($response['body']);
98
+ if (!$data || $data['stat'] == 'fail') {
99
+ return -1;
100
+ }
101
+
102
+ return $data;
103
+ }
104
+ }
105
+
106
+ ?>
lib/wp-cli.php ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Helper script for setting up the WP command line environment
4
+ */
5
+ error_reporting(E_ALL | E_STRICT);
6
+
7
+ if (!isset($argv)) {
8
+ $argv = array();
9
+ }
10
+
11
+ function print_line() {
12
+ $args = func_get_args();
13
+ $result = call_user_func_array('sprintf', $args);
14
+ print("{$result}\n");
15
+ }
16
+
17
+ define('DOING_AJAX', true);
18
+ define('WP_USE_THEMES', false);
19
+ if (isset($_ENV['WORDPRESS_PATH'])) {
20
+ define('WORDPRESS_PATH', $_ENV['WORDPRESS_PATH']);
21
+ } else {
22
+ if (substr($_SERVER['SCRIPT_FILENAME'], 0, 1) != '/') {
23
+ $script_path = $_SERVER['PWD'] . $_SERVER['SCRIPT_FILENAME'];
24
+ } else {
25
+ $script_path = $_SERVER['SCRIPT_FILENAME'];
26
+ }
27
+ $tree = '';
28
+ $paths = array();
29
+ $chunks = explode('/', dirname($script_path));
30
+ foreach ($chunks as $chunk) {
31
+ if (!$chunk) continue;
32
+ $tree = $tree.'/'.$chunk;
33
+ array_push($paths, $tree);
34
+ }
35
+ $paths = array_reverse($paths);
36
+
37
+ foreach ($paths as $path) {
38
+ if (is_file($path.'/wp-config.php')) {
39
+ define('WORDPRESS_PATH', $path);
40
+ break;
41
+ }
42
+ }
43
+ }
44
+
45
+ if (!defined('WORDPRESS_PATH')) {
46
+ print_line("Unable to determine wordpress path. Please set it using WORDPRESS_PATH.");
47
+ die();
48
+ }
49
+
50
+ $_SERVER = array(
51
+ "HTTP_HOST" => "disqus.com",
52
+ "SCRIPT_NAME" => "",
53
+ "PHP_SELF" => __FILE__,
54
+ "SERVER_NAME" => "localhost",
55
+ "REQUEST_URI" => "/",
56
+ "REQUEST_METHOD" => "GET"
57
+ );
58
+ require_once(WORDPRESS_PATH . '/wp-config.php');
59
+
60
+ // swap out the object cache due to memory constraints
61
+
62
+ global $wp_object_cache;
63
+
64
+ class DummyWP_Object_Cache extends WP_Object_Cache {
65
+ function set($id, $data, $group = 'default', $expire = '') {
66
+ return;
67
+ }
68
+ }
69
+
70
+ $wp_object_cache = new DummyWP_Object_Cache();
71
+
72
+ ?>
lib/wpapi.php DELETED
@@ -1,106 +0,0 @@
1
- <?php
2
- /**
3
- * Implementation of the Disqus API designed for WordPress.
4
- *
5
- * @author Disqus <team@disqus.com>
6
- * @copyright 2007-2010 Big Head Labs
7
- * @link http://disqus.com/
8
- * @package Disqus
9
- * @subpackage DisqusWordPressAPI
10
- * @version 2.0
11
- */
12
-
13
- require_once(ABSPATH.WPINC.'/http.php');
14
- require_once(dirname(__FILE__) . '/api/disqus/disqus.php');
15
- /** @#+
16
- * Constants
17
- */
18
- /**
19
- * Base URL for Disqus.
20
- */
21
- define('DISQUS_ALLOWED_HTML', '<b><u><i><h1><h2><h3><code><blockquote><br><hr>');
22
-
23
- /**
24
- * Helper methods for all of the Disqus v2 API methods.
25
- *
26
- * @package Disqus
27
- * @subpackage DisqusWordPressAPI
28
- * @author DISQUS.com <team@disqus.com>
29
- * @copyright 2007-2008 Big Head Labs
30
- * @version 1.0
31
- */
32
- class DisqusWordPressAPI {
33
- var $short_name;
34
- var $forum_api_key;
35
-
36
- function DisqusWordPressAPI($short_name=null, $forum_api_key=null, $user_api_key=null) {
37
- $this->short_name = $short_name;
38
- $this->forum_api_key = $forum_api_key;
39
- $this->user_api_key = $user_api_key;
40
- $this->api = new DisqusAPI($user_api_key, $forum_api_key, DISQUS_API_URL);
41
- }
42
-
43
- function get_last_error() {
44
- return $this->api->get_last_error();
45
- }
46
-
47
- function get_user_api_key($username, $password) {
48
- $response = $this->api->call('get_user_api_key', array(
49
- 'username' => $username,
50
- 'password' => $password,
51
- ), true);
52
- return $response;
53
- }
54
-
55
- function get_forum_list($user_api_key) {
56
- $this->api->user_api_key = $user_api_key;
57
- return $this->api->get_forum_list();
58
- }
59
-
60
- function get_forum_api_key($user_api_key, $id) {
61
- $this->api->user_api_key = $user_api_key;
62
- return $this->api->get_forum_api_key($id);
63
- }
64
-
65
- function get_forum_posts($start_id=0) {
66
- $response = $this->api->get_forum_posts(null, array(
67
- 'filter' => 'approved',
68
- 'start_id' => $start_id,
69
- 'limit' => 100,
70
- 'order' => 'asc',
71
- 'full_info' => 1
72
- ));
73
- return $response;
74
- }
75
-
76
- function import_wordpress_comments($wxr, $timestamp, $eof=true) {
77
- $http = new WP_Http();
78
- $response = $http->request(
79
- DISQUS_IMPORTER_URL . 'api/import-wordpress-comments/',
80
- array(
81
- 'method' => 'POST',
82
- 'body' => array(
83
- 'forum_url' => $this->short_name,
84
- 'forum_api_key' => $this->forum_api_key,
85
- 'response_type' => 'php',
86
- 'wxr' => $wxr,
87
- 'timestamp' => $timestamp,
88
- 'eof' => (int)$eof
89
- )
90
- )
91
- );
92
- if ($response->errors) {
93
- // hack
94
- $this->api->last_error = $response->errors;
95
- return -1;
96
- }
97
- $data = unserialize($response['body']);
98
- if (!$data || $data['stat'] == 'fail') {
99
- return -1;
100
- }
101
-
102
- return $data;
103
- }
104
- }
105
-
106
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
manage.php CHANGED
@@ -4,46 +4,46 @@ global $dsq_api;
4
  require(ABSPATH . 'wp-includes/version.php');
5
 
6
  if ( !current_user_can('manage_options') ) {
7
- die();
8
  }
9
 
10
  if(isset($_POST['dsq_username'])) {
11
- $_POST['dsq_username'] = stripslashes($_POST['dsq_username']);
12
  }
13
 
14
  if(isset($_POST['dsq_password'])) {
15
- $_POST['dsq_password'] = stripslashes($_POST['dsq_password']);
16
  }
17
 
18
  // HACK: For old versions of WordPress
19
  if ( !function_exists('wp_nonce_field') ) {
20
- function wp_nonce_field() {}
21
  }
22
 
23
  // Handle export function.
24
  if( isset($_POST['export']) and DISQUS_CAN_EXPORT ) {
25
- require_once(dirname(__FILE__) . '/export.php');
26
- dsq_export_wp();
27
  }
28
 
29
  // Handle uninstallation.
30
  if ( isset($_POST['uninstall']) ) {
31
- foreach (dsq_options() as $opt) {
32
- delete_option($opt);
33
- }
34
- unset($_POST);
35
- dsq_uninstall_database();
36
  ?>
37
  <div class="wrap">
38
- <h2><?php echo dsq_i('Disqus Uninstalled'); ?></h2>
39
- <form method="POST" action="?page=disqus">
40
- <p>Disqus has been uninstalled successfully.</p>
41
- <ul style="list-style: circle;padding-left:20px;">
42
- <li>Local settings for the plugin were removed.</li>
43
- <li>Database changes by Disqus were reverted.</li>
44
- </ul>
45
- <p>If you wish to <a href="?page=disqus&amp;step=1">reinstall</a>, you can do that now.</p>
46
- </form>
47
  </div>
48
  <?php
49
  die();
@@ -51,27 +51,32 @@ die();
51
 
52
  // Clean-up POST parameters.
53
  foreach ( array('dsq_forum', 'dsq_username', 'dsq_user_api_key') as $key ) {
54
- if ( isset($_POST[$key]) ) { $_POST[$key] = strip_tags($_POST[$key]); }
55
  }
56
 
57
 
58
  // Handle advanced options.
59
  if ( isset($_POST['disqus_forum_url']) && isset($_POST['disqus_replace']) ) {
60
- $disqus_forum_url = $_POST['disqus_forum_url'];
61
- if ( $dot_pos = strpos($disqus_forum_url, '.') ) {
62
- $disqus_forum_url = substr($disqus_forum_url, 0, $dot_pos);
63
- }
64
- update_option('disqus_forum_url', $disqus_forum_url);
65
- update_option('disqus_partner_key', trim(stripslashes($_POST['disqus_partner_key'])));
66
- update_option('disqus_api_key', trim(stripslashes($_POST['disqus_api_key'])));
67
- update_option('disqus_user_api_key', trim(stripslashes($_POST['disqus_user_api_key'])));
68
- update_option('disqus_replace', $_POST['disqus_replace']);
69
- update_option('disqus_cc_fix', isset($_POST['disqus_cc_fix']));
70
- update_option('disqus_manual_sync', isset($_POST['disqus_manual_sync']));
71
- update_option('disqus_disable_ssr', isset($_POST['disqus_disable_ssr']));
72
- update_option('disqus_public_key', $_POST['disqus_public_key']);
73
- update_option('disqus_secret_key', $_POST['disqus_secret_key']);
74
- dsq_manage_dialog('Your settings have been changed.');
 
 
 
 
 
75
  }
76
 
77
  $dsq_user_api_key = isset($_POST['dsq_user_api_key']) ? $_POST['dsq_user_api_key'] : null;
@@ -84,309 +89,317 @@ $step = (dsq_is_installed()) ? 0 : ($step ? $step : 1);
84
 
85
  // Handle installation process.
86
  if ( 3 == $step && isset($_POST['dsq_forum']) && isset($_POST['dsq_user_api_key']) ) {
87
- list($dsq_forum_id, $dsq_forum_url) = explode(':', $_POST['dsq_forum']);
88
- update_option('disqus_forum_url', $dsq_forum_url);
89
- $api_key = $dsq_api->get_forum_api_key($_POST['dsq_user_api_key'], $dsq_forum_id);
90
- if ( !$api_key || $api_key < 0 ) {
91
- update_option('disqus_replace', 'replace');
92
- dsq_manage_dialog(dsq_i('There was an error completing the installation of Disqus. If you are still having issues, refer to the <a href="http://disqus.com/help/wordpress">WordPress help page</a>.'), true);
93
- } else {
94
- update_option('disqus_api_key', $api_key);
95
- update_option('disqus_user_api_key', $_POST['dsq_user_api_key']);
96
- update_option('disqus_replace', 'all');
97
- }
98
-
99
- if (!empty($_POST['disqus_partner_key'])) {
100
- $partner_key = trim(stripslashes($_POST['disqus_partner_key']));
101
- if (!empty($partner_key)) {
102
- update_option('disqus_partner_key', $partner_key);
103
- }
104
- }
105
  }
106
 
107
  if ( 2 == $step && isset($_POST['dsq_username']) && isset($_POST['dsq_password']) ) {
108
- $dsq_user_api_key = $dsq_api->get_user_api_key($_POST['dsq_username'], $_POST['dsq_password']);
109
- if ( $dsq_user_api_key < 0 || !$dsq_user_api_key ) {
110
- $step = 1;
111
- dsq_manage_dialog($dsq_api->get_last_error(), true);
112
- }
113
-
114
- if ( $step == 2 ) {
115
- $dsq_sites = $dsq_api->get_forum_list($dsq_user_api_key);
116
- if ( $dsq_sites < 0 ) {
117
- $step = 1;
118
- dsq_manage_dialog($dsq_api->get_last_error(), true);
119
- } else if ( !$dsq_sites ) {
120
- $step = 1;
121
- dsq_manage_dialog(dsq_i('There aren\'t any sites associated with this account. Maybe you want to <a href="%s">create a site</a>?', 'http://disqus.com/comments/register/'), true);
122
- }
123
- }
124
  }
125
 
126
- $show_advanced = $_GET['t'] == 'adv';
127
 
128
  ?>
129
  <div class="wrap" id="dsq-wrap">
130
- <ul id="dsq-tabs">
131
- <li<?php if (!$show_advanced) echo ' class="selected"'; ?> id="dsq-tab-main" rel="dsq-main"><?php echo (dsq_is_installed() ? 'Manage' : 'Install'); ?></li>
132
- <li<?php if ($show_advanced) echo ' class="selected"'; ?> id="dsq-tab-advanced" rel="dsq-advanced"><?php echo dsq_i('Advanced Options'); ?></li>
133
- </ul>
134
 
135
- <div id="dsq-main" class="dsq-content">
136
- <?php
137
  switch ( $step ) {
138
  case 3:
139
  ?>
140
- <div id="dsq-step-3" class="dsq-main"<?php if ($show_advanced) echo ' style="display:none;"'; ?>>
141
- <h2><?php echo dsq_i('Install Disqus Comments'); ?></h2>
142
 
143
- <p>Disqus has been installed on your blog.</p>
144
- <p>If you have existing comments, you may wish to <a href="?page=disqus&amp;t=adv#export">export them</a> now. Otherwise, you're all set, and the Disqus network is now powering comments on your blog.</p>
145
- </div>
146
  <?php
147
- break;
148
  case 2:
149
  ?>
150
- <div id="dsq-step-2" class="dsq-main"<?php if ($show_advanced) echo ' style="display:none;"'; ?>>
151
- <h2><?php echo dsq_i('Install Disqus Comments'); ?></h2>
152
-
153
- <form method="POST" action="?page=disqus&amp;step=3">
154
- <?php wp_nonce_field('dsq-install-2'); ?>
155
- <table class="form-table">
156
- <tr>
157
- <th scope="row" valign="top"><?php echo dsq_i('Select a website'); ?></th>
158
- <td>
159
  <?php
160
  foreach ( $dsq_sites as $counter => $dsq_site ):
161
  ?>
162
- <input name="dsq_forum" type="radio" id="dsq-site-<?php echo $counter; ?>" value="<?php echo $dsq_site->id; ?>:<?php echo $dsq_site->shortname; ?>" />
163
- <label for="dsq-site-<?php echo $counter; ?>"><strong><?php echo htmlspecialchars($dsq_site->name); ?></strong> (<u><?php echo $dsq_site->shortname; ?>.disqus.com</u>)</label>
164
- <br />
165
  <?php
166
  endforeach;
167
  ?>
168
- <hr />
169
- <a href="<?php echo DISQUS_URL; ?>comments/register/"><?php echo dsq_i('Or register a new one on the Disqus website.'); ?></a>
170
- </td>
171
- </tr>
172
- </table>
173
-
174
- <p class="submit" style="text-align: left">
175
- <input type="hidden" name="dsq_user_api_key" value="<?php echo htmlspecialchars($dsq_user_api_key); ?>"/>
176
- <input name="submit" type="submit" value="Next &raquo;" />
177
- </p>
178
- </form>
179
- </div>
180
  <?php
181
- break;
182
  case 1:
183
  ?>
184
- <div id="dsq-step-1" class="dsq-main"<?php if ($show_advanced) echo ' style="display:none;"'; ?>>
185
- <h2><?php echo dsq_i('Install Disqus Comments'); ?></h2>
186
-
187
- <form method="POST" action="?page=disqus&amp;step=2">
188
- <?php wp_nonce_field('dsq-install-1'); ?>
189
- <table class="form-table">
190
- <tr>
191
- <th scope="row" valign="top"><?php echo dsq_i('Username'); ?></th>
192
- <td>
193
- <input id="dsq-username" name="dsq_username" tabindex="1" type="text" />
194
- <a href="http://disqus.com/profile/"><?php echo dsq_i('(don\'t have a Disqus Profile yet?)'); ?></a>
195
- </td>
196
- </tr>
197
- <tr>
198
- <th scope="row" valign="top"><?php echo dsq_i('Password'); ?></th>
199
- <td>
200
- <input type="password" id="dsq-password" name="dsq_password" tabindex="2">
201
- <a href="http://disqus.com/forgot/"><?php echo dsq_i('(forgot your password?)'); ?></a>
202
- </td>
203
- </tr>
204
- </table>
205
-
206
- <p class="submit" style="text-align: left">
207
- <input name="submit" type="submit" value="Next &raquo;" tabindex="3">
208
- </p>
209
-
210
- <script type="text/javascript"> document.getElementById('dsq-username').focus(); </script>
211
- </form>
212
- </div>
213
  <?php
214
- break;
215
  case 0:
216
- $url = get_option('disqus_forum_url');
217
  ?>
218
- <div class="dsq-main"<?php if ($show_advanced) echo ' style="display:none;"'; ?>>
219
- <h2><?php echo dsq_i('Comments'); ?></h2>
220
- <iframe src="<?php if ($url) {
221
- echo 'http://'.$url.'.'.DISQUS_DOMAIN.'/admin/moderate/';
222
- } else {
223
- echo DISQUS_URL.'admin/moderate/';
224
- } ?>?template=wordpress" style="width: 100%; height: 800px"></iframe>
225
- </div>
226
  <?php } ?>
227
- </div>
228
 
229
  <?php
230
- $dsq_replace = get_option('disqus_replace');
231
- $dsq_forum_url = strtolower(get_option('disqus_forum_url'));
232
- $dsq_api_key = get_option('disqus_api_key');
233
- $dsq_user_api_key = get_option('disqus_user_api_key');
234
- $dsq_partner_key = get_option('disqus_partner_key');
235
- $dsq_cc_fix = get_option('disqus_cc_fix');
236
- $dsq_manual_sync = get_option('disqus_manual_sync');
237
- $dsq_disable_ssr = get_option('disqus_disable_ssr');
238
-
239
- $dsq_public_key = get_option('disqus_public_key');
240
- $dsq_secret_key = get_option('disqus_secret_key');
241
  ?>
242
- <!-- Advanced options -->
243
- <div id="dsq-advanced" class="dsq-content dsq-advanced"<?php if (!$show_advanced) echo ' style="display:none;"'; ?>>
244
- <h2><?php echo dsq_i('Advanced Options'); ?></h2>
245
- <?php echo dsq_i('Version: %s', esc_html(DISQUS_VERSION)); ?>
246
- <form method="POST">
247
- <?php wp_nonce_field('dsq-advanced'); ?>
248
- <h3>Configuration</h3>
249
- <table class="form-table">
250
- <tr>
251
- <th scope="row" valign="top"><?php echo dsq_i('Disqus short name'); ?></th>
252
- <td>
253
- <input name="disqus_forum_url" value="<?php echo esc_attr($dsq_forum_url); ?>" tabindex="1" type="text" />
254
- <br />
255
- <?php echo dsq_i('This is the unique identifier for your website on Disqus Comments.'); ?>
256
- </td>
257
- </tr>
258
-
259
- <tr>
260
- <th scope="row" valign="top"><?php echo dsq_i('Disqus API Key'); ?></th>
261
- <td>
262
- <input type="text" name="disqus_api_key" value="<?php echo esc_attr($dsq_api_key); ?>" tabindex="2">
263
- <br />
264
- <?php echo dsq_i('This is set for you when going through the installation steps.'); ?>
265
- </td>
266
- </tr>
267
-
268
- <tr>
269
- <th scope="row" valign="top"><?php echo dsq_i('Disqus User API Key'); ?></th>
270
- <td>
271
- <input type="text" name="disqus_user_api_key" value="<?php echo esc_attr($dsq_user_api_key); ?>" tabindex="2">
272
- <br />
273
- <?php echo dsq_i('This is set for you when going through the installation steps.'); ?>
274
- </td>
275
- </tr>
276
- <?php if (!empty($dsq_partner_key)) {// this option only shows if it was already present ?>
277
- <tr>
278
- <th scope="row" valign="top"><?php echo dsq_i('Disqus Partner Key'); ?></th>
279
- <td>
280
- <input type="text" name="disqus_partner_key" value="<?php echo esc_attr($dsq_partner_key); ?>" tabindex="2">
281
- <br />
282
- <?php echo dsq_i('Advanced: Used for single sign-on (SSO) integration. (<a href="%s" onclick="window.open(this.href); return false">more info on SSO</a>)', 'http://disqus.com/help/sso'); ?>
283
- </td>
284
- </tr>
285
- <?php } ?>
286
- <tr>
287
- <th scope="row" valign="top"><?php echo dsq_i('Application Public Key'); ?></th>
288
- <td>
289
- <input type="text" name="disqus_public_key" value="<?php echo esc_attr($dsq_public_key); ?>" tabindex="2">
290
- <br />
291
- <?php echo dsq_i('Advanced: Used for single sign-on (SSO) integration. (<a href="%s" onclick="window.open(this.href); return false">more info on SSO</a>)', 'http://docs.disqus.com/developers/sso/'); ?>
292
- </td>
293
- </tr>
294
- <tr>
295
- <th scope="row" valign="top"><?php echo dsq_i('Application Secret Key'); ?></th>
296
- <td>
297
- <input type="text" name="disqus_secret_key" value="<?php echo esc_attr($dsq_secret_key); ?>" tabindex="2">
298
- <br />
299
- <?php echo dsq_i('Advanced: Used for single sign-on (SSO) integration. (<a href="%s" onclick="window.open(this.href); return false">more info on SSO</a>)', 'http://docs.disqus.com/developers/sso/'); ?>
300
- </td>
301
- </tr>
302
-
303
- <tr>
304
- <th scope="row" valign="top"><?php echo dsq_i('Use Disqus Comments on'); ?></th>
305
- <td>
306
- <select name="disqus_replace" tabindex="3" class="disqus-replace">
307
- <option value="all" <?php if('all'==$dsq_replace){echo 'selected';}?>><?php echo dsq_i('On all existing and future blog posts.'); ?></option>
308
- <option value="closed" <?php if('closed'==$dsq_replace){echo 'selected';}?>><?php echo dsq_i('Only on blog posts with closed comments.'); ?></option>
309
- </select>
310
- <br />
311
- <?php echo dsq_i('NOTE: Your WordPress comments will never be lost.'); ?>
312
- </td>
313
- </tr>
314
-
315
- <tr>
316
- <th scope="row" valign="top"><?php echo dsq_i('Comment Counts'); ?></th>
317
- <td>
318
- <input type="checkbox" id="disqus_comment_count" name="disqus_cc_fix" <?php if($dsq_cc_fix){echo 'checked="checked"';}?> >
319
- <label for="disqus_comment_count"><?php echo dsq_i('Output JavaScript in footer'); ?></label>
320
- <br /><?php echo dsq_i('NOTE: Check this if you have problems with the comment count displays including: not showing on permalinks, broken featured image carousels, or longer-than-usual homepage load times (<a href="%s" onclick="window.open(this.href); return false">more info</a>).', 'http://disqus.com/help/wordpress'); ?>
321
- </td>
322
- </tr>
323
-
324
- <tr>
325
- <th scope="row" valign="top"><?php echo dsq_i('Comment Sync'); ?></th>
326
- <td>
327
- <input type="checkbox" id="disqus_manual_sync" name="disqus_manual_sync" <?php if($dsq_manual_sync){echo 'checked="checked"';}?> >
328
- <label for="disqus_manual_sync"><?php echo dsq_i('Disable automated comment importing'); ?></label>
329
- <br /><?php echo dsq_i('NOTE: If you have problems with WP cron taking too long and large numbers of comments you may wish to disable the automated sync cron. Keep in mind that this means comments will not automatically get synced to your local Wordpress database.'); ?>
330
- </td>
331
- </tr>
332
-
333
- <tr>
334
- <th scope="row" valign="top"><?php echo dsq_i('Server Side Rendering'); ?></th>
335
- <td>
336
- <input type="checkbox" id="disqus_disable_ssr" name="disqus_disable_ssr" <?php if($dsq_disable_ssr){echo 'checked="checked"';}?> >
337
- <label for="disqus_disable_ssr"><?php echo dsq_i('Disable server side rendering of comments'); ?></label>
338
- <br /><?php echo dsq_i('NOTE: This will hide comments from nearly all search engines'); ?>
339
- </td>
340
- </tr>
341
- </table>
342
-
343
- <p class="submit" style="text-align: left">
344
- <input name="submit" type="submit" value="Save" class="button-primary button" tabindex="4">
345
- </p>
346
- </form>
347
-
348
- <h3>Import / Export</h3>
349
-
350
- <table class="form-table">
351
- <?php if (DISQUS_CAN_EXPORT): ?>
352
- <tr id="export">
353
- <th scope="row" valign="top"><?php echo dsq_i('Export comments to Disqus'); ?></th>
354
- <td>
355
- <div id="dsq_export">
356
- <p class="status"><a href="#" class="button"><?php echo dsq_i('Export Comments'); ?></a> <?php echo dsq_i('This will export your existing WordPress comments to Disqus'); ?></p>
357
- </div>
358
- </td>
359
- </tr>
360
- <?php endif; ?>
361
- <tr>
362
- <th scope="row" valign="top"><?php echo dsq_i('Sync Disqus with WordPress'); ?></th>
363
- <td>
364
- <div id="dsq_import">
365
- <p class="status"><a href="#" class="button"><?php echo dsq_i('Sync Comments'); ?></a> <label><input type="checkbox" id="dsq_import_wipe" name="dsq_import_wipe" value="1"/> <?php echo dsq_i('Remove all imported Disqus comments before syncing.'); ?></label><br/><br/>
366
- <?php echo dsq_i('This will download your Disqus comments and store them locally in WordPress'); ?></p>
367
- </div>
368
- </td>
369
- </tr>
370
- </table>
371
-
372
- <h3>Uninstall</h3>
373
-
374
- <table class="form-table">
375
- <tr>
376
- <th scope="row" valign="top"><?php echo dsq_i('Uninstall Disqus Comments'); ?></th>
377
- <td>
378
- <form action="?page=disqus" method="POST">
379
- <?php wp_nonce_field('dsq-uninstall'); ?>
380
- <p>This will remove all Disqus specific settings, but it will leave your comments unaffected.</p>
381
- <input type="submit" value="Uninstall" name="uninstall" onclick="return confirm('<?php echo dsq_i('Are you sure you want to uninstall Disqus?'); ?>')" class="button" />
382
- </form>
383
- </td>
384
- </tr>
385
- </table>
386
- <br/>
387
- <h3><?php echo dsq_i('Debug Information'); ?></h3>
388
- <p><?php echo dsq_i('Having problems with the plugin? <a href="%s">Drop us a line</a> and include the following details and we\'ll do what we can.', 'mailto:help+wp@disqus.com'); ?></p>
389
- <textarea style="width:90%; height:200px;">URL: <?php echo get_option('siteurl'); ?>
 
 
 
 
 
 
 
 
390
  Version: <?php echo $wp_version; ?>
391
  Active Theme: <?php $theme = get_theme(get_current_theme()); echo $theme['Name'].' '.$theme['Version']; ?>
392
  URLOpen Method: <?php echo dsq_url_method(); ?>
@@ -396,16 +409,17 @@ Plugin Version: <?php echo DISQUS_VERSION; ?>
396
  Settings:
397
 
398
  dsq_is_installed: <?php echo dsq_is_installed(); ?>
 
399
  <?php foreach (dsq_options() as $opt) {
400
- echo $opt.': '.get_option($opt)."\n";
401
  } ?>
402
 
403
  Plugins:
404
 
405
  <?php
406
  foreach (get_plugins() as $plugin) {
407
- echo $plugin['Name'].' '.$plugin['Version']."\n";
408
  }
409
  ?></textarea><br/>
410
- </div>
411
  </div>
4
  require(ABSPATH . 'wp-includes/version.php');
5
 
6
  if ( !current_user_can('manage_options') ) {
7
+ die();
8
  }
9
 
10
  if(isset($_POST['dsq_username'])) {
11
+ $_POST['dsq_username'] = stripslashes($_POST['dsq_username']);
12
  }
13
 
14
  if(isset($_POST['dsq_password'])) {
15
+ $_POST['dsq_password'] = stripslashes($_POST['dsq_password']);
16
  }
17
 
18
  // HACK: For old versions of WordPress
19
  if ( !function_exists('wp_nonce_field') ) {
20
+ function wp_nonce_field() {}
21
  }
22
 
23
  // Handle export function.
24
  if( isset($_POST['export']) and DISQUS_CAN_EXPORT ) {
25
+ require_once(dirname(__FILE__) . '/export.php');
26
+ dsq_export_wp();
27
  }
28
 
29
  // Handle uninstallation.
30
  if ( isset($_POST['uninstall']) ) {
31
+ foreach (dsq_options() as $opt) {
32
+ delete_option($opt);
33
+ }
34
+ unset($_POST);
35
+ dsq_uninstall_database();
36
  ?>
37
  <div class="wrap">
38
+ <h2><?php echo dsq_i('Disqus Uninstalled'); ?></h2>
39
+ <form method="POST" action="?page=disqus">
40
+ <p>Disqus has been uninstalled successfully.</p>
41
+ <ul style="list-style: circle;padding-left:20px;">
42
+ <li>Local settings for the plugin were removed.</li>
43
+ <li>Database changes by Disqus were reverted.</li>
44
+ </ul>
45
+ <p>If you wish to <a href="?page=disqus&amp;step=1">reinstall</a>, you can do that now.</p>
46
+ </form>
47
  </div>
48
  <?php
49
  die();
51
 
52
  // Clean-up POST parameters.
53
  foreach ( array('dsq_forum', 'dsq_username', 'dsq_user_api_key') as $key ) {
54
+ if ( isset($_POST[$key]) ) { $_POST[$key] = strip_tags($_POST[$key]); }
55
  }
56
 
57
 
58
  // Handle advanced options.
59
  if ( isset($_POST['disqus_forum_url']) && isset($_POST['disqus_replace']) ) {
60
+ $disqus_forum_url = $_POST['disqus_forum_url'];
61
+ if ( $dot_pos = strpos($disqus_forum_url, '.') ) {
62
+ $disqus_forum_url = substr($disqus_forum_url, 0, $dot_pos);
63
+ }
64
+ update_option('disqus_forum_url', $disqus_forum_url);
65
+ update_option('disqus_partner_key', trim(stripslashes($_POST['disqus_partner_key'])));
66
+ update_option('disqus_api_key', trim(stripslashes($_POST['disqus_api_key'])));
67
+ update_option('disqus_user_api_key', trim(stripslashes($_POST['disqus_user_api_key'])));
68
+ update_option('disqus_replace', $_POST['disqus_replace']);
69
+ update_option('disqus_cc_fix', isset($_POST['disqus_cc_fix']));
70
+ update_option('disqus_manual_sync', isset($_POST['disqus_manual_sync']));
71
+ update_option('disqus_disable_ssr', isset($_POST['disqus_disable_ssr']));
72
+ update_option('disqus_public_key', $_POST['disqus_public_key']);
73
+ update_option('disqus_secret_key', $_POST['disqus_secret_key']);
74
+ dsq_manage_dialog('Your settings have been changed.');
75
+ }
76
+
77
+ // handle disqus_active
78
+ if (isset($_GET['active'])) {
79
+ update_option('disqus_active', ($_GET['active'] == '1' ? '1' : '0'));
80
  }
81
 
82
  $dsq_user_api_key = isset($_POST['dsq_user_api_key']) ? $_POST['dsq_user_api_key'] : null;
89
 
90
  // Handle installation process.
91
  if ( 3 == $step && isset($_POST['dsq_forum']) && isset($_POST['dsq_user_api_key']) ) {
92
+ list($dsq_forum_id, $dsq_forum_url) = explode(':', $_POST['dsq_forum']);
93
+ update_option('disqus_forum_url', $dsq_forum_url);
94
+ $api_key = $dsq_api->get_forum_api_key($_POST['dsq_user_api_key'], $dsq_forum_id);
95
+ if ( !$api_key || $api_key < 0 ) {
96
+ update_option('disqus_replace', 'replace');
97
+ dsq_manage_dialog(dsq_i('There was an error completing the installation of Disqus. If you are still having issues, refer to the <a href="http://disqus.com/help/wordpress">WordPress help page</a>.'), true);
98
+ } else {
99
+ update_option('disqus_api_key', $api_key);
100
+ update_option('disqus_user_api_key', $_POST['dsq_user_api_key']);
101
+ update_option('disqus_replace', 'all');
102
+ }
103
+
104
+ if (!empty($_POST['disqus_partner_key'])) {
105
+ $partner_key = trim(stripslashes($_POST['disqus_partner_key']));
106
+ if (!empty($partner_key)) {
107
+ update_option('disqus_partner_key', $partner_key);
108
+ }
109
+ }
110
  }
111
 
112
  if ( 2 == $step && isset($_POST['dsq_username']) && isset($_POST['dsq_password']) ) {
113
+ $dsq_user_api_key = $dsq_api->get_user_api_key($_POST['dsq_username'], $_POST['dsq_password']);
114
+ if ( $dsq_user_api_key < 0 || !$dsq_user_api_key ) {
115
+ $step = 1;
116
+ dsq_manage_dialog($dsq_api->get_last_error(), true);
117
+ }
118
+
119
+ if ( $step == 2 ) {
120
+ $dsq_sites = $dsq_api->get_forum_list($dsq_user_api_key);
121
+ if ( $dsq_sites < 0 ) {
122
+ $step = 1;
123
+ dsq_manage_dialog($dsq_api->get_last_error(), true);
124
+ } else if ( !$dsq_sites ) {
125
+ $step = 1;
126
+ dsq_manage_dialog(dsq_i('There aren\'t any sites associated with this account. Maybe you want to <a href="%s">create a site</a>?', 'http://disqus.com/comments/register/'), true);
127
+ }
128
+ }
129
  }
130
 
131
+ $show_advanced = (isset($_GET['t']) && $_GET['t'] == 'adv');
132
 
133
  ?>
134
  <div class="wrap" id="dsq-wrap">
135
+ <ul id="dsq-tabs">
136
+ <li<?php if (!$show_advanced) echo ' class="selected"'; ?> id="dsq-tab-main" rel="dsq-main"><?php echo (dsq_is_installed() ? 'Manage' : 'Install'); ?></li>
137
+ <li<?php if ($show_advanced) echo ' class="selected"'; ?> id="dsq-tab-advanced" rel="dsq-advanced"><?php echo dsq_i('Advanced Options'); ?></li>
138
+ </ul>
139
 
140
+ <div id="dsq-main" class="dsq-content">
141
+ <?php
142
  switch ( $step ) {
143
  case 3:
144
  ?>
145
+ <div id="dsq-step-3" class="dsq-main"<?php if ($show_advanced) echo ' style="display:none;"'; ?>>
146
+ <h2><?php echo dsq_i('Install Disqus Comments'); ?></h2>
147
 
148
+ <p>Disqus has been installed on your blog.</p>
149
+ <p>If you have existing comments, you may wish to <a href="?page=disqus&amp;t=adv#export">export them</a> now. Otherwise, you're all set, and the Disqus network is now powering comments on your blog.</p>
150
+ </div>
151
  <?php
152
+ break;
153
  case 2:
154
  ?>
155
+ <div id="dsq-step-2" class="dsq-main"<?php if ($show_advanced) echo ' style="display:none;"'; ?>>
156
+ <h2><?php echo dsq_i('Install Disqus Comments'); ?></h2>
157
+
158
+ <form method="POST" action="?page=disqus&amp;step=3">
159
+ <?php wp_nonce_field('dsq-install-2'); ?>
160
+ <table class="form-table">
161
+ <tr>
162
+ <th scope="row" valign="top"><?php echo dsq_i('Select a website'); ?></th>
163
+ <td>
164
  <?php
165
  foreach ( $dsq_sites as $counter => $dsq_site ):
166
  ?>
167
+ <input name="dsq_forum" type="radio" id="dsq-site-<?php echo $counter; ?>" value="<?php echo $dsq_site->id; ?>:<?php echo $dsq_site->shortname; ?>" />
168
+ <label for="dsq-site-<?php echo $counter; ?>"><strong><?php echo htmlspecialchars($dsq_site->name); ?></strong> (<u><?php echo $dsq_site->shortname; ?>.disqus.com</u>)</label>
169
+ <br />
170
  <?php
171
  endforeach;
172
  ?>
173
+ <hr />
174
+ <a href="<?php echo DISQUS_URL; ?>comments/register/"><?php echo dsq_i('Or register a new one on the Disqus website.'); ?></a>
175
+ </td>
176
+ </tr>
177
+ </table>
178
+
179
+ <p class="submit" style="text-align: left">
180
+ <input type="hidden" name="dsq_user_api_key" value="<?php echo htmlspecialchars($dsq_user_api_key); ?>"/>
181
+ <input name="submit" type="submit" value="Next &raquo;" />
182
+ </p>
183
+ </form>
184
+ </div>
185
  <?php
186
+ break;
187
  case 1:
188
  ?>
189
+ <div id="dsq-step-1" class="dsq-main"<?php if ($show_advanced) echo ' style="display:none;"'; ?>>
190
+ <h2><?php echo dsq_i('Install Disqus Comments'); ?></h2>
191
+
192
+ <form method="POST" action="?page=disqus&amp;step=2">
193
+ <?php wp_nonce_field('dsq-install-1'); ?>
194
+ <table class="form-table">
195
+ <tr>
196
+ <th scope="row" valign="top"><?php echo dsq_i('Username'); ?></th>
197
+ <td>
198
+ <input id="dsq-username" name="dsq_username" tabindex="1" type="text" />
199
+ <a href="http://disqus.com/profile/"><?php echo dsq_i('(don\'t have a Disqus Profile yet?)'); ?></a>
200
+ </td>
201
+ </tr>
202
+ <tr>
203
+ <th scope="row" valign="top"><?php echo dsq_i('Password'); ?></th>
204
+ <td>
205
+ <input type="password" id="dsq-password" name="dsq_password" tabindex="2">
206
+ <a href="http://disqus.com/forgot/"><?php echo dsq_i('(forgot your password?)'); ?></a>
207
+ </td>
208
+ </tr>
209
+ </table>
210
+
211
+ <p class="submit" style="text-align: left">
212
+ <input name="submit" type="submit" value="Next &raquo;" tabindex="3">
213
+ </p>
214
+
215
+ <script type="text/javascript"> document.getElementById('dsq-username').focus(); </script>
216
+ </form>
217
+ </div>
218
  <?php
219
+ break;
220
  case 0:
221
+ $url = get_option('disqus_forum_url');
222
  ?>
223
+ <div class="dsq-main"<?php if ($show_advanced) echo ' style="display:none;"'; ?>>
224
+ <h2><?php echo dsq_i('Comments'); ?></h2>
225
+ <iframe src="<?php if ($url) {
226
+ echo 'http://'.$url.'.'.DISQUS_DOMAIN.'/admin/moderate/';
227
+ } else {
228
+ echo DISQUS_URL.'admin/moderate/';
229
+ } ?>?template=wordpress" style="width: 100%; height: 800px"></iframe>
230
+ </div>
231
  <?php } ?>
232
+ </div>
233
 
234
  <?php
235
+ $dsq_replace = get_option('disqus_replace');
236
+ $dsq_forum_url = strtolower(get_option('disqus_forum_url'));
237
+ $dsq_api_key = get_option('disqus_api_key');
238
+ $dsq_user_api_key = get_option('disqus_user_api_key');
239
+ $dsq_partner_key = get_option('disqus_partner_key');
240
+ $dsq_cc_fix = get_option('disqus_cc_fix');
241
+ $dsq_manual_sync = get_option('disqus_manual_sync');
242
+ $dsq_disable_ssr = get_option('disqus_disable_ssr');
243
+
244
+ $dsq_public_key = get_option('disqus_public_key');
245
+ $dsq_secret_key = get_option('disqus_secret_key');
246
  ?>
247
+ <!-- Advanced options -->
248
+ <div id="dsq-advanced" class="dsq-content dsq-advanced"<?php if (!$show_advanced) echo ' style="display:none;"'; ?>>
249
+ <h2><?php echo dsq_i('Advanced Options'); ?></h2>
250
+ <p><?php echo dsq_i('Version: %s', esc_html(DISQUS_VERSION)); ?></p>
251
+ <?php
252
+ if (get_option('disqus_active') === '0') {
253
+ // disqus is not active
254
+ echo '<p class="status">Disqus comments are currently disabled. (<a href="?page=disqus&amp;active=1">Enable</a>)</p>';
255
+ } else {
256
+ echo '<p class="status">Disqus comments are currently enabled. (<a href="?page=disqus&amp;active=0">Disable</a>)</p>';
257
+ }
258
+ ?>
259
+ <form method="POST">
260
+ <?php wp_nonce_field('dsq-advanced'); ?>
261
+ <h3>Configuration</h3>
262
+ <table class="form-table">
263
+ <tr>
264
+ <th scope="row" valign="top"><?php echo dsq_i('Disqus short name'); ?></th>
265
+ <td>
266
+ <input name="disqus_forum_url" value="<?php echo esc_attr($dsq_forum_url); ?>" tabindex="1" type="text" />
267
+ <br />
268
+ <?php echo dsq_i('This is the unique identifier for your website on Disqus Comments.'); ?>
269
+ </td>
270
+ </tr>
271
+
272
+ <tr>
273
+ <th scope="row" valign="top"><?php echo dsq_i('Disqus API Key'); ?></th>
274
+ <td>
275
+ <input type="text" name="disqus_api_key" value="<?php echo esc_attr($dsq_api_key); ?>" tabindex="2">
276
+ <br />
277
+ <?php echo dsq_i('This is set for you when going through the installation steps.'); ?>
278
+ </td>
279
+ </tr>
280
+
281
+ <tr>
282
+ <th scope="row" valign="top"><?php echo dsq_i('Disqus User API Key'); ?></th>
283
+ <td>
284
+ <input type="text" name="disqus_user_api_key" value="<?php echo esc_attr($dsq_user_api_key); ?>" tabindex="2">
285
+ <br />
286
+ <?php echo dsq_i('This is set for you when going through the installation steps.'); ?>
287
+ </td>
288
+ </tr>
289
+ <?php if (!empty($dsq_partner_key)) {// this option only shows if it was already present ?>
290
+ <tr>
291
+ <th scope="row" valign="top"><?php echo dsq_i('Disqus Partner Key'); ?></th>
292
+ <td>
293
+ <input type="text" name="disqus_partner_key" value="<?php echo esc_attr($dsq_partner_key); ?>" tabindex="2">
294
+ <br />
295
+ <?php echo dsq_i('Advanced: Used for single sign-on (SSO) integration. (<a href="%s" onclick="window.open(this.href); return false">more info on SSO</a>)', 'http://disqus.com/help/sso'); ?>
296
+ </td>
297
+ </tr>
298
+ <?php } ?>
299
+ <tr>
300
+ <th scope="row" valign="top"><?php echo dsq_i('Application Public Key'); ?></th>
301
+ <td>
302
+ <input type="text" name="disqus_public_key" value="<?php echo esc_attr($dsq_public_key); ?>" tabindex="2">
303
+ <br />
304
+ <?php echo dsq_i('Advanced: Used for single sign-on (SSO) integration. (<a href="%s" onclick="window.open(this.href); return false">more info on SSO</a>)', 'http://docs.disqus.com/developers/sso/'); ?>
305
+ </td>
306
+ </tr>
307
+ <tr>
308
+ <th scope="row" valign="top"><?php echo dsq_i('Application Secret Key'); ?></th>
309
+ <td>
310
+ <input type="text" name="disqus_secret_key" value="<?php echo esc_attr($dsq_secret_key); ?>" tabindex="2">
311
+ <br />
312
+ <?php echo dsq_i('Advanced: Used for single sign-on (SSO) integration. (<a href="%s" onclick="window.open(this.href); return false">more info on SSO</a>)', 'http://docs.disqus.com/developers/sso/'); ?>
313
+ </td>
314
+ </tr>
315
+
316
+ <tr>
317
+ <th scope="row" valign="top"><?php echo dsq_i('Use Disqus Comments on'); ?></th>
318
+ <td>
319
+ <select name="disqus_replace" tabindex="3" class="disqus-replace">
320
+ <option value="all" <?php if('all'==$dsq_replace){echo 'selected';}?>><?php echo dsq_i('On all existing and future blog posts.'); ?></option>
321
+ <option value="closed" <?php if('closed'==$dsq_replace){echo 'selected';}?>><?php echo dsq_i('Only on blog posts with closed comments.'); ?></option>
322
+ </select>
323
+ <br />
324
+ <?php echo dsq_i('NOTE: Your WordPress comments will never be lost.'); ?>
325
+ </td>
326
+ </tr>
327
+
328
+ <tr>
329
+ <th scope="row" valign="top"><?php echo dsq_i('Comment Counts'); ?></th>
330
+ <td>
331
+ <input type="checkbox" id="disqus_comment_count" name="disqus_cc_fix" <?php if($dsq_cc_fix){echo 'checked="checked"';}?> >
332
+ <label for="disqus_comment_count"><?php echo dsq_i('Output JavaScript in footer'); ?></label>
333
+ <br /><?php echo dsq_i('NOTE: Check this if you have problems with the comment count displays including: not showing on permalinks, broken featured image carousels, or longer-than-usual homepage load times (<a href="%s" onclick="window.open(this.href); return false">more info</a>).', 'http://disqus.com/help/wordpress'); ?>
334
+ </td>
335
+ </tr>
336
+
337
+ <tr>
338
+ <th scope="row" valign="top"><?php echo dsq_i('Comment Sync'); ?></th>
339
+ <td>
340
+ <input type="checkbox" id="disqus_manual_sync" name="disqus_manual_sync" <?php if($dsq_manual_sync){echo 'checked="checked"';}?> >
341
+ <label for="disqus_manual_sync"><?php echo dsq_i('Disable automated comment importing'); ?></label>
342
+ <br /><?php echo dsq_i('NOTE: If you have problems with WP cron taking too long and large numbers of comments you may wish to disable the automated sync cron. Keep in mind that this means comments will not automatically get synced to your local Wordpress database.'); ?>
343
+ </td>
344
+ </tr>
345
+
346
+ <tr>
347
+ <th scope="row" valign="top"><?php echo dsq_i('Server Side Rendering'); ?></th>
348
+ <td>
349
+ <input type="checkbox" id="disqus_disable_ssr" name="disqus_disable_ssr" <?php if($dsq_disable_ssr){echo 'checked="checked"';}?> >
350
+ <label for="disqus_disable_ssr"><?php echo dsq_i('Disable server side rendering of comments'); ?></label>
351
+ <br /><?php echo dsq_i('NOTE: This will hide comments from nearly all search engines'); ?>
352
+ </td>
353
+ </tr>
354
+ </table>
355
+
356
+ <p class="submit" style="text-align: left">
357
+ <input name="submit" type="submit" value="Save" class="button-primary button" tabindex="4">
358
+ </p>
359
+ </form>
360
+
361
+ <h3>Import / Export</h3>
362
+
363
+ <table class="form-table">
364
+ <?php if (DISQUS_CAN_EXPORT): ?>
365
+ <tr id="export">
366
+ <th scope="row" valign="top"><?php echo dsq_i('Export comments to Disqus'); ?></th>
367
+ <td>
368
+ <div id="dsq_export">
369
+ <p class="status"><a href="#" class="button"><?php echo dsq_i('Export Comments'); ?></a> <?php echo dsq_i('This will export your existing WordPress comments to Disqus'); ?></p>
370
+ </div>
371
+ </td>
372
+ </tr>
373
+ <?php endif; ?>
374
+ <tr>
375
+ <th scope="row" valign="top"><?php echo dsq_i('Sync Disqus with WordPress'); ?></th>
376
+ <td>
377
+ <div id="dsq_import">
378
+ <p class="status"><a href="#" class="button"><?php echo dsq_i('Sync Comments'); ?></a> <label><input type="checkbox" id="dsq_import_wipe" name="dsq_import_wipe" value="1"/> <?php echo dsq_i('Remove all imported Disqus comments before syncing.'); ?></label><br/><br/>
379
+ <?php echo dsq_i('This will download your Disqus comments and store them locally in WordPress'); ?></p>
380
+ </div>
381
+ </td>
382
+ </tr>
383
+ </table>
384
+
385
+ <h3>Uninstall</h3>
386
+
387
+ <table class="form-table">
388
+ <tr>
389
+ <th scope="row" valign="top"><?php echo dsq_i('Uninstall Disqus Comments'); ?></th>
390
+ <td>
391
+ <form action="?page=disqus" method="POST">
392
+ <?php wp_nonce_field('dsq-uninstall'); ?>
393
+ <p>This will remove all Disqus specific settings, but it will leave your comments unaffected.</p>
394
+ <input type="submit" value="Uninstall" name="uninstall" onclick="return confirm('<?php echo dsq_i('Are you sure you want to uninstall Disqus?'); ?>')" class="button" />
395
+ </form>
396
+ </td>
397
+ </tr>
398
+ </table>
399
+ <br/>
400
+ <h3><?php echo dsq_i('Debug Information'); ?></h3>
401
+ <p><?php echo dsq_i('Having problems with the plugin? <a href="%s">Drop us a line</a> and include the following details and we\'ll do what we can.', 'mailto:help+wp@disqus.com'); ?></p>
402
+ <textarea style="width:90%; height:200px;">URL: <?php echo get_option('siteurl'); ?>
403
  Version: <?php echo $wp_version; ?>
404
  Active Theme: <?php $theme = get_theme(get_current_theme()); echo $theme['Name'].' '.$theme['Version']; ?>
405
  URLOpen Method: <?php echo dsq_url_method(); ?>
409
  Settings:
410
 
411
  dsq_is_installed: <?php echo dsq_is_installed(); ?>
412
+
413
  <?php foreach (dsq_options() as $opt) {
414
+ echo $opt.': '.get_option($opt)."\n";
415
  } ?>
416
 
417
  Plugins:
418
 
419
  <?php
420
  foreach (get_plugins() as $plugin) {
421
+ echo $plugin['Name'].' '.$plugin['Version']."\n";
422
  }
423
  ?></textarea><br/>
424
+ </div>
425
  </div>
{images → media/images}/logo.png RENAMED
File without changes
{styles → media/styles}/manage-pre25.css RENAMED
File without changes
{styles → media/styles}/manage.css RENAMED
@@ -28,7 +28,7 @@ select.disqus-replace {
28
  }
29
 
30
  .dsq-content h2 {
31
- background: url(../images/logo.png) no-repeat;
32
  line-height: 41px;
33
  margin: 15px 0 10px;
34
  padding: 0 0 0 150px;
28
  }
29
 
30
  .dsq-content h2 {
31
+ background: url(../media/images/logo.png) no-repeat;
32
  line-height: 41px;
33
  margin: 15px 0 10px;
34
  padding: 0 0 0 150px;
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: disqus, alexkingorg, crowdfavorite
3
  Tags: comments, threaded, email, notification, spam, avatars, community, profile, widget, disqus
4
  Requires at least: 2.8
5
  Tested up to: 3.0
6
- Stable tag: 2.61
7
 
8
  The Disqus comment system replaces your WordPress comment system with your comments hosted and powered by Disqus.
9
 
@@ -62,6 +62,23 @@ you should remove it, and the new plugin should be stored in 'disqus'.
62
 
63
  == Changes ==
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  2.61
66
 
67
  * Fixed an issue which was causing invalid information to be presented in RSS feeds.
3
  Tags: comments, threaded, email, notification, spam, avatars, community, profile, widget, disqus
4
  Requires at least: 2.8
5
  Tested up to: 3.0
6
+ Stable tag: 2.64
7
 
8
  The Disqus comment system replaces your WordPress comment system with your comments hosted and powered by Disqus.
9
 
62
 
63
  == Changes ==
64
 
65
+ 2.64
66
+
67
+ * Added an option to disable Disqus without deactivating the plugin.
68
+ * Added a second check for comment sync to prevent stampede race conditions in WP cron.
69
+
70
+ 2.63
71
+
72
+ * Added command line script to import comments from DISQUS (scripts/import-comments.php).
73
+ * Added command line script to export comments to DISQUS (scripts/export-comments.php).
74
+ * The exporter will now only do one post at a time.
75
+ * The exporter now only sends required attributes to DISQUS.
76
+ * Moved media into its own directory.
77
+
78
+ 2.62
79
+
80
+ * Changed legacy query to use = operator instead of LIKE so it can be indexed.
81
+
82
  2.61
83
 
84
  * Fixed an issue which was causing invalid information to be presented in RSS feeds.
scripts/export-comments.php ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/php
2
+ <?php
3
+ /**
4
+ * Incrementally (in chunks of EXPORT_CHUNK_SIZE) exports all comments to DISQUS.
5
+ *
6
+ * ``php export-comments.php``
7
+ */
8
+
9
+ require_once(dirname(__FILE__) . '/../lib/wp-cli.php');
10
+ require_once(dirname(__FILE__) . '/../disqus.php');
11
+ require_once(dirname(__FILE__) . '/../export.php');
12
+
13
+ define('EXPORT_CHUNK_SIZE', 100);
14
+
15
+ $forum_url = get_option('disqus_forum_url');
16
+
17
+ if (empty($forum_url)) {
18
+ print_line("Disqus has not been configured on this installation!");
19
+ die();
20
+ }
21
+
22
+ print_line('---------------------------------------------------------');
23
+ print_line('Discovered DISQUS forum shortname as %s', $forum_url);
24
+ print_line('---------------------------------------------------------');
25
+
26
+ global $wpdb, $dsq_api;
27
+
28
+ $timestamp = 0;
29
+ $post_id = 0;
30
+ $total = 0;
31
+ $eof = 0;
32
+ $total_exported = 0;
33
+ $global_start = microtime();
34
+
35
+ $max_post_id = $wpdb->get_var($wpdb->prepare("
36
+ SELECT MAX(ID)
37
+ FROM $wpdb->posts
38
+ WHERE post_type != 'revision'
39
+ AND post_status = 'publish'
40
+ AND comment_count > 0
41
+ AND ID > %d
42
+ ", $post_id));
43
+
44
+ print_line('Max post id is %d', $max_post_id);
45
+
46
+ while ($post_id < $max_post_id) {
47
+ $start = microtime();
48
+
49
+ $post = $wpdb->get_results($wpdb->prepare("
50
+ SELECT *
51
+ FROM $wpdb->posts
52
+ WHERE post_type != 'revision'
53
+ AND post_status = 'publish'
54
+ AND comment_count > 0
55
+ AND ID > %d
56
+ ORDER BY ID ASC
57
+ LIMIT 1
58
+ ", $post_id));
59
+ $post = $post[0];
60
+ $post_id = $post->ID;
61
+
62
+ print_line(' Exporting comments for post id %d', $post_id);
63
+
64
+ $response = null;
65
+ $query = $wpdb->get_results( $wpdb->prepare("SELECT COUNT(*) as total FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_agent NOT LIKE 'Disqus/%%' LIMIT ".EXPORT_CHUNK_SIZE, $post_id) );
66
+ $total_comments = $query[0]->total;
67
+
68
+ $comments = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_agent NOT LIKE 'Disqus/%%' LIMIT ".EXPORT_CHUNK_SIZE, $post_id) );
69
+ $group_id = null;
70
+ $at = 0;
71
+
72
+ // we need to send empty files to ensure EOF happens
73
+ while (($at === 0 && $post_id == $max_post_id) || $at < $total_comments) {
74
+ if ($post_id == $max_post_id && ($at + EXPORT_CHUNK_SIZE) >= $total_comments) {
75
+ $eof = 1;
76
+ }
77
+ $wxr = dsq_export_wp($post, $comments);
78
+ $response = $dsq_api->import_wordpress_comments($wxr, $timestamp, $eof);
79
+ if (!($response['group_id'] > 0)) {
80
+ print_line('---------------------------------------------------------');
81
+ print_line('There was an error communicating with DISQUS!');
82
+ print_line($dsq_api->get_last_error());
83
+ print_line('---------------------------------------------------------');
84
+ }
85
+ $group_id = $response['group_id'];
86
+ print_line(' %d comments exported', count($comments), $time);
87
+ $total_exported += count($comments);
88
+ $at += EXPORT_CHUNK_SIZE;
89
+ $comments = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_agent NOT LIKE 'Disqus/%%' LIMIT ".EXPORT_CHUNK_SIZE." OFFSET {$at}", $post->ID) );
90
+ }
91
+
92
+ $time = abs(microtime() - $start);
93
+ print_line(' Done! (took %.2fs)', $time);
94
+ }
95
+ $total_time = abs(microtime() - $global_start);
96
+ print_line('---------------------------------------------------------');
97
+ print_line('Done (processing took %.2fs)! %d comments were sent to DISQUS', $total_time, $total_exported);
98
+ if ($group_id) {
99
+ print_line('');
100
+ print_line('Status available at http://import.disqus.com/group/%d/', $group_id);
101
+ }
102
+ print_line('---------------------------------------------------------');
103
+ ?>
scripts/import-comments.php ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/php
2
+ <?php
3
+ /**
4
+ * Incrementally downloads all comments from DISQUS.
5
+ *
6
+ * ``php import-comments.php``
7
+ */
8
+
9
+ require_once(dirname(__FILE__) . '/../lib/wp-cli.php');
10
+ require_once(dirname(__FILE__) . '/../disqus.php');
11
+
12
+ $forum_url = get_option('disqus_forum_url');
13
+
14
+ if (empty($forum_url)) {
15
+ print_line("Disqus has not been configured on this installation!");
16
+ die();
17
+ }
18
+
19
+ print_line('---------------------------------------------------------');
20
+ print_line('Discovered DISQUS forum shortname as %s', $forum_url);
21
+ print_line('---------------------------------------------------------');
22
+
23
+ $imported = true;
24
+ if (in_array('--reset', $argv)) {
25
+ $last_comment_id = 0;
26
+ } else {
27
+ $last_comment_id = get_option('disqus_last_comment_id');
28
+ }
29
+ $total = 0;
30
+ $global_start = microtime();
31
+
32
+ while ($imported) {
33
+ print_line(' Importing chunk starting at comment id %d', $last_comment_id);
34
+ $start = microtime();
35
+ $result = dsq_sync_forum($last_comment_id);
36
+ if ($result === false) {
37
+ print_line('---------------------------------------------------------');
38
+ print_line('There was an error communicating with DISQUS!');
39
+ print_line($dsq_api->get_last_error());
40
+ print_line('---------------------------------------------------------');
41
+ die();
42
+ break;
43
+ } else {
44
+ list($imported, $last_comment_id) = $result;
45
+ }
46
+ $total += $imported;
47
+ $time = abs(microtime() - $start);
48
+ print_line(' %d comments imported (took %.2fs)', $imported, $time);
49
+ }
50
+ $total_time = abs(microtime() - $global_start);
51
+ print_line('---------------------------------------------------------');
52
+ print_line('Done (took %.2fs)! %d comments imported from DISQUS', $total_time, $total);
53
+ print_line('---------------------------------------------------------');
54
+ ?>
scripts/manage.js DELETED
@@ -1,38 +0,0 @@
1
- var dsq_old_onload = window.onload;
2
-
3
- function dsq_tab_func(clicked_tab) {
4
- function _dsq_tab_func(e) {
5
- var tabs = document.getElementById('dsq-tabs').getElementsByTagName('li');
6
- var contents = document.getElementById('dsq-wrap').getElementsByTagName('div');
7
-
8
- for(var i = 0; i < tabs.length; i++) {
9
- tabs[i].className = '';
10
- }
11
-
12
- for(var i = 0; i < contents.length; i++) {
13
- if(contents[i].className == 'dsq-content') {
14
- contents[i].style.display = 'none';
15
- }
16
- }
17
-
18
- document.getElementById('dsq-tab-' + clicked_tab).className = 'selected';
19
- document.getElementById('dsq-' + clicked_tab).style.display = 'block';
20
-
21
- }
22
-
23
- return _dsq_tab_func;
24
- }
25
-
26
- window.onload = function(e) {
27
- // Tabs have an ID prefixed with "dsq-tab-".
28
- // Content containers have an ID prefixed with "dsq-" and a class name of "dsq-content".
29
- var tabs = document.getElementById('dsq-tabs').getElementsByTagName('li');
30
-
31
- for(var i = 0; i < tabs.length; i++) {
32
- tabs[i].onclick = dsq_tab_func(tabs[i].id.substr(tabs[i].id.lastIndexOf('-') + 1));
33
- }
34
-
35
- if(dsq_old_onload) {
36
- dsq_old_onload(e);
37
- }
38
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
upgrade.php CHANGED
@@ -2,18 +2,19 @@
2
  require(ABSPATH . 'wp-includes/version.php');
3
 
4
  if ( !current_user_can('manage_options') ) {
5
- die();
6
  }
7
 
 
8
 
9
  ?>
10
  <div class="wrap">
11
- <h2><?php echo dsq_i('Upgrade Disqus Comments'); ?></h2>
12
- <form method="POST" action="?page=disqus&amp;step=<?php echo $_GET['step']; ?>">
13
- <p>You need to upgrade your database to continue.</p>
14
 
15
- <p class="submit" style="text-align: left">
16
- <input type="submit" name="upgrade" value="Upgrade &raquo;" />
17
- </p>
18
- </form>
19
  </div>
2
  require(ABSPATH . 'wp-includes/version.php');
3
 
4
  if ( !current_user_can('manage_options') ) {
5
+ die();
6
  }
7
 
8
+ $step = (isset($_GET['step']) ? $_GET['step'] : null);
9
 
10
  ?>
11
  <div class="wrap">
12
+ <h2><?php echo dsq_i('Upgrade Disqus Comments'); ?></h2>
13
+ <form method="POST" action="?page=disqus&amp;step=<?php echo $step; ?>">
14
+ <p>You need to upgrade your database to continue.</p>
15
 
16
+ <p class="submit" style="text-align: left">
17
+ <input type="submit" name="upgrade" value="Upgrade &raquo;" />
18
+ </p>
19
+ </form>
20
  </div>