Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance field indexing and schema management, and update README and c… #42

Merged
merged 2 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file.

## [0.2.3] - 2025-03-09

### Changed

- Enhanced field indexing to strictly respect settings configuration
- Improved schema manager to only include explicitly selected fields
- Refined factory class to ensure proper field filtering from settings
- Added filter `wp_loupe_field_{$field_name}` to allow field modification.

## [0.2.2] - 2025-03-08

### Changed
Expand Down Expand Up @@ -252,4 +261,5 @@ All notable changes to this project will be documented in this file.
[0.1.7]: https://github.com/soderlind/wp-loupe/releases/tag/0.1.7
[0.2.0]: https://github.com/soderlind/wp-loupe/releases/tag/0.2.0
[0.2.1]: https://github.com/soderlind/wp-loupe/releases/tag/0.2.1
[0.2.2]: https://github.com/soderlind/wp-loupe/releases/tag/0.2.2
[0.2.2]: https://github.com/soderlind/wp-loupe/releases/tag/0.2.2
[0.2.3]: https://github.com/soderlind/wp-loupe/releases/tag/0.2.3
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,14 @@ This filter allows you to index posts and pages that are protected by a password
add_filter( 'wp_loupe_index_protected','__return_true' );
```

### `wp_loupe_schema_content`
### `wp_loupe_field_{$field_name}`

This filter allows you to modify the content before it's indexed. By default, removes HTML comments from content (i.e. remove WordPress block comments).
This filter allows you to change the field content before it is indexed.

By default, the following is used to remove HTML tags and comments from `post_content`. Among others, it removes the WordPress block comments.

```php
add_filter('wp_loupe_schema_content', function($content) {
// Modify content before indexing
$content = preg_replace('/<!--(.*?)-->/s', '', $content);
return $content;
});
add_filter( 'wp_loupe_field_post_content', 'wp_strip_all_tags' );
```

### `wp_loupe_schema_{$post_type}`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "wordpress-plugin",
"description": "Search engine for WordPress. It uses the Loupe search engine to create a search index for your posts and pages and to search the index.",
"homepage": "https://github.com/soderlind/wp-loupe",
"version": "0.2.2",
"version": "0.2.3",
"license": "GPL-2.0+",
"authors": [
{
Expand Down
5 changes: 3 additions & 2 deletions includes/class-wp-loupe-indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function __construct( $post_types ) {

$this->db = WP_Loupe_DB::get_instance();
$this->schema_manager = new WP_Loupe_Schema_Manager();
$this->init();
$this->register_hooks();
$this->init();
}

/**
Expand All @@ -44,6 +44,7 @@ private function register_hooks() {
}
add_action( 'wp_trash_post', array( $this, 'trash_post' ), 10, 2 );
add_action( 'admin_init', array( $this, 'handle_reindex' ) );
add_filter( 'wp_loupe_field_post_content', 'wp_strip_all_tags' );
}

/**
Expand Down Expand Up @@ -274,7 +275,7 @@ private function prepare_document( \WP_Post $post ): array {
}

if ( property_exists( $post, $field_name ) ) {
$document[ $field_name ] = $post->{$field_name};
$document[ $field_name ] = apply_filters( "wp_loupe_field_{$field_name}", $post->{$field_name} );
} elseif ( strpos( $field_name, 'taxonomy_' ) === 0 ) {
$taxonomy = substr( $field_name, 9 );
$terms = wp_get_post_terms( $post->ID, $taxonomy, array( 'fields' => 'names' ) );
Expand Down
3 changes: 3 additions & 0 deletions includes/class-wp-loupe-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ private function setup_post_types() {
* @return void
*/
private function init_components() {

new WP_Loupe_Updater();
new WPLoupe_Settings_Page();

$this->search = new WP_Loupe_Search( $this->post_types );
$this->indexer = new WP_Loupe_Indexer( $this->post_types );
}
Expand Down
14 changes: 7 additions & 7 deletions includes/class-wp-loupe-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,35 +323,36 @@ public function enqueue_admin_assets($hook) {
return;
}

$version = WP_Loupe_Utils::get_version_number();

// Register and enqueue Select2
wp_register_style(
'select2css',
WP_LOUPE_URL . 'lib/css/select2.min.css',
[],
'4.0.13'
$version
);

wp_register_script(
'select2',
WP_LOUPE_URL . 'lib/js/select2.min.js',
['jquery'],
'4.0.13',
$version,
true
);

// Register and enqueue admin assets
wp_register_style(
'wp-loupe-admin',
WP_LOUPE_URL . 'lib/css/admin.css',
['select2css'],
WP_LOUPE_VERSION
$version
);

wp_register_script(
'wp-loupe-admin',
WP_LOUPE_URL . 'lib/js/admin.js',
['jquery', 'select2', 'wp-api-fetch'],
WP_LOUPE_VERSION,
$version,
true
);

Expand Down Expand Up @@ -452,5 +453,4 @@ public function add_help_tabs() {
)
);
}
}
new WPLoupe_Settings_Page();
}
12 changes: 12 additions & 0 deletions includes/class-wp-loupe-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ public static function dump( $var ) {
}
}

// return version number based upon plugin file version
/**
* Get version number based upon plugin version
*
* @since 0.0.1
* @return string Version number
*/
public static function get_version_number() {
$plugin_data = get_plugin_data( WP_LOUPE_FILE );
$version = $plugin_data[ 'Version' ];
return $version;
}

/**
* Remove transients from the database
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wp-loupe",
"version": "0.2.2",
"version": "0.2.3",
"description": "Enhance the search functionality of your WordPress site with WP Loupe.",
"main": "index.js",
"scripts": {
Expand Down
23 changes: 14 additions & 9 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: search, loupe, posts, pages, custom post types, typo-tolerant, fast search
Requires at least: 6.3
Requires PHP: 8.1
Tested up to: 6.7
Stable tag: 0.2.2
Stable tag: 0.2.3
Donate link: https://paypal.me/PerSoderlind
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -85,10 +85,6 @@ Default: WP_CONTENT_DIR . '/wp-loupe-db'
Modifies which post types are included in search.
Default: ['post', 'page']

= wp_loupe_filterable_attribute_{$post_type} =
Customizes searchable attributes per post type.
Default: ['post_title', 'post_date', 'post_content']

= wp_loupe_posts_per_page =
Controls search results per page.
Default: WordPress "Blog pages show at most" setting
Expand All @@ -97,14 +93,23 @@ Default: WordPress "Blog pages show at most" setting
Controls indexing of password-protected posts.
Default: false

= wp_loupe_schema_content =
Modifies content before indexing.
Default: Removes HTML comments
= wp_loupe_field_{$field_name} =
Modifies a field before indexing.
Default: Removes HTML comments from `post_content`

= wp_loupe_schema_{$post_type} =
Customizes the schema for a post type.

For usage examples, see the plugin's README.md file.
For usage examples, see the [filter documentation at GitHub](https://github.com/soderlind/wp-loupe?tab=readme-ov-file#filters).

== Changelog ==

= 0.2.3 =
* Enhanced field indexing to strictly respect settings configuration
* Improved schema manager to only include explicitly selected fields
* Refined factory class to ensure proper field filtering from settings
* Added filter `wp_loupe_field_{$field_name}` to allow field modification.

= 0.2.2 =
* Changed: Modified field indexing to only include explicitly selected fields in settings
* Changed: Updated schema manager to respect indexable field settings
Expand Down
18 changes: 16 additions & 2 deletions wp-loupe.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Plugin Name: WP Loupe
* Plugin URI: https://github.com/soderlind/wp-loupe
* Description: Enhance the search functionality of your WordPress site with WP Loupe.
* Version: 0.2.2
* Version: 0.2.3
* Author: Per Soderlind
* Author URI: https://soderlind.no
* License: GPL-2.0+
Expand All @@ -30,7 +30,6 @@
}

define( 'WP_LOUPE_FILE', __FILE__ );
define( 'WP_LOUPE_VERSION', '0.1.7' );
define( 'WP_LOUPE_NAME', plugin_basename( WP_LOUPE_FILE ) );
define( 'WP_LOUPE_PATH', plugin_dir_path( WP_LOUPE_FILE ) );
define( 'WP_LOUPE_URL', plugin_dir_url( WP_LOUPE_FILE ) );
Expand All @@ -41,6 +40,21 @@
* Initialize plugin
*/
function init() {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}

// if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
// return;
// }
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
// don't run the setup on heartbeat requests
if ( defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_SERVER['HTTP_X_WP_NONCE'] ) ) {
return;
}

WP_Loupe_Loader::get_instance();
if ( ! WP_Loupe_Utils::has_sqlite() ) {
return;
Expand Down