forked from stefansenk/convert-nextgen-galleries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-nextgen-galleries.php
181 lines (153 loc) · 6.35 KB
/
convert-nextgen-galleries.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/*
Plugin Name: Convert NextGEN Galleries to WordPress
Plugin URI:
Description: Converts NextGEN galleries to WordPress default galleries.
Version: 1.0
Author: Stefan Senk
Author URI: http://www.senktec.com
License: GPL2
Copyright 2014 Stefan Senk (email : info@senktec.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
function cng_admin_url() {
return admin_url('options-general.php?page=convert-nextgen-galleries.php');
}
function cng_get_posts_to_convert_query($post_id = null, $max_number_of_posts = -1) {
$args = array(
's' => '[nggallery',
'post_type' => array( 'post', 'page' ),
'post_status' => 'any',
'p' => $post_id,
'posts_per_page' => $max_number_of_posts
);
return new WP_Query( $args );
}
function cng_find_gallery_shortcodes($post) {
$matches = null;
preg_match_all( '/\[nggallery.*?\]/si', $post->post_content, $matches );
return $matches[0];
}
function cng_get_gallery_id_from_shortcode($shortcode) {
$atts = shortcode_parse_atts($shortcode);
return intval( $atts['id'] );
}
function cng_list_galleries($posts_query) {
echo '<h3>Listing ' . $posts_query->found_posts . ' posts with galleries to convert:</h3>';
echo '<table>';
echo '<tr>';
echo '<th>Post ID</th>';
echo '<th>Post Title</th>';
echo '<th>Galleries</th>';
echo '<th colspan="2">Actions</th>';
echo '<tr>';
foreach ( $posts_query->posts as $post ) {
echo '<tr>';
echo '<td>' . $post->ID . '</td>';
echo '<td>' . $post->post_title . '</td>';
echo '<td>';
foreach ( cng_find_gallery_shortcodes($post) as $shortcode ) {
echo $shortcode . '<br>';
}
echo '</td>';
echo '<td><a href="' . admin_url('post.php?action=edit&post=' . $post->ID) . '">Edit Post</a></td>';
echo '<td><a href="' . cng_admin_url() . '&action=convert&post=' . $post->ID . '" class="button">Convert</a></td>';
echo '<tr>';
}
echo '</table>';
}
function cng_convert_galleries($posts_query) {
set_time_limit( 1000 );
global $wpdb;
echo '<h3>Converting galleries in ' . $posts_query->found_posts . ' posts:</h3>';
foreach ( $posts_query->posts as $post ) {
echo '<h4>' . $post->post_title . '</h4>';
foreach ( cng_find_gallery_shortcodes($post) as $shortcode ) {
$gallery_id = cng_get_gallery_id_from_shortcode($shortcode);
$gallery_directory = $wpdb->get_var( $wpdb->prepare( "SELECT path FROM {$wpdb->prefix}ngg_gallery WHERE gid = %d", $gallery_id ) );
$images = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}ngg_pictures WHERE galleryid = %d ORDER BY sortorder, pid ASC", $gallery_id ) );
$attachment_ids = array();
foreach ( $images as $image ) {
$existing_image_path = ABSPATH . trailingslashit( $gallery_directory ) . $image->filename;
if ( ! file_exists ($existing_image_path) ) {
echo "ERROR: File '$existing_image_path' not found.<br>";
continue;
}
$tmp_image_path = get_temp_dir() . $image->filename;
copy($existing_image_path, $tmp_image_path);
$file_array['name'] = $image->filename;
$file_array['tmp_name'] = $tmp_image_path;
if ( ! trim( $image->alttext ) )
$image->alttext = $image->filename;
$post_data = array(
'post_title' => $image->alttext,
// 'post_content' => $image->alttext,
'post_excerpt' => $image->description,
'menu_order' => $image->sortorder
);
$id = media_handle_sideload( $file_array, $post->ID, null, $post_data );
if ( is_wp_error($id) ) {
echo "ERROR: media_handle_sideload() filed for '$existing_image_path'.<br>";
continue;
}
array_push( $attachment_ids, $id );
$attachment = get_post( $id );
update_post_meta( $attachment->ID, '_wp_attachment_image_alt', $image->alttext );
}
if ( count( $attachment_ids ) == count( $images ) ) {
$new_shortcode = '[gallery columns="4" link="file" ids="'. implode( ',', $attachment_ids ) . '"]';
$post->post_content = str_replace( $shortcode, $new_shortcode, $post->post_content );
wp_update_post( $post );
echo "Replaced <code>$shortcode</code> with <code>$new_shortcode</code>.<br>";
} else {
echo "<p>Not replacing <code>$shortcode</code>. " . count( $attachment_ids ) . " of " . count( $images ) . " images converted.</p>";
}
}
}
}
add_filter( 'plugin_action_links', function($links, $file) {
if ( $file == 'convert-nextgen-galleries/convert-nextgen-galleries.php' ) {
array_unshift( $links, '<a href="' . cng_admin_url() . '">' . __( 'Settings', 'convert-nextgen-galleries' ) . '</a>' );
}
return $links;
}, 10, 2 );
add_action('admin_menu', function() {
add_options_page(
__( 'Convert NextGEN Galleries', 'convert-nextgen-galleries' ),
__( 'Convert NextGEN Galleries', 'convert-nextgen-galleries' ),
'manage_options', 'convert-nextgen-galleries.php', function() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have permission to access this page.', 'convert-nextgen-galleries' ) );
}
?>
<div class="wrap">
<h2><?php _e( 'Convert NextGEN Galleries to WordPress', 'convert-nextgen-galleries' ); ?></h2>
<?php
$post_id = isset($_GET['post']) ? $_GET['post'] : null;
$max_num_to_convert = isset($_GET['max_num']) ? $_GET['max_num'] : -1;
$posts_to_convert_query = cng_get_posts_to_convert_query( $post_id, $max_num_to_convert );
if ( isset( $_GET['action'] ) ) {
if ( $_GET['action'] == 'list' ) {
cng_list_galleries($posts_to_convert_query);
} elseif ( $_GET['action'] == 'convert' ) {
cng_convert_galleries($posts_to_convert_query);
}
} else {
echo '<h3>' . $posts_to_convert_query->found_posts . ' posts with galleries to convert</h3>';
}
?>
<p><a class="" href="<?php echo cng_admin_url() . '&action=list' ?>">List galleries to convert</a></p>
<p><a class="button" href="<?php echo cng_admin_url() . '&action=convert' ?>">Convert all galleries</a></p>
</div>
<?php
});
});