/wp-content/plugins/buddypress-custom-posts/model.php

https://github.com/voidit/nycga2 · PHP · 374 lines · 186 code · 66 blank · 122 comment · 24 complexity · a50edd6af40b4423de303b991ce12058 MD5 · raw file

  1. <?php
  2. /**
  3. * model.php
  4. *
  5. * All database interactions are controlled by this file.
  6. *
  7. * @author kunalbhalla
  8. *
  9. * @since 0.1
  10. */
  11. /**
  12. * Any and all data related functions.
  13. *
  14. * @since 0.1
  15. */
  16. class bpcp_M {
  17. /**
  18. * Eases typing.
  19. *
  20. * @var string $id Post type identifier
  21. *
  22. * @since 0.1
  23. */
  24. var $id;
  25. /**
  26. * A copy of settings stored in the main controller.
  27. * Avoiding the global version to avoid 'timing' issues.
  28. *
  29. * @var Mixed Array $settings
  30. *
  31. * @since 0.1
  32. */
  33. var $settings;
  34. /**
  35. * Constructor, initializes settings and id.
  36. *
  37. * @see bpcp::bpcp
  38. *
  39. * @since 0.1
  40. */
  41. function bpcp_M( $settings ) {
  42. $this->settings = $settings;
  43. $this->id = $settings['id'];
  44. }
  45. /**
  46. * Create a new forum for a given post id.
  47. *
  48. * @param int $post_id Current post id
  49. * @param string $post_name The name of the current post
  50. * @param string $post_desc The description of the current post
  51. *
  52. * @since 0.1
  53. */
  54. function new_forum( $my_post ) {
  55. global $bp, $post;
  56. if ( !$my_post ) {
  57. $post_id = $post->ID;
  58. $post_name = $post->post_name;
  59. $post_desc = $post->content;
  60. } else {
  61. $post_id = $my_post->ID;
  62. $post_name = $my_post->post_name;
  63. $post_desc = $my_post->content;
  64. }
  65. do_action( 'bpcp_' . $this->id . '_new_forum' );
  66. do_action( 'bpcp_new_forum', $this->id );
  67. if ( bp_forums_is_installed_correctly() ) {
  68. $forum_id = bp_forums_new_forum( array( 'forum_name' => $post_name, 'forum_desc' => $post_desc ) );
  69. add_post_meta( $post_id, '_bpcp_forum_id', $forum_id );
  70. }
  71. }
  72. /**
  73. * Add a new forum post.
  74. *
  75. * @param string $post_text Content of post
  76. * @param int $topic_id Topic of the post
  77. * @param int $page Topic page
  78. *
  79. * @since 0.1
  80. */
  81. function new_forum_post( $post_text, $topic_id, $page = false ) {
  82. global $bp, $post;
  83. $type = get_post_type_object( $this->id );
  84. if ( empty( $post_text ) )
  85. return false;
  86. if ( $post_id = bp_forums_insert_post( array( 'post_text' => $post_text, 'topic_id' => $topic_id ) ) ) {
  87. $topic = bp_forums_get_topic_details( $topic_id );
  88. $activity_action = sprintf( __( '%s posted on the forum topic %s in the %s %s:' ), bp_core_get_userlink( $bp->loggedin_user->id ), '<a href="' . get_permalink() . 'forum/topic/' . $topic->topic_slug .'/">' . attribute_escape( $topic->topic_title ) . '</a>', $type->labels->name , '<a href="' . get_permalink() . '">' . attribute_escape( $post->post_title ) . '</a>' );
  89. $activity_content = bp_create_excerpt( $post_text );
  90. $primary_link = get_permalink() . 'forum/topic/' . $topic->topic_slug . '/';
  91. if ( $page )
  92. $primary_link .= "?topic_page=" . $page;
  93. /* Record this in activity streams */
  94. if ( function_exists( 'bp_activity_add' ) )
  95. bp_activity_add( array(
  96. 'action' => $activity_action,
  97. 'content' => $activity_content,
  98. 'component' => $this->id,
  99. 'user_id' => bp_loggedin_user_id(),
  100. 'primary_link' => "{$primary_link}#post-{$post_id}",
  101. 'type' => 'new_forum_post',
  102. 'item_id' => $post->ID,
  103. 'secondary_item_id' => $post_id
  104. ) );
  105. do_action( 'bpcp_' . $this->id . '_new_forum_post' );
  106. do_action( 'bpcp_new_forum_post', $this->id );
  107. return $post_id;
  108. }
  109. return false;
  110. }
  111. /**
  112. * Create a new forum topic
  113. *
  114. * @param string $topic_title The title
  115. * @param string $topic_text
  116. * @param string $topic_tags
  117. * @param int $forum_id
  118. *
  119. * @since 0.1
  120. */
  121. function new_forum_topic( $topic_title, $topic_text, $topic_tags, $forum_id ) {
  122. global $bp, $post;
  123. $type = get_post_type_object( $this->id );
  124. if ( empty( $topic_title ) || empty( $topic_text ) )
  125. return false;
  126. if ( $topic_id = bp_forums_new_topic( array( 'topic_title' => $topic_title, 'topic_text' => $topic_text, 'topic_tags' => $topic_tags, 'forum_id' => $forum_id ) ) ) {
  127. $topic = bp_forums_get_topic_details( $topic_id );
  128. $activity_action = sprintf( __( '%s started the forum topic %s in the %s %s:' ), bp_core_get_userlink( $bp->loggedin_user->id ), '<a href="' . get_permalink() . 'forum/topic/' . $topic->topic_slug .'/">' . attribute_escape( $topic->topic_title ) . '</a>', $type->labels->name, '<a href="' . get_permalink() . '">' . attribute_escape( $post->title ) . '</a>' );
  129. $activity_content = bp_create_excerpt( $topic_text );
  130. /* Record this in activity streams */
  131. if( function_exists( 'bp_activity_add' ) )
  132. bp_activity_add( array(
  133. 'action' => $activity_action,
  134. 'content' => $activity_content,
  135. 'primary_link' => get_permalink() . 'forum/topic/' . $topic->topic_slug . '/',
  136. 'type' => 'new_forum_topic',
  137. 'item_id' => $post->ID,
  138. 'secondary_item_id' => $topic->topic_id,
  139. 'component' => $this->id
  140. ) );
  141. do_action( 'bpcp_' . $this->id . '_new_forum_topic' );
  142. do_action( 'bpcp_new_forum_topic', $this->id );
  143. return $topic;
  144. }
  145. return false;
  146. }
  147. /**
  148. * Delete a forum topic.
  149. *
  150. * @param int $topic_id
  151. *
  152. * @since 0.1
  153. */
  154. function delete_forum_topic( $topic_id ) {
  155. global $bp, $post;
  156. if ( bp_forums_delete_topic( array( 'topic_id' => $topic_id ) ) ) {
  157. /* Delete the activity stream item */
  158. if ( function_exists( 'bp_activity_delete' ) ) {
  159. bp_activity_delete( array( 'item_id' => $post->ID, 'secondary_item_id' => $topic_id, 'component' => $this->id, 'type' => 'new_forum_topic' ) );
  160. }
  161. do_action( 'bpcp_' . $this->id . '_delete_forum_topic' );
  162. do_action( 'bpcp_delete_forum_topic', $this->id );
  163. return true;
  164. }
  165. return false;
  166. }
  167. /**
  168. * Delete a forum post
  169. *
  170. * @param int $post_id
  171. * @param int $topic_id
  172. *
  173. * @since 0.1
  174. */
  175. function delete_forum_post( $post_id, $topic_id ) {
  176. global $bp, $post;
  177. if ( bp_forums_delete_post( array( 'post_id' => $post_id ) ) ) {
  178. /* Delete the activity stream item */
  179. if ( function_exists( 'bp_activity_delete' ) ) {
  180. bp_activity_delete( array( 'item_id' => $post->ID, 'secondary_item_id' => $post_id, 'component' => $this->id, 'type' => 'new_forum_post' ) );
  181. }
  182. do_action( 'bpcp_' . $this->id . '_delete_forum_post' );
  183. do_action( 'bpcp_delete_forum_post', $this->id );
  184. return true;
  185. }
  186. return false;
  187. }
  188. /**
  189. * Get count of topics.
  190. *
  191. * @param string $type
  192. *
  193. * @since 0.1
  194. */
  195. function total_public_forum_topic_count( $type = 'newest' ) {
  196. global $bbdb, $wpdb, $bp;
  197. return $wpdb->get_var( "SELECT COUNT(t.topic_id) FROM {$bbdb->topics} AS t, {$wpdb->posts} AS p WHERE p.id = t.forum_id AND p.post_status = 'public' AND t.topic_status = '0' AND t.topic_sticky != '2' " );
  198. }
  199. /**
  200. * Update a given forum topic.
  201. *
  202. * @param $topic_id
  203. * @param $topic_title
  204. * @param $topic_text
  205. *
  206. * @since 0.1
  207. */
  208. function update_forum_topic( $topic_id, $topic_title, $topic_text ) {
  209. global $bp, $post;
  210. $type = get_post_type_object( $this->id );
  211. if ( $topic = bp_forums_update_topic( array( 'topic_title' => $topic_title, 'topic_text' => $topic_text, 'topic_id' => $topic_id ) ) ) {
  212. /* Update the activity stream item */
  213. if ( function_exists( 'bp_activity_delete_by_item_id' ) )
  214. bp_activity_delete_by_item_id( array( 'item_id' => $post->ID, 'secondary_item_id' => $topic_id, 'component' => $this->id, 'type' => 'new_forum_topic' ) );
  215. $activity_action = sprintf( __( '%s started the forum topic %s in the %s %s:', 'buddypress'), bp_core_get_userlink( $topic->topic_poster ), '<a href="' . get_permalink() . 'forum/topic/' . $topic->topic_slug .'/">' . attribute_escape( $topic->topic_title ) . '</a>', '<a href="' . get_permalink() . '">' . attribute_escape( $post->post_title ) . '</a>' );
  216. $activity_content = bp_create_excerpt( $topic_text );
  217. /* Record this in activity streams */
  218. if( function_exists( 'bp_activity_add' ) )
  219. bp_activity_add( array(
  220. 'action' => $activity_action,
  221. 'content' => $activity_content,
  222. 'primary_link' => get_permalink() . 'forum/topic/' . $topic->topic_slug . '/',
  223. 'type' => 'new_forum_topic',
  224. 'item_id' => (int)$post->ID,
  225. 'user_id' => (int)$topic->topic_poster,
  226. 'secondary_item_id' => $topic->topic_id,
  227. 'recorded_time' => $topic->topic_time,
  228. 'component' => $this->id
  229. ) );
  230. do_action( 'bpcp_' . $this->id . '_update_forum_topic' );
  231. do_action( 'bpcp_update_forum_topic', $this->id );
  232. return $topic;
  233. }
  234. return false;
  235. }
  236. /**
  237. * Update a given forum post
  238. *
  239. * @param int $post_id
  240. * @param string $post_text
  241. * @param int $topic_id
  242. * @param int $page
  243. *
  244. * @since 0.1
  245. */
  246. function update_forum_post( $post_id, $post_text, $topic_id, $page = false ) {
  247. global $bp, $post;
  248. $type = get_post_type_object( $this->id );
  249. $bbpost = bp_forums_get_post( $post_id );
  250. if ( $post_id = bp_forums_insert_post( array( 'post_id' => $post_id, 'post_text' => $post_text, 'post_time' => $bbpost->post_time, 'topic_id' => $topic_id, 'poster_id' => $bbpost->poster_id ) ) ) {
  251. $topic = bp_forums_get_topic_details( $topic_id );
  252. $activity_action = sprintf( __( '%s posted on the forum topic %s in the %s %s:' ), bp_core_get_userlink( $bbpost->poster_id ), '<a href="' . get_permalink() . 'forum/topic/' . $topic->topic_slug .'">' . attribute_escape( $topic->topic_title ) . '</a>', '<a href="' . get_permalink() . '">' . attribute_escape( $post->title ) . '</a>' );
  253. $activity_content = bp_create_excerpt( $post_text );
  254. $primary_link = get_permalink() . 'forum/topic/' . $topic->topic_slug . '/';
  255. if ( $page )
  256. $primary_link .= "?topic_page=" . $page;
  257. /* Fetch an existing entry and update if one exists. */
  258. if ( function_exists( 'bp_activity_get_activity_id' ) )
  259. $id = bp_activity_get_activity_id( array( 'user_id' => $bbpost->poster_id, 'component' => $this->id, 'type' => 'new_forum_post', 'item_id' => $post->ID, 'secondary_item_id' => $post_id ) );
  260. /* Update the entry in activity streams */
  261. bp_activity_add( array(
  262. 'id' => $id,
  263. 'action' => $activity_action,
  264. 'content' => $activity_content,
  265. 'primary_link' => $primary_link . "#post-" . $post_id,
  266. 'type' => 'new_forum_post',
  267. 'item_id' => (int)$post->ID,
  268. 'user_id' => (int)$bbpost->poster_id,
  269. 'secondary_item_id' => $post_id,
  270. 'recorded_time' => $bbpost->post_time,
  271. 'component' => $this->id
  272. ) );
  273. do_action( 'bpcp_' . $this->id . '_update_forum_post' );
  274. do_action( 'bpcp_update_forum_post', $this->id );
  275. return $post_id;
  276. }
  277. return false;
  278. }
  279. /**
  280. * Save the post's data based on $_POST contents.
  281. *
  282. * Heavily simplified version of the labyrinthine
  283. * wp-admin/edit, post, post-new files.
  284. *
  285. * @since 0.1
  286. */
  287. function save_post_data() {
  288. global $post;
  289. //The post administration API.
  290. $admin_path = ABSPATH . '/wp-admin';
  291. require_once( $admin_path . '/includes/post.php' );
  292. $_POST[ 'post_content' ] = $_POST[ 'post-content' ]; //Hackiness.
  293. if ( $posted = ( wp_insert_post( Array(
  294. 'post_status' => 'publish',
  295. 'post_type' => $this->id,
  296. 'post_author' => $_POST[ 'user_ID' ],
  297. 'post_content' => $_POST[ 'post-content' ],
  298. 'post_title' => $_POST[ 'post_title' ],
  299. 'ID' => $_POST[ 'post_ID' ]
  300. ) ) ) != 0 ) {
  301. bp_core_add_message( __( 'Updated succesfully.' ) );
  302. $postid = $_POST[ 'post_ID' ];
  303. $post = get_post( $postid );
  304. }
  305. else
  306. bp_core_add_message( __( 'Could not update.' ) );
  307. }
  308. }