function get_elastic_crp_posts($args = array()) { global $post; // Are we matching only the title or the post content as well? $match_fields = array('post_title'); $match_fields_content = array($post->post_title); if ($args['match_content']) { $match_fields[] = 'post_content'; $match_fields_content[] = crp_excerpt($post->ID, $args['match_content_words'], false); } // Limit the related posts by time $current_time = current_time('timestamp', 0); $from_date = $current_time - $args['daily_range'] * DAY_IN_SECONDS; $from_date = gmdate('Y-m-d H:i:s', $from_date); $limit = $args['strict_limit'] ? $args['limit'] : $args['limit'] * 3; $client = \Grupoadslzone\Singletons\ElasticSearchClient::i(); /*$clientBuilder = Elasticsearch\ClientBuilder::create(); $clientBuilder->setHosts(array('http://127.0.0.1:9200')); $client = $clientBuilder->build();*/ require_once plugin_dir_path(__FILE__) . 'includes/SearchQuery.php'; $search_query = new SearchQuery(); $search_query->setParamsByWPPost($post); $params = ['index' => ep_get_index_name(), 'type' => 'post', 'body' => ['_source' => 'post_id', 'query' => ['more_like_this' => ["fields" => $search_query->getFields(), "like_text" => $search_query->getSearchQuery(), "min_term_freq" => $search_query->getMinTermFreq(), "max_query_terms" => $search_query->getMaxQueryTerms(), "min_word_length" => 2, "minimum_should_match" => $search_query->getMinimumShouldMatch(), "boost" => 9, "boost_terms" => 4]], 'filter' => ["bool" => ["should" => ['range' => ['post_date' => ['from' => $from_date]]], 'must_not' => ['term' => ["post.post_id" => $post->ID]]]], 'from' => 0, 'size' => $limit]]; //$ret = json_encode($params['body']); $response = $client->search($params); $results = $response['hits']['hits']; return $results; }
/** * Fetch related posts IDs. * * @since 1.9 * * @param array $args * @return object $results */ function get_crp_posts_id($args = array()) { global $wpdb, $post, $single, $crp_settings; // Initialise some variables $fields = ''; $where = ''; $join = ''; $groupby = ''; $orderby = ''; $having = ''; $limits = ''; $match_fields = ''; $defaults = array('postid' => FALSE, 'strict_limit' => TRUE); $defaults = array_merge($defaults, $crp_settings); // Parse incoming $args into an array and merge it with $defaults $args = wp_parse_args($args, $defaults); // Declare each item in $args as its own variable i.e. $type, $before. extract($args, EXTR_SKIP); // Fix the thumb size in case it is missing $crp_thumb_size = crp_get_all_image_sizes($thumb_size); if (isset($crp_thumb_size['width'])) { $thumb_width = $crp_thumb_size['width']; $thumb_height = $crp_thumb_size['height']; } if (empty($thumb_width)) { $thumb_width = $crp_settings['thumb_width']; } if (empty($thumb_height)) { $thumb_height = $crp_settings['thumb_height']; } $post = empty($postid) ? $post : get_post($postid); $limit = $strict_limit ? $limit : $limit * 3; parse_str($post_types, $post_types); // Save post types in $post_types variable /** * Filter the post_type clause of the query. * * @since 2.2.0 * * @param array $post_types Array of post types to filter by * @param int $post->ID Post ID */ $post_types = apply_filters('crp_posts_post_types', $post_types, $post->ID); // Are we matching only the title or the post content as well? if ($match_content) { $stuff = $post->post_title . ' ' . crp_excerpt($post->ID, $match_content_words, false); $match_fields = "post_title,post_content"; } else { $stuff = $post->post_title; $match_fields = "post_title"; } // Make sure the post is not from the future $time_difference = get_option('gmt_offset'); $now = gmdate("Y-m-d H:i:s", time() + $time_difference * 3600); // Limit the related posts by time $current_time = current_time('timestamp', 0); $from_date = $current_time - $daily_range * DAY_IN_SECONDS; $from_date = gmdate('Y-m-d H:i:s', $from_date); // Create the SQL query to fetch the related posts from the database if (is_int($post->ID) && '' != $stuff) { // Fields to return $fields = " {$wpdb->posts}.ID "; // Create the base MATCH clause $match = $wpdb->prepare(" AND MATCH (" . $match_fields . ") AGAINST ('%s') ", $stuff); // FULLTEXT matching algorithm /** * Filter the MATCH clause of the query. * * @since 2.1.0 * * @param string $match The MATCH section of the WHERE clause of the query * @param string $stuff String to match fulltext with * @param int $post->ID Post ID */ $match = apply_filters('crp_posts_match', $match, $stuff, $post->ID); // Create the maximum date limit $now_clause = $wpdb->prepare(" AND {$wpdb->posts}.post_date < '%s' ", $now); // Show posts before today /** * Filter the Maximum date clause of the query. * * @since 2.1.0 * * @param string $now_clause The Maximum date of the WHERE clause of the query. * @param int $post->ID Post ID */ $now_clause = apply_filters('crp_posts_now_date', $now_clause, $post->ID); // Create the minimum date limit $from_clause = 0 == $daily_range ? '' : $wpdb->prepare(" AND {$wpdb->posts}.post_date >= '%s' ", $from_date); // Show posts after the date specified /** * Filter the Maximum date clause of the query. * * @since 2.1.0 * * @param string $from_clause The Minimum date of the WHERE clause of the query. * @param int $post->ID Post ID */ $from_clause = apply_filters('crp_posts_from_date', $from_clause, $post->ID); // Create the base WHERE clause $where = $match; $where .= $now_clause; $where .= $from_clause; $where .= " AND {$wpdb->posts}.post_status = 'publish' "; // Only show published posts $where .= $wpdb->prepare(" AND {$wpdb->posts}.ID != %d ", $post->ID); // Show posts after the date specified if ('' != $exclude_post_ids) { $where .= " AND {$wpdb->posts}.ID NOT IN (" . $exclude_post_ids . ") "; } $where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '", $post_types) . "') "; // Array of post types // Create the base LIMITS clause $limits .= $wpdb->prepare(" LIMIT %d ", $limit); /** * Filter the SELECT clause of the query. * * @since 2.0.0 * * @param string $fields The SELECT clause of the query. * @param int $post->ID Post ID */ $fields = apply_filters('crp_posts_fields', $fields, $post->ID); /** * Filter the JOIN clause of the query. * * @since 2.0.0 * * @param string $join The JOIN clause of the query. * @param int $post->ID Post ID */ $join = apply_filters('crp_posts_join', $join, $post->ID); /** * Filter the WHERE clause of the query. * * @since 2.0.0 * * @param string $where The WHERE clause of the query. * @param int $post->ID Post ID */ $where = apply_filters('crp_posts_where', $where, $post->ID); /** * Filter the GROUP BY clause of the query. * * @since 2.0.0 * * @param string $groupby The GROUP BY clause of the query. * @param int $post->ID Post ID */ $groupby = apply_filters('crp_posts_groupby', $groupby, $post->ID); /** * Filter the HAVING clause of the query. * * @since 2.2.0 * * @param string $having The HAVING clause of the query. * @param int $post->ID Post ID */ $having = apply_filters('crp_posts_having', $having, $post->ID); /** * Filter the ORDER BY clause of the query. * * @since 2.0.0 * * @param string $orderby The ORDER BY clause of the query. * @param int $post->ID Post ID */ $orderby = apply_filters('crp_posts_orderby', $orderby, $post->ID); /** * Filter the LIMIT clause of the query. * * @since 2.0.0 * * @param string $limits The LIMIT clause of the query. * @param int $post->ID Post ID */ $limits = apply_filters('crp_posts_limits', $limits, $post->ID); if (!empty($groupby)) { $groupby = 'GROUP BY ' . $groupby; } if (!empty($having)) { $having = 'HAVING ' . $having; } if (!empty($orderby)) { $orderby = 'ORDER BY ' . $orderby; } $sql = "SELECT DISTINCT {$fields} FROM {$wpdb->posts} {$join} WHERE 1=1 {$where} {$groupby} {$having} {$orderby} {$limits}"; $results = $wpdb->get_results($sql); } else { $results = false; } /** * Filter object containing the post IDs. * * @since 1.9 * * @param object $results Object containing the related post IDs */ return apply_filters('get_crp_posts_id', $results); }
/** * Fetch related posts IDs. * * @since 1.9 * * @param array $args * @return object $results */ function get_crp_posts_id($args = array()) { global $wpdb, $post, $single, $crp_settings; // Initialise some variables $fields = ''; $where = ''; $join = ''; $groupby = ''; $orderby = ''; $limits = ''; $match_fields = ''; $defaults = array('postid' => FALSE, 'strict_limit' => FALSE); $defaults = array_merge($defaults, $crp_settings); // Parse incomming $args into an array and merge it with $defaults $args = wp_parse_args($args, $defaults); // Declare each item in $args as its own variable i.e. $type, $before. extract($args, EXTR_SKIP); $post = empty($postid) ? $post : get_post($postid); $limit = $strict_limit ? $limit : $limit * 3; parse_str($post_types, $post_types); // Save post types in $post_types variable // Are we matching only the title or the post content as well? if ($match_content) { $stuff = $post->post_title . ' ' . crp_excerpt($post->ID, $match_content_words, false); $match_fields = "post_title,post_content"; } else { $stuff = $post->post_title; $match_fields = "post_title"; } // Make sure the post is not from the future $time_difference = get_option('gmt_offset'); $now = gmdate("Y-m-d H:i:s", time() + $time_difference * 3600); // Limit the related posts by time $daily_range = $daily_range - 1; $from_date = strtotime('-' . $daily_range . ' DAY', strtotime($now)); $from_date = date('Y-m-d H:i:s', $from_date); // Create the SQL query to fetch the related posts from the database if (is_int($post->ID) && '' != $stuff) { // Fields to return $fields = " {$wpdb->posts}.ID "; // Create the base WHERE clause $where .= $wpdb->prepare(" AND MATCH (" . $match_fields . ") AGAINST ('%s') ", $stuff); // FULLTEXT matching algorithm $where .= $wpdb->prepare(" AND {$wpdb->posts}.post_date < '%s' ", $now); // Show posts before today $where .= $wpdb->prepare(" AND {$wpdb->posts}.post_date >= '%s' ", $from_date); // Show posts after the date specified $where .= " AND {$wpdb->posts}.post_status = 'publish' "; // Only show published posts $where .= $wpdb->prepare(" AND {$wpdb->posts}.ID != %d ", $post->ID); // Show posts after the date specified if ('' != $exclude_post_ids) { $where .= " AND {$wpdb->posts}.ID NOT IN (" . $exclude_post_ids . ") "; } $where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '", $post_types) . "') "; // Array of post types // Create the base LIMITS clause $limits .= $wpdb->prepare(" LIMIT %d ", $limit); /** * Filter the SELECT clause of the query. * * @param string $fields The SELECT clause of the query. */ $fields = apply_filters('crp_posts_fields', $fields, $post->ID); /** * Filter the JOIN clause of the query. * * @param string $join The JOIN clause of the query. */ $join = apply_filters('crp_posts_join', $join, $post->ID); /** * Filter the WHERE clause of the query. * * @param string $where The WHERE clause of the query. */ $where = apply_filters('crp_posts_where', $where, $post->ID); /** * Filter the GROUP BY clause of the query. * * @param string $groupby The GROUP BY clause of the query. */ $groupby = apply_filters('crp_posts_groupby', $groupby, $post->ID); /** * Filter the ORDER BY clause of the query. * * @param string $orderby The ORDER BY clause of the query. */ $orderby = apply_filters('crp_posts_orderby', $orderby, $post->ID); /** * Filter the LIMIT clause of the query. * * @param string $limits The LIMIT clause of the query. */ $limits = apply_filters('crp_posts_limits', $limits, $post->ID); if (!empty($groupby)) { $groupby = 'GROUP BY ' . $groupby; } if (!empty($orderby)) { $orderby = 'ORDER BY ' . $orderby; } $sql = "SELECT DISTINCT {$fields} FROM {$wpdb->posts} {$join} WHERE 1=1 {$where} {$groupby} {$orderby} {$limits}"; $results = $wpdb->get_results($sql); } else { $results = false; } /** * Filter object containing the post IDs. * * @since 1.9 * * @param object $results Object containing the related post IDs */ return apply_filters('get_crp_posts_id', $results); }
function get_crp($is_widget, $limit, $show_excerpt, $post_thumb_op, $thumb_height, $thumb_width) { global $wpdb, $post, $single; $crp_settings = crp_read_options(); if (empty($limit)) $limit = stripslashes($crp_settings['limit']); if (empty($post_thumb_op)) $post_thumb_op = stripslashes($crp_settings['post_thumb_op']); if (!isset($show_excerpt)) $show_excerpt = $crp_settings['show_excerpt']; if (empty($thumb_height)) $thumb_height = stripslashes($crp_settings['thumb_height']); if (empty($thumb_width)) $thumb_width = stripslashes($crp_settings['thumb_width']); parse_str($crp_settings['post_types'],$post_types); $exclude_categories = explode(',',$crp_settings['exclude_categories']); $rel_attribute = (($crp_settings['link_nofollow']) ? ' rel="nofollow" ' : ' ' ); $target_attribute = (($crp_settings['link_nofollow']) ? ' target="_blank" ' : ' ' ); // Make sure the post is not from the future $time_difference = get_option('gmt_offset'); $now = gmdate("Y-m-d H:i:s",(time()+($time_difference*3600))); // Are we matching only the title or the post content as well? if($crp_settings['match_content']) { $stuff = addslashes($post->post_title. ' ' . $post->post_content); } else { $stuff = addslashes($post->post_title); } // Limit the related posts by time $daily_range = $crp_settings['daily_range'] - 1; $current_date = strtotime ( '-'.$daily_range. ' DAY' , strtotime ( $now ) ); $current_date = date ( 'Y-m-d H:i:s' , $current_date ); // Create the SQL query to fetch the related posts from the database if ((is_int($post->ID))&&($stuff != '')) { $sql = "SELECT DISTINCT ID,post_title,post_date " . " FROM ".$wpdb->posts." WHERE " . "MATCH (post_title,post_content) AGAINST ('".$stuff."') " . "AND post_date <= '".$now."' " . "AND post_date >= '".$current_date."' " . "AND post_status = 'publish' " . "AND ID != ".$post->ID." "; if ($crp_settings['exclude_post_ids']!='') $sql .= "AND ID NOT IN (".$crp_settings['exclude_post_ids'].") "; $sql .= "AND ( "; $multiple = false; foreach ($post_types as $post_type) { if ( $multiple ) $sql .= ' OR '; $sql .= " post_type = '".$post_type."' "; $multiple = true; } $sql .=" ) "; $sql .= "LIMIT ".$limit*3; $search_counter = 0; $searches = $wpdb->get_results($sql); } else { $searches = false; } $output = (is_singular()) ? '<div id="crp_related" class="crp_related">' : '<div class="crp_related">'; if($searches){ if(!$is_widget) $output .= (stripslashes($crp_settings['title'])); $output .= $crp_settings['before_list']; foreach($searches as $search) { $categorys = get_the_category($search->ID); //Fetch categories of the plugin $p_in_c = false; // Variable to check if post exists in a particular category $title = crp_max_formatted_content(get_the_title($search->ID),$crp_settings['title_length']); foreach ($categorys as $cat) { // Loop to check if post exists in excluded category $p_in_c = (in_array($cat->cat_ID, $exclude_categories)) ? true : false; if ($p_in_c) break; // End loop if post found in category } if (!$p_in_c) { $output .= $crp_settings['before_list_item']; //$output .= '<a href="'.get_permalink($search->ID).'" class="crp_link">'; // Add beginning of link if ($post_thumb_op=='after') { $output .= '<a href="'.get_permalink($search->ID).'" '.$rel_attribute.' '.$target_attribute.'class="crp_title">'.$title.'</a>'; // Add title if post thumbnail is to be displayed after } if ($post_thumb_op=='inline' || $post_thumb_op=='after' || $post_thumb_op=='thumbs_only') { $output .= '<a href="'.get_permalink($search->ID).'" '.$rel_attribute.' '.$target_attribute.'>'; $output .= crp_get_the_post_thumbnail('postid='.$search->ID.'&thumb_height='.$thumb_height.'&thumb_width='.$thumb_width.'&thumb_meta='.$crp_settings['thumb_meta'].'&thumb_default='.$crp_settings['thumb_default'].'&thumb_default_show='.$crp_settings['thumb_default_show'].'&thumb_timthumb='.$crp_settings['thumb_timthumb'].'&thumb_timthumb_q='.$crp_settings['thumb_timthumb_q'].'&scan_images='.$crp_settings['scan_images'].'&class=crp_thumb&filter=crp_postimage'); //$output .= crp_get_the_post_thumbnail($search->ID, $crp_settings); $output .= '</a>'; } if ($post_thumb_op=='inline' || $post_thumb_op=='text_only') { $output .= '<a href="'.get_permalink($search->ID).'" '.$rel_attribute.' '.$target_attribute.' class="crp_title">'.$title.'</a>'; // Add title when required by settings } //$output .= '</a>'; // Close the link if ($show_excerpt) { $output .= '<span class="crp_excerpt"> '.crp_excerpt($search->ID,$crp_settings['excerpt_length']).'</span>'; } $output .= $crp_settings['after_list_item']; $search_counter++; } if ($search_counter == $limit) break; // End loop when related posts limit is reached } //end of foreach loop if ($crp_settings['show_credit']) { $output .= $crp_settings['before_list_item']; $output .= __('Powered by',CRP_LOCAL_NAME); $output .= ' <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/" rel="nofollow">Contextual Related Posts</a>'.$crp_settings['after_list_item']; } $output .= $crp_settings['after_list']; }else{ $output .= ($crp_settings['blank_output']) ? ' ' : '<p>'.$crp_settings['blank_output_text'].'</p>'; //$output .= '<p>'.strip_tags($sql).'</p>'; } if ((strpos($output, $crp_settings['before_list_item'])) === false) { $output = '<div id="crp_related">'; $output .= ($crp_settings['blank_output']) ? ' ' : '<p>'.$crp_settings['blank_output_text'].'</p>'; } $output .= '</div>'; return $output; }