| 1 |
<?php |
| 2 |
/** |
| 3 |
* Import notification from old plugin. |
| 4 |
* |
| 5 |
* @since 1.0 |
| 6 |
*/ |
| 7 |
|
| 8 |
class BNFW_Import { |
| 9 |
const EMAIL_OPTION = 'bnfw_custom_email_settings'; |
| 10 |
const SETTING_OPTION = 'bnfw_settings'; |
| 11 |
private $events = array( 'create_term', 'publish_post', 'comment_post', 'user_register', 'trackback_post', 'pingback_post', 'lostpassword_post' ); |
| 12 |
|
| 13 |
/** |
| 14 |
* Import notification from old plugin. |
| 15 |
* |
| 16 |
* @since 1.0 |
| 17 |
*/ |
| 18 |
public function import() { |
| 19 |
global $wp_roles; |
| 20 |
$roles = $wp_roles->get_names(); |
| 21 |
|
| 22 |
if ( $this->import_needed() ) { |
| 23 |
$old_events = get_option( self::SETTING_OPTION ); |
| 24 |
foreach ( $old_events as $event => $value ) { |
| 25 |
if ( '1' == $value ) { |
| 26 |
$event_array = explode( '-', $event ); |
| 27 |
if ( 2 == count( $event_array ) ) { |
| 28 |
if ( in_array( $event_array[0], $this->events ) && in_array( $event_array[1], array_keys( $roles ) ) ) { |
| 29 |
$event_array[1] = $roles[$event_array[1]]; |
| 30 |
$this->insert_notification( $event_array ); |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
// delete the old options |
| 36 |
$this->delete_option(); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Check if import is needed. |
| 42 |
* |
| 43 |
* @since 1.0 |
| 44 |
* @return unknown |
| 45 |
*/ |
| 46 |
private function import_needed() { |
| 47 |
if ( get_option( self::EMAIL_OPTION ) && get_option( self::SETTING_OPTION ) ) { |
| 48 |
return true; |
| 49 |
} |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Insert notification. |
| 55 |
* |
| 56 |
* @param mixed $event |
| 57 |
*/ |
| 58 |
private function insert_notification( $event ) { |
| 59 |
$post = array( |
| 60 |
'post_title' => $event[0] . __( ' for ', 'bnfw' ) . $event[1] . __( ' (Auto Imported)', 'bnfw' ), |
| 61 |
'post_type' => BNFW_Notification::POST_TYPE, |
| 62 |
'post_content' => '', |
| 63 |
'post_status' => 'publish', |
| 64 |
); |
| 65 |
|
| 66 |
$post_id = wp_insert_post( $post ); |
| 67 |
if ( $post_id > 0 ) { |
| 68 |
$content = $this->map_notification_content( $event[0] ); |
| 69 |
$setting = array( |
| 70 |
'notification' => $this->map_notification( $event[0] ), |
| 71 |
'user-roles' => array( $event[1] ), |
| 72 |
'users' => array(), |
| 73 |
'subject' => $content['subject'], |
| 74 |
'message' => $content['body'], |
| 75 |
); |
| 76 |
|
| 77 |
foreach ( $setting as $key => $value ) { |
| 78 |
update_post_meta( $post_id, BNFW_Notification::META_KEY_PREFIX . $key, $value ); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Map old notification type to new notification type. |
| 85 |
* |
| 86 |
* @param mixed $event_name |
| 87 |
* @return unknown |
| 88 |
*/ |
| 89 |
private function map_notification( $event_name ) { |
| 90 |
switch ( $event_name ) { |
| 91 |
case 'create_term': |
| 92 |
return 'new-category'; |
| 93 |
break; |
| 94 |
case 'publish_post': |
| 95 |
return 'new-post'; |
| 96 |
break; |
| 97 |
case 'comment_post': |
| 98 |
return 'new-comment'; |
| 99 |
break; |
| 100 |
case 'user_register': |
| 101 |
return 'new-user'; |
| 102 |
break; |
| 103 |
case 'trackback_post': |
| 104 |
return 'new-trackback'; |
| 105 |
break; |
| 106 |
case 'pingback_post': |
| 107 |
return 'new-pingback'; |
| 108 |
break; |
| 109 |
case 'lostpassword_post': |
| 110 |
return 'user-password'; |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Map content from old plugin. |
| 117 |
* |
| 118 |
* @param unknown $event_name |
| 119 |
* @return unknown |
| 120 |
*/ |
| 121 |
private function map_notification_content( $event_name ) { |
| 122 |
$content = array(); |
| 123 |
if ( ! isset( $this->content_map ) ) { |
| 124 |
$this->parse_content(); |
| 125 |
} |
| 126 |
return $this->content_map[$event_name]; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Parse content from old plugins setting. |
| 131 |
* |
| 132 |
* @since 1.0 |
| 133 |
*/ |
| 134 |
private function parse_content() { |
| 135 |
$old_content = get_option( self::EMAIL_OPTION ); |
| 136 |
$content_map = array(); |
| 137 |
foreach ( $old_content as $key => $value ) { |
| 138 |
$key_array = explode( '-', $key ); |
| 139 |
if ( 3 == count( $key_array ) ) { |
| 140 |
$content_map[$key_array[2]][$key_array[1]] = $value; |
| 141 |
} |
| 142 |
} |
| 143 |
$this->content_map = $content_map; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Delete old plugin database options. |
| 148 |
* |
| 149 |
* @since 1.0 |
| 150 |
*/ |
| 151 |
private function delete_option() { |
| 152 |
delete_option( self::EMAIL_OPTION ); |
| 153 |
delete_option( self::SETTING_OPTION ); |
| 154 |
} |
| 155 |
} |
| 156 |
?> |
| 157 |
|