@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | */ |
28 | 28 | |
29 | 29 | wordpoints_register_extension( |
30 | - ' |
|
30 | + ' |
|
31 | 31 | Extension Name: Importer |
32 | 32 | Author: J.D. Grimes |
33 | 33 | Author URI: https://codesymphony.co/ |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | ID: 430 |
42 | 42 | Namespace: Importer |
43 | 43 | ' |
44 | - , __FILE__ |
|
44 | + , __FILE__ |
|
45 | 45 | ); |
46 | 46 | |
47 | 47 | WordPoints_Class_Autoloader::register_dir( dirname( __FILE__ ) . '/includes' ); |
@@ -55,12 +55,12 @@ discard block |
||
55 | 55 | |
56 | 56 | if ( is_admin() ) { |
57 | 57 | |
58 | - /** |
|
59 | - * The extension's admin-side code. |
|
60 | - * |
|
61 | - * @since 1.0.0 |
|
62 | - */ |
|
63 | - require_once dirname( __FILE__ ) . '/admin/admin.php'; |
|
58 | + /** |
|
59 | + * The extension's admin-side code. |
|
60 | + * |
|
61 | + * @since 1.0.0 |
|
62 | + */ |
|
63 | + require_once dirname( __FILE__ ) . '/admin/admin.php'; |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | // EOF |
@@ -14,23 +14,23 @@ discard block |
||
14 | 14 | */ |
15 | 15 | function wordpoints_importer_admin_menu() { |
16 | 16 | |
17 | - add_submenu_page( |
|
18 | - wordpoints_get_main_admin_menu() |
|
19 | - ,__( 'WordPoints — Import', 'wordpoints-importer' ) |
|
20 | - ,__( 'Import', 'wordpoints-importer' ) |
|
21 | - ,'manage_options' |
|
22 | - ,'wordpoints_import' |
|
23 | - ,'wordpoints_import_admin_screen' |
|
24 | - ); |
|
25 | - |
|
26 | - add_submenu_page( |
|
27 | - '_wordpoints_import' // Fake. |
|
28 | - ,__( 'WordPoints — Importing', 'wordpoints-importer' ) |
|
29 | - ,__( 'Importing', 'wordpoints-importer' ) |
|
30 | - ,'manage_options' |
|
31 | - ,'wordpoints_importing' |
|
32 | - ,'wordpoints_importing_admin_screen' |
|
33 | - ); |
|
17 | + add_submenu_page( |
|
18 | + wordpoints_get_main_admin_menu() |
|
19 | + ,__( 'WordPoints — Import', 'wordpoints-importer' ) |
|
20 | + ,__( 'Import', 'wordpoints-importer' ) |
|
21 | + ,'manage_options' |
|
22 | + ,'wordpoints_import' |
|
23 | + ,'wordpoints_import_admin_screen' |
|
24 | + ); |
|
25 | + |
|
26 | + add_submenu_page( |
|
27 | + '_wordpoints_import' // Fake. |
|
28 | + ,__( 'WordPoints — Importing', 'wordpoints-importer' ) |
|
29 | + ,__( 'Importing', 'wordpoints-importer' ) |
|
30 | + ,'manage_options' |
|
31 | + ,'wordpoints_importing' |
|
32 | + ,'wordpoints_importing_admin_screen' |
|
33 | + ); |
|
34 | 34 | } |
35 | 35 | add_action( 'admin_menu', 'wordpoints_importer_admin_menu' ); |
36 | 36 | add_action( 'network_admin_menu', 'wordpoints_importer_admin_menu' ); |
@@ -42,12 +42,12 @@ discard block |
||
42 | 42 | */ |
43 | 43 | function wordpoints_import_admin_screen() { |
44 | 44 | |
45 | - /** |
|
46 | - * The importer admin screen. |
|
47 | - * |
|
48 | - * @since 1.0.0 |
|
49 | - */ |
|
50 | - require dirname( __FILE__ ) . '/screens/import.php'; |
|
45 | + /** |
|
46 | + * The importer admin screen. |
|
47 | + * |
|
48 | + * @since 1.0.0 |
|
49 | + */ |
|
50 | + require dirname( __FILE__ ) . '/screens/import.php'; |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | /** |
@@ -57,12 +57,12 @@ discard block |
||
57 | 57 | */ |
58 | 58 | function wordpoints_importing_admin_screen() { |
59 | 59 | |
60 | - /** |
|
61 | - * The importer admin screen. |
|
62 | - * |
|
63 | - * @since 1.0.0 |
|
64 | - */ |
|
65 | - require dirname( __FILE__ ) . '/screens/importing.php'; |
|
60 | + /** |
|
61 | + * The importer admin screen. |
|
62 | + * |
|
63 | + * @since 1.0.0 |
|
64 | + */ |
|
65 | + require dirname( __FILE__ ) . '/screens/importing.php'; |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | /** |
@@ -72,17 +72,17 @@ discard block |
||
72 | 72 | */ |
73 | 73 | function wordpoints_importer_register_admin_scripts() { |
74 | 74 | |
75 | - $assets_url = wordpoints_extensions_url( |
|
76 | - 'admin/assets' |
|
77 | - , dirname( dirname( __FILE__ ) ) . '/importer.php' |
|
78 | - ); |
|
75 | + $assets_url = wordpoints_extensions_url( |
|
76 | + 'admin/assets' |
|
77 | + , dirname( dirname( __FILE__ ) ) . '/importer.php' |
|
78 | + ); |
|
79 | 79 | |
80 | - $suffix = SCRIPT_DEBUG ? '' : '.min'; |
|
80 | + $suffix = SCRIPT_DEBUG ? '' : '.min'; |
|
81 | 81 | |
82 | - wp_register_style( |
|
83 | - 'wordpoints-importer-feedback' |
|
84 | - , "{$assets_url}/css/feedback{$suffix}.css" |
|
85 | - ); |
|
82 | + wp_register_style( |
|
83 | + 'wordpoints-importer-feedback' |
|
84 | + , "{$assets_url}/css/feedback{$suffix}.css" |
|
85 | + ); |
|
86 | 86 | } |
87 | 87 | add_action( 'init', 'wordpoints_importer_register_admin_scripts' ); |
88 | 88 | |
@@ -93,41 +93,41 @@ discard block |
||
93 | 93 | */ |
94 | 94 | function wordpoints_importer_do_import() { |
95 | 95 | |
96 | - if ( ! defined( 'IFRAME_REQUEST' ) ) { |
|
97 | - define( 'IFRAME_REQUEST', true ); |
|
98 | - } |
|
96 | + if ( ! defined( 'IFRAME_REQUEST' ) ) { |
|
97 | + define( 'IFRAME_REQUEST', true ); |
|
98 | + } |
|
99 | 99 | |
100 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
101 | - wp_die( esc_html__( 'Sorry, you are not allowed to import to WordPoints.', 'wordpoints-importer' ) ); |
|
102 | - } |
|
100 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
101 | + wp_die( esc_html__( 'Sorry, you are not allowed to import to WordPoints.', 'wordpoints-importer' ) ); |
|
102 | + } |
|
103 | 103 | |
104 | - check_admin_referer( 'wordpoints_import' ); |
|
104 | + check_admin_referer( 'wordpoints_import' ); |
|
105 | 105 | |
106 | - if ( ! isset( $_GET['importer'] ) ) { |
|
107 | - wp_die( esc_html__( 'No importer selected.', 'wordpoints-importer' ) ); |
|
108 | - } |
|
106 | + if ( ! isset( $_GET['importer'] ) ) { |
|
107 | + wp_die( esc_html__( 'No importer selected.', 'wordpoints-importer' ) ); |
|
108 | + } |
|
109 | 109 | |
110 | - $importer = WordPoints_Importers::get_importer( |
|
111 | - sanitize_key( $_GET['importer'] ) |
|
112 | - ); |
|
110 | + $importer = WordPoints_Importers::get_importer( |
|
111 | + sanitize_key( $_GET['importer'] ) |
|
112 | + ); |
|
113 | 113 | |
114 | - if ( ! ( $importer instanceof WordPoints_Importer ) ) { |
|
115 | - wp_die( esc_html__( 'Importer not installed.', 'wordpoints-importer' ) ); |
|
116 | - } |
|
114 | + if ( ! ( $importer instanceof WordPoints_Importer ) ) { |
|
115 | + wp_die( esc_html__( 'Importer not installed.', 'wordpoints-importer' ) ); |
|
116 | + } |
|
117 | 117 | |
118 | - wp_enqueue_style( 'wordpoints-importer-feedback' ); |
|
118 | + wp_enqueue_style( 'wordpoints-importer-feedback' ); |
|
119 | 119 | |
120 | - $args = array(); |
|
120 | + $args = array(); |
|
121 | 121 | |
122 | - if ( isset( $_GET['wordpoints_import'] ) && is_array( $_GET['wordpoints_import'] ) ) { |
|
123 | - $args = wp_unslash( $_GET['wordpoints_import'] ); // WPCS: sanitization OK. |
|
124 | - } |
|
122 | + if ( isset( $_GET['wordpoints_import'] ) && is_array( $_GET['wordpoints_import'] ) ) { |
|
123 | + $args = wp_unslash( $_GET['wordpoints_import'] ); // WPCS: sanitization OK. |
|
124 | + } |
|
125 | 125 | |
126 | - iframe_header(); |
|
126 | + iframe_header(); |
|
127 | 127 | |
128 | - $importer->do_import( $args ); |
|
128 | + $importer->do_import( $args ); |
|
129 | 129 | |
130 | - iframe_footer(); |
|
130 | + iframe_footer(); |
|
131 | 131 | } |
132 | 132 | add_action( 'update-custom_wordpoints_import', 'wordpoints_importer_do_import' ); |
133 | 133 | |
@@ -138,9 +138,9 @@ discard block |
||
138 | 138 | */ |
139 | 139 | function wordpoints_importer_admin_screen_points_type_select() { |
140 | 140 | |
141 | - $args = array( 'name' => 'wordpoints_import[points][_data][points_type]' ); |
|
141 | + $args = array( 'name' => 'wordpoints_import[points][_data][points_type]' ); |
|
142 | 142 | |
143 | - ?> |
|
143 | + ?> |
|
144 | 144 | |
145 | 145 | <p> |
146 | 146 | <label for="wordpoints_import[points][_data][points_type]"> |
@@ -152,8 +152,8 @@ discard block |
||
152 | 152 | <?php |
153 | 153 | } |
154 | 154 | add_action( |
155 | - 'wordpoints_importer_before_component_options-points' |
|
156 | - , 'wordpoints_importer_admin_screen_points_type_select' |
|
155 | + 'wordpoints_importer_before_component_options-points' |
|
156 | + , 'wordpoints_importer_admin_screen_points_type_select' |
|
157 | 157 | ); |
158 | 158 | |
159 | 159 | /** |
@@ -169,24 +169,24 @@ discard block |
||
169 | 169 | */ |
170 | 170 | function wordpoints_importer_validate_points_type_setting( $valid, $settings, $feedback ) { |
171 | 171 | |
172 | - if ( $valid ) { |
|
172 | + if ( $valid ) { |
|
173 | 173 | |
174 | - if ( ! isset( $settings['points_type'] ) ) { |
|
175 | - $feedback->warning( __( 'Skipping Points component—no points type specified.', 'wordpoints-importer' ) ); |
|
176 | - $valid = false; |
|
177 | - } elseif ( ! wordpoints_is_points_type( $settings['points_type'] ) ) { |
|
178 | - $feedback->warning( __( 'Skipping Points component—invalid points type selected.', 'wordpoints-importer' ) ); |
|
179 | - $valid = false; |
|
180 | - } |
|
181 | - } |
|
174 | + if ( ! isset( $settings['points_type'] ) ) { |
|
175 | + $feedback->warning( __( 'Skipping Points component—no points type specified.', 'wordpoints-importer' ) ); |
|
176 | + $valid = false; |
|
177 | + } elseif ( ! wordpoints_is_points_type( $settings['points_type'] ) ) { |
|
178 | + $feedback->warning( __( 'Skipping Points component—invalid points type selected.', 'wordpoints-importer' ) ); |
|
179 | + $valid = false; |
|
180 | + } |
|
181 | + } |
|
182 | 182 | |
183 | - return $valid; |
|
183 | + return $valid; |
|
184 | 184 | } |
185 | 185 | add_action( |
186 | - 'wordpoints_import_settings_valid-points' |
|
187 | - , 'wordpoints_importer_validate_points_type_setting' |
|
188 | - , 10 |
|
189 | - , 3 |
|
186 | + 'wordpoints_import_settings_valid-points' |
|
187 | + , 'wordpoints_importer_validate_points_type_setting' |
|
188 | + , 10 |
|
189 | + , 3 |
|
190 | 190 | ); |
191 | 191 | |
192 | 192 | /** |
@@ -196,20 +196,20 @@ discard block |
||
196 | 196 | */ |
197 | 197 | function wordpoints_importer_admin_screen_rank_group_select() { |
198 | 198 | |
199 | - $rank_groups = WordPoints_Rank_Groups::get(); |
|
199 | + $rank_groups = WordPoints_Rank_Groups::get(); |
|
200 | 200 | |
201 | - // See https://github.com/WordPoints/wordpoints/issues/310. |
|
202 | - $options = array(); |
|
203 | - foreach ( $rank_groups as $rank_group ) { |
|
204 | - $options[ $rank_group->slug ] = $rank_group->name; |
|
205 | - } |
|
201 | + // See https://github.com/WordPoints/wordpoints/issues/310. |
|
202 | + $options = array(); |
|
203 | + foreach ( $rank_groups as $rank_group ) { |
|
204 | + $options[ $rank_group->slug ] = $rank_group->name; |
|
205 | + } |
|
206 | 206 | |
207 | - $dropdown = new WordPoints_Dropdown_Builder( |
|
208 | - $options |
|
209 | - , array( 'name' => 'wordpoints_import[ranks][_data][rank_group]' ) |
|
210 | - ); |
|
207 | + $dropdown = new WordPoints_Dropdown_Builder( |
|
208 | + $options |
|
209 | + , array( 'name' => 'wordpoints_import[ranks][_data][rank_group]' ) |
|
210 | + ); |
|
211 | 211 | |
212 | - ?> |
|
212 | + ?> |
|
213 | 213 | |
214 | 214 | <p> |
215 | 215 | <label for="wordpoints_import[ranks][_data][rank_group]"> |
@@ -221,8 +221,8 @@ discard block |
||
221 | 221 | <?php |
222 | 222 | } |
223 | 223 | add_action( |
224 | - 'wordpoints_importer_before_component_options-ranks' |
|
225 | - , 'wordpoints_importer_admin_screen_rank_group_select' |
|
224 | + 'wordpoints_importer_before_component_options-ranks' |
|
225 | + , 'wordpoints_importer_admin_screen_rank_group_select' |
|
226 | 226 | ); |
227 | 227 | |
228 | 228 | /** |
@@ -238,24 +238,24 @@ discard block |
||
238 | 238 | */ |
239 | 239 | function wordpoints_importer_validate_rank_group_setting( $valid, $settings, $feedback ) { |
240 | 240 | |
241 | - if ( $valid ) { |
|
241 | + if ( $valid ) { |
|
242 | 242 | |
243 | - if ( ! isset( $settings['rank_group'] ) ) { |
|
244 | - $feedback->warning( __( 'Skipping Ranks component—no rank group specified.', 'wordpoints-importer' ) ); |
|
245 | - $valid = false; |
|
246 | - } elseif ( ! WordPoints_Rank_Groups::is_group_registered( $settings['rank_group'] ) ) { |
|
247 | - $feedback->warning( __( 'Skipping Ranks component—invalid rank group selected.', 'wordpoints-importer' ) ); |
|
248 | - $valid = false; |
|
249 | - } |
|
250 | - } |
|
243 | + if ( ! isset( $settings['rank_group'] ) ) { |
|
244 | + $feedback->warning( __( 'Skipping Ranks component—no rank group specified.', 'wordpoints-importer' ) ); |
|
245 | + $valid = false; |
|
246 | + } elseif ( ! WordPoints_Rank_Groups::is_group_registered( $settings['rank_group'] ) ) { |
|
247 | + $feedback->warning( __( 'Skipping Ranks component—invalid rank group selected.', 'wordpoints-importer' ) ); |
|
248 | + $valid = false; |
|
249 | + } |
|
250 | + } |
|
251 | 251 | |
252 | - return $valid; |
|
252 | + return $valid; |
|
253 | 253 | } |
254 | 254 | add_action( |
255 | - 'wordpoints_import_settings_valid-ranks' |
|
256 | - , 'wordpoints_importer_validate_rank_group_setting' |
|
257 | - , 10 |
|
258 | - , 3 |
|
255 | + 'wordpoints_import_settings_valid-ranks' |
|
256 | + , 'wordpoints_importer_validate_rank_group_setting' |
|
257 | + , 10 |
|
258 | + , 3 |
|
259 | 259 | ); |
260 | 260 | |
261 | 261 | // EOF |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | */ |
9 | 9 | |
10 | 10 | if ( true !== current_user_can( 'manage_options' ) ) { |
11 | - wp_die(); |
|
11 | + wp_die(); |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | $tabs = array( 'unavailable' => __( 'Unavailable', 'wordpoints-importer' ) ); |
@@ -16,19 +16,19 @@ discard block |
||
16 | 16 | |
17 | 17 | foreach ( WordPoints_Importers::get() as $slug => $args ) { |
18 | 18 | |
19 | - $importer = WordPoints_Importers::get_importer( $slug ); |
|
19 | + $importer = WordPoints_Importers::get_importer( $slug ); |
|
20 | 20 | |
21 | - $is_available = $importer->is_available(); |
|
21 | + $is_available = $importer->is_available(); |
|
22 | 22 | |
23 | - if ( is_wp_error( $is_available ) ) { |
|
24 | - $unavailable[ $args['name'] ] = $is_available; |
|
25 | - } else { |
|
26 | - $tabs[ $slug ] = $args['name']; |
|
27 | - } |
|
23 | + if ( is_wp_error( $is_available ) ) { |
|
24 | + $unavailable[ $args['name'] ] = $is_available; |
|
25 | + } else { |
|
26 | + $tabs[ $slug ] = $args['name']; |
|
27 | + } |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | if ( empty( $unavailable ) ) { |
31 | - unset( $tabs['unavailable'] ); |
|
31 | + unset( $tabs['unavailable'] ); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | $current_tab = wordpoints_admin_get_current_tab( $tabs ); |
@@ -51,14 +51,14 @@ discard block |
||
51 | 51 | <li> |
52 | 52 | <?php |
53 | 53 | |
54 | - printf( |
|
55 | - /* translators: 1 is an importer name, 2 is the reason that it is unavailable. */ |
|
56 | - esc_html__( '%1$s — %2$s', 'wordpoints-importer' ) |
|
57 | - , '<strong>' . esc_html( $name ) . '</strong>' |
|
58 | - , esc_html( $error->get_error_message() ) |
|
59 | - ); |
|
54 | + printf( |
|
55 | + /* translators: 1 is an importer name, 2 is the reason that it is unavailable. */ |
|
56 | + esc_html__( '%1$s — %2$s', 'wordpoints-importer' ) |
|
57 | + , '<strong>' . esc_html( $name ) . '</strong>' |
|
58 | + , esc_html( $error->get_error_message() ) |
|
59 | + ); |
|
60 | 60 | |
61 | - ?> |
|
61 | + ?> |
|
62 | 62 | </li> |
63 | 63 | <?php endforeach; ?> |
64 | 64 | </ul> |
@@ -74,39 +74,39 @@ discard block |
||
74 | 74 | |
75 | 75 | <?php |
76 | 76 | |
77 | - if ( ! $importer->supports_component( $slug ) ) { |
|
78 | - continue; |
|
79 | - } |
|
77 | + if ( ! $importer->supports_component( $slug ) ) { |
|
78 | + continue; |
|
79 | + } |
|
80 | 80 | |
81 | - $supported = true; |
|
81 | + $supported = true; |
|
82 | 82 | |
83 | - ?> |
|
83 | + ?> |
|
84 | 84 | |
85 | 85 | <h3><?php echo esc_html( $component['name'] ); ?></h3> |
86 | 86 | |
87 | 87 | <?php |
88 | 88 | |
89 | - /** |
|
90 | - * Before the import option fields for a component. |
|
91 | - * |
|
92 | - * @since 1.0.0 |
|
93 | - */ |
|
94 | - do_action( "wordpoints_importer_before_component_options-{$slug}" ); |
|
89 | + /** |
|
90 | + * Before the import option fields for a component. |
|
91 | + * |
|
92 | + * @since 1.0.0 |
|
93 | + */ |
|
94 | + do_action( "wordpoints_importer_before_component_options-{$slug}" ); |
|
95 | 95 | |
96 | - ?> |
|
96 | + ?> |
|
97 | 97 | |
98 | 98 | <?php foreach ( $importer->get_options_for_component( $slug ) as $name => $option ) : ?> |
99 | 99 | |
100 | 100 | <?php |
101 | 101 | |
102 | - $can_import = true; |
|
102 | + $can_import = true; |
|
103 | 103 | |
104 | - // Check if this option is available. |
|
105 | - if ( isset( $option['can_import'] ) ) { |
|
106 | - $can_import = call_user_func( $option['can_import'], array() ); |
|
107 | - } |
|
104 | + // Check if this option is available. |
|
105 | + if ( isset( $option['can_import'] ) ) { |
|
106 | + $can_import = call_user_func( $option['can_import'], array() ); |
|
107 | + } |
|
108 | 108 | |
109 | - ?> |
|
109 | + ?> |
|
110 | 110 | |
111 | 111 | <label for="wordpoints_import[<?php echo esc_attr( $slug ); ?>][<?php echo esc_attr( $name ); ?>]"> |
112 | 112 | <input type="checkbox" value="1" id="wordpoints_import[<?php echo esc_attr( $slug ); ?>][<?php echo esc_attr( $name ); ?>]" name="wordpoints_import[<?php echo esc_attr( $slug ); ?>][<?php echo esc_attr( $name ); ?>]" <?php disabled( is_wp_error( $can_import ), true ); ?> /> |
@@ -116,10 +116,10 @@ discard block |
||
116 | 116 | <em> |
117 | 117 | <?php |
118 | 118 | |
119 | - // translators: Error message explaining the reason why the importer is disabled. |
|
120 | - printf( esc_html__( 'Disabled (%s)', 'wordpoints-importer' ), esc_html( $can_import->get_error_message() ) ); |
|
119 | + // translators: Error message explaining the reason why the importer is disabled. |
|
120 | + printf( esc_html__( 'Disabled (%s)', 'wordpoints-importer' ), esc_html( $can_import->get_error_message() ) ); |
|
121 | 121 | |
122 | - ?> |
|
122 | + ?> |
|
123 | 123 | </em> |
124 | 124 | <?php endif; ?> |
125 | 125 | </label> |
@@ -140,20 +140,20 @@ discard block |
||
140 | 140 | |
141 | 141 | <?php |
142 | 142 | |
143 | - if ( ! isset( $supported ) ) { |
|
143 | + if ( ! isset( $supported ) ) { |
|
144 | 144 | |
145 | - esc_html_e( |
|
146 | - 'This importer does not support any of the installed components.' |
|
147 | - , 'wordpoints-importer' |
|
148 | - ); |
|
145 | + esc_html_e( |
|
146 | + 'This importer does not support any of the installed components.' |
|
147 | + , 'wordpoints-importer' |
|
148 | + ); |
|
149 | 149 | |
150 | - } else { |
|
150 | + } else { |
|
151 | 151 | |
152 | - wp_nonce_field( 'wordpoints_import' ); |
|
153 | - submit_button( __( 'Import', 'wordpoints-importer' ) ); |
|
154 | - } |
|
152 | + wp_nonce_field( 'wordpoints_import' ); |
|
153 | + submit_button( __( 'Import', 'wordpoints-importer' ) ); |
|
154 | + } |
|
155 | 155 | |
156 | - ?> |
|
156 | + ?> |
|
157 | 157 | </form> |
158 | 158 | <?php endif; ?> |
159 | 159 | </div> |
@@ -14,237 +14,237 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class WordPoints_Importer { |
16 | 16 | |
17 | - /** |
|
18 | - * The name of the importer. |
|
19 | - * |
|
20 | - * @since 1.0.0 |
|
21 | - * |
|
22 | - * @type string $name |
|
23 | - */ |
|
24 | - protected $name; |
|
25 | - |
|
26 | - /** |
|
27 | - * The components supported by this importer. |
|
28 | - * |
|
29 | - * The keys are the component slugs, the values arrays of options for importing |
|
30 | - * to that component. |
|
31 | - * |
|
32 | - * @since 1.0.0 |
|
33 | - * |
|
34 | - * @type array[] $components |
|
35 | - */ |
|
36 | - protected $components = array(); |
|
37 | - |
|
38 | - /** |
|
39 | - * The feedback provider object. |
|
40 | - * |
|
41 | - * This is only set by self::do_import(). |
|
42 | - * |
|
43 | - * @since 1.0.0 |
|
44 | - * |
|
45 | - * @type WordPoints_Importer_Feedback $feedback |
|
46 | - */ |
|
47 | - protected $feedback; |
|
48 | - |
|
49 | - /** |
|
50 | - * Check if this importer is available. |
|
51 | - * |
|
52 | - * @since 1.0.0 |
|
53 | - * |
|
54 | - * @return true|WP_Error A WP_Error if the importer is not available. |
|
55 | - */ |
|
56 | - abstract public function is_available(); |
|
57 | - |
|
58 | - /** |
|
59 | - * Construct the importer. |
|
60 | - * |
|
61 | - * @since 1.0.0 |
|
62 | - * |
|
63 | - * @param string $name The name of the importer. |
|
64 | - */ |
|
65 | - public function __construct( $name ) { |
|
66 | - |
|
67 | - $this->name = $name; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Check if this importer supports a specific component. |
|
72 | - * |
|
73 | - * @since 1.0.0 |
|
74 | - * |
|
75 | - * @param string $component The slug of a component. |
|
76 | - * |
|
77 | - * @return bool True if the component is supported, otherwise false. |
|
78 | - */ |
|
79 | - public function supports_component( $component ) { |
|
80 | - |
|
81 | - return isset( $this->components[ $component ] ); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Get the import options for a component. |
|
86 | - * |
|
87 | - * @since 1.0.0 |
|
88 | - * |
|
89 | - * @param string $component The slug of a component. |
|
90 | - * |
|
91 | - * @return array[] The options for this component. |
|
92 | - */ |
|
93 | - public function get_options_for_component( $component ) { |
|
94 | - |
|
95 | - if ( ! $this->supports_component( $component ) ) { |
|
96 | - return array(); |
|
97 | - } |
|
98 | - |
|
99 | - return $this->components[ $component ]; |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Run the import. |
|
104 | - * |
|
105 | - * @since 1.0.0 |
|
106 | - * |
|
107 | - * @param array $args The settings for the import. |
|
108 | - * @param WordPoints_Importer_Feedback $feedback The feedback object. |
|
109 | - */ |
|
110 | - public function do_import( array $args, $feedback = null ) { |
|
111 | - |
|
112 | - if ( ! ( $feedback instanceof WordPoints_Importer_Feedback ) ) { |
|
113 | - $feedback = new WordPoints_Importer_Feedback(); |
|
114 | - } |
|
115 | - |
|
116 | - $this->feedback = $feedback; |
|
117 | - |
|
118 | - // translators: Plugin name. |
|
119 | - $this->feedback->info( sprintf( __( 'Importing from %s…', 'wordpoints-importer' ), $this->name ) ); |
|
120 | - |
|
121 | - $this->no_interruptions(); |
|
122 | - |
|
123 | - foreach ( $args as $component => $options ) { |
|
124 | - $this->do_import_for_component( $component, $options ); |
|
125 | - } |
|
126 | - |
|
127 | - $this->feedback->info( __( 'Import complete.', 'wordpoints-importer' ) ); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Prevent any interruptions from occurring during the import. |
|
132 | - * |
|
133 | - * @since 1.2.1 |
|
134 | - */ |
|
135 | - protected function no_interruptions() { |
|
136 | - |
|
137 | - wordpoints_prevent_interruptions(); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Validate the import settings for a component. |
|
142 | - * |
|
143 | - * @since 1.0.0 |
|
144 | - * |
|
145 | - * @param string $component The slug of the component. |
|
146 | - * @param array $settings The settings supplied for this component. |
|
147 | - * |
|
148 | - * @return bool Whether the settings are valid. |
|
149 | - */ |
|
150 | - protected function validate_import_settings( $component, $settings ) { |
|
151 | - |
|
152 | - /** |
|
153 | - * Filter whether the settings are valid before importing. |
|
154 | - * |
|
155 | - * @since 1.0.0 |
|
156 | - * |
|
157 | - * @param bool $valid Whether the settings are valid. |
|
158 | - * @param array $settings The settings for this component. |
|
159 | - * @param WordPoints_Importer_Feedback $feedback The feedback object. |
|
160 | - */ |
|
161 | - return apply_filters( "wordpoints_import_settings_valid-{$component}", true, $settings, $this->feedback ); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Run the import for a component. |
|
166 | - * |
|
167 | - * @since 1.0.0 |
|
168 | - * |
|
169 | - * @param string $component The component to run the import for. |
|
170 | - * @param array $options The selected options of what to import. |
|
171 | - */ |
|
172 | - protected function do_import_for_component( $component, $options ) { |
|
173 | - |
|
174 | - $component_data = WordPoints_Components::instance()->get_component( |
|
175 | - $component |
|
176 | - ); |
|
177 | - |
|
178 | - if ( false === $component_data ) { |
|
179 | - // translators: Component name. |
|
180 | - $this->feedback->warning( sprintf( __( 'Skipping %s component—not installed.', 'wordpoints-importer' ), esc_html( $component ) ) ); |
|
181 | - return; |
|
182 | - } |
|
183 | - |
|
184 | - if ( true !== $this->supports_component( $component ) ) { |
|
185 | - // translators: Component name. |
|
186 | - $this->feedback->warning( sprintf( __( 'Skipping the %s component—not supported.', 'wordpoints-importer' ), $component_data['name'] ) ); |
|
187 | - return; |
|
188 | - } |
|
189 | - |
|
190 | - $settings = array(); |
|
191 | - |
|
192 | - if ( isset( $options['_data'] ) ) { |
|
193 | - $settings = $options['_data']; |
|
194 | - unset( $options['_data'] ); |
|
195 | - } |
|
196 | - |
|
197 | - if ( empty( $options ) || ! $this->validate_import_settings( $component, $settings ) ) { |
|
198 | - return; |
|
199 | - } |
|
200 | - |
|
201 | - // translators: Component name. |
|
202 | - $this->feedback->info( sprintf( __( 'Importing data to the %s component…', 'wordpoints-importer' ), $component_data['name'] ) ); |
|
203 | - |
|
204 | - foreach ( $options as $option => $unused ) { |
|
205 | - $this->do_import_for_option( $option, $component, $settings ); |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Run the import for an option. |
|
211 | - * |
|
212 | - * The import is split up into different options which the user can select (these |
|
213 | - * are displayed to the user as checkboxes in the form). This handles the import |
|
214 | - * for each of the individual things the user has selected to import. These are |
|
215 | - * all optional, so each is just termed an import "option" here. |
|
216 | - * |
|
217 | - * @since 1.0.0 |
|
218 | - * |
|
219 | - * @param string $option An import option that has been selected. |
|
220 | - * @param string $component The component this option is for. |
|
221 | - * @param array $settings Other settings for this component. |
|
222 | - */ |
|
223 | - protected function do_import_for_option( $option, $component, $settings ) { |
|
224 | - |
|
225 | - if ( ! isset( $this->components[ $component ][ $option ] ) ) { |
|
226 | - // translators: Option name. |
|
227 | - $this->feedback->warning( sprintf( __( 'Skipping unrecognized import option “%s”…', 'wordpoints-importer' ), $option ) ); |
|
228 | - return; |
|
229 | - } |
|
230 | - |
|
231 | - $option_data = $this->components[ $component ][ $option ]; |
|
232 | - |
|
233 | - // Check if we can actually run this option. |
|
234 | - if ( isset( $option_data['can_import'] ) ) { |
|
235 | - |
|
236 | - $cant_import = call_user_func( $option_data['can_import'], $settings ); |
|
237 | - |
|
238 | - if ( is_wp_error( $cant_import ) ) { |
|
239 | - // translators: 1. Option name; 2. Reason the import was skipped. |
|
240 | - $this->feedback->warning( sprintf( __( 'Skipping importing %1$s. Reason: %2$s', 'wordpoints-importer' ), $option_data['label'], $cant_import->get_error_message() ) ); |
|
241 | - return; |
|
242 | - } |
|
243 | - } |
|
244 | - |
|
245 | - // OK, we can run the import method for this option. |
|
246 | - call_user_func( $option_data['function'], $settings ); |
|
247 | - } |
|
17 | + /** |
|
18 | + * The name of the importer. |
|
19 | + * |
|
20 | + * @since 1.0.0 |
|
21 | + * |
|
22 | + * @type string $name |
|
23 | + */ |
|
24 | + protected $name; |
|
25 | + |
|
26 | + /** |
|
27 | + * The components supported by this importer. |
|
28 | + * |
|
29 | + * The keys are the component slugs, the values arrays of options for importing |
|
30 | + * to that component. |
|
31 | + * |
|
32 | + * @since 1.0.0 |
|
33 | + * |
|
34 | + * @type array[] $components |
|
35 | + */ |
|
36 | + protected $components = array(); |
|
37 | + |
|
38 | + /** |
|
39 | + * The feedback provider object. |
|
40 | + * |
|
41 | + * This is only set by self::do_import(). |
|
42 | + * |
|
43 | + * @since 1.0.0 |
|
44 | + * |
|
45 | + * @type WordPoints_Importer_Feedback $feedback |
|
46 | + */ |
|
47 | + protected $feedback; |
|
48 | + |
|
49 | + /** |
|
50 | + * Check if this importer is available. |
|
51 | + * |
|
52 | + * @since 1.0.0 |
|
53 | + * |
|
54 | + * @return true|WP_Error A WP_Error if the importer is not available. |
|
55 | + */ |
|
56 | + abstract public function is_available(); |
|
57 | + |
|
58 | + /** |
|
59 | + * Construct the importer. |
|
60 | + * |
|
61 | + * @since 1.0.0 |
|
62 | + * |
|
63 | + * @param string $name The name of the importer. |
|
64 | + */ |
|
65 | + public function __construct( $name ) { |
|
66 | + |
|
67 | + $this->name = $name; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Check if this importer supports a specific component. |
|
72 | + * |
|
73 | + * @since 1.0.0 |
|
74 | + * |
|
75 | + * @param string $component The slug of a component. |
|
76 | + * |
|
77 | + * @return bool True if the component is supported, otherwise false. |
|
78 | + */ |
|
79 | + public function supports_component( $component ) { |
|
80 | + |
|
81 | + return isset( $this->components[ $component ] ); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Get the import options for a component. |
|
86 | + * |
|
87 | + * @since 1.0.0 |
|
88 | + * |
|
89 | + * @param string $component The slug of a component. |
|
90 | + * |
|
91 | + * @return array[] The options for this component. |
|
92 | + */ |
|
93 | + public function get_options_for_component( $component ) { |
|
94 | + |
|
95 | + if ( ! $this->supports_component( $component ) ) { |
|
96 | + return array(); |
|
97 | + } |
|
98 | + |
|
99 | + return $this->components[ $component ]; |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Run the import. |
|
104 | + * |
|
105 | + * @since 1.0.0 |
|
106 | + * |
|
107 | + * @param array $args The settings for the import. |
|
108 | + * @param WordPoints_Importer_Feedback $feedback The feedback object. |
|
109 | + */ |
|
110 | + public function do_import( array $args, $feedback = null ) { |
|
111 | + |
|
112 | + if ( ! ( $feedback instanceof WordPoints_Importer_Feedback ) ) { |
|
113 | + $feedback = new WordPoints_Importer_Feedback(); |
|
114 | + } |
|
115 | + |
|
116 | + $this->feedback = $feedback; |
|
117 | + |
|
118 | + // translators: Plugin name. |
|
119 | + $this->feedback->info( sprintf( __( 'Importing from %s…', 'wordpoints-importer' ), $this->name ) ); |
|
120 | + |
|
121 | + $this->no_interruptions(); |
|
122 | + |
|
123 | + foreach ( $args as $component => $options ) { |
|
124 | + $this->do_import_for_component( $component, $options ); |
|
125 | + } |
|
126 | + |
|
127 | + $this->feedback->info( __( 'Import complete.', 'wordpoints-importer' ) ); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Prevent any interruptions from occurring during the import. |
|
132 | + * |
|
133 | + * @since 1.2.1 |
|
134 | + */ |
|
135 | + protected function no_interruptions() { |
|
136 | + |
|
137 | + wordpoints_prevent_interruptions(); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Validate the import settings for a component. |
|
142 | + * |
|
143 | + * @since 1.0.0 |
|
144 | + * |
|
145 | + * @param string $component The slug of the component. |
|
146 | + * @param array $settings The settings supplied for this component. |
|
147 | + * |
|
148 | + * @return bool Whether the settings are valid. |
|
149 | + */ |
|
150 | + protected function validate_import_settings( $component, $settings ) { |
|
151 | + |
|
152 | + /** |
|
153 | + * Filter whether the settings are valid before importing. |
|
154 | + * |
|
155 | + * @since 1.0.0 |
|
156 | + * |
|
157 | + * @param bool $valid Whether the settings are valid. |
|
158 | + * @param array $settings The settings for this component. |
|
159 | + * @param WordPoints_Importer_Feedback $feedback The feedback object. |
|
160 | + */ |
|
161 | + return apply_filters( "wordpoints_import_settings_valid-{$component}", true, $settings, $this->feedback ); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Run the import for a component. |
|
166 | + * |
|
167 | + * @since 1.0.0 |
|
168 | + * |
|
169 | + * @param string $component The component to run the import for. |
|
170 | + * @param array $options The selected options of what to import. |
|
171 | + */ |
|
172 | + protected function do_import_for_component( $component, $options ) { |
|
173 | + |
|
174 | + $component_data = WordPoints_Components::instance()->get_component( |
|
175 | + $component |
|
176 | + ); |
|
177 | + |
|
178 | + if ( false === $component_data ) { |
|
179 | + // translators: Component name. |
|
180 | + $this->feedback->warning( sprintf( __( 'Skipping %s component—not installed.', 'wordpoints-importer' ), esc_html( $component ) ) ); |
|
181 | + return; |
|
182 | + } |
|
183 | + |
|
184 | + if ( true !== $this->supports_component( $component ) ) { |
|
185 | + // translators: Component name. |
|
186 | + $this->feedback->warning( sprintf( __( 'Skipping the %s component—not supported.', 'wordpoints-importer' ), $component_data['name'] ) ); |
|
187 | + return; |
|
188 | + } |
|
189 | + |
|
190 | + $settings = array(); |
|
191 | + |
|
192 | + if ( isset( $options['_data'] ) ) { |
|
193 | + $settings = $options['_data']; |
|
194 | + unset( $options['_data'] ); |
|
195 | + } |
|
196 | + |
|
197 | + if ( empty( $options ) || ! $this->validate_import_settings( $component, $settings ) ) { |
|
198 | + return; |
|
199 | + } |
|
200 | + |
|
201 | + // translators: Component name. |
|
202 | + $this->feedback->info( sprintf( __( 'Importing data to the %s component…', 'wordpoints-importer' ), $component_data['name'] ) ); |
|
203 | + |
|
204 | + foreach ( $options as $option => $unused ) { |
|
205 | + $this->do_import_for_option( $option, $component, $settings ); |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Run the import for an option. |
|
211 | + * |
|
212 | + * The import is split up into different options which the user can select (these |
|
213 | + * are displayed to the user as checkboxes in the form). This handles the import |
|
214 | + * for each of the individual things the user has selected to import. These are |
|
215 | + * all optional, so each is just termed an import "option" here. |
|
216 | + * |
|
217 | + * @since 1.0.0 |
|
218 | + * |
|
219 | + * @param string $option An import option that has been selected. |
|
220 | + * @param string $component The component this option is for. |
|
221 | + * @param array $settings Other settings for this component. |
|
222 | + */ |
|
223 | + protected function do_import_for_option( $option, $component, $settings ) { |
|
224 | + |
|
225 | + if ( ! isset( $this->components[ $component ][ $option ] ) ) { |
|
226 | + // translators: Option name. |
|
227 | + $this->feedback->warning( sprintf( __( 'Skipping unrecognized import option “%s”…', 'wordpoints-importer' ), $option ) ); |
|
228 | + return; |
|
229 | + } |
|
230 | + |
|
231 | + $option_data = $this->components[ $component ][ $option ]; |
|
232 | + |
|
233 | + // Check if we can actually run this option. |
|
234 | + if ( isset( $option_data['can_import'] ) ) { |
|
235 | + |
|
236 | + $cant_import = call_user_func( $option_data['can_import'], $settings ); |
|
237 | + |
|
238 | + if ( is_wp_error( $cant_import ) ) { |
|
239 | + // translators: 1. Option name; 2. Reason the import was skipped. |
|
240 | + $this->feedback->warning( sprintf( __( 'Skipping importing %1$s. Reason: %2$s', 'wordpoints-importer' ), $option_data['label'], $cant_import->get_error_message() ) ); |
|
241 | + return; |
|
242 | + } |
|
243 | + } |
|
244 | + |
|
245 | + // OK, we can run the import method for this option. |
|
246 | + call_user_func( $option_data['function'], $settings ); |
|
247 | + } |
|
248 | 248 | } |
249 | 249 | |
250 | 250 | // EOF |
@@ -16,84 +16,84 @@ |
||
16 | 16 | */ |
17 | 17 | class WordPoints_Importer_Feedback { |
18 | 18 | |
19 | - /** |
|
20 | - * Send an info message. |
|
21 | - * |
|
22 | - * Use this to give the user any info that wouldn't be handles by the other |
|
23 | - * methods. |
|
24 | - * |
|
25 | - * @since 1.0.0 |
|
26 | - * |
|
27 | - * @param string $message The info message. |
|
28 | - */ |
|
29 | - public function info( $message ) { |
|
30 | - $this->_send( $message, 'info' ); |
|
31 | - } |
|
19 | + /** |
|
20 | + * Send an info message. |
|
21 | + * |
|
22 | + * Use this to give the user any info that wouldn't be handles by the other |
|
23 | + * methods. |
|
24 | + * |
|
25 | + * @since 1.0.0 |
|
26 | + * |
|
27 | + * @param string $message The info message. |
|
28 | + */ |
|
29 | + public function info( $message ) { |
|
30 | + $this->_send( $message, 'info' ); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Send a success message. |
|
35 | - * |
|
36 | - * Use this when you want to let the user know that something was a success. |
|
37 | - * |
|
38 | - * @since 1.0.0 |
|
39 | - * |
|
40 | - * @param string $message The success message. |
|
41 | - */ |
|
42 | - public function success( $message ) { |
|
43 | - $this->_send( $message, 'success' ); |
|
44 | - } |
|
33 | + /** |
|
34 | + * Send a success message. |
|
35 | + * |
|
36 | + * Use this when you want to let the user know that something was a success. |
|
37 | + * |
|
38 | + * @since 1.0.0 |
|
39 | + * |
|
40 | + * @param string $message The success message. |
|
41 | + */ |
|
42 | + public function success( $message ) { |
|
43 | + $this->_send( $message, 'success' ); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Send an error message. |
|
48 | - * |
|
49 | - * Use this when you want to let the user know that there was an error. |
|
50 | - * |
|
51 | - * @since 1.0.0 |
|
52 | - * |
|
53 | - * @param string $message The error message. |
|
54 | - */ |
|
55 | - public function error( $message ) { |
|
56 | - $this->_send( $message, 'error' ); |
|
57 | - } |
|
46 | + /** |
|
47 | + * Send an error message. |
|
48 | + * |
|
49 | + * Use this when you want to let the user know that there was an error. |
|
50 | + * |
|
51 | + * @since 1.0.0 |
|
52 | + * |
|
53 | + * @param string $message The error message. |
|
54 | + */ |
|
55 | + public function error( $message ) { |
|
56 | + $this->_send( $message, 'error' ); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Send a warning message. |
|
61 | - * |
|
62 | - * Use this to warn the user about something. A warning doesn't necessarily imply |
|
63 | - * an error, but that something unexpected has occured that the user might need |
|
64 | - * to know. |
|
65 | - * |
|
66 | - * @since 1.0.0 |
|
67 | - * |
|
68 | - * @param string $message The warning message. |
|
69 | - */ |
|
70 | - public function warning( $message ) { |
|
71 | - $this->_send( $message, 'warning' ); |
|
72 | - } |
|
59 | + /** |
|
60 | + * Send a warning message. |
|
61 | + * |
|
62 | + * Use this to warn the user about something. A warning doesn't necessarily imply |
|
63 | + * an error, but that something unexpected has occured that the user might need |
|
64 | + * to know. |
|
65 | + * |
|
66 | + * @since 1.0.0 |
|
67 | + * |
|
68 | + * @param string $message The warning message. |
|
69 | + */ |
|
70 | + public function warning( $message ) { |
|
71 | + $this->_send( $message, 'warning' ); |
|
72 | + } |
|
73 | 73 | |
74 | - // |
|
75 | - // Protected Methods. |
|
76 | - // |
|
74 | + // |
|
75 | + // Protected Methods. |
|
76 | + // |
|
77 | 77 | |
78 | - /** |
|
79 | - * Send a message to the user. |
|
80 | - * |
|
81 | - * @since 1.0.0 |
|
82 | - * |
|
83 | - * @param string $message The feedback message. |
|
84 | - * @param string $type The type of message: 'info' (defuault), 'sucess', |
|
85 | - * 'error', 'warning'. |
|
86 | - */ |
|
87 | - protected function _send( $message, $type = 'info' ) { // @codingStandardsIgnoreLine |
|
78 | + /** |
|
79 | + * Send a message to the user. |
|
80 | + * |
|
81 | + * @since 1.0.0 |
|
82 | + * |
|
83 | + * @param string $message The feedback message. |
|
84 | + * @param string $type The type of message: 'info' (defuault), 'sucess', |
|
85 | + * 'error', 'warning'. |
|
86 | + */ |
|
87 | + protected function _send( $message, $type = 'info' ) { // @codingStandardsIgnoreLine |
|
88 | 88 | |
89 | - ?> |
|
89 | + ?> |
|
90 | 90 | |
91 | 91 | <p class="wordpoints-feedback wordpoints-feedback-<?php echo esc_attr( $type ); ?>"> |
92 | 92 | <?php echo wp_kses( $message, 'wordpoints_importer_feedback' ); ?> |
93 | 93 | </p> |
94 | 94 | |
95 | 95 | <?php |
96 | - } |
|
96 | + } |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | // EOF |
@@ -15,12 +15,12 @@ discard block |
||
15 | 15 | */ |
16 | 16 | function wordpoints_importer_load_textdomain() { |
17 | 17 | |
18 | - _deprecated_function( __FUNCTION__, '1.3.0' ); |
|
18 | + _deprecated_function( __FUNCTION__, '1.3.0' ); |
|
19 | 19 | |
20 | - wordpoints_load_module_textdomain( |
|
21 | - 'wordpoints-importer' |
|
22 | - , wordpoints_module_basename( dirname( dirname( __FILE__ ) ) ) . '/languages' |
|
23 | - ); |
|
20 | + wordpoints_load_module_textdomain( |
|
21 | + 'wordpoints-importer' |
|
22 | + , wordpoints_module_basename( dirname( dirname( __FILE__ ) ) ) . '/languages' |
|
23 | + ); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | /** |
@@ -30,12 +30,12 @@ discard block |
||
30 | 30 | */ |
31 | 31 | function wordpoints_importer_register_importers() { |
32 | 32 | |
33 | - $args = array( |
|
34 | - 'class' => 'WordPoints_CubePoints_Importer', |
|
35 | - 'name' => __( 'CubePoints', 'wordpoints-importer' ), |
|
36 | - ); |
|
33 | + $args = array( |
|
34 | + 'class' => 'WordPoints_CubePoints_Importer', |
|
35 | + 'name' => __( 'CubePoints', 'wordpoints-importer' ), |
|
36 | + ); |
|
37 | 37 | |
38 | - WordPoints_Importers::register( 'cubepoints', $args ); |
|
38 | + WordPoints_Importers::register( 'cubepoints', $args ); |
|
39 | 39 | } |
40 | 40 | add_action( 'wordpoints_register_importers', 'wordpoints_importer_register_importers' ); |
41 | 41 |
@@ -14,707 +14,707 @@ |
||
14 | 14 | */ |
15 | 15 | class WordPoints_CubePoints_Importer extends WordPoints_Importer { |
16 | 16 | |
17 | - /** |
|
18 | - * Reversible log types indexed by the reversing log type. |
|
19 | - * |
|
20 | - * This information is used to set the `auto_reversed` and `original_log_id` |
|
21 | - * points log metadata for the imported logs. |
|
22 | - * |
|
23 | - * @since 1.2.0 |
|
24 | - * |
|
25 | - * @see WordPoints_CubePoints_Importer::import_points_log() |
|
26 | - * |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $reversible_log_types = array( |
|
30 | - 'comment_remove' => 'comment', |
|
31 | - 'post_comment_remove' => 'post_comment', |
|
32 | - ); |
|
33 | - |
|
34 | - /** |
|
35 | - * Primary entity slugs for each imported log type. |
|
36 | - * |
|
37 | - * These are points log meta keys under which to save the entity ID in the |
|
38 | - * CubePoints `data` log field. |
|
39 | - * |
|
40 | - * @since 1.2.0 |
|
41 | - * |
|
42 | - * @see WordPoints_CubePoints_Importer::import_points_log() |
|
43 | - * |
|
44 | - * @var array |
|
45 | - */ |
|
46 | - protected $log_type_entities = array( |
|
47 | - 'comment' => 'comment', |
|
48 | - 'comment_remove' => 'comment', |
|
49 | - 'post' => 'post', |
|
50 | - 'post_comment' => 'comment', |
|
51 | - 'post_comment_remove' => 'comment', |
|
52 | - 'register' => 'user', |
|
53 | - ); |
|
54 | - |
|
55 | - /** |
|
56 | - * IDs of reversible logs, indexed by CubePoints log type and object ID. |
|
57 | - * |
|
58 | - * @since 1.2.0 |
|
59 | - * |
|
60 | - * @see WordPoints_CubePoints_Importer::import_points_log() |
|
61 | - * |
|
62 | - * @var int[][] |
|
63 | - */ |
|
64 | - protected $reversible_log_ids; |
|
65 | - |
|
66 | - /** |
|
67 | - * @since 1.0.0 |
|
68 | - */ |
|
69 | - public function __construct( $name ) { |
|
70 | - |
|
71 | - parent::__construct( $name ); |
|
72 | - |
|
73 | - $this->components = array( |
|
74 | - 'points' => array( |
|
75 | - 'excluded_users' => array( |
|
76 | - 'label' => __( 'Excluded users', 'wordpoints-importer' ), |
|
77 | - 'function' => array( $this, 'import_excluded_users' ), |
|
78 | - ), |
|
79 | - 'settings' => array( |
|
80 | - 'label' => __( 'Points Reactions', 'wordpoints-importer' ), |
|
81 | - 'description' => __( 'If checked, the settings for the number of points to award for posts, comments, etc. are imported.', 'wordpoints-importer' ), |
|
82 | - 'function' => array( $this, 'import_points_settings' ), |
|
83 | - ), |
|
84 | - 'user_points' => array( |
|
85 | - 'label' => __( 'User points', 'wordpoints-importer' ), |
|
86 | - 'function' => array( $this, 'import_user_points' ), |
|
87 | - ), |
|
88 | - 'logs' => array( |
|
89 | - 'label' => __( 'Points logs', 'wordpoints-importer' ), |
|
90 | - 'function' => array( $this, 'import_points_logs' ), |
|
91 | - 'can_import' => array( $this, 'can_import_points_logs' ), |
|
92 | - ), |
|
93 | - ), |
|
94 | - 'ranks' => array( |
|
95 | - 'ranks' => array( |
|
96 | - 'label' => __( 'Rank settings', 'wordpoints-importer' ), |
|
97 | - 'description' => __( 'If checked, the list of ranks is imported, and users will have the correct ranks assigned to them.', 'wordpoints-importer' ), |
|
98 | - 'function' => array( $this, 'import_ranks' ), |
|
99 | - ), |
|
100 | - ), |
|
101 | - ); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @since 1.0.0 |
|
106 | - */ |
|
107 | - public function is_available() { |
|
108 | - |
|
109 | - if ( ! $this->is_cubepoints_installed() ) { |
|
110 | - return new WP_Error( |
|
111 | - 'cubepoints_not_installed' |
|
112 | - , __( 'CubePoints is not installed', 'wordpoints-importer' ) |
|
113 | - ); |
|
114 | - } |
|
115 | - |
|
116 | - return true; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Check if CubePoints is installed. |
|
121 | - * |
|
122 | - * @since 1.0.0 |
|
123 | - */ |
|
124 | - public function is_cubepoints_installed() { |
|
125 | - |
|
126 | - return (bool) get_option( 'cp_db_version' ); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Check if CubePoints is active. |
|
131 | - * |
|
132 | - * @since 1.0.0 |
|
133 | - */ |
|
134 | - public function is_cubepoints_active() { |
|
135 | - |
|
136 | - return function_exists( 'cp_ready' ); |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Import the excluded users. |
|
141 | - * |
|
142 | - * @since 1.0.0 |
|
143 | - */ |
|
144 | - protected function import_excluded_users() { |
|
145 | - |
|
146 | - $this->feedback->info( __( 'Importing excluded users…', 'wordpoints-importer' ) ); |
|
147 | - |
|
148 | - $excluded_users = get_option( 'cp_topfilter' ); |
|
149 | - |
|
150 | - if ( ! is_array( $excluded_users ) ) { |
|
151 | - $this->feedback->warning( __( 'No excluded users found.', 'wordpoints-importer' ) ); |
|
152 | - return; |
|
153 | - } |
|
154 | - |
|
155 | - $user_ids = array(); |
|
156 | - |
|
157 | - foreach ( $excluded_users as $user_login ) { |
|
158 | - |
|
159 | - $user = get_user_by( 'login', $user_login ); |
|
160 | - |
|
161 | - if ( $user instanceof WP_User ) { |
|
162 | - $user_ids[] = $user->ID; |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - $excluded_user_ids = wordpoints_get_maybe_network_array_option( |
|
167 | - 'wordpoints_excluded_users' |
|
168 | - ); |
|
169 | - |
|
170 | - $excluded_user_ids = array_unique( |
|
171 | - array_merge( $excluded_user_ids, $user_ids ) |
|
172 | - ); |
|
173 | - |
|
174 | - wordpoints_update_maybe_network_option( |
|
175 | - 'wordpoints_excluded_users' |
|
176 | - , $excluded_user_ids |
|
177 | - ); |
|
178 | - |
|
179 | - $this->feedback->success( |
|
180 | - sprintf( |
|
181 | - // translators: Number of users. |
|
182 | - __( 'Imported %s excluded users.', 'wordpoints-importer' ) |
|
183 | - , count( $user_ids ) |
|
184 | - ) |
|
185 | - ); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Import the points settings to the points hooks. |
|
190 | - * |
|
191 | - * @since 1.1.0 |
|
192 | - * |
|
193 | - * @param array $settings The settings for the points component import. |
|
194 | - */ |
|
195 | - protected function import_points_settings( $settings ) { |
|
196 | - |
|
197 | - $this->feedback->info( __( 'Importing points reactions…', 'wordpoints-importer' ) ); |
|
198 | - |
|
199 | - $options = array( |
|
200 | - 'cp_comment_points' => array( |
|
201 | - 'event' => 'comment_leave\post', |
|
202 | - 'target' => array( 'comment\post', 'author', 'user' ), |
|
203 | - /* translators: The post type name */ |
|
204 | - 'log_text' => __( 'Comment on a %s.', 'wordpoints-importer' ), |
|
205 | - /* translators: The post type name */ |
|
206 | - 'description' => __( 'Commenting on a %s.', 'wordpoints-importer' ), |
|
207 | - 'legacy_log_type' => 'cubepoints-comment', |
|
208 | - 'legacy_meta_key' => 'comment', |
|
209 | - ), |
|
210 | - 'cp_post_points' => array( |
|
211 | - 'event' => 'post_publish\post', |
|
212 | - 'target' => array( 'post\post', 'author', 'user' ), |
|
213 | - /* translators: The post type name */ |
|
214 | - 'log_text' => __( 'Published a Post.', 'wordpoints-importer' ), |
|
215 | - /* translators: The post type name */ |
|
216 | - 'description' => __( 'Publishing a Post.', 'wordpoints-importer' ), |
|
217 | - 'legacy_log_type' => 'cubepoints-post', |
|
218 | - 'legacy_meta_key' => 'post', |
|
219 | - // CubePoints doesn't remove points when a post is deleted. |
|
220 | - 'blocker' => array( 'toggle_off' => true ), |
|
221 | - 'points_legacy_repeat_blocker' => array( 'toggle_on' => true ), |
|
222 | - ), |
|
223 | - 'cp_reg_points' => array( |
|
224 | - 'event' => 'user_register', |
|
225 | - 'log_text' => __( 'Registration.', 'wordpoints-importer' ), |
|
226 | - 'description' => __( 'Registration.', 'wordpoints-importer' ), |
|
227 | - 'legacy_log_type' => 'cubepoints-register', |
|
228 | - ), |
|
229 | - 'cp_post_author_points' => array( |
|
230 | - 'event' => 'comment_leave\post', |
|
231 | - 'target' => array( 'comment\post', 'post\post', 'post\post', 'author', 'user' ), |
|
232 | - /* translators: The post type name */ |
|
233 | - 'log_text' => __( 'Received a comment on a %s.', 'wordpoints-importer' ), |
|
234 | - /* translators: The post type name */ |
|
235 | - 'description' => __( 'Receiving a comment on a %s.', 'wordpoints-importer' ), |
|
236 | - 'legacy_log_type' => 'cubepoints-post_author', |
|
237 | - 'legacy_meta_key' => 'comment', |
|
238 | - ), |
|
239 | - ); |
|
240 | - |
|
241 | - // Don't import this module setting if the module isn't active. |
|
242 | - if ( function_exists( 'cp_module_activated' ) && ! cp_module_activated( 'post_author_points' ) ) { |
|
243 | - unset( $options['cp_post_author_points'] ); |
|
244 | - } |
|
245 | - |
|
246 | - $imported = 0; |
|
247 | - |
|
248 | - foreach ( $options as $option => $hook_settings ) { |
|
249 | - |
|
250 | - $points = get_option( $option ); |
|
251 | - |
|
252 | - if ( wordpoints_posint( $points ) ) { |
|
253 | - |
|
254 | - $hook_settings['points'] = $points; |
|
255 | - $hook_settings['points_type'] = $settings['points_type']; |
|
256 | - |
|
257 | - if ( |
|
258 | - // The CubePoints post points were only awarded for Posts. |
|
259 | - 'post_publish\post' !== $hook_settings['event'] |
|
260 | - && strpos( $hook_settings['event'], '\post' ) |
|
261 | - ) { |
|
262 | - |
|
263 | - $post_type_slugs = get_post_types( array( 'public' => true ) ); |
|
264 | - |
|
265 | - foreach ( $post_type_slugs as $post_type_slug ) { |
|
266 | - |
|
267 | - $added = $this->add_points_hook( |
|
268 | - $this->format_settings_for_post_type( |
|
269 | - $post_type_slug |
|
270 | - , $hook_settings |
|
271 | - ) |
|
272 | - ); |
|
273 | - |
|
274 | - if ( $added ) { |
|
275 | - $imported++; |
|
276 | - } |
|
277 | - } |
|
278 | - |
|
279 | - } else { |
|
280 | - if ( $this->add_points_hook( $hook_settings ) ) { |
|
281 | - $imported++; |
|
282 | - } |
|
283 | - } |
|
284 | - } |
|
285 | - |
|
286 | - } // End foreach ( $options ). |
|
287 | - |
|
288 | - if ( $this->import_daily_points_hook( $settings ) ) { |
|
289 | - $imported++; |
|
290 | - } |
|
291 | - |
|
292 | - $this->feedback->success( |
|
293 | - // translators: Number of reactions. |
|
294 | - sprintf( __( 'Imported %s points reactions.', 'wordpoints-importer' ), $imported ) |
|
295 | - ); |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Import the settings for the Daily Points module to a points hook. |
|
300 | - * |
|
301 | - * @since 1.1.0 |
|
302 | - * |
|
303 | - * @param array $settings The settings for the points component import. |
|
304 | - * |
|
305 | - * @return bool True if the settings were imported, false otherwise. |
|
306 | - */ |
|
307 | - protected function import_daily_points_hook( $settings ) { |
|
308 | - |
|
309 | - // Don't import this module setting if the module isn't active. |
|
310 | - if ( function_exists( 'cp_module_activated' ) && ! cp_module_activated( 'dailypoints' ) ) { |
|
311 | - return false; |
|
312 | - } |
|
313 | - |
|
314 | - $points = get_option( 'cp_module_dailypoints_points' ); |
|
315 | - $period = get_option( 'cp_module_dailypoints_time' ); |
|
316 | - |
|
317 | - if ( ! wordpoints_int( $points ) || ! wordpoints_posint( $period ) ) { |
|
318 | - return false; |
|
319 | - } |
|
320 | - |
|
321 | - return $this->add_points_hook( |
|
322 | - array( |
|
323 | - 'event' => 'user_visit', |
|
324 | - 'target' => array( 'current:user' ), |
|
325 | - 'reactor' => 'points_legacy', |
|
326 | - 'points' => $points, |
|
327 | - 'points_type' => $settings['points_type'], |
|
328 | - 'points_legacy_periods' => array( |
|
329 | - 'fire' => array( |
|
330 | - array( |
|
331 | - 'length' => $period, |
|
332 | - 'args' => array( array( 'current:user' ) ), |
|
333 | - 'relative' => true, |
|
334 | - ), |
|
335 | - ), |
|
336 | - ), |
|
337 | - 'log_text' => __( 'Visiting the site.', 'wordpoints-importer' ), |
|
338 | - 'description' => __( 'Visiting the site.', 'wordpoints-importer' ), |
|
339 | - 'points_legacy_reversals' => array(), |
|
340 | - 'legacy_log_type' => 'cubepoints-dailypoints', |
|
341 | - ) |
|
342 | - ); |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Programmatically create a new instance of a points hook. |
|
347 | - * |
|
348 | - * @since 1.0.0 |
|
349 | - * @since 1.2.0 Now just accepts a single argument, $settings. |
|
350 | - * |
|
351 | - * @param array $settings The settings for this hook. |
|
352 | - * |
|
353 | - * @return bool True if added successfully, or false on failure. |
|
354 | - */ |
|
355 | - private function add_points_hook( $settings = array() ) { |
|
356 | - |
|
357 | - $reaction_store = wordpoints_hooks()->get_reaction_store( 'points' ); |
|
358 | - |
|
359 | - $settings = array_merge( |
|
360 | - array( |
|
361 | - 'target' => array( 'user' ), |
|
362 | - 'reactor' => 'points_legacy', |
|
363 | - 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
364 | - ) |
|
365 | - , $settings |
|
366 | - ); |
|
367 | - |
|
368 | - $reaction = $reaction_store->create_reaction( $settings ); |
|
369 | - |
|
370 | - if ( ! $reaction instanceof WordPoints_Hook_ReactionI ) { |
|
371 | - return false; |
|
372 | - } |
|
373 | - |
|
374 | - return true; |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * Format the settings for a reaction for a particular post type. |
|
379 | - * |
|
380 | - * @since 1.2.0 |
|
381 | - * |
|
382 | - * @param string $post_type The slug of the post type to format the settings for. |
|
383 | - * @param array $settings The reaction settings. |
|
384 | - * |
|
385 | - * @return array The settings modified for this particular post type. |
|
386 | - */ |
|
387 | - protected function format_settings_for_post_type( $post_type, $settings ) { |
|
388 | - |
|
389 | - $settings['event'] = str_replace( |
|
390 | - '\post' |
|
391 | - , '\\' . $post_type |
|
392 | - , $settings['event'] |
|
393 | - ); |
|
394 | - |
|
395 | - $settings['target'] = str_replace( |
|
396 | - '\post' |
|
397 | - , '\\' . $post_type |
|
398 | - , $settings['target'] |
|
399 | - ); |
|
400 | - |
|
401 | - $labels = get_post_type_labels( get_post_type_object( $post_type ) ); |
|
402 | - |
|
403 | - $settings['log_text'] = sprintf( $settings['log_text'], $labels->singular_name ); |
|
404 | - $settings['description'] = sprintf( $settings['description'], $labels->singular_name ); |
|
405 | - |
|
406 | - return $settings; |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * Import the user points. |
|
411 | - * |
|
412 | - * @since 1.0.0 |
|
413 | - * |
|
414 | - * @param array $settings The settings for the points component import. |
|
415 | - */ |
|
416 | - protected function import_user_points( $settings ) { |
|
417 | - |
|
418 | - $this->feedback->info( __( 'Importing users' points…', 'wordpoints-importer' ) ); |
|
419 | - |
|
420 | - // We don't log the import transactions. |
|
421 | - add_filter( 'wordpoints_points_log', '__return_false' ); |
|
422 | - |
|
423 | - $start = 0; |
|
424 | - $rows = $this->get_next_user_points_batch( $start ); |
|
425 | - |
|
426 | - // We do the import in batches. |
|
427 | - while ( $rows ) { |
|
428 | - |
|
429 | - foreach ( $rows as $row ) { |
|
430 | - |
|
431 | - wordpoints_alter_points( |
|
432 | - $row->user_id |
|
433 | - , $row->points |
|
434 | - , $settings['points_type'] |
|
435 | - , 'cubepoints_import' // This is only for the hooks, it isn't being logged. |
|
436 | - ); |
|
437 | - } |
|
438 | - |
|
439 | - $start += count( $rows ); |
|
440 | - $rows = $this->get_next_user_points_batch( $start ); |
|
441 | - } |
|
442 | - |
|
443 | - remove_filter( 'wordpoints_points_log', '__return_false' ); |
|
444 | - |
|
445 | - // translators: Number of users. |
|
446 | - $this->feedback->success( sprintf( __( 'Imported points for %s users…', 'wordpoints-importer' ), $start ) ); |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * Get a batch of user points to import. |
|
451 | - * |
|
452 | - * @since 1.0.0 |
|
453 | - * |
|
454 | - * @param int $start The offset number to begin counting at. |
|
455 | - * |
|
456 | - * @return object[]|false The rows, or false. |
|
457 | - */ |
|
458 | - protected function get_next_user_points_batch( $start ) { |
|
459 | - |
|
460 | - global $wpdb; |
|
461 | - |
|
462 | - $rows = $wpdb->get_results( |
|
463 | - $wpdb->prepare( |
|
464 | - " |
|
17 | + /** |
|
18 | + * Reversible log types indexed by the reversing log type. |
|
19 | + * |
|
20 | + * This information is used to set the `auto_reversed` and `original_log_id` |
|
21 | + * points log metadata for the imported logs. |
|
22 | + * |
|
23 | + * @since 1.2.0 |
|
24 | + * |
|
25 | + * @see WordPoints_CubePoints_Importer::import_points_log() |
|
26 | + * |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $reversible_log_types = array( |
|
30 | + 'comment_remove' => 'comment', |
|
31 | + 'post_comment_remove' => 'post_comment', |
|
32 | + ); |
|
33 | + |
|
34 | + /** |
|
35 | + * Primary entity slugs for each imported log type. |
|
36 | + * |
|
37 | + * These are points log meta keys under which to save the entity ID in the |
|
38 | + * CubePoints `data` log field. |
|
39 | + * |
|
40 | + * @since 1.2.0 |
|
41 | + * |
|
42 | + * @see WordPoints_CubePoints_Importer::import_points_log() |
|
43 | + * |
|
44 | + * @var array |
|
45 | + */ |
|
46 | + protected $log_type_entities = array( |
|
47 | + 'comment' => 'comment', |
|
48 | + 'comment_remove' => 'comment', |
|
49 | + 'post' => 'post', |
|
50 | + 'post_comment' => 'comment', |
|
51 | + 'post_comment_remove' => 'comment', |
|
52 | + 'register' => 'user', |
|
53 | + ); |
|
54 | + |
|
55 | + /** |
|
56 | + * IDs of reversible logs, indexed by CubePoints log type and object ID. |
|
57 | + * |
|
58 | + * @since 1.2.0 |
|
59 | + * |
|
60 | + * @see WordPoints_CubePoints_Importer::import_points_log() |
|
61 | + * |
|
62 | + * @var int[][] |
|
63 | + */ |
|
64 | + protected $reversible_log_ids; |
|
65 | + |
|
66 | + /** |
|
67 | + * @since 1.0.0 |
|
68 | + */ |
|
69 | + public function __construct( $name ) { |
|
70 | + |
|
71 | + parent::__construct( $name ); |
|
72 | + |
|
73 | + $this->components = array( |
|
74 | + 'points' => array( |
|
75 | + 'excluded_users' => array( |
|
76 | + 'label' => __( 'Excluded users', 'wordpoints-importer' ), |
|
77 | + 'function' => array( $this, 'import_excluded_users' ), |
|
78 | + ), |
|
79 | + 'settings' => array( |
|
80 | + 'label' => __( 'Points Reactions', 'wordpoints-importer' ), |
|
81 | + 'description' => __( 'If checked, the settings for the number of points to award for posts, comments, etc. are imported.', 'wordpoints-importer' ), |
|
82 | + 'function' => array( $this, 'import_points_settings' ), |
|
83 | + ), |
|
84 | + 'user_points' => array( |
|
85 | + 'label' => __( 'User points', 'wordpoints-importer' ), |
|
86 | + 'function' => array( $this, 'import_user_points' ), |
|
87 | + ), |
|
88 | + 'logs' => array( |
|
89 | + 'label' => __( 'Points logs', 'wordpoints-importer' ), |
|
90 | + 'function' => array( $this, 'import_points_logs' ), |
|
91 | + 'can_import' => array( $this, 'can_import_points_logs' ), |
|
92 | + ), |
|
93 | + ), |
|
94 | + 'ranks' => array( |
|
95 | + 'ranks' => array( |
|
96 | + 'label' => __( 'Rank settings', 'wordpoints-importer' ), |
|
97 | + 'description' => __( 'If checked, the list of ranks is imported, and users will have the correct ranks assigned to them.', 'wordpoints-importer' ), |
|
98 | + 'function' => array( $this, 'import_ranks' ), |
|
99 | + ), |
|
100 | + ), |
|
101 | + ); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @since 1.0.0 |
|
106 | + */ |
|
107 | + public function is_available() { |
|
108 | + |
|
109 | + if ( ! $this->is_cubepoints_installed() ) { |
|
110 | + return new WP_Error( |
|
111 | + 'cubepoints_not_installed' |
|
112 | + , __( 'CubePoints is not installed', 'wordpoints-importer' ) |
|
113 | + ); |
|
114 | + } |
|
115 | + |
|
116 | + return true; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Check if CubePoints is installed. |
|
121 | + * |
|
122 | + * @since 1.0.0 |
|
123 | + */ |
|
124 | + public function is_cubepoints_installed() { |
|
125 | + |
|
126 | + return (bool) get_option( 'cp_db_version' ); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Check if CubePoints is active. |
|
131 | + * |
|
132 | + * @since 1.0.0 |
|
133 | + */ |
|
134 | + public function is_cubepoints_active() { |
|
135 | + |
|
136 | + return function_exists( 'cp_ready' ); |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Import the excluded users. |
|
141 | + * |
|
142 | + * @since 1.0.0 |
|
143 | + */ |
|
144 | + protected function import_excluded_users() { |
|
145 | + |
|
146 | + $this->feedback->info( __( 'Importing excluded users…', 'wordpoints-importer' ) ); |
|
147 | + |
|
148 | + $excluded_users = get_option( 'cp_topfilter' ); |
|
149 | + |
|
150 | + if ( ! is_array( $excluded_users ) ) { |
|
151 | + $this->feedback->warning( __( 'No excluded users found.', 'wordpoints-importer' ) ); |
|
152 | + return; |
|
153 | + } |
|
154 | + |
|
155 | + $user_ids = array(); |
|
156 | + |
|
157 | + foreach ( $excluded_users as $user_login ) { |
|
158 | + |
|
159 | + $user = get_user_by( 'login', $user_login ); |
|
160 | + |
|
161 | + if ( $user instanceof WP_User ) { |
|
162 | + $user_ids[] = $user->ID; |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + $excluded_user_ids = wordpoints_get_maybe_network_array_option( |
|
167 | + 'wordpoints_excluded_users' |
|
168 | + ); |
|
169 | + |
|
170 | + $excluded_user_ids = array_unique( |
|
171 | + array_merge( $excluded_user_ids, $user_ids ) |
|
172 | + ); |
|
173 | + |
|
174 | + wordpoints_update_maybe_network_option( |
|
175 | + 'wordpoints_excluded_users' |
|
176 | + , $excluded_user_ids |
|
177 | + ); |
|
178 | + |
|
179 | + $this->feedback->success( |
|
180 | + sprintf( |
|
181 | + // translators: Number of users. |
|
182 | + __( 'Imported %s excluded users.', 'wordpoints-importer' ) |
|
183 | + , count( $user_ids ) |
|
184 | + ) |
|
185 | + ); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Import the points settings to the points hooks. |
|
190 | + * |
|
191 | + * @since 1.1.0 |
|
192 | + * |
|
193 | + * @param array $settings The settings for the points component import. |
|
194 | + */ |
|
195 | + protected function import_points_settings( $settings ) { |
|
196 | + |
|
197 | + $this->feedback->info( __( 'Importing points reactions…', 'wordpoints-importer' ) ); |
|
198 | + |
|
199 | + $options = array( |
|
200 | + 'cp_comment_points' => array( |
|
201 | + 'event' => 'comment_leave\post', |
|
202 | + 'target' => array( 'comment\post', 'author', 'user' ), |
|
203 | + /* translators: The post type name */ |
|
204 | + 'log_text' => __( 'Comment on a %s.', 'wordpoints-importer' ), |
|
205 | + /* translators: The post type name */ |
|
206 | + 'description' => __( 'Commenting on a %s.', 'wordpoints-importer' ), |
|
207 | + 'legacy_log_type' => 'cubepoints-comment', |
|
208 | + 'legacy_meta_key' => 'comment', |
|
209 | + ), |
|
210 | + 'cp_post_points' => array( |
|
211 | + 'event' => 'post_publish\post', |
|
212 | + 'target' => array( 'post\post', 'author', 'user' ), |
|
213 | + /* translators: The post type name */ |
|
214 | + 'log_text' => __( 'Published a Post.', 'wordpoints-importer' ), |
|
215 | + /* translators: The post type name */ |
|
216 | + 'description' => __( 'Publishing a Post.', 'wordpoints-importer' ), |
|
217 | + 'legacy_log_type' => 'cubepoints-post', |
|
218 | + 'legacy_meta_key' => 'post', |
|
219 | + // CubePoints doesn't remove points when a post is deleted. |
|
220 | + 'blocker' => array( 'toggle_off' => true ), |
|
221 | + 'points_legacy_repeat_blocker' => array( 'toggle_on' => true ), |
|
222 | + ), |
|
223 | + 'cp_reg_points' => array( |
|
224 | + 'event' => 'user_register', |
|
225 | + 'log_text' => __( 'Registration.', 'wordpoints-importer' ), |
|
226 | + 'description' => __( 'Registration.', 'wordpoints-importer' ), |
|
227 | + 'legacy_log_type' => 'cubepoints-register', |
|
228 | + ), |
|
229 | + 'cp_post_author_points' => array( |
|
230 | + 'event' => 'comment_leave\post', |
|
231 | + 'target' => array( 'comment\post', 'post\post', 'post\post', 'author', 'user' ), |
|
232 | + /* translators: The post type name */ |
|
233 | + 'log_text' => __( 'Received a comment on a %s.', 'wordpoints-importer' ), |
|
234 | + /* translators: The post type name */ |
|
235 | + 'description' => __( 'Receiving a comment on a %s.', 'wordpoints-importer' ), |
|
236 | + 'legacy_log_type' => 'cubepoints-post_author', |
|
237 | + 'legacy_meta_key' => 'comment', |
|
238 | + ), |
|
239 | + ); |
|
240 | + |
|
241 | + // Don't import this module setting if the module isn't active. |
|
242 | + if ( function_exists( 'cp_module_activated' ) && ! cp_module_activated( 'post_author_points' ) ) { |
|
243 | + unset( $options['cp_post_author_points'] ); |
|
244 | + } |
|
245 | + |
|
246 | + $imported = 0; |
|
247 | + |
|
248 | + foreach ( $options as $option => $hook_settings ) { |
|
249 | + |
|
250 | + $points = get_option( $option ); |
|
251 | + |
|
252 | + if ( wordpoints_posint( $points ) ) { |
|
253 | + |
|
254 | + $hook_settings['points'] = $points; |
|
255 | + $hook_settings['points_type'] = $settings['points_type']; |
|
256 | + |
|
257 | + if ( |
|
258 | + // The CubePoints post points were only awarded for Posts. |
|
259 | + 'post_publish\post' !== $hook_settings['event'] |
|
260 | + && strpos( $hook_settings['event'], '\post' ) |
|
261 | + ) { |
|
262 | + |
|
263 | + $post_type_slugs = get_post_types( array( 'public' => true ) ); |
|
264 | + |
|
265 | + foreach ( $post_type_slugs as $post_type_slug ) { |
|
266 | + |
|
267 | + $added = $this->add_points_hook( |
|
268 | + $this->format_settings_for_post_type( |
|
269 | + $post_type_slug |
|
270 | + , $hook_settings |
|
271 | + ) |
|
272 | + ); |
|
273 | + |
|
274 | + if ( $added ) { |
|
275 | + $imported++; |
|
276 | + } |
|
277 | + } |
|
278 | + |
|
279 | + } else { |
|
280 | + if ( $this->add_points_hook( $hook_settings ) ) { |
|
281 | + $imported++; |
|
282 | + } |
|
283 | + } |
|
284 | + } |
|
285 | + |
|
286 | + } // End foreach ( $options ). |
|
287 | + |
|
288 | + if ( $this->import_daily_points_hook( $settings ) ) { |
|
289 | + $imported++; |
|
290 | + } |
|
291 | + |
|
292 | + $this->feedback->success( |
|
293 | + // translators: Number of reactions. |
|
294 | + sprintf( __( 'Imported %s points reactions.', 'wordpoints-importer' ), $imported ) |
|
295 | + ); |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Import the settings for the Daily Points module to a points hook. |
|
300 | + * |
|
301 | + * @since 1.1.0 |
|
302 | + * |
|
303 | + * @param array $settings The settings for the points component import. |
|
304 | + * |
|
305 | + * @return bool True if the settings were imported, false otherwise. |
|
306 | + */ |
|
307 | + protected function import_daily_points_hook( $settings ) { |
|
308 | + |
|
309 | + // Don't import this module setting if the module isn't active. |
|
310 | + if ( function_exists( 'cp_module_activated' ) && ! cp_module_activated( 'dailypoints' ) ) { |
|
311 | + return false; |
|
312 | + } |
|
313 | + |
|
314 | + $points = get_option( 'cp_module_dailypoints_points' ); |
|
315 | + $period = get_option( 'cp_module_dailypoints_time' ); |
|
316 | + |
|
317 | + if ( ! wordpoints_int( $points ) || ! wordpoints_posint( $period ) ) { |
|
318 | + return false; |
|
319 | + } |
|
320 | + |
|
321 | + return $this->add_points_hook( |
|
322 | + array( |
|
323 | + 'event' => 'user_visit', |
|
324 | + 'target' => array( 'current:user' ), |
|
325 | + 'reactor' => 'points_legacy', |
|
326 | + 'points' => $points, |
|
327 | + 'points_type' => $settings['points_type'], |
|
328 | + 'points_legacy_periods' => array( |
|
329 | + 'fire' => array( |
|
330 | + array( |
|
331 | + 'length' => $period, |
|
332 | + 'args' => array( array( 'current:user' ) ), |
|
333 | + 'relative' => true, |
|
334 | + ), |
|
335 | + ), |
|
336 | + ), |
|
337 | + 'log_text' => __( 'Visiting the site.', 'wordpoints-importer' ), |
|
338 | + 'description' => __( 'Visiting the site.', 'wordpoints-importer' ), |
|
339 | + 'points_legacy_reversals' => array(), |
|
340 | + 'legacy_log_type' => 'cubepoints-dailypoints', |
|
341 | + ) |
|
342 | + ); |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Programmatically create a new instance of a points hook. |
|
347 | + * |
|
348 | + * @since 1.0.0 |
|
349 | + * @since 1.2.0 Now just accepts a single argument, $settings. |
|
350 | + * |
|
351 | + * @param array $settings The settings for this hook. |
|
352 | + * |
|
353 | + * @return bool True if added successfully, or false on failure. |
|
354 | + */ |
|
355 | + private function add_points_hook( $settings = array() ) { |
|
356 | + |
|
357 | + $reaction_store = wordpoints_hooks()->get_reaction_store( 'points' ); |
|
358 | + |
|
359 | + $settings = array_merge( |
|
360 | + array( |
|
361 | + 'target' => array( 'user' ), |
|
362 | + 'reactor' => 'points_legacy', |
|
363 | + 'points_legacy_reversals' => array( 'toggle_off' => 'toggle_on' ), |
|
364 | + ) |
|
365 | + , $settings |
|
366 | + ); |
|
367 | + |
|
368 | + $reaction = $reaction_store->create_reaction( $settings ); |
|
369 | + |
|
370 | + if ( ! $reaction instanceof WordPoints_Hook_ReactionI ) { |
|
371 | + return false; |
|
372 | + } |
|
373 | + |
|
374 | + return true; |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * Format the settings for a reaction for a particular post type. |
|
379 | + * |
|
380 | + * @since 1.2.0 |
|
381 | + * |
|
382 | + * @param string $post_type The slug of the post type to format the settings for. |
|
383 | + * @param array $settings The reaction settings. |
|
384 | + * |
|
385 | + * @return array The settings modified for this particular post type. |
|
386 | + */ |
|
387 | + protected function format_settings_for_post_type( $post_type, $settings ) { |
|
388 | + |
|
389 | + $settings['event'] = str_replace( |
|
390 | + '\post' |
|
391 | + , '\\' . $post_type |
|
392 | + , $settings['event'] |
|
393 | + ); |
|
394 | + |
|
395 | + $settings['target'] = str_replace( |
|
396 | + '\post' |
|
397 | + , '\\' . $post_type |
|
398 | + , $settings['target'] |
|
399 | + ); |
|
400 | + |
|
401 | + $labels = get_post_type_labels( get_post_type_object( $post_type ) ); |
|
402 | + |
|
403 | + $settings['log_text'] = sprintf( $settings['log_text'], $labels->singular_name ); |
|
404 | + $settings['description'] = sprintf( $settings['description'], $labels->singular_name ); |
|
405 | + |
|
406 | + return $settings; |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * Import the user points. |
|
411 | + * |
|
412 | + * @since 1.0.0 |
|
413 | + * |
|
414 | + * @param array $settings The settings for the points component import. |
|
415 | + */ |
|
416 | + protected function import_user_points( $settings ) { |
|
417 | + |
|
418 | + $this->feedback->info( __( 'Importing users' points…', 'wordpoints-importer' ) ); |
|
419 | + |
|
420 | + // We don't log the import transactions. |
|
421 | + add_filter( 'wordpoints_points_log', '__return_false' ); |
|
422 | + |
|
423 | + $start = 0; |
|
424 | + $rows = $this->get_next_user_points_batch( $start ); |
|
425 | + |
|
426 | + // We do the import in batches. |
|
427 | + while ( $rows ) { |
|
428 | + |
|
429 | + foreach ( $rows as $row ) { |
|
430 | + |
|
431 | + wordpoints_alter_points( |
|
432 | + $row->user_id |
|
433 | + , $row->points |
|
434 | + , $settings['points_type'] |
|
435 | + , 'cubepoints_import' // This is only for the hooks, it isn't being logged. |
|
436 | + ); |
|
437 | + } |
|
438 | + |
|
439 | + $start += count( $rows ); |
|
440 | + $rows = $this->get_next_user_points_batch( $start ); |
|
441 | + } |
|
442 | + |
|
443 | + remove_filter( 'wordpoints_points_log', '__return_false' ); |
|
444 | + |
|
445 | + // translators: Number of users. |
|
446 | + $this->feedback->success( sprintf( __( 'Imported points for %s users…', 'wordpoints-importer' ), $start ) ); |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * Get a batch of user points to import. |
|
451 | + * |
|
452 | + * @since 1.0.0 |
|
453 | + * |
|
454 | + * @param int $start The offset number to begin counting at. |
|
455 | + * |
|
456 | + * @return object[]|false The rows, or false. |
|
457 | + */ |
|
458 | + protected function get_next_user_points_batch( $start ) { |
|
459 | + |
|
460 | + global $wpdb; |
|
461 | + |
|
462 | + $rows = $wpdb->get_results( |
|
463 | + $wpdb->prepare( |
|
464 | + " |
|
465 | 465 | SELECT `user_id`, `meta_value` as points |
466 | 466 | FROM {$wpdb->usermeta} |
467 | 467 | WHERE `meta_key` = 'cpoints' |
468 | 468 | ORDER BY `umeta_id` |
469 | 469 | LIMIT %d,500 |
470 | 470 | " |
471 | - , $start |
|
472 | - ) |
|
473 | - ); // WPCS: cache OK. |
|
474 | - |
|
475 | - if ( ! is_array( $rows ) ) { |
|
476 | - return false; |
|
477 | - } |
|
478 | - |
|
479 | - return $rows; |
|
480 | - } |
|
481 | - |
|
482 | - /** |
|
483 | - * Check if the points logs can be imported. |
|
484 | - * |
|
485 | - * @since 1.0.0 |
|
486 | - * |
|
487 | - * @return true|WP_Error True on success or a WP_Error on failure. |
|
488 | - */ |
|
489 | - public function can_import_points_logs() { |
|
490 | - |
|
491 | - if ( ! $this->is_cubepoints_active() ) { |
|
492 | - |
|
493 | - return new WP_Error( |
|
494 | - 'wordpoints_import_points_logs_cubepoints_inactive' |
|
495 | - , __( 'CubePoints must be active.', 'wordpoints-importer' ) |
|
496 | - ); |
|
497 | - } |
|
498 | - |
|
499 | - return true; |
|
500 | - } |
|
501 | - |
|
502 | - /** |
|
503 | - * Import the points logs. |
|
504 | - * |
|
505 | - * @since 1.0.0 |
|
506 | - * |
|
507 | - * @param array $settings The import settings for the points component. |
|
508 | - */ |
|
509 | - protected function import_points_logs( $settings ) { |
|
510 | - |
|
511 | - $this->feedback->info( __( 'Importing points logs…', 'wordpoints-importer' ) ); |
|
512 | - |
|
513 | - $start = 0; |
|
514 | - $logs = $this->get_next_points_logs_batch( $start ); |
|
515 | - |
|
516 | - while ( $logs ) { |
|
517 | - |
|
518 | - foreach ( $logs as $log ) { |
|
519 | - |
|
520 | - $this->import_points_log( |
|
521 | - $log->uid |
|
522 | - , $log->points |
|
523 | - , $settings['points_type'] |
|
524 | - , "cubepoints-{$log->type}" |
|
525 | - , array( |
|
526 | - 'cubepoints_type' => $log->type, |
|
527 | - 'cubepoints_data' => $log->data, |
|
528 | - ) |
|
529 | - , date( 'Y-m-d H:i:s', $log->timestamp ) |
|
530 | - ); |
|
531 | - } |
|
532 | - |
|
533 | - $start += count( $logs ); |
|
534 | - $logs = $this->get_next_points_logs_batch( $start ); |
|
535 | - } |
|
536 | - |
|
537 | - // translators: Number of points logs. |
|
538 | - $this->feedback->success( sprintf( __( 'Imported %s points log entries.', 'wordpoints-importer' ), $start ) ); |
|
539 | - } |
|
540 | - |
|
541 | - /** |
|
542 | - * Import a points log. |
|
543 | - * |
|
544 | - * @since 1.0.0 |
|
545 | - * |
|
546 | - * @param int $user_id The ID of the user the log is for. |
|
547 | - * @param int $points The number of points awarded. |
|
548 | - * @param string $points_type The points type. |
|
549 | - * @param string $log_type The log type. |
|
550 | - * @param array $meta The metadata for this log. |
|
551 | - * @param int $date The date the transaction took place. |
|
552 | - */ |
|
553 | - protected function import_points_log( $user_id, $points, $points_type, $log_type, $meta, $date ) { |
|
554 | - |
|
555 | - global $wpdb; |
|
556 | - |
|
557 | - $result = $wpdb->insert( |
|
558 | - $wpdb->wordpoints_points_logs, |
|
559 | - array( |
|
560 | - 'user_id' => $user_id, |
|
561 | - 'points' => $points, |
|
562 | - 'points_type' => $points_type, |
|
563 | - 'log_type' => $log_type, |
|
564 | - 'text' => $this->render_points_log_text( '', $user_id, $points, $points_type, $log_type, $meta ), |
|
565 | - 'date' => $date, |
|
566 | - 'site_id' => $wpdb->siteid, |
|
567 | - 'blog_id' => $wpdb->blogid, |
|
568 | - ), |
|
569 | - array( '%d', '%d', '%s', '%s', '%s', '%s', '%d', '%d' ) |
|
570 | - ); |
|
571 | - |
|
572 | - if ( false !== $result ) { |
|
573 | - |
|
574 | - $log_id = (int) $wpdb->insert_id; |
|
575 | - |
|
576 | - // Set auto_reversed and original_log_id metadata for reversed logs. |
|
577 | - foreach ( $this->reversible_log_types as $reverse_type => $type ) { |
|
578 | - |
|
579 | - if ( $meta['cubepoints_type'] === $type ) { |
|
580 | - |
|
581 | - // Save this log ID for later, in case this log was reversed. |
|
582 | - // cubepoints_data will contain the entity ID. |
|
583 | - $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ] = $log_id; |
|
584 | - |
|
585 | - } elseif ( |
|
586 | - $meta['cubepoints_type'] === $reverse_type |
|
587 | - && isset( $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ] ) |
|
588 | - ) { |
|
589 | - |
|
590 | - // This log was reverses another one. Set the original log ID. |
|
591 | - $meta['original_log_id'] = $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ]; |
|
592 | - |
|
593 | - // And mark the original as auto_reversed. |
|
594 | - wordpoints_add_points_log_meta( $meta['original_log_id'], 'auto_reversed', $log_id ); |
|
595 | - |
|
596 | - // No need to keep this info anymore. |
|
597 | - unset( $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ] ); |
|
598 | - } |
|
599 | - } |
|
600 | - |
|
601 | - // Set the entity IDs to their own meta keys, for the sake of reversals. |
|
602 | - if ( isset( $this->log_type_entities[ $meta['cubepoints_type'] ] ) ) { |
|
603 | - $meta[ $this->log_type_entities[ $meta['cubepoints_type'] ] ] = $meta['cubepoints_data']; |
|
604 | - } |
|
605 | - |
|
606 | - foreach ( $meta as $meta_key => $meta_value ) { |
|
607 | - |
|
608 | - wordpoints_add_points_log_meta( $log_id, $meta_key, $meta_value ); |
|
609 | - } |
|
610 | - |
|
611 | - do_action( 'wordpoints_points_log', $user_id, $points, $points_type, $log_type, $meta, $log_id ); |
|
612 | - |
|
613 | - } // End if ( inserted successfully ). |
|
614 | - } |
|
615 | - |
|
616 | - /** |
|
617 | - * Generate the log text when importing a points log. |
|
618 | - * |
|
619 | - * @since 1.0.0 |
|
620 | - */ |
|
621 | - public function render_points_log_text( $text, $user_id, $points, $points_type, $log_type, $meta ) { |
|
622 | - |
|
623 | - ob_start(); |
|
624 | - do_action( 'cp_logs_description', $meta['cubepoints_type'], $user_id, $points, $meta['cubepoints_data'] ); |
|
625 | - return ob_get_clean(); |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * Get the next 500 points logs from CubePoints. |
|
630 | - * |
|
631 | - * @since 1.0.0 |
|
632 | - * |
|
633 | - * @param int $start The offset number to begin counting the 500 at. |
|
634 | - * |
|
635 | - * @return object[]|false The rows from the database. |
|
636 | - */ |
|
637 | - protected function get_next_points_logs_batch( $start ) { |
|
638 | - |
|
639 | - global $wpdb; |
|
640 | - |
|
641 | - $logs = $wpdb->get_results( // WPCS: unprepared SQL OK. |
|
642 | - $wpdb->prepare( // WPCS: unprepared SQL OK. |
|
643 | - ' |
|
471 | + , $start |
|
472 | + ) |
|
473 | + ); // WPCS: cache OK. |
|
474 | + |
|
475 | + if ( ! is_array( $rows ) ) { |
|
476 | + return false; |
|
477 | + } |
|
478 | + |
|
479 | + return $rows; |
|
480 | + } |
|
481 | + |
|
482 | + /** |
|
483 | + * Check if the points logs can be imported. |
|
484 | + * |
|
485 | + * @since 1.0.0 |
|
486 | + * |
|
487 | + * @return true|WP_Error True on success or a WP_Error on failure. |
|
488 | + */ |
|
489 | + public function can_import_points_logs() { |
|
490 | + |
|
491 | + if ( ! $this->is_cubepoints_active() ) { |
|
492 | + |
|
493 | + return new WP_Error( |
|
494 | + 'wordpoints_import_points_logs_cubepoints_inactive' |
|
495 | + , __( 'CubePoints must be active.', 'wordpoints-importer' ) |
|
496 | + ); |
|
497 | + } |
|
498 | + |
|
499 | + return true; |
|
500 | + } |
|
501 | + |
|
502 | + /** |
|
503 | + * Import the points logs. |
|
504 | + * |
|
505 | + * @since 1.0.0 |
|
506 | + * |
|
507 | + * @param array $settings The import settings for the points component. |
|
508 | + */ |
|
509 | + protected function import_points_logs( $settings ) { |
|
510 | + |
|
511 | + $this->feedback->info( __( 'Importing points logs…', 'wordpoints-importer' ) ); |
|
512 | + |
|
513 | + $start = 0; |
|
514 | + $logs = $this->get_next_points_logs_batch( $start ); |
|
515 | + |
|
516 | + while ( $logs ) { |
|
517 | + |
|
518 | + foreach ( $logs as $log ) { |
|
519 | + |
|
520 | + $this->import_points_log( |
|
521 | + $log->uid |
|
522 | + , $log->points |
|
523 | + , $settings['points_type'] |
|
524 | + , "cubepoints-{$log->type}" |
|
525 | + , array( |
|
526 | + 'cubepoints_type' => $log->type, |
|
527 | + 'cubepoints_data' => $log->data, |
|
528 | + ) |
|
529 | + , date( 'Y-m-d H:i:s', $log->timestamp ) |
|
530 | + ); |
|
531 | + } |
|
532 | + |
|
533 | + $start += count( $logs ); |
|
534 | + $logs = $this->get_next_points_logs_batch( $start ); |
|
535 | + } |
|
536 | + |
|
537 | + // translators: Number of points logs. |
|
538 | + $this->feedback->success( sprintf( __( 'Imported %s points log entries.', 'wordpoints-importer' ), $start ) ); |
|
539 | + } |
|
540 | + |
|
541 | + /** |
|
542 | + * Import a points log. |
|
543 | + * |
|
544 | + * @since 1.0.0 |
|
545 | + * |
|
546 | + * @param int $user_id The ID of the user the log is for. |
|
547 | + * @param int $points The number of points awarded. |
|
548 | + * @param string $points_type The points type. |
|
549 | + * @param string $log_type The log type. |
|
550 | + * @param array $meta The metadata for this log. |
|
551 | + * @param int $date The date the transaction took place. |
|
552 | + */ |
|
553 | + protected function import_points_log( $user_id, $points, $points_type, $log_type, $meta, $date ) { |
|
554 | + |
|
555 | + global $wpdb; |
|
556 | + |
|
557 | + $result = $wpdb->insert( |
|
558 | + $wpdb->wordpoints_points_logs, |
|
559 | + array( |
|
560 | + 'user_id' => $user_id, |
|
561 | + 'points' => $points, |
|
562 | + 'points_type' => $points_type, |
|
563 | + 'log_type' => $log_type, |
|
564 | + 'text' => $this->render_points_log_text( '', $user_id, $points, $points_type, $log_type, $meta ), |
|
565 | + 'date' => $date, |
|
566 | + 'site_id' => $wpdb->siteid, |
|
567 | + 'blog_id' => $wpdb->blogid, |
|
568 | + ), |
|
569 | + array( '%d', '%d', '%s', '%s', '%s', '%s', '%d', '%d' ) |
|
570 | + ); |
|
571 | + |
|
572 | + if ( false !== $result ) { |
|
573 | + |
|
574 | + $log_id = (int) $wpdb->insert_id; |
|
575 | + |
|
576 | + // Set auto_reversed and original_log_id metadata for reversed logs. |
|
577 | + foreach ( $this->reversible_log_types as $reverse_type => $type ) { |
|
578 | + |
|
579 | + if ( $meta['cubepoints_type'] === $type ) { |
|
580 | + |
|
581 | + // Save this log ID for later, in case this log was reversed. |
|
582 | + // cubepoints_data will contain the entity ID. |
|
583 | + $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ] = $log_id; |
|
584 | + |
|
585 | + } elseif ( |
|
586 | + $meta['cubepoints_type'] === $reverse_type |
|
587 | + && isset( $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ] ) |
|
588 | + ) { |
|
589 | + |
|
590 | + // This log was reverses another one. Set the original log ID. |
|
591 | + $meta['original_log_id'] = $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ]; |
|
592 | + |
|
593 | + // And mark the original as auto_reversed. |
|
594 | + wordpoints_add_points_log_meta( $meta['original_log_id'], 'auto_reversed', $log_id ); |
|
595 | + |
|
596 | + // No need to keep this info anymore. |
|
597 | + unset( $this->reversible_log_ids[ $type ][ $meta['cubepoints_data'] ] ); |
|
598 | + } |
|
599 | + } |
|
600 | + |
|
601 | + // Set the entity IDs to their own meta keys, for the sake of reversals. |
|
602 | + if ( isset( $this->log_type_entities[ $meta['cubepoints_type'] ] ) ) { |
|
603 | + $meta[ $this->log_type_entities[ $meta['cubepoints_type'] ] ] = $meta['cubepoints_data']; |
|
604 | + } |
|
605 | + |
|
606 | + foreach ( $meta as $meta_key => $meta_value ) { |
|
607 | + |
|
608 | + wordpoints_add_points_log_meta( $log_id, $meta_key, $meta_value ); |
|
609 | + } |
|
610 | + |
|
611 | + do_action( 'wordpoints_points_log', $user_id, $points, $points_type, $log_type, $meta, $log_id ); |
|
612 | + |
|
613 | + } // End if ( inserted successfully ). |
|
614 | + } |
|
615 | + |
|
616 | + /** |
|
617 | + * Generate the log text when importing a points log. |
|
618 | + * |
|
619 | + * @since 1.0.0 |
|
620 | + */ |
|
621 | + public function render_points_log_text( $text, $user_id, $points, $points_type, $log_type, $meta ) { |
|
622 | + |
|
623 | + ob_start(); |
|
624 | + do_action( 'cp_logs_description', $meta['cubepoints_type'], $user_id, $points, $meta['cubepoints_data'] ); |
|
625 | + return ob_get_clean(); |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * Get the next 500 points logs from CubePoints. |
|
630 | + * |
|
631 | + * @since 1.0.0 |
|
632 | + * |
|
633 | + * @param int $start The offset number to begin counting the 500 at. |
|
634 | + * |
|
635 | + * @return object[]|false The rows from the database. |
|
636 | + */ |
|
637 | + protected function get_next_points_logs_batch( $start ) { |
|
638 | + |
|
639 | + global $wpdb; |
|
640 | + |
|
641 | + $logs = $wpdb->get_results( // WPCS: unprepared SQL OK. |
|
642 | + $wpdb->prepare( // WPCS: unprepared SQL OK. |
|
643 | + ' |
|
644 | 644 | SELECT * |
645 | 645 | FROM `' . CP_DB . '` |
646 | 646 | ORDER BY `id` |
647 | 647 | LIMIT %d,500 |
648 | 648 | ' |
649 | - , $start |
|
650 | - ) |
|
651 | - ); // WPCS: cache OK. |
|
649 | + , $start |
|
650 | + ) |
|
651 | + ); // WPCS: cache OK. |
|
652 | 652 | |
653 | - if ( ! is_array( $logs ) ) { |
|
654 | - $this->feedback->error( __( 'Unable to retrieve the logs from CubePoints…', 'wordpoints-importer' ) ); |
|
655 | - return false; |
|
656 | - } |
|
653 | + if ( ! is_array( $logs ) ) { |
|
654 | + $this->feedback->error( __( 'Unable to retrieve the logs from CubePoints…', 'wordpoints-importer' ) ); |
|
655 | + return false; |
|
656 | + } |
|
657 | 657 | |
658 | - return $logs; |
|
659 | - } |
|
658 | + return $logs; |
|
659 | + } |
|
660 | 660 | |
661 | - /** |
|
662 | - * Import ranks. |
|
663 | - * |
|
664 | - * @since 1.1.0 |
|
665 | - * |
|
666 | - * @param array $settings The import settings for the ranks component. |
|
667 | - */ |
|
668 | - public function import_ranks( $settings ) { |
|
661 | + /** |
|
662 | + * Import ranks. |
|
663 | + * |
|
664 | + * @since 1.1.0 |
|
665 | + * |
|
666 | + * @param array $settings The import settings for the ranks component. |
|
667 | + */ |
|
668 | + public function import_ranks( $settings ) { |
|
669 | 669 | |
670 | - $this->feedback->info( __( 'Importing ranks…', 'wordpoints-importer' ) ); |
|
670 | + $this->feedback->info( __( 'Importing ranks…', 'wordpoints-importer' ) ); |
|
671 | 671 | |
672 | - $ranks_data = get_option( 'cp_module_ranks_data' ); |
|
672 | + $ranks_data = get_option( 'cp_module_ranks_data' ); |
|
673 | 673 | |
674 | - if ( empty( $ranks_data ) || ! is_array( $ranks_data ) ) { |
|
675 | - $this->feedback->error( __( 'No ranks found.', 'wordpoints-importer' ) ); |
|
676 | - return; |
|
677 | - } |
|
674 | + if ( empty( $ranks_data ) || ! is_array( $ranks_data ) ) { |
|
675 | + $this->feedback->error( __( 'No ranks found.', 'wordpoints-importer' ) ); |
|
676 | + return; |
|
677 | + } |
|
678 | 678 | |
679 | - $i = 0; |
|
679 | + $i = 0; |
|
680 | 680 | |
681 | - // The base rank already exists, so we just update it. |
|
682 | - if ( isset( $ranks_data[0] ) ) { |
|
681 | + // The base rank already exists, so we just update it. |
|
682 | + if ( isset( $ranks_data[0] ) ) { |
|
683 | 683 | |
684 | - wordpoints_update_rank( |
|
685 | - WordPoints_Rank_Groups::get_group( $settings['rank_group'] )->get_rank( 0 ) |
|
686 | - , $ranks_data[0] |
|
687 | - , 'base' |
|
688 | - , $settings['rank_group'] |
|
689 | - , 0 |
|
690 | - ); |
|
684 | + wordpoints_update_rank( |
|
685 | + WordPoints_Rank_Groups::get_group( $settings['rank_group'] )->get_rank( 0 ) |
|
686 | + , $ranks_data[0] |
|
687 | + , 'base' |
|
688 | + , $settings['rank_group'] |
|
689 | + , 0 |
|
690 | + ); |
|
691 | 691 | |
692 | - $i++; |
|
692 | + $i++; |
|
693 | 693 | |
694 | - unset( $ranks_data[0] ); |
|
695 | - } |
|
694 | + unset( $ranks_data[0] ); |
|
695 | + } |
|
696 | 696 | |
697 | - $points_type = substr( $settings['rank_group'], strlen( 'points_type-' ) ); |
|
698 | - $rank_type = 'points-' . $points_type; |
|
697 | + $points_type = substr( $settings['rank_group'], strlen( 'points_type-' ) ); |
|
698 | + $rank_type = 'points-' . $points_type; |
|
699 | 699 | |
700 | - ksort( $ranks_data ); |
|
700 | + ksort( $ranks_data ); |
|
701 | 701 | |
702 | - foreach ( $ranks_data as $points => $rank_name ) { |
|
702 | + foreach ( $ranks_data as $points => $rank_name ) { |
|
703 | 703 | |
704 | - wordpoints_add_rank( |
|
705 | - $rank_name |
|
706 | - , $rank_type |
|
707 | - , $settings['rank_group'] |
|
708 | - , $i |
|
709 | - , array( 'points' => $points, 'points_type' => $points_type ) |
|
710 | - ); |
|
704 | + wordpoints_add_rank( |
|
705 | + $rank_name |
|
706 | + , $rank_type |
|
707 | + , $settings['rank_group'] |
|
708 | + , $i |
|
709 | + , array( 'points' => $points, 'points_type' => $points_type ) |
|
710 | + ); |
|
711 | 711 | |
712 | - $i++; |
|
713 | - } |
|
712 | + $i++; |
|
713 | + } |
|
714 | 714 | |
715 | - // translators: Number of ranks. |
|
716 | - $this->feedback->success( sprintf( __( 'Imported %s ranks.', 'wordpoints-importer' ), $i ) ); |
|
717 | - } |
|
715 | + // translators: Number of ranks. |
|
716 | + $this->feedback->success( sprintf( __( 'Imported %s ranks.', 'wordpoints-importer' ), $i ) ); |
|
717 | + } |
|
718 | 718 | } |
719 | 719 | |
720 | 720 | // EOF |
@@ -8,10 +8,10 @@ |
||
8 | 8 | */ |
9 | 9 | |
10 | 10 | return array( |
11 | - 'wordpoints_importers' => 'class-importers.php', |
|
12 | - 'wordpoints_importer' => 'class-importer.php', |
|
13 | - 'wordpoints_cubepoints_importer' => 'importers/cubepoints.php', |
|
14 | - 'wordpoints_importer_feedback' => 'class-feedback.php', |
|
11 | + 'wordpoints_importers' => 'class-importers.php', |
|
12 | + 'wordpoints_importer' => 'class-importer.php', |
|
13 | + 'wordpoints_cubepoints_importer' => 'importers/cubepoints.php', |
|
14 | + 'wordpoints_importer_feedback' => 'class-feedback.php', |
|
15 | 15 | ); |
16 | 16 | |
17 | 17 | // EOF |
@@ -14,138 +14,138 @@ |
||
14 | 14 | */ |
15 | 15 | final class WordPoints_Importers { |
16 | 16 | |
17 | - // |
|
18 | - // Private Vars. |
|
19 | - // |
|
20 | - |
|
21 | - /** |
|
22 | - * The registered importers. |
|
23 | - * |
|
24 | - * @since 1.0.0 |
|
25 | - * |
|
26 | - * @type array $importers |
|
27 | - */ |
|
28 | - private static $importers = array(); |
|
29 | - |
|
30 | - /** |
|
31 | - * Whether the class has been initialized yet. |
|
32 | - * |
|
33 | - * @since 1.0.0 |
|
34 | - * |
|
35 | - * @type bool $initialized |
|
36 | - */ |
|
37 | - private static $initialized = false; |
|
38 | - |
|
39 | - // |
|
40 | - // Private Functions. |
|
41 | - // |
|
42 | - |
|
43 | - /** |
|
44 | - * Initialize the class. |
|
45 | - * |
|
46 | - * @since 1.0.0 |
|
47 | - */ |
|
48 | - private static function init() { |
|
49 | - |
|
50 | - // We do this first so we avoid infinite loops if this class is called by a |
|
51 | - // function hooked to the below action. |
|
52 | - self::$initialized = true; |
|
53 | - |
|
54 | - /** |
|
55 | - * Register importers. |
|
56 | - * |
|
57 | - * @since 1.0.0 |
|
58 | - */ |
|
59 | - do_action( 'wordpoints_register_importers' ); |
|
60 | - } |
|
61 | - |
|
62 | - // |
|
63 | - // Public Functions. |
|
64 | - // |
|
65 | - |
|
66 | - /** |
|
67 | - * Get all of the registered importers. |
|
68 | - * |
|
69 | - * @since 1.0.0 |
|
70 | - * |
|
71 | - * @return array All of the registered importers. |
|
72 | - */ |
|
73 | - public static function get() { |
|
74 | - |
|
75 | - if ( ! self::$initialized ) { |
|
76 | - self::init(); |
|
77 | - } |
|
78 | - |
|
79 | - return self::$importers; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Register an importer. |
|
84 | - * |
|
85 | - * If the importer is already registered, it will be overwritten. |
|
86 | - * |
|
87 | - * @since 1.0.0 |
|
88 | - * |
|
89 | - * @param string $slug The unique identifier for this importer. |
|
90 | - * @param array $args { |
|
91 | - * Other importer arguments. |
|
92 | - * |
|
93 | - * @type string $class The Importer class. |
|
94 | - * @type string $name The name of this importer. |
|
95 | - * } |
|
96 | - */ |
|
97 | - public static function register( $slug, array $args ) { |
|
98 | - self::$importers[ $slug ] = $args; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Deregister an importer. |
|
103 | - * |
|
104 | - * @since 1.0.0 |
|
105 | - * |
|
106 | - * @param string $slug The slug of the importer to deregister. |
|
107 | - */ |
|
108 | - public static function deregister( $slug ) { |
|
109 | - unset( self::$importers[ $slug ] ); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Check if an importer is registered. |
|
114 | - * |
|
115 | - * @since 1.0.0 |
|
116 | - * |
|
117 | - * @param string $slug The slug of the importer. |
|
118 | - * |
|
119 | - * @return bool True if the importer is registered, otherwise false. |
|
120 | - */ |
|
121 | - public static function is_registered( $slug ) { |
|
122 | - |
|
123 | - if ( ! self::$initialized ) { |
|
124 | - self::init(); |
|
125 | - } |
|
126 | - |
|
127 | - return isset( self::$importers[ $slug ] ); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Get an instance of an importer. |
|
132 | - * |
|
133 | - * @since 1.0.0 |
|
134 | - * |
|
135 | - * @param string $slug The slug of the importer to get an instance of. |
|
136 | - * |
|
137 | - * @return WordPoints_Importer|false The importer, or false if it isn't registered. |
|
138 | - */ |
|
139 | - public static function get_importer( $slug ) { |
|
140 | - |
|
141 | - if ( ! self::is_registered( $slug ) ) { |
|
142 | - return false; |
|
143 | - } |
|
144 | - |
|
145 | - $importer = self::$importers[ $slug ]; |
|
146 | - |
|
147 | - return new $importer['class']( $importer['name'] ); |
|
148 | - } |
|
17 | + // |
|
18 | + // Private Vars. |
|
19 | + // |
|
20 | + |
|
21 | + /** |
|
22 | + * The registered importers. |
|
23 | + * |
|
24 | + * @since 1.0.0 |
|
25 | + * |
|
26 | + * @type array $importers |
|
27 | + */ |
|
28 | + private static $importers = array(); |
|
29 | + |
|
30 | + /** |
|
31 | + * Whether the class has been initialized yet. |
|
32 | + * |
|
33 | + * @since 1.0.0 |
|
34 | + * |
|
35 | + * @type bool $initialized |
|
36 | + */ |
|
37 | + private static $initialized = false; |
|
38 | + |
|
39 | + // |
|
40 | + // Private Functions. |
|
41 | + // |
|
42 | + |
|
43 | + /** |
|
44 | + * Initialize the class. |
|
45 | + * |
|
46 | + * @since 1.0.0 |
|
47 | + */ |
|
48 | + private static function init() { |
|
49 | + |
|
50 | + // We do this first so we avoid infinite loops if this class is called by a |
|
51 | + // function hooked to the below action. |
|
52 | + self::$initialized = true; |
|
53 | + |
|
54 | + /** |
|
55 | + * Register importers. |
|
56 | + * |
|
57 | + * @since 1.0.0 |
|
58 | + */ |
|
59 | + do_action( 'wordpoints_register_importers' ); |
|
60 | + } |
|
61 | + |
|
62 | + // |
|
63 | + // Public Functions. |
|
64 | + // |
|
65 | + |
|
66 | + /** |
|
67 | + * Get all of the registered importers. |
|
68 | + * |
|
69 | + * @since 1.0.0 |
|
70 | + * |
|
71 | + * @return array All of the registered importers. |
|
72 | + */ |
|
73 | + public static function get() { |
|
74 | + |
|
75 | + if ( ! self::$initialized ) { |
|
76 | + self::init(); |
|
77 | + } |
|
78 | + |
|
79 | + return self::$importers; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Register an importer. |
|
84 | + * |
|
85 | + * If the importer is already registered, it will be overwritten. |
|
86 | + * |
|
87 | + * @since 1.0.0 |
|
88 | + * |
|
89 | + * @param string $slug The unique identifier for this importer. |
|
90 | + * @param array $args { |
|
91 | + * Other importer arguments. |
|
92 | + * |
|
93 | + * @type string $class The Importer class. |
|
94 | + * @type string $name The name of this importer. |
|
95 | + * } |
|
96 | + */ |
|
97 | + public static function register( $slug, array $args ) { |
|
98 | + self::$importers[ $slug ] = $args; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Deregister an importer. |
|
103 | + * |
|
104 | + * @since 1.0.0 |
|
105 | + * |
|
106 | + * @param string $slug The slug of the importer to deregister. |
|
107 | + */ |
|
108 | + public static function deregister( $slug ) { |
|
109 | + unset( self::$importers[ $slug ] ); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Check if an importer is registered. |
|
114 | + * |
|
115 | + * @since 1.0.0 |
|
116 | + * |
|
117 | + * @param string $slug The slug of the importer. |
|
118 | + * |
|
119 | + * @return bool True if the importer is registered, otherwise false. |
|
120 | + */ |
|
121 | + public static function is_registered( $slug ) { |
|
122 | + |
|
123 | + if ( ! self::$initialized ) { |
|
124 | + self::init(); |
|
125 | + } |
|
126 | + |
|
127 | + return isset( self::$importers[ $slug ] ); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Get an instance of an importer. |
|
132 | + * |
|
133 | + * @since 1.0.0 |
|
134 | + * |
|
135 | + * @param string $slug The slug of the importer to get an instance of. |
|
136 | + * |
|
137 | + * @return WordPoints_Importer|false The importer, or false if it isn't registered. |
|
138 | + */ |
|
139 | + public static function get_importer( $slug ) { |
|
140 | + |
|
141 | + if ( ! self::is_registered( $slug ) ) { |
|
142 | + return false; |
|
143 | + } |
|
144 | + |
|
145 | + $importer = self::$importers[ $slug ]; |
|
146 | + |
|
147 | + return new $importer['class']( $importer['name'] ); |
|
148 | + } |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | // EOF |