Skip to content

Commit 3b65e37

Browse files
committed
refactor: rename fulltext index names and improve index management
1 parent c5a2617 commit 3b65e37

5 files changed

Lines changed: 119 additions & 59 deletions

File tree

includes/admin/class-admin-notices-api.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
/**
1818
* Class to handle admin notices.
1919
*
20-
* @since 4.0.4
20+
* @since 4.1.0
2121
*/
2222
class Admin_Notices_API {
2323

2424
/**
2525
* Array of registered notices.
2626
*
27-
* @since 4.0.4
27+
* @since 4.1.0
2828
*
2929
* @var array Registered notices.
3030
*/
@@ -33,7 +33,7 @@ class Admin_Notices_API {
3333
/**
3434
* Constructor class.
3535
*
36-
* @since 4.0.4
36+
* @since 4.1.0
3737
*/
3838
public function __construct() {
3939
Hook_Registry::add_action( 'admin_notices', array( $this, 'display_notices' ) );
@@ -43,7 +43,7 @@ public function __construct() {
4343
/**
4444
* Register a new notice.
4545
*
46-
* @since 4.0.4
46+
* @since 4.1.0
4747
*
4848
* @param array $notice {
4949
* Notice arguments.
@@ -82,7 +82,7 @@ public function register_notice( array $notice ) {
8282
/**
8383
* Display registered notices.
8484
*
85-
* @since 4.0.4
85+
* @since 4.1.0
8686
*/
8787
public function display_notices() {
8888
$screen = get_current_screen();
@@ -130,7 +130,7 @@ public function display_notices() {
130130
/**
131131
* Print scripts for notice dismissal.
132132
*
133-
* @since 4.0.4
133+
* @since 4.1.0
134134
*/
135135
private function print_scripts() {
136136
static $printed = false;
@@ -164,7 +164,7 @@ private function print_scripts() {
164164
/**
165165
* Handle notice dismissal via AJAX.
166166
*
167-
* @since 4.0.4
167+
* @since 4.1.0
168168
*/
169169
public function handle_notice_dismissal() {
170170
check_ajax_referer( 'crp_dismiss_notice', 'nonce' );
@@ -192,7 +192,7 @@ public function handle_notice_dismissal() {
192192
/**
193193
* Check if a notice has been dismissed.
194194
*
195-
* @since 4.0.4
195+
* @since 4.1.0
196196
*
197197
* @param string $notice_id Notice ID.
198198
* @return bool Whether the notice has been dismissed.

includes/admin/class-db.php

Lines changed: 72 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public function __construct() {
3535
*/
3636
public static function get_fulltext_indexes() {
3737
$indexes = array(
38-
'crp_related' => '(post_title, post_content)',
39-
'crp_related_title' => '(post_title)',
40-
'crp_related_content' => '(post_content)',
38+
'wz_title_content' => '(post_title, post_content)',
39+
'wz_title' => '(post_title)',
40+
'wz_content' => '(post_content)',
4141
);
4242

4343
/**
@@ -51,29 +51,34 @@ public static function get_fulltext_indexes() {
5151
}
5252

5353
/**
54-
* Create fulltext indexes on the posts table.
54+
* Get the list of old fulltext indexes.
5555
*
56-
* @since 3.5.0
56+
* @since 4.1.0
5757
*
58-
* @param string $action Action to perform - create or delete.
58+
* @return array Array of fulltext indexes with their respective columns.
5959
*/
60-
public static function fulltext_indexes( $action ) {
61-
global $wpdb;
62-
63-
$indexes = self::get_fulltext_indexes();
60+
public static function get_old_fulltext_indexes() {
61+
return array(
62+
'crp_related' => '(post_title, post_content)',
63+
'crp_related_title' => '(post_title)',
64+
'crp_related_content' => '(post_content)',
65+
);
66+
}
6467

65-
foreach ( $indexes as $index => $columns ) {
66-
$index_exists = self::is_index_installed( $index );
68+
/**
69+
* Install a fulltext index on the posts table.
70+
*
71+
* @since 4.1.0
72+
*
73+
* @param string $index Index name.
74+
* @param string $columns Columns to be indexed.
75+
* @return void
76+
*/
77+
public static function install_fulltext_index( $index, $columns ) {
78+
global $wpdb;
6779

68-
if ( 'create' === $action && ! $index_exists ) {
69-
$index = esc_sql( $index );
70-
$columns = esc_sql( $columns );
71-
$wpdb->query( "ALTER TABLE {$wpdb->posts} ADD FULLTEXT $index $columns" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
72-
} elseif ( 'delete' === $action && $index_exists ) {
73-
$index = esc_sql( $index );
74-
$wpdb->query( "ALTER TABLE {$wpdb->posts} DROP INDEX $index" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
75-
}
76-
}
80+
// Install the fulltext index if it doesn't exist.
81+
$wpdb->query( "ALTER TABLE {$wpdb->posts} ADD FULLTEXT {$index} {$columns};" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
7782
}
7883

7984
/**
@@ -82,7 +87,15 @@ public static function fulltext_indexes( $action ) {
8287
* @since 3.5.0
8388
*/
8489
public static function create_fulltext_indexes() {
85-
call_user_func( array( __CLASS__, 'fulltext_indexes' ), 'create' );
90+
// Get the list of fulltext indexes.
91+
$indexes = self::get_fulltext_indexes();
92+
93+
// Loop through the indexes and create them if not exist.
94+
foreach ( $indexes as $index => $columns ) {
95+
if ( ! self::is_index_installed( $index ) ) {
96+
self::install_fulltext_index( $index, $columns );
97+
}
98+
}
8699
}
87100

88101
/**
@@ -91,24 +104,47 @@ public static function create_fulltext_indexes() {
91104
* @since 3.5.0
92105
*/
93106
public static function delete_fulltext_indexes() {
94-
call_user_func( array( __CLASS__, 'fulltext_indexes' ), 'delete' );
107+
global $wpdb;
108+
109+
$indexes = array_merge( self::get_fulltext_indexes(), self::get_old_fulltext_indexes() );
110+
111+
foreach ( $indexes as $index => $columns ) {
112+
if ( self::is_index_installed( $index ) ) {
113+
$index = esc_sql( $index );
114+
$wpdb->query( "ALTER TABLE {$wpdb->posts} DROP INDEX $index" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
115+
}
116+
}
95117
}
96118

97119
/**
98120
* Check if a fulltext index already exists on the posts table.
99121
*
100-
* @since 4.0.1
122+
* @since 4.1.0
101123
*
102124
* @param string $index Index name.
103125
* @return bool True if the index exists, false otherwise.
104126
*/
105127
public static function is_index_installed( $index ) {
106128
global $wpdb;
107129

130+
$new_indexes = self::get_fulltext_indexes();
131+
$old_indexes = self::get_old_fulltext_indexes();
132+
133+
// Find the corresponding old index name if the given index is a new one.
134+
$old_index_name = '';
135+
if ( in_array( $index, array_keys( $new_indexes ), true ) ) {
136+
$key = array_search( $index, array_keys( $new_indexes ), true );
137+
$old_index_keys = array_keys( $old_indexes );
138+
if ( isset( $old_index_keys[ $key ] ) ) {
139+
$old_index_name = $old_index_keys[ $key ];
140+
}
141+
}
142+
108143
$index_exists = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
109144
$wpdb->prepare(
110-
"SHOW INDEX FROM {$wpdb->posts} WHERE Key_name = %s",
111-
$index
145+
"SHOW INDEX FROM {$wpdb->posts} WHERE Key_name = %s OR Key_name = %s",
146+
$index,
147+
$old_index_name
112148
)
113149
);
114150

@@ -118,7 +154,7 @@ public static function is_index_installed( $index ) {
118154
/**
119155
* Check if all fulltext indexes are installed.
120156
*
121-
* @since 4.0.1
157+
* @since 4.1.0
122158
*
123159
* @return bool True if all fulltext indexes are installed, false if any are missing.
124160
*/
@@ -137,7 +173,7 @@ public static function is_fulltext_index_installed() {
137173
/**
138174
* Check the status of all fulltext indexes.
139175
*
140-
* @since 4.0.1
176+
* @since 4.1.0
141177
*
142178
* @return array Array of index statuses indicating whether they are installed.
143179
*/
@@ -148,9 +184,14 @@ public static function check_fulltext_indexes() {
148184

149185
// Check if each index is installed and add to the report.
150186
foreach ( $indexes as $index => $columns ) {
151-
$statuses[ $index ] = self::is_index_installed( $index )
152-
? '<span style="color: #006400;">' . __( 'Installed', 'contextual-related-posts' ) . '</span>'
153-
: '<span style="color: #8B0000;">' . __( 'Not Installed', 'contextual-related-posts' ) . '</span>';
187+
$is_installed = self::is_index_installed( $index );
188+
189+
$statuses[ $index ] = array(
190+
'columns' => $columns,
191+
'status' => $is_installed
192+
? '<span style="color: #006400;">' . __( 'Installed', 'contextual-related-posts' ) . '</span>'
193+
: '<span style="color: #8B0000;">' . __( 'Not Installed', 'contextual-related-posts' ) . '</span>',
194+
);
154195
}
155196

156197
/**

includes/admin/class-tools-page.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function render_page() {
6767
if ( ( isset( $_POST['crp_recreate_indices'] ) ) && ( check_admin_referer( 'crp-tools-settings' ) ) ) {
6868
Db::delete_fulltext_indexes();
6969
Db::create_fulltext_indexes();
70-
add_settings_error( 'crp-notices', '', esc_html__( 'Indices have been recreated', 'contextual-related-posts' ), 'updated' );
70+
add_settings_error( 'crp-notices', '', esc_html__( 'Indices have been recreated', 'contextual-related-posts' ), 'success' );
7171
}
7272

7373
/* Message for successful file import */
@@ -107,15 +107,22 @@ public static function render_page() {
107107
</div>
108108

109109
<div class="postbox">
110-
<h2 class="hndle"><span><?php esc_html_e( 'Recreate Indices', 'contextual-related-posts' ); ?></span></h2>
110+
<h2 class="hndle"><span><?php esc_html_e( 'Recreate FULLTEXT index', 'contextual-related-posts' ); ?></span></h2>
111111
<div class="inside">
112112
<form method="post">
113113
<p>
114-
<input name="crp_recreate_indices" type="submit" id="crp_recreate_indices" value="<?php esc_attr_e( 'Recreate Indices', 'contextual-related-posts' ); ?>" class="button button-secondary" />
114+
<?php
115+
printf(
116+
'<input name="crp_recreate_indices" type="submit" id="crp_recreate_indices" class="button button-secondary" value="%2$s" onclick="if ( ! confirm(\'%1$s\') ) return false;" />',
117+
esc_attr__( 'Are you sure you want to recreate the index?', 'contextual-related-posts' ),
118+
esc_attr__( 'Recreate Index', 'contextual-related-posts' )
119+
);
120+
?>
115121
</p>
116122
<p class="description">
117-
<?php esc_html_e( 'Deletes and recreates the FULLTEXT index in the posts table. If the above function gives an error, then you can run the below code in phpMyAdmin or Adminer. Remember to backup your database first!', 'contextual-related-posts' ); ?>
123+
<?php esc_html_e( 'Recreate the FULLTEXT index that Contextual Related Posts uses to get the relevant related posts. This might take a lot of time to regenerate if you have a lot of posts.', 'contextual-related-posts' ); ?>
118124
</p>
125+
<p class="description"><?php esc_html_e( 'If the Recreate Index button fails, please run the following queries in phpMyAdmin or Adminer. Remember to backup your database first!', 'contextual-related-posts' ); ?></p>
119126
<div class="crp-code-wrapper">
120127
<?php $sql_queries = self::recreate_indices_sql(); ?>
121128
<pre id="crp-indices-sql"><code><?php echo implode( "\n", array_map( 'esc_html', $sql_queries ) ); ?></code></pre>
@@ -189,21 +196,32 @@ public static function render_page() {
189196
}
190197

191198
/**
192-
* Retrieves the SQL code to recreate the PRIMARY KEY.
199+
* Retrieves the SQL code to recreate the fulltext indexes.
193200
*
194201
* @since 3.5.0
195202
*/
196203
public static function recreate_indices_sql() {
197204
global $wpdb;
198205

199-
$sql = array(
200-
"ALTER TABLE {$wpdb->posts} DROP INDEX crp_related;",
201-
"ALTER TABLE {$wpdb->posts} ADD FULLTEXT crp_related (post_title, post_content);",
202-
"ALTER TABLE {$wpdb->posts} DROP INDEX crp_related_title;",
203-
"ALTER TABLE {$wpdb->posts} ADD FULLTEXT crp_related_title (post_title);",
204-
"ALTER TABLE {$wpdb->posts} DROP INDEX crp_related_content;",
205-
"ALTER TABLE {$wpdb->posts} ADD FULLTEXT crp_related_content (post_content);",
206-
);
206+
$old_indexes = Db::get_old_fulltext_indexes();
207+
$new_indexes = Db::get_fulltext_indexes();
208+
$all_indexes = array_keys( array_merge( $old_indexes, $new_indexes ) );
209+
210+
$sql = array();
211+
212+
// Add DROP statements for all possible indexes.
213+
foreach ( $all_indexes as $index ) {
214+
if ( Db::is_index_installed( $index ) ) {
215+
$sql[] = "ALTER TABLE {$wpdb->posts} DROP INDEX {$index};";
216+
}
217+
}
218+
219+
// Add ADD statements only for the new indexes.
220+
if ( ! empty( $new_indexes ) ) {
221+
foreach ( $new_indexes as $index => $value ) {
222+
$sql[] = "ALTER TABLE {$wpdb->posts} ADD FULLTEXT {$index} {$value};";
223+
}
224+
}
207225

208226
/**
209227
* Filter the SQL code to recreate the Fulltext indices.

includes/options-api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function crp_get_option( $key = '', $default_value = null ) {
8181
/**
8282
* Get an option from a specific blog in a multisite network.
8383
*
84-
* @since 4.0.4
84+
* @since 4.1.0
8585
*
8686
* @param int $blog_id Blog ID to fetch the option from.
8787
* @param string $key Key of the option to fetch.
@@ -111,7 +111,7 @@ function crp_get_blog_option( $blog_id, $key = '', $default_value = false ) {
111111
/**
112112
* Filters a blog option value.
113113
*
114-
* @since 4.0.4
114+
* @since 4.1.0
115115
*
116116
* @param mixed $value The option value.
117117
* @param int $blog_id Blog ID.

readme.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Tags: related posts, related, contextual related posts, similar posts, seo
33
Contributors: webberzone, ajay
44
Donate link: https://wzn.io/donate-crp
5-
Stable tag: 4.0.3
6-
Requires at least: 6.3
5+
Stable tag: 4.1.0
6+
Requires at least: 6.5
77
Tested up to: 6.8
88
Requires PHP: 7.4
99
License: GPLv2 or later
@@ -164,12 +164,13 @@ If you enable thumbnails, the plugin will try to find the correct thumbnail in t
164164

165165
== Changelog ==
166166

167-
= 4.0.4 =
167+
= 4.1.0 =
168168

169169
* Modifications:
170170
* Renamed `CRP_VERSION`, `CRP_PLUGIN_FILE`, CRP_PLUGIN_DIR` and `CRP_PLUGIN_URL` constants to `WZ_CRP_VERSION`, `WZ_CRP_PLUGIN_FILE`, `WZ_CRP_PLUGIN_DIR` and `WZ_CRP_PLUGIN_URL` respectively to avoid conflicts with other plugins.
171171
* New function `crp_get_blog_option()` to fetch an option from a specific blog in WordPress multisite.
172172
* Better handling of options if they haven't been set.
173+
* Fulltext indexes are now named `wz_title_content`, `wz_title`, and `wz_content` to ensure compatibility and optimize database space, especially when using Contextual Related Posts. After updating to this version, please recreate the indexes to benefit from the changes—until then, the plugin will use the previous index names.
173174
* [Pro] Improved the UI and functionality of the Custom Table indexing process.
174175
* [Pro] Multsite Settings page for Enhanced Content Search Index (ECSI) has been modified.
175176

@@ -229,5 +230,5 @@ For the changelog of earlier versions, please refer to the separate changelog.tx
229230

230231
== Upgrade Notice ==
231232

232-
= 4.0.3 =
233-
Bugs fixed. Please read the changelog and the release post for more details.
233+
= 4.1.0 =
234+
Important plugin constants renamed to prevent conflicts, improved multisite support, and fixes for ordering issues, WPML conflicts, and Exclude Posts functionality. Custom Table indexing UI enhanced in Pro version.

0 commit comments

Comments
 (0)