File size: 1,442 Bytes
f51c224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php
defined( 'ABSPATH' ) || exit;

class SA_Orch_Link {

    public static function write( array $args ) {
        global $wpdb;

        $arbitration_id = isset( $args['arbitration_id'] ) ? (int) $args['arbitration_id'] : 0;
        $plan_surface   = isset( $args['plan_surface'] )   ? trim( (string) $args['plan_surface'] )   : '';
        $plan_ref       = isset( $args['plan_ref'] )       ? trim( (string) $args['plan_ref'] )       : '';
        $link_kind      = isset( $args['link_kind'] )      ? trim( (string) $args['link_kind'] )      : 'derived';

        if ( $arbitration_id === 0 ) {
            return new WP_Error( 'missing_arbitration_id', 'arbitration_id is required' );
        }
        if ( $plan_surface === '' || $plan_ref === '' ) {
            return new WP_Error( 'missing_plan_ref', 'plan_surface and plan_ref are required' );
        }

        $created_at = current_time( 'mysql', true );

        $table = $wpdb->prefix . SA_ORCH_DB_PREFIX . 'arbitration_plan_link';
        $ok = $wpdb->insert( $table, [
            'arbitration_id' => $arbitration_id,
            'plan_surface'   => $plan_surface,
            'plan_ref'       => $plan_ref,
            'link_kind'      => $link_kind,
            'created_at'     => $created_at,
        ] );

        if ( $ok === false ) {
            return new WP_Error( 'db_insert_failed', $wpdb->last_error );
        }

        return (int) $wpdb->insert_id;
    }
}