Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Temp Fix Automatic Linking Not Working. This MU will allow to define max number posts (SMARTCRAWL_AUTO_LINKING_POSTS_LIMIT) for auto linking default is 2000. And also limit query only for public post type.
<?php
/**
* Plugin Name: [SmartCrawl Pro] - Fix Automatic Linking Not Working
* Description: [SmartCrawl Pro] - Fix Automatic Linking Not Working. This MU will allow to define max number posts (SMARTCRAWL_AUTO_LINKING_POSTS_LIMIT) for auto linking default is 3000. And also limit query only for public post type. - 1135131965625303
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'plugins_loaded', 'wpmudev_sc_tempfix_automatic_linking_not_working_func', 100 );
function wpmudev_sc_tempfix_automatic_linking_not_working_func() {
if( defined('SMARTCRAWL_VERSION') && class_exists( 'Smartcrawl_Loader' ) ){
// quit on admin screen
if( is_admin() && ! wp_doing_ajax() ) return;
class WPMUDEV_SC_Improve_Auto_Liking_Query{
private $save_query;
private $cpt_char_limit;
public function __construct(){
$options = Smartcrawl_Settings::get_options();
if( empty( $options['autolinks'] ) ) return;
add_filter( 'query', array( $this, 'change_automatic_linking_query' ) );
}
public function automatic_linking_get_query_posts(){
if( is_null( $this->save_query ) ){
global $wpdb;
$options = Smartcrawl_Settings::get_options();
$cpt_char_limit = ! empty( $options['cpt_char_limit'] ) ? (int) $options['cpt_char_limit'] : false;
$cpt_char_limit = (int) $cpt_char_limit ? (int) $cpt_char_limit : intval( SMARTCRAWL_AUTOLINKS_DEFAULT_CHAR_LIMIT );
$this->save_query = $wpdb->prepare(
"SELECT post_title, ID, post_type FROM {$wpdb->posts} WHERE post_status = 'publish' AND LENGTH(post_title)>=%d ORDER BY LENGTH(post_title) DESC LIMIT 2000",
$cpt_char_limit
);
$this->cpt_char_limit = $cpt_char_limit;
}
return $this->save_query;
}
public function change_automatic_linking_query( $query ){
global $post;
if( ! empty( $post->post_type ) && strpos( $query, 'LENGTH' ) !== false && $query === $this->automatic_linking_get_query_posts() ){
global $wpdb;
$limit = (int) ( defined('SMARTCRAWL_AUTO_LINKING_POSTS_LIMIT') ? SMARTCRAWL_AUTO_LINKING_POSTS_LIMIT : 0 );
$limit = $limit > 0 ? $limit : 3000;
$exclusions = array(
'revision',
'nav_menu_item',
'attachment',
);
$public_post_types = array_diff( get_post_types( array(
'public' => true,
'exclude_from_search' => false
) ), $exclusions );
$public_post_types = join("','", $public_post_types );
$query = $wpdb->prepare(
"SELECT post_title, ID, post_type FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ('{$public_post_types}') AND LENGTH(post_title)>=%d ORDER BY LENGTH(post_title) DESC LIMIT %d",
$this->cpt_char_limit,
$limit
);
}
return $query;
}
}
$run = new WPMUDEV_SC_Improve_Auto_Liking_Query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.