PluginProbe
Front End PM / trunk
Front End PM vtrunk
trunk 1.1 1.2 1.3 10.1.1 10.1.2 10.1.3 10.1.4 10.1.5 10.1.6 10.1.7 10.2.1 11.1.1 11.2.1 11.2.2 11.2.3 11.3.1 11.3.3 11.3.4 11.3.5 11.3.6 11.3.7 11.3.8 11.3.9 11.4.1 All 58 releases
front-end-pm / admin / class-fep-update.php

class-fep-update.php in Front End PM trunk, at admin/class-fep-update.php

667 lines 23.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly.
4 }
5
6 class Fep_Update {
7 private static $instance;
8
9 public static function init() {
10 if ( ! self::$instance instanceof self ) {
11 self::$instance = new self;
12 }
13 return self::$instance;
14 }
15
16 function actions_filters() {
17 if( ! is_admin() ){
18 return;
19 }
20 add_action( 'admin_enqueue_scripts', array( $this, 'fep_update_script' ) );
21 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
22 add_action( 'admin_notices', array( $this, 'notice_delete_all' ) );
23 add_action( 'admin_post_fep_delete_all', array( $this, 'delete_all' ) );
24
25 add_action( 'admin_init', array( $this, 'install' ), 5 );
26 add_action( 'admin_init', array( $this, 'message_view_changed' ), 25 );
27 add_action( 'admin_init', array( $this, 'auto_update' ), 30 );
28
29 add_action( 'wp_ajax_fep_update_ajax', array( $this, 'ajax' ) );
30 add_action( 'fep_plugin_update', array( $this, 'update' ) );
31 }
32
33 function fep_update_script() {
34 wp_register_script( 'fep_update_script', FEP_PLUGIN_URL . 'assets/js/fep_update_script.js', array( 'jquery' ), FEP_PLUGIN_VERSION, true );
35 }
36
37 function admin_menu(){
38 add_submenu_page( 'fep-non-exist-menu', 'Front End PM - ' . __( 'Update', 'front-end-pm' ), __( 'Update', 'front-end-pm' ), apply_filters( 'fep_admin_cap', 'manage_options' ), 'fep_update', array( $this, 'update_page' ) );
39 }
40
41 function update_page() {
42 wp_enqueue_script( 'fep_update_script' );
43 ?>
44 <div class="wrap">
45 <h2><?php printf(__( '%s update', 'front-end-pm' ), fep_is_pro() ? 'Front End PM PRO' : 'Front End PM' ); ?></h2>
46 <noscript><p><em><?php _e( 'You must enable Javascript in order to proceed!', 'front-end-pm' ) ?></em></p></noscript>
47 <div id="fep-update-warning">
48 <strong><?php _e( 'DO NOT close this window. This may take a while.', 'front-end-pm' ); ?></strong>
49 </div>
50 <div>
51 <img src="<?php echo FEP_PLUGIN_URL; ?>assets/images/loading.gif" class="fep-ajax-img hidden" />
52 </div>
53 <div>
54 <button class="fep-start-update button-primary hide-if-no-js"><?php _e( 'Start Update', 'front-end-pm' ); ?></button>
55 </div>
56 <div id="fep-ajax-response"></div>
57 </div>
58 <?php
59 }
60
61 public function notice_delete_all() {
62 if( ! current_user_can( 'manage_options' ) || ! get_option( '_fep_can_delete_all' ) ){
63 return;
64 }
65 ?>
66 <div class="notice notice-info">
67 <p><?php printf( __( 'You can now safely delete %s legacy messages and announcements.', 'front-end-pm' ), fep_is_pro() ? 'Front End PM PRO' : 'Front End PM' ); ?></p>
68 <p>
69 <a href="<?php echo wp_nonce_url( add_query_arg( 'action', 'fep_delete_all', admin_url( 'admin-post.php' ) ), 'fep_delete_all' ); ?>" class="button button-primary"><?php _e( 'Proceed', 'front-end-pm' ); ?></a>
70 </p>
71 </div>
72 <?php
73 }
74
75 function delete_all(){
76 global $wpdb;
77
78 if( ! current_user_can( 'manage_options' ) || ! get_option( '_fep_can_delete_all' ) ){
79 wp_die( __( 'Invalid request!', 'front-end-pm' ) );
80 }
81 if( ! wp_verify_nonce( $_GET['_wpnonce'], 'fep_delete_all' ) ){
82 wp_die( __( 'Invalid nonce!', 'front-end-pm' ) );
83 }
84
85 $wpdb->query( "DELETE $wpdb->posts, $wpdb->postmeta FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id) WHERE {$wpdb->posts}.post_type IN ('fep_message', 'fep_announcement')" );
86
87 update_option( '_fep_can_delete_all', 0 );
88
89 wp_safe_redirect( admin_url() );
90 exit;
91 }
92
93 function install() {
94 if ( false !== fep_get_option( 'plugin_version', false ) ) {
95 return;
96 }
97 global $wpdb;
98 $roles = array_keys( get_editable_roles() );
99 $id = $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%[front-end-pm%' AND post_status = 'publish' AND post_type = 'page' LIMIT 1" );
100 $options = array();
101 $options['userrole_access'] = $roles;
102 $options['userrole_new_message'] = $roles;
103 $options['userrole_reply'] = $roles;
104 $options['plugin_version'] = get_option( 'FEP_admin_options' ) ? '4.0' : FEP_PLUGIN_VERSION;
105 $options['page_id'] = $id;
106 fep_update_option( $options );
107 //fep_add_caps_to_roles();
108 $this->create_htaccess();
109 }
110
111 function update( $prev_ver ) {
112 global $wpdb;
113 if ( version_compare( $prev_ver, '11.4.1', '<' ) ) {
114 $this->create_htaccess();
115 }
116 if ( version_compare( $prev_ver, '11.4.1', '=' ) || version_compare( $prev_ver, '11.4.2', '=' ) ) {
117 $this->remove_htaccess();
118 }
119 if ( version_compare( $prev_ver, '6.1', '<' ) ) {
120 $options = array();
121 $options['show_directory'] = fep_get_option( 'hide_directory', 0 ) ? 0 : 1;
122 $options['show_notification'] = fep_get_option( 'hide_notification', 0 ) ? 0 : 1;
123 $options['show_branding'] = fep_get_option( 'hide_branding', 0 ) ? 0 : 1;
124 $options['show_autosuggest'] = fep_get_option( 'hide_autosuggest', 0 ) ? 0 : 1;
125 fep_update_option( $options );
126 }
127 if ( version_compare( $prev_ver, '11.1.1', '<' ) ) {
128 $wpdb->query( "DELETE mm FROM $wpdb->fep_messagemeta mm LEFT JOIN $wpdb->fep_messages m ON mm.fep_message_id = m.mgs_id WHERE m.mgs_id IS NULL" );
129 }
130 if ( version_compare( $prev_ver, '11.2.1', '<' ) ) {
131 // Drop the old index. dbDelta() doesn't do the drop.
132 $wpdb->query( "ALTER TABLE $wpdb->fep_messages DROP INDEX mgs_parent" );
133 $wpdb->query( "ALTER TABLE $wpdb->fep_messages DROP INDEX mgs_author" );
134 $wpdb->query( "ALTER TABLE $wpdb->fep_messages DROP INDEX mgs_created" );
135 $wpdb->query( "ALTER TABLE $wpdb->fep_messages DROP INDEX type_status" );
136 $wpdb->query( "ALTER TABLE $wpdb->fep_messages DROP INDEX mgs_last_reply_time" );
137
138 $wpdb->query( "ALTER TABLE " . FEP_PARTICIPANT_TABLE . " DROP INDEX mgs_parent_read" );
139 $wpdb->query( "ALTER TABLE " . FEP_PARTICIPANT_TABLE . " DROP INDEX mgs_deleted" );
140 $wpdb->query( "ALTER TABLE " . FEP_PARTICIPANT_TABLE . " DROP INDEX mgs_archived" );
141
142 $wpdb->query( "ALTER TABLE " . FEP_ATTACHMENT_TABLE . " DROP INDEX att_status" );
143 }
144 }
145
146 function message_view_changed() {
147 if ( get_option( '_fep_message_view_changed' ) ) {
148 add_filter( 'fep_update_enable_version_check', '__return_false' );
149 add_filter( 'fep_require_manual_update', '__return_true' );
150 add_action( 'fep_plugin_manual_update', array( $this, 'individual_to_threaded' ) );
151 }
152 }
153
154 function auto_update() {
155 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
156 return;
157 }
158 if ( isset( $_GET['page'] ) && 'fep_update' == $_GET['page'] ) {
159 return;
160 }
161 $prev_ver = fep_get_option( 'plugin_version', '3.3' );
162 if ( version_compare( $prev_ver, FEP_PLUGIN_VERSION, '=' ) && apply_filters( 'fep_update_enable_version_check', true ) ) {
163 return;
164 }
165 $require = false;
166
167 if ( version_compare( $prev_ver, '10.1.1', '<' ) ) {
168 $require = true;
169 }
170 if ( apply_filters( 'fep_require_manual_update', $require, $prev_ver ) ) {
171 add_action( 'admin_notices', array( $this, 'notice_update' ) );
172 } else {
173 do_action( 'fep_plugin_auto_update', $prev_ver );
174 do_action( 'fep_plugin_update', $prev_ver );
175 fep_update_option( 'plugin_version', FEP_PLUGIN_VERSION );
176 }
177 }
178
179 public function notice_update() {
180 if ( ! current_user_can( 'manage_options' ) ) {
181 return;
182 }
183 if ( isset( $_GET['page'] ) && 'fep_update' == $_GET['page'] ) {
184 return;
185 }
186 ?>
187 <div class="notice notice-info">
188 <p><?php printf( __( '%s needs to database update.', 'front-end-pm' ), fep_is_pro() ? 'Front End PM PRO' : 'Front End PM' ); ?></p>
189 <p>
190 <a href="<?php echo add_query_arg( 'page', 'fep_update', admin_url( 'admin.php' ) ); ?>" class="button button-primary"><?php _e( 'Proceed', 'front-end-pm' ); ?></a>
191 </p>
192 </div>
193 <?php
194 }
195
196 function ajax() {
197 if ( ! current_user_can( 'manage_options' ) ) {
198 $response = array(
199 'update' => 'completed',
200 'message' => __( 'You do not have permission to trigger update.', 'front-end-pm' ),
201 );
202 wp_send_json( $response );
203 }
204 $prev_ver = fep_get_option( 'plugin_version', '3.3' );
205 if ( version_compare( $prev_ver, '4.1', '<' ) ) {
206 //$this->update_version_41();
207 $response = array(
208 'update' => 'completed',
209 'message' => __( 'You cannot update from your current version.', 'front-end-pm' ),
210 );
211 wp_send_json( $response );
212 }
213 ignore_user_abort( true );
214 if ( ! fep_is_func_disabled( 'set_time_limit' ) ) {
215 set_time_limit( 3600 );
216 }
217
218 if ( version_compare( $prev_ver, '5.1', '<' ) ) {
219 $this->update_version_51();
220 }
221 if ( version_compare( $prev_ver, '10.1.1', '<' ) ) {
222 $this->update_version_1011();
223 }
224 do_action( 'fep_plugin_manual_update', $prev_ver );
225 do_action( 'fep_plugin_update', $prev_ver );
226 fep_update_option( 'plugin_version', FEP_PLUGIN_VERSION );
227 $response = array(
228 'update' => 'completed',
229 'message' => __( 'Update completed.', 'front-end-pm' ),
230 );
231 wp_send_json( $response );
232 }
233
234 function update_version_51() {
235 $updated = fep_get_option( 'v51', 0, 'fep_updated_versions' );
236 if ( $updated ) {
237 return;
238 }
239 $custom_int = isset( $_POST['custom_int'] ) ? absint( $_POST['custom_int'] ) : 0;
240
241 global $wpdb;
242 $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = REPLACE( meta_key, '_message_key', '_fep_message_key' ) WHERE meta_key = '_message_key'" );
243 $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = REPLACE( meta_key, '_participant_roles', '_fep_participant_roles' ) WHERE meta_key = '_participant_roles'" );
244 $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = REPLACE( meta_key, '_participants', '_fep_participants' ) WHERE meta_key = '_participants'" );
245
246 fep_update_option( 'v51', 1, 'fep_updated_versions' );
247 $response = array(
248 'update' => 'continue',
249 'message' => __( 'Messages meta updated', 'front-end-pm' ),
250 'custom_int'=> 0,
251 'custom_str'=> '',
252 );
253 wp_send_json( $response );
254 }
255
256 function update_version_1011() {
257 global $wpdb;
258
259 $updated = fep_get_option( 'v1011', 0, 'fep_updated_versions' );
260 if ( $updated ) {
261 return;
262 }
263
264 $custom_int = isset( $_POST['custom_int'] ) ? absint( $_POST['custom_int'] ) : 0;
265 $custom_str = isset( $_POST['custom_str'] ) ? sanitize_text_field( $_POST['custom_str']) : 'messages';
266
267 if ( 'announcements' == $custom_str ) {
268 $args = array(
269 'post_type' => 'fep_announcement',
270 'posts_per_page'=> 50,
271 'post_status' => 'any',
272 'meta_query' => array(
273 array(
274 'key' => '_fep_new_id',
275 'compare' => 'NOT EXISTS',
276 ),
277 ),
278 );
279 $announcements = get_posts( $args );
280 if ( $announcements ) {
281 foreach ( $announcements as $announcement ) {
282 $this->insert_announcement( $announcement );
283 }
284 $custom_int = $custom_int + count( $announcements );
285 $response = array(
286 'update' => 'continue',
287 'message' => sprintf( _n( '%s announcement updated', '%s announcements updated', $custom_int, 'front-end-pm' ), number_format_i18n( $custom_int ) ),
288 'custom_int'=> $custom_int,
289 'custom_str'=> $custom_str,
290 );
291 wp_send_json( $response );
292 }
293 } else {
294 $args = array(
295 'post_type' => 'fep_message',
296 'posts_per_page'=> 25,
297 'post_status' => 'any',
298 'post_parent' => 0,
299 'meta_query' => array(
300 array(
301 'key' => '_fep_new_id',
302 'compare' => 'NOT EXISTS',
303 ),
304 ),
305 );
306 $messages = get_posts( $args );
307 if ( $messages ) {
308 foreach ( $messages as $message ) {
309 $custom_int += $this->insert_message( $message );
310 }
311 //$custom_int = $custom_int + count( $messages );
312 $response = array(
313 'update' => 'continue',
314 'message' => sprintf( _n( '%s message updated', '%s messages updated', $custom_int, 'front-end-pm' ), number_format_i18n( $custom_int ) ),
315 'custom_int'=> $custom_int,
316 'custom_str'=> $custom_str,
317 );
318 wp_send_json( $response );
319 } else {
320 $response = array(
321 'update' => 'continue',
322 'message' => __( 'All messages updated', 'front-end-pm' ),
323 'custom_int'=> 0,
324 'custom_str'=> 'announcements',
325 );
326 wp_send_json( $response );
327 }
328 }
329
330 update_option( '_fep_can_delete_all', 1 );
331 // multisite add prefix_blogid_ to meta, so delete those as well.
332 $wpdb->query(
333 $wpdb->prepare( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE %s OR meta_key LIKE %s OR meta_key LIKE %s OR meta_key LIKE %s",
334 '%' . $wpdb->esc_like( '_FEP_user_options' ), // Not delete main site meta which is without leading underscore.
335 '%' . $wpdb->esc_like( '_fep_user_message_count' ),
336 '%' . $wpdb->esc_like( '_fep_user_announcement_count' ),
337 '%' . $wpdb->esc_like( '_fep_notification_dismiss' )
338 )
339 );
340
341 $wp_roles = wp_roles();
342 $roles = $wp_roles->get_names();
343 $roles = array_keys( $roles );
344
345 $caps = array(
346 'delete_published_fep_messages' => 1,
347 'delete_private_fep_messages' => 1,
348 'delete_others_fep_messages' => 1,
349 'delete_fep_messages' => 1,
350 'publish_fep_messages' => 1,
351 'read_private_fep_messages' => 1,
352 'edit_private_fep_messages' => 1,
353 'edit_others_fep_messages' => 1,
354 'edit_fep_messages' => 1,
355 'edit_published_fep_messages' => 1,
356 'create_fep_messages' => 1,
357
358 'delete_published_fep_announcements'=> 1,
359 'delete_private_fep_announcements' => 1,
360 'delete_others_fep_announcements' => 1,
361 'delete_fep_announcements' => 1,
362 'publish_fep_announcements' => 1,
363 'read_private_fep_announcements' => 1,
364 'edit_private_fep_announcements' => 1,
365 'edit_others_fep_announcements' => 1,
366 'edit_fep_announcements' => 1,
367 'edit_published_fep_announcements' => 1,
368 'create_fep_announcements' => 1,
369 );
370 foreach( $roles as $role ) {
371 $role_obj = get_role( $role );
372 if ( ! $role_obj ) {
373 continue;
374 }
375 foreach( $caps as $cap => $val ) {
376 $role_obj->remove_cap( $cap );
377 }
378 }
379
380 fep_update_option( 'v1011', 1, 'fep_updated_versions' );
381 $response = array(
382 'update' => 'continue',
383 'message' => __( 'All messages and announcements updated', 'front-end-pm' ),
384 'custom_int'=> 0,
385 'custom_str'=> '',
386 );
387 wp_send_json( $response );
388 }
389
390 function individual_to_threaded() {
391 global $wpdb;
392 $custom_int = isset( $_POST['custom_int'] ) ? absint( $_POST['custom_int'] ) : 0;
393
394 $args = array(
395 'mgs_type' => 'message',
396 'mgs_parent' => 0,
397 'mgs_status' => 'publish',
398 'per_page' => 50,
399 'orderby' => 'mgs_created',
400 'mgs_last_reply_by' => 0,
401 'fields' => array( 'mgs_id' ),
402 );
403 $messages = fep_get_messages( $args );
404 if ( $messages ) {
405 foreach ( $messages as $mgs_id ) {
406 fep_update_reply_info( $mgs_id );
407 }
408 $custom_int = $custom_int + count( $messages );
409 $response = array(
410 'update' => 'continue',
411 'message' => sprintf( _n( '%s message meta updated', '%s messages meta updated', $custom_int, 'front-end-pm' ), number_format_i18n( $custom_int ) ),
412 'custom_int'=> $custom_int,
413 'custom_str'=> '',
414 );
415 wp_send_json( $response );
416 }
417 delete_metadata( 'user', 0, '_fep_user_message_count', '', true );
418 update_option( '_fep_message_view_changed', 0 );
419 }
420
421 function insert_announcement( $announcement ) {
422 $arr = array(
423 'mgs_id' => $announcement->ID,
424 'mgs_parent' => $announcement->post_parent,
425 'mgs_author' => $announcement->post_author,
426 'mgs_created' => $announcement->post_date_gmt,
427 'mgs_title' => $announcement->post_title,
428 'mgs_content' => $announcement->post_content,
429 'mgs_last_reply_excerpt'=> fep_get_the_excerpt_from_content( 100, $announcement->post_content ),
430 'mgs_type' => 'announcement',
431 'mgs_status' => $announcement->post_status,
432 );
433
434 $mgs_obj = new FEP_Message;
435 $ann_id = $mgs_obj->insert( $arr );
436
437 if( ! $ann_id ){
438 return;
439 }
440 add_post_meta( $announcement->ID, '_fep_new_id', $ann_id );
441 fep_add_meta( $ann_id, '_fep_email_sent', get_post_meta( $announcement->ID, '_fep_email_sent', true ) );
442
443 $roles = get_post_meta( $announcement->ID, '_fep_participant_roles' );
444
445 if ( $roles ) {
446 $user_ids = get_users( [ 'fields' => 'ids', 'role__in' => $roles ] );
447 $user_ids[] = $mgs_obj->mgs_author;
448 $user_ids = array_unique( $user_ids );
449
450 $new_participants = [];
451 $read_by = get_post_meta( $announcement->ID, '_fep_read_by', true );
452 if( ! is_array( $read_by ) ){
453 $read_by = [];
454 } else {
455 $read_by = array_flip( $read_by );
456 }
457 $deleted_by = get_post_meta( $announcement->ID, '_fep_deleted_by', true );
458 if ( ! is_array( $deleted_by ) ) {
459 $deleted_by = array();
460 }
461
462 foreach( $user_ids as $participant ){
463 $new_participants[] = [
464 'mgs_participant' => $participant,
465 'mgs_read' => isset( $read_by[ $participant ] ) ? $read_by[ $participant ] : 0,
466 'mgs_parent_read' => isset( $read_by[ $participant ] ) ? $read_by[ $participant ] : 0,
467 'mgs_deleted' => isset( $deleted_by[ $participant ] ) ? $deleted_by[ $participant ] : 0,
468 'mgs_archived' => 0,
469 ];
470 }
471 if( $new_participants ){
472 $mgs_obj->insert_participants( $new_participants );
473 }
474
475 foreach( $roles as $role ){
476 fep_add_meta( $ann_id, '_fep_participant_roles', $role );
477 }
478 }
479
480 $this->insert_attachment( $announcement->ID, $mgs_obj );
481 $this->insert_meta( $announcement->ID, $ann_id );
482 }
483
484 function insert_message( $message ) {
485 $arr = array(
486 'mgs_id' => $message->ID,
487 'mgs_parent' => 0,
488 'mgs_author' => $message->post_author,
489 'mgs_created' => $message->post_date_gmt,
490 'mgs_title' => $message->post_title,
491 'mgs_content' => $message->post_content,
492 'mgs_type' => 'message',
493 'mgs_status' => $message->post_status,
494 );
495 if( 'threaded' == fep_get_message_view() ){
496 $arr['mgs_last_reply_by'] = get_post_meta( $message->ID, '_fep_last_reply_by', true );
497 $arr['mgs_last_reply_excerpt'] = fep_get_the_excerpt_from_content( 100, get_post_field('post_content', get_post_meta( $message->ID, '_fep_last_reply_id', true ) ) );
498 $arr['mgs_last_reply_time'] = get_post_field('post_date_gmt', get_post_meta( $message->ID, '_fep_last_reply_id', true ) );
499 }
500 $mgs_obj = new FEP_Message;
501 $message_id = $mgs_obj->insert( $arr );
502
503 if( ! $message_id ){
504 return 0;
505 }
506 $count = 1;
507
508 add_post_meta( $message->ID, '_fep_new_id', $message_id, true );
509 fep_add_meta( $message_id, '_fep_email_sent', get_post_meta( $message->ID, '_fep_email_sent', true ) );
510
511 $this->insert_participants( $message->ID, $mgs_obj );
512 $this->insert_attachment( $message->ID, $mgs_obj );
513 $this->insert_meta( $message->ID, $message_id );
514 $count += $this->insert_replies( $message->ID, $mgs_obj );
515 //$this->delete_message( $message->id );
516 return $count;
517 }
518
519 function insert_replies( $message_id, $mgs_obj ) {
520 $args = array(
521 'post_type' => 'fep_message',
522 'posts_per_page'=> -1,
523 'post_status' => 'any',
524 'post_parent' => $message_id,
525 );
526
527 if ( ! $replies = get_posts( $args ) ) {
528 return 0;
529 }
530 foreach ( $replies as $reply ) {
531 $arr = array(
532 'mgs_id' => $reply->ID,
533 'mgs_parent' => $mgs_obj->mgs_id,
534 'mgs_author' => $reply->post_author,
535 'mgs_created' => $reply->post_date_gmt,
536 'mgs_title' => $reply->post_title,
537 'mgs_content' => $reply->post_content,
538 'mgs_type' => 'message',
539 'mgs_status' => $reply->post_status,
540 );
541 $reply_obj = new FEP_Message;
542 $reply_id = $reply_obj->insert( $arr );
543
544 if ( $reply_id ) {
545 add_post_meta( $reply->ID, '_fep_new_id', $reply_id, true );
546 fep_add_meta( $reply_id, '_fep_email_sent', get_post_meta( $reply->ID, '_fep_email_sent', true ) );
547 $this->insert_participants( $reply->ID, $reply_obj );
548 $this->insert_attachment( $reply->ID, $reply_obj );
549 $this->insert_meta( $reply->ID, $reply_id );
550 }
551 }
552 return count( $replies );
553 }
554
555 function insert_attachment( $message_id, $mgs_obj ) {
556 $args = array(
557 'post_type' => 'attachment',
558 'posts_per_page'=> -1,
559 'post_status' => 'any',
560 'post_parent' => $message_id,
561 );
562 if ( ! $attachments = get_posts( $args ) ) {
563 return;
564 }
565 $new_attachments = [];
566
567 foreach ( $attachments as $attachment ) {
568 $new_attachments[] = [
569 'att_mime' => $attachment->post_mime_type,
570 'att_file' => get_attached_file( $attachment->ID ),
571 'att_status' => in_array( $attachment->post_status, [ 'publish', 'inherit' ] ) ? 'publish' : $attachment->post_status,
572 ];
573 }
574 if( $new_attachments ){
575 $mgs_obj->insert_attachments( $new_attachments );
576 }
577 }
578
579 function insert_participants( $message_id, $mgs_obj ){
580 if( 'threaded' == fep_get_message_view() && $mgs_obj->mgs_parent ){
581 $participants = get_post_meta( wp_get_post_parent_id( $message_id ), '_fep_participants' );
582 } else {
583 $participants = get_post_meta( $message_id, '_fep_participants' );
584 }
585 $new_participants = [];
586 $read_by = get_post_meta( $message_id, '_fep_read_by', true );
587 if( ! is_array( $read_by ) ){
588 $read_by = [];
589 } else {
590 $read_by = array_flip( $read_by );
591 }
592
593 foreach( $participants as $participant ){
594 $args = [
595 'mgs_participant' => $participant,
596 'mgs_read' => isset( $read_by[ $participant ] ) ? $read_by[ $participant ] : 0,
597 'mgs_deleted' => get_post_meta( $message_id, '_fep_delete_by_' . $participant, true ),
598 'mgs_archived' => get_post_meta( $message_id, '_fep_archived_by_' . $participant, true ),
599 ];
600 if( 'threaded' == fep_get_message_view() && $mgs_obj->mgs_parent ){
601 $args['mgs_parent_read'] = get_post_meta( wp_get_post_parent_id( $message_id ), '_fep_parent_read_by_' . $participant, true );
602 } else {
603 $args['mgs_parent_read'] = get_post_meta( $message_id, '_fep_parent_read_by_' . $participant, true );
604 }
605 $new_participants[] = $args;
606 }
607 if( $new_participants ){
608 $mgs_obj->insert_participants( $new_participants );
609 }
610 }
611
612 function insert_meta( $prev_id, $new_id ) {
613 if( ! $prev_id || ! is_numeric( $prev_id ) || ! $new_id || ! is_numeric( $new_id ) ) {
614 return false;
615 }
616 $meta = get_post_meta( $prev_id );
617 if ( ! $meta || ! is_array( $meta ) ) {
618 return false;
619 }
620 unset( $meta['_fep_read_by'], $meta['_fep_deleted_by'], $meta['_fep_participants'], $meta['_fep_participant_roles'], $meta['_fep_email_sent'], $meta['_fep_new_id'], $meta['_fep_last_reply_by'], $meta['_fep_last_reply_id'], $meta['_fep_last_reply_time'] );
621
622 foreach ( $meta as $meta_key => $meta_values ) {
623 if ( 0 === strpos( $meta_key, '_fep_parent_read_by_' ) || 0 === strpos( $meta_key, '_fep_delete_by_' ) || 0 === strpos( $meta_key, '_fep_archived_by_' ) ) {
624 continue;
625 }
626 foreach ( $meta_values as $meta_value ) {
627 fep_add_meta( $new_id, $meta_key, $meta_value );
628 }
629 }
630 }
631
632 function create_htaccess() {
633 add_filter( 'upload_dir', array( Fep_Attachment::init(), 'upload_dir' ), 99 );
634 $wp_upload_dir = wp_upload_dir();
635 remove_filter( 'upload_dir', array( Fep_Attachment::init(), 'upload_dir' ), 99 );
636
637 $upload_path = $wp_upload_dir['basedir'] . '/front-end-pm';
638
639 fep_create_htaccess_index( $upload_path );
640
641 $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($upload_path, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
642 foreach( $objects as $name => $object ){
643 if($object->isDir()){
644 fep_create_htaccess_index( $name );
645 }
646 }
647 }
648
649 function remove_htaccess(){
650 //Created htaccess files outside front-end-pm folder
651 add_filter( 'upload_dir', array( Fep_Attachment::init(), 'upload_dir' ), 99 );
652 $wp_upload_dir = wp_upload_dir();
653 remove_filter( 'upload_dir', array( Fep_Attachment::init(), 'upload_dir' ), 99 );
654
655 $upload_path = $wp_upload_dir['basedir'];
656 if( file_exists( $upload_path . '/.htaccess' ) ){
657 $content = file_get_contents( $upload_path . '/.htaccess' );
658 if( "Options -Indexes\ndeny from all\n" == $content ){
659 unlink( $upload_path . '/.htaccess' );
660 }
661 }
662 }
663 } //END CLASS
664
665 add_action( 'init', array( Fep_Update::init(), 'actions_filters' ) );
666
667