PluginProbe
Customize WordPress Emails and Alerts – Better Notifications for WP / 1.6
Customize WordPress Emails and Alerts – Better Notifications for WP v1.6
1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.8 1.8.1 1.8.10 1.8.11 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 All 81 releases
bnfw / includes / import.php

import.php in Customize WordPress Emails and Alerts – Better Notifications for WP 1.6, at includes/import.php

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