| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | // POST /sites/%s/sync | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | class Jetpack_JSON_API_Sync_Endpoint extends Jetpack_JSON_API_Endpoint { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 	protected $needed_capabilities = 'manage_options'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 		parent::validate_call( $_blog_id, $capability, false ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	protected function result() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 		$args = $this->input(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 		$modules = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		// convert list of modules in comma-delimited format into an array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		// of "$modulename => true" | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 18 |  | View Code Duplication | 		if ( isset( $args['modules'] ) && ! empty( $args['modules'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 			$modules = array_map( '__return_true', array_flip( array_map( 'trim', explode( ',', $args['modules'] ) ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 		foreach ( array( 'posts', 'comments', 'users' ) as $module_name ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 			if ( 'users' === $module_name && isset( $args[ $module_name ] ) && 'initial' === $args[ $module_name ] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 				$modules[ 'users' ] = 'initial'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 			} elseif ( isset( $args[ $module_name ] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 				$ids = explode( ',', $args[ $module_name ] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 				if ( count( $ids ) > 0 ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 					$modules[ $module_name ] = $ids; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		if ( empty( $modules ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 			$modules = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		return array( 'started' => Jetpack_Sync_Actions::do_full_sync( $modules ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	protected function validate_queue( $query ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		if ( ! isset( $query ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 45 |  | View Code Duplication | 		if ( ! in_array( $query, array( 'sync', 'full_sync' ) ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		return $query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | // GET /sites/%s/sync/status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | class Jetpack_JSON_API_Sync_Status_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	protected function result() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		$sync_module = Jetpack_Sync_Modules::get_module( 'full-sync' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		$sender      = Jetpack_Sync_Sender::get_instance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		$queue       = $sender->get_sync_queue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$full_queue  = $sender->get_full_sync_queue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$cron_timestamps = array_keys( _get_cron_array() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$next_cron = $cron_timestamps[0] - time(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		return array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			$sync_module->get_status(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 				'cron_size'             => count( $cron_timestamps ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 				'next_cron'             => $next_cron, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 				'queue_size'            => $queue->size(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 				'queue_lag'             => $queue->lag(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 				'queue_next_sync'       => ( $sender->get_next_sync_time( 'sync' ) - microtime( true ) ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 				'full_queue_size'       => $full_queue->size(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 				'full_queue_lag'        => $full_queue->lag(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 				'full_queue_next_sync'  => ( $sender->get_next_sync_time( 'full_sync' ) - microtime( true ) ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | // GET /sites/%s/data-check | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | class Jetpack_JSON_API_Sync_Check_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	protected function result() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$store = new Jetpack_Sync_WP_Replicastore(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		return $store->checksum_all(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | // GET /sites/%s/data-histogram | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | class Jetpack_JSON_API_Sync_Histogram_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	protected function result() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		$args = $this->query_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		if ( isset( $args['columns'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			$columns = array_map( 'trim', explode( ',', $args['columns'] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			$columns = null; // go with defaults | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-wp-replicastore.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		$store = new Jetpack_Sync_WP_Replicastore(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		return $store->checksum_histogram( $args['object_type'], $args['buckets'], $args['start_id'], $args['end_id'], $columns, $args['strip_non_ascii'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | // POST /sites/%s/sync/settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | class Jetpack_JSON_API_Sync_Modify_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	protected function result() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		$args = $this->input(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-settings.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		$sync_settings = Jetpack_Sync_Settings::get_settings(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		foreach ( $args as $key => $value ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 			if ( $value !== false ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 				if ( is_numeric( $value ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 					$value = (int) $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 				 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 				// special case for sending empty arrays - a string with value 'empty' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 				if ( $value === 'empty' ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 					$value = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 				$sync_settings[ $key ] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		Jetpack_Sync_Settings::update_settings( $sync_settings ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		// re-fetch so we see what's really being stored | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		return Jetpack_Sync_Settings::get_settings(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | // GET /sites/%s/sync/settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | class Jetpack_JSON_API_Sync_Get_Settings_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	protected function result() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-settings.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		return Jetpack_Sync_Settings::get_settings(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | // GET /sites/%s/sync/object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | class Jetpack_JSON_API_Sync_Object extends Jetpack_JSON_API_Sync_Endpoint { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	protected function result() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		$args = $this->query_args(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 		$module_name = $args['module_name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-modules.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 		if ( ! $sync_module = Jetpack_Sync_Modules::get_module( $module_name ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 			return new WP_Error( 'invalid_module', 'You specified an invalid sync module' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		$object_type = $args['object_type']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		$object_ids  = $args['object_ids']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		$codec = Jetpack_Sync_Sender::get_instance()->get_codec(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 		Jetpack_Sync_Settings::set_is_syncing( true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		$objects = $codec->encode( $sync_module->get_objects_by_id( $object_type, $object_ids ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 		Jetpack_Sync_Settings::set_is_syncing( false ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 			'objects' => $objects, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | class Jetpack_JSON_API_Sync_Now_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	protected function result() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 		$args = $this->input(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 		$queue_name = $this->validate_queue( $args['queue'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 		if ( is_wp_error( $queue_name ) ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 			return $queue_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 		$sender = Jetpack_Sync_Sender::get_instance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 		$response = $sender->do_sync_for_queue( new Jetpack_Sync_Queue( $args['queue'] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 		return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 			'response' => $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | class Jetpack_JSON_API_Sync_Checkout_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 	protected function result() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 		$args = $this->input(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 		$queue_name = $this->validate_queue( $args['queue'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 		if ( is_wp_error( $queue_name ) ){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 			return $queue_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  | 		if ( $args[ 'number_of_items' ] < 1 || $args[ 'number_of_items' ] > 100  ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 			return new WP_Error( 'invalid_number_of_items', 'Number of items needs to be an integer that is larger than 0 and less then 100', 400 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  | 		$queue = new Jetpack_Sync_Queue( $queue_name ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  | 		if ( 0 === $queue->size() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  | 			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  | 		$sender = Jetpack_Sync_Sender::get_instance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  | 		// try to give ourselves as much time as possible | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  | 		set_time_limit( 0 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  | 		// let's delete the checkin state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  | 		if ( $args['force'] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  | 			$queue->unlock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  | 		$buffer = $this->get_buffer( $queue, $args[ 'number_of_items' ] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  | 		// Check that the $buffer is not checkout out already | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  | 		if ( is_wp_error( $buffer ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  | 			return new WP_Error( 'buffer_open', "We couldn't get the buffer it is currently checked out", 400 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  | 		if ( ! is_object( $buffer ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  | 			return new WP_Error( 'buffer_non-object', 'Buffer is not an object', 400 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  | 		Jetpack_Sync_Settings::set_is_syncing( true ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  | 		list( $items_to_send, $skipped_items_ids, $items ) = $sender->get_items_to_send( $buffer, $args['encode'] ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  | 		Jetpack_Sync_Settings::set_is_syncing( false ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  | 		return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  | 			'buffer_id'      => $buffer->id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  | 			'items'          => $items_to_send, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  | 			'skipped_items'  => $skipped_items_ids, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  | 			'codec'          => $args['encode'] ? $sender->get_codec()->name() : null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  | 			'sent_timestamp' => time(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  | 	protected function get_buffer( $queue, $number_of_items ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  | 		$start = time(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  | 		$max_duration = 5; // this will try to get the buffer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  | 		$buffer = $queue->checkout( $number_of_items ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  | 		$duration = time() - $start; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  | 		while( is_wp_error( $buffer ) && $duration < $max_duration ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  | 			sleep( 2 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  | 			$duration = time() - $start; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  | 			$buffer = $queue->checkout( $number_of_items ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  | 		if ( $buffer === false ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  | 			return new WP_Error( 'queue_size', 'The queue is empty and there is nothing to send', 400 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  | 		return $buffer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  | class Jetpack_JSON_API_Sync_Close_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  | 	protected function result() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  | 		$request_body = $this->input(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  | 		$queue_name = $this->validate_queue( $request_body['queue'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  | 		if ( is_wp_error( $queue_name ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  | 			return $queue_name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  | 		if ( ! isset( $request_body['buffer_id'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  | 			return new WP_Error( 'missing_buffer_id', 'Please provide a buffer id', 400 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  | 		if ( ! isset( $request_body['item_ids'] ) || ! is_array( $request_body['item_ids'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  | 			return new WP_Error( 'missing_item_ids', 'Please provide a list of item ids in the item_ids argument', 400 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  | 		//Limit to A-Z,a-z,0-9,_,- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  | 		$request_body ['buffer_id'] = preg_replace( '/[^A-Za-z0-9]/', '', $request_body['buffer_id'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  | 		$request_body['item_ids'] = array_filter( array_map( array( 'Jetpack_JSON_API_Sync_Close_Endpoint', 'sanitize_item_ids' ), $request_body['item_ids'] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  | 		$buffer = new Jetpack_Sync_Queue_Buffer( $request_body['buffer_id'], $request_body['item_ids'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  | 		$queue = new Jetpack_Sync_Queue( $queue_name ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  | 		$response = $queue->close( $buffer, $request_body['item_ids'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  | 		if ( is_wp_error( $response ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  | 			return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  | 		return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  | 			'success' => $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  | 	protected static function sanitize_item_ids( $item ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  | 		// lets not delete any options that don't start with jpsq_sync- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  | 		if ( substr( $item, 0, 5 ) !== 'jpsq_' ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  | 			return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  | 		//Limit to A-Z,a-z,0-9,_,-,. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  | 		return preg_replace( '/[^A-Za-z0-9-_.]/', '', $item ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  | class Jetpack_JSON_API_Sync_Unlock_Endpoint extends Jetpack_JSON_API_Sync_Endpoint { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  | 	protected function result() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  | 		$args = $this->input(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  | 		if ( ! isset( $args['queue'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  | 			return new WP_Error( 'invalid_queue', 'Queue name is required', 400 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 327 |  | View Code Duplication | 		if ( ! in_array( $args['queue'], array( 'sync', 'full_sync' ) ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  | 			return new WP_Error( 'invalid_queue', 'Queue name should be sync or full_sync', 400 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  | 		require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-queue.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  | 		$queue = new Jetpack_Sync_Queue( $args['queue'] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  | 		// False means that there was no lock to delete. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  | 		$response = $queue->unlock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  | 		return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  | 			'success' => $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 340 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 341 |  |  |  | 
            
                        
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.