/wp-content/plugins/social/lib/social/controller/aggregation.php

https://gitlab.com/Blueprint-Marketing/interoccupy.net · PHP · 178 lines · 135 code · 26 blank · 17 comment · 26 complexity · cf01e662d597493be4412425185f542a MD5 · raw file

  1. <?php
  2. /**
  3. * Aggregation controller
  4. *
  5. * @package Social
  6. * @subpackage controllers
  7. */
  8. final class Social_Controller_Aggregation extends Social_Controller {
  9. /**
  10. * Runs the aggregation for the requested post ID.
  11. *
  12. * @return void
  13. */
  14. public function action_run() {
  15. $this->verify_nonce();
  16. $fetch = Social::option('aggregate_comments');
  17. if (empty($fetch)) {
  18. Social::log('Aggregation has been disabled, exiting.');
  19. return;
  20. }
  21. $post = get_post($this->request->query('post_id'));
  22. if ($post === null) {
  23. return;
  24. }
  25. Social::log('Begin aggregation for post #:post_id.', array(
  26. 'post_id' => $post->ID,
  27. ));
  28. // Get URLs to query
  29. $default_urls = array(
  30. home_url('?p='.$post->ID)
  31. );
  32. $url = wp_get_shortlink($post->ID);
  33. if (strpos($url, '?p=') === false) {
  34. $default_urls[] = $url;
  35. }
  36. // Add the permalink?
  37. $permalink = get_permalink($post->ID);
  38. if ($default_urls[0] != $permalink) {
  39. $default_urls[] = $permalink;
  40. }
  41. $broadcasted_ids = get_post_meta($post->ID, '_social_broadcasted_ids', true);
  42. if (empty($broadcasted_ids)) {
  43. $broadcasted_ids = array();
  44. }
  45. $aggregated_ids = get_post_meta($post->ID, '_social_aggregated_ids', true);
  46. if (empty($aggregated_ids)) {
  47. $aggregated_ids = array();
  48. }
  49. $post->broadcasted_ids = $broadcasted_ids;
  50. $post->aggregated_ids = $aggregated_ids;
  51. $post->results = array();
  52. foreach ($this->social->services() as $key => $service) {
  53. $urls = $default_urls;
  54. $post->results[$key] = array();
  55. if (!isset($post->aggregated_ids[$key])) {
  56. $post->aggregated_ids[$key] = array();
  57. }
  58. if (isset($broadcasted_ids[$key]) and count($broadcasted_ids[$key])) {
  59. $service->aggregate_by_api($post);
  60. foreach ($broadcasted_ids[$key] as $broadcasted) {
  61. foreach ($broadcasted as $data) {
  62. if (isset($data['urls']) and is_array($data['urls'])) {
  63. foreach ($data['urls'] as $url) {
  64. $urls[] = $url;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. // URL Search
  71. $urls = apply_filters('social_search_urls', $urls, $key);
  72. $urls = array_unique($urls);
  73. if (count($urls)) {
  74. foreach ($urls as $key => $url) {
  75. $urls[$key] = urlencode($url);
  76. }
  77. $service->aggregate_by_url($post, $urls);
  78. }
  79. }
  80. if (count($post->results)) {
  81. foreach ($post->results as $key => $results) {
  82. if (count($results)) {
  83. $this->social->service($key)->save_aggregated_comments($post);
  84. }
  85. }
  86. update_post_meta($post->ID, '_social_aggregated_ids', $post->aggregated_ids);
  87. }
  88. Social::log('Aggregation for post #:post_id complete.', array(
  89. 'post_id' => $post->ID,
  90. ));
  91. // Some cleanup...
  92. unset($post->broadcasted_ids);
  93. unset($post->aggregated_ids);
  94. unset($post->results);
  95. if ($this->request->is_ajax()) {
  96. // Re-add to the queue?
  97. $queue = Social_Aggregation_Queue::factory();
  98. if (!$queue->find($post->ID)) {
  99. $queue->add($post->ID, '24hr')->save();
  100. }
  101. $log = Social_Aggregation_Log::instance($post->ID);
  102. $log->save(true);
  103. if (isset($_GET['render']) and $_GET['render'] == 'false') {
  104. $total = 0;
  105. $log = $log->current();
  106. if (isset($log->items)) {
  107. foreach ($log->items as $service => $items) {
  108. foreach ($items as $item) {
  109. if (!$item->ignored) {
  110. ++$total;
  111. }
  112. }
  113. }
  114. }
  115. $awaiting_mod = wp_count_comments();
  116. $awaiting_mod = $awaiting_mod->moderated;
  117. $link = esc_url(admin_url('edit-comments.php?p='.$post->ID));
  118. $html = '';
  119. if (!isset($_GET['hide_li']) or $_GET['hide_li'] == 'false') {
  120. $html = '<li id="wp-adminbar-comments-social">';
  121. }
  122. $html .= '<a href="'.$link.'"><span class="social-aggregation-results">'.sprintf(__('(%s New)', 'social'), $total).'</span></a>';
  123. if (!isset($_GET['hide_li']) or $_GET['hide_li'] == 'false') {
  124. $html .= '</li>';
  125. }
  126. $response = array(
  127. 'total' => number_format_i18n($awaiting_mod),
  128. 'link' => $link,
  129. 'html' => $html,
  130. );
  131. echo json_encode($response);
  132. }
  133. else {
  134. $queue = $queue->find($post->ID);
  135. $next_run = 0;
  136. if ($queue !== false) {
  137. $next_run = Social_Aggregation_Queue::next_run($queue->next_run);
  138. }
  139. echo json_encode(array(
  140. 'html' => $log->render(),
  141. 'next_run' => $next_run,
  142. ));
  143. }
  144. exit;
  145. }
  146. else {
  147. Social_Aggregation_Log::instance($post->ID)->save();
  148. }
  149. // Decrement the semaphore
  150. Social_Semaphore::factory()->decrement();
  151. }
  152. } // End Social_Controller_Aggregation