source: trunk/www.guidonia.net/wp/wp-content/plugins/simple-tags/extras/simple-tags.importer.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 8.2 KB
Line 
1<?php
2/*
3&copy; Copyright 2007 Amaury BALMER (balmer.amaury@gmail.com)
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14*/
15
16Class EmbeddedImporter {
17 /**
18 * Constructor = nothing
19 *
20 * @return EmbeddedImporter
21 */
22 function EmbeddedImporter () {}
23
24 /**
25 * Select good page depending URL
26 *
27 */
28 function dispatch () {
29 $step = ( empty($_GET['step']) ) ? 0 : (int) $_GET['step'];
30
31 // load the header
32 $this->header();
33
34 // load the page
35 switch ( $step ) {
36 case 0 :
37 $this->greet();
38 break;
39 case 1 :
40 $this->importer();
41 break;
42 case 2:
43 $this->done();
44 break;
45 }
46
47 // load the footer
48 $this->footer();
49 }
50
51 /**
52 * Print header importer
53 *
54 */
55 function header() {
56 echo '<div class="wrap">';
57 echo '<h2>'.__('Import Embedded Tags', 'simpletags').'</h2>';
58 echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.', 'simpletags').'</p>';
59 echo '<p>'.__('Visit the <a href="http://www.herewithme.fr/wordpress-plugins/simple-tags">plugin\'s homepage</a> for further details. If you find a bug, or have a fantastic idea for this plugin, <a href="mailto:amaury@wordpress-fr.net">ask me</a> !', 'simpletags').'</p>';
60 }
61
62 /**
63 * Print avertissement before import (backup DB !)
64 *
65 */
66 function greet() {
67 echo '<div class="narrow">';
68 echo '<p>'.__('Howdy! This imports tags from embedded tags into this blog using the new WordPress native tagging structure.', 'simpletags').'</p>';
69 echo '<p>'.__('To accommodate larger databases for those tag-crazy authors out there, we have made this into an easy 4-step program to help you kick that nasty Embedded Tags habit. Just keep clicking along and we will let you know when you are in the clear!', 'simpletags').'</p>';
70 echo '<p><strong>'.__('Don&#8217;t be stupid - backup your database before proceeding!', 'simpletags').'</strong></p>';
71 echo '<form action="admin.php?import=simple-tags.importer&amp;step=1" method="post">';
72 echo '<p class="submit"><input type="submit" name="submit" value="'.__('Step 1 &raquo;', 'simpletags').'" /></p>';
73 echo '</form>';
74 echo '</div>';
75 }
76
77 /**
78 * Importer with dynamic pages for skip timeout
79 *
80 */
81 function importer() {
82 $action = false;
83 if ( $_GET['action'] == 'import_embedded_tag' ) {
84 $action = true;
85
86 // Get values
87 $n = ( isset($_GET['n']) ) ? intval($_GET['n']) : 0;
88 $start = $_GET['start'];
89 $end = $_GET['end'];
90 $clean = ( isset($_GET['clean']) ) ? 1 : 0;
91 $typep = ( isset($_GET['typep']) ) ? 1 : 0;
92 $type_posts = ( isset($_GET['typep']) ) ? "post_type IN('page', 'post')" : "post_type = 'post'";
93
94 if ( empty($_GET['start']) || empty($_GET['end']) ) {
95 wp_die(__('Missing parameters in URL. (Start or End)', 'simpletags'));
96 }
97
98 // Get datas
99 global $wpdb;
100 $objects = $wpdb->get_results( "SELECT ID, post_title, post_content FROM {$wpdb->posts} WHERE {$type_posts} ORDER BY ID DESC LIMIT {$n}, 20" );
101 }
102
103 // First step
104 if ( $action === false ) : ?>
105
106 <div class="narrow">
107 <h3><?php _e('Configure and add tags to posts&#8230;', 'simpletags'); ?></h3>
108
109 <form action="admin.php" method="get">
110 <input type="hidden" name="import" value="simple-tags.importer" />
111 <input type="hidden" name="step" value="1" />
112 <input type="hidden" name="action" value="import_embedded_tag" />
113
114 <p><label for="start"><?php _e('Start marker', 'simpletags'); ?></label><br />
115 <input type="text" value="[tags]" name="start" id="start" size="10" /></p>
116
117 <p><label for="end"><?php _e('End marker', 'simpletags'); ?></label><br />
118 <input type="text" value="[/tags]" name="end" id="end" size="10" /></p>
119
120 <p><input type="checkbox" value="1" id="clean" name="clean" /> <label for="clean"><?php _e('Delete embedded tags once imported ?', 'simpletags'); ?></label></p>
121
122 <p><input type="checkbox" value="1" id="typep" name="typep" /> <label for="typep"><?php _e('Import also embedded tags from page ?', 'simpletags'); ?></label></p>
123
124 <p class="submit">
125 <input type="submit" name="submit" value="<?php _e('Start import &raquo;', 'simpletags'); ?>" /></p>
126 </form>
127 </div>
128
129 <?php else: // Dynamic pages
130
131 echo '<div class="narrow">';
132
133 if( !empty($objects) ) {
134
135 echo '<ul>';
136 foreach( (array) $objects as $object ) {
137 // Return Tags
138 preg_match_all('/(' . $this->regexEscape($start) . '(.*?)' . $this->regexEscape($end) . ')/is', $object->post_content, $matches);
139
140 $tags = array();
141 foreach ( (array) $matches[2] as $match) {
142 foreach( (array) explode(',', $match) as $tag) {
143 $tags[] = $tag;
144 }
145 }
146
147 if( !empty($tags) ) {
148 // Remove empty and duplicate elements
149 $tags = array_filter($tags, array(&$this, 'deleteEmptyElement'));
150 $tags = array_unique($tags);
151
152 wp_set_post_tags( $object->ID, $tags, true ); // Append tags
153
154 if ( $clean == '1' ) {
155 // remove embedded tags
156 $new_content = preg_replace('/(' . $this->regexEscape($start) . '(.*?)' . $this->regexEscape($end) . ')/is', '', $object->post_content);
157 $new_content = addslashes_gpc($new_content);
158 $wpdb->query("UPDATE {$wpdb->posts} SET post_content = '{$new_content}' WHERE ID = {$object->ID} LIMIT 1");
159 }
160 }
161
162 echo '<li>#'. $object->ID .' '. $object->post_title .'</li>';
163 unset($tags, $object, $matches, $match, $new_content);
164 }
165 echo '</ul>';
166 ?>
167 <p><?php _e("If your browser doesn't start loading the next page automatically click this link:", 'simpletags'); ?> <a href="admin.php?import=simple-tags.importer&amp;step=1&amp;action=import_embedded_tag&amp;typep=<?php echo $typep; ?>&amp;start=<?php echo $start; ?>&amp;end=<?php echo $end; ?>&amp;clean=<?php echo $clean; ?>&amp;n=<?php echo ($n + 20) ?>"><?php _e('Next content', 'simpletags'); ?></a></p>
168 <script type="text/javascript">
169 <!--
170 function nextPage() {
171 location.href = '<?php get_option('siteurl'); ?>/wp-admin/admin.php?import=simple-tags.importer&step=1&action=import_embedded_tag&typep=<?php echo $typep; ?>&start=<?php echo $start; ?>&end=<?php echo $end; ?>&clean=<?php echo $clean; ?>&n=<?php echo ($n + 20) ?>';
172 }
173 setTimeout( 'nextPage', 250 );
174 //-->
175 </script>
176 <?php
177
178 } else { // end
179
180 echo '<p><strong>'.__('Done!', 'simpletags').'</strong><br /></p>';
181 echo '<form action="admin.php?import=simple-tags.importer&amp;step=2" method="post">';
182 echo '<p class="submit"><input type="submit" name="submit" value="'.__('Step 2 &raquo;', 'simpletags').'" /></p>';
183 echo '</form>';
184
185 }
186 echo '</div>';
187
188 endif; ?>
189 </div>
190 <?php
191 }
192
193 /**
194 * Print end message importer
195 *
196 */
197 function done() {
198 echo '<div class="narrow">';
199 echo '<p><h3>'.__('Import Complete!', 'simpletags').'</h3></p>';
200 echo '<p>' . __('OK, so we lied about this being a 4-step program! You&#8217;re done!', 'simpletags') . '</p>';
201 echo '<p>' . __('Now wasn&#8217;t that easy?', 'simpletags') . '</p>';
202 echo '<p><strong>' . __('You can manage tags now !', 'simpletags') . '</strong></p>';
203 echo '</div>';
204 }
205
206 /**
207 * Print footer importer
208 *
209 */
210 function footer() {
211 echo '</div>';
212 }
213
214 /**
215 * Escape string so that it can used in Regex. E.g. used for [tags]...[/tags]
216 *
217 * @param string $content
218 * @return string
219 */
220 function regexEscape( $content ) {
221 return strtr($content, array("\\" => "\\\\", "/" => "\\/", "[" => "\\[", "]" => "\\]"));
222 }
223
224 /**
225 * trim and remove empty element
226 *
227 * @param string $element
228 * @return string
229 */
230 function deleteEmptyElement( &$element ) {
231 $element = trim($element);
232 if ( !empty($element) ) {
233 return $element;
234 }
235 }
236}
237
238// create the import object
239$embedded_importer = new EmbeddedImporter();
240
241// add it to the import page!
242register_importer('simple-tags.importer', __('Embedded Tags', 'simpletags'), __('Import Embedded Tags into the new WordPress native tagging structure.', 'simpletags'), array(&$embedded_importer, 'dispatch'));
243?>
Note: See TracBrowser for help on using the repository browser.