PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/social/aggregation/log.php

https://github.com/tgharoutunian/wp-social
PHP | 188 lines | 73 code | 21 blank | 94 comment | 7 complexity | 3494dbc5e203d4fbf205b884a48a8f37 MD5 | raw file
  1. <?php
  2. /**
  3. * Aggregation Logger
  4. *
  5. * @package Social
  6. * @subpackage aggregation
  7. */
  8. final class Social_Aggregation_Log {
  9. /**
  10. * @var array array of singleton instances
  11. */
  12. private static $instances = array();
  13. /**
  14. * Initializes the aggregation logger for the defined post ID.
  15. *
  16. * [!!!] If the post ID is invalid an Exception will be thrown.
  17. *
  18. * @static
  19. * @param int $post_id
  20. * @return Social_Aggregation_Log
  21. */
  22. public static function instance($post_id) {
  23. if (!isset(self::$instances[$post_id])) {
  24. self::$instances[$post_id] = new self($post_id);
  25. }
  26. return self::$instances[$post_id];
  27. }
  28. /**
  29. * @var array array of log items
  30. */
  31. private $_log = array();
  32. /**
  33. * @var int log timestamp
  34. */
  35. private $_timestamp = 0;
  36. /**
  37. * @var int post ID
  38. */
  39. private $_post_id = 0;
  40. /**
  41. * Loads the current log for the defined post ID.
  42. *
  43. * [!] For 1.0.2 we changed the logger to have a manual flag for each pull. This will
  44. * apply the update if the loaded log is from older versions of the plugin.
  45. *
  46. * @param int $post_id
  47. */
  48. public function __construct($post_id) {
  49. $this->_post_id = $post_id;
  50. $this->_timestamp = current_time('timestamp', 1);
  51. $post = get_post($this->_post_id);
  52. if ($post === null) {
  53. throw new Exception(sprintf(__('Social failed to initialize the Aggregation_Log for post #%s.', 'social'), $this->_post_id));
  54. }
  55. // Load the current log for the post
  56. $this->_log = get_post_meta($post_id, '_social_aggregation_log', true);
  57. }
  58. /**
  59. * Hook caught by echoing Social_Aggregate_Log.
  60. *
  61. * @return string
  62. */
  63. public function __toString() {
  64. return $this->render();
  65. }
  66. /**
  67. * Adds an item to the log.
  68. *
  69. * Log format:
  70. *
  71. * array(
  72. * '1234567890' => (object) array( // current_time('timestamp')
  73. * 'manual' => false, // Can be "true" or "false"
  74. * 'items' => array(
  75. * 'twitter' => array(
  76. * (object) array(
  77. * 'id' => '1234567890', // Broadcasted ID
  78. * 'type' => 'url', // url|reply|retweet or whatever you want to group this item by
  79. * 'ignored' => false, // Can be "true" or "false"
  80. * 'data' => array(), // Extra data you want to use when displaying this item in the View
  81. * ),
  82. * // ... More items
  83. * ),
  84. * // ... Other registered services in aggregation
  85. * )
  86. * )
  87. * )
  88. *
  89. * @param string $service service key (twitter, facebook, etc.)
  90. * @param string $id object id
  91. * @param string $type type of response (reply, retweet, url)
  92. * @param bool $ignored comment ignored?
  93. * @param array $data extra data for output
  94. * @return Social_Aggregation_Log
  95. */
  96. public function add($service, $id, $type, $ignored = false, array $data = null) {
  97. if (!isset($this->_log[$this->_timestamp])) {
  98. $this->_log[$this->_timestamp] = (object) array(
  99. 'manual' => false,
  100. 'items' => array(),
  101. );
  102. }
  103. if (!isset($this->_log[$this->_timestamp]->items[$service])) {
  104. $this->_log[$this->_timestamp]->items[$service] = array();
  105. }
  106. foreach ($this->_log[$this->_timestamp]->items[$service] as $item) {
  107. if ($item->id === $id) {
  108. // Bail! Item already exists.
  109. return $this;
  110. }
  111. }
  112. $this->_log[$this->_timestamp]->items[$service][] = (object) array(
  113. 'id' => $id,
  114. 'type' => $type,
  115. 'ignored' => $ignored,
  116. 'data' => $data,
  117. );
  118. return $this;
  119. }
  120. /**
  121. * Force an item to be flagged as ignored.
  122. *
  123. * @param int $id
  124. * @return $this
  125. */
  126. public function ignore($id) {
  127. foreach ($this->_log[$this->_timestamp]->items as $service => $items) {
  128. foreach ($items as $key => $item) {
  129. if ($item->id == $id) {
  130. $item->ignored = true;
  131. $this->_log[$this->_timestamp]->items[$service][$key] = $item;
  132. return $this;
  133. }
  134. }
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Saves the log.
  140. *
  141. * @param bool $manual
  142. * @return void
  143. */
  144. public function save($manual = false) {
  145. $this->_log[$this->_timestamp]->manual = $manual;
  146. update_post_meta($this->_post_id, '_social_aggregation_log', $this->_log);
  147. }
  148. /**
  149. * Returns the current log.
  150. *
  151. * @return array
  152. */
  153. public function current() {
  154. return isset($this->_log[$this->_timestamp]) ? $this->_log[$this->_timestamp] : array();
  155. }
  156. /**
  157. * Renders the log to HTML.
  158. *
  159. * @return Social_View
  160. */
  161. public function render() {
  162. return Social_View::factory('wp-admin/post/meta/log/output', array(
  163. 'log' => $this->_log,
  164. 'services' => Social::instance()->services(),
  165. ))->render();
  166. }
  167. } // End Social_Aggregation_Log