/** * Default options. * * @since 1.0.1 * * @return array Default options */ function crp_default_options() { $title = __('<h3>Related Posts:</h3>', CRP_LOCAL_NAME); $blank_output_text = __('No related posts found', CRP_LOCAL_NAME); $thumb_default = plugins_url('default.png', __FILE__); $crp_get_all_image_sizes = crp_get_all_image_sizes(); // get relevant post types $args = array('public' => true, '_builtin' => true); $post_types = array('post' => 'post', 'page' => 'page'); $post_types = http_build_query($post_types, '', '&'); $crp_settings = array('cache' => false, 'add_to_content' => true, 'add_to_page' => true, 'add_to_feed' => false, 'add_to_home' => false, 'add_to_category_archives' => false, 'add_to_tag_archives' => false, 'add_to_archives' => false, 'content_filter_priority' => 10, 'show_metabox' => true, 'show_metabox_admins' => false, 'show_credit' => false, 'limit' => '6', 'daily_range' => '1095', 'match_content' => true, 'match_content_words' => '0', 'post_types' => $post_types, 'exclude_categories' => '', 'exclude_cat_slugs' => '', 'exclude_post_ids' => '', 'title' => $title, 'blank_output' => true, 'blank_output_text' => $blank_output_text, 'show_excerpt' => false, 'show_date' => false, 'show_author' => false, 'excerpt_length' => '10', 'title_length' => '60', 'link_new_window' => false, 'link_nofollow' => false, 'before_list' => '<ul>', 'after_list' => '</ul>', 'before_list_item' => '<li>', 'after_list_item' => '</li>', 'exclude_on_post_ids' => '', 'exclude_on_post_types' => '', 'post_thumb_op' => 'inline', 'thumb_size' => 'crp_thumbnail', 'thumb_height' => '150', 'thumb_width' => '150', 'thumb_crop' => true, 'thumb_html' => 'html', 'thumb_meta' => 'post-image', 'scan_images' => true, 'thumb_default' => $thumb_default, 'thumb_default_show' => true, 'limit_feed' => '5', 'post_thumb_op_feed' => 'text_only', 'thumb_height_feed' => '50', 'thumb_width_feed' => '50', 'show_excerpt_feed' => false, 'custom_CSS' => '', 'include_default_style' => true, 'crp_styles' => 'rounded_thumbs'); /** * Filters the default options array. * * @since 1.9.1 * * @param array $crp_settings Default options */ return apply_filters('crp_default_options', $crp_settings); }
/** * Function generates the plugin settings page. * * @since 1.0.1 */ function crp_options() { global $wpdb, $crp_url; $crp_settings = crp_read_options(); $wp_post_types = get_post_types(array('public' => true)); parse_str($crp_settings['post_types'], $post_types); $posts_types_inc = array_intersect($wp_post_types, $post_types); parse_str($crp_settings['exclude_on_post_types'], $exclude_on_post_types); $posts_types_excl = array_intersect($wp_post_types, $exclude_on_post_types); // Temporary check if default styles are off and rounded thumbnails are selected - will be eventually deprecated // This is a mismatch, so we force it to no style if (false == $crp_settings['include_default_style'] && 'rounded_thumbs' == $crp_settings['crp_styles']) { $crp_settings['crp_styles'] = 'no_style'; update_option('ald_crp_settings', $crp_settings); } if (true == $crp_settings['include_default_style'] && 'rounded_thumbs' != $crp_settings['crp_styles']) { $crp_settings['crp_styles'] = 'rounded_thumbs'; update_option('ald_crp_settings', $crp_settings); } if (isset($_POST['crp_save']) && check_admin_referer('crp-plugin-settings')) { /**** General options ***/ $crp_settings['cache'] = isset($_POST['cache']) ? true : false; $crp_settings['limit'] = intval($_POST['limit']); $crp_settings['daily_range'] = intval($_POST['daily_range']); $crp_settings['match_content'] = isset($_POST['match_content']) ? true : false; $crp_settings['match_content_words'] = intval($_POST['match_content_words']); $crp_settings['add_to_content'] = isset($_POST['add_to_content']) ? true : false; $crp_settings['add_to_page'] = isset($_POST['add_to_page']) ? true : false; $crp_settings['add_to_feed'] = isset($_POST['add_to_feed']) ? true : false; $crp_settings['add_to_home'] = isset($_POST['add_to_home']) ? true : false; $crp_settings['add_to_category_archives'] = isset($_POST['add_to_category_archives']) ? true : false; $crp_settings['add_to_tag_archives'] = isset($_POST['add_to_tag_archives']) ? true : false; $crp_settings['add_to_archives'] = isset($_POST['add_to_archives']) ? true : false; $crp_settings['content_filter_priority'] = intval($_POST['content_filter_priority']); $crp_settings['show_metabox'] = isset($_POST['show_metabox']) ? true : false; $crp_settings['show_metabox_admins'] = isset($_POST['show_metabox_admins']) ? true : false; $crp_settings['show_credit'] = isset($_POST['show_credit']) ? true : false; /**** Output options ****/ $crp_settings['title'] = wp_kses_post($_POST['title']); $crp_settings['blank_output'] = $_POST['blank_output'] == 'blank' ? true : false; $crp_settings['blank_output_text'] = wp_kses_post($_POST['blank_output_text']); $crp_settings['show_excerpt'] = isset($_POST['show_excerpt']) ? true : false; $crp_settings['show_date'] = isset($_POST['show_date']) ? true : false; $crp_settings['show_author'] = isset($_POST['show_author']) ? true : false; $crp_settings['excerpt_length'] = intval($_POST['excerpt_length']); $crp_settings['title_length'] = intval($_POST['title_length']); $crp_settings['link_new_window'] = isset($_POST['link_new_window']) ? true : false; $crp_settings['link_nofollow'] = isset($_POST['link_nofollow']) ? true : false; $crp_settings['before_list'] = wp_kses_post($_POST['before_list']); $crp_settings['after_list'] = wp_kses_post($_POST['after_list']); $crp_settings['before_list_item'] = wp_kses_post($_POST['before_list_item']); $crp_settings['after_list_item'] = wp_kses_post($_POST['after_list_item']); $crp_settings['exclude_on_post_ids'] = $_POST['exclude_on_post_ids'] == '' ? '' : implode(',', array_map('intval', explode(',', $_POST['exclude_on_post_ids']))); $crp_settings['exclude_post_ids'] = $_POST['exclude_post_ids'] == '' ? '' : implode(',', array_map('intval', explode(',', $_POST['exclude_post_ids']))); /**** Thumbnail options ****/ $crp_settings['post_thumb_op'] = wp_kses_post($_POST['post_thumb_op']); $crp_settings['thumb_size'] = $_POST['thumb_size']; if ('crp_thumbnail' != $crp_settings['thumb_size']) { $crp_thumb_size = crp_get_all_image_sizes($crp_settings['thumb_size']); $crp_settings['thumb_height'] = intval($crp_thumb_size['height']); $crp_settings['thumb_width'] = intval($crp_thumb_size['width']); $crp_settings['thumb_crop'] = $crp_thumb_size['crop']; } else { $crp_settings['thumb_height'] = intval($_POST['thumb_height']); $crp_settings['thumb_width'] = intval($_POST['thumb_width']); $crp_settings['thumb_crop'] = isset($_POST['thumb_crop']) ? true : false; } $crp_settings['thumb_html'] = $_POST['thumb_html']; $crp_settings['thumb_meta'] = '' == $_POST['thumb_meta'] ? 'post-image' : wp_kses_post($_POST['thumb_meta']); $crp_settings['scan_images'] = isset($_POST['scan_images']) ? true : false; $crp_settings['thumb_default'] = '' == $_POST['thumb_default'] || '/default.png' == $_POST['thumb_default'] ? $crp_url . '/default.png' : $_POST['thumb_default']; $crp_settings['thumb_default_show'] = isset($_POST['thumb_default_show']) ? true : false; /**** Feed options ****/ $crp_settings['limit_feed'] = intval($_POST['limit_feed']); $crp_settings['post_thumb_op_feed'] = wp_kses_post($_POST['post_thumb_op_feed']); $crp_settings['thumb_height_feed'] = intval($_POST['thumb_height_feed']); $crp_settings['thumb_width_feed'] = intval($_POST['thumb_width_feed']); $crp_settings['show_excerpt_feed'] = isset($_POST['show_excerpt_feed']) ? true : false; /**** Styles ****/ $crp_settings['custom_CSS'] = wp_kses_post($_POST['custom_CSS']); $crp_settings['crp_styles'] = wp_kses_post($_POST['crp_styles']); if ('rounded_thumbs' == $crp_settings['crp_styles']) { $crp_settings['include_default_style'] = true; $crp_settings['post_thumb_op'] = 'inline'; $crp_settings['show_excerpt'] = false; $crp_settings['show_author'] = false; $crp_settings['show_date'] = false; } elseif ('text_only' == $crp_settings['crp_styles']) { $crp_settings['include_default_style'] = false; $crp_settings['post_thumb_op'] = 'text_only'; } else { $crp_settings['include_default_style'] = false; } /**** Exclude categories ****/ $exclude_categories_slugs = array_map('trim', explode(',', wp_kses_post($_POST['exclude_cat_slugs']))); $crp_settings['exclude_cat_slugs'] = implode(', ', $exclude_categories_slugs); foreach ($exclude_categories_slugs as $exclude_categories_slug) { $catObj = get_category_by_slug($exclude_categories_slug); if (isset($catObj->term_taxonomy_id)) { $exclude_categories[] = $catObj->term_taxonomy_id; } } $crp_settings['exclude_categories'] = isset($exclude_categories) ? join(',', $exclude_categories) : ''; /**** Post types to include ****/ $wp_post_types = get_post_types(array('public' => true)); $post_types_arr = isset($_POST['post_types']) && is_array($_POST['post_types']) ? $_POST['post_types'] : array('post' => 'post'); $post_types = array_intersect($wp_post_types, $post_types_arr); $crp_settings['post_types'] = http_build_query($post_types, '', '&'); /**** Post types to exclude display on ****/ $post_types_excl_arr = isset($_POST['exclude_on_post_types']) && is_array($_POST['exclude_on_post_types']) ? $_POST['exclude_on_post_types'] : array(); $exclude_on_post_types = array_intersect($wp_post_types, $post_types_excl_arr); $crp_settings['exclude_on_post_types'] = http_build_query($exclude_on_post_types, '', '&'); /** * Filters $crp_settings before it is saved into the database * * @since 2.0.0 * * @param array $crp_settings CRP settings * @param array $_POST POST array that consists of the saved settings */ $crp_settings = apply_filters('crp_save_options', $crp_settings, $_POST); /**** Update CRP options into the database ****/ update_option('ald_crp_settings', $crp_settings); $crp_settings = crp_read_options(); parse_str($crp_settings['post_types'], $post_types); $posts_types_inc = array_intersect($wp_post_types, $post_types); parse_str($crp_settings['exclude_on_post_types'], $exclude_on_post_types); $posts_types_excl = array_intersect($wp_post_types, $exclude_on_post_types); // Delete the cache crp_cache_delete(); /* Echo a success message */ $str = '<div id="message" class="notice is-dismissible updated"><p>' . __('Options saved successfully. If enabled, the cache has been cleared.', 'contextual-related-posts') . '</p>'; if ('rounded_thumbs' == $crp_settings['crp_styles']) { $str .= '<p>' . __('Rounded Thumbnails style selected. Author, Excerpt and Date will not be displayed.', 'contextual-related-posts') . '</p>'; } if ('text_only' == $crp_settings['crp_styles']) { $str .= '<p>' . __('Text Only style selected. Thumbnails will not be displayed.', 'contextual-related-posts') . '</p>'; } if ('crp_thumbnail' != $crp_settings['thumb_size']) { $str .= '<p>' . sprintf(__('Pre-built thumbnail size selected. Thumbnail set to %d x %d.', 'contextual-related-posts'), $crp_settings['thumb_width'], $crp_settings['thumb_height']) . '</p>'; } $str .= '</div>'; echo $str; } if (isset($_POST['crp_default']) && check_admin_referer('crp-plugin-settings')) { delete_option('ald_crp_settings'); $crp_settings = crp_default_options(); update_option('ald_crp_settings', $crp_settings); $crp_settings = crp_read_options(); $wp_post_types = get_post_types(array('public' => true)); parse_str($crp_settings['post_types'], $post_types); $posts_types_inc = array_intersect($wp_post_types, $post_types); parse_str($crp_settings['exclude_on_post_types'], $exclude_on_post_types); $posts_types_excl = array_intersect($wp_post_types, $exclude_on_post_types); $str = '<div id="message" class="updated fade"><p>' . __('Options set to Default.', 'contextual-related-posts') . '</p></div>'; echo $str; } if (isset($_POST['crp_recreate']) && check_admin_referer('crp-plugin-settings')) { crp_delete_index(); crp_create_index(); $str = '<div id="message" class="updated fade"><p>' . __('Index recreated', 'contextual-related-posts') . '</p></div>'; echo $str; } /**** Include the views page ****/ include_once 'main-view.php'; }
/** * Function generates the plugin settings page. * * @since 1.0.1 * */ function crp_options() { global $wpdb, $crp_url; $crp_settings = crp_read_options(); $wp_post_types = get_post_types(array('public' => true)); parse_str($crp_settings['post_types'], $post_types); $posts_types_inc = array_intersect($wp_post_types, $post_types); parse_str($crp_settings['exclude_on_post_types'], $exclude_on_post_types); $posts_types_excl = array_intersect($wp_post_types, $exclude_on_post_types); if (isset($_POST['crp_save']) && check_admin_referer('crp-plugin-settings')) { /**** General options ***/ $crp_settings['cache'] = isset($_POST['cache']) ? true : false; $crp_settings['limit'] = intval($_POST['limit']); $crp_settings['daily_range'] = intval($_POST['daily_range']); $crp_settings['match_content'] = isset($_POST['match_content']) ? true : false; $crp_settings['match_content_words'] = intval($_POST['match_content_words']); $crp_settings['add_to_content'] = isset($_POST['add_to_content']) ? true : false; $crp_settings['add_to_page'] = isset($_POST['add_to_page']) ? true : false; $crp_settings['add_to_feed'] = isset($_POST['add_to_feed']) ? true : false; $crp_settings['add_to_home'] = isset($_POST['add_to_home']) ? true : false; $crp_settings['add_to_category_archives'] = isset($_POST['add_to_category_archives']) ? true : false; $crp_settings['add_to_tag_archives'] = isset($_POST['add_to_tag_archives']) ? true : false; $crp_settings['add_to_archives'] = isset($_POST['add_to_archives']) ? true : false; $crp_settings['content_filter_priority'] = intval($_POST['content_filter_priority']); $crp_settings['show_metabox'] = isset($_POST['show_metabox']) ? true : false; $crp_settings['show_metabox_admins'] = isset($_POST['show_metabox_admins']) ? true : false; $crp_settings['show_credit'] = isset($_POST['show_credit']) ? true : false; /**** Output options ****/ $crp_settings['title'] = wp_kses_post($_POST['title']); $crp_settings['blank_output'] = $_POST['blank_output'] == 'blank' ? true : false; $crp_settings['blank_output_text'] = wp_kses_post($_POST['blank_output_text']); $crp_settings['show_excerpt'] = isset($_POST['show_excerpt']) ? true : false; $crp_settings['show_date'] = isset($_POST['show_date']) ? true : false; $crp_settings['show_author'] = isset($_POST['show_author']) ? true : false; $crp_settings['excerpt_length'] = intval($_POST['excerpt_length']); $crp_settings['title_length'] = intval($_POST['title_length']); $crp_settings['link_new_window'] = isset($_POST['link_new_window']) ? true : false; $crp_settings['link_nofollow'] = isset($_POST['link_nofollow']) ? true : false; $crp_settings['before_list'] = wp_kses_post($_POST['before_list']); $crp_settings['after_list'] = wp_kses_post($_POST['after_list']); $crp_settings['before_list_item'] = wp_kses_post($_POST['before_list_item']); $crp_settings['after_list_item'] = wp_kses_post($_POST['after_list_item']); $crp_settings['exclude_on_post_ids'] = $_POST['exclude_on_post_ids'] == '' ? '' : implode(',', array_map('intval', explode(",", $_POST['exclude_on_post_ids']))); $crp_settings['exclude_post_ids'] = $_POST['exclude_post_ids'] == '' ? '' : implode(',', array_map('intval', explode(",", $_POST['exclude_post_ids']))); /**** Thumbnail options ****/ $crp_settings['post_thumb_op'] = wp_kses_post($_POST['post_thumb_op']); $crp_settings['thumb_size'] = $_POST['thumb_size']; if ('crp_thumbnail' != $crp_settings['thumb_size']) { $crp_thumb_size = crp_get_all_image_sizes($crp_settings['thumb_size']); $crp_settings['thumb_height'] = intval($crp_thumb_size['height']); $crp_settings['thumb_width'] = intval($crp_thumb_size['width']); $crp_settings['thumb_crop'] = $crp_thumb_size['crop']; } else { $crp_settings['thumb_height'] = intval($_POST['thumb_height']); $crp_settings['thumb_width'] = intval($_POST['thumb_width']); $crp_settings['thumb_crop'] = isset($_POST['thumb_crop']) ? true : false; } $crp_settings['thumb_html'] = $_POST['thumb_html']; $crp_settings['thumb_meta'] = '' == $_POST['thumb_meta'] ? 'post-image' : wp_kses_post($_POST['thumb_meta']); $crp_settings['scan_images'] = isset($_POST['scan_images']) ? true : false; $crp_settings['thumb_default'] = '' == $_POST['thumb_default'] || "/default.png" == $_POST['thumb_default'] ? $crp_url . '/default.png' : $_POST['thumb_default']; $crp_settings['thumb_default_show'] = isset($_POST['thumb_default_show']) ? true : false; /**** Feed options ****/ $crp_settings['limit_feed'] = intval($_POST['limit_feed']); $crp_settings['post_thumb_op_feed'] = wp_kses_post($_POST['post_thumb_op_feed']); $crp_settings['thumb_height_feed'] = intval($_POST['thumb_height_feed']); $crp_settings['thumb_width_feed'] = intval($_POST['thumb_width_feed']); $crp_settings['show_excerpt_feed'] = isset($_POST['show_excerpt_feed']) ? true : false; /**** Custom styles ****/ $crp_settings['custom_CSS'] = wp_kses_post($_POST['custom_CSS']); if (isset($_POST['include_default_style'])) { $crp_settings['include_default_style'] = true; $crp_settings['post_thumb_op'] = 'inline'; $crp_settings['show_excerpt'] = false; $crp_settings['show_author'] = false; $crp_settings['show_date'] = false; } else { $crp_settings['include_default_style'] = false; } /**** Exclude categories ****/ $crp_settings['exclude_cat_slugs'] = wp_kses_post($_POST['exclude_cat_slugs']); $exclude_categories_slugs = explode(", ", $crp_settings['exclude_cat_slugs']); foreach ($exclude_categories_slugs as $exclude_categories_slug) { $catObj = get_category_by_slug($exclude_categories_slug); if (isset($catObj->term_id)) { $exclude_categories[] = $catObj->term_id; } } $crp_settings['exclude_categories'] = isset($exclude_categories) ? join(',', $exclude_categories) : ''; /**** Post types to include ****/ $wp_post_types = get_post_types(array('public' => true)); $post_types_arr = isset($_POST['post_types']) && is_array($_POST['post_types']) ? $_POST['post_types'] : array('post' => 'post'); $post_types = array_intersect($wp_post_types, $post_types_arr); $crp_settings['post_types'] = http_build_query($post_types, '', '&'); /**** Post types to exclude display on ****/ $post_types_excl_arr = isset($_POST['exclude_on_post_types']) && is_array($_POST['exclude_on_post_types']) ? $_POST['exclude_on_post_types'] : array(); $exclude_on_post_types = array_intersect($wp_post_types, $post_types_excl_arr); $crp_settings['exclude_on_post_types'] = http_build_query($exclude_on_post_types, '', '&'); /** * Filters $crp_settings before it is saved into the database * * @since 2.0.0 * * @param array $crp_settings CRP settings * @param array $_POST POST array that consists of the saved settings */ $crp_settings = apply_filters('crp_save_options', $crp_settings, $_POST); /**** Update CRP options into the database ****/ update_option('ald_crp_settings', $crp_settings); $crp_settings = crp_read_options(); parse_str($crp_settings['post_types'], $post_types); $posts_types_inc = array_intersect($wp_post_types, $post_types); parse_str($crp_settings['exclude_on_post_types'], $exclude_on_post_types); $posts_types_excl = array_intersect($wp_post_types, $exclude_on_post_types); // Delete the cache delete_post_meta_by_key('crp_related_posts'); delete_post_meta_by_key('crp_related_posts_widget'); delete_post_meta_by_key('crp_related_posts_feed'); delete_post_meta_by_key('crp_related_posts_widget_feed'); /* Echo a success message */ $str = '<div id="message" class="notice is-dismissible updated"><p>' . __('Options saved successfully.', CRP_LOCAL_NAME) . '</p>'; if (isset($_POST['include_default_style'])) { $str .= '<p>' . __('Default styles selected. Author, Excerpt and Date will not be displayed.', CRP_LOCAL_NAME) . '</p>'; } if ('crp_thumbnail' != $crp_settings['thumb_size']) { $str .= '<p>' . sprintf(__('Pre-built thumbnail size selected. Thumbnail set to %d x %d.', CRP_LOCAL_NAME), $crp_settings['thumb_width'], $crp_settings['thumb_height']) . '</p>'; } $str .= '</div>'; echo $str; } if (isset($_POST['crp_default']) && check_admin_referer('crp-plugin-settings')) { delete_option('ald_crp_settings'); $crp_settings = crp_default_options(); update_option('ald_crp_settings', $crp_settings); $crp_settings = crp_read_options(); $wp_post_types = get_post_types(array('public' => true)); parse_str($crp_settings['post_types'], $post_types); $posts_types_inc = array_intersect($wp_post_types, $post_types); parse_str($crp_settings['exclude_on_post_types'], $exclude_on_post_types); $posts_types_excl = array_intersect($wp_post_types, $exclude_on_post_types); $str = '<div id="message" class="updated fade"><p>' . __('Options set to Default.', CRP_LOCAL_NAME) . '</p></div>'; echo $str; } if (isset($_POST['crp_recreate']) && check_admin_referer('crp-plugin-settings')) { if ($wpdb->get_results("SHOW INDEX FROM {$wpdb->posts} where Key_name = 'crp_related'")) { $wpdb->query("ALTER TABLE " . $wpdb->posts . " DROP INDEX crp_related"); } if ($wpdb->get_results("SHOW INDEX FROM {$wpdb->posts} where Key_name = 'crp_related_title'")) { $wpdb->query("ALTER TABLE " . $wpdb->posts . " DROP INDEX crp_related_title"); } if ($wpdb->get_results("SHOW INDEX FROM {$wpdb->posts} where Key_name = 'crp_related_content'")) { $wpdb->query("ALTER TABLE " . $wpdb->posts . " DROP INDEX crp_related_content"); } crp_single_activate(); $str = '<div id="message" class="updated fade"><p>' . __('Index recreated', CRP_LOCAL_NAME) . '</p></div>'; echo $str; } /**** Include the views page ****/ include_once 'main-view.php'; }
<p style="color: #F00"><?php _e('This setting cannot be changed because an inbuilt style has been selected under the Styles section. If you would like to change this option, please select <strong>No styles</strong> under the Styles section.', 'contextual-related-posts'); ?> </p> <?php } ?> </td> </tr> <tr><th scope="row"><?php _e('Thumbnail size:', 'contextual-related-posts'); ?> </th> <td> <?php $crp_get_all_image_sizes = crp_get_all_image_sizes(); if (isset($crp_get_all_image_sizes['crp_thumbnail'])) { unset($crp_get_all_image_sizes['crp_thumbnail']); } foreach ($crp_get_all_image_sizes as $size) { ?> <label> <input type="radio" name="thumb_size" value="<?php echo $size['name']; ?> " id="<?php echo $size['name']; ?> " <?php if ($crp_settings['thumb_size'] == $size['name']) { echo 'checked="checked"';
/** * Fetch related posts IDs. * * @since 1.9 * * @param array $args * @return object $results */ function get_crp_posts_id($args = array()) { global $wpdb, $post, $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); // Fix the thumb size in case it is missing $crp_thumb_size = crp_get_all_image_sizes($args['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($args['postid']) ? $post : get_post($args['postid']); $limit = $args['strict_limit'] ? $args['limit'] : $args['limit'] * 3; // Save post types in $post_types variable parse_str($args['post_types'], $post_types); /** * 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? $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); } /** * Filter the fields that are to be matched. * * @since 2.2.0 * * @param array $match_fields Array of fields to be matched * @param int $post->ID Post ID */ $match_fields = apply_filters('crp_posts_match_fields', $match_fields, $post->ID); /** * Filter the content of the fields that are to be matched. * * @since 2.2.0 * * @param array $match_fields_content Array of content of fields to be matched * @param int $post->ID Post ID */ $match_fields_content = apply_filters('crp_posts_match_fields_content', $match_fields_content, $post->ID); // Convert our arrays into their corresponding strings after they have been filtered $match_fields = implode(',', $match_fields); $stuff = implode(' ', $match_fields_content); // 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 - $args['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)) { // 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 == $args['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 ('' != $args['exclude_post_ids']) { $where .= " AND {$wpdb->posts}.ID NOT IN (" . $args['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); }