Skip to content

Commit

Permalink
Metabox manual related posts update
Browse files Browse the repository at this point in the history
Fixes #207

The metabox for manual related posts can now be customized to perform searches without relevance. To disable relevance in searches, use the filter `crp_meta_box_manual_related_relevance` with the following code: `add_filter('crp_meta_box_manual_related_relevance', '__return_false');`
  • Loading branch information
ajaydsouza committed Dec 22, 2024
1 parent bd875ac commit 4d9714f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
55 changes: 41 additions & 14 deletions includes/admin/class-metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ public static function call_meta_box() {
// Exclude terms.
$exclude_words = isset( $post_meta['exclude_words'] ) ? $post_meta['exclude_words'] : '';

/**
* Filter the relevance of manual related posts.
*
* @since 3.6.0
*
* @param int $manual_related_relevance Search for related posts using relevance or not. Default 1.
*/
$manual_related_relevance = apply_filters( 'crp_meta_box_manual_related_relevance', 1 );

?>
<p>
<label for="crp_disable_here"><strong><?php esc_html_e( 'Disable Related Posts display:', 'contextual-related-posts' ); ?></strong></label>
Expand Down Expand Up @@ -161,7 +170,7 @@ public static function call_meta_box() {

<p>
<label for="manual_related"><strong><?php esc_html_e( 'Manual related posts:', 'contextual-related-posts' ); ?></strong></label>
<input type="text" id="crp-manual-related" name="manual-related-posts" value="" class="widefat" placeholder="<?php esc_attr_e( 'Start typing to find related posts', 'contextual-related-posts' ); ?>" />
<input type="text" id="crp-manual-related" name="manual-related-posts" value="" class="widefat" placeholder="<?php esc_attr_e( 'Start typing to find related posts', 'contextual-related-posts' ); ?>" data-wp-relevance="<?php echo absint( $manual_related_relevance ); ?>" />
<input type="hidden" id="crp-manual-related-csv" name="manual_related" value="<?php echo esc_attr( $manual_related ); ?>" class="widefat" />
</p>
<ul id="crp-post-list">
Expand Down Expand Up @@ -365,32 +374,50 @@ public static function get_posts_action() {

$search_term = isset( $_POST['search_term'] ) ? sanitize_text_field( wp_unslash( $_POST['search_term'] ) ) : '';
$postid = isset( $_POST['postid'] ) ? absint( $_POST['postid'] ) : 0;
$exclude_post_ids = isset( $_POST['exclude_post_ids'] ) ? wp_parse_id_list( wp_unslash( $_POST['exclude_post_ids'] ) ) : '';
$exclude_post_ids = isset( $_POST['exclude_post_ids'] ) ? wp_parse_id_list( wp_unslash( $_POST['exclude_post_ids'] ) ) : array();
$relevance = isset( $_POST['relevance'] ) ? (bool) $_POST['relevance'] : true;

if ( empty( $search_term ) || empty( $postid ) ) {
wp_send_json_error();
}

$args = array(
'postid' => $postid,
'posts_per_page' => 7,
'keyword' => $search_term,
'exclude_post_ids' => $exclude_post_ids,
'manual_related' => 0,
'include_words' => $search_term,
'match_content' => false,
);
if ( is_numeric( $search_term ) ) {
$args['include_post_ids'] = array( $search_term );
if ( ! $relevance ) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 7,
's' => $search_term,
'post__not_in' => array_merge( array( $postid ), $exclude_post_ids ),
);
if ( is_numeric( $search_term ) ) {
$args['p'] = absint( $search_term );
unset( $args['s'] );
}
$posts = get_posts( $args );
} else {
$args = array(
'postid' => $postid,
'posts_per_page' => 7,
'keyword' => $search_term,
'exclude_post_ids' => $exclude_post_ids,
'manual_related' => 0,
'include_words' => $search_term,
'match_content' => false,
);
if ( is_numeric( $search_term ) ) {
$args['include_post_ids'] = array( $search_term );
}
$posts = \get_crp_posts( $args );
}

$posts = \get_crp_posts( $args );
$result = array();
foreach ( $posts as $post ) {
$result[] = array(
'id' => $post->ID,
'title' => sprintf( '%1$s (%2$s)', $post->post_title, $post->ID ),
);
}

echo wp_json_encode( $result );
wp_die();
}
Expand Down
5 changes: 4 additions & 1 deletion includes/admin/js/metabox.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jQuery(document).ready(function ($) {
ajaxAction: 'crp_get_posts_action',
postList: '#crp-post-list',
hiddenField: '#crp-manual-related-csv',
relevance: 1,
};

// Get the AJAX action and delete it from the options object
Expand All @@ -24,6 +25,7 @@ jQuery(document).ready(function ($) {
delete (options.postList);
const hiddenField = options.hiddenField ? $(options.hiddenField) : $(defaults.hiddenField);
delete (options.hiddenField);
const relevance = options.relevance || $element.attr('data-wp-relevance') || defaults.relevance;

// Get the post IDs from the hidden field
function getManualRelatedIDs() {
Expand Down Expand Up @@ -62,7 +64,8 @@ jQuery(document).ready(function ($) {
search_term: request.term,
crp_get_posts_nonce: crp_metabox.nonce,
postid: postid,
exclude_post_ids: getManualRelatedIDs()
exclude_post_ids: getManualRelatedIDs(),
relevance: relevance
}
}).done(function (data) {
// Map the response data to an array of post titles
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/js/metabox.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ If you enable thumbnails, the plugin will try to find the correct thumbnail in t
* Auto-insertion of default and global settings attributes, with an option to disable this in the **List tuning** tab.

* Modifications:
* The metabox for manual related posts can now be customized to perform searches without relevance. To disable relevance in searches, use the filter `crp_meta_box_manual_related_relevance` with the following code: `add_filter('crp_meta_box_manual_related_relevance', '__return_false');`
* Show admin notices when the Style is set to "Rounded Thumbnails", "Rounded Thumbnails with Grid", or "Text Only" in the Settings page. A notice will appear below the affected settings, indicating that these options cannot be modified.
* Updated Freemius SDK to 2.10.1

Expand Down

0 comments on commit 4d9714f

Please sign in to comment.