PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/simple-tags/extras/simple-tags.importer.php

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