Completed
Push — add/user-authentication ( 651ac4...684086 )
by
unknown
26:10 queued 18:04
created

Term_Relationships::estimate_full_sync_actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 10
rs 9.9332
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\Listener;
12
use Automattic\Jetpack\Sync\Settings;
13
14
/**
15
 * Class to handle sync for term relationships.
16
 */
17
class Term_Relationships extends Module {
18
	/**
19
	 * Sync module name.
20
	 *
21
	 * @access public
22
	 *
23
	 * @return string
24
	 */
25
	public function name() {
26
		return 'term_relationships';
27
	}
28
29
	/**
30
	 * Initialize term relationships action listeners for full sync.
31
	 *
32
	 * @access public
33
	 *
34
	 * @param callable $callable Action handler callable.
35
	 */
36
	public function init_full_sync_listeners( $callable ) {
37
		add_action( 'jetpack_full_sync_term_relationships', $callable, 10, 2 );
38
	}
39
40
	/**
41
	 * Initialize the module in the sender.
42
	 *
43
	 * @access public
44
	 */
45
	public function init_before_send() {
46
		// Full sync.
47
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_term_relationships', array( $this, 'expand_term_relationships' ) );
48
	}
49
50
	/**
51
	 * Enqueue the term relationships actions for full sync.
52
	 *
53
	 * @access public
54
	 *
55
	 * @todo This method has similarities with Automattic\Jetpack\Sync\Modules\Module::enqueue_all_ids_as_action. Refactor to keep DRY.
56
	 * @see Automattic\Jetpack\Sync\Modules\Module::enqueue_all_ids_as_action
57
	 *
58
	 * @param array   $config               Full sync configuration for this sync module.
59
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue.
60
	 * @param boolean $state                True if full sync has finished enqueueing this module, false otherwise.
61
	 * @return array Number of actions enqueued, and next module state.
62
	 */
63
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
64
		global $wpdb;
65
		$items_per_page        = 1000;
66
		$chunk_count           = 0;
67
		$previous_interval_end = $state ? $state : array(
68
			'object_id'        => '~0',
69
			'term_taxonomy_id' => '~0',
70
		);
71
		$listener              = Listener::get_instance();
72
		$action_name           = 'jetpack_full_sync_term_relationships';
73
74
		// Count down from max_id to min_id so we get term relationships for the newest posts and terms first.
75
		// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
76
		while ( $ids = $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id <= {$previous_interval_end['object_id']} AND term_taxonomy_id < {$previous_interval_end['term_taxonomy_id']} ORDER BY object_id DESC, term_taxonomy_id DESC LIMIT {$items_per_page}", ARRAY_A ) ) {
77
			// Request term relationships in groups of N for efficiency.
78
			$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE );
79
80
			// If we hit our row limit, process and return.
81 View Code Duplication
			if ( $chunk_count + count( $chunked_ids ) >= $max_items_to_enqueue ) {
82
				$remaining_items_count                      = $max_items_to_enqueue - $chunk_count;
83
				$remaining_items                            = array_slice( $chunked_ids, 0, $remaining_items_count );
84
				$remaining_items_with_previous_interval_end = $this->get_chunks_with_preceding_end( $remaining_items, $previous_interval_end );
85
				$listener->bulk_enqueue_full_sync_actions( $action_name, $remaining_items_with_previous_interval_end );
86
87
				$last_chunk = end( $remaining_items );
88
				return array( $remaining_items_count + $chunk_count, end( $last_chunk ) );
89
			}
90
			$chunked_ids_with_previous_end = $this->get_chunks_with_preceding_end( $chunked_ids, $previous_interval_end );
91
92
			$listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids_with_previous_end );
93
94
			$chunk_count += count( $chunked_ids );
95
96
			// The $ids are ordered in descending order.
97
			$previous_interval_end = end( $ids );
98
		}
99
100
		return array( $chunk_count, true );
101
	}
102
103
	/**
104
	 * Retrieve an estimated number of actions that will be enqueued.
105
	 *
106
	 * @access public
107
	 *
108
	 * @param array $config Full sync configuration for this sync module.
109
	 * @return int Number of items yet to be enqueued.
110
	 */
111
	public function estimate_full_sync_actions( $config ) {
112
		global $wpdb;
113
114
		$query = "SELECT COUNT(*) FROM $wpdb->term_relationships";
115
116
		// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
117
		$count = $wpdb->get_var( $query );
118
119
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
120
	}
121
122
	/**
123
	 * Retrieve the actions that will be sent for this module during a full sync.
124
	 *
125
	 * @access public
126
	 *
127
	 * @return array Full sync actions of this module.
128
	 */
129
	public function get_full_sync_actions() {
130
		return array( 'jetpack_full_sync_term_relationships' );
131
	}
132
133
	/**
134
	 * Expand the term relationships within a hook before they are serialized and sent to the server.
135
	 *
136
	 * @access public
137
	 *
138
	 * @param array $args The hook parameters.
139
	 * @return array $args The expanded hook parameters.
140
	 */
141
	public function expand_term_relationships( $args ) {
142
		list( $term_relationships, $previous_end ) = $args;
143
144
		return array(
145
			'term_relationships' => $term_relationships,
146
			'previous_end'       => $previous_end,
147
		);
148
	}
149
}
150