/** * Main function to generate the related posts output * * @since 1.0.1 * * @param array $args Parameters in a query string format * @return string HTML formatted list of related posts */ function get_crp($args = array()) { global $wpdb, $post, $crp_settings; $defaults = array('is_widget' => FALSE, 'echo' => TRUE, 'heading' => TRUE); $defaults = array_merge($defaults, $crp_settings); // Parse incomming $args into an array and merge it with $defaults $args = wp_parse_args($args, $defaults); //Support caching to speed up retrieval if (!empty($args['cache'])) { $meta_key = 'crp_related_posts'; if ($is_widget) { $meta_key .= '_widget'; } if (is_feed()) { $meta_key .= '_feed'; } $output = get_post_meta($post->ID, $meta_key, true); if ($output) { return $output; } } //TODO implementar categorías a excluir //$exclude_categories = explode( ',', $args['exclude_categories'] ); $results = get_elastic_crp_posts(array_merge($args, array('postid' => $post->ID, 'strict_limit' => TRUE))); $output = is_singular() ? '<div id="crp_related" class="crp_related' . ($args['is_widget'] ? '_widget' : '') . '">' : '<div class="crp_related' . ($args['is_widget'] ? '_widget' : '') . '">'; if (is_array($results)) { $loop_counter = 0; $output .= crp_heading_title($args); $output .= crp_before_list($args); foreach ($results as $el_result) { /** * Filter the post ID for each result. Allows a custom function to hook in and change the ID if needed. * * @since 1.9 * * @param int $result->ID ID of the post */ $resultid = apply_filters('crp_post_id', $el_result['_source']['post_id']); $result = get_post($resultid); // Let's get the Post using the ID $output .= crp_before_list_item($args, $result); $output .= crp_list_link($args, $result); if ($args['show_author']) { $output .= crp_author($args, $result); } if ($args['show_date']) { $output .= '<span class="crp_date"> ' . mysql2date(get_option('date_format', 'd/m/y'), $result->post_date) . '</span> '; } if ($args['show_excerpt']) { $output .= '<span class="crp_excerpt"> ' . crp_excerpt($result->ID, $excerpt_length) . '</span>'; } $loop_counter++; $output .= crp_after_list_item($args, $result); if ($loop_counter == $args['limit']) { break; } // End loop when related posts limit is reached } //end of foreach loop if ($args['show_credit']) { $output .= crp_before_list_item($args, $result); $output .= sprintf(__('Powered by <a href="%s" rel="nofollow">Contextual Related Posts</a>', CRP_LOCAL_NAME), esc_url('https://webberzone.com/plugins/contextual-related-posts/')); $output .= crp_after_list_item($args, $result); } $output .= crp_after_list($args); $clearfix = '<div class="crp_clear"></div>'; /** * Filter the clearfix div tag. This is included after the closing tag to clear any miscellaneous floating elements; * * @since 2.0.0 * * @param string $clearfix Contains: <div style="clear:both"></div> */ $output .= apply_filters('crp_clearfix', $clearfix); } else { $output .= $args['blank_output'] ? ' ' : '<p>' . $args['blank_output_text'] . '</p>'; } // Check if the opening list tag is missing in the output, it means all of our results were eliminated cause of the category filter if (false === strpos($output, $args['before_list_item'])) { $output = '<div id="crp_related">'; $output .= $args['blank_output'] ? ' ' : '<p>' . $args['blank_output_text'] . '</p>'; } $output .= '</div>'; // closing div of 'crp_related' //Support caching to speed up retrieval if (!empty($args['cache'])) { update_post_meta($post->ID, $meta_key, $output, ''); } /** * Filter the output * * @since 1.9.1 * * @param string $output Formatted list of related posts * @param array $args Complete set of arguments */ return apply_filters('get_crp', $output, $args); }
/** * Main function to generate the related posts output * * @since 1.0.1 * * @param array $args Parameters in a query string format * @return string HTML formatted list of related posts */ function get_crp($args = array()) { global $wpdb, $post, $crp_settings; // if set, save $exclude_categories if (isset($args['exclude_categories']) && '' != $args['exclude_categories']) { $exclude_categories = explode(',', $args['exclude_categories']); $args['strict_limit'] = false; } $defaults = array('is_widget' => false, 'is_shortcode' => false, 'is_manual' => false, 'echo' => true, 'heading' => true); $defaults = array_merge($defaults, $crp_settings); // Parse incomming $args into an array and merge it with $defaults $args = wp_parse_args($args, $defaults); // WPML support. if (function_exists('wpml_object_id_filter') || function_exists('icl_object_id')) { $args['strict_limit'] = false; } // Support caching to speed up retrieval if (!empty($args['cache'])) { $meta_key = 'crp_related_posts'; if ($args['is_widget']) { $meta_key .= '_widget'; } if ($args['is_manual']) { $meta_key .= '_manual'; } if (is_feed()) { $meta_key .= '_feed'; } $output = get_post_meta($post->ID, $meta_key, true); if ($output) { return $output; } } // Retrieve the list of posts $results = get_crp_posts_id(array_merge($args, array('postid' => $post->ID, 'strict_limit' => isset($args['strict_limit']) ? $args['strict_limit'] : true))); /** * Filter to create a custom HTML output * * @since 2.2.3 * * @param mixed Default return value * @param array $results Array of IDs of related posts * @param array $args Array of settings * @return string Custom HTML formatted list of related posts */ $custom_template = apply_filters('crp_custom_template', NULL, $results, $args); if (!empty($custom_template)) { if (!empty($args['cache'])) { update_post_meta($post->ID, $meta_key, $custom_template, ''); } return $custom_template; } $widget_class = $args['is_widget'] ? 'crp_related_widget' : 'crp_related '; $shortcode_class = $args['is_shortcode'] ? 'crp_related_shortcode ' : ''; $post_classes = $widget_class . $shortcode_class; /** * Filter the classes added to the div wrapper of the Top 10. * * @since 2.2.3 * * @param string $post_classes Post classes string. */ $post_classes = apply_filters('crp_post_class', $post_classes); $output = '<div class="' . $post_classes . '">'; if ($results) { $loop_counter = 0; $output .= crp_heading_title($args); $output .= crp_before_list($args); // We need this for WPML support $processed_results = array(); foreach ($results as $result) { /* Support WPML */ $resultid = crp_object_id_cur_lang($result->ID); // If this is NULL or already processed ID or matches current post then skip processing this loop. if (!$resultid || in_array($resultid, $processed_results) || intval($resultid) === intval($post->ID)) { continue; } // Push the current ID into the array to ensure we're not repeating it array_push($processed_results, $resultid); /** * Filter the post ID for each result. Allows a custom function to hook in and change the ID if needed. * * @since 1.9 * * @param int $resultid ID of the post */ $resultid = apply_filters('crp_post_id', $resultid); $result = get_post($resultid); // Let's get the Post using the ID // Process the category exclusion if passed in the shortcode if (isset($exclude_categories)) { $categorys = get_the_category($result->ID); // Fetch categories of the plugin $p_in_c = false; // Variable to check if post exists in a particular category 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; // Skip loop execution and go to the next step } } if ($p_in_c) { continue; // Skip loop execution and go to the next step } } $output .= crp_before_list_item($args, $result); $output .= crp_list_link($args, $result); if ($args['show_author']) { $output .= crp_author($args, $result); } if ($args['show_date']) { $output .= '<span class="crp_date"> ' . mysql2date(get_option('date_format', 'd/m/y'), $result->post_date) . '</span> '; } if ($args['show_excerpt']) { $output .= '<span class="crp_excerpt"> ' . crp_excerpt($result->ID, $args['excerpt_length']) . '</span>'; } $loop_counter++; $output .= crp_after_list_item($args, $result); if ($loop_counter == $args['limit']) { break; // End loop when related posts limit is reached } } //end of foreach loop if ($args['show_credit']) { $output .= crp_before_list_item($args, $result); $output .= sprintf(__('Powered by <a href="%s" rel="nofollow">Contextual Related Posts</a>', 'contextual-related-posts'), esc_url('https://webberzone.com/plugins/contextual-related-posts/')); $output .= crp_after_list_item($args, $result); } $output .= crp_after_list($args); $clearfix = '<div class="crp_clear"></div>'; /** * Filter the clearfix div tag. This is included after the closing tag to clear any miscellaneous floating elements; * * @since 2.0.0 * * @param string $clearfix Contains: <div style="clear:both"></div> */ $output .= apply_filters('crp_clearfix', $clearfix); } else { $output .= $args['blank_output'] ? ' ' : '<p>' . $args['blank_output_text'] . '</p>'; } // Check if the opening list tag is missing in the output, it means all of our results were eliminated cause of the category filter if (false === strpos($output, $args['before_list_item'])) { $output = '<div id="crp_related">'; $output .= $args['blank_output'] ? ' ' : '<p>' . $args['blank_output_text'] . '</p>'; } $output .= '</div>'; // closing div of 'crp_related' // Support caching to speed up retrieval if (!empty($args['cache'])) { update_post_meta($post->ID, $meta_key, $output, ''); } /** * Filter the output * * @since 1.9.1 * * @param string $output Formatted list of related posts * @param array $args Complete set of arguments */ return apply_filters('get_crp', $output, $args); }