1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* @TODO |
4
|
|
|
*/ |
5
|
|
|
if ( ! class_exists( 'FooGallery_Nextgen_Gallery_Importer_Extension' ) ) { |
6
|
|
|
|
7
|
|
|
require_once 'class-nextgen-helper.php'; |
8
|
|
|
require_once 'class-nextgen-import-progress.php'; |
9
|
|
|
require_once 'class-nextgen-import-progress-album.php'; |
10
|
|
|
|
11
|
|
|
class FooGallery_Nextgen_Gallery_Importer_Extension { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var FooGallery_NextGen_Helper |
15
|
|
|
*/ |
16
|
|
|
private $nextgen; |
17
|
|
|
|
18
|
|
|
function __construct() { |
19
|
|
|
$this->nextgen = new FooGallery_NextGen_Helper(); |
20
|
|
|
|
21
|
|
|
//always show the menu |
22
|
|
|
add_action( 'foogallery_admin_menu_after', array( $this, 'add_menu' ) ); |
23
|
|
|
add_action( 'foogallery_extension_activated-nextgen', array( $this, 'add_menu' ) ); |
24
|
|
|
|
25
|
|
|
//hook into the foogallery menu |
26
|
|
|
add_action( 'foogallery_admin_help_after_section_one', array( $this, 'show_nextgen_import_help' ) ); |
27
|
|
|
|
28
|
|
|
// Ajax calls for importing galleries |
29
|
|
|
add_action( 'wp_ajax_foogallery_nextgen_import', array( $this, 'ajax_nextgen_start_import' ) ); |
30
|
|
|
add_action( 'wp_ajax_foogallery_nextgen_import_refresh', array( $this, 'ajax_nextgen_continue_import' ) ); |
31
|
|
|
add_action( 'wp_ajax_foogallery_nextgen_import_cancel', array( $this, 'ajax_nextgen_cancel_import' ) ); |
32
|
|
|
add_action( 'wp_ajax_foogallery_nextgen_import_reset', array( $this, 'ajax_nextgen_reset_import' ) ); |
33
|
|
|
|
34
|
|
|
// Ajax calls for importing albums |
35
|
|
|
add_action( 'wp_ajax_foogallery_nextgen_album_import_reset', array( $this, 'ajax_nextgen_reset_album_import' ) ); |
36
|
|
|
add_action( 'wp_ajax_foogallery_nextgen_album_import', array( $this, 'ajax_nextgen_start_album_import' ) ); |
37
|
|
|
|
38
|
|
|
// Ajax calls for converting shortcodes |
39
|
|
|
add_action( 'wp_ajax_foogallery_nextgen_find_shortcodes', array( $this, 'ajax_nextgen_find_shortcodes' ) ); |
40
|
|
|
add_action( 'wp_ajax_foogallery_nextgen_replace_shortcodes', array( $this, 'ajax_nextgen_replace_shortcodes' ) ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
function add_menu() { |
44
|
|
|
foogallery_add_submenu_page( __( 'NextGen Importer', 'foogallery' ), 'manage_options', 'foogallery-nextgen-importer', array( |
45
|
|
|
$this, |
46
|
|
|
'render_view', |
47
|
|
|
) ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
function render_view() { |
51
|
|
|
require_once 'view-importer.php'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
function ajax_nextgen_start_import() { |
55
|
|
|
if ( check_admin_referer( 'foogallery_nextgen_import', 'foogallery_nextgen_import' ) ) { |
56
|
|
|
|
57
|
|
|
$this->nextgen->ignore_previously_imported_galleries(); |
58
|
|
|
|
59
|
|
|
if ( array_key_exists( 'nextgen-id', $_POST ) ) { |
60
|
|
|
|
61
|
|
|
$nextgen_gallery_ids = $_POST['nextgen-id']; |
62
|
|
|
|
63
|
|
|
foreach ( $nextgen_gallery_ids as $gid ) { |
64
|
|
|
$foogallery_title = stripslashes( $_POST[ 'foogallery-name-' . $gid ] ); |
65
|
|
|
|
66
|
|
|
//init the start progress of the import for the gallery |
67
|
|
|
$this->nextgen->init_import_progress( $gid, $foogallery_title ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->nextgen->start_import(); |
71
|
|
|
|
72
|
|
|
} else { |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->nextgen->render_import_form(); |
78
|
|
|
|
79
|
|
|
die(); |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
function ajax_nextgen_continue_import() { |
84
|
|
|
if ( check_admin_referer( 'foogallery_nextgen_import_refresh', 'foogallery_nextgen_import_refresh' ) ) { |
85
|
|
|
|
86
|
|
|
$this->nextgen->continue_import(); |
87
|
|
|
|
88
|
|
|
$this->nextgen->render_import_form(); |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
die(); |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
function ajax_nextgen_cancel_import() { |
97
|
|
|
if ( check_admin_referer( 'foogallery_nextgen_import_cancel', 'foogallery_nextgen_import_cancel' ) ) { |
98
|
|
|
|
99
|
|
|
$this->nextgen->cancel_import(); |
100
|
|
|
|
101
|
|
|
$this->nextgen->render_import_form(); |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
die(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
function ajax_nextgen_reset_import() { |
108
|
|
|
if ( check_admin_referer( 'foogallery_nextgen_reset', 'foogallery_nextgen_reset' ) ) { |
109
|
|
|
|
110
|
|
|
$this->nextgen->reset_import(); |
111
|
|
|
|
112
|
|
|
$this->nextgen->render_import_form(); |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
die(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
function ajax_nextgen_start_album_import() { |
119
|
|
|
if ( check_admin_referer( 'foogallery_nextgen_album_import', 'foogallery_nextgen_album_import' ) ) { |
120
|
|
|
|
121
|
|
|
if ( array_key_exists( 'nextgen_album_id', $_POST ) ) { |
122
|
|
|
|
123
|
|
|
$nextgen_album_id = $_POST['nextgen_album_id']; |
124
|
|
|
$foogallery_album_title = stripslashes( $_POST[ 'foogallery_album_name' ] ); |
125
|
|
|
|
126
|
|
|
//import the album |
127
|
|
|
$this->nextgen->import_album( $nextgen_album_id, $foogallery_album_title ); |
128
|
|
|
|
129
|
|
|
} else { |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->nextgen->render_album_import_form(); |
135
|
|
|
|
136
|
|
|
die(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
function ajax_nextgen_reset_album_import() { |
140
|
|
|
if ( check_admin_referer( 'foogallery_nextgen_album_reset', 'foogallery_nextgen_album_reset' ) ) { |
141
|
|
|
|
142
|
|
|
//$this->nextgen->reset_import(); |
|
|
|
|
143
|
|
|
|
144
|
|
|
$this->nextgen->render_album_import_form(); |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
die(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
function show_nextgen_import_help() { |
151
|
|
|
?> |
152
|
|
|
<div class="changelog"> |
153
|
|
|
|
154
|
|
|
<div class="feature-section"> |
155
|
|
|
<img src="<?php echo FOOGALLERY_URL . 'assets/screenshots/admin-nextgen-import.jpg'; ?>" |
156
|
|
|
class="foogallery-help-screenshot"/> |
157
|
|
|
|
158
|
|
|
<h2><?php _e( 'Import Your NextGen Galleries', 'foogallery' ); ?></h2> |
159
|
|
|
|
160
|
|
|
<h4><?php _e( 'Import Galleries', 'foogallery' ); ?></h4> |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
|
164
|
|
|
<p><?php printf( __( 'Import all your NextGen galleries in a single click, or choose the galleries you would like to migrate over to %s.', 'foogallery' ), foogallery_plugin_name() ); ?></p> |
165
|
|
|
|
166
|
|
|
<h4><?php _e( 'Import Images', 'foogallery' ); ?></h4> |
167
|
|
|
|
168
|
|
|
<p><?php _e( 'NextGen gallery images are imported into your WordPress media library, where they should be!', 'foogallery' ); ?></p> |
169
|
|
|
|
170
|
|
|
</div> |
171
|
|
|
</div> |
172
|
|
|
<?php |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
function ajax_nextgen_find_shortcodes() { |
|
|
|
|
176
|
|
|
if ( check_admin_referer( 'foogallery_nextgen_find_shortcodes' ) ) { |
177
|
|
|
$this->echo_findings_for_shortcode('nggallery', 'id'); |
178
|
|
|
$this->echo_findings_for_shortcode('ngg_images', 'container_ids'); |
179
|
|
|
$this->echo_findings_for_shortcode('imagebrowser', 'id'); |
180
|
|
|
$this->echo_findings_for_shortcode('slideshow', 'id'); |
181
|
|
|
|
182
|
|
|
?> |
183
|
|
|
<p> |
184
|
|
|
<input type="submit" class="button button-primary replace-shortcodes" value="<?php _e( 'Replace Shortcodes', 'foogallery' ); ?>"> |
185
|
|
|
<?php wp_nonce_field( 'foogallery_nextgen_replace_shortcodes', 'foogallery_nextgen_replace_shortcodes' ); ?> |
186
|
|
|
<div style="width:40px; position: absolute;"><span class="spinner"></span></div> |
187
|
|
|
</p> |
188
|
|
|
<?php |
189
|
|
|
} |
190
|
|
|
die(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
function ajax_nextgen_replace_shortcodes() { |
|
|
|
|
194
|
|
|
if ( check_admin_referer( 'foogallery_nextgen_replace_shortcodes' ) ) { |
195
|
|
|
$this->echo_replacements_for_shortcode('nggallery', 'id'); |
196
|
|
|
$this->echo_replacements_for_shortcode('ngg_images', 'container_ids'); |
197
|
|
|
$this->echo_replacements_for_shortcode('imagebrowser', 'id'); |
198
|
|
|
$this->echo_replacements_for_shortcode('slideshow', 'id'); |
199
|
|
|
} |
200
|
|
|
die(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
function echo_findings_for_shortcode( $shortcode, $id_attrib ) { |
|
|
|
|
204
|
|
|
echo '<h3>[' . $shortcode . '] Shortcodes</h3>'; |
205
|
|
|
|
206
|
|
|
$results = $this->find_posts_with_shortcode($shortcode, $id_attrib); |
207
|
|
|
|
208
|
|
|
$posts = array(); |
209
|
|
|
$replacements = 0; |
210
|
|
|
$non_convertible = 0; |
211
|
|
|
foreach ($results as $result) { |
212
|
|
|
if ( !array_key_exists( $result->post_id, $posts ) ) { |
213
|
|
|
$posts[$result->post_id] = $result->post_id; |
214
|
|
|
} |
215
|
|
|
if ( isset( $result->foogallery_id ) ) { |
216
|
|
|
//a replacement is possible |
217
|
|
|
$replacements++; |
218
|
|
|
} else { |
219
|
|
|
$non_convertible++; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
if ( count($posts) > 0 ) { |
223
|
|
|
echo '<strong>' . count( $posts ) . '</strong> ' . __( 'posts found containing the shortcode', 'foogallery' ) . ' [' . $shortcode . ']<br>'; |
224
|
|
|
if ( $replacements > 0 ) { |
225
|
|
|
echo '<strong>' . $replacements . '</strong> ' . __( 'shortcodes found that can be replaced!', 'foogallery' ) . '<br>'; |
226
|
|
|
} |
227
|
|
|
if ( $non_convertible > 0 ) { |
228
|
|
|
echo '<strong>' . $non_convertible . '</strong> ' . __( 'shortcodes found that cannot be replaced, due to the gallery not being imported.', 'foogallery' ) . '<br>'; |
229
|
|
|
} |
230
|
|
|
} else { |
231
|
|
|
echo __( 'NO posts were found containing the shortcode', 'foogallery' ) . ' [' . $shortcode . ']'; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
function echo_replacements_for_shortcode( $shortcode, $id_attrib ) { |
|
|
|
|
236
|
|
|
echo '<h3>[' . $shortcode . '] Shortcodes</h3>'; |
237
|
|
|
|
238
|
|
|
$results = $this->find_posts_with_shortcode($shortcode, $id_attrib); |
239
|
|
|
|
240
|
|
|
$replacements = 0; |
241
|
|
|
|
242
|
|
|
foreach ($results as $result) { |
243
|
|
|
if ( $result->foogallery_id > 0 ) { |
244
|
|
|
$content = str_replace( $result->original, $result->replacement, $result->post_content ); |
245
|
|
|
|
246
|
|
|
$my_post = array( |
247
|
|
|
'ID' => $result->post_id, |
248
|
|
|
'post_content' => $content, |
249
|
|
|
); |
250
|
|
|
|
251
|
|
|
//update the post in the database! |
252
|
|
|
wp_update_post( $my_post ); |
253
|
|
|
$replacements++; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
if ( $replacements > 0 ) { |
257
|
|
|
echo '<strong>' . $replacements . '</strong> ' . __( 'shortcodes were replaced!', 'foogallery' ) . '<br>'; |
258
|
|
|
} else { |
259
|
|
|
echo __( 'NO replacements were made for the shortcode', 'foogallery' ) . ' [' . $shortcode . ']'; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
function find_posts_with_shortcode( $shortcode, $id_attrib ) { |
|
|
|
|
264
|
|
|
global $shortcode_tags; |
|
|
|
|
265
|
|
|
$temp_shortcode_tags = $shortcode_tags; |
266
|
|
|
$shortcode_tags = array( $shortcode => '' ); |
267
|
|
|
$regex = '/' . get_shortcode_regex() . '/s'; |
268
|
|
|
$shortcode_tags = $temp_shortcode_tags; |
269
|
|
|
|
270
|
|
|
$posts = get_posts( array( |
271
|
|
|
'numberposts' => -1, |
272
|
|
|
'post_type' => 'any', |
273
|
|
|
's' => '[' . $shortcode, |
274
|
|
|
) ); |
275
|
|
|
|
276
|
|
|
$results = array(); |
277
|
|
|
|
278
|
|
|
if ( count( $posts ) > 0 ) { |
279
|
|
|
foreach ( $posts as $post ) { |
280
|
|
|
|
281
|
|
|
preg_match_all( $regex, $post->post_content, $matches ); |
282
|
|
|
|
283
|
|
|
if ( isset( $matches[2] ) ) { |
284
|
|
|
foreach( $matches[2] as $key => $value ) { |
285
|
|
|
if ( $shortcode === $value ) { |
286
|
|
|
//we have found a shortcode match so store it |
287
|
|
|
$result = new stdClass(); |
288
|
|
|
$result->post_id = $post->ID; |
289
|
|
|
$result->post_content = $post->post_content; |
290
|
|
|
$result->original = $matches[0][$key]; |
291
|
|
|
$result->attributes = shortcode_parse_atts( $matches[3][$key] ); |
292
|
|
|
if ( array_key_exists( $id_attrib, $result->attributes ) ) { |
293
|
|
|
$result->nextgen_id = intval( $result->attributes[$id_attrib] ); |
294
|
|
|
|
295
|
|
|
$helper = new FooGallery_NextGen_Helper(); |
296
|
|
|
$progress = $helper->get_import_progress( $result->nextgen_id ); |
297
|
|
|
|
298
|
|
|
if ( $progress->foogallery_id > 0 ) { |
299
|
|
|
$result->foogallery_id = $progress->foogallery_id; |
300
|
|
|
$result->replacement = '[foogallery id="' . $result->foogallery_id . '"]'; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
$results[] = $result; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return $results; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.