PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/disqus-comment-system/public/class-disqus-public.php

https://bitbucket.org/srimegha/webgurukul
PHP | 311 lines | 141 code | 37 blank | 133 comment | 19 complexity | 32a6776e7d934112eb9f22d93bdbe64f MD5 | raw file
Possible License(s): GPL-2.0, MIT, Apache-2.0
  1. <?php
  2. /**
  3. * The public-facing functionality of the plugin.
  4. *
  5. * @link http://example.com
  6. * @since 3.0
  7. *
  8. * @package Plugin_Name
  9. * @subpackage Plugin_Name/public
  10. */
  11. /**
  12. * The public-facing functionality of the plugin.
  13. *
  14. * Defines the plugin name, version, and two examples hooks for how to
  15. * enqueue the admin-specific stylesheet and JavaScript.
  16. *
  17. * @package Plugin_Name
  18. * @subpackage Plugin_Name/public
  19. * @author Your Name <email@example.com>
  20. */
  21. class Disqus_Public {
  22. /**
  23. * Returns the Disqus identifier for a given post.
  24. *
  25. * @since 3.0
  26. * @param WP_Post $post The WordPress post to create the title for.
  27. * @return string The formatted identifier to be passed to Disqus.
  28. */
  29. public static function dsq_identifier_for_post( $post ) {
  30. return $post->ID . ' ' . $post->guid;
  31. }
  32. /**
  33. * Returns the Disqus title for a given post.
  34. *
  35. * @since 3.0
  36. * @param WP_Post $post The WordPress post to create the title for.
  37. * @return string The cleaned title to be passed to Disqus.
  38. */
  39. public static function dsq_title_for_post( $post ) {
  40. $title = get_the_title( $post );
  41. $title = strip_tags( $title, '<b><u><i><h1><h2><h3><code><blockquote><br><hr>' );
  42. return $title;
  43. }
  44. /**
  45. * Returns the signed payload to authenticate an SSO user in Disqus.
  46. *
  47. * @since 3.0
  48. * @param WP_User $user The WordPress user to authenticate.
  49. * @param string $secret_key The Disqus API Secret Key.
  50. * @return array The signed payload to authenticate a user with Single Sign-On.
  51. */
  52. public static function remote_auth_s3_for_user( $user, $secret_key ) {
  53. $payload_user = array();
  54. if ( $user->ID ) {
  55. $payload_user['id'] = $user->ID;
  56. $payload_user['username'] = $user->display_name;
  57. $payload_user['avatar'] = get_avatar( $user->ID, 92 );
  58. $payload_user['email'] = $user->user_email;
  59. $payload_user['url'] = $user->user_url;
  60. }
  61. $payload_user = base64_encode( json_encode( $payload_user ) );
  62. $time = time();
  63. $hmac = hash_hmac( 'sha1', $payload_user . ' ' . $time, $secret_key );
  64. return $payload_user . ' ' . $hmac . ' ' . $time;
  65. }
  66. /**
  67. * Returns the Disqus comments embed configuration.
  68. *
  69. * @since 3.0
  70. * @access private
  71. * @param WP_Post $post The WordPress post to create the configuration for.
  72. * @return array The embed configuration to localize the comments embed script with.
  73. */
  74. public static function embed_vars_for_post( $post ) {
  75. global $DISQUSVERSION;
  76. $embed_vars = array(
  77. 'disqusConfig' => array(
  78. 'integration' => 'wordpress ' . $DISQUSVERSION,
  79. ),
  80. 'disqusIdentifier' => Disqus_Public::dsq_identifier_for_post( $post ),
  81. 'disqusShortname' => get_option( 'disqus_forum_url' ),
  82. 'disqusTitle' => Disqus_Public::dsq_title_for_post( $post ),
  83. 'disqusUrl' => get_permalink( $post ),
  84. 'postId' => $post->ID,
  85. );
  86. $public_key = get_option( 'disqus_public_key' );
  87. $secret_key = get_option( 'disqus_secret_key' );
  88. $can_enable_sso = $public_key && $secret_key && get_option( 'disqus_sso_enabled' );
  89. if ( $can_enable_sso ) {
  90. $user = wp_get_current_user();
  91. $login_redirect = get_admin_url( null, 'profile.php?opener=dsq-sso-login' );
  92. $embed_vars['disqusConfig']['sso'] = array(
  93. 'name' => esc_js( get_bloginfo( 'name' ) ),
  94. 'button' => esc_js( get_option( 'disqus_sso_button' ) ),
  95. 'url' => wp_login_url( $login_redirect ),
  96. 'logout' => wp_logout_url(),
  97. 'width' => '800',
  98. 'height' => '700',
  99. );
  100. $embed_vars['disqusConfig']['api_key'] = $public_key;
  101. $embed_vars['disqusConfig']['remote_auth_s3'] = Disqus_Public::remote_auth_s3_for_user( $user, $secret_key );
  102. }
  103. return $embed_vars;
  104. }
  105. /**
  106. * The ID of this plugin.
  107. *
  108. * @since 3.0
  109. * @access private
  110. * @var string $disqus The ID of this plugin.
  111. */
  112. private $disqus;
  113. /**
  114. * The version of this plugin.
  115. *
  116. * @since 3.0
  117. * @access private
  118. * @var string $version The current version of this plugin.
  119. */
  120. private $version;
  121. /**
  122. * The unique Disqus forum shortname.
  123. *
  124. * @since 3.0
  125. * @access private
  126. * @var string $shortname The unique Disqus forum shortname.
  127. */
  128. private $shortname;
  129. /**
  130. * Initialize the class and set its properties.
  131. *
  132. * @since 3.0
  133. * @param string $disqus The name of the plugin.
  134. * @param string $version The version of this plugin.
  135. * @param string $shortname The configured Disqus shortname.
  136. */
  137. public function __construct( $disqus, $version, $shortname ) {
  138. $this->disqus = $disqus;
  139. $this->version = $version;
  140. $this->shortname = $shortname;
  141. }
  142. /**
  143. * Determines if Disqus is configured and can load on a given page.
  144. *
  145. * @since 3.0
  146. * @param string $comment_text The default comment text.
  147. * @return string The new comment text.
  148. */
  149. public function dsq_comments_link_template( $comment_text ) {
  150. global $post;
  151. if ( $this->dsq_can_load( 'count' ) ) {
  152. $disqus_identifier = esc_attr( $this->dsq_identifier_for_post( $post ) );
  153. return '<span class="dsq-postid" data-dsqidentifier="' . $disqus_identifier . '">'
  154. . $comment_text .
  155. '</span>';
  156. } else {
  157. return $comment_text;
  158. }
  159. }
  160. /**
  161. * Returns the Disqus embed comments template
  162. *
  163. * @since 3.0
  164. * @return string The new comment text.
  165. */
  166. public function dsq_comments_template() {
  167. global $post;
  168. if ( $this->dsq_embed_can_load_for_post( $post ) ) {
  169. do_action( 'dsq_before_comments' );
  170. do_action( 'dsq_enqueue_comments_script' );
  171. return plugin_dir_path( dirname( __FILE__ ) ) . 'public/partials/disqus-public-display.php';
  172. }
  173. }
  174. /**
  175. * Renders a script which checks to see if the window was opened
  176. * by the Disqus embed for Single Sign-on purposes, and closes
  177. * itself.
  178. *
  179. * @since 3.0
  180. */
  181. public function dsq_close_window_template() {
  182. require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/partials/disqus-public-sso-login-profile.php';
  183. }
  184. /**
  185. * Enqueues javascript files for displaying comment counts.
  186. *
  187. * @since 3.0
  188. */
  189. public function enqueue_comment_count() {
  190. if ( $this->dsq_can_load( 'count' ) ) {
  191. $count_vars = array(
  192. 'disqusShortname' => $this->shortname,
  193. );
  194. wp_enqueue_script( $this->disqus . '_count', plugin_dir_url( __FILE__ ) . 'js/comment_count.js', array(), $this->version, true );
  195. wp_localize_script( $this->disqus . '_count', 'countVars', $count_vars );
  196. }
  197. }
  198. /**
  199. * Enqueues javascript files for displaying the comment embed.
  200. *
  201. * @since 3.0
  202. */
  203. public function enqueue_comment_embed() {
  204. global $post;
  205. if ( $this->dsq_embed_can_load_for_post( $post ) && ! get_option( 'disqus_render_js' ) ) {
  206. $embed_vars = Disqus_Public::embed_vars_for_post( $post );
  207. wp_enqueue_script( $this->disqus . '_embed', plugin_dir_url( __FILE__ ) . 'js/comment_embed.js', array(), $this->version, true );
  208. wp_localize_script( $this->disqus . '_embed', 'embedVars', $embed_vars );
  209. }
  210. }
  211. /**
  212. * Determines if Disqus is configured and can load on a given page.
  213. *
  214. * @since 3.0
  215. * @access private
  216. * @param string $script_name The name of the script Disqus intends to load.
  217. * @return boolean Whether Disqus is configured properly and can load on the current page.
  218. */
  219. private function dsq_can_load( $script_name ) {
  220. // Don't load any Disqus scripts if there's no shortname.
  221. if ( ! $this->shortname ) {
  222. return false;
  223. }
  224. // Don't load any Disqus scripts on feed pages.
  225. if ( is_feed() ) {
  226. return false;
  227. }
  228. $site_allows_load = apply_filters( 'dsq_can_load', $script_name );
  229. if ( is_bool( $site_allows_load ) ) {
  230. return $site_allows_load;
  231. }
  232. return true;
  233. }
  234. /**
  235. * Determines if Disqus is configured and can the comments embed on a given page.
  236. *
  237. * @since 3.0
  238. * @access private
  239. * @param WP_Post $post The WordPress post used to determine if Disqus can be loaded.
  240. * @return boolean Whether Disqus is configured properly and can load on the current page.
  241. */
  242. private function dsq_embed_can_load_for_post( $post ) {
  243. // Checks if the plugin is configured properly
  244. // and is a valid page.
  245. if ( ! $this->dsq_can_load( 'embed' ) ) {
  246. return false;
  247. }
  248. // Make sure we have a $post object.
  249. if ( ! isset( $post ) ) {
  250. return false;
  251. }
  252. // Don't load embed for certain types of non-public posts because these post types typically still have the
  253. // ID-based URL structure, rather than a friendly permalink URL.
  254. $illegal_post_statuses = array(
  255. 'draft',
  256. 'auto-draft',
  257. 'pending',
  258. 'future',
  259. 'trash',
  260. );
  261. if ( in_array( $post->post_status, $illegal_post_statuses ) ) {
  262. return false;
  263. }
  264. // Don't load embed when comments are closed on a post.
  265. if ( 'open' != $post->comment_status ) {
  266. return false;
  267. }
  268. // Don't load embed if it's not a single post page.
  269. if ( ! is_singular() ) {
  270. return false;
  271. }
  272. return true;
  273. }
  274. }