function admin_screen()
    {
        $searches = \Pressbooks\Modules\SearchAndReplace\Search::get_searches();
        if (isset($_POST['search_pattern']) && !wp_verify_nonce($_POST['pressbooks-search-and-replace-nonce'], 'search')) {
            return;
        }
        $search_pattern = $replace_pattern = '';
        if (isset($_POST['search_pattern'])) {
            $search_pattern = stripslashes($_POST['search_pattern']);
        }
        if (isset($_POST['replace_pattern'])) {
            $replace_pattern = stripslashes($_POST['replace_pattern']);
        }
        $search_pattern = str_replace("\\'", "'", $search_pattern);
        $replace_pattern = str_replace("\\'", "'", $replace_pattern);
        $orderby = 'asc';
        if (isset($_POST['orderby']) && 'desc' === $_POST['orderby']) {
            $orderby = 'desc';
        }
        $limit = isset($_POST['limit']) ? intval($_POST['limit']) : 0;
        $offset = 0;
        $source = isset($_POST['source']) ? stripslashes($_POST['source']) : '';
        if (\Pressbooks\Modules\SearchAndReplace\Search::valid_search($source) && (isset($_POST['search']) || isset($_POST['replace']) || isset($_POST['replace_and_save']))) {
            $searcher = new $source();
            // Make sure no one sneaks in with a replace
            if (!current_user_can('administrator')) {
                unset($_POST['replace']);
                unset($_POST['replace_and_save']);
                $_POST['search'] = 'search';
            }
            $results = array();
            if (isset($_POST['search'])) {
                $results = $searcher->search_for_pattern($search_pattern, $limit, $offset, $orderby);
            } elseif (isset($_POST['replace'])) {
                $results = $searcher->search_and_replace($search_pattern, $replace_pattern, $limit, $offset, $orderby);
            } elseif (isset($_POST['replace_and_save'])) {
                $results = $searcher->search_and_replace($search_pattern, $replace_pattern, $limit, $offset, $orderby, true);
            }
            if (!is_array($results)) {
                $this->render_error($results);
            } elseif (isset($_POST['replace_and_save'])) {
                ?>
		  <div class="updated" id="message" onclick="this.parentNode.removeChild (this)">
		   <p><?php 
                printf(_n('%d occurrence replaced.', '%d occurrences replaced.', count($results)), count($results));
                ?>
</p>
		  </div>
<?php 
            }
            $this->render('search', array('search' => $search_pattern, 'replace' => $replace_pattern, 'searches' => $searches, 'source' => $source));
            if (is_array($results) && !isset($_POST['replace_and_save'])) {
                $this->render('results', array('search' => $searcher, 'results' => $results));
            }
        } else {
            $this->render('search', array('search' => $search_pattern, 'replace' => $replace_pattern, 'searches' => $searches, 'source' => $source));
        }
    }
예제 #2
0
 static function valid_search($class)
 {
     $classes = \Pressbooks\Modules\SearchAndReplace\Search::get_searches();
     foreach ($classes as $item) {
         if (strcasecmp(get_class($item), $class) === 0) {
             return true;
         }
     }
     return false;
 }