/wp-content/plugins/blog2social/includes/B2S/Post/Tools.php

https://github.com/livinglab/openlab · PHP · 161 lines · 146 code · 12 blank · 3 comment · 49 complexity · 90d02a202e4bd4152e768252f2a122cd MD5 · raw file

  1. <?php
  2. class B2S_Post_Tools {
  3. public static function updateUserSchedTimePost($post_id, $date, $time, $timezone) {
  4. global $wpdb;
  5. $sql = $wpdb->prepare("SELECT id FROM {$wpdb->prefix}b2s_posts WHERE id =%d AND blog_user_id = %d AND publish_date = %s", (int) $post_id, (int) get_current_user_id(), "0000-00-00 00:00:00");
  6. $id = $wpdb->get_col($sql);
  7. if (isset($id[0]) && $id[0] == $post_id) {
  8. $insert_time = strtotime($date . ' ' . $time);
  9. if ($insert_time < time()) {
  10. $insert_time = time();
  11. }
  12. $insert_datetime_utc = B2S_Util::getUTCForDate(date('Y-m-d H:i:s', $insert_time), $timezone * (-1));
  13. $wpdb->update($wpdb->prefix.'b2s_posts', array('hook_action' => 2, 'sched_date' => date('Y-m-d H:i:s', $insert_time), 'sched_date_utc' => $insert_datetime_utc), array('id' => $post_id));
  14. return array('result' => true, 'postId' => $post_id, 'time' => B2S_Util::getCustomDateFormat(date('Y-m-d H:i:s', $insert_time), substr(B2S_LANGUAGE, 0, 2)));
  15. }
  16. return array('result' => false);
  17. }
  18. public static function deleteUserSchedPost($postIds = array()) {
  19. global $wpdb;
  20. $resultPostIds = array();
  21. $blogPostId = 0;
  22. $tosCrossPosting = unserialize(B2S_PLUGIN_NETWORK_CROSSPOSTING_LIMIT);
  23. foreach ($postIds as $v) {
  24. $sql = $wpdb->prepare("SELECT b.id,b.post_id,b.post_for_relay,b.post_for_approve,b.sched_details_id,d.network_id,d.network_type FROM {$wpdb->prefix}b2s_posts b LEFT JOIN {$wpdb->prefix}b2s_posts_network_details d ON (d.id = b.network_details_id) WHERE b.id =%d AND b.publish_date = %s", (int) $v, "0000-00-00 00:00:00");
  25. $row = $wpdb->get_row($sql);
  26. if (isset($row->id) && (int) $row->id == $v) {
  27. if ((int) $row->post_for_approve == 1) {
  28. $wpdb->update($wpdb->prefix.'b2s_posts', array('hide' => 1), array('id' => $v));
  29. } else {
  30. //TOS Crossposting delete entry
  31. if ($row->network_id != null && $row->network_type != null && (int) $row->sched_details_id > 0) {
  32. if (isset($tosCrossPosting[$row->network_id][$row->network_type])) {
  33. //get network_tos_group_id form sched_data
  34. $sql = $wpdb->prepare("SELECT sched_data FROM {$wpdb->prefix}b2s_posts_sched_details WHERE id =%d", (int) $row->sched_details_id);
  35. $schedData = $wpdb->get_col($sql);
  36. if (isset($schedData[0]) && !empty($schedData[0])) {
  37. $schedData = unserialize($schedData[0]);
  38. if ($schedData !== false && isset($schedData['network_tos_group_id']) && !empty($schedData['network_tos_group_id'])) {
  39. $options = new B2S_Options(0, 'B2S_PLUGIN_TOS_XING_GROUP_CROSSPOSTING');
  40. $options->deleteValueByKey($row->post_id, $schedData['network_tos_group_id']);
  41. }
  42. }
  43. }
  44. }
  45. $wpdb->update($wpdb->prefix.'b2s_posts', array('hook_action' => 3, 'hide' => 1), array('id' => $v));
  46. }
  47. $resultPostIds[] = $v;
  48. $blogPostId = $row->post_id;
  49. //is post for relay
  50. if ((int) $row->post_for_relay == 1) {
  51. $res = self::getAllRelayByPrimaryPostId($row->id);
  52. if (is_array($res) && !empty($res)) {
  53. foreach ($res as $item) {
  54. if (isset($item->id) && (int) $item->id > 0) {
  55. $wpdb->update($wpdb->prefix.'b2s_posts', array('hook_action' => 3, 'hide' => 1), array('id' => $item->id));
  56. $resultPostIds[] = $item->id;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. if (!empty($resultPostIds) && is_array($resultPostIds)) {
  64. B2S_Heartbeat::getInstance()->deleteSchedPost();
  65. $resultPostIds = array_unique($resultPostIds);
  66. return array('result' => true, 'postId' => $resultPostIds, 'postCount' => count($resultPostIds), 'blogPostId' => $blogPostId);
  67. }
  68. return array('result' => false);
  69. }
  70. public static function getAllRelayByPrimaryPostId($primary_post_id = 0) {
  71. global $wpdb;
  72. $sqlData = $wpdb->prepare("SELECT `id` FROM `{$wpdb->prefix}b2s_posts` WHERE `hide` = 0 AND `sched_type` = 4 AND `{$wpdb->prefix}b2s_posts`.`publish_date` = '0000-00-00 00:00:00' AND `relay_primary_post_id` = %d ", $primary_post_id);
  73. return $wpdb->get_results($sqlData);
  74. }
  75. public static function deleteUserPublishPost($postIds = array()) {
  76. global $wpdb;
  77. $resultPostIds = array();
  78. $blogPostId = 0;
  79. $count = 0;
  80. foreach ($postIds as $v) {
  81. $sql = $wpdb->prepare("SELECT id,v2_id,post_id FROM {$wpdb->prefix}b2s_posts WHERE id =%d", (int) $v);
  82. $row = $wpdb->get_row($sql);
  83. if (isset($row->id) && (int) $row->id == $v) {
  84. $hook_action = (isset($row->v2_id) && (int) $row->v2_id > 0) ? 0 : 4; //oldItems
  85. $wpdb->update($wpdb->prefix.'b2s_posts', array('hook_action' => $hook_action, 'hide' => 1), array('id' => $v));
  86. $resultPostIds[] = $v;
  87. $blogPostId = $row->post_id;
  88. $count++;
  89. }
  90. }
  91. if (!empty($resultPostIds) && is_array($resultPostIds)) {
  92. return array('result' => true, 'postId' => $resultPostIds, 'postCount' => $count, 'blogPostId' => $blogPostId);
  93. }
  94. return array('result' => false);
  95. }
  96. public static function deleteUserApprovePost($postIds = array()) {
  97. global $wpdb;
  98. $resultPostIds = array();
  99. $blogPostId = 0;
  100. $count = 0;
  101. foreach ($postIds as $v) {
  102. $sql = $wpdb->prepare("SELECT id,v2_id,post_id FROM {$wpdb->prefix}b2s_posts WHERE id =%d", (int) $v);
  103. $row = $wpdb->get_row($sql);
  104. if (isset($row->id) && (int) $row->id == $v) {
  105. $hook_action = (isset($row->v2_id) && (int) $row->v2_id > 0) ? 0 : 4; //oldItems
  106. $wpdb->update($wpdb->prefix.'b2s_posts', array('hide' => 1), array('id' => $v));
  107. $resultPostIds[] = $v;
  108. $blogPostId = $row->post_id;
  109. $count++;
  110. }
  111. }
  112. if (!empty($resultPostIds) && is_array($resultPostIds)) {
  113. return array('result' => true, 'postId' => $resultPostIds, 'postCount' => $count, 'blogPostId' => $blogPostId);
  114. }
  115. return array('result' => false);
  116. }
  117. public static function countNewNotifications($userId = 0) {
  118. if($userId > 0) {
  119. require_once(B2S_PLUGIN_DIR . 'includes/Options.php');
  120. $options = new B2S_Options($userId);
  121. $lastDate = $options->_getOption('lastNotificationUpdate');
  122. if($lastDate == false || empty($lastDate)) {
  123. $lastDate = '2021-01-04 12:00:00';
  124. }
  125. $optionUserTimeZone = $options->_getOption('user_time_zone');
  126. $userTimeZone = ($optionUserTimeZone !== false) ? $optionUserTimeZone : get_option('timezone_string');
  127. $userTimeZoneOffset = (empty($userTimeZone)) ? get_option('gmt_offset') : B2S_Util::getOffsetToUtcByTimeZone($userTimeZone);
  128. $lastDate = date('Y-m-d H:i:s', strtotime(B2S_Util::getUTCForDate($lastDate, $userTimeZoneOffset)));
  129. global $wpdb;
  130. $sql = $wpdb->prepare("SELECT count(id) AS noticeCount FROM {$wpdb->prefix}b2s_posts WHERE publish_date > '%s' AND publish_error_code != '' AND blog_user_id = %d", $lastDate, (int) $userId);
  131. $row = $wpdb->get_row($sql);
  132. if (isset($row->noticeCount)) {
  133. return (int) $row->noticeCount;
  134. }
  135. }
  136. return 0;
  137. }
  138. public static function countReadyForApprove($userId = 0) {
  139. if($userId > 0) {
  140. global $wpdb;
  141. $sql = $wpdb->prepare("SELECT count(id) AS approveCount FROM {$wpdb->prefix}b2s_posts WHERE hide = %d AND publish_date = '%s' AND sched_date_utc <= '%s' AND post_for_approve = %d AND blog_user_id = %d", 0, '0000-00-00 00:00:00', gmdate('Y-m-d H:i:s'), 1, (int) $userId);
  142. $row = $wpdb->get_row($sql);
  143. if (isset($row->approveCount)) {
  144. return (int) $row->approveCount;
  145. }
  146. }
  147. return 0;
  148. }
  149. }