Ticket #4247: stp.php.2.patch

File stp.php.2.patch, 5.2 KB (added by joostdevalk, 5 years ago)

Patch with nonces added

  • wp-admin/import/stp.php

     
     1<?php 
     2 
     3class STP_Import { 
     4         
     5        function header()  { 
     6                echo '<div class="wrap">'; 
     7                echo '<h2>'.__('Import Simple Tagging').'</h2>'; 
     8                echo '<p>'.__('Steps may take a few minutes depending on the size of your database. Please be patient.').'<br /><br /></p>'; 
     9        } 
     10 
     11        function footer() { 
     12                echo '</div>'; 
     13        } 
     14 
     15        function greet() { 
     16                echo '<div class="narrow">'; 
     17                echo '<p>'.__('Howdy! This imports tags from an existing Simple Tagging 1.6.2 installation into this blog using the new WordPress native tagging structure.').'</p>'; 
     18                echo '<p>'.__('This has not been tested on any other versions of Simple Tagging. Mileage may vary.').'</p>'; 
     19                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 Simple Tagging habit. Just keep clicking along and we will let you know when you are in the clear!').'</p>'; 
     20                echo '<p><strong>'.__('Don&#8217;t be stupid - backup your database before proceeding!').'</strong></p>'; 
     21                echo '<form action="admin.php?import=stp&amp;step=1" method="post">'; 
     22                wp_nonce_field('import-utw');  
     23                echo '<p class="submit"><input type="submit" name="submit" value="'.__('Step 1 &raquo;').'" /></p>'; 
     24                echo '</form>'; 
     25                echo '</div>'; 
     26        } 
     27         
     28         
     29        function dispatch () { 
     30                if ( empty( $_GET['step'] ) ) { 
     31                        $step = 0; 
     32                } else { 
     33                        $step = (int) $_GET['step']; 
     34                } 
     35                 
     36                // load the header 
     37                $this->header(); 
     38                 
     39                switch ( $step ) { 
     40                        case 0 : 
     41                                $this->greet(); 
     42                                break; 
     43                        case 1 : 
     44                                check_admin_referer('import-utw'); 
     45                                $this->import_posts(); 
     46                                break; 
     47                        case 2: 
     48                                check_admin_referer('import-utw'); 
     49                                $this->import_t2p(); 
     50                                break; 
     51                        case 3: 
     52                                check_admin_referer('import-utw'); 
     53                                $this->cleanup_import(); 
     54                                break; 
     55                } 
     56                 
     57                // load the footer 
     58                $this->footer(); 
     59        } 
     60         
     61         
     62        function import_posts ( ) { 
     63                echo '<div class="narrow">'; 
     64                echo '<p><h3>'.__('Reading STP Post Tags&#8230;').'</h3></p>'; 
     65 
     66                // read in all the STP tag -> post settings 
     67                $posts = $this->get_stp_posts(); 
     68 
     69                // if we didn't get any tags back, that's all there is folks! 
     70                if ( !is_array($posts) ) { 
     71                        echo '<p>' . __('No posts were found to have tags!') . '</p>'; 
     72                        return false; 
     73                } 
     74                else { 
     75 
     76                        // if there's an existing entry, delete it 
     77                        if ( get_option('stpimp_posts') ) { 
     78                                delete_option('stpimp_posts'); 
     79                        } 
     80 
     81                        add_option('stpimp_posts', $posts); 
     82                                 
     83 
     84                        $count = count($posts); 
     85                                 
     86                        echo '<p>' . sprintf( __('Done! <strong>%s</strong> tag to post relationships were read.'), $count ) . '<br /></p>'; 
     87                                 
     88                } 
     89 
     90                echo '<form action="admin.php?import=stp&amp;step=2" method="post">'; 
     91                wp_nonce_field('import-utw');  
     92                echo '<p class="submit"><input type="submit" name="submit" value="'.__('Step 2 &raquo;').'" /></p>'; 
     93                echo '</form>'; 
     94                echo '</div>'; 
     95 
     96        } 
     97         
     98         
     99        function import_t2p ( ) { 
     100 
     101                echo '<div class="narrow">'; 
     102                echo '<p><h3>'.__('Adding Tags to Posts&#8230;').'</h3></p>'; 
     103 
     104                // run that funky magic! 
     105                $tags_added = $this->tag2post(); 
     106                 
     107                echo '<p>' . sprintf( __('Done! <strong>%s</strong> tags where added!'), $tags_added ) . '<br /></p>'; 
     108 
     109                echo '<form action="admin.php?import=stp&amp;step=3" method="post">'; 
     110                wp_nonce_field('import-utw');  
     111                echo '<p class="submit"><input type="submit" name="submit" value="'.__('Step 3 &raquo;').'" /></p>'; 
     112                echo '</form>'; 
     113                echo '</div>'; 
     114 
     115        } 
     116         
     117         
     118        function get_stp_posts ( ) { 
     119                 
     120                global $wpdb; 
     121                 
     122                // read in all the posts from the STP post->tag table: should be wp_post2tag 
     123                $posts_query = "SELECT post_id, tag_name FROM " . $wpdb->prefix . "stp_tags"; 
     124                 
     125                $posts = $wpdb->get_results($posts_query); 
     126                 
     127                return $posts; 
     128                 
     129        } 
     130         
     131         
     132        function tag2post ( ) { 
     133                 
     134                // get the tags and posts we imported in the last 2 steps 
     135                $posts = get_option('stpimp_posts'); 
     136                 
     137                // null out our results 
     138                $tags_added = 0; 
     139                 
     140                // loop through each post and add its tags to the db 
     141                foreach ( $posts as $this_post ) { 
     142                         
     143                        $the_post = (int) $this_post->post_id; 
     144                        $the_tag = $this_post->tag_name; 
     145 
     146                        // try to add the tag 
     147                        wp_add_post_tags($the_post, $the_tag); 
     148 
     149                        $tags_added++; 
     150                         
     151                } 
     152                 
     153                // that's it, all posts should be linked to their tags properly, pending any errors we just spit out! 
     154                return $tags_added; 
     155                 
     156                 
     157        } 
     158         
     159         
     160        function cleanup_import ( ) { 
     161                 
     162                delete_option('stpimp_posts'); 
     163                 
     164                $this->done(); 
     165                 
     166        } 
     167         
     168         
     169        function done ( ) { 
     170                 
     171                echo '<div class="narrow">'; 
     172                echo '<p><h3>'.__('Import Complete!').'</h3></p>'; 
     173                 
     174                echo '<p>' . __('OK, so we lied about this being a 4-step program! You&#8217;re done!') . '</p>'; 
     175                 
     176                echo '<p>' . __('Now wasn&#8217;t that easy?') . '</p>'; 
     177                 
     178                echo '</div>'; 
     179                 
     180        } 
     181         
     182 
     183        function STP_Import ( ) { 
     184                 
     185                // Nothing. 
     186                 
     187        } 
     188         
     189} 
     190 
     191 
     192// create the import object 
     193$stp_import = new STP_Import(); 
     194 
     195// add it to the import page! 
     196register_importer('stp', 'Simple Tagging', __('Import Simple Tagging tags into the new native tagging structure.'), array($stp_import, 'dispatch')); 
     197 
     198?>