PageRenderTime 36ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/front-end-pm/admin/class-fep-update.php

https://gitlab.com/pankajmohale/chef2go
PHP | 325 lines | 248 code | 72 blank | 5 comment | 30 complexity | afab012b89b48a0011ab972b07ee4b3d MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly.
  4. }
  5. class Fep_Update
  6. {
  7. private static $instance;
  8. public static function init()
  9. {
  10. if(!self::$instance instanceof self) {
  11. self::$instance = new self;
  12. }
  13. return self::$instance;
  14. }
  15. function actions_filters()
  16. {
  17. add_filter('fep_admin_settings_tabs', array($this, 'sections'));
  18. add_filter('fep_settings_fields', array($this, 'fields'));
  19. add_action('fep_admin_settings_field_output_update', array($this, 'field_output'));
  20. add_action('wp_ajax_fep_update_ajax', array($this, 'ajax'));
  21. }
  22. function sections( $tabs)
  23. {
  24. $tabs['update'] = array(
  25. 'priority' => 45,
  26. 'tab_title' => __('Update', 'front-end-pm'),
  27. );
  28. return $tabs;
  29. }
  30. function fields( $fields)
  31. {
  32. $fields['update'] = array(
  33. 'type' => 'update',
  34. 'section' => 'update',
  35. 'label' => __( 'Update', 'front-end-pm' )
  36. );
  37. return $fields;
  38. }
  39. function field_output( $field ){
  40. global $wpdb;
  41. if( $wpdb->get_var("SHOW TABLES LIKE '". FEP_MESSAGES_TABLE . "'") != FEP_MESSAGES_TABLE ) {
  42. _e('You are up to date, No need to update.', 'front-end-pm' );
  43. return;
  44. }
  45. if( ! $wpdb->get_var("SELECT COUNT(*) FROM " . FEP_MESSAGES_TABLE . " WHERE id IS NOT NULL LIMIT 1") ) {
  46. $wpdb->query( "DROP TABLE IF EXISTS ".FEP_MESSAGES_TABLE );
  47. $wpdb->query( "DROP TABLE IF EXISTS ".FEP_META_TABLE );
  48. _e('You are up to date, No need to update.', 'front-end-pm' );
  49. return;
  50. }
  51. wp_enqueue_script( 'fep_update_script'); ?>
  52. <form id="fep_update_form" action="" method="post">
  53. <div class="form">
  54. <div id="fep-prev-version-div">
  55. <label for="fep-prev-version"><?php _e('Previous version', 'front-end-pm' ); ?></label>
  56. <select name="fep-prev-version" id="fep-prev-version">
  57. <option value="31"><?php _e('3.1', 'front-end-pm' ); ?></option>
  58. <option value="32"><?php _e('3.2', 'front-end-pm' ); ?></option>
  59. <option value="33"><?php _e('3.3', 'front-end-pm' ); ?></option>
  60. </select>
  61. </div>
  62. <div id="fep-submit_button">
  63. <p class="submit"><button id="fep-update-button" class="button button-secondary"><?php _e('Update', 'front-end-pm' ); ?></button>
  64. <img src="<?php echo FEP_PLUGIN_URL; ?>assets/images/loading.gif" class="fep-ajax-img" style="display:none;"/></p>
  65. </div>
  66. <div id="fep-ajax-response"></div>
  67. <div>
  68. </div>
  69. </div>
  70. </form><?php
  71. }
  72. function ajax(){
  73. global $wpdb;
  74. check_ajax_referer( 'fep_settings-options' );
  75. $prev_version = ! empty( $_POST['fep-prev-version'] ) ? absint($_POST['fep-prev-version']) : '';
  76. $messages = $this->get_messages();
  77. $total = count($messages);
  78. $announcements = $this->get_announcements();
  79. $output['success'] = 0;
  80. if( ! $total && ! $announcements ) {
  81. //Delete Table
  82. $wpdb->query( "DROP TABLE IF EXISTS ".FEP_MESSAGES_TABLE );
  83. $wpdb->query( "DROP TABLE IF EXISTS ".FEP_META_TABLE );
  84. $output['message'] = __('You are up to date, No need to update.', 'front-end-pm' );
  85. echo wp_json_encode( $output );
  86. wp_die();
  87. }
  88. $count = 0;
  89. if( $messages ) {
  90. foreach ( $messages as $message ) {
  91. $arr = array(
  92. 'message_title' => $message->message_title,
  93. 'message_content' => $message->message_contents,
  94. 'fep_parent_id' => 0,
  95. 'message_to_id' => $message->to_user
  96. );
  97. $override = array(
  98. 'post_author' => $message->from_user,
  99. 'post_date' => $message->send_date
  100. );
  101. if( $message_id = fep_send_message( $arr, $override ) ) {
  102. if( 1 == $message->status ) {
  103. fep_make_read( true, $message_id, $message->to_user );
  104. }
  105. if( 1 == $message->from_del ) {
  106. add_post_meta( $message_id, '_fep_delete_by_'. $message->from_user, time(), true ); //No time from previous version
  107. } elseif( 1 == $message->to_del ) {
  108. add_post_meta( $message_id, '_fep_delete_by_'. $message->to_user, time(), true );
  109. }
  110. $this->insert_attachment( $message_id, $message->id, $message->from_user );
  111. $this->insert_replies( $message_id, $message->id );
  112. $this->delete_message( $message->id );
  113. ++$count;
  114. }
  115. }
  116. }
  117. if( $announcements ) {
  118. foreach( $announcements as $announcement ) {
  119. $arr = array(
  120. 'post_title' => $announcement->message_title,
  121. 'post_content' => $announcement->message_contents,
  122. 'post_author' => $announcement->from_user,
  123. 'post_date' => $announcement->send_date,
  124. 'post_type' => 'fep_announcement',
  125. 'post_status' => 'publish'
  126. );
  127. if( ! $ann_id = wp_insert_post($arr) )
  128. continue;
  129. $this->insert_attachment( $ann_id, $announcement->id, $announcement->from_user );
  130. foreach( array_keys( get_editable_roles() ) as $role ) {
  131. add_post_meta( $ann_id, '_participant_roles', $role );
  132. }
  133. $seen = $this->get_announcement_meta( $announcement->id );
  134. $seen = maybe_unserialize( $seen );
  135. if( $seen && is_array($seen) ) {
  136. add_post_meta( $ann_id, '_fep_read_by', $seen, true );
  137. }
  138. $deleted = $this->get_announcement_meta( $announcement->id, 'announcement_deleted_user_id' );
  139. $deleted = maybe_unserialize( $deleted );
  140. if( $deleted && is_array($deleted) ) {
  141. foreach( $deleted as $del ) {
  142. add_post_meta( $ann_id, '_fep_delete_by_'. $del, time(), true );
  143. }
  144. }
  145. $this->delete_message( $announcement->id );
  146. }
  147. }
  148. delete_metadata( 'user', 0, '_fep_user_announcement_count', '', true );
  149. if( $count == $total ) {
  150. //Delete Table
  151. $wpdb->query( "DROP TABLE IF EXISTS ".FEP_MESSAGES_TABLE );
  152. $wpdb->query( "DROP TABLE IF EXISTS ".FEP_META_TABLE );
  153. $output['success'] = 1;
  154. $output['message'] = sprintf(__('Successfully updated.', 'front-end-pm' ), $count, $total);
  155. } else {
  156. $output['message'] = __('Please refresh this page and update again.', 'front-end-pm' );
  157. }
  158. echo wp_json_encode( $output );
  159. wp_die();
  160. }
  161. function insert_attachment( $message_id, $message_prev_id, $author )
  162. {
  163. if( ! $attachments = $this->get_attachments( $message_prev_id ) )
  164. return;
  165. foreach( $attachments as $attachment ) {
  166. $unserialized_file = maybe_unserialize( $attachment->field_value );
  167. if ( $unserialized_file['type'] && $unserialized_file['url'] && $unserialized_file['file'] ) {
  168. // Prepare an array of post data for the attachment.
  169. $att = array(
  170. 'guid' => $unserialized_file['url'],
  171. 'post_mime_type' => $unserialized_file['type'],
  172. 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $unserialized_file['url'] ) ),
  173. 'post_content' => '',
  174. 'post_author' => $author,
  175. 'post_status' => 'inherit'
  176. );
  177. // Insert the attachment.
  178. wp_insert_attachment( $att, $unserialized_file['file'], $message_id );
  179. }
  180. }
  181. }
  182. function insert_replies( $message_id, $message_prev_id )
  183. {
  184. if( ! $replies = $this->get_replies( $message_prev_id ) )
  185. return;
  186. foreach( $replies as $reply ) {
  187. $arr = array(
  188. 'message_title' => $reply->message_title,
  189. 'message_content' => $reply->message_contents,
  190. 'fep_parent_id' => $message_id
  191. );
  192. $override = array(
  193. 'post_author' => $reply->from_user,
  194. 'post_date' => $reply->send_date
  195. );
  196. if( $reply_id = fep_send_message( $arr, $override ) ) {
  197. $this->insert_attachment( $reply_id, $reply->id, $reply->from_user );
  198. $this->delete_message( $reply->id );
  199. }
  200. }
  201. }
  202. function delete_message( $message_id )
  203. { global $wpdb;
  204. $wpdb->query($wpdb->prepare("DELETE FROM ".FEP_MESSAGES_TABLE." WHERE id = %d", $message_id ));
  205. $wpdb->query($wpdb->prepare("DELETE FROM ".FEP_META_TABLE." WHERE message_id = %d", $message_id ));
  206. }
  207. function get_messages()
  208. { global $wpdb;
  209. return $wpdb->get_results($wpdb->prepare("SELECT * FROM ".FEP_MESSAGES_TABLE." WHERE parent_id = %d AND (status = 0 OR status = 1) ORDER BY last_date DESC", 0 ));
  210. }
  211. function get_replies( $parent )
  212. {
  213. global $wpdb;
  214. return $wpdb->get_results($wpdb->prepare("SELECT * FROM ".FEP_MESSAGES_TABLE." WHERE parent_id = %d AND (status = 0 OR status = 1) ORDER BY last_date DESC", $parent ));
  215. }
  216. function get_attachments( $message_id )
  217. {
  218. global $wpdb;
  219. return $wpdb->get_results($wpdb->prepare("SELECT * FROM ".FEP_META_TABLE." WHERE message_id = %d AND field_name = %s", $message_id, 'attachment' ));
  220. }
  221. function get_announcements()
  222. { global $wpdb;
  223. return $wpdb->get_results($wpdb->prepare("SELECT * FROM ".FEP_MESSAGES_TABLE." WHERE status = %d", 2 ));
  224. }
  225. function get_announcement_meta( $id, $meta = 'announcement_seen_user_id' )
  226. { global $wpdb;
  227. return $wpdb->get_var($wpdb->prepare("SELECT field_value FROM ".FEP_META_TABLE." WHERE message_id = %d AND field_name = %s LIMIT 1", $id, $meta ));
  228. }
  229. } //END CLASS
  230. add_action('admin_init', array(Fep_Update::init(), 'actions_filters'));
  231. function fep_update_script() {
  232. wp_register_script( 'fep_update_script', FEP_PLUGIN_URL . 'assets/js/fep_update_script.js', array( 'jquery' ), '3.1', true );
  233. }
  234. add_action( 'admin_enqueue_scripts', 'fep_update_script' );
  235. function fep_insert_dummy_message(){
  236. global $wpdb;
  237. for( $i = 0; $i < 3; $i++ ) {
  238. $from = rand(1, 5);
  239. $wpdb->insert( FEP_MESSAGES_TABLE, array(
  240. 'from_user' => $from,
  241. 'to_user' => rand(1, 5),
  242. 'message_title' => 'this is title',
  243. 'message_contents' => 'this is message',
  244. 'parent_id' => 0,
  245. 'last_sender' => $from,
  246. 'send_date' => current_time('mysql'),
  247. 'last_date' => current_time('mysql'),
  248. 'status' => rand(0, 2)
  249. ),
  250. array( '%d', '%d', '%s', '%s', '%d', '%d', '%s', '%s', '%d' ));
  251. }
  252. }
  253. //add_action('wp_loaded', 'fep_insert_dummy_message' );