96 lines
2.3 KiB
PHP
96 lines
2.3 KiB
PHP
<?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;
|
|
}
|
|
}
|