function bawmrp_get_related_posts_auto($post) { $bawmrp_options = get_option('bawmrp'); // Get all manuals ids $rel_id = bawmrp_get_related_posts($post->ID, false); // How many auto posts we need $num_posts = (int) $bawmrp_options['max_posts'] == 0 ? -1 : (int) $bawmrp_options['max_posts'] - count(wp_parse_id_list($rel_id)); // If we still need auto posts or post number is illimited if ($num_posts > 0 || $num_posts == -1) { $rel_id = array_filter(wp_parse_id_list($rel_id)); if (isset($bawmrp_options['auto_posts']) && 'none' != $bawmrp_options['auto_posts']) { $args = array('post_type' => $post->post_type, 'post_status' => 'publish', 'post__not_in' => array_merge(array($post->ID), $rel_id), 'numberposts' => $num_posts, 'post_count' => $num_posts, 'order' => isset($bawmrp_options['random_order']) ? 'RAND' : 'DESC'); // auto posts by tags if ('none' != $bawmrp_options['auto_posts']) { // $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); // if ( $tags ) { // $args['tag__in'] = $tags; // } $cat = get_the_category($post->ID); $cat_ids = wp_list_pluck($cat, 'term_id'); $args['category'] = implode(',', $cat_ids); } // auto posts query $relative_query = get_posts($args); $rel_id = array(); // set empty // we found posts ? if (!empty($relative_query)) { $rel_id = wp_list_pluck($relative_query, 'ID'); } } // do we still need posts ? ok go sticky if checked if ($num_posts > count($rel_id)) { $rel_id = array_merge($rel_id, get_option('sticky_posts')); } $rel_id = array_flip($rel_id); if (isset($rel_id[$post->ID])) { unset($rel_id[$post->ID]); } $rel_id = array_flip($rel_id); // do we still need posts ? ok go recent if checked if ($num_posts > count($rel_id)) { $args = array('post_type' => $post->post_type, 'post_status' => 'publish', 'post__not_in' => array_merge(array($post->ID), $rel_id), 'numberposts' => $num_posts - count($rel_id), 'post_count' => $num_posts - count($rel_id)); $recent_query = get_posts($args); // we found posts ? if (!empty($recent_query)) { $rel_id = array_merge($rel_id, wp_list_pluck($recent_query, 'ID')); } } // return not empty return wp_parse_id_list($rel_id); } return array(); }
function bawmrp_box($post) { $related_post_ids = bawmrp_get_related_posts($post->ID, false); $related_post_ids = is_array($related_post_ids) ? '' : $related_post_ids; ?> <div> <input class="hide-if-js" type="text" name="bawmrp_post_ids" id="bawmrp_post_ids" value="<?php echo esc_attr($related_post_ids); ?> " /> <?php wp_nonce_field('add-relastedpostsids_' . $post->ID, '_wpnonce_yyarpp'); ?> <div> <a href="javascript:void(0);" id="bawmrp_open_find_posts_button" class="button button-small hide-if-no-js"><?php _e('Add a related post', 'bawmrp'); ?> </a> <span class="hide-if-js"><?php _e('Add posts IDs from posts you want to relate, comma separated.', 'bawmrp'); ?> </span> </div> <ul id="ul_yyarpp" class="tagchecklist"> <?php if (!empty($related_post_ids)) { $related_post_ids = wp_parse_id_list($related_post_ids); foreach ($related_post_ids as $id) { ?> <li data-id="<?php echo (int) $id; ?> "><span style="float:none;"><a class="hide-if-no-js erase_yyarpp">X</a> <?php echo get_the_title((int) $id); ?> </span></li> <?php } } ?> </ul> <span class="plugins widefat"><a href="javascript:void(0);" id="bawmrp_delete_related_posts" class="delete hide-if-no-js"><?php _e('Clear List'); ?> </a></span> </div> <?php }
function bawmrp_the_content($content = '') { global $post, $in_bawmrp_loop; $bawmrp_options = get_option('bawmrp'); if (!$post || $bawmrp_options['in_content'] != 'on' && $content != '' || apply_filters('stop_bawmrp', false)) { return $content; } if (is_home() && $bawmrp_options['in_homepage'] == 'on' && in_the_loop() || is_singular($bawmrp_options['post_types'])) { $ids_manual = wp_parse_id_list(bawmrp_get_related_posts($post->ID)); $lang = isset($_GET['lang']) ? $_GET['lang'] : get_locale(); $transient_name = apply_filters('bawmrp_transient_name', 'bawmrp_' . $post->ID . '_' . substr(md5(serialize($ids_manual) . serialize($bawmrp_options) . get_permalink($post->ID) . $lang), 0, 12)); if ($contents = get_transient($transient_name)) { extract($contents); if (!empty($list) && is_array($list) && isset($bawmrp_options['random_posts'])) { shuffle($list); } $final = $content . $head . @implode("\n", $list) . $foot; $content = apply_filters('bawmrp_posts_content', $final, $content, $head, $list, $foot); return $content; } $ids_auto = isset($bawmrp_options['auto_posts']) && 'none' != $bawmrp_options['auto_posts'] ? bawmrp_get_related_posts_auto($post) : array(); $ids = wp_parse_id_list(array_merge($ids_manual, $ids_auto)); if (defined('ICL_LANGUAGE_CODE')) { $head_title = isset($bawmrp_options['head_titles'][$post->post_type][bawmrp_wpml_lang_by_code(ICL_LANGUAGE_CODE)]) && is_string($bawmrp_options['head_titles'][$post->post_type][bawmrp_wpml_lang_by_code(ICL_LANGUAGE_CODE)]) ? $bawmrp_options['head_titles'][$post->post_type][bawmrp_wpml_lang_by_code(ICL_LANGUAGE_CODE)] : $head_title; } elseif (isset($bawmrp_options['head_titles'][$post->post_type][get_locale()]) && is_string($bawmrp_options['head_titles'][$post->post_type][get_locale()])) { $head_title = $bawmrp_options['head_titles'][$post->post_type][get_locale()]; } else { $head_title = isset($bawmrp_options['head_titles'][$post->post_type]) && is_string($bawmrp_options['head_titles'][$post->post_type]) ? $bawmrp_options['head_titles'][$post->post_type] : $head_title; } if (!empty($ids) && is_array($ids) && isset($ids[0]) && $ids[0] != 0) { $ids = wp_parse_id_list($ids); $list = array(); if (isset($bawmrp_options['random_posts'])) { shuffle($ids); } if ((int) $bawmrp_options['max_posts'] > 0 && count($ids) > (int) $bawmrp_options['max_posts']) { $ids = array_slice($ids, 0, (int) $bawmrp_options['max_posts']); } $head = '<div class="bawmrp"><h3>' . $head_title . '</h3><ul>'; do_action('bawmrp_first_li'); $style = apply_filters('bawmrp_li_style', 'float:left;width:120px;height:auto;overflow:hidden;list-style:none;border-right: 1px solid #ccc;text-align:center;padding:0px 5px;'); $n = 0; $in_bawmrp_loop = true; foreach ($ids as $id) { if (in_array($id, $ids_manual)) { $class = 'bawmrp_manual'; } elseif (in_array($id, get_option('sticky_posts'))) { $class = 'bawmrp_sticky'; } elseif (in_array($id, $ids_auto)) { $class = 'bawmrp_auto'; } $_content = ''; if (isset($bawmrp_options['display_content'])) { $p = get_post($id); $_content = '<br />' . apply_filters('the_excerpt', $p->post_excerpt) . '<p> </p>'; } $_content = apply_filters('bawmrp_the_content', $_content, $id); $_content = apply_filters('bawmrp_more_content', $_content); if ($bawmrp_options['in_content_mode'] == 'list') { $list[] = '<li class="' . $class . '">' . '<a href="' . esc_url(apply_filters('the_permalink', get_permalink($id))) . '">' . get_the_title($id) . '</a>' . $_content . '</li>'; } else { $no_thumb = apply_filters('bawmrp_no_thumb', admin_url('/images/w-logo-blue.png'), $id); $thumb_size = apply_filters('bawmrp_thumb_size', array(100, 100)); if (current_theme_supports('post-thumbnails')) { $thumb = has_post_thumbnail($id) ? get_the_post_thumbnail($id, $thumb_size) : '<img alt="" src="' . baw_first_image(isset($bawmrp_options['first_image']) && $bawmrp_options['first_image'] == 'on' ? $id : null, $no_thumb) . '" height="' . $thumb_size[0] . '" width="' . $thumb_size[1] . '" />'; } else { $thumb = '<img src="' . baw_first_image(isset($bawmrp_options['first_image']) && $bawmrp_options['first_image'] == 'on' ? $id : null, $no_thumb) . '" height="' . $thumb_size[0] . '" width="' . $thumb_size[1] . '" />'; } $list[] = '<li style="' . esc_attr($style) . '" class="' . $class . '"><a href="' . esc_url(apply_filters('the_permalink', get_permalink($id))) . '">' . $thumb . '<br />' . get_the_title($id) . '</a></li>'; } $list = apply_filters('bawmrp_li', $list, ++$n); } do_action('bawmrp_last_li'); $list = apply_filters('bawmrp_list_li', $list); if ($bawmrp_options['in_content_mode'] == 'list') { $foot = '</ul></div>'; } else { $foot = '</ul></div><div style="clear:both;"></div>'; } $final = $content . $head . implode("\n", $list) . $foot; $content = apply_filters('bawmrp_posts_content', $final, $content, $head, $list, $foot); } else { $head = ''; //<div class="bawmrp"><h3>' . esc_html( $head_title ) . '</h3>'; $list = ''; //<ul><li>' . __( 'No posts found.' ) . '</li></ul>'; $foot = ''; //</div>'; $final = $content . $head . $list . $foot; $content = apply_filters('bawmrp_posts_content', $final, $content, $head, $list, $foot); } } if (!empty($list)) { set_transient($transient_name, array('head' => $head, 'list' => $list, 'foot' => $foot)); } $in_bawmrp_loop = false; return $content; }