function ktzplg_get_bing_article($keyword, $num = 4, $related = false)
 {
     //Set query if any passed
     $keywords = isset($keyword) ? str_replace(' ', '+', $keyword) : '';
     /* 
      * @str_get_html Simple HTML DOM and get url via ktzplg_fetch_agc ( curl and fopen )
      * ktzplg_fetch_agc find in _functions.php
      */
     $fetch = ktzplg_fetch_agc('http://www.bing.com/search?q=' . $keywords);
     $html = new simple_html_dom();
     $html->load($fetch);
     //Simple HTML now use the result craped by cURL.
     $result = array();
     if ($html && is_object($html)) {
         foreach ($html->find('li.b_algo') as $g) {
             /*
              * each search results are in a list item with a class name 'g'
              * we are seperating each of the elements within, into an array
              * Summaries are stored in a p
              * Summaries are stored in h2 a
              */
             $item['title'] = isset($g->find('h2 a', 0)->innertext) ? $g->find('h2 a', 0)->innertext : '';
             $item['description'] = isset($g->find('p', 0)->innertext) ? $g->find('p', 0)->innertext : '';
             $result[] = $item;
         }
         // Clean up memory
         $html->clear();
         unset($html);
         /* 
          * Otherwise it prints out the array structure so that it
          * is more human readible. You could instead perform a 
          * foreach loop on the variable $result so that you can 
          * organize the html output, or insert the data into a database
          */
         $output = '';
         if ($result) {
             $i = 0;
             if ($related == true) {
                 $output .= '<h3 class="ktzplg-title-related">' . __('Related posts to ', 'ktzagcplugin');
                 $output .= $keyword;
                 $output .= '</h3>';
                 $output .= '<ul class="ktzplg-list-agcplugin">';
                 foreach ($result as $r) {
                     $output .= '<li>';
                     if (!empty($r['title'])) {
                         $result_title = ktzplg_filter_badwords($r['title']);
                         $result_title = sanitize_title($result_title);
                         $result_title = str_replace('-', ' ', $result_title);
                         $result_title = ktzplg_plugin_clean_character($result_title);
                         $result_title = ucwords($result_title);
                         $output .= '<h3><a href="';
                         $output .= ktzplg_permalink($result_title, $choice = 'default');
                         $output .= '" title="' . __('Permalink to ', 'ktzagcplugin') . $result_title . '">';
                         $output .= $result_title;
                         $output .= '</a></h3>';
                     }
                     if (!empty($r['description'])) {
                         $result_desc = ktzplg_filter_badwords($r['description']);
                         $output .= '<p>' . ucfirst(ktzplg_plugin_clean_character($result_desc)) . '.</p>';
                     }
                     $output .= '</li>';
                     if (++$i == $num) {
                         break;
                     }
                 }
                 $output .= '</ul>';
             } else {
                 $output .= '<p>';
                 foreach ($result as $r) {
                     if (!empty($r['description'])) {
                         # ktzplg_plugin_clean_character find in _functions.php
                         $result = ktzplg_filter_badwords($r['description']);
                         $output .= ucfirst(ktzplg_plugin_clean_character($result)) . '.';
                     }
                     if (++$i == $num) {
                         break;
                     }
                 }
                 $output .= '</p>';
             }
         }
     }
     return $output;
 }
 function ktzplg_get_ask_video($keyword, $num = 1)
 {
     //Set query if any passed
     $keywords = isset($keyword) ? str_replace(' ', '+', $keyword) : '';
     /* 
      * @str_get_html Simple HTML DOM and get url via ktzplg_fetch_agc ( curl and fopen )
      * ktzplg_fetch_agc find in _functions.php
      */
     $fetch = ktzplg_fetch_agc('http://www.ask.com/youtube?q=' . $keywords);
     $html = new simple_html_dom();
     $html->load($fetch);
     //Simple HTML now use the result craped by cURL.
     $result = array();
     if ($html && is_object($html)) {
         foreach ($html->find('div.video-result') as $g) {
             /*
              * each search results are in a list item with a class name 'g'
              * we are seperating each of the elements within, into an array
              * Summaries are stored in a p with a classname of 'web-result-description'
              */
             $item['videourl'] = isset($g->find('.video-result-thumbnail-link', 0)->href) ? $g->find('.video-result-thumbnail-link', 0)->href : '';
             $result[] = $item;
         }
         // Clean up memory
         $html->clear();
         unset($html);
         /* 
          * Otherwise it prints out the array structure so that it
          * is more human readible. You could instead perform a 
          * foreach loop on the variable $result so that you can 
          * organize the html output, or insert the data into a database
          */
         $output = '';
         if ($result) {
             $i = 0;
             $output .= '<p>';
             foreach ($result as $r) {
                 if (!empty($r['videourl'])) {
                     // get youtube id with function, please see above function
                     $youtube_id = ktzplg_get_youtube_id_from_url($r['videourl']);
                     # ktzplg_plugin_clean_character find in _functions.php
                     $output .= '<div class="ktzplg-responsive-video"><iframe height="352" width="625" src="https://www.youtube.com/embed/' . $youtube_id . '" frameborder="0" allowfullscreen></iframe></div>';
                 }
                 if (++$i == $num) {
                     break;
                 }
             }
             $output .= '</p>';
         }
     }
     return $output;
 }
Beispiel #3
0
/**
 * Google API Request
 * @param boolean $test
 * @return array
 */
function ktzplg_search_request($posts, $q)
{
    if ($q->is_single !== true && $q->is_search === true) {
        global $wp_query;
        $query = html_entity_decode(get_search_query());
        # Set query if any passed
        $keywords = isset($query) ? str_replace(' ', '+', $query) : '';
        /* 
         * @str_get_html Simple HTML DOM and get url via ktzplg_fetch_agc ( curl and fopen )
         * ktzplg_fetch_agc find in _functions.php
         */
        # option source AGC in search page
        $agcsource_option = Ktzagcplugin_Option::get_option('ktzplg_agc_searchresults');
        if ($agcsource_option == 'bing') {
            $fetch = ktzplg_fetch_agc('http://www.bing.com/search?q=' . $keywords);
        } elseif ($agcsource_option == 'ask') {
            $fetch = ktzplg_fetch_agc('http://www.ask.com/web?q=' . $keywords);
        } else {
            $fetch = ktzplg_fetch_agc('http://www.google.com/search?hl=en&q=' . $keywords);
        }
        $html = new simple_html_dom();
        $html->load($fetch);
        //Simple HTML now use the result craped by cURL.
        $result = array();
        if ($html && is_object($html)) {
            if ($agcsource_option == 'bing') {
                foreach ($html->find('li.b_algo') as $g) {
                    /*
                     * each search results are in a list item with a class name 'g'
                     * we are seperating each of the elements within, into an array
                     * Summaries are stored in a p
                     * Summaries are stored in h2 a
                     */
                    $item['title'] = isset($g->find('h2 a', 0)->innertext) ? $g->find('h2 a', 0)->innertext : '';
                    $item['description'] = isset($g->find('p', 0)->innertext) ? $g->find('p', 0)->innertext : '';
                    $result[] = $item;
                }
            } elseif ($agcsource_option == 'ask') {
                foreach ($html->find('div.web-result') as $g) {
                    /*
                     * each search results are in a list item with a class name 'g'
                     * we are seperating each of the elements within, into an array
                     * Summaries are stored in a p with a classname of 'web-result-description'
                     * title are stored in a with a classname of 'web-result-title-link'
                     */
                    $item['title'] = isset($g->find('a.web-result-title-link', 0)->innertext) ? $g->find('a.web-result-title-link', 0)->innertext : '';
                    $item['description'] = isset($g->find('.web-result-description', 0)->innertext) ? $g->find('.web-result-description', 0)->innertext : '';
                    $result[] = $item;
                }
            } else {
                foreach ($html->find('div.g') as $g) {
                    /*
                     * each search results are in a list item with a class name 'g'
                     * we are seperating each of the elements within, into an array
                     * Summaries are stored in a span with a classname of 'st'
                     * Title are stored in a h3 with a classname of 'r'
                     */
                    $item['title'] = isset($g->find('h3.r', 0)->innertext) ? $g->find('h3.r', 0)->innertext : '';
                    $item['description'] = isset($g->find('span.st', 0)->innertext) ? $g->find('span.st', 0)->innertext : '';
                    $result[] = $item;
                }
            }
            # endif $agcsource_option
            // Clean up memory
            $html->clear();
            unset($html);
            /* 
             * Otherwise it prints out the array structure so that it
             * is more human readible. You could instead perform a 
             * foreach loop on the variable $result so that you can 
             * organize the html output, or insert the data into a database
             */
            if ($result) {
                $results = array();
                $i = 0;
                foreach ($result as $r) {
                    $post = get_post($ID);
                    $result_title = ktzplg_filter_badwords($r['title']);
                    $result_title = sanitize_title($result_title);
                    $result_title = str_replace('-', ' ', $result_title);
                    $result_title = ktzplg_plugin_clean_character($result_title);
                    $result_title = ucwords($result_title);
                    $result_desc = ktzplg_filter_badwords($r['description']);
                    $format = get_option('date_format') . ' ' . get_option('time_format');
                    $post_date = date_i18n($format, current_time('timestamp'));
                    if (empty($post)) {
                        $post = (object) array('post_type' => 'post', 'post_title' => $result_title, 'post_date' => $post_date, 'post_category' => 1, 'post_author' => 1, 'post_status' => 'published', 'post_excerpt' => $result_desc, 'post_content' => $result_desc, 'guid' => ktzplg_permalink($result_title, $choice = 'default'), 'ID' => 0, 'comment_status' => 'closed');
                    }
                    if (++$i == 8) {
                        break;
                    }
                    $results[] = $post;
                }
                $post = '';
                // Set results as posts
                $posts = $results;
                $results = '';
                // Update post count
                $wp_query->post_count = count($posts);
                $wp_query->found_posts = 8;
                // Apply filters
                add_filter('the_permalink', 'ktzplg_search_permalink');
                add_filter('post_link', 'ktzplg_search_permalink');
            }
        }
    }
    return $posts;
}
 function ktzplg_get_ask_image($keyword, $max_keyword = '', $num = 1, $related = false)
 {
     //Set query if any passed
     $keywords = isset($keyword) ? str_replace(' ', '+', $keyword) : '';
     // Add maximal number keyword in search query
     if ($max_keyword != '') {
         $max_keywords = (int) $max_keyword;
         $keywords_5_first = isset($keyword) ? implode(' ', array_splice(explode(' ', $keyword), 0, $max_keywords)) : '';
         $keywords = isset($keywords_5_first) ? str_replace(array(' ', '-'), '+', $keywords_5_first) : '';
     }
     /* 
      * @str_get_html Simple HTML DOM and get url via ktzplg_fetch_agc ( curl and fopen )
      * ktzplg_fetch_agc find in _functions.php
      */
     $fetch = ktzplg_fetch_agc('http://www.ask.com/pictures?q=' . $keywords);
     $html = new simple_html_dom();
     $html->load($fetch);
     //Simple HTML now use the result craped by cURL.
     $result = array();
     if ($html && is_object($html)) {
         foreach ($html->find('div[class="picturesContainer"] div[class="picturesItem"]') as $gm) {
             /*
              * each search results are in a list item with a class name '$gm'
              * we are seperating each of the elements within, into an array
              */
             # Find element a with data-imageurl attribute where image url can find there
             $get_href_attr = $gm->getAttribute('data-imageurl');
             # Find element a with data-imagetitle attribute where image title can find there
             $get_title_attr = $gm->getAttribute('data-imagetitle');
             # Get query imgrefurl
             $item['imgsrc'] = $get_href_attr;
             $item['title'] = $get_title_attr;
             $result[] = $item;
         }
         // Clean up memory
         $html->clear();
         unset($html);
         /* 
          * Otherwise it prints out the array structure so that it
          * is more human readible. You could instead perform a 
          * foreach loop on the variable $result so that you can 
          * organize the html output, or insert the data into a database
          */
         $output = '';
         if ($result) {
             $i = 0;
             if ($related == true) {
                 $output .= '<h3 class="ktzplg-title-related">' . __('Related images to ', 'ktzagcplugin');
                 $output .= $keyword;
                 $output .= '</h3>';
                 $output .= '<ul class="ktzplg-list-image-agcplugin">';
                 foreach ($result as $r) {
                     $output .= '<li>';
                     if (!empty($r['imgsrc'])) {
                         $result_title = ktzplg_filter_badwords($r['title']);
                         $result_title = sanitize_title($result_title);
                         $result_title = str_replace('-', ' ', $result_title);
                         $result_title = ktzplg_plugin_clean_character($result_title);
                         $result_title = ucwords($result_title);
                         $output .= '<a href="';
                         $output .= ktzplg_permalink($result_title, $choice = 'default');
                         $output .= '" title="' . __('Permalink to ', 'ktzagcplugin') . $result_title . '">';
                         $output .= '<img src="';
                         if (!empty($r['imgsrc'])) {
                             $output .= $r['imgsrc'];
                         }
                         $output .= '"';
                         if (!empty($r['title'])) {
                             $output .= ' alt="' . $result_title . '" title="' . $result_title . '"';
                         }
                         $output .= ' />';
                         $output .= '</a>';
                     }
                     $output .= '</li>';
                     if (++$i == $num) {
                         break;
                     }
                 }
                 $output .= '</ul>';
             } else {
                 foreach ($result as $r) {
                     $output .= '<div class="wp-caption aligncenter">';
                     $output .= '<img src="';
                     if (!empty($r['imgsrc'])) {
                         $output .= $r['imgsrc'];
                     }
                     $output .= '"';
                     if (!empty($r['title'])) {
                         $result_title = ktzplg_filter_badwords($r['title']);
                         $result_title = sanitize_title($result_title);
                         $result_title = str_replace('-', ' ', $result_title);
                         $result_title = ktzplg_plugin_clean_character($result_title);
                         $result_title = ucwords($result_title);
                         $output .= ' alt="' . $result_title . '" title="' . $result_title . '"';
                     }
                     $output .= ' />';
                     if (!empty($r['title'])) {
                         $result_title = ktzplg_filter_badwords($r['title']);
                         $result_title = sanitize_title($result_title);
                         $result_title = str_replace('-', ' ', $result_title);
                         $result_title = ktzplg_plugin_clean_character($result_title);
                         $result_title = ucwords($result_title);
                         $output .= '<p class="wp-caption-text">' . $result_title . '</p>';
                     }
                     $output .= '</div>';
                     if (++$i == $num) {
                         break;
                     }
                 }
             }
         }
     }
     return $output;
 }