Completed
Push — try/full-sync-term-relationshi... ( 6e7921 )
by Marin
19:39 queued 12:33
created

Term_Relationships::enqueue_full_sync_actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * Term relationships sync module.
4
 *
5
 * @package automattic/jetpack-sync
6
 */
7
8
namespace Automattic\Jetpack\Sync\Modules;
9
10
use Automattic\Jetpack\Sync\Defaults;
11
use Automattic\Jetpack\Sync\Settings;
12
13
/**
14
 * Class to handle sync for term relationships.
15
 */
16
class Term_Relationships extends Module {
17
	/**
18
	 * Sync module name.
19
	 *
20
	 * @access public
21
	 *
22
	 * @return string
23
	 */
24
	public function name() {
25
		return 'term_relationships';
26
	}
27
28
	/**
29
	 * Initialize term relationships action listeners for full sync.
30
	 *
31
	 * @access public
32
	 *
33
	 * @param callable $callable Action handler callable.
34
	 */
35
	public function init_full_sync_listeners( $callable ) {
36
		add_action( 'jetpack_full_sync_term_relationships', $callable, 10, 2 );
37
	}
38
39
	/**
40
	 * Initialize the module in the sender.
41
	 *
42
	 * @access public
43
	 */
44
	public function init_before_send() {
45
		// Full sync.
46
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_term_relationships', array( $this, 'expand_term_relationships' ) );
47
	}
48
49
	/**
50
	 * Enqueue the term relationships actions for full sync.
51
	 *
52
	 * @access public
53
	 *
54
	 * @param array   $config               Full sync configuration for this sync module.
55
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue.
56
	 * @param boolean $state                True if full sync has finished enqueueing this module, false otherwise.
57
	 * @return array Number of actions enqueued, and next module state.
58
	 */
59
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
60
		global $wpdb;
61
		return $this->enqueue_all_ids_as_action(
62
			'jetpack_full_sync_term_relationships',
63
			$wpdb->term_relationships,
64
			'object_id',
65
			'1=1',
66
			$max_items_to_enqueue,
67
			$state
68
		);
69
	}
70
71
	/**
72
	 * Retrieve an estimated number of actions that will be enqueued.
73
	 *
74
	 * @access public
75
	 *
76
	 * @param array $config Full sync configuration for this sync module.
77
	 * @return int Number of items yet to be enqueued.
78
	 */
79
	public function estimate_full_sync_actions( $config ) {
80
		global $wpdb;
81
82
		$query = "SELECT COUNT(*) FROM $wpdb->term_relationships";
83
84
		// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
85
		$count = $wpdb->get_var( $query );
86
87
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
88
	}
89
90
	/**
91
	 * Retrieve the actions that will be sent for this module during a full sync.
92
	 *
93
	 * @access public
94
	 *
95
	 * @return array Full sync actions of this module.
96
	 */
97
	public function get_full_sync_actions() {
98
		return array( 'jetpack_full_sync_term_relationships' );
99
	}
100
101
	/**
102
	 * Expand the term relationships within a hook before they are serialized and sent to the server.
103
	 *
104
	 * @access public
105
	 *
106
	 * @param array $args The hook parameters.
107
	 * @return array $args The expanded hook parameters.
108
	 */
109
	public function expand_term_relationships( $args ) {
110
		list( $term_relationships, $previous_end ) = $args;
111
112
		return array(
113
			'term_relationships' => $term_relationships,
114
			'previous_end'       => $previous_end,
115
		);
116
	}
117
}
118