This commit is contained in:
Javier Casares 2026-03-02 11:22:59 +00:00
commit 5cdf7f48e5
29 changed files with 6526 additions and 0 deletions

View file

@ -0,0 +1,96 @@
<?php
/**
* MLP5 site relations writer.
*
* Writes site connection rows to the MLP5 site_relations table.
*
* MLP5 site_relations schema (identical to MLP2):
* site_1 INT UNSIGNED -- lower site ID
* site_2 INT UNSIGNED -- higher site ID
* PRIMARY KEY (site_1, site_2)
*
* @package Robotstxt_Mlp2_Mlp5
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Robotstxt_Mlp2_Mlp5_Writer_Site_Relations
*/
class Robotstxt_Mlp2_Mlp5_Writer_Site_Relations {
/**
* Check whether a site relation already exists in MLP5.
*
* @param int $site_1 First site ID (will be normalised to lower).
* @param int $site_2 Second site ID.
* @return bool
*/
public static function relation_exists( int $site_1, int $site_2 ): bool {
global $wpdb;
$mlp5 = new Robotstxt_Mlp2_Mlp5_Detector_Mlp5();
$table = $mlp5->get_site_relations_table();
if ( '' === $table ) {
return false;
}
// Normalise: site_1 must be the lower value.
if ( $site_1 > $site_2 ) {
list( $site_1, $site_2 ) = array( $site_2, $site_1 );
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$found = $wpdb->get_var(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"SELECT COUNT(*) FROM `{$table}` WHERE site_1 = %d AND site_2 = %d",
$site_1,
$site_2
)
);
return (int) $found > 0;
}
/**
* Write a site relation to MLP5.
*
* Normalises site IDs (smaller value in site_1) before inserting.
* Returns false if the target table does not exist.
*
* @param int $site_1 First site ID.
* @param int $site_2 Second site ID.
* @return bool True on success, false on failure.
*/
public static function write_relation( int $site_1, int $site_2 ): bool {
global $wpdb;
$mlp5 = new Robotstxt_Mlp2_Mlp5_Detector_Mlp5();
$table = $mlp5->get_site_relations_table();
if ( '' === $table ) {
return false;
}
// Normalise.
if ( $site_1 > $site_2 ) {
list( $site_1, $site_2 ) = array( $site_2, $site_1 );
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$result = $wpdb->insert(
$table,
array(
'site_1' => $site_1,
'site_2' => $site_2,
),
array( '%d', '%d' )
);
return false !== $result;
}
}