| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Basic methods implemented by Jetpack Sync extensions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | abstract class Jetpack_Sync_Module { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	const ARRAY_CHUNK_SIZE = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	abstract public function name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	public function get_object_by_id( $object_type, $id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	// override these to set up listeners and set/reset data/defaults | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	public function init_listeners( $callable ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	public function init_full_sync_listeners( $callable ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	public function init_before_send() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	public function set_defaults() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	public function reset_data() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		// in subclasses, return the number of actions enqueued, and next module state (true == done) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		return array( 0, true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	public function estimate_full_sync_actions( $config ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		// in subclasses, return the number of items yet to be enqueued | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	public function get_full_sync_actions() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		return array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	protected function count_actions( $action_names, $actions_to_count ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		return count( array_intersect( $action_names, $actions_to_count ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 	protected function get_check_sum( $values ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 		return crc32( json_encode( $values ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		if ( ! $where_sql ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			$where_sql = '1 = 1'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$items_per_page   = 1000; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$page             = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$chunk_count      = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$previous_max_id  = $state ? $state : '~0'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$listener         = Jetpack_Sync_Listener::get_instance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		// count down from max_id to min_id so we get newest posts/comments/etc first | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		while ( $ids = $wpdb->get_col( "SELECT {$id_field} FROM {$table_name} WHERE {$where_sql} AND {$id_field} < {$previous_max_id} ORDER BY {$id_field} DESC LIMIT {$items_per_page}" ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			// Request posts in groups of N for efficiency | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			// if we hit our row limit, process and return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			if ( $chunk_count + count( $chunked_ids ) >= $max_items_to_enqueue ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 				$remaining_items_count = $max_items_to_enqueue - $chunk_count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 				$remaining_items = array_slice( $chunked_ids, 0, $remaining_items_count ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 				$listener->bulk_enqueue_full_sync_actions( $action_name, $remaining_items ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 				$last_chunk = end( $remaining_items ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 				return array( $remaining_items_count + $chunk_count, end( $last_chunk ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			$listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			$chunk_count += count( $chunked_ids ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			$page += 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			$previous_max_id = end( $ids ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		return array( $chunk_count, true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		$table = _get_meta_table( $meta_type ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		$id    = $meta_type . '_id'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		if ( ! $table ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			return array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		$private_meta_whitelist_sql = "'" . implode( "','", array_map( 'esc_sql', $meta_key_whitelist ) ) . "'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		return array_map(  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			array( $this, 'unserialize_meta' ),  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			$wpdb->get_results(  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 				"SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 				" AND meta_key IN ( $private_meta_whitelist_sql ) " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 				, OBJECT ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	protected function get_term_relationships( $ids ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		global $wpdb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		return $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )', OBJECT ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	public function unserialize_meta( $meta ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		$meta->meta_value = maybe_unserialize( $meta->meta_value ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		return $meta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	public function get_objects_by_id( $object_type, $ids ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		if ( empty( $ids ) || empty( $object_type ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 			return array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		$objects = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		foreach( (array) $ids as $id ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 			$object = $this->get_object_by_id( $object_type, $id ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			// Only add object if we have the object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			if ( $object ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 				$objects[ $id ] = $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		return $objects; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 147 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 148 |  |  |  |