function ktzplg_get_bingrss_article($keyword, $num = 4, $related = false) { //Set query if any passed $keywords = isset($keyword) ? str_replace(' ', '+', $keyword) : ''; /* * This is BING XML where the picture come from. :D */ $xmlbing = array('http://www.bing.com/search?q=' . $keywords . '&go=&form=QBLH&filt=all&format=rss'); if ($xmlbing) { foreach ($xmlbing as $xmlbing) { $rss = fetch_feed($xmlbing); if (!is_wp_error($rss)) { # Checks that the object is created correctly // Figure out how many total items there are, but limit it to $num. $maxitems = $rss->get_item_quantity($num); // Build an array of all the items, starting with element $startfrom (first element). $bing_result = $rss->get_items(0, $maxitems); } $output = ''; if (!is_wp_error($rss)) { if ($maxitems != 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 ($bing_result as $bing_results) { $output .= '<li>'; $result_title = ktzplg_filter_badwords($bing_results->get_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>'; /* * ktzplg_plugin_clean_character find in _functions.php */ $result_desc = ktzplg_filter_badwords($bing_results->get_description()); $output .= '<p>' . ucfirst(ktzplg_plugin_clean_character($result_desc)) . '.</p>'; $output .= '</li>'; } $output .= '</ul>'; } else { $output .= '<p>'; foreach ($bing_result as $bing_results) { /* * ktzplg_plugin_clean_character find in _functions.php */ $result = ktzplg_filter_badwords($bing_results->get_description()); $output .= ucfirst(ktzplg_plugin_clean_character($result)) . '.'; } $output .= '</p>'; } } } } } return $output; }
/** * 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_permalink($keyword, $choice = 'default') { if ($choice == 'default') { $keyword = ktzplg_plugin_clean_character($keyword); $change = array('+', ' ', '--'); $searchpermalink = home_url('/') . strtolower(str_replace($change, '-', $keyword)) . '.html'; } elseif ($choice == 'search') { $keyword = ktzplg_plugin_clean_character($keyword); $change = array('+', ' ', '--'); $searchpermalink = '/search/'; $searchpermalink = home_url($searchpermalink) . '' . strtolower(str_replace($change, '-', $keyword)) . '.html'; } elseif ($choice == 'rand') { $keyword = ktzplg_plugin_clean_character($keyword); $change = array('+', ' ', '--'); // 9 character random $searchpermalink_rand = ktzplg_random_numalpha(9); $searchpermalink = '/' . $searchpermalink_rand . '/'; $searchpermalink = home_url($searchpermalink) . '' . strtolower(str_replace($change, '-', $keyword)) . '.html'; } elseif ($choice == 'first_word') { $keyword = ktzplg_plugin_clean_character($keyword); $change = array('+', ' ', '--'); // get first keyword $searchpermalink_first = ktzplg_first_word($keyword); $searchpermalink = '/' . $searchpermalink_first . '/'; $searchpermalink = home_url($searchpermalink) . '' . strtolower(str_replace($change, '-', $keyword)) . '.html'; } return $searchpermalink; }
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; }