Esempio n. 1
0
/**
 * Options page
 *
 * @since 1.0
 *
 * @return output
 */
function gcse_options_page()
{
    $options = get_option('gcse_options');
    $response = gcse_request();
    $errors = array('keyInvalid' => __('Invalid API key.'), 'invalid' => __('Invalid Custom Search Engine ID.'), 'accessNotConfigured' => __('"Custom Search API" service isn\'t enabled in the APIs Console.'));
    if ($response && isset($response['error'])) {
        $reason = $response['error']['errors'][0]['reason'];
        $error = array_key_exists($reason, $errors) ? $errors[$reason] : $reason;
    }
    preg_match('/^[0-9]+\\.?[0-9]+/', get_bloginfo('version'), $versionCheck);
    ?>
    <div class="wrap">
        <div id="icon-options-general" class="icon32"></div>
        <h2><?php 
    _e('Google Custom Search Engine Settings');
    ?>
</h2>

        <?php 
    if ($versionCheck && $versionCheck[0] < 3.3) {
        ?>
            <div class="error below-h2">
                <p><strong><?php 
        _e('ERROR');
        ?>
</strong>: <?php 
        _e('You are running a to old version of WordPress. This plugin requires at least 3.3.');
        ?>
            </div>
        <?php 
    }
    ?>

        <?php 
    if (isset($error) && $error) {
        ?>
            <div class="error below-h2">
                <p><strong><?php 
        _e('ERROR');
        ?>
</strong>: <?php 
        echo $error;
        ?>
</p>
            </div>
        <?php 
    }
    ?>

        <div class="narrow">
            <form action="options.php" method="post">
                <?php 
    settings_fields('gcse_options');
    ?>

                <table class="form-table">
                    <tr valign="top">
                        <th scope="row"><label for="gcse_options[key]">Google API key</label></th>
                        <td>
                            <input type="text" size="50" id="gcse_options[key]" name="gcse_options[key]" value="<?php 
    echo isset($options['key']) ? $options['key'] : '';
    ?>
" /><br />
                            <span class="description">Enable the Custom Search API and get your API key at <a href="https://code.google.com/apis/console/" title="Google APIs Console" target="_blank" />Google APIs Console</a>.</span>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row"><label for="gcse_options[id]">Google Custom Search Engine</label></th>
                        <td>
                            <input type="text" size="50" id="gcse_options[id]" name="gcse_options[id]" value="<?php 
    echo isset($options['id']) ? $options['id'] : '';
    ?>
" />
                            <span class="description">Search engine ID (CX)<br />Visit <a href="http://www.google.com/cse/">Google Custom Search Engine</a> to create or manage your existing search engines.</span>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row"><label for="gcse_options[match]">Disable matching</label></th>
                        <td>
                            <input type="checkbox" value="1" id="gcse_options[match]" name="gcse_options[match]"<?php 
    echo isset($options['match']) ? ' checked="checked"' : '';
    ?>
 />
                            <span class="description">Each item in the search result will <strong>not</strong> try to match with content on this site. If a match is found, the title, excerpt etc. will be displayed from WordPress (not Google) for that post.</span>
                        </td>
                    </tr>
                </table>

                <p class="submit">
                    <input type="submit" class="button-primary" value="<?php 
    _e('Save Settings');
    ?>
" />
                </p>
            </form>
        </div>
    </div>
    <?php 
}
/**
 * Search Results
 *
 * @since 1.0
 *
 */
function gcse_results($posts, $q)
{
    if ($q->is_single !== true && $q->is_search === true) {
        global $wp_query;
        $response = gcse_request();
        if (isset($response['items']) && $response['items']) {
            $results = array();
            $options = get_option('gcse_options');
            foreach ($response['items'] as $result) {
                if (!isset($options['match']) && ($id = gcse_url_to_postid($result['link']))) {
                    $post = get_post($id);
                } else {
                    $mime = false;
                    if (!empty($result['mime'])) {
                        switch ($result['mime']) {
                            case "application/pdf":
                                $mime = "PDF";
                                break;
                            case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
                            case "application/vnd.openxmlformats-officedocument.presentationml.template":
                            case "application/vnd.openxmlformats-officedocument.presentationml.slideshow":
                            case "application/vnd.ms-powerpoint.addin.macroEnabled.12":
                            case "application/vnd.ms-powerpoint.presentation.macroEnabled.12":
                            case "application/vnd.ms-powerpoint.template.macroEnabled.12":
                            case "application/vnd.ms-powerpoint.slideshow.macroEnabled.12":
                            case "application/vnd.ms-powerpoint":
                                $mime = "PPT";
                                break;
                            case "application/msword":
                            case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
                            case "application/vnd.openxmlformats-officedocument.wordprocessingml.template":
                            case "application/vnd.ms-word.document.macroEnabled.12":
                            case "application/vnd.ms-word.template.macroEnabled.12":
                                $mime = "DOC";
                                break;
                            case "application/vnd.ms-excel":
                            case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
                            case "application/vnd.openxmlformats-officedocument.spreadsheetml.template":
                            case "application/vnd.ms-excel.sheet.macroEnabled.12":
                            case "application/vnd.ms-excel.template.macroEnabled.12":
                            case "application/vnd.ms-excel.addin.macroEnabled.12":
                            case "application/vnd.ms-excel.sheet.binary.macroEnabled.12":
                                $mime = "XLS";
                                break;
                        }
                    }
                    $post = (object) array('post_type' => 'page', 'post_title' => $result['title'], 'post_author' => '', 'post_date' => '', 'post_status' => 'published', 'post_excerpt' => $result['snippet'], 'post_content' => $result['htmlSnippet'], 'guid' => $result['link'], 'post_type' => 'search', 'ID' => 0, 'comment_status' => 'closed', 'mime' => $mime);
                    // Adding in the featured image. You can use it if you'd like.
                    if (isset($result['pagemap']) && isset($result['pagemap']['cse_image']['0'])) {
                        $post->cse_img = $result['pagemap']['cse_image'][0]['src'];
                    }
                }
                $results[] = $post;
            }
            $post = '';
            // Set results as posts
            $posts = $results;
            $results = '';
            // Update post count
            $wp_query->post_count = count($posts);
            $wp_query->found_posts = $response['searchInformation']['totalResults'];
            // Pagination
            $posts_per_page = $wp_query->query_vars['posts_per_page'] < 11 ? $wp_query->query_vars['posts_per_page'] : 10;
            $wp_query->max_num_pages = ceil($response['searchInformation']['totalResults'] / $posts_per_page);
            // Apply filters
            add_filter('the_permalink', 'gcse_permalink');
        }
    }
    return $posts;
}