step = (string) $step; $this->run = '' !== (string) $run ? (string) $run : AIEO_Site_Migration_Journal::run_id(); return $this->run; } protected function report() { return new AIEO_Site_Migration_Report(); } protected function journal( $type, $id, $action, $before = null ) { if ( '' === $this->run ) { $this->begin( '' !== $this->step ? $this->step : 'copy' ); } return AIEO_Site_Migration_Journal::record( $this->run, $this->id(), $this->step, $type, $id, $action, $before ); } protected function remember( $key, $value ) { AIEO_Site_Migration::remember( $this->id(), (string) $key, $value ); } protected function remembered( $key, $default = null ) { return AIEO_Site_Migration::remembered( $this->id(), (string) $key, $default ); } protected function read_option( $name ) { $value = get_option( (string) $name, array() ); return is_array( $value ) ? $value : array(); } protected function write_option( $name, $value ) { update_option( (string) $name, $value ); } protected function put_option( $name, $value ) { $name = (string) $name; $absent = new stdClass(); $before = get_option( $name, $absent ); $exists = $before !== $absent; if ( $exists && $before === $value ) { return false; } $this->journal( 'option', $name, $exists ? 'update' : 'create', array( 'exists' => $exists, 'value' => $exists ? $before : null ) ); $this->write_option( $name, $value ); return true; } protected function put_option_path( $name, array $path, $value ) { $option = $this->read_option( $name ); $found = self::path_get( $option, $path, $exists ); if ( $exists && $found === $value ) { return false; } $this->journal( 'option_path', (string) $name, $exists ? 'update' : 'create', array( 'path' => array_values( $path ), 'exists' => $exists, 'value' => $exists ? $found : null ) ); self::path_set( $option, $path, $value ); $this->write_option( $name, $option ); return true; } protected static function path_get( array $array, array $path, &$exists = null ) { $node = $array; $exists = true; foreach ( $path as $key ) { if ( ! is_array( $node ) || ! array_key_exists( $key, $node ) ) { $exists = false; return null; } $node = $node[ $key ]; } return $node; } protected static function path_set( array &$array, array $path, $value ) { $node = &$array; foreach ( $path as $key ) { if ( ! is_array( $node ) ) { $node = array(); } if ( ! array_key_exists( $key, $node ) ) { $node[ $key ] = null; } $node = &$node[ $key ]; } $node = $value; } protected static function path_unset( array &$array, array $path ) { $last = array_pop( $path ); $node = &$array; foreach ( $path as $key ) { if ( ! isset( $node[ $key ] ) || ! is_array( $node[ $key ] ) ) { return; } $node = &$node[ $key ]; } unset( $node[ $last ] ); } protected function row_sets() { return array(); } protected function journal_rows_before( $name ) { global $wpdb; $set = $this->row_sets()[ $name ] ?? null; if ( ! $set || ! $this->table_exists( $set['table'] ) ) { return; } $keys = (array) $wpdb->get_col( "SELECT {$set['key']} FROM `{$set['table']}` WHERE " . ( $set['where'] ?? '1=1' ) ); $this->journal( 'rows_before', $name, 'create', array_map( 'strval', $keys ) ); } protected function max_id( $name ) { global $wpdb; $set = $this->row_sets()[ $name ] ?? null; return $set && $this->table_exists( $set['table'] ) ? (int) $wpdb->get_var( "SELECT COALESCE(MAX({$set['key']}), 0) FROM `{$set['table']}`" ) : 0; } protected function journal_rows_window( $name, $from ) { $to = $this->max_id( $name ); if ( $to > (int) $from ) { $this->journal( 'rows_window', $name, 'create', array( 'from' => (int) $from, 'to' => $to ) ); } } public function undo( array $opts ) { $report = $this->report(); $step = (string) ( $opts['step'] ?? '' ); $entries = AIEO_Site_Migration_Journal::entries( $this->id(), $step ); if ( ! $entries ) { $report->line( 'Nothing to undo: the journal holds no change of this source' . ( '' !== $step ? " for the {$step}" : '' ) . '.' ); return $report; } $done = array(); foreach ( $entries as $entry ) { if ( $this->revert( $entry, $report ) ) { $done[] = $entry['id']; } } AIEO_Site_Migration_Journal::mark_undone( $done ); $report->count( 'changes put back', count( $done ) ); if ( count( $done ) < count( $entries ) ) { $report->fail( sprintf( '%d journalled change(s) could not be put back.', count( $entries ) - count( $done ) ) ); } $this->after_undo( $step, $report ); return $report; } protected function after_undo( $step, AIEO_Site_Migration_Report $report ) {} protected function revert( array $entry, AIEO_Site_Migration_Report $report ) { $before = is_array( $entry['before'] ) ? $entry['before'] : array(); if ( 'option' === $entry['object_type'] ) { if ( empty( $before['exists'] ) ) { delete_option( $entry['object_id'] ); } else { $this->write_option( $entry['object_id'], $before['value'] ?? array() ); } return true; } if ( 'option_path' === $entry['object_type'] ) { $option = $this->read_option( $entry['object_id'] ); $path = array_values( (array) ( $before['path'] ?? array() ) ); if ( ! $path ) { return false; } if ( empty( $before['exists'] ) ) { self::path_unset( $option, $path ); } else { self::path_set( $option, $path, $before['value'] ?? null ); } $this->write_option( $entry['object_id'], $option ); return true; } if ( 'rows_before' === $entry['object_type'] || 'rows_window' === $entry['object_type'] ) { global $wpdb; $set = $this->row_sets()[ $entry['object_id'] ] ?? null; if ( ! $set || ! $this->table_exists( $set['table'] ) ) { return false; } $where = $set['where'] ?? '1=1'; if ( 'rows_window' === $entry['object_type'] ) { $removed = (int) $wpdb->query( $wpdb->prepare( "DELETE FROM `{$set['table']}` WHERE {$where} AND {$set['key']} > %d AND {$set['key']} <= %d", (int) ( $before['from'] ?? 0 ), (int) ( $before['to'] ?? 0 ) ) ); } else { $keep = array_flip( array_map( 'strval', $before ) ); $removed = 0; foreach ( (array) $wpdb->get_col( "SELECT {$set['key']} FROM `{$set['table']}` WHERE {$where}" ) as $key ) { if ( ! isset( $keep[ (string) $key ] ) ) { $removed += (int) $wpdb->query( $wpdb->prepare( "DELETE FROM `{$set['table']}` WHERE {$where} AND {$set['key']} = %s", (string) $key ) ); } } } $report->count( $entry['object_id'] . ': rows removed', $removed ); return true; } $report->warn( sprintf( 'A journalled change of kind %s (%s) is not one this source can put back.', $entry['object_type'], $entry['object_id'] ) ); return false; } protected function table_exists( $table ) { global $wpdb; return (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( (string) $table ) ) ) === (string) $table; } protected function table_rows( $table ) { global $wpdb; return $this->table_exists( $table ) ? (int) $wpdb->get_var( "SELECT COUNT(*) FROM `{$table}`" ) : -1; } protected function missing( array $names ) { $missing = array(); foreach ( $names as $name ) { if ( ! class_exists( $name ) && ! function_exists( $name ) ) { $missing[] = $name; } } return $missing; } protected function stop_unless_loaded( array $names ) { $missing = $this->missing( $names ); if ( ! $missing ) { return null; } return $this->report()->fail( 'Rooster One\'s part for this source is not loaded here: ' . implode( ', ', $missing ) . '.' ); } protected function plugins_active() { $active = (array) get_option( 'active_plugins', array() ); if ( is_multisite() ) { $active = array_merge( $active, array_keys( (array) get_site_option( 'active_sitewide_plugins', array() ) ) ); } return array_values( array_intersect( $this->plugins(), $active ) ); } protected function name_plugins( AIEO_Site_Migration_Report $report ) { foreach ( $this->plugins_active() as $file ) { $report->deactivate( $file ); } } protected static function keys( $value ) { if ( is_array( $value ) ) { $value = implode( ',', $value ); } return array_values( array_unique( array_filter( array_map( 'sanitize_key', explode( ',', (string) $value ) ), 'strlen' ) ) ); } }