-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity-link-engine.php
More file actions
118 lines (110 loc) · 4.26 KB
/
Copy pathentity-link-engine.php
File metadata and controls
118 lines (110 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Plugin Name: Entity Link Engine
* Plugin URI: https://eullrich.com/
* Description: Automatic internal linking with entity mapping and fan-out query retrieval. Maps entities in your content to existing posts, generates fan-out retrieval queries and inserts score-ranked internal links — not just random links.
* Version: 1.0.0
* Requires at least: 6.0
* Requires PHP: 7.4
* Tested up to: 7.0
* Author: Eugen Ullrich
* Author URI: https://eullrich.com
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: entity-link-engine
*
* Entity Link Engine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* Entity Link Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* @package EntityLinkEngine
*/
defined( 'ABSPATH' ) || exit;
define( 'ELINK_VERSION', '1.0.0' );
define( 'ELINK_FILE', __FILE__ );
define( 'ELINK_DIR', plugin_dir_path( __FILE__ ) );
define( 'ELINK_URL', plugin_dir_url( __FILE__ ) );
define( 'ELINK_BASENAME', plugin_basename( __FILE__ ) );
require_once ELINK_DIR . 'includes/class-install.php';
require_once ELINK_DIR . 'includes/class-entity-map.php';
require_once ELINK_DIR . 'includes/class-fanout-query.php';
require_once ELINK_DIR . 'includes/class-retriever.php';
require_once ELINK_DIR . 'includes/class-link-insert.php';
require_once ELINK_DIR . 'includes/class-embedder.php';
require_once ELINK_DIR . 'includes/class-engine.php';
require_once ELINK_DIR . 'includes/class-report.php';
require_once ELINK_DIR . 'includes/class-rest.php';
require_once ELINK_DIR . 'includes/class-admin.php';
/**
* Hook the engine into post lifecycle.
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an update.
*/
function elink_on_save_post( $post_id, $post, $update ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
return;
}
// Re-entrancy guard: the engine itself calls wp_update_post(), which fires
// save_post again. The transient set right before that update suppresses
// the follow-up run (which would be a no-op anyway).
if ( get_transient( 'elink_just_ran_' . $post_id ) ) {
delete_transient( 'elink_just_ran_' . $post_id );
return;
}
if ( ! $post instanceof WP_Post || 'publish' !== $post->post_status ) {
return;
}
// Skip bulk edit / quick edit saves that do not carry real content edits.
if ( isset( $_POST['bulk_edit'] ) || isset( $_POST['action'] ) && 'inline-save' === $_POST['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification
return;
}
$settings = ELINK_Install::get_settings();
if ( empty( $settings['auto_on_publish'] ) ) {
return;
}
$engine = new ELINK_Engine();
$engine->run( $post_id );
}
add_action( 'save_post', 'elink_on_save_post', 20, 3 );
// Register activation / deactivation.
register_activation_hook( ELINK_FILE, array( 'ELINK_Install', 'activate' ) );
register_deactivation_hook( ELINK_FILE, array( 'ELINK_Install', 'deactivate' ) );
// Admin hooks (menus, meta box, REST, cron tick).
if ( is_admin() ) {
new ELINK_Admin();
new ELINK_REST();
}
add_action( 'elink_bulk_tick', 'elink_bulk_tick_callback' );
/**
* Cron callback for the bulk run.
*
* @param array $args Tick arguments.
*/
function elink_bulk_tick_callback( $args ) {
$engine = new ELINK_Engine();
$engine->bulk_tick( $args );
}
/**
* Optional: allow other plugins to extend the entity index with
* their own entities. Filter returns array of
* array( 'entity_label' => string, 'target_post_id' => int, 'priority' => int ).
*
* @return array
*/
function elink_manual_entities() {
$stored = get_option( 'elink_entities_manual', array() );
$stored = is_array( $stored ) ? $stored : array();
$filtered = apply_filters( 'elink_manual_entities', $stored );
return is_array( $filtered ) ? $filtered : $stored;
}