Completed
Push — add/sync-rest-2 ( 5ec2d9...380c9a )
by
unknown
08:41
created

Jetpack_Sync_Meta::delete_meta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 11
nc 2
nop 4
1
<?php
2
3
class Jetpack_Sync_Meta {
4
5
	static $sync = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sync.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
6
	static $delete = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $delete.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
7
8
	static $max_to_sync = 10;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $max_to_sync.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
9
10
	static private $meta_types = array( 'post', 'comment' );
11
12
13
	static function init() {
14
		foreach ( self::$meta_types as $type ) {
15
			add_action( 'added_' . $type . '_meta', array( __CLASS__, 'update_meta' ), 10, 4 );
16
			add_action( 'updated_' . $type . '_meta', array( __CLASS__, 'update_meta' ), 10, 4 );
17
			add_action( 'deleted_' . $type . '_meta', array( __CLASS__, 'delete_meta' ), 10, 4 );
18
		}
19
	}
20
21
	static function update_meta( $meta_ids, $object_id, $meta_key, $meta_value ) {
22
		$action_array = explode( '_', current_action() );
23
		$type = $action_array[1];
24
25
		$ignore_meta_keys = array( '_edit_lock', '_pingme', '_encloseme' );
26
		if ( in_array( $meta_key, $ignore_meta_keys ) ) {
27
			return;
28
		}
29
		$data = array( 'id' => $meta_ids, 'key' => $meta_key, $type.'_id' => $object_id, 'value' => $meta_value );
30
		$key  = ( ! is_array( $meta_ids ) ? $meta_ids : json_encode( $data ) );
31
32
		self::$sync[ $type ][ $key ] = $data;
33
		Jetpack_Sync::schedule_sync();
34
	}
35
36
	static function delete_meta( $meta_ids, $object_id, $meta_key, $meta_value ) {
37
		$action_array = explode( '_', current_action() );
38
		$type = $action_array[1];
39
		$data = array(
40
			'id'      => $meta_ids,
41
			'key'     => $meta_key,
42
			$type.'_id' => $object_id,
43
			'value'   => $meta_value,
44
		);
45
46
		$key = ( ! is_array( $meta_ids ) ? $meta_ids : json_encode( $data ) );
47
48
		self::$delete[ $type ][ $key ] = $data;
49
		Jetpack_Sync::schedule_sync();
50
	}
51
52
	static function meta_to_sync( $type ) {
53
		if ( isset( self::$sync[ $type ] ) ) {
54
			return array_values( self::$sync[ $type ] );
55
		}
56
		return array();
57
	}
58
59
	static function meta_to_delete( $type ) {
60
		if ( isset(  self::$delete[ $type ] ) ) {
61
			return array_values( self::$delete[ $type ] );
62
		}
63
		return array();
64
	}
65
}
66
67