/
storage-handler.php
117 lines (107 loc) · 2.99 KB
/
storage-handler.php
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
<?php
/**
* Storage handler, which sets the correct storage for meta box objects.
*
* @package Meta Box
* @subpackage MB Relationships
*/
/**
* Storage handler class.
*/
class MBR_Storage_Handler {
/**
* Reference to relationship factory.
*
* @var MBR_Relationship_Factory
*/
protected $factory;
/**
* The storage object for relationships table.
*
* @var RWMB_Storage_Interface
*/
protected $storage;
/**
* Constructor.
*
* @param MBR_Relationship_Factory $factory Reference to relationship factory.
*/
public function __construct( MBR_Relationship_Factory $factory ) {
$this->factory = $factory;
}
/**
* Class initialize.
*/
public function init() {
add_filter( 'rwmb_get_storage', array( $this, 'filter_storage' ), 10, 3 );
add_action( 'deleted_post', array( $this, 'delete_object_data' ) );
add_action( 'deleted_user', array( $this, 'delete_object_data' ) );
add_action( 'delete_term', array( $this, 'delete_object_data' ) );
}
/**
* Filter storage object.
*
* @param RWMB_Storage_Interface $storage Storage object.
* @param string $object_type Object type.
* @param RW_Meta_Box $meta_box Meta box object.
*
* @return mixed
*/
public function filter_storage( $storage, $object_type, $meta_box ) {
global $wpdb;
if ( ! $meta_box || ! $this->is_relationships( $meta_box ) ) {
return $storage;
}
if ( ! $this->storage ) {
$this->storage = new MBR_Storage( $this->factory );
}
return $this->storage;
}
/**
* Check if meta box is relationships.
*
* @param RW_Meta_Box $meta_box Meta box object.
*
* @return bool
*/
protected function is_relationships( $meta_box ) {
return 'relationships_table' === $meta_box->storage_type;
}
/**
* Delete object data in cache and in the database.
*
* @param int $object_id Object ID.
*/
public function delete_object_data( $object_id ) {
$object_type = str_replace( array( 'deleted_', 'delete_' ), '', current_filter() );
$relationships = $this->factory->filter_by( $object_type );
foreach ( $relationships as $relationship ) {
$setting = $this->factory->get_settings( $relationship->id );
$target = null;
if ( $setting['from']['object_type'] !== $setting['to']['object_type'] ) {
$target = $setting['from']['object_type'] === $object_type ? 'from' : 'to';
}
$this->delete_object_relationships( $object_id, $relationship->id, $target );
}
}
/**
* Delete all relationships to an object.
*
* @param int $object_id ID of the object metadata is for.
* @param string $type The relationship type.
* @param string $target The relationship target.
*/
protected function delete_object_relationships( $object_id, $type, $target ) {
global $wpdb;
$sql = $target
? "DELETE FROM $wpdb->mb_relationships WHERE `type`=%s AND `$target`=%d"
: "DELETE FROM $wpdb->mb_relationships WHERE `type`=%s AND (`from`=%d OR `to`=%d)";
$wpdb->query(
$wpdb->prepare(
$sql,
$type,
$object_id
)
);
}
}